Skip to content

Commit 49d62cc

Browse files
authored
Merge pull request ethereumjs#445 from davidmurdoch/storage-trie-cache-fix
Ensure contract storage cache is cleared when state root is updated
2 parents f2bcbc4 + 73de8f5 commit 49d62cc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/state/stateManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ proto.setStateRoot = function (stateRoot, cb) {
399399
if (stateRoot === self._trie.EMPTY_TRIE_ROOT) {
400400
self._trie.root = stateRoot
401401
self._cache.clear()
402+
self._storageTries = {}
402403
return cb()
403404
}
404405
self._trie.checkRoot(stateRoot, function (err, hasRoot) {
@@ -407,6 +408,7 @@ proto.setStateRoot = function (stateRoot, cb) {
407408
} else {
408409
self._trie.root = stateRoot
409410
self._cache.clear()
411+
self._storageTries = {}
410412
cb()
411413
}
412414
})

tests/api/state/stateManager.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ tape('StateManager', (t) => {
2929
const getAccount = promisify((...args) => stateManager.getAccount(...args))
3030
const commit = promisify((...args) => stateManager.commit(...args))
3131
const setStateRoot = promisify((...args) => stateManager.setStateRoot(...args))
32+
const putContractStorage = promisify((...args) => stateManager.putContractStorage(...args))
33+
const getContractStorage = promisify((...args) => stateManager.getContractStorage(...args))
3234

35+
// test account storage cache
3336
const initialStateRoot = await getStateRoot()
3437
await checkpoint()
3538
await putAccount(addressBuffer, account)
@@ -45,6 +48,20 @@ tape('StateManager', (t) => {
4548
const account2 = await getAccount(addressBuffer)
4649
st.equal(account2.balance.toString('hex'), '', 'account value is set to 0 in original state root')
4750

51+
// test contract storage cache
52+
await checkpoint()
53+
const key = Buffer.from('0x1234')
54+
const value = Buffer.from('0x1234')
55+
await putContractStorage(addressBuffer, key, value)
56+
57+
const contract0 = await getContractStorage(addressBuffer, key)
58+
st.equal(contract0.toString('hex'), value.toString('hex'), 'contract key\'s value is set in the _storageTries cache')
59+
60+
await commit()
61+
await setStateRoot(initialStateRoot)
62+
const contract1 = await getContractStorage(addressBuffer, key)
63+
st.equal(contract1.toString('hex'), '', 'contract key\'s value is unset in the _storageTries cache')
64+
4865
st.end()
4966
})
5067

0 commit comments

Comments
 (0)