From f06cc737da6aa83592ebbf0d7210e831a0cb79df Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 11 Jan 2017 16:52:59 +0100 Subject: [PATCH] fix locals decoding --- src/solidity/types/ArrayType.js | 9 +++++---- src/solidity/types/DynamicByteArray.js | 26 +++++--------------------- src/solidity/types/RefType.js | 19 +++++++++++++++---- src/solidity/types/StringType.js | 15 ++++++--------- src/solidity/types/Struct.js | 12 +++++++----- src/solidity/types/ValueType.js | 8 +++++++- 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/solidity/types/ArrayType.js b/src/solidity/types/ArrayType.js index 8b040d9c8d..39fb90a265 100644 --- a/src/solidity/types/ArrayType.js +++ b/src/solidity/types/ArrayType.js @@ -57,23 +57,24 @@ class ArrayType extends RefType { } decodeFromMemory (offset, memory) { + offset = 2 * offset var ret = [] var length = extractLength(this, offset, memory) if (this.arraySize === 'dynamic') { offset = offset + 64 } + this.underlyingType.location = this.location for (var k = 0; k < length; k++) { var contentOffset = offset - if (this.underlyingType.typeName === 'bytes' || this.underlyingType.typeName === 'string' || this.underlyingType.typeName === 'array' || this.underlyingType.typeName === 'struct') { + if (this.underlyingType.basicType === 'RefType') { contentOffset = memory.substr(offset, 64) - contentOffset = 2 * parseInt(contentOffset, 16) + contentOffset = parseInt(contentOffset, 16) } - ret.push(this.underlyingType.decodeFromMemory(contentOffset, memory)) + ret.push(this.underlyingType.decode(contentOffset, memory)) offset += 64 } return ret } - } function extractLength (type, offset, memory) { diff --git a/src/solidity/types/DynamicByteArray.js b/src/solidity/types/DynamicByteArray.js index 26fa2c26d9..42fb5c41c0 100644 --- a/src/solidity/types/DynamicByteArray.js +++ b/src/solidity/types/DynamicByteArray.js @@ -1,15 +1,11 @@ 'use strict' var util = require('./util') var BN = require('ethereumjs-util').BN -var ValueType = require('./ValueType') +var RefType = require('./RefType') -class DynamicByteArray extends ValueType { - constructor () { - super(1, 32, 'bytes') - } - - decodeValue (value) { - return '0x' + value.toUpperCase() +class DynamicByteArray extends RefType { + constructor (location) { + super(1, 32, 'bytes', location) } decodeFromStorage (location, storageContent) { @@ -39,20 +35,8 @@ class DynamicByteArray extends ValueType { } } - decodeFromStack (stackDepth, stack, memory) { - if (stack.length - 1 < stackDepth) { - return { - value: '0x', - length: '0x0' - } - } else { - var offset = stack[stack.length - 1 - stackDepth] - offset = 2 * parseInt(offset, 16) - return this.decodeFromMemory(offset, memory) - } - } - decodeFromMemory (offset, memory) { + offset = 2 * offset var length = memory.substr(offset, 64) length = 2 * parseInt(length, 16) return { diff --git a/src/solidity/types/RefType.js b/src/solidity/types/RefType.js index 23d40f00e5..c3fde503ef 100644 --- a/src/solidity/types/RefType.js +++ b/src/solidity/types/RefType.js @@ -7,20 +7,31 @@ class RefType { this.storageSlots = storageSlots this.storageBytes = storageBytes this.typeName = typeName + this.basicType = 'RefType' } decodeFromStack (stackDepth, stack, memory, storage) { + if (!storage) { + storage = {} // TODO this is a fallback, should manage properly locals store in storage + } if (stack.length - 1 < stackDepth) { - return [] + return { error: '' } } var offset = stack[stack.length - 1 - stackDepth] - offset = 2 * parseInt(offset, 16) + offset = parseInt(offset, 16) + return this.decode(offset, memory, storage) + } + + decode (offset, memory, storage) { + if (!storage) { + storage = {} // TODO this is a fallback, should manage properly locals store in storage + } if (util.storageStore(this)) { - return this.decodeFromStorage(offset, storage) + return this.decodeFromStorage({ offset: 0, slot: offset }, storage) } else if (util.memoryStore(this)) { return this.decodeFromMemory(offset, memory) } else { - return '' + return { error: '' } } } } diff --git a/src/solidity/types/StringType.js b/src/solidity/types/StringType.js index 18a4a517a5..e8a101cb77 100644 --- a/src/solidity/types/StringType.js +++ b/src/solidity/types/StringType.js @@ -2,24 +2,18 @@ var DynamicBytes = require('./DynamicByteArray') class StringType extends DynamicBytes { - constructor () { - super() + constructor (location) { + super(location) this.typeName = 'string' } - decodeValue (value) { - var decoded = super.decodeValue(value) - return format(decoded) - } - decodeFromStorage (location, storageContent) { var decoded = super.decodeFromStorage(location, storageContent) return format(decoded) } decodeFromStack (stackDepth, stack, memory) { - var decoded = super.decodeFromStack(stackDepth, stack, memory) - return format(decoded) + return super.decodeFromStack(stackDepth, stack, memory) } decodeFromMemory (offset, memory) { @@ -29,6 +23,9 @@ class StringType extends DynamicBytes { } function format (decoded) { + if (decoded.error) { + return decoded + } var value = decoded.value value = value.replace('0x', '').replace(/(..)/g, '%$1') var ret = { diff --git a/src/solidity/types/Struct.js b/src/solidity/types/Struct.js index 276ea04dfb..908528814a 100644 --- a/src/solidity/types/Struct.js +++ b/src/solidity/types/Struct.js @@ -21,14 +21,16 @@ class Struct extends RefType { } decodeFromMemory (offset, memory) { + offset = 2 * offset var ret = {} - this.members.map(function (item, i) { + this.members.map((item, i) => { var contentOffset = offset - if (item.type.typeName === 'bytes' || item.type.typeName === 'string' || item.type.typeName === 'array' || item.type.typeName === 'struct') { - contentOffset = memory.substr(offset, 64) - contentOffset = 2 * parseInt(contentOffset, 16) + if (item.type.basicType === 'RefType') { + contentOffset = memory.substr(contentOffset, 64) + contentOffset = parseInt(contentOffset, 16) } - var member = item.type.decodeFromMemory(contentOffset, memory) + item.type.location = this.location + var member = item.type.decode(contentOffset, memory) ret[item.name] = member offset += 64 }) diff --git a/src/solidity/types/ValueType.js b/src/solidity/types/ValueType.js index 6c22e05aea..fd2d1cf88e 100644 --- a/src/solidity/types/ValueType.js +++ b/src/solidity/types/ValueType.js @@ -6,6 +6,7 @@ class ValueType { this.storageSlots = storageSlots this.storageBytes = storageBytes this.typeName = typeName + this.basicType = 'ValueType' } decodeFromStorage (location, storageContent) { @@ -22,9 +23,14 @@ class ValueType { } decodeFromMemory (offset, memory) { - var value = memory.substr(offset, 64) + var value = memory.substr(2 * offset, 64) return this.decodeValue(util.extractHexByteSlice(value, this.storageBytes, 0)) } + + decode (offset, memory) { + offset = offset / 2 + return this.decodeFromMemory(offset, memory) + } } module.exports = ValueType