|
1 |
| -module.exports = StorageReader |
2 |
| - |
3 |
| -function StorageReader (stateManager) { |
4 |
| - this._stateManager = stateManager |
5 |
| - this._storageCache = new Map() |
6 |
| -} |
7 |
| - |
8 |
| -const proto = StorageReader.prototype |
9 |
| - |
10 |
| -proto.getContractStorage = function getContractStorage (address, key, cb) { |
11 |
| - const self = this |
12 |
| - const addressHex = address.toString('hex') |
13 |
| - const keyHex = key.toString('hex') |
14 |
| - |
15 |
| - self._stateManager.getContractStorage(address, key, function (err, current) { |
16 |
| - if (err) return cb(err) |
17 |
| - |
18 |
| - let map = null |
19 |
| - if (!self._storageCache.has(addressHex)) { |
20 |
| - map = new Map() |
21 |
| - self._storageCache.set(addressHex, map) |
22 |
| - } else { |
23 |
| - map = self._storageCache.get(addressHex) |
24 |
| - } |
25 |
| - |
26 |
| - let original = null |
27 |
| - |
28 |
| - if (map.has(keyHex)) { |
29 |
| - original = map.get(keyHex) |
30 |
| - } else { |
31 |
| - map.set(keyHex, current) |
32 |
| - original = current |
33 |
| - } |
34 |
| - |
35 |
| - cb(null, { |
36 |
| - original, |
37 |
| - current |
| 1 | +module.exports = class StorageReader { |
| 2 | + constructor (stateManager) { |
| 3 | + this._stateManager = stateManager |
| 4 | + this._storageCache = new Map() |
| 5 | + } |
| 6 | + |
| 7 | + getContractStorage (address, key, cb) { |
| 8 | + const addressHex = address.toString('hex') |
| 9 | + const keyHex = key.toString('hex') |
| 10 | + |
| 11 | + this._stateManager.getContractStorage(address, key, (err, current) => { |
| 12 | + if (err) return cb(err) |
| 13 | + |
| 14 | + let map = null |
| 15 | + if (!this._storageCache.has(addressHex)) { |
| 16 | + map = new Map() |
| 17 | + this._storageCache.set(addressHex, map) |
| 18 | + } else { |
| 19 | + map = this._storageCache.get(addressHex) |
| 20 | + } |
| 21 | + |
| 22 | + let original = null |
| 23 | + |
| 24 | + if (map.has(keyHex)) { |
| 25 | + original = map.get(keyHex) |
| 26 | + } else { |
| 27 | + map.set(keyHex, current) |
| 28 | + original = current |
| 29 | + } |
| 30 | + |
| 31 | + cb(null, { |
| 32 | + original, |
| 33 | + current |
| 34 | + }) |
38 | 35 | })
|
39 |
| - }) |
| 36 | + } |
40 | 37 | }
|
0 commit comments