@typeberry/jam 0.4.1-9e565b9 → 0.4.1-f776cce

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
@@ -28434,9 +28434,438 @@ class ArrayView {
28434
28434
  }
28435
28435
  }
28436
28436
 
28437
+ ;// CONCATENATED MODULE: ./packages/core/collections/blob-dictionary.ts
28438
+
28439
+
28440
+ /** A map which uses byte blobs as keys */
28441
+ class BlobDictionary extends WithDebug {
28442
+ mapNodeThreshold;
28443
+ /**
28444
+ * The root node of the dictionary.
28445
+ *
28446
+ * This is the main internal data structure that organizes entries
28447
+ * in a tree-like fashion (array-based nodes up to `mapNodeThreshold`,
28448
+ * map-based nodes beyond it). All insertions, updates, and deletions
28449
+ * operate through this structure.
28450
+ */
28451
+ root = Node.withList();
28452
+ /**
28453
+ * Auxiliary map that stores references to the original keys and their values.
28454
+ *
28455
+ * - Overriding a value in the main structure does not replace the original key reference.
28456
+ * - Used for efficient iteration over `keys()`, `values()`, `entries()`, and computing `size`.
28457
+ */
28458
+ keyvals = new Map();
28459
+ /**
28460
+ * Protected constructor used internally by `BlobDictionary.new`
28461
+ * and `BlobDictionary.fromEntries`.
28462
+ *
28463
+ * This enforces controlled instantiation — users should create instances
28464
+ * through the provided static factory methods instead of calling the
28465
+ * constructor directly.
28466
+ *
28467
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
28468
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
28469
+ */
28470
+ constructor(mapNodeThreshold) {
28471
+ super();
28472
+ this.mapNodeThreshold = mapNodeThreshold;
28473
+ }
28474
+ /**
28475
+ * Returns the number of entries in the dictionary.
28476
+ *
28477
+ * The count is derived from the auxiliary `keyvals` map, which stores
28478
+ * all original key references and their associated values. This ensures
28479
+ * that the `size` reflects the actual number of entries, independent of
28480
+ * internal overrides in the main `root` structure.
28481
+ *
28482
+ * @returns The total number of entries in the dictionary.
28483
+ */
28484
+ get size() {
28485
+ return this.keyvals.size;
28486
+ }
28487
+ [TEST_COMPARE_USING]() {
28488
+ const vals = Array.from(this);
28489
+ vals.sort((a, b) => a[0].compare(b[0]).value);
28490
+ return vals;
28491
+ }
28492
+ /**
28493
+ * Creates an empty `BlobDictionary`.
28494
+ *
28495
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
28496
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
28497
+ * Defaults to `0`.
28498
+ *
28499
+ * @returns A new, empty `BlobDictionary` instance.
28500
+ */
28501
+ static new(mapNodeThreshold = 0) {
28502
+ return new BlobDictionary(mapNodeThreshold);
28503
+ }
28504
+ /**
28505
+ * Creates a new `BlobDictionary` initialized with the given entries.
28506
+ *
28507
+ * @param entries - An array of `[key, value]` pairs used to populate the dictionary.
28508
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
28509
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
28510
+ * Defaults to `0`.
28511
+ *
28512
+ * @returns A new `BlobDictionary` containing the provided entries.
28513
+ */
28514
+ static fromEntries(entries, mapNodeThreshold) {
28515
+ const dict = BlobDictionary.new(mapNodeThreshold);
28516
+ for (const [key, value] of entries) {
28517
+ dict.set(key, value);
28518
+ }
28519
+ return dict;
28520
+ }
28521
+ /**
28522
+ * Internal helper that inserts, updates or deletes an entry in the dictionary.
28523
+ *
28524
+ * Behaviour details:
28525
+ * - Passing `undefined` as `value` indicates a deletion. (E.g. `delete` uses `internalSet(key, undefined)`.)
28526
+ * - When an add (new entry) or a delete actually changes the structure, the method returns the affected leaf node.
28527
+ * - When the call only overrides an existing value (no structural add/delete), the method returns `null`.
28528
+ *
28529
+ * This method is intended for internal use by the dictionary implementation and allows `undefined` as a
28530
+ * sentinel value to signal removals.
28531
+ *
28532
+ * @param key - The key to insert, update or remove.
28533
+ * @param value - The value to associate with the key, or `undefined` to remove the key.
28534
+ * @returns The leaf node created or removed on add/delete, or `null` if the operation only overwrote an existing value.
28535
+ */
28536
+ internalSet(key, value) {
28537
+ let node = this.root;
28538
+ const keyChunkGenerator = key.chunks(CHUNK_SIZE);
28539
+ let depth = 0;
28540
+ for (;;) {
28541
+ const maybeKeyChunk = keyChunkGenerator.next().value;
28542
+ if (maybeKeyChunk === undefined) {
28543
+ if (value === undefined) {
28544
+ return node.remove(key);
28545
+ }
28546
+ return node.set(key, value);
28547
+ }
28548
+ const keyChunk = opaque_asOpaqueType(maybeKeyChunk);
28549
+ if (node.children instanceof ListChildren) {
28550
+ const subkey = bytes_BytesBlob.blobFrom(key.raw.subarray(CHUNK_SIZE * depth));
28551
+ const leaf = value !== undefined ? node.children.insert(subkey, { key, value }) : node.children.remove(subkey);
28552
+ if (subkey.length > CHUNK_SIZE && node.children.children.length > this.mapNodeThreshold) {
28553
+ node.convertListChildrenToMap();
28554
+ }
28555
+ return leaf;
28556
+ }
28557
+ depth += 1;
28558
+ const children = node.children;
28559
+ if (children instanceof ListChildren) {
28560
+ throw new Error("We handle list node earlier. If we fall through, we know it's for the `Map` case.");
28561
+ }
28562
+ if (children instanceof MapChildren) {
28563
+ const maybeNode = children.getChild(keyChunk);
28564
+ if (maybeNode !== undefined) {
28565
+ // simply go one level deeper
28566
+ node = maybeNode;
28567
+ }
28568
+ else {
28569
+ // we are trying to remove an item, but it does not exist
28570
+ if (value === undefined) {
28571
+ return null;
28572
+ }
28573
+ // no more child nodes, we insert a new one.
28574
+ const newNode = Node.withList();
28575
+ children.setChild(keyChunk, newNode);
28576
+ node = newNode;
28577
+ }
28578
+ continue;
28579
+ }
28580
+ debug_assertNever(children);
28581
+ }
28582
+ }
28583
+ /**
28584
+ * Adds a new entry to the dictionary or updates the value of an existing key.
28585
+ *
28586
+ * If an entry with the given key already exists, its value is replaced
28587
+ * with the new one.
28588
+ *
28589
+ * @param key - The key to add or update in the dictionary.
28590
+ * @param value - The value to associate with the specified key.
28591
+ * @returns Nothing (`void`).
28592
+ */
28593
+ set(key, value) {
28594
+ const leaf = this.internalSet(key, value);
28595
+ if (leaf !== null) {
28596
+ this.keyvals.set(leaf.key, leaf);
28597
+ }
28598
+ }
28599
+ /**
28600
+ * Retrieves the value associated with the given key from the dictionary.
28601
+ *
28602
+ * If the key does not exist, this method returns `undefined`.
28603
+ *
28604
+ * @param key - The key whose associated value should be retrieved.
28605
+ * @returns The value associated with the specified key, or `undefined` if the key is not present.
28606
+ */
28607
+ get(key) {
28608
+ let node = this.root;
28609
+ const pathChunksGenerator = key.chunks(CHUNK_SIZE);
28610
+ let depth = 0;
28611
+ while (node !== undefined) {
28612
+ const maybePathChunk = pathChunksGenerator.next().value;
28613
+ if (node.children instanceof ListChildren) {
28614
+ const subkey = bytes_BytesBlob.blobFrom(key.raw.subarray(depth * CHUNK_SIZE));
28615
+ const child = node.children.find(subkey);
28616
+ if (child !== null) {
28617
+ return child.value;
28618
+ }
28619
+ }
28620
+ if (maybePathChunk === undefined) {
28621
+ return node.getLeaf()?.value;
28622
+ }
28623
+ if (node.children instanceof MapChildren) {
28624
+ const pathChunk = opaque_asOpaqueType(maybePathChunk);
28625
+ node = node.children.getChild(pathChunk);
28626
+ depth += 1;
28627
+ }
28628
+ }
28629
+ return undefined;
28630
+ }
28631
+ /**
28632
+ * Checks whether the dictionary contains an entry for the given key.
28633
+ *
28634
+ * ⚠️ **Note:** Avoid using `has(...)` together with `get(...)` in a pattern like this:
28635
+ *
28636
+ * ```ts
28637
+ * if (dict.has(key)) {
28638
+ * const value = dict.get(key);
28639
+ * ...
28640
+ * }
28641
+ * ```
28642
+ *
28643
+ * This approach performs two lookups for the same key.
28644
+ *
28645
+ * Instead, prefer the following pattern, which retrieves the value once:
28646
+ *
28647
+ * ```ts
28648
+ * const value = dict.get(key);
28649
+ * if (value !== undefined) {
28650
+ * ...
28651
+ * }
28652
+ * ```
28653
+ *
28654
+ * @param key - The key to check for.
28655
+ * @returns `true` if the dictionary contains an entry for the given key, otherwise `false`.
28656
+ */
28657
+ has(key) {
28658
+ return this.get(key) !== undefined;
28659
+ }
28660
+ /**
28661
+ * Removes an entry with the specified key from the dictionary.
28662
+ *
28663
+ * Internally, this calls {@link internalSet} with `undefined` to mark the entry as deleted.
28664
+ *
28665
+ * @param key - The key of the entry to remove.
28666
+ * @returns `true` if an entry was removed (i.e. the key existed), otherwise `false`.
28667
+ */
28668
+ delete(key) {
28669
+ const leaf = this.internalSet(key, undefined);
28670
+ if (leaf !== null) {
28671
+ this.keyvals.delete(leaf.key);
28672
+ return true;
28673
+ }
28674
+ return false;
28675
+ }
28676
+ /**
28677
+ * Returns an iterator over the keys in the dictionary.
28678
+ *
28679
+ * The iterator yields each key in insertion order.
28680
+ *
28681
+ * @returns An iterator over all keys in the dictionary.
28682
+ */
28683
+ keys() {
28684
+ return this.keyvals.keys();
28685
+ }
28686
+ /**
28687
+ * Returns an iterator over the values in the dictionary.
28688
+ *
28689
+ * The iterator yields each value in insertion order.
28690
+ *
28691
+ * @returns An iterator over all values in the dictionary.
28692
+ */
28693
+ *values() {
28694
+ for (const leaf of this.keyvals.values()) {
28695
+ yield leaf.value;
28696
+ }
28697
+ }
28698
+ /**
28699
+ * Returns an iterator over the `[key, value]` pairs in the dictionary.
28700
+ *
28701
+ * The iterator yields entries in insertion order.
28702
+ *
28703
+ * @returns An iterator over `[key, value]` tuples for each entry in the dictionary.
28704
+ */
28705
+ *entries() {
28706
+ for (const leaf of this.keyvals.values()) {
28707
+ yield [leaf.key, leaf.value];
28708
+ }
28709
+ }
28710
+ /**
28711
+ * Default iterator for the dictionary.
28712
+ *
28713
+ * Equivalent to calling {@link entries}.
28714
+ * Enables iteration with `for...of`:
28715
+ *
28716
+ * ```ts
28717
+ * for (const [key, value] of dict) {
28718
+ * ...
28719
+ * }
28720
+ * ```
28721
+ *
28722
+ * @returns An iterator over `[key, value]` pairs.
28723
+ */
28724
+ [Symbol.iterator]() {
28725
+ return this.entries();
28726
+ }
28727
+ /**
28728
+ * Creates a new sorted array of values, ordered by their corresponding keys.
28729
+ *
28730
+ * Iterates over all entries in the dictionary and sorts them according
28731
+ * to the provided comparator function applied to the keys.
28732
+ *
28733
+ * @param comparator - A comparator function that can compare two keys.
28734
+ *
28735
+ * @returns A new array containing all values from the dictionary,
28736
+ * sorted according to their keys.
28737
+ */
28738
+ toSortedArray(comparator) {
28739
+ const vals = Array.from(this);
28740
+ vals.sort((a, b) => comparator(a[0], b[0]).value);
28741
+ return vals.map((x) => x[1]);
28742
+ }
28743
+ }
28744
+ const CHUNK_SIZE = 6;
28745
+ /**
28746
+ * A function to transform a bytes chunk (up to 6 bytes into U48 number)
28747
+ *
28748
+ * Note that it uses 3 additional bits to store length(`value * 8 + len;`),
28749
+ * It is needed to distinguish shorter chunks that have 0s at the end, for example: [1, 2] and [1, 2, 0]
28750
+ * */
28751
+ function bytesAsU48(bytes) {
28752
+ const len = bytes.length;
28753
+ debug_check `${len <= CHUNK_SIZE} Length has to be <= ${CHUNK_SIZE}, got: ${len}`;
28754
+ let value = bytes[3] | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
28755
+ for (let i = 4; i < bytes.length; i++) {
28756
+ value = value * 256 + bytes[i];
28757
+ }
28758
+ return value * 8 + len;
28759
+ }
28760
+ class Node {
28761
+ leaf;
28762
+ children;
28763
+ convertListChildrenToMap() {
28764
+ if (!(this.children instanceof ListChildren)) {
28765
+ return;
28766
+ }
28767
+ this.children = MapChildren.fromListNode(this.children);
28768
+ }
28769
+ static withList() {
28770
+ return new Node(undefined, ListChildren.new());
28771
+ }
28772
+ static withMap() {
28773
+ return new Node(undefined, MapChildren.new());
28774
+ }
28775
+ constructor(leaf, children) {
28776
+ this.leaf = leaf;
28777
+ this.children = children;
28778
+ }
28779
+ getLeaf() {
28780
+ return this.leaf;
28781
+ }
28782
+ remove(_key) {
28783
+ if (this.leaf === undefined) {
28784
+ return null;
28785
+ }
28786
+ const removedLeaf = this.leaf;
28787
+ this.leaf = undefined;
28788
+ return removedLeaf;
28789
+ }
28790
+ set(key, value) {
28791
+ if (this.leaf === undefined) {
28792
+ this.leaf = { key, value };
28793
+ return this.leaf;
28794
+ }
28795
+ this.leaf.value = value;
28796
+ return null;
28797
+ }
28798
+ }
28799
+ class ListChildren {
28800
+ children = [];
28801
+ constructor() { }
28802
+ find(key) {
28803
+ const result = this.children.find((item) => item[0].isEqualTo(key));
28804
+ if (result !== undefined) {
28805
+ return result[1];
28806
+ }
28807
+ return null;
28808
+ }
28809
+ remove(key) {
28810
+ const existingIndex = this.children.findIndex((item) => item[0].isEqualTo(key));
28811
+ if (existingIndex >= 0) {
28812
+ const ret = this.children.splice(existingIndex, 1);
28813
+ return ret[0][1];
28814
+ }
28815
+ return null;
28816
+ }
28817
+ insert(key, leaf) {
28818
+ const existingIndex = this.children.findIndex((item) => item[0].isEqualTo(key));
28819
+ if (existingIndex >= 0) {
28820
+ const existing = this.children[existingIndex];
28821
+ existing[1].value = leaf.value;
28822
+ return null;
28823
+ }
28824
+ this.children.push([key, leaf]);
28825
+ return leaf;
28826
+ }
28827
+ static new() {
28828
+ return new ListChildren();
28829
+ }
28830
+ }
28831
+ class MapChildren {
28832
+ children = new Map();
28833
+ constructor() { }
28834
+ static new() {
28835
+ return new MapChildren();
28836
+ }
28837
+ static fromListNode(node) {
28838
+ const mapNode = new MapChildren();
28839
+ for (const [key, leaf] of node.children) {
28840
+ const currentKeyChunk = opaque_asOpaqueType(bytes_BytesBlob.blobFrom(key.raw.subarray(0, CHUNK_SIZE)));
28841
+ const subKey = bytes_BytesBlob.blobFrom(key.raw.subarray(CHUNK_SIZE));
28842
+ let child = mapNode.getChild(currentKeyChunk);
28843
+ if (child === undefined) {
28844
+ child = Node.withList();
28845
+ mapNode.setChild(currentKeyChunk, child);
28846
+ }
28847
+ const children = child.children;
28848
+ children.insert(subKey, leaf);
28849
+ }
28850
+ return mapNode;
28851
+ }
28852
+ getChild(keyChunk) {
28853
+ const chunkAsNumber = bytesAsU48(keyChunk.raw);
28854
+ return this.children.get(chunkAsNumber);
28855
+ }
28856
+ setChild(keyChunk, node) {
28857
+ const chunkAsNumber = bytesAsU48(keyChunk.raw);
28858
+ this.children.set(chunkAsNumber, node);
28859
+ }
28860
+ }
28861
+
28437
28862
  ;// CONCATENATED MODULE: ./packages/core/collections/hash-dictionary.ts
