-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathstatefulBinaryTreeStateManager.ts
More file actions
789 lines (703 loc) · 26.2 KB
/
statefulBinaryTreeStateManager.ts
File metadata and controls
789 lines (703 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import { BinaryTree } from '@ethereumjs/binarytree'
import { BinaryTreeAccessedStateType } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import type { Address, BinaryTreeExecutionWitness, PrefixedHexString } from '@ethereumjs/util'
import {
Account,
BINARY_TREE_CODE_CHUNK_SIZE,
BINARY_TREE_CODE_OFFSET,
BINARY_TREE_NODE_WIDTH,
BinaryTreeLeafType,
EthereumJSErrorWithoutCode,
KECCAK256_NULL,
MapDB,
bigIntToBytes,
bytesToBigInt,
bytesToHex,
chunkifyBinaryTreeCode,
createAddressFromString,
createPartialAccount,
createPartialAccountFromRLP,
decodeBinaryTreeLeafBasicData,
encodeBinaryTreeLeafBasicData,
equalsBytes,
generateBinaryTreeChunkSuffixes,
generateBinaryTreeCodeStems,
getBinaryTreeKeyForStorageSlot,
getBinaryTreeStem,
hexToBigInt,
hexToBytes,
isDebugEnabled,
padToEven,
setLengthLeft,
setLengthRight,
short,
unprefixedHexToBytes,
} from '@ethereumjs/util'
import { blake3 } from '@noble/hashes/blake3.js'
import { keccak_256 } from '@noble/hashes/sha3.js'
import debugDefault from 'debug'
import { OriginalStorageCache } from './cache/originalStorageCache.ts'
import { modifyAccountFields } from './util.ts'
import type {
AccountFields,
BinaryTreeAccessWitnessInterface,
BinaryTreeAccessedStateWithAddress,
GenesisState,
StateManagerInterface,
StorageDump,
StoragePair,
StorageRange,
} from '@ethereumjs/common'
import type { Debugger } from 'debug'
import type { Caches } from './cache/caches.ts'
import type { BinaryTreeState, StatefulBinaryTreeStateManagerOpts } from './types.ts'
const ZEROVALUE = '0x0000000000000000000000000000000000000000000000000000000000000000'
export class StatefulBinaryTreeStateManager implements StateManagerInterface {
protected _debug: Debugger
protected _caches?: Caches
preStateRoot: Uint8Array
originalStorageCache: OriginalStorageCache
hashFunction: (input: Uint8Array) => Uint8Array
protected _tree: BinaryTree
protected _checkpointCount: number
// Post-state provided from the executionWitness.
// Should not update. Used for comparing our computed post-state with the canonical one.
private _postState: BinaryTreeState = {}
/**
* StateManager is run in DEBUG mode (default: false)
* Taken from DEBUG environment variable
*
* Safeguards on debug() calls are added for
* performance reasons to avoid string literal evaluation
* @hidden
*/
protected readonly DEBUG: boolean = false
private keccakFunction: Function
constructor(opts: StatefulBinaryTreeStateManagerOpts) {
// Skip DEBUG calls unless 'ethjs' included in environmental DEBUG variables
this.DEBUG = isDebugEnabled('ethjs')
this._checkpointCount = 0
if (opts.common?.isActivatedEIP(7864) === false) {
throw EthereumJSErrorWithoutCode('EIP-7864 required for binary tree state management')
}
this.hashFunction = opts.hashFunction ?? blake3
this._tree =
opts.tree ??
new BinaryTree({
hashFunction: this.hashFunction,
db: new MapDB<string, string | Uint8Array>(),
useRootPersistence: false,
cacheSize: 0,
})
this._debug = debugDefault('statemanager:binarytree')
this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this))
this._caches = opts.caches
this.keccakFunction = keccak_256
this.preStateRoot = new Uint8Array(32) // Initial state root is zeroes
}
/**
* Gets the account associated with `address` or `undefined` if account does not exist
* @param address - Address of the `account` to get
*/
getAccount = async (address: Address): Promise<Account | undefined> => {
const elem = this._caches?.account?.get(address)
if (elem !== undefined) {
return elem.accountRLP !== undefined
? createPartialAccountFromRLP(elem.accountRLP)
: undefined
}
const stem = getBinaryTreeStem(this.hashFunction, address, 0)
// First retrieve the account "header" values from the trie
const accountValues = await this._tree.get(stem, [
BinaryTreeLeafType.BasicData,
BinaryTreeLeafType.CodeHash,
])
let account
if (accountValues[0] !== null && accountValues[0] !== undefined) {
const basicData = decodeBinaryTreeLeafBasicData(accountValues[0]!)
account = createPartialAccount({
version: basicData.version,
balance: basicData.balance,
nonce: basicData.nonce,
// Codehash is either untouched (i.e. null) or deleted (i.e. overwritten with zeros)
codeHash:
accountValues[1] === null ||
accountValues[1] === undefined ||
equalsBytes(accountValues[1], new Uint8Array(32))
? KECCAK256_NULL
: accountValues[1],
codeSize: basicData.codeSize,
storageRoot: KECCAK256_NULL,
})
} else if (accountValues[1] === undefined || accountValues[1] === null) {
// account does not exist if both basic fields and codehash are undefined
if (this.DEBUG) {
this._debug(`getAccount address=${address.toString()} from DB (non-existent)`)
}
this._caches?.account?.put(address, account)
}
if (this.DEBUG) {
this._debug(`getAccount address=${address.toString()} stem=${short(stem)}`)
}
return account
}
public initBinaryTreeExecutionWitness(
_blockNum: bigint,
executionWitness?: BinaryTreeExecutionWitness | null,
) {
if (executionWitness === null || executionWitness === undefined) {
const errorMsg = `Invalid executionWitness=${executionWitness} for initBinaryTreeExecutionWitness`
this._debug(errorMsg)
throw Error(errorMsg)
}
this.preStateRoot = hexToBytes(executionWitness.parentStateRoot) // set prestate root
// Populate the post-state from the executionWitness
const postStateRaw = executionWitness.stateDiff.flatMap(({ stem, suffixDiffs }) => {
const suffixDiffPairs = suffixDiffs.map(({ newValue, currentValue, suffix }) => {
const key = `${stem}${padToEven(Number(suffix).toString(16))}` as PrefixedHexString
// A postState value of null means there was no change from the preState.
// In this implementation, we therefore replace null with the preState.
const value = newValue ?? currentValue
return {
[key]: value,
}
})
return suffixDiffPairs
})
const postState = postStateRaw.reduce((prevValue, currentValue) => {
const acc = { ...prevValue, ...currentValue }
return acc
}, {})
this._postState = postState
this._debug(`initBinaryTreeExecutionWitness postState=${JSON.stringify(this._postState)}`)
}
/**
* Saves an account into state under the provided `address`.
* @param address - Address under which to store `account`
* @param account - The account to store or undefined if to be deleted
*/
putAccount = async (address: Address, account?: Account): Promise<void> => {
if (this.DEBUG) {
this._debug(
`putAccount address=${address} nonce=${account?.nonce} balance=${
account?.balance
} contract=${account && account.isContract() ? 'yes' : 'no'} empty=${
account && account.isEmpty() ? 'yes' : 'no'
}`,
)
}
if (this._caches?.account === undefined) {
if (account !== undefined) {
const stem = getBinaryTreeStem(this.hashFunction, address, 0)
const basicDataBytes = encodeBinaryTreeLeafBasicData(account)
await this._tree.put(
stem,
[BinaryTreeLeafType.BasicData, BinaryTreeLeafType.CodeHash],
[basicDataBytes, account.codeHash],
)
} else {
// Delete account
await this.deleteAccount(address)
}
} else {
if (account !== undefined) {
this._caches?.account?.put(address, account, true)
} else {
this._caches?.account?.del(address)
}
}
}
/**
* Deletes an account from state under the provided `address`.
* @param address - Address of the account which should be deleted
*/
deleteAccount = async (address: Address): Promise<void> => {
if (this.DEBUG) {
this._debug(`Delete account ${address}`)
}
this._caches?.deleteAccount(address)
if (this._caches?.account === undefined) {
const stem = getBinaryTreeStem(this.hashFunction, address)
// Special instance where we delete the account and revert the trie value to untouched
await this._tree.put(
stem,
[BinaryTreeLeafType.BasicData, BinaryTreeLeafType.CodeHash],
[null, null],
)
}
}
modifyAccountFields = async (address: Address, accountFields: AccountFields): Promise<void> => {
await modifyAccountFields(this, address, accountFields)
}
putCode = async (address: Address, value: Uint8Array): Promise<void> => {
if (this.DEBUG) {
this._debug(`putCode address=${address.toString()} value=${short(value)}`)
}
this._caches?.code?.put(address, value)
const codeHash = keccak_256(value)
if (equalsBytes(codeHash, KECCAK256_NULL)) {
// If the code hash is the null hash, no code has to be stored
return
}
if ((await this.getAccount(address)) === undefined) {
await this.putAccount(address, new Account())
}
if (this.DEBUG) {
this._debug(`Update codeHash (-> ${short(codeHash)}) for account ${address}`)
}
const codeChunks = chunkifyBinaryTreeCode(value)
const chunkStems = generateBinaryTreeCodeStems(codeChunks.length, address, this.hashFunction)
const chunkSuffixes: number[] = generateBinaryTreeChunkSuffixes(codeChunks.length)
// Put the code chunks corresponding to the first stem (up to 128 chunks)
await this._tree.put(
chunkStems[0],
chunkSuffixes.slice(
0,
chunkSuffixes.length <= BINARY_TREE_CODE_OFFSET
? chunkSuffixes.length
: BINARY_TREE_CODE_OFFSET,
),
codeChunks.slice(
0,
codeChunks.length <= BINARY_TREE_CODE_OFFSET ? codeChunks.length : BINARY_TREE_CODE_OFFSET,
),
)
// Put additional chunks under additional stems as applicable
for (let stem = 1; stem < chunkStems.length; stem++) {
const sliceStart = BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * (stem - 1)
const sliceEnd =
value.length <= BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * stem
? value.length
: BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * stem
await this._tree.put(
chunkStems[stem],
chunkSuffixes.slice(sliceStart, sliceEnd),
codeChunks.slice(sliceStart, sliceEnd),
)
}
await this.modifyAccountFields(address, {
codeHash,
codeSize: value.length,
})
}
getCode = async (address: Address): Promise<Uint8Array> => {
if (this.DEBUG) {
this._debug(`getCode address=${address.toString()}`)
}
const elem = this._caches?.code?.get(address)
if (elem !== undefined) {
return elem.code ?? new Uint8Array(0)
}
const account = await this.getAccount(address)
if (!account) {
return new Uint8Array(0)
}
if (!account.isContract()) {
return new Uint8Array(0)
}
// allocate the code
const codeSize = account.codeSize
const stems = generateBinaryTreeCodeStems(
Math.ceil(codeSize / BINARY_TREE_CODE_CHUNK_SIZE),
address,
this.hashFunction,
)
const chunkSuffixes = generateBinaryTreeChunkSuffixes(
Math.ceil(codeSize / BINARY_TREE_CODE_CHUNK_SIZE),
)
const chunksByStem = new Array(stems.length)
// Retrieve the code chunks stored in the first leaf node
chunksByStem[0] = await this._tree.get(
stems[0],
chunkSuffixes.slice(
0,
codeSize <= BINARY_TREE_CODE_OFFSET ? codeSize : BINARY_TREE_CODE_OFFSET,
),
)
// Retrieve code chunks on any additional stems
for (let stem = 1; stem < stems.length; stem++) {
const sliceStart = BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * (stem - 1)
const sliceEnd =
codeSize <= BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * stem
? codeSize
: BINARY_TREE_CODE_OFFSET + BINARY_TREE_NODE_WIDTH * stem
chunksByStem[stem] = await this._tree.get(
stems[stem],
chunkSuffixes.slice(sliceStart, sliceEnd),
)
}
const chunks = chunksByStem.flat()
const code = new Uint8Array(codeSize)
// Insert code chunks into final array (skipping PUSHDATA overflow indicator byte)
for (let x = 0; x < chunks.length; x++) {
if (chunks[x] === undefined)
throw EthereumJSErrorWithoutCode(`expected code chunk at index ${x}, got undefined`)
let lastChunkByteIndex = BINARY_TREE_CODE_CHUNK_SIZE
// Determine code ending byte (if we're on the last chunk)
if (x === chunks.length - 1) {
// On the last chunk, the slice either ends on a partial chunk (if codeSize doesn't exactly fit in full chunks), or a full chunk
lastChunkByteIndex = codeSize % BINARY_TREE_CODE_CHUNK_SIZE || BINARY_TREE_CODE_CHUNK_SIZE
}
code.set(
chunks[x]!.slice(1, lastChunkByteIndex + 1),
code.byteOffset + x * BINARY_TREE_CODE_CHUNK_SIZE,
)
}
this._caches?.code?.put(address, code)
return code
}
getCodeSize = async (address: Address): Promise<number> => {
const accountBytes = (
await this._tree.get(getBinaryTreeStem(this.hashFunction, address), [
BinaryTreeLeafType.BasicData,
])
)[0]
if (accountBytes === null) return 0
return decodeBinaryTreeLeafBasicData(accountBytes).codeSize
}
getStorage = async (address: Address, key: Uint8Array): Promise<Uint8Array> => {
if (key.length !== 32) {
throw EthereumJSErrorWithoutCode('Storage key must be 32 bytes long')
}
const cachedValue = this._caches?.storage?.get(address, key)
if (cachedValue !== undefined) {
const decoded = RLP.decode(cachedValue ?? new Uint8Array(0)) as Uint8Array
return decoded
}
const account = await this.getAccount(address)
if (!account) {
return new Uint8Array()
}
const storageKey = getBinaryTreeKeyForStorageSlot(
address,
bytesToBigInt(key),
this.hashFunction,
)
const value = await this._tree.get(storageKey.slice(0, 31), [storageKey[31]])
this._caches?.storage?.put(address, key, value[0] ?? hexToBytes('0x80'))
const decoded = (value[0] ?? new Uint8Array(0)) as Uint8Array
return setLengthLeft(decoded, 32)
}
putStorage = async (address: Address, key: Uint8Array, value: Uint8Array): Promise<void> => {
this._caches?.storage?.put(address, key, RLP.encode(value))
if (this._caches?.storage === undefined) {
const storageKey = getBinaryTreeKeyForStorageSlot(
address,
bytesToBigInt(key),
this.hashFunction,
)
await this._tree.put(storageKey.slice(0, 31), [storageKey[31]], [setLengthLeft(value, 32)])
}
}
clearStorage = async (address: Address): Promise<void> => {
this._caches?.storage?.clearStorage(address)
}
checkpoint = async (): Promise<void> => {
this._tree.checkpoint()
this._caches?.checkpoint()
this._checkpointCount++
}
commit = async (): Promise<void> => {
await this._tree.commit()
this._caches?.commit()
this._checkpointCount--
if (this._checkpointCount === 0) {
await this.flush()
this.originalStorageCache.clear()
}
if (this.DEBUG) {
this._debug(`state checkpoint committed`)
}
}
revert = async (): Promise<void> => {
await this._tree.revert()
this._caches?.revert()
this._checkpointCount--
if (this._checkpointCount === 0) {
await this.flush()
this.originalStorageCache.clear()
}
}
flush = async (): Promise<void> => {
const codeItems = this._caches?.code?.flush() ?? []
for (const item of codeItems) {
const addr = createAddressFromString(`0x${item[0]}`)
const code = item[1].code
if (code === undefined) {
continue
}
await this.putCode(addr, code)
}
const storageItems = this._caches?.storage?.flush() ?? []
for (const item of storageItems) {
const address = createAddressFromString(`0x${item[0]}`)
const keyHex = item[1]
const keyBytes = unprefixedHexToBytes(keyHex)
const value = item[2]
const decoded = RLP.decode(value ?? new Uint8Array(0)) as Uint8Array
const account = await this.getAccount(address)
if (account) {
await this.putStorage(address, keyBytes, decoded)
}
}
const accountItems = this._caches?.account?.flush() ?? []
for (const item of accountItems) {
const address = createAddressFromString(`0x${item[0]}`)
const elem = item[1]
if (elem.accountRLP === undefined) {
await this.deleteAccount(address)
} else {
const account = createPartialAccountFromRLP(elem.accountRLP)
await this.putAccount(address, account)
}
}
}
async getComputedValue(
accessedState: BinaryTreeAccessedStateWithAddress,
): Promise<PrefixedHexString | null> {
const { address, type } = accessedState
switch (type) {
case BinaryTreeAccessedStateType.BasicData: {
if (this._caches === undefined) {
const accountData = await this.getAccount(address)
if (accountData === undefined) {
return null
}
const basicDataBytes = encodeBinaryTreeLeafBasicData(accountData)
return bytesToHex(basicDataBytes)
} else {
const encodedAccount = this._caches?.account?.get(address)?.accountRLP
if (encodedAccount === undefined) {
return null
}
const basicDataBytes = encodeBinaryTreeLeafBasicData(
createPartialAccountFromRLP(encodedAccount),
)
return bytesToHex(basicDataBytes)
}
}
case BinaryTreeAccessedStateType.CodeHash: {
if (this._caches === undefined) {
const accountData = await this.getAccount(address)
if (accountData === undefined) {
return null
}
return bytesToHex(accountData.codeHash)
} else {
const encodedAccount = this._caches?.account?.get(address)?.accountRLP
if (encodedAccount === undefined) {
return null
}
return bytesToHex(createPartialAccountFromRLP(encodedAccount).codeHash)
}
}
case BinaryTreeAccessedStateType.Code: {
const { codeOffset } = accessedState
let code: Uint8Array | undefined | null = null
if (this._caches === undefined) {
code = await this.getCode(address)
if (code === undefined) {
return null
}
} else {
code = this._caches?.code?.get(address)?.code
if (code === undefined) {
return null
}
}
// we can only compare the actual code because to compare the first byte would
// be very tricky and impossible in certain scenarios like when the previous code chunk
// was not accessed and hence not even provided in the witness
// We are left-padding with two zeroes to get a 32-byte length, but these bytes should not be considered reliable
return bytesToHex(
setLengthLeft(
setLengthRight(
code.slice(codeOffset, codeOffset + BINARY_TREE_CODE_CHUNK_SIZE),
BINARY_TREE_CODE_CHUNK_SIZE,
),
BINARY_TREE_CODE_CHUNK_SIZE + 1,
),
)
}
case BinaryTreeAccessedStateType.Storage: {
const { slot } = accessedState
const key = setLengthLeft(bigIntToBytes(slot), 32)
let storage: Uint8Array | undefined | null = null
if (this._caches === undefined) {
storage = await this.getStorage(address, key)
if (storage === undefined) {
return null
}
} else {
storage = this._caches?.storage?.get(address, key)
}
if (storage === undefined) {
return null
}
return bytesToHex(setLengthLeft(storage, 32))
}
}
}
// Verifies that the witness post-state matches the computed post-state
async verifyBinaryTreePostState(
accessWitness: BinaryTreeAccessWitnessInterface,
): Promise<boolean> {
// track what all chunks were accessed so as to compare in the end if any chunks were missed
// in access while comparing against the provided poststate in the execution witness
const accessedChunks = new Map<string, boolean>()
// switch to false if postVerify fails
let postFailures = 0
for (const accessedState of accessWitness?.accesses() ?? []) {
const { address, type } = accessedState
let extraMeta = ''
if (accessedState.type === BinaryTreeAccessedStateType.Code) {
extraMeta = `codeOffset=${accessedState.codeOffset}`
} else if (accessedState.type === BinaryTreeAccessedStateType.Storage) {
extraMeta = `slot=${accessedState.slot}`
}
const { chunkKey } = accessedState
accessedChunks.set(chunkKey, true)
let computedValue: PrefixedHexString | null | undefined =
await this.getComputedValue(accessedState)
if (computedValue === undefined) {
this.DEBUG &&
this._debug(
`Missing computed value for address=${address} type=${type} ${extraMeta} chunkKey=${chunkKey}`,
)
postFailures++
continue
}
let canonicalValue: PrefixedHexString | null | undefined = this._postState[chunkKey]
if (canonicalValue === undefined) {
this.DEBUG &&
this._debug(
`Block accesses missing from postState for address=${address} type=${type} ${extraMeta} chunkKey=${chunkKey}`,
)
postFailures++
continue
}
// if the access type is code, then we can't match the first byte because since the computed value
// doesn't has the first byte for push data since previous chunk code itself might not be available
if (accessedState.type === BinaryTreeAccessedStateType.Code) {
computedValue = computedValue !== null ? `0x${computedValue.slice(4)}` : null
canonicalValue = canonicalValue !== null ? `0x${canonicalValue.slice(4)}` : null
} else if (
accessedState.type === BinaryTreeAccessedStateType.Storage &&
canonicalValue === null &&
computedValue === ZEROVALUE
) {
canonicalValue = ZEROVALUE
}
this._debug(`computed ${computedValue} canonical ${canonicalValue}`)
if (computedValue !== canonicalValue) {
if (type === BinaryTreeAccessedStateType.BasicData) {
this.DEBUG &&
this._debug(
`canonical value: `,
canonicalValue === null
? null
: decodeBinaryTreeLeafBasicData(hexToBytes(canonicalValue)),
)
this.DEBUG &&
this._debug(
`computed value: `,
computedValue === null
? null
: decodeBinaryTreeLeafBasicData(hexToBytes(computedValue)),
)
}
this.DEBUG &&
this._debug(
`Block accesses mismatch address=${address} type=${type} ${extraMeta} chunkKey=${chunkKey}`,
)
this.DEBUG && this._debug(`expected=${canonicalValue}`)
this.DEBUG && this._debug(`computed=${computedValue}`)
postFailures++
}
}
for (const canChunkKey of Object.keys(this._postState)) {
if (accessedChunks.get(canChunkKey) === undefined) {
this.DEBUG && this._debug(`Missing chunk access for canChunkKey=${canChunkKey}`)
postFailures++
}
}
const verifyPassed = postFailures === 0
this.DEBUG &&
this._debug(
`verifyBinaryTreePostState verifyPassed=${verifyPassed} postFailures=${postFailures}`,
)
return verifyPassed
}
getStateRoot(): Promise<Uint8Array> {
return Promise.resolve(this._tree.root())
}
setStateRoot(stateRoot: Uint8Array, clearCache?: boolean): Promise<void> {
this._tree.root(stateRoot)
clearCache === true && this.clearCaches()
return Promise.resolve()
}
hasStateRoot(root: Uint8Array): Promise<boolean> {
return this._tree.checkRoot(root)
}
dumpStorage?(_address: Address): Promise<StorageDump> {
throw EthereumJSErrorWithoutCode('Method not implemented.')
}
dumpStorageRange?(_address: Address, _startKey: bigint, _limit: number): Promise<StorageRange> {
throw EthereumJSErrorWithoutCode('Method not implemented.')
}
clearCaches(): void {
this._caches?.clear()
}
shallowCopy(_downlevelCaches?: boolean): StateManagerInterface {
throw EthereumJSErrorWithoutCode('Method not implemented.')
}
async checkChunkWitnessPresent(_address: Address, _codeOffset: number): Promise<boolean> {
throw EthereumJSErrorWithoutCode('Method not implemented.')
}
async generateCanonicalGenesis(genesisState: GenesisState) {
await this._tree.createRootNode()
await this.checkpoint()
for (const addressStr of Object.keys(genesisState) as PrefixedHexString[]) {
const addrState = genesisState[addressStr]
let nonce: PrefixedHexString | undefined
let balance: PrefixedHexString | bigint
let code: PrefixedHexString | undefined
let storage: StoragePair[] | undefined = []
if (Array.isArray(addrState)) {
;[balance, code, storage, nonce] = addrState
} else {
balance = hexToBigInt(addrState)
nonce = '0x1'
code = '0x'
}
const address = createAddressFromString(addressStr)
await this.putAccount(address, new Account())
const codeBuf = hexToBytes(code ?? '0x')
const codeHash = this.keccakFunction(codeBuf)
// Set contract storage
if (storage !== undefined) {
for (const [storageKey, valHex] of storage) {
const val = hexToBytes(valHex)
if (['0x', '0x00'].includes(bytesToHex(val))) {
continue
}
const key = setLengthLeft(hexToBytes(storageKey), 32)
await this.putStorage(address, key, val)
}
}
// Put contract code
await this.putCode(address, codeBuf)
// Put account data
const account = createPartialAccount({
nonce,
balance,
codeHash,
codeSize: codeBuf.byteLength,
})
await this.putAccount(address, account)
}
await this.commit()
await this.flush()
}
}