@feelyourprotocol/blockchain 8141.0.0

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.
Files changed (132) hide show
  1. package/README.md +269 -0
  2. package/dist/cjs/blockchain.d.ts +372 -0
  3. package/dist/cjs/blockchain.d.ts.map +1 -0
  4. package/dist/cjs/blockchain.js +1105 -0
  5. package/dist/cjs/blockchain.js.map +1 -0
  6. package/dist/cjs/consensus/casper.d.ts +16 -0
  7. package/dist/cjs/consensus/casper.d.ts.map +1 -0
  8. package/dist/cjs/consensus/casper.js +28 -0
  9. package/dist/cjs/consensus/casper.js.map +1 -0
  10. package/dist/cjs/consensus/clique.d.ts +164 -0
  11. package/dist/cjs/consensus/clique.d.ts.map +1 -0
  12. package/dist/cjs/consensus/clique.js +520 -0
  13. package/dist/cjs/consensus/clique.js.map +1 -0
  14. package/dist/cjs/consensus/ethash.d.ts +29 -0
  15. package/dist/cjs/consensus/ethash.d.ts.map +1 -0
  16. package/dist/cjs/consensus/ethash.js +48 -0
  17. package/dist/cjs/consensus/ethash.js.map +1 -0
  18. package/dist/cjs/consensus/index.d.ts +5 -0
  19. package/dist/cjs/consensus/index.d.ts.map +1 -0
  20. package/dist/cjs/consensus/index.js +10 -0
  21. package/dist/cjs/consensus/index.js.map +1 -0
  22. package/dist/cjs/constructors.d.ts +13 -0
  23. package/dist/cjs/constructors.d.ts.map +1 -0
  24. package/dist/cjs/constructors.js +80 -0
  25. package/dist/cjs/constructors.js.map +1 -0
  26. package/dist/cjs/db/cache.d.ts +17 -0
  27. package/dist/cjs/db/cache.d.ts.map +1 -0
  28. package/dist/cjs/db/cache.js +38 -0
  29. package/dist/cjs/db/cache.js.map +1 -0
  30. package/dist/cjs/db/constants.d.ts +23 -0
  31. package/dist/cjs/db/constants.d.ts.map +1 -0
  32. package/dist/cjs/db/constants.js +54 -0
  33. package/dist/cjs/db/constants.js.map +1 -0
  34. package/dist/cjs/db/helpers.d.ts +9 -0
  35. package/dist/cjs/db/helpers.d.ts.map +1 -0
  36. package/dist/cjs/db/helpers.js +68 -0
  37. package/dist/cjs/db/helpers.js.map +1 -0
  38. package/dist/cjs/db/manager.d.ts +78 -0
  39. package/dist/cjs/db/manager.d.ts.map +1 -0
  40. package/dist/cjs/db/manager.js +203 -0
  41. package/dist/cjs/db/manager.js.map +1 -0
  42. package/dist/cjs/db/operation.d.ts +45 -0
  43. package/dist/cjs/db/operation.d.ts.map +1 -0
  44. package/dist/cjs/db/operation.js +110 -0
  45. package/dist/cjs/db/operation.js.map +1 -0
  46. package/dist/cjs/helpers.d.ts +19 -0
  47. package/dist/cjs/helpers.d.ts.map +1 -0
  48. package/dist/cjs/helpers.js +34 -0
  49. package/dist/cjs/helpers.js.map +1 -0
  50. package/dist/cjs/index.d.ts +7 -0
  51. package/dist/cjs/index.d.ts.map +1 -0
  52. package/dist/cjs/index.js +33 -0
  53. package/dist/cjs/index.js.map +1 -0
  54. package/dist/cjs/package.json +3 -0
  55. package/dist/cjs/types.d.ts +219 -0
  56. package/dist/cjs/types.d.ts.map +1 -0
  57. package/dist/cjs/types.js +3 -0
  58. package/dist/cjs/types.js.map +1 -0
  59. package/dist/esm/blockchain.d.ts +372 -0
  60. package/dist/esm/blockchain.d.ts.map +1 -0
  61. package/dist/esm/blockchain.js +1101 -0
  62. package/dist/esm/blockchain.js.map +1 -0
  63. package/dist/esm/consensus/casper.d.ts +16 -0
  64. package/dist/esm/consensus/casper.d.ts.map +1 -0
  65. package/dist/esm/consensus/casper.js +24 -0
  66. package/dist/esm/consensus/casper.js.map +1 -0
  67. package/dist/esm/consensus/clique.d.ts +164 -0
  68. package/dist/esm/consensus/clique.d.ts.map +1 -0
  69. package/dist/esm/consensus/clique.js +516 -0
  70. package/dist/esm/consensus/clique.js.map +1 -0
  71. package/dist/esm/consensus/ethash.d.ts +29 -0
  72. package/dist/esm/consensus/ethash.d.ts.map +1 -0
  73. package/dist/esm/consensus/ethash.js +44 -0
  74. package/dist/esm/consensus/ethash.js.map +1 -0
  75. package/dist/esm/consensus/index.d.ts +5 -0
  76. package/dist/esm/consensus/index.d.ts.map +1 -0
  77. package/dist/esm/consensus/index.js +5 -0
  78. package/dist/esm/consensus/index.js.map +1 -0
  79. package/dist/esm/constructors.d.ts +13 -0
  80. package/dist/esm/constructors.d.ts.map +1 -0
  81. package/dist/esm/constructors.js +76 -0
  82. package/dist/esm/constructors.js.map +1 -0
  83. package/dist/esm/db/cache.d.ts +17 -0
  84. package/dist/esm/db/cache.d.ts.map +1 -0
  85. package/dist/esm/db/cache.js +34 -0
  86. package/dist/esm/db/cache.js.map +1 -0
  87. package/dist/esm/db/constants.d.ts +23 -0
  88. package/dist/esm/db/constants.d.ts.map +1 -0
  89. package/dist/esm/db/constants.js +46 -0
  90. package/dist/esm/db/constants.js.map +1 -0
  91. package/dist/esm/db/helpers.d.ts +9 -0
  92. package/dist/esm/db/helpers.d.ts.map +1 -0
  93. package/dist/esm/db/helpers.js +61 -0
  94. package/dist/esm/db/helpers.js.map +1 -0
  95. package/dist/esm/db/manager.d.ts +78 -0
  96. package/dist/esm/db/manager.d.ts.map +1 -0
  97. package/dist/esm/db/manager.js +199 -0
  98. package/dist/esm/db/manager.js.map +1 -0
  99. package/dist/esm/db/operation.d.ts +45 -0
  100. package/dist/esm/db/operation.d.ts.map +1 -0
  101. package/dist/esm/db/operation.js +106 -0
  102. package/dist/esm/db/operation.js.map +1 -0
  103. package/dist/esm/helpers.d.ts +19 -0
  104. package/dist/esm/helpers.d.ts.map +1 -0
  105. package/dist/esm/helpers.js +30 -0
  106. package/dist/esm/helpers.js.map +1 -0
  107. package/dist/esm/index.d.ts +7 -0
  108. package/dist/esm/index.d.ts.map +1 -0
  109. package/dist/esm/index.js +7 -0
  110. package/dist/esm/index.js.map +1 -0
  111. package/dist/esm/package.json +3 -0
  112. package/dist/esm/types.d.ts +219 -0
  113. package/dist/esm/types.d.ts.map +1 -0
  114. package/dist/esm/types.js +2 -0
  115. package/dist/esm/types.js.map +1 -0
  116. package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
  117. package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
  118. package/package.json +75 -0
  119. package/src/blockchain.ts +1353 -0
  120. package/src/consensus/casper.ts +33 -0
  121. package/src/consensus/clique.ts +633 -0
  122. package/src/consensus/ethash.ts +69 -0
  123. package/src/consensus/index.ts +5 -0
  124. package/src/constructors.ts +119 -0
  125. package/src/db/cache.ts +39 -0
  126. package/src/db/constants.ts +73 -0
  127. package/src/db/helpers.ts +81 -0
  128. package/src/db/manager.ts +243 -0
  129. package/src/db/operation.ts +152 -0
  130. package/src/helpers.ts +37 -0
  131. package/src/index.ts +12 -0
  132. package/src/types.ts +256 -0
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EthashConsensus = exports.CliqueConsensus = exports.CasperConsensus = void 0;
4
+ const casper_ts_1 = require("./casper.js");
5
+ Object.defineProperty(exports, "CasperConsensus", { enumerable: true, get: function () { return casper_ts_1.CasperConsensus; } });
6
+ const clique_ts_1 = require("./clique.js");
7
+ Object.defineProperty(exports, "CliqueConsensus", { enumerable: true, get: function () { return clique_ts_1.CliqueConsensus; } });
8
+ const ethash_ts_1 = require("./ethash.js");
9
+ Object.defineProperty(exports, "EthashConsensus", { enumerable: true, get: function () { return ethash_ts_1.EthashConsensus; } });
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/consensus/index.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAIpC,gGAJA,2BAAe,OAIA;AAHxB,2CAA6C;AAGnB,gGAHjB,2BAAe,OAGiB;AAFzC,2CAA6C;AAEF,gGAFlC,2BAAe,OAEkC"}
@@ -0,0 +1,13 @@
1
+ import { Blockchain } from './index.ts';
2
+ import type { BlockData } from '@feelyourprotocol/block';
3
+ import type { BlockchainOptions } from './index.ts';
4
+ export declare function createBlockchain(opts?: BlockchainOptions): Promise<Blockchain>;
5
+ /**
6
+ * Creates a blockchain from a list of block objects,
7
+ * objects must be readable by {@link createBlock}
8
+ *
9
+ * @param blockData List of block objects
10
+ * @param opts Constructor options, see {@link BlockchainOptions}
11
+ */
12
+ export declare function createBlockchainFromBlocksData(blocksData: BlockData[], opts?: BlockchainOptions): Promise<Blockchain>;
13
+ //# sourceMappingURL=constructors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constructors.d.ts","sourceRoot":"","sources":["../../src/constructors.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,UAAU,EAMX,MAAM,YAAY,CAAA;AAEnB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAElD,OAAO,KAAK,EAAE,iBAAiB,EAAQ,MAAM,YAAY,CAAA;AAKzD,wBAAsB,gBAAgB,CAAC,IAAI,GAAE,iBAAsB,uBAsElE;AAED;;;;;;GAMG;AACH,wBAAsB,8BAA8B,CAClD,UAAU,EAAE,SAAS,EAAE,EACvB,IAAI,GAAE,iBAAsB,uBAW7B"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBlockchain = createBlockchain;
4
+ exports.createBlockchainFromBlocksData = createBlockchainFromBlocksData;
5
+ const block_1 = require("@feelyourprotocol/block");
6
+ const util_1 = require("@feelyourprotocol/util");
7
+ const debug_1 = require("debug");
8
+ const index_ts_1 = require("./index.js");
9
+ const DEBUG = (0, util_1.isDebugEnabled)('ethjs');
10
+ const debug = (0, debug_1.default)('blockchain:#');
11
+ async function createBlockchain(opts = {}) {
12
+ const blockchain = new index_ts_1.Blockchain(opts);
13
+ await blockchain.consensus?.setup({ blockchain });
14
+ let stateRoot = opts.genesisBlock?.header.stateRoot ?? opts.genesisStateRoot;
15
+ if (stateRoot === undefined) {
16
+ if (blockchain['_customGenesisState'] !== undefined) {
17
+ stateRoot = await (0, index_ts_1.genGenesisStateRoot)(blockchain['_customGenesisState'], blockchain.common);
18
+ }
19
+ else {
20
+ stateRoot = await (0, index_ts_1.getGenesisStateRoot)(Number(blockchain.common.chainId()), blockchain.common);
21
+ }
22
+ }
23
+ const genesisBlock = opts.genesisBlock ?? blockchain.createGenesisBlock(stateRoot);
24
+ let genesisHash = await blockchain.dbManager.numberToHash(util_1.BIGINT_0);
25
+ const dbGenesisBlock = genesisHash !== undefined ? await blockchain.dbManager.getBlock(genesisHash) : undefined;
26
+ // If the DB has a genesis block, then verify that the genesis block in the
27
+ // DB is indeed the Genesis block generated or assigned.
28
+ if (dbGenesisBlock !== undefined && !(0, util_1.equalsBytes)(genesisBlock.hash(), dbGenesisBlock.hash())) {
29
+ throw (0, util_1.EthereumJSErrorWithoutCode)('The genesis block in the DB has a different hash than the provided genesis block.');
30
+ }
31
+ genesisHash = genesisBlock.hash();
32
+ if (!dbGenesisBlock) {
33
+ // If there is no genesis block put the genesis block in the DB.
34
+ // For that TD, the BlockOrHeader, and the Lookups have to be saved.
35
+ const dbOps = [];
36
+ dbOps.push((0, index_ts_1.DBSetTD)(genesisBlock.header.difficulty, util_1.BIGINT_0, genesisHash));
37
+ (0, index_ts_1.DBSetBlockOrHeader)(genesisBlock).map((op) => dbOps.push(op));
38
+ (0, index_ts_1.DBSaveLookups)(genesisHash, util_1.BIGINT_0).map((op) => dbOps.push(op));
39
+ await blockchain.dbManager.batch(dbOps);
40
+ await blockchain.consensus?.genesisInit(genesisBlock);
41
+ }
42
+ // At this point, we can safely set the genesis:
43
+ // it is either the one we put in the DB, or it is equal to the one
44
+ // which we read from the DB.
45
+ blockchain['_genesisBlock'] = genesisBlock;
46
+ // load verified iterator heads
47
+ const heads = await blockchain.dbManager.getHeads();
48
+ blockchain['_heads'] = heads ?? {};
49
+ // load headerchain head
50
+ let hash = await blockchain.dbManager.getHeadHeader();
51
+ blockchain['_headHeaderHash'] = hash ?? genesisHash;
52
+ // load blockchain head
53
+ hash = await blockchain.dbManager.getHeadBlock();
54
+ blockchain['_headBlockHash'] = hash ?? genesisHash;
55
+ if (blockchain['_hardforkByHeadBlockNumber']) {
56
+ const latestHeader = await blockchain['_getHeader'](blockchain['_headHeaderHash']);
57
+ await blockchain.checkAndTransitionHardForkByNumber(latestHeader.number, latestHeader.timestamp);
58
+ }
59
+ DEBUG && debug(`genesis block initialized with hash ${(0, util_1.bytesToHex)(genesisHash)}`);
60
+ return blockchain;
61
+ }
62
+ /**
63
+ * Creates a blockchain from a list of block objects,
64
+ * objects must be readable by {@link createBlock}
65
+ *
66
+ * @param blockData List of block objects
67
+ * @param opts Constructor options, see {@link BlockchainOptions}
68
+ */
69
+ async function createBlockchainFromBlocksData(blocksData, opts = {}) {
70
+ const blockchain = await createBlockchain(opts);
71
+ for (const blockData of blocksData) {
72
+ const block = (0, block_1.createBlock)(blockData, {
73
+ common: blockchain.common,
74
+ setHardfork: true,
75
+ });
76
+ await blockchain.putBlock(block);
77
+ }
78
+ return blockchain;
79
+ }
80
+ //# sourceMappingURL=constructors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constructors.js","sourceRoot":"","sources":["../../src/constructors.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,17 @@
1
+ import { LRUCache } from 'lru-cache';
2
+ /**
3
+ * Simple LRU Cache that allows for keys of type Uint8Array
4
+ * @hidden
5
+ */
6
+ export declare class Cache<V> {
7
+ _cache: LRUCache<string, {
8
+ value: V;
9
+ }, void>;
10
+ constructor(opts: LRUCache.Options<string, {
11
+ value: V;
12
+ }, void>);
13
+ set(key: string | Uint8Array, value: V): void;
14
+ get(key: string | Uint8Array): V | undefined;
15
+ del(key: string | Uint8Array): void;
16
+ }
17
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../src/db/cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEpC;;;GAGG;AACH,qBAAa,KAAK,CAAC,CAAC;IAClB,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,CAAC,CAAA;KAAE,EAAE,IAAI,CAAC,CAAA;gBAEhC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,CAAC,CAAA;KAAE,EAAE,IAAI,CAAC;IAI9D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAQ7C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,CAAC,GAAG,SAAS;IAS5C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;CAOpC"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Cache = void 0;
4
+ const util_1 = require("@feelyourprotocol/util");
5
+ const lru_cache_1 = require("lru-cache");
6
+ /**
7
+ * Simple LRU Cache that allows for keys of type Uint8Array
8
+ * @hidden
9
+ */
10
+ class Cache {
11
+ constructor(opts) {
12
+ this._cache = new lru_cache_1.LRUCache(opts);
13
+ }
14
+ set(key, value) {
15
+ if (key instanceof Uint8Array) {
16
+ // Using deprecated bytesToUnprefixedHex for performance: used as LRU cache keys (string encoding).
17
+ key = (0, util_1.bytesToUnprefixedHex)(key);
18
+ }
19
+ this._cache.set(key, { value });
20
+ }
21
+ get(key) {
22
+ if (key instanceof Uint8Array) {
23
+ // Using deprecated bytesToUnprefixedHex for performance: used as LRU cache keys (string encoding).
24
+ key = (0, util_1.bytesToUnprefixedHex)(key);
25
+ }
26
+ const elem = this._cache.get(key);
27
+ return elem !== undefined ? elem.value : undefined;
28
+ }
29
+ del(key) {
30
+ if (key instanceof Uint8Array) {
31
+ // Using deprecated bytesToUnprefixedHex for performance: used as LRU cache keys (string encoding).
32
+ key = (0, util_1.bytesToUnprefixedHex)(key);
33
+ }
34
+ this._cache.delete(key);
35
+ }
36
+ }
37
+ exports.Cache = Cache;
38
+ //# sourceMappingURL=cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../src/db/cache.ts"],"names":[],"mappings":";;;AAAA,2CAAuD;AACvD,yCAAoC;AAEpC;;;GAGG;AACH,MAAa,KAAK;IAGhB,YAAY,IAAkD;QAC5D,IAAI,CAAC,MAAM,GAAG,IAAI,oBAAQ,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAED,GAAG,CAAC,GAAwB,EAAE,KAAQ;QACpC,IAAI,GAAG,YAAY,UAAU,EAAE,CAAC;YAC9B,mGAAmG;YACnG,GAAG,GAAG,IAAA,2BAAoB,EAAC,GAAG,CAAC,CAAA;QACjC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;IACjC,CAAC;IAED,GAAG,CAAC,GAAwB;QAC1B,IAAI,GAAG,YAAY,UAAU,EAAE,CAAC;YAC9B,mGAAmG;YACnG,GAAG,GAAG,IAAA,2BAAoB,EAAC,GAAG,CAAC,CAAA;QACjC,CAAC;QACD,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACjC,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IACpD,CAAC;IAED,GAAG,CAAC,GAAwB;QAC1B,IAAI,GAAG,YAAY,UAAU,EAAE,CAAC;YAC9B,mGAAmG;YACnG,GAAG,GAAG,IAAA,2BAAoB,EAAC,GAAG,CAAC,CAAA;QACjC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACzB,CAAC;CACF;AA/BD,sBA+BC"}
@@ -0,0 +1,23 @@
1
+ declare const HEADS_KEY = "heads";
2
+ /**
3
+ * Current canonical head for light sync
4
+ */
5
+ declare const HEAD_HEADER_KEY = "LastHeader";
6
+ /**
7
+ * Current canonical head for full sync
8
+ */
9
+ declare const HEAD_BLOCK_KEY = "LastBlock";
10
+ /**
11
+ * Convert bigint to big endian Uint8Array
12
+ */
13
+ declare const bytesBE8: (n: bigint) => Uint8Array<ArrayBufferLike>;
14
+ declare const tdKey: (n: bigint, hash: Uint8Array) => Uint8Array<ArrayBuffer>;
15
+ declare const headerKey: (n: bigint, hash: Uint8Array) => Uint8Array<ArrayBuffer>;
16
+ declare const bodyKey: (n: bigint, hash: Uint8Array) => Uint8Array<ArrayBuffer>;
17
+ declare const numberToHashKey: (n: bigint) => Uint8Array<ArrayBuffer>;
18
+ declare const hashToNumberKey: (hash: Uint8Array) => Uint8Array<ArrayBuffer>;
19
+ /**
20
+ * @hidden
21
+ */
22
+ export { bodyKey, bytesBE8, hashToNumberKey, HEAD_BLOCK_KEY, HEAD_HEADER_KEY, headerKey, HEADS_KEY, numberToHashKey, tdKey, };
23
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/db/constants.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,SAAS,UAAU,CAAA;AAEzB;;GAEG;AACH,QAAA,MAAM,eAAe,eAAe,CAAA;AAEpC;;GAEG;AACH,QAAA,MAAM,cAAc,cAAc,CAAA;AA6BlC;;GAEG;AACH,QAAA,MAAM,QAAQ,GAAI,GAAG,MAAM,gCAAyC,CAAA;AAEpE,QAAA,MAAM,KAAK,GAAI,GAAG,MAAM,EAAE,MAAM,UAAU,4BACgB,CAAA;AAE1D,QAAA,MAAM,SAAS,GAAI,GAAG,MAAM,EAAE,MAAM,UAAU,4BAAkD,CAAA;AAEhG,QAAA,MAAM,OAAO,GAAI,GAAG,MAAM,EAAE,MAAM,UAAU,4BAAgD,CAAA;AAE5F,QAAA,MAAM,eAAe,GAAI,GAAG,MAAM,4BAAwD,CAAA;AAE1F,QAAA,MAAM,eAAe,GAAI,MAAM,UAAU,4BAAyC,CAAA;AAElF;;GAEG;AACH,OAAO,EACL,OAAO,EACP,QAAQ,EACR,eAAe,EACf,cAAc,EACd,eAAe,EACf,SAAS,EACT,SAAS,EACT,eAAe,EACf,KAAK,GACN,CAAA"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tdKey = exports.numberToHashKey = exports.HEADS_KEY = exports.headerKey = exports.HEAD_HEADER_KEY = exports.HEAD_BLOCK_KEY = exports.hashToNumberKey = exports.bytesBE8 = exports.bodyKey = void 0;
4
+ const util_1 = require("@feelyourprotocol/util");
5
+ // Geth compatible DB keys
6
+ const HEADS_KEY = 'heads';
7
+ exports.HEADS_KEY = HEADS_KEY;
8
+ /**
9
+ * Current canonical head for light sync
10
+ */
11
+ const HEAD_HEADER_KEY = 'LastHeader';
12
+ exports.HEAD_HEADER_KEY = HEAD_HEADER_KEY;
13
+ /**
14
+ * Current canonical head for full sync
15
+ */
16
+ const HEAD_BLOCK_KEY = 'LastBlock';
17
+ exports.HEAD_BLOCK_KEY = HEAD_BLOCK_KEY;
18
+ /**
19
+ * headerPrefix + number + hash -> header
20
+ */
21
+ const HEADER_PREFIX = (0, util_1.utf8ToBytes)('h');
22
+ /**
23
+ * headerPrefix + number + hash + tdSuffix -> td
24
+ */
25
+ const TD_SUFFIX = (0, util_1.utf8ToBytes)('t');
26
+ /**
27
+ * headerPrefix + number + numSuffix -> hash
28
+ */
29
+ const NUM_SUFFIX = (0, util_1.utf8ToBytes)('n');
30
+ /**
31
+ * blockHashPrefix + hash -> number
32
+ */
33
+ const BLOCK_HASH_PREFIX = (0, util_1.utf8ToBytes)('H');
34
+ /**
35
+ * bodyPrefix + number + hash -> block body
36
+ */
37
+ const BODY_PREFIX = (0, util_1.utf8ToBytes)('b');
38
+ // Utility functions
39
+ /**
40
+ * Convert bigint to big endian Uint8Array
41
+ */
42
+ const bytesBE8 = (n) => (0, util_1.bigIntToBytes)(BigInt.asUintN(64, n));
43
+ exports.bytesBE8 = bytesBE8;
44
+ const tdKey = (n, hash) => (0, util_1.concatBytes)(HEADER_PREFIX, bytesBE8(n), hash, TD_SUFFIX);
45
+ exports.tdKey = tdKey;
46
+ const headerKey = (n, hash) => (0, util_1.concatBytes)(HEADER_PREFIX, bytesBE8(n), hash);
47
+ exports.headerKey = headerKey;
48
+ const bodyKey = (n, hash) => (0, util_1.concatBytes)(BODY_PREFIX, bytesBE8(n), hash);
49
+ exports.bodyKey = bodyKey;
50
+ const numberToHashKey = (n) => (0, util_1.concatBytes)(HEADER_PREFIX, bytesBE8(n), NUM_SUFFIX);
51
+ exports.numberToHashKey = numberToHashKey;
52
+ const hashToNumberKey = (hash) => (0, util_1.concatBytes)(BLOCK_HASH_PREFIX, hash);
53
+ exports.hashToNumberKey = hashToNumberKey;
54
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/db/constants.ts"],"names":[],"mappings":";;;AAAA,2CAA0E;AAE1E,0BAA0B;AAE1B,MAAM,SAAS,GAAG,OAAO,CAAA;AAiEvB,8BAAS;AA/DX;;GAEG;AACH,MAAM,eAAe,GAAG,YAAY,CAAA;AA0DlC,0CAAe;AAxDjB;;GAEG;AACH,MAAM,cAAc,GAAG,WAAW,CAAA;AAoDhC,wCAAc;AAlDhB;;GAEG;AACH,MAAM,aAAa,GAAG,IAAA,kBAAW,EAAC,GAAG,CAAC,CAAA;AAEtC;;GAEG;AACH,MAAM,SAAS,GAAG,IAAA,kBAAW,EAAC,GAAG,CAAC,CAAA;AAElC;;GAEG;AACH,MAAM,UAAU,GAAG,IAAA,kBAAW,EAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAA,kBAAW,EAAC,GAAG,CAAC,CAAA;AAE1C;;GAEG;AACH,MAAM,WAAW,GAAG,IAAA,kBAAW,EAAC,GAAG,CAAC,CAAA;AAEpC,oBAAoB;AAEpB;;GAEG;AACH,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAA,oBAAa,EAAC,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;AAkBlE,4BAAQ;AAhBV,MAAM,KAAK,GAAG,CAAC,CAAS,EAAE,IAAgB,EAAE,EAAE,CAC5C,IAAA,kBAAW,EAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAAA;AAsBxD,sBAAK;AApBP,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,IAAgB,EAAE,EAAE,CAAC,IAAA,kBAAW,EAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;AAiB9F,8BAAS;AAfX,MAAM,OAAO,GAAG,CAAC,CAAS,EAAE,IAAgB,EAAE,EAAE,CAAC,IAAA,kBAAW,EAAC,WAAW,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;AAU1F,0BAAO;AART,MAAM,eAAe,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAA,kBAAW,EAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAA;AAexF,0CAAe;AAbjB,MAAM,eAAe,GAAG,CAAC,IAAgB,EAAE,EAAE,CAAC,IAAA,kBAAW,EAAC,iBAAiB,EAAE,IAAI,CAAC,CAAA;AAQhF,0CAAe"}
@@ -0,0 +1,9 @@
1
+ import { Block } from '@feelyourprotocol/block';
2
+ import { DBOp } from './operation.ts';
3
+ import type { BlockHeader } from '@feelyourprotocol/block';
4
+ declare function DBSetTD(TD: bigint, blockNumber: bigint, blockHash: Uint8Array): DBOp;
5
+ declare function DBSetBlockOrHeader(blockBody: Block | BlockHeader): DBOp[];
6
+ declare function DBSetHashToNumber(blockHash: Uint8Array, blockNumber: bigint): DBOp;
7
+ declare function DBSaveLookups(blockHash: Uint8Array, blockNumber: bigint, skipNumIndex?: boolean): DBOp[];
8
+ export { DBOp, DBSaveLookups, DBSetBlockOrHeader, DBSetHashToNumber, DBSetTD };
9
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/db/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAKzC,OAAO,EAAE,IAAI,EAAY,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAOpD,iBAAS,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,GAAG,IAAI,CAK7E;AASD,iBAAS,kBAAkB,CAAC,SAAS,EAAE,KAAK,GAAG,WAAW,GAAG,IAAI,EAAE,CA4BlE;AAED,iBAAS,iBAAiB,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAK3E;AAED,iBAAS,aAAa,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,IAAI,EAAE,CAajG;AAED,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,OAAO,EAAE,CAAA"}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DBOp = void 0;
4
+ exports.DBSaveLookups = DBSaveLookups;
5
+ exports.DBSetBlockOrHeader = DBSetBlockOrHeader;
6
+ exports.DBSetHashToNumber = DBSetHashToNumber;
7
+ exports.DBSetTD = DBSetTD;
8
+ const block_1 = require("@feelyourprotocol/block");
9
+ const rlp_1 = require("@feelyourprotocol/rlp");
10
+ const util_1 = require("@feelyourprotocol/util");
11
+ const constants_ts_1 = require("./constants.js");
12
+ const operation_ts_1 = require("./operation.js");
13
+ Object.defineProperty(exports, "DBOp", { enumerable: true, get: function () { return operation_ts_1.DBOp; } });
14
+ /*
15
+ * This extra helper file serves as an interface between the blockchain API functionality
16
+ * and the DB operations from `db/operation.ts` and also handles the right encoding of the keys
17
+ */
18
+ function DBSetTD(TD, blockNumber, blockHash) {
19
+ return operation_ts_1.DBOp.set(operation_ts_1.DBTarget.TotalDifficulty, rlp_1.RLP.encode(TD), {
20
+ blockNumber,
21
+ blockHash,
22
+ });
23
+ }
24
+ /*
25
+ * This method accepts either a BlockHeader or a Block and returns a list of DatabaseOperation instances
26
+ *
27
+ * - A "Set Header Operation" is always added
28
+ * - A "Set Body Operation" is only added if the body is not empty (it has transactions/uncles) or if the block is the genesis block
29
+ * (if there is a header but no block saved the DB will implicitly assume the block to be empty)
30
+ */
31
+ function DBSetBlockOrHeader(blockBody) {
32
+ const header = blockBody instanceof block_1.Block ? blockBody.header : blockBody;
33
+ const dbOps = [];
34
+ const blockNumber = header.number;
35
+ const blockHash = header.hash();
36
+ const headerValue = header.serialize();
37
+ dbOps.push(operation_ts_1.DBOp.set(operation_ts_1.DBTarget.Header, headerValue, {
38
+ blockNumber,
39
+ blockHash,
40
+ }));
41
+ const isGenesis = header.number === util_1.BIGINT_0;
42
+ if (isGenesis || blockBody instanceof block_1.Block) {
43
+ const bodyValue = rlp_1.RLP.encode(blockBody.raw().slice(1));
44
+ dbOps.push(operation_ts_1.DBOp.set(operation_ts_1.DBTarget.Body, bodyValue, {
45
+ blockNumber,
46
+ blockHash,
47
+ }));
48
+ }
49
+ return dbOps;
50
+ }
51
+ function DBSetHashToNumber(blockHash, blockNumber) {
52
+ const blockNumber8Byte = (0, constants_ts_1.bytesBE8)(blockNumber);
53
+ return operation_ts_1.DBOp.set(operation_ts_1.DBTarget.HashToNumber, blockNumber8Byte, {
54
+ blockHash,
55
+ });
56
+ }
57
+ function DBSaveLookups(blockHash, blockNumber, skipNumIndex) {
58
+ const ops = [];
59
+ if (skipNumIndex !== true) {
60
+ ops.push(operation_ts_1.DBOp.set(operation_ts_1.DBTarget.NumberToHash, blockHash, { blockNumber }));
61
+ }
62
+ const blockNumber8Bytes = (0, constants_ts_1.bytesBE8)(blockNumber);
63
+ ops.push(operation_ts_1.DBOp.set(operation_ts_1.DBTarget.HashToNumber, blockNumber8Bytes, {
64
+ blockHash,
65
+ }));
66
+ return ops;
67
+ }
68
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/db/helpers.ts"],"names":[],"mappings":";;;AAgFe,sCAAa;AAAE,gDAAkB;AAAE,8CAAiB;AAAE,0BAAO;AAhF5E,6CAAyC;AACzC,yCAAqC;AACrC,2CAA2C;AAE3C,iDAAyC;AACzC,iDAA+C;AA2EtC,qFA3EA,mBAAI,OA2EA;AAvEb;;;GAGG;AAEH,SAAS,OAAO,CAAC,EAAU,EAAE,WAAmB,EAAE,SAAqB;IACrE,OAAO,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,eAAe,EAAE,SAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;QACxD,WAAW;QACX,SAAS;KACV,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,SAA8B;IACxD,MAAM,MAAM,GAAgB,SAAS,YAAY,aAAK,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;IACrF,MAAM,KAAK,GAAG,EAAE,CAAA;IAEhB,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAA;IACjC,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,EAAE,CAAA;IAE/B,MAAM,WAAW,GAAG,MAAM,CAAC,SAAS,EAAE,CAAA;IACtC,KAAK,CAAC,IAAI,CACR,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,MAAM,EAAE,WAAW,EAAE;QACrC,WAAW;QACX,SAAS;KACV,CAAC,CACH,CAAA;IAED,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,KAAK,eAAQ,CAAA;IAE5C,IAAI,SAAS,IAAI,SAAS,YAAY,aAAK,EAAE,CAAC;QAC5C,MAAM,SAAS,GAAG,SAAG,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QACtD,KAAK,CAAC,IAAI,CACR,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,IAAI,EAAE,SAAS,EAAE;YACjC,WAAW;YACX,SAAS;SACV,CAAC,CACH,CAAA;IACH,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,iBAAiB,CAAC,SAAqB,EAAE,WAAmB;IACnE,MAAM,gBAAgB,GAAG,IAAA,uBAAQ,EAAC,WAAW,CAAC,CAAA;IAC9C,OAAO,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,YAAY,EAAE,gBAAgB,EAAE;QACvD,SAAS;KACV,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,SAAqB,EAAE,WAAmB,EAAE,YAAsB;IACvF,MAAM,GAAG,GAAG,EAAE,CAAA;IACd,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;QAC1B,GAAG,CAAC,IAAI,CAAC,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,YAAY,EAAE,SAAS,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC,CAAA;IACvE,CAAC;IAED,MAAM,iBAAiB,GAAG,IAAA,uBAAQ,EAAC,WAAW,CAAC,CAAA;IAC/C,GAAG,CAAC,IAAI,CACN,mBAAI,CAAC,GAAG,CAAC,uBAAQ,CAAC,YAAY,EAAE,iBAAiB,EAAE;QACjD,SAAS;KACV,CAAC,CACH,CAAA;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -0,0 +1,78 @@
1
+ import { Cache } from './cache.ts';
2
+ import { DBOp, DBTarget } from './operation.ts';
3
+ import type { Block, BlockBodyBytes } from '@feelyourprotocol/block';
4
+ import type { Common } from '@feelyourprotocol/common';
5
+ import type { DB, DBObject } from '@feelyourprotocol/util';
6
+ import type { DatabaseKey } from './operation.ts';
7
+ /**
8
+ * @hidden
9
+ */
10
+ export interface GetOpts {
11
+ keyEncoding?: string;
12
+ valueEncoding?: string;
13
+ cache?: string;
14
+ }
15
+ export type CacheMap = {
16
+ [key: string]: Cache<Uint8Array>;
17
+ };
18
+ /**
19
+ * Abstraction over a DB to facilitate storing/fetching blockchain-related
20
+ * data, such as blocks and headers, indices, and the head block.
21
+ * @hidden
22
+ */
23
+ export declare class DBManager {
24
+ private _cache;
25
+ readonly common: Common;
26
+ private _db;
27
+ constructor(db: DB<Uint8Array | string, Uint8Array | string | DBObject>, common: Common);
28
+ /**
29
+ * Fetches iterator heads from the db.
30
+ */
31
+ getHeads(): Promise<{
32
+ [key: string]: Uint8Array;
33
+ }>;
34
+ /**
35
+ * Fetches header of the head block.
36
+ */
37
+ getHeadHeader(): Promise<Uint8Array | undefined>;
38
+ /**
39
+ * Fetches head block.
40
+ */
41
+ getHeadBlock(): Promise<Uint8Array | undefined>;
42
+ /**
43
+ * Fetches a block (header and body) given a block id,
44
+ * which can be either its hash or its number.
45
+ */
46
+ getBlock(blockId: Uint8Array | bigint | number): Promise<Block | undefined>;
47
+ /**
48
+ * Fetches body of a block given its hash and number.
49
+ */
50
+ getBody(blockHash: Uint8Array, blockNumber: bigint): Promise<BlockBodyBytes | undefined>;
51
+ /**
52
+ * Fetches header of a block given its hash and number.
53
+ */
54
+ getHeader(blockHash: Uint8Array, blockNumber: bigint): Promise<import("@feelyourprotocol/block").BlockHeader>;
55
+ /**
56
+ * Fetches total difficulty for a block given its hash and number.
57
+ */
58
+ getTotalDifficulty(blockHash: Uint8Array, blockNumber: bigint): Promise<bigint>;
59
+ /**
60
+ * Performs a block hash to block number lookup.
61
+ */
62
+ hashToNumber(blockHash: Uint8Array): Promise<bigint | undefined>;
63
+ /**
64
+ * Performs a block number to block hash lookup.
65
+ */
66
+ numberToHash(blockNumber: bigint): Promise<Uint8Array | undefined>;
67
+ /**
68
+ * Fetches a key from the db. If `opts.cache` is specified
69
+ * it first tries to load from cache, and on cache miss will
70
+ * try to put the fetched item on cache afterwards.
71
+ */
72
+ get(dbOperationTarget: DBTarget, key?: DatabaseKey): Promise<any>;
73
+ /**
74
+ * Performs a batch operation on db.
75
+ */
76
+ batch(ops: DBOp[]): Promise<void>;
77
+ }
78
+ //# sourceMappingURL=manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../src/db/manager.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc,EAA4B,MAAM,mBAAmB,CAAA;AACxF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,EAAa,EAAE,EAAE,QAAQ,EAAsB,MAAM,kBAAkB,CAAA;AACnF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,MAAM,QAAQ,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC,CAAA;CAAE,CAAA;AAE3D;;;;GAIG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAU;IACxB,SAAgB,MAAM,EAAE,MAAM,CAAA;IAC9B,OAAO,CAAC,GAAG,CAAyD;gBAExD,EAAE,EAAE,EAAE,CAAC,UAAU,GAAG,MAAM,EAAE,UAAU,GAAG,MAAM,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM;IAYvF;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;KAAE,CAAC;IAaxD;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAItD;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAIrD;;;OAGG;IACG,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC;IAqDjF;;OAEG;IACG,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAK9F;;OAEG;IACG,SAAS,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM;IAQ1D;;OAEG;IACG,kBAAkB,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKrF;;OAEG;IACG,YAAY,CAAC,SAAS,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAQtE;;OAEG;IACG,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAKxE;;;;OAIG;IACG,GAAG,CAAC,iBAAiB,EAAE,QAAQ,EAAE,GAAG,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IA6BvE;;OAEG;IACG,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE;CAmBxB"}
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DBManager = void 0;
4
+ const block_1 = require("@feelyourprotocol/block");
5
+ const rlp_1 = require("@feelyourprotocol/rlp");
6
+ const util_1 = require("@feelyourprotocol/util");
7
+ const cache_ts_1 = require("./cache.js");
8
+ const operation_ts_1 = require("./operation.js");
9
+ /**
10
+ * Abstraction over a DB to facilitate storing/fetching blockchain-related
11
+ * data, such as blocks and headers, indices, and the head block.
12
+ * @hidden
13
+ */
14
+ class DBManager {
15
+ constructor(db, common) {
16
+ this._db = db;
17
+ this.common = common;
18
+ this._cache = {
19
+ td: new cache_ts_1.Cache({ max: 1024 }),
20
+ header: new cache_ts_1.Cache({ max: 512 }),
21
+ body: new cache_ts_1.Cache({ max: 256 }),
22
+ numberToHash: new cache_ts_1.Cache({ max: 2048 }),
23
+ hashToNumber: new cache_ts_1.Cache({ max: 2048 }),
24
+ };
25
+ }
26
+ /**
27
+ * Fetches iterator heads from the db.
28
+ */
29
+ async getHeads() {
30
+ const heads = (await this.get(operation_ts_1.DBTarget.Heads));
31
+ if (heads === undefined)
32
+ return heads;
33
+ const decodedHeads = {};
34
+ for (const key of Object.keys(heads)) {
35
+ // Heads are stored in DB as hex strings since Level converts Uint8Arrays
36
+ // to nested JSON objects when they are included in a value being stored
37
+ // in the DB
38
+ decodedHeads[key] = (0, util_1.unprefixedHexToBytes)(heads[key]);
39
+ }
40
+ return decodedHeads;
41
+ }
42
+ /**
43
+ * Fetches header of the head block.
44
+ */
45
+ async getHeadHeader() {
46
+ return this.get(operation_ts_1.DBTarget.HeadHeader);
47
+ }
48
+ /**
49
+ * Fetches head block.
50
+ */
51
+ async getHeadBlock() {
52
+ return this.get(operation_ts_1.DBTarget.HeadBlock);
53
+ }
54
+ /**
55
+ * Fetches a block (header and body) given a block id,
56
+ * which can be either its hash or its number.
57
+ */
58
+ async getBlock(blockId) {
59
+ if (typeof blockId === 'number' && Number.isInteger(blockId)) {
60
+ blockId = BigInt(blockId);
61
+ }
62
+ let number;
63
+ let hash;
64
+ if (blockId === undefined)
65
+ return undefined;
66
+ if (blockId instanceof Uint8Array) {
67
+ hash = blockId;
68
+ number = await this.hashToNumber(blockId);
69
+ }
70
+ else if (typeof blockId === 'bigint') {
71
+ number = blockId;
72
+ hash = await this.numberToHash(blockId);
73
+ }
74
+ else {
75
+ throw (0, util_1.EthereumJSErrorWithoutCode)('Unknown blockId type');
76
+ }
77
+ if (hash === undefined || number === undefined)
78
+ return undefined;
79
+ const header = await this.getHeader(hash, number);
80
+ let body = await this.getBody(hash, number);
81
+ // be backward compatible where we didn't use to store a body with no txs, uncles, withdrawals
82
+ // otherwise the body is never partially stored and if we have some body, its in entirety
83
+ if (body === undefined) {
84
+ body = [[], []];
85
+ // Do extra validations on the header since we are assuming empty transactions and uncles
86
+ if (!(0, util_1.equalsBytes)(header.transactionsTrie, util_1.KECCAK256_RLP)) {
87
+ throw (0, util_1.EthereumJSErrorWithoutCode)('transactionsTrie root should be equal to hash of null');
88
+ }
89
+ if (!(0, util_1.equalsBytes)(header.uncleHash, util_1.KECCAK256_RLP_ARRAY)) {
90
+ throw (0, util_1.EthereumJSErrorWithoutCode)('uncle hash should be equal to hash of empty array');
91
+ }
92
+ // If this block had empty withdrawals push an empty array in body
93
+ if (header.withdrawalsRoot !== undefined) {
94
+ // Do extra validations for withdrawal before assuming empty withdrawals
95
+ if (!(0, util_1.equalsBytes)(header.withdrawalsRoot, util_1.KECCAK256_RLP)) {
96
+ throw (0, util_1.EthereumJSErrorWithoutCode)('withdrawals root shoot be equal to hash of null when no withdrawals');
97
+ }
98
+ else {
99
+ body.push([]);
100
+ }
101
+ }
102
+ }
103
+ const blockData = [header.raw(), ...body];
104
+ const opts = { common: this.common, setHardfork: true };
105
+ return (0, block_1.createBlockFromBytesArray)(blockData, opts);
106
+ }
107
+ /**
108
+ * Fetches body of a block given its hash and number.
109
+ */
110
+ async getBody(blockHash, blockNumber) {
111
+ const body = await this.get(operation_ts_1.DBTarget.Body, { blockHash, blockNumber });
112
+ return body !== undefined ? rlp_1.RLP.decode(body) : undefined;
113
+ }
114
+ /**
115
+ * Fetches header of a block given its hash and number.
116
+ */
117
+ async getHeader(blockHash, blockNumber) {
118
+ const encodedHeader = await this.get(operation_ts_1.DBTarget.Header, { blockHash, blockNumber });
119
+ const headerValues = rlp_1.RLP.decode(encodedHeader);
120
+ const opts = { common: this.common, setHardfork: true };
121
+ return (0, block_1.createBlockHeaderFromBytesArray)(headerValues, opts);
122
+ }
123
+ /**
124
+ * Fetches total difficulty for a block given its hash and number.
125
+ */
126
+ async getTotalDifficulty(blockHash, blockNumber) {
127
+ const td = await this.get(operation_ts_1.DBTarget.TotalDifficulty, { blockHash, blockNumber });
128
+ return (0, util_1.bytesToBigInt)(rlp_1.RLP.decode(td));
129
+ }
130
+ /**
131
+ * Performs a block hash to block number lookup.
132
+ */
133
+ async hashToNumber(blockHash) {
134
+ const value = await this.get(operation_ts_1.DBTarget.HashToNumber, { blockHash });
135
+ if (value === undefined) {
136
+ throw (0, util_1.EthereumJSErrorWithoutCode)(`value for ${(0, util_1.bytesToHex)(blockHash)} not found in DB`);
137
+ }
138
+ return value !== undefined ? (0, util_1.bytesToBigInt)(value) : undefined;
139
+ }
140
+ /**
141
+ * Performs a block number to block hash lookup.
142
+ */
143
+ async numberToHash(blockNumber) {
144
+ const value = await this.get(operation_ts_1.DBTarget.NumberToHash, { blockNumber });
145
+ return value;
146
+ }
147
+ /**
148
+ * Fetches a key from the db. If `opts.cache` is specified
149
+ * it first tries to load from cache, and on cache miss will
150
+ * try to put the fetched item on cache afterwards.
151
+ */
152
+ async get(dbOperationTarget, key) {
153
+ const dbGetOperation = operation_ts_1.DBOp.get(dbOperationTarget, key);
154
+ const cacheString = dbGetOperation.cacheString;
155
+ const dbKey = dbGetOperation.baseDBOp.key;
156
+ if (cacheString !== undefined) {
157
+ if (this._cache[cacheString] === undefined) {
158
+ throw (0, util_1.EthereumJSErrorWithoutCode)(`Invalid cache: ${cacheString}`);
159
+ }
160
+ let value = this._cache[cacheString].get(dbKey);
161
+ if (value === undefined) {
162
+ value = (await this._db.get(dbKey, {
163
+ keyEncoding: dbGetOperation.baseDBOp.keyEncoding,
164
+ valueEncoding: dbGetOperation.baseDBOp.valueEncoding,
165
+ }));
166
+ if (value !== undefined) {
167
+ this._cache[cacheString].set(dbKey, value);
168
+ }
169
+ }
170
+ return value;
171
+ }
172
+ return this._db.get(dbKey, {
173
+ keyEncoding: dbGetOperation.baseDBOp.keyEncoding,
174
+ valueEncoding: dbGetOperation.baseDBOp.valueEncoding,
175
+ });
176
+ }
177
+ /**
178
+ * Performs a batch operation on db.
179
+ */
180
+ async batch(ops) {
181
+ const convertedOps = ops.map((op) => {
182
+ const type = op.baseDBOp.type ?? (op.baseDBOp.value !== undefined ? 'put' : 'del');
183
+ const convertedOp = {
184
+ key: op.baseDBOp.key,
185
+ value: op.baseDBOp.value,
186
+ type,
187
+ opts: {
188
+ keyEncoding: op.baseDBOp.keyEncoding,
189
+ valueEncoding: op.baseDBOp.valueEncoding,
190
+ },
191
+ };
192
+ if (type === 'put')
193
+ return convertedOp;
194
+ else
195
+ return convertedOp;
196
+ });
197
+ // update the current cache for each operation
198
+ ops.map((op) => op.updateCache(this._cache));
199
+ return this._db.batch(convertedOps);
200
+ }
201
+ }
202
+ exports.DBManager = DBManager;
203
+ //# sourceMappingURL=manager.js.map