@typeberry/jam 0.4.1-dae2283 → 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.
@@ -6712,9 +6712,438 @@ class ArrayView {
6712
6712
  }
6713
6713
  }
6714
6714
 
6715
+ ;// CONCATENATED MODULE: ./packages/core/collections/blob-dictionary.ts
6716
+
6717
+
6718
+ /** A map which uses byte blobs as keys */
6719
+ class BlobDictionary extends WithDebug {
6720
+ mapNodeThreshold;
6721
+ /**
6722
+ * The root node of the dictionary.
6723
+ *
6724
+ * This is the main internal data structure that organizes entries
6725
+ * in a tree-like fashion (array-based nodes up to `mapNodeThreshold`,
6726
+ * map-based nodes beyond it). All insertions, updates, and deletions
6727
+ * operate through this structure.
6728
+ */
6729
+ root = Node.withList();
6730
+ /**
6731
+ * Auxiliary map that stores references to the original keys and their values.
6732
+ *
6733
+ * - Overriding a value in the main structure does not replace the original key reference.
6734
+ * - Used for efficient iteration over `keys()`, `values()`, `entries()`, and computing `size`.
6735
+ */
6736
+ keyvals = new Map();
6737
+ /**
6738
+ * Protected constructor used internally by `BlobDictionary.new`
6739
+ * and `BlobDictionary.fromEntries`.
6740
+ *
6741
+ * This enforces controlled instantiation — users should create instances
6742
+ * through the provided static factory methods instead of calling the
6743
+ * constructor directly.
6744
+ *
6745
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
6746
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
6747
+ */
6748
+ constructor(mapNodeThreshold) {
6749
+ super();
6750
+ this.mapNodeThreshold = mapNodeThreshold;
6751
+ }
6752
+ /**
6753
+ * Returns the number of entries in the dictionary.
6754
+ *
6755
+ * The count is derived from the auxiliary `keyvals` map, which stores
6756
+ * all original key references and their associated values. This ensures
6757
+ * that the `size` reflects the actual number of entries, independent of
6758
+ * internal overrides in the main `root` structure.
6759
+ *
6760
+ * @returns The total number of entries in the dictionary.
6761
+ */
6762
+ get size() {
6763
+ return this.keyvals.size;
6764
+ }
6765
+ [TEST_COMPARE_USING]() {
6766
+ const vals = Array.from(this);
6767
+ vals.sort((a, b) => a[0].compare(b[0]).value);
6768
+ return vals;
6769
+ }
6770
+ /**
6771
+ * Creates an empty `BlobDictionary`.
6772
+ *
6773
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
6774
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
6775
+ * Defaults to `0`.
6776
+ *
6777
+ * @returns A new, empty `BlobDictionary` instance.
6778
+ */
6779
+ static new(mapNodeThreshold = 0) {
6780
+ return new BlobDictionary(mapNodeThreshold);
6781
+ }
6782
+ /**
6783
+ * Creates a new `BlobDictionary` initialized with the given entries.
6784
+ *
6785
+ * @param entries - An array of `[key, value]` pairs used to populate the dictionary.
6786
+ * @param mapNodeThreshold - The threshold that determines when the dictionary
6787
+ * switches from using an array-based (`ListChildren`) node to a map-based (`MapChildren`) node for storing entries.
6788
+ * Defaults to `0`.
6789
+ *
6790
+ * @returns A new `BlobDictionary` containing the provided entries.
6791
+ */
6792
+ static fromEntries(entries, mapNodeThreshold) {
6793
+ const dict = BlobDictionary.new(mapNodeThreshold);
6794
+ for (const [key, value] of entries) {
6795
+ dict.set(key, value);
6796
+ }
6797
+ return dict;
6798
+ }
6799
+ /**
6800
+ * Internal helper that inserts, updates or deletes an entry in the dictionary.
6801
+ *
6802
+ * Behaviour details:
6803
+ * - Passing `undefined` as `value` indicates a deletion. (E.g. `delete` uses `internalSet(key, undefined)`.)
6804
+ * - When an add (new entry) or a delete actually changes the structure, the method returns the affected leaf node.
6805
+ * - When the call only overrides an existing value (no structural add/delete), the method returns `null`.
6806
+ *
6807
+ * This method is intended for internal use by the dictionary implementation and allows `undefined` as a
6808
+ * sentinel value to signal removals.
6809
+ *
6810
+ * @param key - The key to insert, update or remove.
6811
+ * @param value - The value to associate with the key, or `undefined` to remove the key.
6812
+ * @returns The leaf node created or removed on add/delete, or `null` if the operation only overwrote an existing value.
6813
+ */
6814
+ internalSet(key, value) {
6815
+ let node = this.root;
6816
+ const keyChunkGenerator = key.chunks(CHUNK_SIZE);
6817
+ let depth = 0;
6818
+ for (;;) {
6819
+ const maybeKeyChunk = keyChunkGenerator.next().value;
6820
+ if (maybeKeyChunk === undefined) {
6821
+ if (value === undefined) {
6822
+ return node.remove(key);
6823
+ }
6824
+ return node.set(key, value);
6825
+ }
6826
+ const keyChunk = opaque_asOpaqueType(maybeKeyChunk);
6827
+ if (node.children instanceof ListChildren) {
6828
+ const subkey = bytes_BytesBlob.blobFrom(key.raw.subarray(CHUNK_SIZE * depth));
6829
+ const leaf = value !== undefined ? node.children.insert(subkey, { key, value }) : node.children.remove(subkey);
6830
+ if (subkey.length > CHUNK_SIZE && node.children.children.length > this.mapNodeThreshold) {
6831
+ node.convertListChildrenToMap();
6832
+ }
6833
+ return leaf;
6834
+ }
6835
+ depth += 1;
6836
+ const children = node.children;
6837
+ if (children instanceof ListChildren) {
6838
+ throw new Error("We handle list node earlier. If we fall through, we know it's for the `Map` case.");
6839
+ }
6840
+ if (children instanceof MapChildren) {
6841
+ const maybeNode = children.getChild(keyChunk);
6842
+ if (maybeNode !== undefined) {
6843
+ // simply go one level deeper
6844
+ node = maybeNode;
6845
+ }
6846
+ else {
6847
+ // we are trying to remove an item, but it does not exist
6848
+ if (value === undefined) {
6849
+ return null;
6850
+ }
6851
+ // no more child nodes, we insert a new one.
6852
+ const newNode = Node.withList();
6853
+ children.setChild(keyChunk, newNode);
6854
+ node = newNode;
6855
+ }
6856
+ continue;
6857
+ }
6858
+ debug_assertNever(children);
6859
+ }
6860
+ }
6861
+ /**
6862
+ * Adds a new entry to the dictionary or updates the value of an existing key.
6863
+ *
6864
+ * If an entry with the given key already exists, its value is replaced
6865
+ * with the new one.
6866
+ *
6867
+ * @param key - The key to add or update in the dictionary.
6868
+ * @param value - The value to associate with the specified key.
6869
+ * @returns Nothing (`void`).
6870
+ */
6871
+ set(key, value) {
6872
+ const leaf = this.internalSet(key, value);
6873
+ if (leaf !== null) {
6874
+ this.keyvals.set(leaf.key, leaf);
6875
+ }
6876
+ }
6877
+ /**
6878
+ * Retrieves the value associated with the given key from the dictionary.
6879
+ *
6880
+ * If the key does not exist, this method returns `undefined`.
6881
+ *
6882
+ * @param key - The key whose associated value should be retrieved.
6883
+ * @returns The value associated with the specified key, or `undefined` if the key is not present.
6884
+ */
6885
+ get(key) {
6886
+ let node = this.root;
6887
+ const pathChunksGenerator = key.chunks(CHUNK_SIZE);
6888
+ let depth = 0;
6889
+ while (node !== undefined) {
6890
+ const maybePathChunk = pathChunksGenerator.next().value;
6891
+ if (node.children instanceof ListChildren) {
6892
+ const subkey = bytes_BytesBlob.blobFrom(key.raw.subarray(depth * CHUNK_SIZE));
6893
+ const child = node.children.find(subkey);
6894
+ if (child !== null) {
6895
+ return child.value;
6896
+ }
6897
+ }
6898
+ if (maybePathChunk === undefined) {
6899
+ return node.getLeaf()?.value;
6900
+ }
6901
+ if (node.children instanceof MapChildren) {
6902
+ const pathChunk = opaque_asOpaqueType(maybePathChunk);
6903
+ node = node.children.getChild(pathChunk);
6904
+ depth += 1;
6905
+ }
6906
+ }
6907
+ return undefined;
6908
+ }
6909
+ /**
6910
+ * Checks whether the dictionary contains an entry for the given key.
6911
+ *
6912
+ * ⚠️ **Note:** Avoid using `has(...)` together with `get(...)` in a pattern like this:
6913
+ *
6914
+ * ```ts
6915
+ * if (dict.has(key)) {
6916
+ * const value = dict.get(key);
6917
+ * ...
6918
+ * }
6919
+ * ```
6920
+ *
6921
+ * This approach performs two lookups for the same key.
6922
+ *
6923
+ * Instead, prefer the following pattern, which retrieves the value once:
6924
+ *
6925
+ * ```ts
6926
+ * const value = dict.get(key);
6927
+ * if (value !== undefined) {
6928
+ * ...
6929
+ * }
6930
+ * ```
6931
+ *
6932
+ * @param key - The key to check for.
6933
+ * @returns `true` if the dictionary contains an entry for the given key, otherwise `false`.
6934
+ */
6935
+ has(key) {
6936
+ return this.get(key) !== undefined;
6937
+ }
6938
+ /**
6939
+ * Removes an entry with the specified key from the dictionary.
6940
+ *
6941
+ * Internally, this calls {@link internalSet} with `undefined` to mark the entry as deleted.
6942
+ *
6943
+ * @param key - The key of the entry to remove.
6944
+ * @returns `true` if an entry was removed (i.e. the key existed), otherwise `false`.
6945
+ */
6946
+ delete(key) {
6947
+ const leaf = this.internalSet(key, undefined);
6948
+ if (leaf !== null) {
6949
+ this.keyvals.delete(leaf.key);
6950
+ return true;
6951
+ }
6952
+ return false;
6953
+ }
6954
+ /**
6955
+ * Returns an iterator over the keys in the dictionary.
6956
+ *
6957
+ * The iterator yields each key in insertion order.
6958
+ *
6959
+ * @returns An iterator over all keys in the dictionary.
6960
+ */
6961
+ keys() {
6962
+ return this.keyvals.keys();
6963
+ }
6964
+ /**
6965
+ * Returns an iterator over the values in the dictionary.
6966
+ *
6967
+ * The iterator yields each value in insertion order.
6968
+ *
6969
+ * @returns An iterator over all values in the dictionary.
6970
+ */
6971
+ *values() {
6972
+ for (const leaf of this.keyvals.values()) {
6973
+ yield leaf.value;
6974
+ }
6975
+ }
6976
+ /**
6977
+ * Returns an iterator over the `[key, value]` pairs in the dictionary.
6978
+ *
6979
+ * The iterator yields entries in insertion order.
6980
+ *
6981
+ * @returns An iterator over `[key, value]` tuples for each entry in the dictionary.
6982
+ */
6983
+ *entries() {
6984
+ for (const leaf of this.keyvals.values()) {
6985
+ yield [leaf.key, leaf.value];
6986
+ }
6987
+ }
6988
+ /**
6989
+ * Default iterator for the dictionary.
6990
+ *
6991
+ * Equivalent to calling {@link entries}.
6992
+ * Enables iteration with `for...of`:
6993
+ *
6994
+ * ```ts
6995
+ * for (const [key, value] of dict) {
6996
+ * ...
6997
+ * }
6998
+ * ```
6999
+ *
7000
+ * @returns An iterator over `[key, value]` pairs.
7001
+ */
7002
+ [Symbol.iterator]() {
7003
+ return this.entries();
7004
+ }
7005
+ /**
7006
+ * Creates a new sorted array of values, ordered by their corresponding keys.
7007
+ *
7008
+ * Iterates over all entries in the dictionary and sorts them according
7009
+ * to the provided comparator function applied to the keys.
7010
+ *
7011
+ * @param comparator - A comparator function that can compare two keys.
7012
+ *
7013
+ * @returns A new array containing all values from the dictionary,
7014
+ * sorted according to their keys.
7015
+ */
7016
+ toSortedArray(comparator) {
7017
+ const vals = Array.from(this);
7018
+ vals.sort((a, b) => comparator(a[0], b[0]).value);
7019
+ return vals.map((x) => x[1]);
7020
+ }
7021
+ }
7022
+ const CHUNK_SIZE = 6;
7023
+ /**
7024
+ * A function to transform a bytes chunk (up to 6 bytes into U48 number)
7025
+ *
7026
+ * Note that it uses 3 additional bits to store length(`value * 8 + len;`),
7027
+ * It is needed to distinguish shorter chunks that have 0s at the end, for example: [1, 2] and [1, 2, 0]
7028
+ * */
7029
+ function bytesAsU48(bytes) {
7030
+ const len = bytes.length;
7031
+ debug_check `${len <= CHUNK_SIZE} Length has to be <= ${CHUNK_SIZE}, got: ${len}`;
7032
+ let value = bytes[3] | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
7033
+ for (let i = 4; i < bytes.length; i++) {
7034
+ value = value * 256 + bytes[i];
7035
+ }
7036
+ return value * 8 + len;
7037
+ }
7038
+ class Node {
7039
+ leaf;
7040
+ children;
7041
+ convertListChildrenToMap() {
7042
+ if (!(this.children instanceof ListChildren)) {
7043
+ return;
7044
+ }
7045
+ this.children = MapChildren.fromListNode(this.children);
7046
+ }
7047
+ static withList() {
7048
+ return new Node(undefined, ListChildren.new());
7049
+ }
7050
+ static withMap() {
7051
+ return new Node(undefined, MapChildren.new());
7052
+ }
7053
+ constructor(leaf, children) {
7054
+ this.leaf = leaf;
7055
+ this.children = children;
7056
+ }
7057
+ getLeaf() {
7058
+ return this.leaf;
7059
+ }
7060
+ remove(_key) {
7061
+ if (this.leaf === undefined) {
7062
+ return null;
7063
+ }
7064
+ const removedLeaf = this.leaf;
7065
+ this.leaf = undefined;
7066
+ return removedLeaf;
7067
+ }
7068
+ set(key, value) {
7069
+ if (this.leaf === undefined) {
7070
+ this.leaf = { key, value };
7071
+ return this.leaf;
7072
+ }
7073
+ this.leaf.value = value;
7074
+ return null;
7075
+ }
7076
+ }
7077
+ class ListChildren {
7078
+ children = [];
7079
+ constructor() { }
7080
+ find(key) {
7081
+ const result = this.children.find((item) => item[0].isEqualTo(key));
7082
+ if (result !== undefined) {
7083
+ return result[1];
7084
+ }
7085
+ return null;
7086
+ }
7087
+ remove(key) {
7088
+ const existingIndex = this.children.findIndex((item) => item[0].isEqualTo(key));
7089
+ if (existingIndex >= 0) {
7090
+ const ret = this.children.splice(existingIndex, 1);
7091
+ return ret[0][1];
7092
+ }
7093
+ return null;
7094
+ }
7095
+ insert(key, leaf) {
7096
+ const existingIndex = this.children.findIndex((item) => item[0].isEqualTo(key));
7097
+ if (existingIndex >= 0) {
7098
+ const existing = this.children[existingIndex];
7099
+ existing[1].value = leaf.value;
7100
+ return null;
7101
+ }
7102
+ this.children.push([key, leaf]);
7103
+ return leaf;
7104
+ }
7105
+ static new() {
7106
+ return new ListChildren();
7107
+ }
7108
+ }
7109
+ class MapChildren {
7110
+ children = new Map();
7111
+ constructor() { }
7112
+ static new() {
7113
+ return new MapChildren();
7114
+ }
7115
+ static fromListNode(node) {
7116
+ const mapNode = new MapChildren();
7117
+ for (const [key, leaf] of node.children) {
7118
+ const currentKeyChunk = opaque_asOpaqueType(bytes_BytesBlob.blobFrom(key.raw.subarray(0, CHUNK_SIZE)));
7119
+ const subKey = bytes_BytesBlob.blobFrom(key.raw.subarray(CHUNK_SIZE));
7120
+ let child = mapNode.getChild(currentKeyChunk);
7121
+ if (child === undefined) {
7122
+ child = Node.withList();
7123
+ mapNode.setChild(currentKeyChunk, child);
7124
+ }
7125
+ const children = child.children;
7126
+ children.insert(subKey, leaf);
7127
+ }
7128
+ return mapNode;
7129
+ }
7130
+ getChild(keyChunk) {
7131
+ const chunkAsNumber = bytesAsU48(keyChunk.raw);
7132
+ return this.children.get(chunkAsNumber);
7133
+ }
7134
+ setChild(keyChunk, node) {
7135
+ const chunkAsNumber = bytesAsU48(keyChunk.raw);
7136
+ this.children.set(chunkAsNumber, node);
7137
+ }
7138
+ }
7139
+
6715
7140
  ;// CONCATENATED MODULE: ./packages/core/collections/hash-dictionary.ts
