Merge pull request #141 from ethereum/decodeType

Decode basic type (int, uint) and add basic testing
pull/7/head
chriseth 8 years ago committed by GitHub
commit be8e28b6f1
  1. 4
      package.json
  2. 3
      src/solidity/types/Int.js
  3. 5
      src/solidity/types/Uint.js
  4. 42
      src/solidity/types/util.js
  5. 32
      test/solidity/contracts/intStorage.js
  6. 0
      test/solidity/contracts/miscContracts.js
  7. 2
      test/solidity/decodeInfo.js
  8. 61
      test/solidity/storageDecoder.js
  9. 4
      test/solidity/storageLocation.js
  10. 2
      test/tests.js

@ -24,13 +24,13 @@
"ethereumjs-util": "^4.5.0",
"http-server": "^0.9.0",
"nightwatch": "^0.9.5",
"solc": "^0.4.3",
"standard": "^7.0.1",
"standard-reporter": "^1.0.5",
"tape": "^4.6.0",
"web3": "^0.15.3",
"yo-yo": "^1.2.1",
"yo-yoify": "^3.1.0",
"solc": "^0.4.3"
"yo-yoify": "^3.1.0"
},
"scripts": {
"start_node": "./runNode.sh",

@ -1,4 +1,5 @@
'use strict'
var util = require('./util')
function Int (storageBytes) {
this.storageSlots = 1
@ -7,7 +8,7 @@ function Int (storageBytes) {
}
Int.prototype.decodeFromStorage = function (location, storageContent) {
return '<not implemented yet>'
return util.decodeInt(location, storageContent, this.storageBytes, true)
}
module.exports = Int

@ -1,13 +1,16 @@
'use strict'
var IntType = require('./Int')
var util = require('./util')
function Uint (storageBytes) {
this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'uint'
this.decodeInt = new IntType(storageBytes)
}
Uint.prototype.decodeFromStorage = function (location, storageContent) {
return '<not implemented yet>'
return util.decodeInt(location, storageContent, this.storageBytes, false)
}
module.exports = Uint

@ -0,0 +1,42 @@
'use strict'
var ethutil = require('ethereumjs-util')
var BN = require('ethereumjs-util').BN
module.exports = {
extractHexByteSlice: extractHexByteSlice,
readFromStorage: readFromStorage,
decodeInt: decodeInt
}
function decodeInt (location, storageContent, byteLength, signed) {
var slotvalue = readFromStorage(location.slot, storageContent)
var value = extractHexByteSlice(slotvalue, byteLength, location.offset)
var bigNumber = new BN(value, 16)
if (signed) {
bigNumber = bigNumber.fromTwos(8 * byteLength)
}
return bigNumber.toString(10)
}
function readFromStorage (slot, storageContent) {
var hexSlot = ethutil.bufferToHex(slot)
if (storageContent[hexSlot] !== undefined) {
return storageContent[hexSlot]
} else {
hexSlot = ethutil.bufferToHex(ethutil.setLengthLeft(slot, 32))
if (storageContent[hexSlot] !== undefined) {
return storageContent[hexSlot]
} else {
return '0x0'
}
}
}
function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) {
slotValue = slotValue.replace('0x', '')
if (slotValue.length < 64) {
slotValue = (new Array(64 - slotValue.length + 1).join('0')) + slotValue
}
var offset = slotValue.length - 2 * offsetFromLSB - 2 * byteLength
return slotValue.substr(offset, 2 * byteLength)
}

@ -0,0 +1,32 @@
'use strict'
module.exports = {
contract: `
contract intStorage {
uint8 ui8 = 130;
uint16 ui16 = 456;
uint32 ui32 = 4356;
uint64 ui64 = 3543543543;
uint128 ui128 = 234567;
uint256 public ui256 = 115792089237316195423570985008687907853269984665640564039457584007880697216513;
uint ui = 123545666;
int8 i8 = -45;
int16 i16 = -1234;
int32 i32 = 3455;
int64 i64 = -35566;
int128 i128 = -444444;
int256 i256 = 3434343;
int i = -32432423423;
int32 ishrink = 2;
}
`,
fullStorage: {
'0x0000000000000000000000000000000000000000000000000000000000000000': '0x000000000000000000000000000003944700000000d3362ef70000110401c882',
'0x0000000000000000000000000000000000000000000000000000000000000001': '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffff872e07e01',
'0x0000000000000000000000000000000000000000000000000000000000000002': '0x00000000000000000000000000000000000000000000000000000000075d2842',
'0x0000000000000000000000000000000000000000000000000000000000000003': '0x00fffffffffffffffffffffffffff937e4ffffffffffff751200000d7ffb2ed3',
'0x0000000000000000000000000000000000000000000000000000000000000004': '0x0000000000000000000000000000000000000000000000000000000000346767',
'0x0000000000000000000000000000000000000000000000000000000000000005': '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffff872e07e01',
'0x0000000000000000000000000000000000000000000000000000000000000006': '0x0000000000000000000000000000000000000000000000000000000000000002'
}
}

@ -2,7 +2,7 @@
var tape = require('tape')
var compiler = require('solc')
var index = require('../../src/index')
var contracts = require('./contracts')
var contracts = require('./contracts/miscContracts')
tape('solidity', function (t) {
t.test('astHelper, decodeInfo', function (st) {

@ -0,0 +1,61 @@
'use strict'
var tape = require('tape')
var compiler = require('solc')
var stateDecoder = require('../../src/index').solidity.stateDecoder
tape('solidity', function (t) {
t.test('storage decoder', function (st) {
testIntStorage(st)
})
})
function testIntStorage (st) {
var intStorage = require('./contracts/intStorage')
var output = compiler.compile(intStorage.contract, 0)
for (var storage of [intStorage.fullStorage, shrinkStorage(intStorage.fullStorage)]) {
var decoded = stateDecoder.solidityState(storage, output.sources, 'intStorage')
st.equal(decoded['ui8'], '130')
st.equal(decoded['ui16'], '456')
st.equal(decoded['ui32'], '4356')
st.equal(decoded['ui64'], '3543543543')
st.equal(decoded['ui128'], '234567')
st.equal(decoded['ui256'], '115792089237316195423570985008687907853269984665640564039457584007880697216513')
st.equal(decoded['ui'], '123545666')
st.equal(decoded['i8'], '-45')
st.equal(decoded['i16'], '-1234')
st.equal(decoded['i32'], '3455')
st.equal(decoded['i64'], '-35566')
st.equal(decoded['i128'], '-444444')
st.equal(decoded['i256'], '3434343')
st.equal(decoded['i'], '-32432423423')
st.equal(decoded['ishrink'], '2')
}
decoded = stateDecoder.solidityState({}, output.sources, 'intStorage')
st.equal(decoded['ui8'], '0')
st.equal(decoded['ui16'], '0')
st.equal(decoded['ui32'], '0')
st.equal(decoded['ui64'], '0')
st.equal(decoded['ui128'], '0')
st.equal(decoded['ui256'], '0')
st.equal(decoded['ui'], '0')
st.equal(decoded['i8'], '0')
st.equal(decoded['i16'], '0')
st.equal(decoded['i32'], '0')
st.equal(decoded['i64'], '0')
st.equal(decoded['i128'], '0')
st.equal(decoded['i256'], '0')
st.equal(decoded['i'], '0')
st.equal(decoded['ishrink'], '0')
st.end()
}
function shrinkStorage (storage) {
var shrinkedStorage = {}
var regex = /0x(00)*(..)/
for (var key in storage) {
var value = storage[key]
shrinkedStorage[key.replace(regex, '0x$2')] = value.replace(regex, '0x$2')
}
return shrinkedStorage
}

@ -2,10 +2,10 @@
var tape = require('tape')
var compiler = require('solc')
var index = require('../../src/index')
var contracts = require('./contracts')
var contracts = require('./contracts/miscContracts')
tape('solidity', function (t) {
t.test('astHelper, decodeInfo', function (st) {
t.test('storage location', function (st) {
var output = compiler.compile(contracts, 0)
var stateDec = index.solidity.stateDecoder.extractStateVariables('contractUint', output.sources)
checkLocation(st, stateDec[0].location, 0, 0)

@ -9,3 +9,5 @@ require('./eventManager.js')
require('./sourceMappingDecoder.js')
require('./solidity/decodeInfo.js')
require('./solidity/storageLocation.js')
require('./solidity/storageDecoder.js')

Loading…
Cancel
Save