@@ -3,25 +3,19 @@ import VM from '../../'
3
3
import Account from 'ethereumjs-account'
4
4
import * as utils from 'ethereumjs-util'
5
5
import { promisify } from 'util'
6
+ import PStateManager from '../../lib/state/promisified'
7
+ import { toBuffer } from 'ethereumjs-util'
6
8
7
- const Trie = require ( 'merkle-patricia-tree/secure' )
8
9
const Block = require ( 'ethereumjs-block' )
9
10
const Blockchain = require ( 'ethereumjs-blockchain' )
10
11
const BlockHeader = require ( 'ethereumjs-block/header.js' )
11
- const levelMem = require ( 'level-mem' )
12
12
const testData = require ( './test-data' )
13
13
const level = require ( 'level' )
14
14
15
- const rlp = utils . rlp
16
-
17
15
async function main ( ) {
18
- const blockchainDB = levelMem ( )
19
- const state = new Trie ( blockchainDB )
20
-
21
16
const hardfork = testData . network . toLowerCase ( )
22
17
23
18
const blockchain = new Blockchain ( {
24
- db : blockchainDB ,
25
19
hardfork,
26
20
// This flag can be control whether the blocks are validated. This includes:
27
21
// * Verifying PoW
@@ -34,12 +28,11 @@ async function main() {
34
28
setEthashCache ( blockchain )
35
29
36
30
const vm = new VM ( {
37
- state : state ,
38
31
blockchain : blockchain ,
39
32
hardfork,
40
33
} )
41
34
42
- await setupPreConditions ( blockchainDB , state , testData )
35
+ await setupPreConditions ( vm , testData )
43
36
44
37
await setGenesisBlock ( blockchain , hardfork )
45
38
@@ -60,29 +53,33 @@ function setEthashCache(blockchain: any) {
60
53
}
61
54
}
62
55
63
- async function setupPreConditions ( db : any , state : any , testData : any ) {
56
+ async function setupPreConditions ( vm : VM , testData : any ) {
57
+ const psm = new PStateManager ( vm . stateManager )
64
58
for ( const address of Object . keys ( testData . pre ) ) {
65
- const acctData = testData . pre [ address ]
66
- const account = new Account ( )
59
+ const addressBuf = utils . toBuffer ( address )
67
60
68
- account . nonce = utils . toBuffer ( acctData . nonce )
69
- account . balance = utils . toBuffer ( acctData . balance )
61
+ const acctData = testData . pre [ address ]
62
+ const account = new Account ( {
63
+ nonce : acctData . nonce ,
64
+ balance : acctData . balance ,
65
+ } )
70
66
71
- const storageTrie = new Trie ( db )
67
+ await psm . putAccount ( addressBuf , account )
72
68
73
69
for ( const hexStorageKey of Object . keys ( acctData . storage ) ) {
74
70
const val = utils . toBuffer ( acctData . storage [ hexStorageKey ] )
75
71
const storageKey = utils . setLength ( utils . toBuffer ( hexStorageKey ) , 32 )
76
72
77
- await promisify ( storageTrie . put . bind ( storageTrie ) ) ( storageKey , rlp . encode ( val ) )
73
+ await psm . putContractStorage ( addressBuf , storageKey , val )
78
74
}
79
75
80
76
const codeBuf = utils . toBuffer ( acctData . code )
81
77
82
- await setCode ( account , state , codeBuf )
83
-
84
- await promisify ( state . put . bind ( state ) ) ( utils . toBuffer ( address ) , account . serialize ( ) )
78
+ await psm . putContractCode ( addressBuf , codeBuf )
85
79
}
80
+
81
+ // This forces a cache flush, which is necessary here
82
+ await psm . getStateRoot ( )
86
83
}
87
84
88
85
async function setCode ( account : Account , state : any , code : Buffer ) {
0 commit comments