6716
- /** A map which uses hashes as keys. */
6717
- class HashDictionary {
7141
+ /**
7142
+ * A map which uses hashes as keys.
7143
+ *
7144
+ * @deprecated
7145
+ * */
7146
+ class StringHashDictionary {
6718
7147
  // TODO [ToDr] [crit] We can't use `TrieHash` directly in the map,
6719
7148
  // because of the way it's being compared. Hence having `string` here.
6720
7149
  // This has to be benchmarked and re-written to a custom map most likely.
@@ -6780,6 +7209,17 @@ class HashDictionary {
6780
7209
  }
6781
7210
  }
6782
7211
 
7212
+ /**
7213
+ * A value that indicates when `BlobDictionary` transforms Array nodes into Map nodes.
7214
+ * In practice, it doesn't matter much because, in real life, arrays in this structure usually have a length close to 1.
7215
+ */
7216
+ const BLOB_DICTIONARY_THRESHOLD = 5;
7217
+ class HashDictionary extends BlobDictionary {
7218
+ constructor() {
7219
+ super(BLOB_DICTIONARY_THRESHOLD);
7220
+ }
7221
+ }
7222
+
6783
7223
  ;// CONCATENATED MODULE: ./packages/core/collections/hash-set.ts
6784
7224
 
6785
7225
  /** A set specialized for storing hashes. */
@@ -7244,6 +7684,18 @@ class SortedSet extends SortedArray {
7244
7684
 
7245
7685
 
7246
7686
 
7687
+ function getTruncatedKey(key) {
7688
+ // Always return exactly TRUNCATED_HASH_SIZE bytes.
7689
+ if (key.length === TRUNCATED_HASH_SIZE) {
7690
+ return key;
7691
+ }
7692
+ return bytes_Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_HASH_SIZE), TRUNCATED_HASH_SIZE);
7693
+ }
7694
+ /**
7695
+ * A value that indicates when `BlobDictionary` transforms Array nodes into Map nodes.
7696
+ * In practice, it doesn't matter much because, in real life, arrays in this structure usually have a length close to 1.
7697
+ */
7698
+ const truncated_hash_dictionary_BLOB_DICTIONARY_THRESHOLD = 5;
7247
7699
  /**
7248
7700
  * A collection of hash-based keys (likely `StateKey`s) which ignores
7249
7701
  * differences on the last byte.
@@ -7256,48 +7708,37 @@ class TruncatedHashDictionary {
7256
7708
  * Each key will be copied and have the last byte replace with a 0.
7257
7709
  */
7258
7710
  static fromEntries(entries) {
7259
- /** Copy key bytes of an entry and replace the last one with 0. */
7260
- const mapped = Array.from(entries).map(([key, value]) => {
7261
- const newKey = bytes_Bytes.zero(HASH_SIZE).asOpaque();
7262
- newKey.raw.set(key.raw.subarray(0, TRUNCATED_HASH_SIZE));
7263
- return [newKey, value];
7264
- });
7265
- return new TruncatedHashDictionary(HashDictionary.fromEntries(mapped));
7711
+ return new TruncatedHashDictionary(BlobDictionary.fromEntries(Array.from(entries).map(([key, value]) => [getTruncatedKey(key), value]), truncated_hash_dictionary_BLOB_DICTIONARY_THRESHOLD));
7266
7712
  }
7267
- /** A truncated key which we re-use to query the dictionary. */
7268
- truncatedKey = bytes_Bytes.zero(HASH_SIZE).asOpaque();
7269
7713
  constructor(dict) {
7270
7714
  this.dict = dict;
7271
7715
  }
7272
7716
  [TEST_COMPARE_USING]() {
7273
- return this.dict;
7717
+ return Array.from(this.dict);
7274
7718
  }
7275
7719
  /** Return number of items in the dictionary. */
7276
7720
  get size() {
7277
7721
  return this.dict.size;
7278
7722
  }
