@feelyourprotocol/statemanager 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 (169) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +331 -0
  3. package/dist/cjs/cache/account.d.ts +85 -0
  4. package/dist/cjs/cache/account.d.ts.map +1 -0
  5. package/dist/cjs/cache/account.js +252 -0
  6. package/dist/cjs/cache/account.js.map +1 -0
  7. package/dist/cjs/cache/cache.d.ts +23 -0
  8. package/dist/cjs/cache/cache.d.ts.map +1 -0
  9. package/dist/cjs/cache/cache.js +31 -0
  10. package/dist/cjs/cache/cache.js.map +1 -0
  11. package/dist/cjs/cache/caches.d.ts +19 -0
  12. package/dist/cjs/cache/caches.d.ts.map +1 -0
  13. package/dist/cjs/cache/caches.js +107 -0
  14. package/dist/cjs/cache/caches.js.map +1 -0
  15. package/dist/cjs/cache/code.d.ts +87 -0
  16. package/dist/cjs/cache/code.d.ts.map +1 -0
  17. package/dist/cjs/cache/code.js +258 -0
  18. package/dist/cjs/cache/code.js.map +1 -0
  19. package/dist/cjs/cache/index.d.ts +7 -0
  20. package/dist/cjs/cache/index.d.ts.map +1 -0
  21. package/dist/cjs/cache/index.js +23 -0
  22. package/dist/cjs/cache/index.js.map +1 -0
  23. package/dist/cjs/cache/originalStorageCache.d.ts +21 -0
  24. package/dist/cjs/cache/originalStorageCache.d.ts.map +1 -0
  25. package/dist/cjs/cache/originalStorageCache.js +52 -0
  26. package/dist/cjs/cache/originalStorageCache.js.map +1 -0
  27. package/dist/cjs/cache/storage.d.ts +101 -0
  28. package/dist/cjs/cache/storage.d.ts.map +1 -0
  29. package/dist/cjs/cache/storage.js +337 -0
  30. package/dist/cjs/cache/storage.js.map +1 -0
  31. package/dist/cjs/cache/types.d.ts +36 -0
  32. package/dist/cjs/cache/types.d.ts.map +1 -0
  33. package/dist/cjs/cache/types.js +8 -0
  34. package/dist/cjs/cache/types.js.map +1 -0
  35. package/dist/cjs/index.d.ts +8 -0
  36. package/dist/cjs/index.d.ts.map +1 -0
  37. package/dist/cjs/index.js +24 -0
  38. package/dist/cjs/index.js.map +1 -0
  39. package/dist/cjs/merkleStateManager.d.ts +260 -0
  40. package/dist/cjs/merkleStateManager.d.ts.map +1 -0
  41. package/dist/cjs/merkleStateManager.js +616 -0
  42. package/dist/cjs/merkleStateManager.js.map +1 -0
  43. package/dist/cjs/package.json +3 -0
  44. package/dist/cjs/proof/index.d.ts +3 -0
  45. package/dist/cjs/proof/index.d.ts.map +1 -0
  46. package/dist/cjs/proof/index.js +19 -0
  47. package/dist/cjs/proof/index.js.map +1 -0
  48. package/dist/cjs/proof/merkle.d.ts +40 -0
  49. package/dist/cjs/proof/merkle.d.ts.map +1 -0
  50. package/dist/cjs/proof/merkle.js +182 -0
  51. package/dist/cjs/proof/merkle.js.map +1 -0
  52. package/dist/cjs/proof/rpc.d.ts +10 -0
  53. package/dist/cjs/proof/rpc.d.ts.map +1 -0
  54. package/dist/cjs/proof/rpc.js +20 -0
  55. package/dist/cjs/proof/rpc.js.map +1 -0
  56. package/dist/cjs/rpcStateManager.d.ts +162 -0
  57. package/dist/cjs/rpcStateManager.d.ts.map +1 -0
  58. package/dist/cjs/rpcStateManager.js +313 -0
  59. package/dist/cjs/rpcStateManager.js.map +1 -0
  60. package/dist/cjs/simpleStateManager.d.ts +54 -0
  61. package/dist/cjs/simpleStateManager.d.ts.map +1 -0
  62. package/dist/cjs/simpleStateManager.js +125 -0
  63. package/dist/cjs/simpleStateManager.js.map +1 -0
  64. package/dist/cjs/statefulBinaryTreeStateManager.d.ts +69 -0
  65. package/dist/cjs/statefulBinaryTreeStateManager.d.ts.map +1 -0
  66. package/dist/cjs/statefulBinaryTreeStateManager.js +576 -0
  67. package/dist/cjs/statefulBinaryTreeStateManager.js.map +1 -0
  68. package/dist/cjs/types.d.ts +92 -0
  69. package/dist/cjs/types.d.ts.map +1 -0
  70. package/dist/cjs/types.js +3 -0
  71. package/dist/cjs/types.js.map +1 -0
  72. package/dist/cjs/util.d.ts +4 -0
  73. package/dist/cjs/util.d.ts.map +1 -0
  74. package/dist/cjs/util.js +21 -0
  75. package/dist/cjs/util.js.map +1 -0
  76. package/dist/esm/cache/account.d.ts +85 -0
  77. package/dist/esm/cache/account.d.ts.map +1 -0
  78. package/dist/esm/cache/account.js +248 -0
  79. package/dist/esm/cache/account.js.map +1 -0
  80. package/dist/esm/cache/cache.d.ts +23 -0
  81. package/dist/esm/cache/cache.d.ts.map +1 -0
  82. package/dist/esm/cache/cache.js +27 -0
  83. package/dist/esm/cache/cache.js.map +1 -0
  84. package/dist/esm/cache/caches.d.ts +19 -0
  85. package/dist/esm/cache/caches.d.ts.map +1 -0
  86. package/dist/esm/cache/caches.js +103 -0
  87. package/dist/esm/cache/caches.js.map +1 -0
  88. package/dist/esm/cache/code.d.ts +87 -0
  89. package/dist/esm/cache/code.d.ts.map +1 -0
  90. package/dist/esm/cache/code.js +254 -0
  91. package/dist/esm/cache/code.js.map +1 -0
  92. package/dist/esm/cache/index.d.ts +7 -0
  93. package/dist/esm/cache/index.d.ts.map +1 -0
  94. package/dist/esm/cache/index.js +7 -0
  95. package/dist/esm/cache/index.js.map +1 -0
  96. package/dist/esm/cache/originalStorageCache.d.ts +21 -0
  97. package/dist/esm/cache/originalStorageCache.d.ts.map +1 -0
  98. package/dist/esm/cache/originalStorageCache.js +48 -0
  99. package/dist/esm/cache/originalStorageCache.js.map +1 -0
  100. package/dist/esm/cache/storage.d.ts +101 -0
  101. package/dist/esm/cache/storage.d.ts.map +1 -0
  102. package/dist/esm/cache/storage.js +333 -0
  103. package/dist/esm/cache/storage.js.map +1 -0
  104. package/dist/esm/cache/types.d.ts +36 -0
  105. package/dist/esm/cache/types.d.ts.map +1 -0
  106. package/dist/esm/cache/types.js +5 -0
  107. package/dist/esm/cache/types.js.map +1 -0
  108. package/dist/esm/index.d.ts +8 -0
  109. package/dist/esm/index.d.ts.map +1 -0
  110. package/dist/esm/index.js +8 -0
  111. package/dist/esm/index.js.map +1 -0
  112. package/dist/esm/merkleStateManager.d.ts +260 -0
  113. package/dist/esm/merkleStateManager.d.ts.map +1 -0
  114. package/dist/esm/merkleStateManager.js +612 -0
  115. package/dist/esm/merkleStateManager.js.map +1 -0
  116. package/dist/esm/package.json +3 -0
  117. package/dist/esm/proof/index.d.ts +3 -0
  118. package/dist/esm/proof/index.d.ts.map +1 -0
  119. package/dist/esm/proof/index.js +3 -0
  120. package/dist/esm/proof/index.js.map +1 -0
  121. package/dist/esm/proof/merkle.d.ts +40 -0
  122. package/dist/esm/proof/merkle.d.ts.map +1 -0
  123. package/dist/esm/proof/merkle.js +175 -0
  124. package/dist/esm/proof/merkle.js.map +1 -0
  125. package/dist/esm/proof/rpc.d.ts +10 -0
  126. package/dist/esm/proof/rpc.d.ts.map +1 -0
  127. package/dist/esm/proof/rpc.js +17 -0
  128. package/dist/esm/proof/rpc.js.map +1 -0
  129. package/dist/esm/rpcStateManager.d.ts +162 -0
  130. package/dist/esm/rpcStateManager.d.ts.map +1 -0
  131. package/dist/esm/rpcStateManager.js +308 -0
  132. package/dist/esm/rpcStateManager.js.map +1 -0
  133. package/dist/esm/simpleStateManager.d.ts +54 -0
  134. package/dist/esm/simpleStateManager.d.ts.map +1 -0
  135. package/dist/esm/simpleStateManager.js +121 -0
  136. package/dist/esm/simpleStateManager.js.map +1 -0
  137. package/dist/esm/statefulBinaryTreeStateManager.d.ts +69 -0
  138. package/dist/esm/statefulBinaryTreeStateManager.d.ts.map +1 -0
  139. package/dist/esm/statefulBinaryTreeStateManager.js +572 -0
  140. package/dist/esm/statefulBinaryTreeStateManager.js.map +1 -0
  141. package/dist/esm/types.d.ts +92 -0
  142. package/dist/esm/types.d.ts.map +1 -0
  143. package/dist/esm/types.js +2 -0
  144. package/dist/esm/types.js.map +1 -0
  145. package/dist/esm/util.d.ts +4 -0
  146. package/dist/esm/util.d.ts.map +1 -0
  147. package/dist/esm/util.js +18 -0
  148. package/dist/esm/util.js.map +1 -0
  149. package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
  150. package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
  151. package/package.json +74 -0
  152. package/src/cache/account.ts +277 -0
  153. package/src/cache/cache.ts +35 -0
  154. package/src/cache/caches.ts +125 -0
  155. package/src/cache/code.ts +277 -0
  156. package/src/cache/index.ts +6 -0
  157. package/src/cache/originalStorageCache.ts +57 -0
  158. package/src/cache/storage.ts +369 -0
  159. package/src/cache/types.ts +38 -0
  160. package/src/index.ts +7 -0
  161. package/src/merkleStateManager.ts +737 -0
  162. package/src/proof/index.ts +2 -0
  163. package/src/proof/merkle.ts +264 -0
  164. package/src/proof/rpc.ts +24 -0
  165. package/src/rpcStateManager.ts +381 -0
  166. package/src/simpleStateManager.ts +154 -0
  167. package/src/statefulBinaryTreeStateManager.ts +789 -0
  168. package/src/types.ts +103 -0
  169. package/src/util.ts +28 -0
