refactor storageRangeInternal

pull/62/head
Iuri Matias 5 years ago committed by aniket-engg
parent 5c81c76873
commit e411be3eda
  1. 52
      libs/remix-debug/src/storage/storageResolver.js

@ -26,7 +26,9 @@ class StorageResolver {
* @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value} * @param {Function} - callback - contains a map: [hashedKey] = {key, hashedKey, value}
*/ */
storageRange (tx, stepIndex, address, callback) { storageRange (tx, stepIndex, address, callback) {
this.storageRangeInternal(this, this.zeroSlot, tx, stepIndex, address, callback) this.storageRangeInternal(this, this.zeroSlot, tx, stepIndex, address).then((result) => {
callback(null, result)
}).catch(callback)
} }
/** /**
@ -63,13 +65,9 @@ class StorageResolver {
* @param {Function} - callback - {key, hashedKey, value} - * @param {Function} - callback - {key, hashedKey, value} -
*/ */
storageSlot (slot, tx, stepIndex, address, callback) { storageSlot (slot, tx, stepIndex, address, callback) {
this.storageRangeInternal(this, slot, tx, stepIndex, address, (error, storage) => { this.storageRangeInternal(this, slot, tx, stepIndex, address).then((storage) => {
if (error) { callback(null, storage[slot] !== undefined ? storage[slot] : null)
callback(error) }).catch(callback)
} else {
callback(null, storage[slot] !== undefined ? storage[slot] : null)
}
})
} }
/** /**
@ -88,26 +86,28 @@ class StorageResolver {
* even if the next 1000 items are not in the cache. * even if the next 1000 items are not in the cache.
* - If @arg slot is not cached, the corresponding value will be resolved and the next 1000 slots. * - If @arg slot is not cached, the corresponding value will be resolved and the next 1000 slots.
*/ */
storageRangeInternal (self, slotKey, tx, stepIndex, address, callback) { storageRangeInternal (self, slotKey, tx, stepIndex, address) {
var cached = this.fromCache(self, address) return new Promise((resolve, reject) => {
if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... var cached = this.fromCache(self, address)
return callback(null, cached.storage) if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000...
} return resolve(cached.storage)
this.storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, nextKey) => {
if (error) {
return callback(error)
} }
if (!storage[slotKey] && slotKey !== self.zeroSlot) { // we don't cache the zero slot (could lead to inconsistency) this.storageRangeWeb3Call(tx, address, slotKey, self.maxSize, (error, storage, nextKey) => {
storage[slotKey] = { if (error) {
key: slotKey, return reject(error)
value: self.zeroSlot
} }
} if (!storage[slotKey] && slotKey !== self.zeroSlot) { // we don't cache the zero slot (could lead to inconsistency)
self.toCache(self, address, storage) storage[slotKey] = {
if (slotKey === self.zeroSlot && !nextKey) { // only working if keys are sorted !! key: slotKey,
self.storageByAddress[address].complete = true value: self.zeroSlot
} }
callback(null, storage) }
self.toCache(self, address, storage)
if (slotKey === self.zeroSlot && !nextKey) { // only working if keys are sorted !!
self.storageByAddress[address].complete = true
}
return resolve(storage)
})
}) })
} }

Loading…
Cancel
Save