Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure contract storage cache is cleared when state root is updated #445

Merged
merged 3 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/state/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ proto.setStateRoot = function (stateRoot, cb) {
if (stateRoot === self._trie.EMPTY_TRIE_ROOT) {
self._trie.root = stateRoot
self._cache.clear()
self._storageTries = {}
return cb()
}
self._trie.checkRoot(stateRoot, function (err, hasRoot) {
Expand All @@ -407,6 +408,7 @@ proto.setStateRoot = function (stateRoot, cb) {
} else {
self._trie.root = stateRoot
self._cache.clear()
self._storageTries = {}
cb()
}
})
Expand Down
17 changes: 17 additions & 0 deletions tests/api/state/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ tape('StateManager', (t) => {
const getAccount = promisify((...args) => stateManager.getAccount(...args))
const commit = promisify((...args) => stateManager.commit(...args))
const setStateRoot = promisify((...args) => stateManager.setStateRoot(...args))
const putContractStorage = promisify((...args) => stateManager.putContractStorage(...args))
const getContractStorage = promisify((...args) => stateManager.getContractStorage(...args))

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

// test contract storage cache
await checkpoint()
const key = Buffer.from('0x1234')
const value = Buffer.from('0x1234')
await putContractStorage(addressBuffer, key, value)

const contract0 = await getContractStorage(addressBuffer, key)
st.equal(contract0.toString('hex'), value.toString('hex'), 'contract key\'s value is set in the _storageTries cache')

await commit()
await setStateRoot(initialStateRoot)
const contract1 = await getContractStorage(addressBuffer, key)
st.equal(contract1.toString('hex'), '', 'contract key\'s value is unset in the _storageTries cache')

st.end()
})

Expand Down