move decodeFromStorage, decodeFromStack, decodeFromMemory to ValueType

pull/7/head
yann300 8 years ago
parent 20c32ddfad
commit 1a10971792
  1. 17
      src/solidity/types/Address.js
  2. 19
      src/solidity/types/Bool.js
  3. 4
      src/solidity/types/DynamicByteArray.js
  4. 34
      src/solidity/types/Enum.js
  5. 20
      src/solidity/types/FixedByteArray.js
  6. 18
      src/solidity/types/Int.js
  7. 9
      src/solidity/types/Mapping.js
  8. 5
      src/solidity/types/StringType.js
  9. 16
      src/solidity/types/Uint.js
  10. 19
      src/solidity/types/ValueType.js
  11. 2
      test/solidity/localsTests/misc.js

@ -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

@ -1,28 +1,19 @@
'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'
}
}

@ -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)

@ -1,5 +1,4 @@
'use strict'
var util = require('./util')
var ValueType = require('./ValueType')
class Enum extends ValueType {
@ -14,37 +13,18 @@ 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)
}
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
if (this.enumDef.children.length > value) {
return this.enumDef.children[value].attributes.name
} else {
return 'INVALID_ENUM<' + value + '>'
}
}
}
}
module.exports = Enum

@ -1,5 +1,4 @@
'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)
}
}

@ -1,13 +1,12 @@
'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)
}
}

@ -1,4 +1,5 @@
'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')

Loading…
Cancel
Save