forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
89 lines (68 loc) · 2.67 KB
/
index.ts
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
import { Account, Address, toBuffer, setLengthLeft } from 'ethereumjs-util'
import { Block } from '@ethereumjs/block'
import Blockchain from '@ethereumjs/blockchain'
import Common from '@ethereumjs/common'
import VM from '../../'
const testData = require('./test-data')
const level = require('level')
async function main() {
const common = new Common({ chain: testData.network.toLowerCase() })
const validatePow = common.consensusType() === 'pow'
const validateBlocks = true
const blockchain = await Blockchain.create({
common,
validateConsensus: validatePow,
validateBlocks,
genesisBlock: getGenesisBlock(common)
})
// When verifying PoW, setting this cache improves the
// performance of subsequent runs of this script.
// Note that this optimization is a bit hacky and might
// not be working in the future though. :-)
if (validatePow) {
blockchain._ethash!.cacheDB = level('./.cachedb')
}
const vm = new VM({ blockchain, common })
await setupPreConditions(vm, testData)
await putBlocks(blockchain, common, testData)
await vm.runBlockchain(blockchain)
const blockchainHead = await vm.blockchain.getHead()
console.log('--- Finished processing the BlockChain ---')
console.log('New head:', '0x' + blockchainHead.hash().toString('hex'))
console.log('Expected:', testData.lastblockhash)
}
async function setupPreConditions(vm: VM, testData: any) {
await vm.stateManager.checkpoint()
for (const addr of Object.keys(testData.pre)) {
const { nonce, balance, storage, code } = testData.pre[addr]
const address = new Address(Buffer.from(addr.slice(2), 'hex'))
const account = Account.fromAccountData({ nonce, balance })
await vm.stateManager.putAccount(address, account)
for (const hexStorageKey of Object.keys(storage)) {
const val = Buffer.from(storage[hexStorageKey], 'hex')
const storageKey = setLengthLeft(Buffer.from(hexStorageKey, 'hex'), 32)
await vm.stateManager.putContractStorage(address, storageKey, val)
}
const codeBuf = Buffer.from(code.slice(2), 'hex')
await vm.stateManager.putContractCode(address, codeBuf)
}
await vm.stateManager.commit()
}
function getGenesisBlock(common: Common) {
const header = testData.genesisBlockHeader
const genesis = Block.genesis({ header }, { common })
return genesis
}
async function putBlocks(blockchain: any, common: Common, testData: any) {
for (const blockData of testData.blocks) {
const blockRlp = toBuffer(blockData.rlp)
const block = Block.fromRLPSerializedBlock(blockRlp, { common })
await blockchain.putBlock(block)
}
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})