Skip to content

Commit 596f0b4

Browse files
committed
Use StateManager in run-blokchain example
1 parent 15661c7 commit 596f0b4

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

examples/run-blockchain/index.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@ import VM from '../../'
33
import Account from 'ethereumjs-account'
44
import * as utils from 'ethereumjs-util'
55
import { promisify } from 'util'
6+
import PStateManager from '../../lib/state/promisified'
7+
import { toBuffer } from 'ethereumjs-util'
68

7-
const Trie = require('merkle-patricia-tree/secure')
89
const Block = require('ethereumjs-block')
910
const Blockchain = require('ethereumjs-blockchain')
1011
const BlockHeader = require('ethereumjs-block/header.js')
11-
const levelMem = require('level-mem')
1212
const testData = require('./test-data')
1313
const level = require('level')
1414

15-
const rlp = utils.rlp
16-
1715
async function main() {
18-
const blockchainDB = levelMem()
19-
const state = new Trie(blockchainDB)
20-
2116
const hardfork = testData.network.toLowerCase()
2217

2318
const blockchain = new Blockchain({
24-
db: blockchainDB,
2519
hardfork,
2620
// This flag can be control whether the blocks are validated. This includes:
2721
// * Verifying PoW
@@ -34,12 +28,11 @@ async function main() {
3428
setEthashCache(blockchain)
3529

3630
const vm = new VM({
37-
state: state,
3831
blockchain: blockchain,
3932
hardfork,
4033
})
4134

42-
await setupPreConditions(blockchainDB, state, testData)
35+
await setupPreConditions(vm, testData)
4336

4437
await setGenesisBlock(blockchain, hardfork)
4538

@@ -60,29 +53,33 @@ function setEthashCache(blockchain: any) {
6053
}
6154
}
6255

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)
6458
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)
6760

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+
})
7066

71-
const storageTrie = new Trie(db)
67+
await psm.putAccount(addressBuf, account)
7268

7369
for (const hexStorageKey of Object.keys(acctData.storage)) {
7470
const val = utils.toBuffer(acctData.storage[hexStorageKey])
7571
const storageKey = utils.setLength(utils.toBuffer(hexStorageKey), 32)
7672

77-
await promisify(storageTrie.put.bind(storageTrie))(storageKey, rlp.encode(val))
73+
await psm.putContractStorage(addressBuf, storageKey, val)
7874
}
7975

8076
const codeBuf = utils.toBuffer(acctData.code)
8177

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)
8579
}
80+
81+
// This forces a cache flush, which is necessary here
82+
await psm.getStateRoot()
8683
}
8784

8885
async function setCode(account: Account, state: any, code: Buffer) {

0 commit comments

Comments
 (0)