@typeberry/jam 0.5.1-31343a9 → 0.5.1-45102d3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -152591,12 +152591,7 @@ function getDatabasePath(blake2b, nodeName, genesisHeader, databaseBasePath) {
152591
152591
  genesisHeaderHash,
152592
152592
  };
152593
152593
  }
152594
- /**
152595
- * Initialize the database unless it's already initialized.
152596
- *
152597
- * The function checks the genesis header
152598
- */
152599
- async function initializeDatabase(spec, blake2b, genesisHeaderHash, rootDb, config, ancestry) {
152594
+ async function initializeDatabase(spec, blake2b, genesisHeaderHash, rootDb, config, ancestry, options = {}) {
152600
152595
  const blocks = rootDb.getBlocksDb();
152601
152596
  const states = rootDb.getStatesDb();
152602
152597
  const header = blocks.getBestHeaderHash();
@@ -152617,14 +152612,16 @@ async function initializeDatabase(spec, blake2b, genesisHeaderHash, rootDb, conf
152617
152612
  common_logger.log `🧬 Writing genesis block #${genesisHeader.timeSlotIndex}: ${genesisHeaderHash}`;
152618
152613
  const { genesisStateSerialized, genesisStateRootHash } = loadGenesisState(spec, blake2b, config.genesisState);
152619
152614
  // write to db
152620
- await blocks.insertBlock(new WithHash(genesisHeaderHash, blockView));
152615
+ // When initGenesisFromAncestry is set, use ancestry[0][0] as the initial block hash (for fuzz-target mode)
152616
+ const initialBlockHash = (options.initGenesisFromAncestry ?? false) && ancestry.length > 0 ? ancestry[0][0] : genesisHeaderHash;
152617
+ await blocks.insertBlock(new WithHash(initialBlockHash, blockView));
152621
152618
  // insert fake blocks for ancestry data
152622
152619
  for (const [hash, slot] of ancestry) {
152623
152620
  await blocks.insertBlock(new WithHash(hash, reencodeAsView(Block.Codec, emptyBlock(slot), spec)));
152624
152621
  }
152625
- await states.insertInitialState(genesisHeaderHash, genesisStateSerialized);
152626
- await blocks.setPostStateRoot(genesisHeaderHash, genesisStateRootHash);
152627
- await blocks.setBestHeaderHash(genesisHeaderHash);
152622
+ await states.insertInitialState(initialBlockHash, genesisStateSerialized);
152623
+ await blocks.setPostStateRoot(initialBlockHash, genesisStateRootHash);
152624
+ await blocks.setBestHeaderHash(initialBlockHash);
152628
152625
  }
152629
152626
  function loadGenesisState(spec, blake2b, data) {
152630
152627
  const stateEntries = state_entries_StateEntries.fromEntriesUnsafe(data.entries());
@@ -166611,6 +166608,7 @@ class Importer {
166611
166608
  logger;
166612
166609
  blocks;
166613
166610
  states;
166611
+ options;
166614
166612
  verifier;
166615
166613
  stf;
166616
166614
  // TODO [ToDr] we cannot assume state reference does not change.
@@ -166618,11 +166616,12 @@ class Importer {
166618
166616
  // Hash of the block that we have the posterior state for in `state`.
166619
166617
  currentHash;
166620
166618
  metrics;
166621
- constructor(spec, pvm, hasher, logger, blocks, states) {
166619
+ constructor(spec, pvm, hasher, logger, blocks, states, options = {}) {
166622
166620
  this.hasher = hasher;
166623
166621
  this.logger = logger;
166624
166622
  this.blocks = blocks;
166625
166623
  this.states = states;
166624
+ this.options = options;
166626
166625
  this.metrics = metrics_createMetrics();
166627
166626
  const currentBestHeaderHash = this.blocks.getBestHeaderHash();
166628
166627
  const state = states.getState(currentBestHeaderHash);
@@ -166677,7 +166676,9 @@ class Importer {
166677
166676
  logger.log `🧱 Attempting to import a new block`;
166678
166677
  const timerVerify = measure("import:verify");
166679
166678
  const verifyStart = now();
166680
- const hash = await this.verifier.verifyBlock(block);
166679
+ const hash = await this.verifier.verifyBlock(block, {
166680
+ skipParentAndStateRoot: this.options.initGenesisFromAncestry ?? false,
166681
+ });
166681
166682
  const verifyDuration = now() - verifyStart;
166682
166683
  logger.log `${timerVerify()}`;
166683
166684
  if (hash.isError) {
@@ -166780,14 +166781,14 @@ function extractTimeSlot(block) {
166780
166781
  const main_logger = logger_Logger.new(import.meta.filename, "importer");
166781
166782
  const keccakHasher = KeccakHasher.create();
166782
166783
  const blake2b = blake2b_Blake2b.createHasher();
166783
- async function createImporter(config) {
166784
+ async function createImporter(config, options = {}) {
166784
166785
  const chainSpec = config.chainSpec;
166785
166786
  const db = config.openDatabase({ readonly: false });
166786
166787
  const pvm = config.workerParams.pvm;
166787
166788
  const blocks = db.getBlocksDb();
166788
166789
  const states = db.getStatesDb();
166789
166790
  const hasher = new TransitionHasher(await keccakHasher, await blake2b);
166790
- const importer = new Importer(chainSpec, pvm, hasher, main_logger, blocks, states);
166791
+ const importer = new Importer(chainSpec, pvm, hasher, main_logger, blocks, states, options);
166791
166792
  return {
166792
166793
  importer,
166793
166794
  db,
@@ -170123,7 +170124,7 @@ const initNetwork = async (importer, rootDb, baseConfig, genesisHeaderHash, netw
170123
170124
 
170124
170125
 
170125
170126
  const zeroHash = bytes_Bytes.zero(hash_HASH_SIZE).asOpaque();
170126
- async function mainImporter(config, withRelPath) {
170127
+ async function mainImporter(config, withRelPath, options = {}) {
170127
170128
  await initAll();
170128
170129
  common_logger.info `🫐 Typeberry ${node_package_namespaceObject.rE}. GP: ${CURRENT_VERSION} (${CURRENT_SUITE})`;
170129
170130
  common_logger.info `🎸 Starting importer: ${config.nodeName}.`;
@@ -170153,9 +170154,13 @@ async function mainImporter(config, withRelPath) {
170153
170154
  // Initialize the database with genesis state and block if there isn't one.
170154
170155
  common_logger.info `🛢️ Opening database at ${dbPath}`;
170155
170156
  const rootDb = workerConfig.openDatabase({ readonly: false });
170156
- await initializeDatabase(chainSpec, blake2b, genesisHeaderHash, rootDb, config.node.chainSpec, config.ancestry);
170157
+ await initializeDatabase(chainSpec, blake2b, genesisHeaderHash, rootDb, config.node.chainSpec, config.ancestry, {
170158
+ initGenesisFromAncestry: options.initGenesisFromAncestry,
170159
+ });
170157
170160
  await rootDb.close();
170158
- const { db, importer } = await createImporter(workerConfig);
170161
+ const { db, importer } = await createImporter(workerConfig, {
170162
+ initGenesisFromAncestry: options.initGenesisFromAncestry,
170163
+ });
170159
170164
  await importer.prepareForNextEpoch();
170160
170165
  const api = {
170161
170166
  chainSpec,
@@ -170254,7 +170259,7 @@ async function mainFuzz(fuzzConfig, withRelPath) {
170254
170259
  },
170255
170260
  ancestry,
170256
170261
  network: null,
170257
- }, withRelPath);
170262
+ }, withRelPath, { initGenesisFromAncestry: fuzzConfig.initGenesisFromAncestry });
170258
170263
  runningNode = newNode;
170259
170264
  return await newNode.getBestStateRootHash();
170260
170265
  },
@@ -170486,6 +170491,7 @@ const jam_package_namespaceObject = {"rE":"0.5.1"};
170486
170491
 
170487
170492
 
170488
170493
 
170494
+
170489
170495
  const HELP = `
170490
170496
  @typeberry/jam ${jam_package_namespaceObject.rE} by Fluffy Labs.
170491
170497
 
@@ -170574,6 +170580,11 @@ function parseArgs(input, withRelPath) {
170574
170580
  case Command.FuzzTarget: {
170575
170581
  const data = parseSharedOptions(args);
170576
170582
  const { version } = parseValueOption(args, "version", "number", parseFuzzVersion, 1);
170583
+ const initGenesisFromAncestry = args["init-genesis-from-ancestry"] === true;
170584
+ delete args["init-genesis-from-ancestry"];
170585
+ if (initGenesisFromAncestry) {
170586
+ common_logger.warn `Init genesis from ancestry is enabled. Parent hash and state root verification is skipped.`;
170587
+ }
170577
170588
  const socket = args._.shift() ?? null;
170578
170589
  assertNoMoreArgs(args);
170579
170590
  return {
@@ -170582,6 +170593,7 @@ function parseArgs(input, withRelPath) {
170582
170593
  ...data,
170583
170594
  version,
170584
170595
  socket,
170596
+ initGenesisFromAncestry,
170585
170597
  },
170586
170598
  };
170587
170599
  }
@@ -170781,7 +170793,8 @@ async function startNode(args, withRelPath) {
170781
170793
  if (args.command === Command.FuzzTarget) {
170782
170794
  const version = args.args.version;
170783
170795
  const socket = args.args.socket;
170784
- return mainFuzz({ jamNodeConfig, version, socket }, withRelPath);
170796
+ const initGenesisFromAncestry = args.args.initGenesisFromAncestry;
170797
+ return mainFuzz({ jamNodeConfig, version, socket, initGenesisFromAncestry }, withRelPath);
170785
170798
  }
170786
170799
  // Just import a bunch of blocks
170787
170800
  if (args.command === Command.Import) {