28438
- /** A map which uses hashes as keys. */
28439
- class hash_dictionary_HashDictionary {
28863
+ /**
28864
+ * A map which uses hashes as keys.
28865
+ *
28866
+ * @deprecated
28867
+ * */
28868
+ class StringHashDictionary {
28440
28869
  // TODO [ToDr] [crit] We can't use `TrieHash` directly in the map,
28441
28870
  // because of the way it's being compared. Hence having `string` here.
28442
28871
  // This has to be benchmarked and re-written to a custom map most likely.
@@ -28502,6 +28931,17 @@ class hash_dictionary_HashDictionary {
28502
28931
  }
28503
28932
  }
28504
28933
 
28934
+ /**
28935
+ * A value that indicates when `BlobDictionary` transforms Array nodes into Map nodes.
28936
+ * In practice, it doesn't matter much because, in real life, arrays in this structure usually have a length close to 1.
28937
+ */
28938
+ const BLOB_DICTIONARY_THRESHOLD = 5;
28939
+ class hash_dictionary_HashDictionary extends BlobDictionary {
28940
+ constructor() {
28941
+ super(BLOB_DICTIONARY_THRESHOLD);
28942
+ }
28943
+ }
28944
+
28505
28945
  ;// CONCATENATED MODULE: ./packages/core/collections/hash-set.ts
28506
28946
 
28507
28947
  /** A set specialized for storing hashes. */
@@ -28966,6 +29406,18 @@ class SortedSet extends SortedArray {
28966
29406
 
28967
29407
 
28968
29408
 
29409
+ function getTruncatedKey(key) {
29410
+ // Always return exactly TRUNCATED_HASH_SIZE bytes.
29411
+ if (key.length === TRUNCATED_HASH_SIZE) {
29412
+ return key;
29413
+ }
29414
+ return bytes_Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_HASH_SIZE), TRUNCATED_HASH_SIZE);
29415
+ }
29416
+ /**
29417
+ * A value that indicates when `BlobDictionary` transforms Array nodes into Map nodes.
29418
+ * In practice, it doesn't matter much because, in real life, arrays in this structure usually have a length close to 1.
29419
+ */
29420
+ const truncated_hash_dictionary_BLOB_DICTIONARY_THRESHOLD = 5;
28969
29421
  /**
28970
29422
  * A collection of hash-based keys (likely `StateKey`s) which ignores
28971
29423
  * differences on the last byte.
@@ -28978,48 +29430,37 @@ class TruncatedHashDictionary {
28978
29430
  * Each key will be copied and have the last byte replace with a 0.
28979
29431
  */
