@@ -5,7 +5,6 @@ const Account = require('ethereumjs-account').default
5
5
const Trie = require ( 'merkle-patricia-tree/secure' )
6
6
const BN = ethUtil . BN
7
7
const { getRequiredForkConfigAlias } = require ( './util' )
8
- const { HookedStateManager } = require ( '../dist/stateless' )
9
8
const StateManager = require ( '../dist/state/stateManager' ) . default
10
9
11
10
const VM = require ( '../dist/index.js' ) . default
@@ -29,9 +28,10 @@ async function runTestCase (options, testData, t) {
29
28
return
30
29
}
31
30
32
- // Hooked state manager proves necessary trie nodes
33
- // for a stateless execution
34
- let stateManager = new HookedStateManager ( )
31
+ let stateManager = new StateManager ( )
32
+ await promisify ( testUtil . setupPreConditions ) ( stateManager . _trie , testData )
33
+ const preStateRoot = stateManager . _trie . root
34
+
35
35
// Set up VM
36
36
let vm = new VM ( {
37
37
stateManager : stateManager ,
@@ -40,10 +40,33 @@ async function runTestCase (options, testData, t) {
40
40
if ( options . jsontrace ) {
41
41
hookVM ( vm , t )
42
42
}
43
- await promisify ( testUtil . setupPreConditions ) ( stateManager . _trie , testData )
44
43
45
- const preStateRoot = stateManager . _trie . root
46
- stateManager . origState = new StateManager ( { trie : new Trie ( stateManager . _trie . db . _leveldb , preStateRoot ) } )
44
+ // Determine set of all node hashes in the database
45
+ // before running the tx.
46
+ const existingKeys = new Set ( )
47
+ const it = stateManager . _trie . db . _leveldb . iterator ( )
48
+ const next = promisify ( it . next . bind ( it ) )
49
+ while ( true ) {
50
+ const key = await next ( )
51
+ if ( ! key ) break
52
+ existingKeys . add ( key . toString ( 'hex' ) )
53
+ }
54
+
55
+ // Hook leveldb.get and add any node that was fetched during execution
56
+ // to a bag of proof nodes, under the condition that this node existed
57
+ // before execution.
58
+ const proofNodes = new Map ( )
59
+ const getFunc = stateManager . _trie . db . _leveldb . get . bind ( stateManager . _trie . db . _leveldb )
60
+ stateManager . _trie . db . _leveldb . get = ( key , opts , cb ) => {
61
+ getFunc ( key , opts , ( err , v ) => {
62
+ if ( ! err && v ) {
63
+ if ( existingKeys . has ( key . toString ( 'hex' ) ) ) {
64
+ proofNodes . set ( key . toString ( 'hex' ) , v )
65
+ }
66
+ }
67
+ cb ( err , v )
68
+ } )
69
+ }
47
70
48
71
try {
49
72
await vm . runTx ( { tx : tx , block : block } )
@@ -52,9 +75,10 @@ async function runTestCase (options, testData, t) {
52
75
}
53
76
t . equal ( stateManager . _trie . root . toString ( 'hex' ) , expectedPostStateRoot , 'the state roots should match' )
54
77
78
+ // Save bag of proof nodes to a new trie's underlying leveldb
55
79
const trie = new Trie ( null , preStateRoot )
56
80
const opStack = [ ]
57
- for ( const [ k , v ] of stateManager . proofNodes ) {
81
+ for ( const [ k , v ] of proofNodes ) {
58
82
opStack . push ( { type : 'put' , key : Buffer . from ( k , 'hex' ) , value : v } )
59
83
}
60
84
await promisify ( trie . db . batch . bind ( trie . db ) ) ( opStack )
0 commit comments