Skip to content

Commit 005b8ea

Browse files
committed
stateless: all state tests pass, greatly simplified version
1 parent 5b309b5 commit 005b8ea

File tree

2 files changed

+32
-108
lines changed

2 files changed

+32
-108
lines changed

lib/stateless.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/StatelessRunner.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const Account = require('ethereumjs-account').default
55
const Trie = require('merkle-patricia-tree/secure')
66
const BN = ethUtil.BN
77
const { getRequiredForkConfigAlias } = require('./util')
8-
const { HookedStateManager } = require('../dist/stateless')
98
const StateManager = require('../dist/state/stateManager').default
109

1110
const VM = require('../dist/index.js').default
@@ -29,9 +28,10 @@ async function runTestCase (options, testData, t) {
2928
return
3029
}
3130

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+
3535
// Set up VM
3636
let vm = new VM({
3737
stateManager: stateManager,
@@ -40,10 +40,33 @@ async function runTestCase (options, testData, t) {
4040
if (options.jsontrace) {
4141
hookVM(vm, t)
4242
}
43-
await promisify(testUtil.setupPreConditions)(stateManager._trie, testData)
4443

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+
}
4770

4871
try {
4972
await vm.runTx({ tx: tx, block: block })
@@ -52,9 +75,10 @@ async function runTestCase (options, testData, t) {
5275
}
5376
t.equal(stateManager._trie.root.toString('hex'), expectedPostStateRoot, 'the state roots should match')
5477

78+
// Save bag of proof nodes to a new trie's underlying leveldb
5579
const trie = new Trie(null, preStateRoot)
5680
const opStack = []
57-
for (const [k, v] of stateManager.proofNodes) {
81+
for (const [k, v] of proofNodes) {
5882
opStack.push({ type: 'put', key: Buffer.from(k, 'hex'), value: v })
5983
}
6084
await promisify(trie.db.batch.bind(trie.db))(opStack)

0 commit comments

Comments
 (0)