28980
29432
  static fromEntries(entries) {
28981
- /** Copy key bytes of an entry and replace the last one with 0. */
28982
- const mapped = Array.from(entries).map(([key, value]) => {
28983
- const newKey = bytes_Bytes.zero(hash_HASH_SIZE).asOpaque();
28984
- newKey.raw.set(key.raw.subarray(0, TRUNCATED_HASH_SIZE));
28985
- return [newKey, value];
28986
- });
28987
- return new TruncatedHashDictionary(hash_dictionary_HashDictionary.fromEntries(mapped));
29433
+ return new TruncatedHashDictionary(BlobDictionary.fromEntries(Array.from(entries).map(([key, value]) => [getTruncatedKey(key), value]), truncated_hash_dictionary_BLOB_DICTIONARY_THRESHOLD));
28988
29434
  }
28989
- /** A truncated key which we re-use to query the dictionary. */
28990
- truncatedKey = bytes_Bytes.zero(hash_HASH_SIZE).asOpaque();
28991
29435
  constructor(dict) {
28992
29436
  this.dict = dict;
28993
29437
  }
28994
29438
  [TEST_COMPARE_USING]() {
28995
- return this.dict;
29439
+ return Array.from(this.dict);
28996
29440
  }
28997
29441
  /** Return number of items in the dictionary. */