@@ -0,0 +1,737 @@
1
+ import { Common, Mainnet } from '@feelyourprotocol/common'
2
+ import { MerklePatriciaTrie } from '@feelyourprotocol/mpt'
3
+ import { RLP } from '@feelyourprotocol/rlp'
4
+ import {
5
+ Account,
6
+ EthereumJSErrorWithoutCode,
7
+ bytesToUnprefixedHex,
8
+ concatBytes,
9
+ createAccount,
10
+ createAccountFromRLP,
11
+ createAddressFromString,
12
+ equalsBytes,
13
+ hexToBytes,
14
+ isDebugEnabled,
15
+ short,
16
+ toBytes,
17
+ unpadBytes,
18
+ unprefixedHexToBytes,
19
+ utf8ToBytes,
20
+ } from '@feelyourprotocol/util'
21
+ import { keccak_256 } from '@noble/hashes/sha3.js'
22
+ import debugDefault from 'debug'
23
+
24
+ import { OriginalStorageCache } from './cache/index.ts'
25
+ import type { Caches, MerkleStateManagerOpts } from './index.ts'
26
+ import { modifyAccountFields } from './util.ts'
27
+
28
+ import type {
29
+ AccountFields,
30
+ StateManagerInterface,
31
+ StorageDump,
32
+ StorageRange,
33
+ } from '@feelyourprotocol/common'
34
+ import type { Address, DB } from '@feelyourprotocol/util'
35
+ import type { Debugger } from 'debug'
36
+
37
+ /**
38
+ * Prefix to distinguish between a contract deployed with code `0x80`
39
+ * and `RLP([])` (also having the value `0x80`).
40
+ *
41
+ * Otherwise the creation of the code hash for the `0x80` contract
42
+ * will be the same as the hash of the empty trie which leads to
43
+ * misbehaviour in the underlying trie library.
44
+ */
45
+ export const CODEHASH_PREFIX = utf8ToBytes('c')
46
+
47
+ /**
48
+ * Default StateManager implementation for the VM.
49
+ *
50
+ * The state manager abstracts from the underlying data store
51
+ * by providing higher level access to accounts, contract code
52
+ * and storage slots.
53
+ *
54
+ * The default state manager implementation uses a
55
+ * `@feelyourprotocol/mpt` trie as a data backend.
56
+ *
57
+ * Note that there is a `SimpleStateManager` dependency-free state
58
+ * manager implementation available shipped with the `@feelyourprotocol/statemanager`
59
+ * package which might be an alternative to this implementation
60
+ * for many basic use cases.
61
+ */
62
+ export class MerkleStateManager implements StateManagerInterface {
63
+ protected _debug: Debugger
64
+ protected _caches?: Caches
65
+
66
+ originalStorageCache: OriginalStorageCache
67
+
68
+ protected _trie: MerklePatriciaTrie
69
+ protected _storageTries: { [key: string]: MerklePatriciaTrie }
70
+
71
+ protected readonly _prefixCodeHashes: boolean
72
+ protected readonly _prefixStorageTrieKeys: boolean
73
+
74
+ public readonly common: Common
75
+
76
+ protected _checkpointCount: number
77
+
78
+ private keccakFunction: Function
79
+
80
+ /**
81
+ * StateManager is run in DEBUG mode (default: false)
82
+ * Taken from DEBUG environment variable
83
+ *
84
+ * Safeguards on debug() calls are added for
85
+ * performance reasons to avoid string literal evaluation
86
+ * @hidden
87
+ */
88
+ protected readonly DEBUG: boolean = false
89
+
90
+ /**
91
+ * Instantiate the StateManager interface.
92
+ */
93
+ constructor(opts: MerkleStateManagerOpts = {}) {
94
+ // Skip DEBUG calls unless 'ethjs' included in environmental DEBUG variables
95
+ this.DEBUG = isDebugEnabled('ethjs')
96
+
97
+ this._debug = debugDefault('statemanager:merkle')
98
+
99
+ this.common = opts.common ?? new Common({ chain: Mainnet })
100
+
101
+ this._checkpointCount = 0
102
+
103
+ this._trie = opts.trie ?? new MerklePatriciaTrie({ useKeyHashing: true, common: this.common })
104
+ this._storageTries = {}
105
+
106
+ this.keccakFunction = opts.common?.customCrypto.keccak256 ?? keccak_256
107
+
108
+ this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this))
109
+
110
+ this._prefixCodeHashes = opts.prefixCodeHashes ?? true
111
+ this._prefixStorageTrieKeys = opts.prefixStorageTrieKeys ?? false
112
+
113
+ this._caches = opts.caches
114
+ }
115
+
116
+ /**
117
+ * Gets the account associated with `address` or `undefined` if account does not exist
118
+ * @param address - Address of the `account` to get
119
+ */
120
+ async getAccount(address: Address): Promise<Account | undefined> {
121
+ const elem = this._caches?.account?.get(address)
122
+ if (elem !== undefined) {
123
+ return elem.accountRLP !== undefined ? createAccountFromRLP(elem.accountRLP) : undefined
124
+ }
125
+
126
+ const rlp = await this._trie.get(address.bytes)
127
+ const account = rlp !== null ? createAccountFromRLP(rlp) : undefined
128
+ if (this.DEBUG) {
129
+ this._debug(`Get account ${address} from DB (${account ? 'exists' : 'non-existent'})`)
130
+ }
131
+ this._caches?.account?.put(address, account)
132
+ return account
133
+ }
134
+
135
+ /**
136
+ * Saves an account into state under the provided `address`.
137
+ * @param address - Address under which to store `account`
138
+ * @param account - The account to store or undefined if to be deleted
139
+ */
140
+ async putAccount(address: Address, account: Account | undefined): Promise<void> {
141
+ if (this.DEBUG) {
142
+ this._debug(
143
+ `Save account address=${address} nonce=${account?.nonce} balance=${
144
+ account?.balance
145
+ } contract=${account && account.isContract() ? 'yes' : 'no'} empty=${
146
+ account && account.isEmpty() ? 'yes' : 'no'
147
+ }`,
148
+ )
149
+ }
150
+ if (this._caches?.account === undefined) {
151
+ const trie = this._trie
152
+ if (account !== undefined) {
153
+ await trie.put(address.bytes, account.serialize())
154
+ } else {
155
+ await trie.del(address.bytes)
156
+ }
157
+ } else {
158
+ if (account !== undefined) {
159
+ this._caches.account?.put(address, account)
160
+ } else {
161
+ this._caches.account?.del(address)
162
+ }
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Gets the account associated with `address`, modifies the given account
168
+ * fields, then saves the account into state. Account fields can include
169
+ * `nonce`, `balance`, `storageRoot`, and `codeHash`.
170
+ * @param address - Address of the account to modify
171
+ * @param accountFields - Object containing account fields and values to modify
172
+ */
173
+ async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
174
+ await modifyAccountFields(this, address, accountFields)
175
+ }
176
+
177
+ /**
178
+ * Deletes an account from state under the provided `address`.
179
+ * @param address - Address of the account which should be deleted
180
+ */
181
+ async deleteAccount(address: Address) {
182
+ if (this.DEBUG) {
183
+ this._debug(`Delete account ${address}`)
184
+ }
185
+
186
+ this._caches?.deleteAccount(address)
187
+
188
+ if (this._caches?.account === undefined) {
189
+ await this._trie.del(address.bytes)
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Adds `value` to the state trie as code, and sets `codeHash` on the account
195
+ * corresponding to `address` to reference this.
196
+ * @param address - Address of the `account` to add the `code` for
197
+ * @param value - The value of the `code`
198
+ */
199
+ async putCode(address: Address, value: Uint8Array): Promise<void> {
200
+ const codeHash = this.keccakFunction(value)
201
+
202
+ if (this._caches?.code !== undefined) {
203
+ this._caches!.code!.put(address, value)
204
+ } else {
205
+ const key = this._prefixCodeHashes ? concatBytes(CODEHASH_PREFIX, codeHash) : codeHash
206
+ await this._getCodeDB().put(key, value)
207
+ }
208
+
209
+ if (this.DEBUG) {
210
+ this._debug(`Update codeHash (-> ${short(codeHash)}) for account ${address}`)
211
+ }
212
+
213
+ if ((await this.getAccount(address)) === undefined) {
214
+ await this.putAccount(address, new Account())
215
+ }
216
+ await this.modifyAccountFields(address, { codeHash })
217
+ }
218
+
219
+ /**
220
+ * Gets the code corresponding to the provided `address`.
221
+ * @param address - Address to get the `code` for
222
+ * @returns {Promise<Uint8Array>} - Resolves with the code corresponding to the provided address.
223
+ * Returns an empty `Uint8Array` if the account has no associated code.
224
+ */
225
+ async getCode(address: Address): Promise<Uint8Array> {
226
+ const elem = this._caches?.code?.get(address)
227
+ if (elem !== undefined) {
228
+ return elem.code ?? new Uint8Array(0)
229
+ }
230
+ const account = await this.getAccount(address)
231
+ if (!account) {
232
+ return new Uint8Array(0)
233
+ }
234
+ if (!account.isContract()) {
235
+ return new Uint8Array(0)
236
+ }
237
+ const key = this._prefixCodeHashes
238
+ ? concatBytes(CODEHASH_PREFIX, account.codeHash)
239
+ : account.codeHash
240
+ const code = (await this._trie.database().get(key)) ?? new Uint8Array(0)
241
+
242
+ this._caches?.code?.put(address, code)
243
+ return code
244
+ }
245
+
246
+ async getCodeSize(address: Address): Promise<number> {
247
+ const contractCode = await this.getCode(address)
248
+ return contractCode.length
249
+ }
250
+
251
+ /**
252
+ * Gets the storage trie for the EVM-internal account identified by the provided address/hash.
253
+ * If the storage trie is not in the local cache ('this._storageTries'),
254
+ * generates a new storage trie object based on a lookup (shallow copy from 'this._trie'),
255
+ * applies the storage root of the provided rootAccount (or an
256
+ * empty trie root if no rootAccount is provided), and stores the new entry
257
+ * in the local cache.
258
+ * @param addressOrHash Address (or other object) with populated 'bytes', or a raw Uint8Array.
259
+ * Used to identify the requested storage trie in the local cache and define the
260
+ * prefix used when creating a new storage trie.
261
+ * @param rootAccount (Optional) Account object whose 'storageRoot' is to be used as
262
+ * the root of the new storageTrie returned when there is no pre-existing trie.
263
+ * If left undefined, the EMPTY_TRIE_ROOT will be used as the root instead.
264
+ * @returns storage MerklePatriciaTrie object
265
+ * @private
266
+ */
267
+ // TODO PR: have a better interface for hashed address pull?
268
+ protected _getStorageTrie(
269
+ addressOrHash: Address | { bytes: Uint8Array } | Uint8Array,
270
+ rootAccount?: Account,
271
+ ): MerklePatriciaTrie {
272
+ // use hashed key for lookup from storage cache
273
+ const addressBytes: Uint8Array =
274
+ addressOrHash instanceof Uint8Array ? addressOrHash : this.keccakFunction(addressOrHash.bytes)
275
+ // Using deprecated bytesToUnprefixedHex for performance: used as object keys for trie cache lookups.
276
+ const addressHex: string = bytesToUnprefixedHex(addressBytes)
277
+ let storageTrie = this._storageTries[addressHex]
278
+ if (storageTrie === undefined) {
279
+ const keyPrefix = this._prefixStorageTrieKeys ? addressBytes.slice(0, 7) : undefined
280
+ storageTrie = this._trie.shallowCopy(false, { keyPrefix })
281
+ if (rootAccount !== undefined) {
282
+ storageTrie.root(rootAccount.storageRoot)
283
+ } else {
284
+ storageTrie.root(storageTrie.EMPTY_TRIE_ROOT)
285
+ }
286
+ storageTrie.flushCheckpoints()
287
+ this._storageTries[addressHex] = storageTrie
288
+ }
289
+ return storageTrie
290
+ }
291
+
292
+ /**
293
+ * Gets the storage trie for an account from the storage
294
+ * cache or does a lookup.
295
+ * @private
296
+ */
297
+ protected _getAccountTrie(): MerklePatriciaTrie {
298
+ return this._trie
299
+ }
300
+
301
+ /**
302
+ * Gets the storage trie for an account from the storage
303
+ * cache or does a lookup.
304
+ * @private
305
+ */
306
+ protected _getCodeDB(): DB {
307
+ return this._trie.database()
308
+ }
309
+
310
+ /**
311
+ * Gets the storage value associated with the provided `address` and `key`. This method returns
312
+ * the shortest representation of the stored value.
313
+ * @param address - Address of the account to get the storage for
314
+ * @param key - Key in the account's storage to get the value for. Must be 32 bytes long.
315
+ * @returns - The storage value for the account
316
+ * corresponding to the provided address at the provided key.
317
+ * If this does not exist an empty `Uint8Array` is returned.
318
+ */
319
+ async getStorage(address: Address, key: Uint8Array): Promise<Uint8Array> {
320
+ if (key.length !== 32) {
321
+ throw EthereumJSErrorWithoutCode('Storage key must be 32 bytes long')
322
+ }
323
+ const cachedValue = this._caches?.storage?.get(address, key)
324
+ if (cachedValue !== undefined) {
325
+ const decoded = RLP.decode(cachedValue ?? new Uint8Array(0)) as Uint8Array
326
+ return decoded
327
+ }
328
+
329
+ const account = await this.getAccount(address)
330
+ if (!account) {
331
+ return new Uint8Array()
332
+ }
333
+ const trie = this._getStorageTrie(address, account)
334
+ const value = await trie.get(key)
335
+ this._caches?.storage?.put(address, key, value ?? hexToBytes('0x80'))
336
+ const decoded = RLP.decode(value ?? new Uint8Array(0)) as Uint8Array
337
+ return decoded
338
+ }
339
+
340
+ /**
341
+ * Modifies the storage trie of an account.
342
+ * @private
343
+ * @param address - Address of the account whose storage is to be modified
344
+ * @param modifyTrie - Function to modify the storage trie of the account
345
+ */
346
+ protected async _modifyContractStorage(
347
+ address: Address,
348
+ account: Account,
349
+ modifyTrie: (storageTrie: MerklePatriciaTrie, done: Function) => void,
350
+ ): Promise<void> {
351
+ // eslint-disable-next-line no-async-promise-executor
352
+ return new Promise(async (resolve) => {
353
+ const storageTrie = this._getStorageTrie(address, account)
354
+
355
+ modifyTrie(storageTrie, async () => {
356
+ // update storage cache
357
+ // Using deprecated bytesToUnprefixedHex for performance: used as object keys for trie cache lookups.
358
+ const addressHex = bytesToUnprefixedHex(address.bytes)
359
+ this._storageTries[addressHex] = storageTrie
360
+
361
+ // update contract storageRoot
362
+ account.storageRoot = storageTrie.root()
363
+ await this.putAccount(address, account)
364
+ resolve()
365
+ })
366
+ })
367
+ }
368
+
369
+ protected async _writeContractStorage(
370
+ address: Address,
371
+ account: Account,
372
+ key: Uint8Array,
373
+ value: Uint8Array,
374
+ ) {
375
+ await this._modifyContractStorage(address, account, async (storageTrie, done) => {
376
+ if (value instanceof Uint8Array && value.length) {
377
+ // format input
378
+ const encodedValue = RLP.encode(value)
379
+ if (this.DEBUG) {
380
+ this._debug(`Update contract storage for account ${address} to ${short(value)}`)
381
+ }
382
+ await storageTrie.put(key, encodedValue)
383
+ } else {
384
+ // deleting a value
385
+ if (this.DEBUG) {
386
+ this._debug(`Delete contract storage for account`)
387
+ }
388
+ await storageTrie.del(key)
389
+ }
390
+ done()
391
+ })
392
+ }
393
+
394
+ /**
395
+ * Adds value to the state trie for the `account`
396
+ * corresponding to `address` at the provided `key`.
397
+ * @param address - Address to set a storage value for
398
+ * @param key - Key to set the value at. Must be 32 bytes long.
399
+ * @param value - Value to set at `key` for account corresponding to `address`.
400
+ * Cannot be more than 32 bytes. Leading zeros are stripped.
401
+ * If it is a empty or filled with zeros, deletes the value.
402
+ */
403
+ async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise<void> {
404
+ if (key.length !== 32) {
405
+ throw EthereumJSErrorWithoutCode('Storage key must be 32 bytes long')
406
+ }
407
+
408
+ if (value.length > 32) {
409
+ throw EthereumJSErrorWithoutCode('Storage value cannot be longer than 32 bytes')
410
+ }
411
+
412
+ const account = await this.getAccount(address)
413
+ if (!account) {
414
+ throw EthereumJSErrorWithoutCode('putStorage() called on non-existing account')
415
+ }
416
+
417
+ value = unpadBytes(value)
418
+ this._caches?.storage?.put(address, key, RLP.encode(value)) ??
419
+ (await this._writeContractStorage(address, account, key, value))
420
+ }
421
+
422
+ /**
423
+ * Clears all storage entries for the account corresponding to `address`.
424
+ * @param address - Address to clear the storage of
425
+ */
426
+ async clearStorage(address: Address): Promise<void> {
427
+ let account = await this.getAccount(address)
428
+ if (!account) {
429
+ account = new Account()
430
+ }
431
+ this._caches?.storage?.clearStorage(address)
432
+ await this._modifyContractStorage(address, account, (storageTrie, done) => {
433
+ storageTrie.root(storageTrie.EMPTY_TRIE_ROOT)
434
+ done()
435
+ })
436
+ }
437
+
438
+ /**
439
+ * Checkpoints the current state of the StateManager instance.
440
+ * State changes that follow can then be committed by calling
441
+ * `commit` or `reverted` by calling rollback.
442
+ */
443
+ async checkpoint(): Promise<void> {
444
+ this._trie.checkpoint()
445
+ this._caches?.checkpoint()
446
+ this._checkpointCount++
447
+ }
448
+
449
+ /**
450
+ * Commits the current change-set to the instance since the
451
+ * last call to checkpoint.
452
+ */
453
+ async commit(): Promise<void> {
454
+ // setup trie checkpointing
455
+ await this._trie.commit()
456
+ this._caches?.commit()
457
+ this._checkpointCount--
458
+
459
+ if (this._checkpointCount === 0) {
460
+ await this.flush()
461
+ this.originalStorageCache.clear()
462
+ }
463
+
464
+ if (this.DEBUG) {
465
+ this._debug(`state checkpoint committed`)
466
+ }
467
+ }
468
+
469
+ /**
470
+ * Reverts the current change-set to the instance since the
471
+ * last call to checkpoint.
472
+ */
473
+ async revert(): Promise<void> {
474
+ // setup trie checkpointing
475
+ await this._trie.revert()
476
+ this._caches?.revert()
477
+
478
+ this._storageTries = {}
479
+
480
+ this._checkpointCount--
481
+
482
+ if (this._checkpointCount === 0) {
483
+ await this.flush()
484
+ this.originalStorageCache.clear()
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Writes all cache items to the trie
490
+ */
491
+ async flush(): Promise<void> {
492
+ const codeItems = this._caches?.code?.flush() ?? []
493
+ for (const item of codeItems) {
494
+ const addr = createAddressFromString(`0x${item[0]}`)
495
+
496
+ const code = item[1].code
497
+ if (code === undefined) {
498
+ continue
499
+ }
500
+
501
+ // update code in database
502
+ const codeHash = this.keccakFunction(code)
503
+ const key = this._prefixCodeHashes ? concatBytes(CODEHASH_PREFIX, codeHash) : codeHash
504
+ await this._getCodeDB().put(key, code)
505
+
506
+ // update code root of associated account
507
+ if ((await this.getAccount(addr)) === undefined) {
508
+ await this.putAccount(addr, new Account())
509
+ }
510
+ await this.modifyAccountFields(addr, { codeHash })
511
+ }
512
+ const storageItems = this._caches?.storage?.flush() ?? []
513
+ for (const item of storageItems) {
514
+ const address = createAddressFromString(`0x${item[0]}`)
515
+ const keyHex = item[1]
516
+ const keyBytes = unprefixedHexToBytes(keyHex)
517
+ const value = item[2]
518
+
519
+ const decoded = RLP.decode(value ?? new Uint8Array(0)) as Uint8Array
520
+ const account = await this.getAccount(address)
521
+ if (account) {
522
+ await this._writeContractStorage(address, account, keyBytes, decoded)
523
+ }
524
+ }
525
+
526
+ const accountItems = this._caches?.account?.flush() ?? []
527
+ for (const item of accountItems) {
528
+ const addressHex = item[0]
529
+ const addressBytes = unprefixedHexToBytes(addressHex)
530
+ const elem = item[1]
531
+ if (elem.accountRLP === undefined) {
532
+ const trie = this._trie
533
+ await trie.del(addressBytes)
534
+ } else {
535
+ const trie = this._trie
536
+ await trie.put(addressBytes, elem.accountRLP)
537
+ }
538
+ }
539
+ }
540
+
541
+ /**
542
+ * Gets the state-root of the Merkle-Patricia trie representation
543
+ * of the state of this StateManager. Will error if there are uncommitted
544
+ * checkpoints on the instance.
545
+ * @returns {Promise<Uint8Array>} - Returns the state-root of the `StateManager`
546
+ */
547
+ async getStateRoot(): Promise<Uint8Array> {
548
+ await this.flush()
549
+ return this._trie.root()
550
+ }
551
+
552
+ /**
553
+ * Sets the state of the instance to that represented
554
+ * by the provided `stateRoot`. Will error if there are uncommitted
555
+ * checkpoints on the instance or if the state root does not exist in
556
+ * the state trie.
557
+ * @param stateRoot - The state-root to reset the instance to
558
+ */
559
+ async setStateRoot(stateRoot: Uint8Array, clearCache: boolean = true): Promise<void> {
560
+ await this.flush()
561
+
562
+ if (!equalsBytes(stateRoot, this._trie.EMPTY_TRIE_ROOT)) {
563
+ const hasRoot = await this._trie.checkRoot(stateRoot)
564
+ if (!hasRoot) {
565
+ throw EthereumJSErrorWithoutCode('State trie does not contain state root')
566
+ }
567
+ }
568
+
569
+ this._trie.root(stateRoot)
570
+ if (clearCache) {
571
+ this._caches?.clear()
572
+ }
573
+ this._storageTries = {}
574
+ }
575
+
576
+ /**
577
+ * Dumps the RLP-encoded storage values for an `account` specified by `address`.
578
+ * @param address - The address of the `account` to return storage for
579
+ * @returns {Promise<StorageDump>} - The state of the account as an `Object` map.
580
+ * Keys are are the storage keys, values are the storage values as strings.
581
+ * Both are represented as hex strings without the `0x` prefix.
582
+ */
583
+ async dumpStorage(address: Address): Promise<StorageDump> {
584
+ await this.flush()
585
+ const account = await this.getAccount(address)
586
+ if (!account) {
587
+ throw EthereumJSErrorWithoutCode(`dumpStorage f() can only be called for an existing account`)
588
+ }
589
+ const trie = this._getStorageTrie(address, account)
590
+
591
+ return trie.getValueMap().then((value) => {
592
+ return value.values
593
+ })
594
+ }
595
+
596
+ /**
597
+ Dumps a limited number of RLP-encoded storage values for an account specified by `address`,
598
+ starting from `startKey` or greater.
599
+ @param address - The address of the `account` to return storage for.
600
+ @param startKey - The bigint representation of the smallest storage key that will be returned.
601
+ @param limit - The maximum number of storage values that will be returned.
602
+ @returns {Promise<StorageRange>} - A {@link StorageRange} object that will contain at most `limit` entries in its `storage` field.
603
+ The object will also contain `nextKey`, the next (hashed) storage key after the range included in `storage`.
604
+ */
605
+ async dumpStorageRange(address: Address, startKey: bigint, limit: number): Promise<StorageRange> {
606
+ if (!Number.isSafeInteger(limit) || limit < 0) {
607
+ throw EthereumJSErrorWithoutCode(`Limit is not a proper uint.`)
608
+ }
609
+
610
+ await this.flush()
611
+ const account = await this.getAccount(address)
612
+ if (!account) {
613
+ throw EthereumJSErrorWithoutCode(`Account does not exist.`)
614
+ }
615
+
616
+ const trie = this._getStorageTrie(address, account)
617
+
618
+ return trie.getValueMap(startKey, limit).then((value) => {
619
+ const values = value.values
620
+ const dump = Object.create(null)
621
+ for (const key of Object.keys(values)) {
622
+ const val = values[key]
623
+ dump[key] = {
624
+ key: null,
625
+ value: val,
626
+ }
627
+ }
628
+
629
+ return {
630
+ storage: dump,
631
+ nextKey: value.nextKey,
632
+ }
633
+ })
634
+ }
635
+
636
+ /**
637
+ * Initializes the provided genesis state into the state trie.
638
+ * Will error if there are uncommitted checkpoints on the instance.
639
+ * @param initState address -> balance | [balance, code, storage]
640
+ */
641
+ async generateCanonicalGenesis(initState: any): Promise<void> {
642
+ if (this._checkpointCount !== 0) {
643
+ throw EthereumJSErrorWithoutCode('Cannot create genesis state with uncommitted checkpoints')
644
+ }
645
+ if (this.DEBUG) {
646
+ this._debug(`Save genesis state into the state trie`)
647
+ }
648
+ const addresses = Object.keys(initState)
649
+ for (const address of addresses) {
650
+ const addr = createAddressFromString(address)
651
+ const state = initState[address]
652
+ if (!Array.isArray(state)) {
653
+ // Prior format: address -> balance
654
+ const account = createAccount({ balance: state })
655
+ await this.putAccount(addr, account)
656
+ } else {
657
+ // New format: address -> [balance, code, storage]
658
+ const [balance, code, storage, nonce] = state
659
+ const account = createAccount({ balance, nonce })
660
+ await this.putAccount(addr, account)
661
+ if (code !== undefined) {
662
+ await this.putCode(addr, toBytes(code))
663
+ }
664
+ if (storage !== undefined) {
665
+ for (const [key, value] of storage) {
666
+ await this.putStorage(addr, toBytes(key), toBytes(value))
667
+ }
668
+ }
669
+ }
670
+ }
671
+ await this.flush()
672
+ }
673
+
674
+ /**
675
+ * Checks whether there is a state corresponding to a stateRoot
676
+ */
677
+ async hasStateRoot(root: Uint8Array): Promise<boolean> {
678
+ return this._trie.checkRoot(root)
679
+ }
680
+
681
+ /**
682
+ * Copies the current instance of the `StateManager`
683
+ * at the last fully committed point, i.e. as if all current
684
+ * checkpoints were reverted.
685
+ *
686
+ * Caches are downleveled (so: adopted for short-term usage)
687
+ * by default.
688
+ *
689
+ * This means in particular:
690
+ * 1. For caches instantiated as an LRU cache type
691
+ * the copy() method will instantiate with an ORDERED_MAP cache
692
+ * instead, since copied instances are mostly used in
693
+ * short-term usage contexts and LRU cache instantiation would create
694
+ * a large overhead here.
695
+ * 2. The underlying trie object is initialized with 0 cache size
696
+ *
697
+ * Both adoptions can be deactivated by setting `downlevelCaches` to
698
+ * `false`.
699
+ *
700
+ * Cache values are generally not copied along regardless of the
701
+ * `downlevelCaches` setting.
702
+ */
703
+ shallowCopy(downlevelCaches = true): MerkleStateManager {
704
+ const common = this.common.copy()
705
+ common.setHardfork(this.common.hardfork())
706
+
707
+ const cacheSize = !downlevelCaches ? this._trie['_opts']['cacheSize'] : 0
708
+ const trie = this._trie.shallowCopy(false, { cacheSize })
709
+ const prefixCodeHashes = this._prefixCodeHashes
710
+ const prefixStorageTrieKeys = this._prefixStorageTrieKeys
711
+
712
+ return new MerkleStateManager({
713
+ common,
714
+ trie,
715
+ prefixStorageTrieKeys,
716
+ prefixCodeHashes,
717
+ caches: this._caches?.shallowCopy(downlevelCaches),
718
+ })
719
+ }
720
+
721
+ /**
722
+ * Clears all underlying caches
723
+ */
724
+ clearCaches() {
725
+ this._caches?.clear()
726
+ }
727
+
728
+ /**
729
+ * Returns the applied key for a given address
730
+ * Used for saving preimages
731
+ * @param address - The address to return the applied key
732
+ * @returns {Uint8Array} - The applied key (e.g. hashed address)
733
+ */
734
+ getAppliedKey(address: Uint8Array): Uint8Array {
735
+ return this._trie['appliedKey'](address)
736
+ }
737
+ }