@typeberry/jam 0.0.4 → 0.0.5

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/importer/index.js CHANGED
@@ -25330,6 +25330,8 @@ class Importer {
25330
25330
  stf;
25331
25331
  // TODO [ToDr] we cannot assume state reference does not change.
25332
25332
  state;
25333
+ // Hash of the block that we have the posterior state for in `state`.
25334
+ currentHash;
25333
25335
  constructor(spec, hasher, logger, blocks, states) {
25334
25336
  this.logger = logger;
25335
25337
  this.blocks = blocks;
@@ -25342,6 +25344,7 @@ class Importer {
25342
25344
  this.verifier = new BlockVerifier(hasher, blocks);
25343
25345
  this.stf = new OnChain(spec, state, blocks, hasher, { enableParallelSealVerification: true });
25344
25346
  this.state = state;
25347
+ this.currentHash = currentBestHeaderHash;
25345
25348
  logger.info(`😎 Best time slot: ${state.timeslot} (header hash: ${currentBestHeaderHash})`);
25346
25349
  }
25347
25350
  /** Attempt to pre-verify the seal to speed up importing. */
@@ -25368,6 +25371,20 @@ class Importer {
25368
25371
  if (hash.isError) {
25369
25372
  return importerError(ImporterErrorKind.Verifier, hash);
25370
25373
  }
25374
+ // TODO [ToDr] This is incomplete/temporary fork support!
25375
+ const parentHash = block.header.view().parentHeaderHash.materialize();
25376
+ if (!this.currentHash.isEqualTo(parentHash)) {
25377
+ const state = this.states.getState(parentHash);
25378
+ if (state === null) {
25379
+ const e = result_Result.error(BlockVerifierError.StateRootNotFound);
25380
+ if (!e.isError) {
25381
+ throw new Error("unreachable, just adding to make compiler happy");
25382
+ }
25383
+ return importerError(ImporterErrorKind.Verifier, e);
25384
+ }
25385
+ this.state.updateBackend(state?.backend);
25386
+ this.currentHash = parentHash;
25387
+ }
25371
25388
  const timeSlot = block.header.view().timeSlotIndex.materialize();
25372
25389
  const headerHash = hash.ok;
25373
25390
  logger.log(`🧱 Verified block: Got hash ${headerHash} for block at slot ${timeSlot}.`);
@@ -25392,6 +25409,7 @@ class Importer {
25392
25409
  // TODO [ToDr] This is a temporary measure. We should rather read
25393
25410
  // the state of a parent block to support forks and create a fresh STF.
25394
25411
  this.state.updateBackend(newState.backend);
25412
+ this.currentHash = headerHash;
25395
25413
  logger.log(timerState());
25396
25414
  // insert new state and the block to DB.
25397
25415
  const timerDb = measure("import:db");