28998
29442
  get size() {
28999
29443
  return this.dict.size;
29000
29444
  }
29001
29445
  /** Retrieve a value that matches the key on `TRUNCATED_HASH_SIZE`. */
29002
- get(fullKey) {
29003
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
29004
- return this.dict.get(this.truncatedKey);
29446
+ get(key) {
29447
+ const truncatedKey = getTruncatedKey(key);
29448
+ return this.dict.get(truncatedKey);
29005
29449
  }
29006
29450
  /** Return true if the key is present in the dictionary */
29007
- has(fullKey) {
29008
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
29009
- return this.dict.has(this.truncatedKey);
29451
+ has(key) {
29452
+ const truncatedKey = getTruncatedKey(key);
29453
+ return this.dict.has(truncatedKey);
29010
29454
  }
29011
29455
  /** Set or update a value that matches the key on `TRUNCATED_HASH_SIZE`. */
29012
- set(fullKey, value) {
29013
- // NOTE we can't use the the shared key here, since the collection will
29014
- // store the key for us, hence the copy.
29015
- const key = bytes_Bytes.zero(hash_HASH_SIZE);
29016
- key.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
29017
- this.dict.set(key.asOpaque(), value);
29456
+ set(key, value) {
29457
+ const truncatedKey = getTruncatedKey(key);
29458
+ this.dict.set(truncatedKey, value);
29018
29459
  }
29019
29460
  /** Remove a value that matches the key on `TRUNCATED_HASH_SIZE`. */
29020
- delete(fullKey) {
29021
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
29022
- this.dict.delete(this.truncatedKey);
29461
+ delete(key) {
29462
+ const truncatedKey = getTruncatedKey(key);
29463
+ this.dict.delete(truncatedKey);
29023
29464
  }
29024
29465
  /** Iterator over values of the dictionary. */
29025
29466
  values() {
@@ -29027,9 +29468,7 @@ class TruncatedHashDictionary {
29027
29468
  }
29028
29469
  /** Iterator over entries of the dictionary (with truncated keys) */
29029
29470
  *entries() {
29030
- for (const [key, value] of this.dict.entries()) {
29031
- yield [bytes_Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_HASH_SIZE), TRUNCATED_HASH_SIZE).asOpaque(), value];
29032
- }
29471
+ yield* this.dict.entries();
29033
29472
  }
29034
29473
  [Symbol.iterator]() {
29035
29474
  return this.entries();
@@ -29046,6 +29485,7 @@ class TruncatedHashDictionary {
29046
29485
 
29047
29486
 
29048
29487
 
29488
+
29049
29489
  ;// CONCATENATED MODULE: ./packages/jam/block/codec.ts
29050
29490
 
29051
29491
 
@@ -37746,7 +38186,7 @@ class BlockVerifier {
37746
38186
  this.hasher = hasher;
37747
38187
  this.blocks = blocks;
37748
38188
  }
37749
- async verifyBlock(block) {
38189
+ async verifyBlock(block, options = { skipParentAndStateRoot: false }) {
37750
38190
  const headerView = block.header.view();
37751
38191
  const headerHash = this.hasher.header(headerView);
37752
38192
  // check if current block is already imported
@@ -37758,7 +38198,7 @@ class BlockVerifier {
37758
38198
  // https://graypaper.fluffylabs.dev/#/cc517d7/0c9d000c9d00?v=0.6.5
37759
38199
  const parentHash = headerView.parentHeaderHash.materialize();
37760
38200
  // importing genesis block
37761
- if (!parentHash.isEqualTo(block_verifier_ZERO_HASH)) {
38201
+ if (!parentHash.isEqualTo(block_verifier_ZERO_HASH) && !options.skipParentAndStateRoot) {
37762
38202
  const parentBlock = this.blocks.getHeader(parentHash);
37763
38203
  if (parentBlock === null) {
37764
38204
  return result_Result.error(BlockVerifierError.ParentNotFound, () => `Parent ${parentHash.toString()} not found`);
@@ -37778,21 +38218,20 @@ class BlockVerifier {
37778
38218
  if (!extrinsicHash.isEqualTo(extrinsicMerkleCommitment.hash)) {
37779
38219
  return result_Result.error(BlockVerifierError.InvalidExtrinsic, () => `Invalid extrinsic hash: ${extrinsicHash.toString()}, expected ${extrinsicMerkleCommitment.hash.toString()}`);
37780
38220
  }
37781
- // Check if the state root is valid.
37782
- // https://graypaper.fluffylabs.dev/#/cc517d7/0c18010c1801?v=0.6.5
37783
- const stateRoot = headerView.priorStateRoot.materialize();
37784
- const posteriorStateRoot = this.blocks.getPostStateRoot(parentHash);
37785
- if (posteriorStateRoot === null) {
37786
- return result_Result.error(BlockVerifierError.StateRootNotFound, () => `Posterior state root ${parentHash.toString()} not found`);
37787
- }
37788
- if (!stateRoot.isEqualTo(posteriorStateRoot)) {
37789
- return result_Result.error(BlockVerifierError.InvalidStateRoot, () => `Invalid prior state root: ${stateRoot.toString()}, expected ${posteriorStateRoot.toString()} (ours)`);
38221
+ if (!options.skipParentAndStateRoot) {
38222
+ // Check if the state root is valid.
38223
+ // https://graypaper.fluffylabs.dev/#/ab2cdbd/0c73010c7301?v=0.7.2
38224
+ const stateRoot = headerView.priorStateRoot.materialize();
38225
+ const posteriorStateRoot = this.blocks.getPostStateRoot(parentHash);
38226
+ if (posteriorStateRoot === null) {
38227
+ return result_Result.error(BlockVerifierError.StateRootNotFound, () => `Posterior state root ${parentHash.toString()} not found`);
38228
+ }
38229
+ if (!stateRoot.isEqualTo(posteriorStateRoot)) {
38230
+ return result_Result.error(BlockVerifierError.InvalidStateRoot, () => `Invalid prior state root: ${stateRoot.toString()}, expected ${posteriorStateRoot.toString()} (ours)`);
38231
+ }
37790
38232
  }
37791
38233
  return result_Result.ok(headerHash.hash);
37792
38234
  }
37793
- hashHeader(block) {
37794
- return this.hasher.header(block.header.view());
37795
- }
37796
38235
  }
37797
38236
 
37798
38237
  ;// CONCATENATED MODULE: ./packages/jam/transition/disputes/disputes-error-code.ts