Commit 668daa86 authored by yann300's avatar yann300

make retrieve value from storage async

parent 734bd145
......@@ -8,11 +8,11 @@ var decodeInfo = require('./decodeInfo')
* @param {Map} storageContent - storage
* @return {Map} - decoded state variable
*/
function decodeState (stateVars, storageContent) {
async function decodeState (stateVars, storageContent) {
var ret = {}
for (var k in stateVars) {
var stateVar = stateVars[k]
ret[stateVar.name] = stateVar.type.decodeFromStorage(stateVar.storagelocation, storageContent)
ret[stateVar.name] = await stateVar.type.decodeFromStorage(stateVar.storagelocation, storageContent)
}
return ret
}
......
......@@ -23,10 +23,10 @@ class ArrayType extends RefType {
this.arraySize = arraySize
}
decodeFromStorage (location, storageContent) {
async decodeFromStorage (location, storageContent) {
var ret = []
var size = null
var slotValue = util.extractHexValue(location, storageContent, this.storageBytes)
var slotValue = await util.extractHexValue(location, storageContent, this.storageBytes)
var currentLocation = {
offset: 0,
slot: location.slot
......@@ -39,7 +39,8 @@ class ArrayType extends RefType {
}
var k = util.toBN(0)
for (; k.lt(size) && k.ltn(300); k.iaddn(1)) {
ret.push(this.underlyingType.decodeFromStorage(currentLocation, storageContent))
var item = await this.underlyingType.decodeFromStorage(currentLocation, storageContent)
ret.push(item)
if (this.underlyingType.storageSlots === 1 && location.offset + this.underlyingType.storageBytes <= 32) {
currentLocation.offset += this.underlyingType.storageBytes
if (currentLocation.offset + this.underlyingType.storageBytes > 32) {
......
......@@ -8,19 +8,19 @@ class DynamicByteArray extends RefType {
super(1, 32, 'bytes', location)
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
async decodeFromStorage (location, storageContent) {
var value = await util.extractHexValue(location, storageContent, this.storageBytes)
var bn = new BN(value, 16)
if (bn.testn(0)) {
var length = bn.div(new BN(2))
var dataPos = new BN(util.sha3(location.slot).replace('0x', ''), 16)
var ret = ''
var currentSlot = util.readFromStorage(dataPos, storageContent)
var currentSlot = await util.readFromStorage(dataPos, storageContent)
while (length.gt(ret.length) && ret.length < 32000) {
currentSlot = currentSlot.replace('0x', '')
ret += currentSlot
dataPos = dataPos.add(new BN(1))
currentSlot = util.readFromStorage(dataPos, storageContent)
currentSlot = await util.readFromStorage(dataPos, storageContent)
}
return {
value: '0x' + ret.replace(/(00)+$/, ''),
......
......@@ -6,7 +6,7 @@ class Mapping extends RefType {
super(1, 32, 'mapping', 'storage')
}
decodeFromStorage (location, storageContent) {
async decodeFromStorage (location, storageContent) {
return {
value: '<not implemented>',
length: '0x',
......
......@@ -19,7 +19,7 @@ class RefType {
* @param {Object} - storage
* @return {Object} decoded value
*/
decodeFromStack (stackDepth, stack, memory, storage) {
async decodeFromStack (stackDepth, stack, memory, storage) {
if (stack.length - 1 < stackDepth) {
return {
error: '<decoding failed - stack underflow ' + stackDepth + '>',
......@@ -32,7 +32,7 @@ class RefType {
var offset = stack[stack.length - 1 - stackDepth]
if (this.isInStorage()) {
offset = util.toBN(offset)
return this.decodeFromStorage({ offset: 0, slot: offset }, storage)
return await this.decodeFromStorage({ offset: 0, slot: offset }, storage)
} else if (this.isInMemory()) {
offset = parseInt(offset, 16)
return this.decodeFromMemoryInternal(offset, memory)
......
......@@ -7,8 +7,8 @@ class StringType extends DynamicBytes {
this.typeName = 'string'
}
decodeFromStorage (location, storageContent) {
var decoded = super.decodeFromStorage(location, storageContent)
async decodeFromStorage (location, storageContent) {
var decoded = await super.decodeFromStorage(location, storageContent)
return format(decoded)
}
......
......@@ -8,14 +8,14 @@ class Struct extends RefType {
this.members = memberDetails.members
}
decodeFromStorage (location, storageContent) {
async decodeFromStorage (location, storageContent) {
var ret = {}
this.members.map(function (item, i) {
this.members.map(async (item, i) => {
var globalLocation = {
offset: location.offset + item.storagelocation.offset,
slot: util.add(location.slot, item.storagelocation.slot)
}
ret[item.name] = item.type.decodeFromStorage(globalLocation, storageContent)
ret[item.name] = await item.type.decodeFromStorage(globalLocation, storageContent)
})
return {
value: ret,
......
......@@ -16,8 +16,8 @@ class ValueType {
* @param {Object} storageContent - storageContent (storage)
* @return {Object} - decoded value
*/
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
async decodeFromStorage (location, storageContent) {
var value = await util.extractHexValue(location, storageContent, this.storageBytes)
return {
value: this.decodeValue(value),
type: this.typeName
......
......@@ -4,7 +4,6 @@ var BN = require('ethereumjs-util').BN
module.exports = {
readFromStorage: readFromStorage,
decodeInt: decodeInt,
decodeIntFromHex: decodeIntFromHex,
extractHexValue: extractHexValue,
extractHexByteSlice: extractHexByteSlice,
......@@ -15,12 +14,6 @@ module.exports = {
removeLocation: removeLocation
}
function decodeInt (location, storageContent, byteLength, signed) {
var slotvalue = readFromStorage(location.slot, storageContent)
var value = extractHexByteSlice(slotvalue, byteLength, location.offset)
return decodeIntFromHex(value, byteLength, signed)
}
function decodeIntFromHex (value, byteLength, signed) {
var bigNumber = new BN(value, 16)
if (signed) {
......@@ -29,9 +22,10 @@ function decodeIntFromHex (value, byteLength, signed) {
return bigNumber.toString(10)
}
function readFromStorage (slot, storageContent) {
async function readFromStorage (slot, storageContent) {
var ret
var hexSlot = ethutil.bufferToHex(slot)
return new Promise((resolve, reject) => {
if (storageContent[hexSlot] !== undefined) {
ret = storageContent[hexSlot].replace(/^0x/, '')
} else {
......@@ -45,7 +39,8 @@ function readFromStorage (slot, storageContent) {
if (ret.length < 64) {
ret = (new Array(64 - ret.length + 1).join('0')) + ret
}
return ret
return resolve(ret)
})
}
/**
......@@ -67,8 +62,8 @@ function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) {
* @param {Object} storageContent - full storage mapping.
* @param {Int} byteLength - Length of the byte slice to extract
*/
function extractHexValue (location, storageContent, byteLength) {
var slotvalue = readFromStorage(location.slot, storageContent)
async function extractHexValue (location, storageContent, byteLength) {
var slotvalue = await readFromStorage(location.slot, storageContent)
return extractHexByteSlice(slotvalue, byteLength, location.offset)
}
......
......@@ -52,7 +52,11 @@ SolidityState.prototype.init = function () {
self.basicPanel.update({})
console.log(error)
} else {
self.basicPanel.update(stateDecoder.decodeState(stateVars, storage))
stateDecoder.decodeState(stateVars, storage).then((result) => {
if (!result.error) {
self.basicPanel.update(result)
}
})
}
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment