Commit 1a109717 authored by yann300's avatar yann300

move decodeFromStorage, decodeFromStack, decodeFromMemory to ValueType

parent 20c32ddf
......@@ -7,24 +7,13 @@ class Address extends ValueType {
super(1, 20, 'address')
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
return '0x' + value.toUpperCase()
}
decodeFromStack (stackDepth, stack, memory) {
if (stackDepth >= stack.length) {
decodeValue (value) {
if (!value) {
return '0x0000000000000000000000000000000000000000'
} else {
return '0x' + util.extractHexByteSlice(stack[stack.length - 1 - stackDepth], this.storageBytes, 0)
return '0x' + util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
value = util.extractHexByteSlice(value, this.storageBytes, 0)
return value
}
}
module.exports = Address
'use strict'
var util = require('./util')
var ValueType = require('./ValueType')
var util = require('./util')
class Bool extends ValueType {
constructor () {
super(1, 1, 'bool')
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
return value !== '00'
}
decodeFromStack (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) {
decodeValue (value) {
if (!value) {
return false
} else {
return util.extractHexByteSlice(stack[stack.length - 1 - stackDepth], this.storageBytes, 0) !== '00'
value = util.extractHexByteSlice(value, this.storageBytes, 0)
return value !== '00'
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
return util.extractHexByteSlice(value, this.storageBytes, 0) !== '00'
}
}
module.exports = Bool
......@@ -8,6 +8,10 @@ class DynamicByteArray extends ValueType {
super(1, 32, 'bytes')
}
decodeValue (value) {
return '0x' + value.toUpperCase()
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
var bn = new BN(value, 16)
......
'use strict'
var util = require('./util')
var ValueType = require('./ValueType')
class Enum extends ValueType {
......@@ -14,36 +13,17 @@ class Enum extends ValueType {
this.enumDef = enumDef
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
value = parseInt(value, 16)
return output(value, this.enumDef)
}
decodeFromStack (stackDepth, stack, memory) {
var defaultValue = 0
if (stack.length - 1 < stackDepth) {
defaultValue = 0
decodeValue (value) {
if (!value) {
return this.enumDef.children[0].attributes.name
} else {
defaultValue = util.extractHexByteSlice(stack[stack.length - 1 - stackDepth], this.storageBytes, 0)
defaultValue = parseInt(defaultValue, 16)
value = parseInt(value, 16)
if (this.enumDef.children.length > value) {
return this.enumDef.children[value].attributes.name
} else {
return 'INVALID_ENUM<' + value + '>'
}
}
return output(defaultValue, this.enumDef)
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
value = util.extractHexByteSlice(value, this.storageBytes, 0)
value = parseInt(value, 16)
return output(value, this.enumDef)
}
}
function output (value, enumDef) {
if (enumDef.children.length > value) {
return enumDef.children[value].attributes.name
} else {
return 'INVALID_ENUM<' + value + '>'
}
}
......
'use strict'
var util = require('./util')
var ValueType = require('./ValueType')
class FixedByteArray extends ValueType {
......@@ -7,23 +6,8 @@ class FixedByteArray extends ValueType {
super(1, storageBytes, 'bytesX')
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
return '0x' + value.toUpperCase()
}
decodeFromStack (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) {
return '0x'
} else {
var value = stack[stack.length - 1 - stackDepth]
return '0x' + value.substr(2, 2 * this.storageBytes).toUpperCase()
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
return util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
decodeValue (value) {
return '0x' + value.substr(0, 2 * this.storageBytes).toUpperCase()
}
}
......
......@@ -7,21 +7,9 @@ class Int extends ValueType {
super(1, storageBytes, 'int')
}
decodeFromStorage (location, storageContent) {
return util.decodeInt(location, storageContent, this.storageBytes, true)
}
decodeFromStack (stackDepth, stack, memory) {
if (stackDepth >= stack.length) {
return '0'
} else {
return util.decodeIntFromHex(stack[stack.length - 1 - stackDepth].replace('0x', ''), 32, true)
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
return util.decodeIntFromHex(value, 32, true)
decodeValue (value) {
value = util.extractHexByteSlice(value, this.storageBytes, 0)
return util.decodeIntFromHex(value, this.storageBytes, true)
}
}
......
'use strict'
var RefType = require('./RefType')
class Mapping {
class Mapping extends RefType {
constructor () {
this.storageSlots = 1
this.storageBytes = 32
this.typeName = 'mapping'
super(1, 32, 'mapping')
}
decodeFromStorage (location, storageContent) {
decodeValue (value) {
return '<not implemented>'
}
}
......
......@@ -7,6 +7,11 @@ class StringType extends DynamicBytes {
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)
......
......@@ -7,20 +7,8 @@ class Uint extends ValueType {
super(1, storageBytes, 'uint')
}
decodeFromStorage (location, storageContent) {
return util.decodeInt(location, storageContent, this.storageBytes, false)
}
decodeFromStack (stackDepth, stack, memory) {
if (stackDepth >= stack.length) {
return '0'
} else {
return util.decodeIntFromHex(stack[stack.length - 1 - stackDepth].replace('0x', ''), this.storageBytes, false)
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
decodeValue (value) {
value = util.extractHexByteSlice(value, this.storageBytes, 0)
return util.decodeIntFromHex(value, this.storageBytes, false)
}
}
......
'use strict'
var util = require('./util')
class ValueType {
constructor (storageSlots, storageBytes, typeName) {
......@@ -6,6 +7,24 @@ class ValueType {
this.storageBytes = storageBytes
this.typeName = typeName
}
decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes)
return this.decodeValue(value)
}
decodeFromStack (stackDepth, stack, memory) {
if (stackDepth >= stack.length) {
return this.decodeValue('')
} else {
return this.decodeValue(stack[stack.length - 1 - stackDepth].replace('0x', ''))
}
}
decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64)
return this.decodeValue(util.extractHexByteSlice(value, this.storageBytes, 0))
}
}
module.exports = ValueType
......@@ -32,7 +32,7 @@ module.exports = function (st, vm, privateKey, contractBytecode, compilationResu
st.equals(locals['boolFalse'], false)
st.equals(locals['boolTrue'], true)
st.equals(locals['testEnum'], 'three')
st.equals(locals['sender'], '0x4b0897b0513fdc7c541b6d9d7e929c4e5364d2db')
st.equals(locals['sender'], '0x4B0897B0513FDC7C541B6D9D7E929C4E5364D2DB')
st.equals(locals['_bytes1'], '0x99')
st.equals(locals['__bytes1'], '0x99')
st.equals(locals['__bytes2'], '0x99AB')
......
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