-
Notifications
You must be signed in to change notification settings - Fork 143
/
ethereumContext.tsx
904 lines (812 loc) · 24.2 KB
/
ethereumContext.tsx
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
import { Buffer } from 'buffer'
import React, { createContext, useEffect, useState, useRef } from 'react'
import { Block } from '@ethereumjs/block'
import { Common, Chain, HardforkTransitionConfig } from '@ethereumjs/common'
import {
EVM,
EvmError,
getActivePrecompiles,
InterpreterStep,
} from '@ethereumjs/evm'
import { RunState } from '@ethereumjs/evm/dist/cjs/interpreter'
import { Opcode, OpcodeList } from '@ethereumjs/evm/src/opcodes'
import { TypedTransaction, TxData, TransactionFactory } from '@ethereumjs/tx'
import { Address, Account, bytesToHex } from '@ethereumjs/util'
import { RunTxOpts, VM } from '@ethereumjs/vm'
import { Common as EOFCommon } from '@ethjs-eof/common'
// @ts-ignore it confused with pre-EOF version
import { createEVM, EVM as EOFEVM } from '@ethjs-eof/evm'
// @ts-ignore it confused with pre-EOF version
import { createTxFromTxData as createTxFromTxDataEOF } from '@ethjs-eof/tx'
// @ts-ignore it confused with pre-EOF version
import { VM as EOFVM, runTx as runTxEOF } from '@ethjs-eof/vm'
import OpcodesMeta from 'opcodes.json'
import PrecompiledMeta from 'precompiled.json'
import {
IReferenceItem,
IReferenceItemMetaList,
IInstruction,
IStorage,
IExecutionState,
IChain,
ITransientStorage,
} from 'types'
import {
CURRENT_FORK,
EOF_ENABLED_FORK,
EOF_FORK_NAME,
FORKS_WITH_TIMESTAMPS,
} from 'util/constants'
import {
calculateOpcodeDynamicFee,
calculatePrecompiledDynamicFee,
} from 'util/gas'
import { toHex, fromBuffer } from 'util/string'
let vm: VM | EOFVM
let common: Common | EOFCommon
let currentOpcodes: OpcodeList | undefined
const storageMemory = new Map()
const transientStorageMemory = new Map<string, Map<string, string>>()
const privateKey = Buffer.from(
'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
'hex',
)
const accountBalance = 18 // 1eth
const accountAddress = Address.fromPrivateKey(privateKey)
const contractAddress = Address.generate(accountAddress, 1n)
const gasLimit = 0xffffffffffffn
const postMergeHardforkNames: Array<string> = ['merge', 'shanghai', 'cancun']
export const prevrandaoDocName = '44_merge'
const EOF_EIPS = [
663, 3540, 3670, 4200, 4750, 5450, 6206, 7069, 7480, 7620, 7692, 7698,
]
type ContextProps = {
common: Common | EOFCommon | undefined
chains: IChain[]
forks: HardforkTransitionConfig[]
selectedChain: IChain | undefined
selectedFork: HardforkTransitionConfig | undefined
opcodes: IReferenceItem[]
precompiled: IReferenceItem[]
instructions: IInstruction[]
deployedContractAddress: string | undefined
isExecuting: boolean
executionState: IExecutionState
vmError: string | undefined
areForksLoaded: boolean
onChainChange: (chainId: number) => void
onForkChange: (forkName: string) => void
transactionData: (
byteCode: string,
value: bigint,
to?: Address,
) => Promise<TypedTransaction | TxData | undefined>
loadInstructions: (byteCode: string) => void
startExecution: (byteCode: string, value: bigint, data: string) => void
startTransaction: (tx: TypedTransaction | TxData) => Promise<{
error?: EvmError
returnValue: Uint8Array
createdAddress: Address | undefined
}>
continueExecution: () => void
addBreakpoint: (instructionId: number) => void
removeBreakpoint: (instructionId: number) => void
nextExecution: () => void
resetExecution: () => void
}
const initialExecutionState: IExecutionState = {
stack: [],
storage: [],
transientStorage: [],
memory: undefined,
programCounter: undefined,
totalGas: undefined,
currentGas: undefined,
returnValue: undefined,
}
export const EthereumContext = createContext<ContextProps>({
common: undefined,
chains: [],
forks: [],
selectedChain: undefined,
selectedFork: undefined,
opcodes: [],
precompiled: [],
instructions: [],
deployedContractAddress: undefined,
isExecuting: false,
executionState: initialExecutionState,
vmError: undefined,
areForksLoaded: false,
onChainChange: () => undefined,
onForkChange: () => undefined,
transactionData: () =>
new Promise((resolve) => {
resolve(undefined)
}),
loadInstructions: () => undefined,
startExecution: () => undefined,
startTransaction: () => Promise.reject(),
continueExecution: () => undefined,
addBreakpoint: () => undefined,
removeBreakpoint: () => undefined,
nextExecution: () => undefined,
resetExecution: () => undefined,
})
export const CheckIfAfterMergeHardfork = (forkName?: string) => {
if (forkName == null) {
return false
}
return postMergeHardforkNames.indexOf(forkName) > -1
}
export const EthereumProvider: React.FC<{}> = ({ children }) => {
const [chains, setChains] = useState<IChain[]>([])
const [forks, setForks] = useState<HardforkTransitionConfig[]>([])
const [selectedChain, setSelectedChain] = useState<IChain>()
const [selectedFork, setSelectedFork] = useState<HardforkTransitionConfig>()
const [opcodes, setOpcodes] = useState<IReferenceItem[]>([])
const [precompiled, setPrecompiled] = useState<IReferenceItem[]>([])
const [instructions, setInstructions] = useState<IInstruction[]>([])
const [isExecuting, setIsExecuting] = useState(false)
const [executionState, setExecutionState] = useState<IExecutionState>(
initialExecutionState,
)
const [deployedContractAddress, setDeployedContractAddress] = useState<
string | undefined
>()
const [vmError, setVmError] = useState<string | undefined>()
const [areForksLoaded, setAreForksLoaded] = useState<boolean>(false)
const nextStepFunction = useRef<any>()
const isExecutionPaused = useRef(true)
const breakpointIds = useRef<number[]>([])
useEffect(() => {
void (async () => {
await initVmInstance()
setAreForksLoaded(true)
})()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
/**
* Initializes the EVM instance.
*/
const initVmInstance = async (fork?: string) => {
const forkName = fork == EOF_FORK_NAME ? EOF_ENABLED_FORK : fork
common = new EOFCommon({
chain: Chain.Mainnet,
hardfork: forkName || CURRENT_FORK,
eips: forkName === EOF_ENABLED_FORK ? EOF_EIPS : [],
})
vm = await EOFVM.create({ common })
const evm = await createEVM({
common,
})
currentOpcodes = evm.getActiveOpcodes()
if (forks.length === 0) {
_loadChainAndForks(common)
}
_loadOpcodes()
_loadPrecompiled()
_setupStateManager()
_setupAccount()
vm.evm.events?.on(
'step',
(e: InterpreterStep, contFunc: ((result?: any) => void) | undefined) => {
_stepInto(e, contFunc)
},
)
}
/**
* Callback on changing the EVM chain.
* @param chainId The chain ID.
*/
const onChainChange = (chainId: number) => {
const chain = chains.find((chain) => chain.id === chainId)
if (chain) {
void (async () => {
// NOTE: we first setup the vm to make sure it has the correct version before refreshing the fork details
await initVmInstance(selectedFork?.name)
setSelectedChain(chain)
resetExecution()
})()
}
}
/**
* Callback on changing the EVM hard fork.
* @param forkName The hard fork name.
*/
const onForkChange = (forkName: string) => {
const fork = forks.find((f) => f.name === forkName)
if (fork) {
;(async () => {
// NOTE: we first setup the vm to make sure it has the correct version before refreshing the fork details
await initVmInstance(forkName)
setSelectedFork(fork)
resetExecution()
})()
}
}
/*
* Deploys the contract code to the EVM.
* @param byteCode The contract bytecode.
* @returns The deployed contract transaction data.
*/
const transactionData = async (data: string, value: bigint, to?: Address) => {
const account = await vm.stateManager.getAccount(accountAddress)
const txData = {
to,
value: value,
gasLimit,
gasPrice: 10,
data: '0x' + data,
nonce: account?.nonce,
}
if (vm.evm instanceof EOFEVM) {
return createTxFromTxDataEOF(txData).sign(privateKey)
} else {
return TransactionFactory.fromTxData(txData).sign(privateKey)
}
}
/**
* Loads contract instructions to the context state.
* @param byteCode The contract bytecode.
*/
const loadInstructions = (byteCode: string) => {
const opcodes = currentOpcodes
const instructions: IInstruction[] = []
if (!opcodes) {
return
}
for (let i = 0; i < byteCode.length; i += 2) {
const instruction = parseInt(byteCode.slice(i, i + 2), 16)
// The id to reference back with breakpoints
const id = i / 2
const opcode = opcodes.get(instruction)
if (!opcode) {
instructions.push({
id,
name: 'INVALID',
})
} else if (opcode.name === 'PUSH') {
const count = parseInt(opcode.fullName.slice(4), 10) * 2
instructions.push({
id,
name: opcode.fullName,
value: byteCode.slice(i + 2, i + 2 + count),
})
i += count
} else {
instructions.push({
id,
name: opcode.fullName,
})
}
}
setInstructions(instructions)
}
/**
* Starts EVM execution of the instructions.
* @param byteCode The contract bytecode.
* @param value The callvalue.
* @param data The calldata.
*/
const startExecution = async (
byteCode: string,
value: bigint,
data: string,
) => {
vm.stateManager.putContractCode(
contractAddress,
Buffer.from(byteCode, 'hex'),
)
transientStorageMemory.clear()
startTransaction(await transactionData(data, value, contractAddress))
}
/**
* Starts EVM execution of the instructions.
* @param tx The transaction data to run from.
*/
const startTransaction = (tx: TypedTransaction | TxData | undefined) => {
// always start paused
isExecutionPaused.current = true
setIsExecuting(true)
setVmError(undefined)
// starting execution via deployed contract's transaction
return vm
.runTx({ tx: tx as TypedTransaction, block: _getBlock() })
.then(({ execResult, totalGasSpent, createdAddress }) => {
_loadRunState({
totalGasSpent,
runState: execResult.runState,
newContractAddress: createdAddress,
returnValue: execResult.returnValue,
exceptionError: execResult.exceptionError,
})
return {
error: execResult.exceptionError,
returnValue: execResult.returnValue,
createdAddress: createdAddress,
}
})
.finally(() => setIsExecuting(false))
}
/**
* Resets EVM execution state to the initial state.
*/
const resetExecution = () => {
setInstructions([])
setExecutionState(initialExecutionState)
setDeployedContractAddress(undefined)
setVmError(undefined)
isExecutionPaused.current = true
breakpointIds.current = []
nextStepFunction.current = undefined
setIsExecuting(false)
}
/**
* Adds a breakpoint to pause the EVM execution at a given instruction.
* @param instructionId The instruction id provided by in the `instructions[]`.
*/
const addBreakpoint = (instructionId: number) => {
breakpointIds.current.push(instructionId)
setInstructions(
instructions.map((el) => {
if (el.id === instructionId) {
return {
...el,
hasBreakpoint: true,
}
}
return el
}),
)
}
/**
* Removes previously added breakpoint.
* @param instructionId The instruction id provided by in the `instructions[]`.
* @see `addBreakpoint`
*/
const removeBreakpoint = (instructionId: number) => {
breakpointIds.current = breakpointIds.current.filter(
(id) => id !== instructionId,
)
setInstructions(
instructions.map((el) => {
if (el.id === instructionId) {
return {
...el,
hasBreakpoint: false,
}
}
return el
}),
)
}
/**
* Resumes the EVM execution.
*/
const continueExecution = () => {
isExecutionPaused.current = false
nextExecution()
}
/**
* Runs the next EVM execution.
*/
const nextExecution = () => {
// FIXME: Instead of allowing to get into exception,
// prevent from executing when all instructions have been completed.
try {
if (nextStepFunction.current) {
nextStepFunction.current()
}
} catch (_e) {
const error = _e as Error
if (error.message.match(/Callback was already called/i)) {
return
}
throw error
}
}
const _loadChainAndForks = (common: Common | EOFCommon) => {
const chainIds: number[] = []
const chainNames: string[] = []
const forks: HardforkTransitionConfig[] = []
// iterate over TS enum to pick key,val
for (const chain in Chain) {
if (isNaN(Number(chain))) {
chainNames.push(chain)
} else {
chainIds.push(parseInt(chain))
}
}
setChains(
chainIds.map((chainId, index) => {
return { id: chainId, name: chainNames[index] }
}),
)
setSelectedChain({ id: chainIds[0], name: chainNames[0] })
let currentForkFound = false
common.hardforks().forEach((rawFork) => {
// FIXME: After shanghai, timestamps are used, so support them in addition
// to blocks, and in the meantime use timestamp as the block num.
const block = rawFork.block
? rawFork.block
: FORKS_WITH_TIMESTAMPS[rawFork.name]
const fork = {
...rawFork,
block,
}
if (typeof fork.block === 'number') {
forks.push(fork)
// set initially selected fork
if (!currentForkFound && fork.name === CURRENT_FORK) {
setSelectedFork(fork)
currentForkFound = true
}
}
})
forks.push({
name: EOF_FORK_NAME,
block: 1710338135,
})
setForks(forks)
}
const extractDocFromOpcode = (op: Opcode) => {
const meta = OpcodesMeta as IReferenceItemMetaList
// TODO: need to implement proper selection of doc according to selected fork (maybe similar to dynamic gas fee)
// Hack for "difficulty" -> "prevrandao" replacement for "merge" HF
if (
CheckIfAfterMergeHardfork(selectedFork?.name) &&
toHex(op.code) == '44'
) {
return {
...meta[prevrandaoDocName],
...{
opcodeOrAddress: toHex(op.code),
staticFee: op.fee,
minimumFee: 0,
name: 'PREVRANDAO',
},
}
}
return {
...meta[toHex(op.code)],
...{
opcodeOrAddress: toHex(op.code),
staticFee: op.fee,
minimumFee: 0,
name: op.fullName,
},
}
}
const _loadOpcodes = () => {
const opcodes: IReferenceItem[] = []
currentOpcodes?.forEach((op: Opcode) => {
const opcode = extractDocFromOpcode(op)
opcode.minimumFee = parseInt(
calculateOpcodeDynamicFee(opcode, common, {}),
)
opcodes.push(opcode)
})
setOpcodes(opcodes)
}
const _loadPrecompiled = () => {
const precompiled: IReferenceItem[] = []
const addressIterator = getActivePrecompiles(common).keys()
let result = addressIterator.next()
while (!result.done) {
const meta = PrecompiledMeta as IReferenceItemMetaList
const addressString = '0x' + result.value.slice(-2)
const contract = {
...meta[addressString],
...{
opcodeOrAddress: addressString,
minimumFee: 0,
name: meta[addressString].name,
},
}
contract.minimumFee = parseInt(
calculatePrecompiledDynamicFee(contract, common, {}),
)
precompiled.push(contract)
result = addressIterator.next()
}
setPrecompiled(precompiled)
}
function traceStorageMethodCalls(obj: any) {
const handler = {
get(target: any, propKey: any) {
const origMethod = target[propKey]
return (...args: any[]) => {
const result = origMethod.apply(target, args)
if (propKey == 'clearContractStorage' || propKey == 'clearStorage') {
_clearContractStorage(args[0])
}
if (propKey == 'putContractStorage' || propKey == 'putStorage') {
_putContractStorage(args[0], args[1], args[2])
}
return result
}
},
}
return new Proxy(obj, handler)
}
function traceTransientStorageMethodCalls(obj: any) {
const handler = {
get(target: any, propKey: any) {
const origMethod = target[propKey]
return (...args: any[]) => {
const result = origMethod.apply(target, args)
if (propKey == 'put') {
const [rawAddress, rawKey, rawValue] = args as [
Address,
Uint8Array,
Uint8Array,
]
const address = rawAddress.toString()
const key = bytesToHex(rawKey)
const value = bytesToHex(rawValue)
let addressTransientStorage = transientStorageMemory.get(address)
// Add the address to the transient storage
if (addressTransientStorage === undefined) {
transientStorageMemory.set(address, new Map<string, string>())
addressTransientStorage = transientStorageMemory.get(
address,
) as Map<string, string>
}
addressTransientStorage.set(key, value)
}
return result
}
},
}
return new Proxy(obj, handler)
}
// In this function we create a proxy EEI object that will intercept
// putContractStorage and clearContractStorage and route them to our
// implementations at _putContractStorage and _clearContractStorage
// respectively AFTER applying the original methods.
// This is necessary in order to handle storage operations easily.
const _setupStateManager = () => {
const evm = vm.evm
// Storage handler
const proxyStateManager = traceStorageMethodCalls(evm.stateManager)
if (evm instanceof EVM) {
evm.stateManager.putContractStorage = proxyStateManager.putContractStorage
evm.stateManager.clearContractStorage =
proxyStateManager.clearContractStorage
// Transient storage handler
const transientStorageMethodProxy = traceTransientStorageMethodCalls(
evm.transientStorage,
)
evm.transientStorage.put = transientStorageMethodProxy.put
} else if (evm instanceof EOFEVM) {
// @ts-ignore confused package
evm.stateManager.putStorage = proxyStateManager.putStorage
// @ts-ignore confused package
evm.stateManager.clearStorage = proxyStateManager.clearStorage
// Transient storage handler
const transientStorageMethodProxy = traceTransientStorageMethodCalls(
evm.transientStorage,
)
evm.transientStorage.put = transientStorageMethodProxy.put
// NOTE: they renamed a few functions with the EOF changes
// @ts-ignore it's confused because of the pre eof version
evm.stateManager.putContractCode = evm.stateManager.putCode
vm.runTx = (opts: RunTxOpts) => runTxEOF(vm, opts)
}
storageMemory.clear()
transientStorageMemory.clear()
}
const _setupAccount = () => {
// Add a fake account
const accountData = {
nonce: 2,
balance: BigInt(10 ** accountBalance),
}
const contractData = {
nonce: 0,
balance: 0,
}
vm.stateManager.putAccount(
accountAddress,
Account.fromAccountData(accountData),
)
vm.stateManager.putAccount(
contractAddress,
Account.fromAccountData(contractData),
)
}
const _loadRunState = ({
totalGasSpent,
runState,
newContractAddress,
returnValue,
exceptionError,
}: {
totalGasSpent: bigint
runState: RunState | undefined
newContractAddress?: Address
returnValue?: Uint8Array
exceptionError?: EvmError
}) => {
if (runState) {
const { programCounter: pc, stack, memory, memoryWordCount } = runState
_setExecutionState({
pc,
totalGasSpent,
stack: stack.getStack(),
memory: memory._store,
memoryWordCount,
returnValue,
})
}
if (exceptionError) {
setVmError(exceptionError.error)
} else if (newContractAddress) {
setDeployedContractAddress(newContractAddress.toString())
}
}
const _getBlock = () => {
// base fee is only applicable since london hardfork, ie block 12965000
if (selectedFork && (selectedFork.block || 0) < 12965000) {
return undefined
}
return Block.fromBlockData(
{
header: {
baseFeePerGas: 10,
gasLimit,
gasUsed: 60,
},
},
{ common },
)
}
const _stepInto = (
{
depth,
pc,
gasLeft,
opcode,
stack,
memory,
memoryWordCount,
}: InterpreterStep,
continueFunc: ((result?: any) => void) | undefined,
) => {
// We skip over the calls
if (depth !== 0 && continueFunc) {
continueFunc()
return
}
const totalGasSpent = gasLimit - gasLeft
_setExecutionState({
pc,
totalGasSpent,
stack,
memory,
memoryWordCount,
currentGas: opcode.fee,
})
nextStepFunction.current = continueFunc
if (isExecutionPaused.current === false) {
if (breakpointIds.current.includes(pc)) {
isExecutionPaused.current = true
} else {
nextExecution()
}
}
}
const _setExecutionState = ({
pc,
totalGasSpent,
stack,
memory,
memoryWordCount,
currentGas,
returnValue,
}: {
pc: number
totalGasSpent: bigint
stack: bigint[]
memory: Uint8Array
memoryWordCount: bigint
currentGas?: bigint | number
returnValue?: Uint8Array
}) => {
const storage: IStorage[] = []
storageMemory.forEach((sm, address) => {
sm.forEach((value: string, slot: string) => {
storage.push({ address, slot, value })
})
})
const transientStorage: ITransientStorage[] = []
for (const [address, entries] of transientStorageMemory.entries()) {
for (const [key, value] of entries.entries()) {
transientStorage.push({
address,
key,
value,
})
}
}
setExecutionState({
programCounter: pc,
stack: stack.map((value) => value.toString(16)).reverse(),
totalGas: totalGasSpent.toString(),
memory: fromBuffer(Buffer.from(memory)).substring(
0,
Number(memoryWordCount) * 64,
),
transientStorage,
storage,
currentGas: currentGas ? currentGas.toString() : undefined,
returnValue: returnValue
? Buffer.from(returnValue).toString('hex')
: undefined,
})
}
// Update storage slot `key` for contract `address`
// to `value` in our storage memory Map
const _putContractStorage = (
address: Address,
key: Buffer,
value: Buffer,
) => {
const addressText = address.toString()
const keyText = fromBuffer(key)
const valueText = fromBuffer(value)
if (value.length == 0) {
if (storageMemory.has(addressText)) {
const addressStorage = storageMemory.get(addressText)
addressStorage.delete(keyText)
if (addressStorage.size == 0) {
storageMemory.delete(addressText)
}
}
} else {
if (storageMemory.has(addressText)) {
storageMemory.get(addressText).set(keyText, valueText)
} else {
storageMemory.set(addressText, new Map([[keyText, valueText]]))
}
}
}
// Clear all storage slots of contract at `address` in our storage memory Map
const _clearContractStorage = (address: Address) => {
const addressText = address.toString()
storageMemory.delete(addressText)
}
return (
<EthereumContext.Provider
value={{
common,
chains,
forks,
selectedChain,
selectedFork,
opcodes,
precompiled,
instructions,
deployedContractAddress,
isExecuting,
executionState,
vmError,
areForksLoaded,
onChainChange,
onForkChange,
transactionData,
loadInstructions,
startExecution,
startTransaction,
continueExecution,
addBreakpoint,
removeBreakpoint,
nextExecution,
resetExecution,
}}
>
{children}
</EthereumContext.Provider>
)
}