Merge pull request #141 from ethereum/decodeType
Decode basic type (int, uint) and add basic testingpull/7/head
commit
be8e28b6f1
@ -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' |
||||
} |
||||
} |
@ -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 |
||||
} |
Loading…
Reference in new issue