-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StateManager interface simplification
Simplifies the stateManager interface used by the VM by removing dependencies on the following methods: * getAccountBalance * putAccountBalance * copy
- Loading branch information
1 parent
7de7a5b
commit c346a04
Showing
7 changed files
with
158 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module.exports = StorageReader | ||
|
||
function StorageReader (stateManager) { | ||
this._stateManager = stateManager | ||
this._storageCache = new Map() | ||
} | ||
|
||
const proto = StorageReader.prototype | ||
|
||
proto.getContractStorage = function getContractStorage (address, key, cb) { | ||
const self = this | ||
const addressHex = address.toString('hex') | ||
const keyHex = key.toString('hex') | ||
|
||
self._stateManager.getContractStorage(address, key, function (err, current) { | ||
if (err) return cb(err) | ||
|
||
let map = null | ||
if (!self._storageCache.has(addressHex)) { | ||
map = new Map() | ||
self._storageCache.set(addressHex, map) | ||
} else { | ||
map = self._storageCache.get(addressHex) | ||
} | ||
|
||
let original = null | ||
|
||
if (map.has(keyHex)) { | ||
original = map.get(keyHex) | ||
} else { | ||
map.set(keyHex, current) | ||
original = current | ||
} | ||
|
||
cb(null, { | ||
original, | ||
current | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const { promisify } = require('util') | ||
const tape = require('tape') | ||
const StorageReader = require('../../lib/storageReader') | ||
|
||
const mkStateManagerMock = () => { | ||
let i = 0 | ||
return { | ||
getContractStorage: function (address, key, cb) { | ||
cb(null, i++) | ||
} | ||
} | ||
} | ||
|
||
tape('StateManager', (t) => { | ||
t.test('should get value from stateManager', async (st) => { | ||
const storageReader = new StorageReader(mkStateManagerMock()) | ||
const getContractStorage = promisify((...args) => storageReader.getContractStorage(...args)) | ||
const { | ||
original, | ||
current | ||
} = await getContractStorage(Buffer.from([1]), Buffer.from([1, 1])) | ||
|
||
st.equal(original, 0) | ||
st.equal(current, 0) | ||
st.end() | ||
}) | ||
|
||
t.test('should retrieve original from cache', async (st) => { | ||
const storageReader = new StorageReader(mkStateManagerMock()) | ||
const getContractStorage = promisify((...args) => storageReader.getContractStorage(...args)) | ||
await getContractStorage(Buffer.from([1]), Buffer.from([1, 1])) | ||
const { | ||
original, | ||
current | ||
} = await getContractStorage(Buffer.from([1]), Buffer.from([1, 1])) | ||
|
||
st.equal(original, 0) | ||
st.equal(current, 1) | ||
st.end() | ||
}) | ||
|
||
t.test('should cache keys separately', async (st) => { | ||
const storageReader = new StorageReader(mkStateManagerMock()) | ||
const getContractStorage = promisify((...args) => storageReader.getContractStorage(...args)) | ||
await getContractStorage(Buffer.from([1]), Buffer.from([1, 1])) | ||
const { | ||
original, | ||
current | ||
} = await getContractStorage(Buffer.from([1]), Buffer.from([1, 2])) | ||
|
||
st.equal(original, 1) | ||
st.equal(current, 1) | ||
st.end() | ||
}) | ||
|
||
t.test('should cache addresses separately', async (st) => { | ||
const storageReader = new StorageReader(mkStateManagerMock()) | ||
const getContractStorage = promisify((...args) => storageReader.getContractStorage(...args)) | ||
await getContractStorage(Buffer.from([1]), Buffer.from([1, 1])) | ||
const { | ||
original, | ||
current | ||
} = await getContractStorage(Buffer.from([2]), Buffer.from([1, 1])) | ||
|
||
st.equal(original, 1) | ||
st.equal(current, 1) | ||
st.end() | ||
}) | ||
|
||
t.test('should forward errors from stateManager.getContractStorage', async (st) => { | ||
const storageReader = new StorageReader({ | ||
getContractStorage: (address, key, cb) => cb(new Error('test')) | ||
}) | ||
const getContractStorage = promisify((...args) => storageReader.getContractStorage(...args)) | ||
|
||
await getContractStorage(Buffer.from([2]), Buffer.from([1, 1])) | ||
.then(() => t.fail('should have returned error')) | ||
.catch((e) => t.equal(e.message, 'test')) | ||
|
||
st.end() | ||
}) | ||
}) |