7279
7723
  /** Retrieve a value that matches the key on `TRUNCATED_HASH_SIZE`. */
7280
- get(fullKey) {
7281
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
7282
- return this.dict.get(this.truncatedKey);
7724
+ get(key) {
7725
+ const truncatedKey = getTruncatedKey(key);
7726
+ return this.dict.get(truncatedKey);
7283
7727
  }
7284
7728
  /** Return true if the key is present in the dictionary */
7285
- has(fullKey) {
7286
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
7287
- return this.dict.has(this.truncatedKey);
7729
+ has(key) {
7730
+ const truncatedKey = getTruncatedKey(key);
7731
+ return this.dict.has(truncatedKey);
7288
7732
  }
7289
7733
  /** Set or update a value that matches the key on `TRUNCATED_HASH_SIZE`. */
7290
- set(fullKey, value) {
7291
- // NOTE we can't use the the shared key here, since the collection will
7292
- // store the key for us, hence the copy.
7293
- const key = bytes_Bytes.zero(HASH_SIZE);
7294
- key.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
7295
- this.dict.set(key.asOpaque(), value);
7734
+ set(key, value) {
7735
+ const truncatedKey = getTruncatedKey(key);
7736
+ this.dict.set(truncatedKey, value);
7296
7737
  }
7297
7738
  /** Remove a value that matches the key on `TRUNCATED_HASH_SIZE`. */
7298
- delete(fullKey) {
7299
- this.truncatedKey.raw.set(fullKey.raw.subarray(0, TRUNCATED_HASH_SIZE));
7300
- this.dict.delete(this.truncatedKey);
7739
+ delete(key) {
7740
+ const truncatedKey = getTruncatedKey(key);
7741
+ this.dict.delete(truncatedKey);
7301
7742
  }
7302
7743
  /** Iterator over values of the dictionary. */
7303
7744
  values() {
@@ -7305,9 +7746,7 @@ class TruncatedHashDictionary {
7305
7746
  }
7306
7747
  /** Iterator over entries of the dictionary (with truncated keys) */
7307
7748
  *entries() {
7308
- for (const [key, value] of this.dict.entries()) {
7309
- yield [bytes_Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_HASH_SIZE), TRUNCATED_HASH_SIZE).asOpaque(), value];
7310
- }
7749
+ yield* this.dict.entries();
7311
7750
  }
7312
7751
  [Symbol.iterator]() {
7313
7752
  return this.entries();
@@ -7324,6 +7763,7 @@ class TruncatedHashDictionary {
7324
7763
 
7325
7764
 
7326
7765
 
7766
+
7327
7767
  ;// CONCATENATED MODULE: ./packages/jam/config/chain-spec.ts
7328
7768
 
7329
7769