Commit f0a2e6e5 authored by yann300's avatar yann300

fix tests

parent 05645cb5
...@@ -10,13 +10,14 @@ class Struct extends RefType { ...@@ -10,13 +10,14 @@ class Struct extends RefType {
async decodeFromStorage (location, storageResolver) { async decodeFromStorage (location, storageResolver) {
var ret = {} var ret = {}
this.members.map(async (item, i) => { for (var k in this.members) {
var item = this.members[k]
var globalLocation = { var globalLocation = {
offset: location.offset + item.storagelocation.offset, offset: location.offset + item.storagelocation.offset,
slot: util.add(location.slot, item.storagelocation.slot) slot: util.add(location.slot, item.storagelocation.slot)
} }
ret[item.name] = await item.type.decodeFromStorage(globalLocation, storageResolver) ret[item.name] = await item.type.decodeFromStorage(globalLocation, storageResolver)
}) }
return { return {
value: ret, value: ret,
type: this.typeName type: this.typeName
......
'use strict'
var util = require('../../babelify-src/helpers/util')
class MockStorageResolver {
constructor (_storage) {
this.storage = {}
for (var k in _storage) {
var hashed = util.sha3(k)
this.storage[hashed] = {
hashed: hashed,
key: k,
value: _storage[k]
}
}
}
storageRange (callback) {
callback(null, this.storage)
}
storageSlot (slot, callback) {
var hashed = util.sha3(slot)
callback(null, this.storage[hashed])
}
isComplete (address) {
return true
}
fromCache (address, slotKey) {
return this.storage[slotKey]
}
toCache (address, storage, complete) {
}
}
module.exports = MockStorageResolver
...@@ -2,21 +2,27 @@ ...@@ -2,21 +2,27 @@
var tape = require('tape') var tape = require('tape')
var compiler = require('solc') var compiler = require('solc')
var stateDecoder = require('../../src/index').solidity.stateDecoder var stateDecoder = require('../../src/index').solidity.stateDecoder
var MockStorageResolver = require('./mockStorageResolver')
tape('solidity', function (t) { tape('solidity', function (t) {
t.test('storage decoder', function (st) { t.test('storage decoder', function (st) {
testIntStorage(st) testIntStorage(st, function () {
testByteStorage(st) testByteStorage(st, function () {
testStructArrayStorage(st) testStructArrayStorage(st, function () {
st.end() st.end()
}) })
})
})
})
}) })
function testIntStorage (st) { function testIntStorage (st, cb) {
var intStorage = require('./contracts/intStorage') var intStorage = require('./contracts/intStorage')
var output = compiler.compile(intStorage.contract, 0) var output = compiler.compile(intStorage.contract, 0)
var mockStorageResolver
for (var storage of [intStorage.fullStorage, shrinkStorage(intStorage.fullStorage)]) { for (var storage of [intStorage.fullStorage, shrinkStorage(intStorage.fullStorage)]) {
var decoded = stateDecoder.solidityState(storage, output.sources, 'intStorage') mockStorageResolver = new MockStorageResolver(storage)
stateDecoder.solidityState(mockStorageResolver, output.sources, 'intStorage').then((decoded) => {
st.equal(decoded['ui8'].value, '130') st.equal(decoded['ui8'].value, '130')
st.equal(decoded['ui16'].value, '456') st.equal(decoded['ui16'].value, '456')
st.equal(decoded['ui32'].value, '4356') st.equal(decoded['ui32'].value, '4356')
...@@ -32,9 +38,11 @@ function testIntStorage (st) { ...@@ -32,9 +38,11 @@ function testIntStorage (st) {
st.equal(decoded['i256'].value, '3434343') st.equal(decoded['i256'].value, '3434343')
st.equal(decoded['i'].value, '-32432423423') st.equal(decoded['i'].value, '-32432423423')
st.equal(decoded['ishrink'].value, '2') st.equal(decoded['ishrink'].value, '2')
})
} }
decoded = stateDecoder.solidityState({}, output.sources, 'intStorage') mockStorageResolver = new MockStorageResolver({})
stateDecoder.solidityState(mockStorageResolver, output.sources, 'intStorage').then((decoded) => {
st.equal(decoded['ui8'].value, '0') st.equal(decoded['ui8'].value, '0')
st.equal(decoded['ui16'].value, '0') st.equal(decoded['ui16'].value, '0')
st.equal(decoded['ui32'].value, '0') st.equal(decoded['ui32'].value, '0')
...@@ -50,13 +58,17 @@ function testIntStorage (st) { ...@@ -50,13 +58,17 @@ function testIntStorage (st) {
st.equal(decoded['i256'].value, '0') st.equal(decoded['i256'].value, '0')
st.equal(decoded['i'].value, '0') st.equal(decoded['i'].value, '0')
st.equal(decoded['ishrink'].value, '0') st.equal(decoded['ishrink'].value, '0')
cb()
})
} }
function testByteStorage (st) { function testByteStorage (st, cb) {
var byteStorage = require('./contracts/byteStorage') var byteStorage = require('./contracts/byteStorage')
var output = compiler.compile(byteStorage.contract, 0) var output = compiler.compile(byteStorage.contract, 0)
var mockStorageResolver
for (var storage of [byteStorage.storage, shrinkStorage(byteStorage.storage)]) { for (var storage of [byteStorage.storage, shrinkStorage(byteStorage.storage)]) {
var decoded = stateDecoder.solidityState(storage, output.sources, 'byteStorage') mockStorageResolver = new MockStorageResolver(storage)
stateDecoder.solidityState(mockStorageResolver, output.sources, 'byteStorage').then((decoded) => {
st.equal(decoded['b1'].value, false) st.equal(decoded['b1'].value, false)
st.equal(decoded['a1'].value, '0xFE350F199F244AC9A79038D254400B632A633225') st.equal(decoded['a1'].value, '0xFE350F199F244AC9A79038D254400B632A633225')
st.equal(decoded['b2'].value, true) st.equal(decoded['b2'].value, true)
...@@ -99,9 +111,11 @@ function testByteStorage (st) { ...@@ -99,9 +111,11 @@ function testByteStorage (st) {
st.equal(decoded['str1'].value, 'short') st.equal(decoded['str1'].value, 'short')
st.equal(decoded['str12'].value, 'шеллы') st.equal(decoded['str12'].value, 'шеллы')
st.equal(decoded['str2'].value, 'long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long') st.equal(decoded['str2'].value, 'long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long__long')
})
} }
decoded = stateDecoder.solidityState({}, output.sources, 'byteStorage') mockStorageResolver = new MockStorageResolver({})
stateDecoder.solidityState(mockStorageResolver, output.sources, 'byteStorage').then((decoded) => {
st.equal(decoded['b1'].value, false) st.equal(decoded['b1'].value, false)
st.equal(decoded['a1'].value, '0x0000000000000000000000000000000000000000') st.equal(decoded['a1'].value, '0x0000000000000000000000000000000000000000')
st.equal(decoded['b2'].value, false) st.equal(decoded['b2'].value, false)
...@@ -146,6 +160,8 @@ function testByteStorage (st) { ...@@ -146,6 +160,8 @@ function testByteStorage (st) {
st.equal(decoded['str1'].value, '') st.equal(decoded['str1'].value, '')
st.equal(decoded['str12'].value, '') st.equal(decoded['str12'].value, '')
st.equal(decoded['str2'].value, '') st.equal(decoded['str2'].value, '')
cb()
})
} }
function shrinkStorage (storage) { function shrinkStorage (storage) {
...@@ -158,17 +174,17 @@ function shrinkStorage (storage) { ...@@ -158,17 +174,17 @@ function shrinkStorage (storage) {
return shrinkedStorage return shrinkedStorage
} }
function testStructArrayStorage (st) { function testStructArrayStorage (st, cb) {
var structArrayStorage = require('./contracts/structArrayStorage') var structArrayStorage = require('./contracts/structArrayStorage')
var output = compiler.compile(structArrayStorage.contract, 0) var output = compiler.compile(structArrayStorage.contract, 0)
var decoded = stateDecoder.solidityState(structArrayStorage.storage, output.sources, 'structArrayStorage') var mockStorageResolver = new MockStorageResolver(structArrayStorage.storage)
stateDecoder.solidityState(mockStorageResolver, output.sources, 'structArrayStorage').then((decoded) => {
st.equal(decoded['intStructDec'].value['i8'].value, '32') st.equal(decoded['intStructDec'].value['i8'].value, '32')
st.equal(decoded['intStructDec'].value['i16'].value, '-54') st.equal(decoded['intStructDec'].value['i16'].value, '-54')
st.equal(decoded['intStructDec'].value['ui32'].value, '128') st.equal(decoded['intStructDec'].value['ui32'].value, '128')
st.equal(decoded['intStructDec'].value['i256'].value, '-1243565465756') st.equal(decoded['intStructDec'].value['i256'].value, '-1243565465756')
st.equal(decoded['intStructDec'].value['ui16'].value, '34556') st.equal(decoded['intStructDec'].value['ui16'].value, '34556')
st.equal(decoded['intStructDec'].value['i32'].value, '-345446546') st.equal(decoded['intStructDec'].value['i32'].value, '-345446546')
st.equal(decoded['i5'].length, '0x7') st.equal(decoded['i5'].length, '0x7')
st.equal(decoded['i5'].value[0].value, '-2134') st.equal(decoded['i5'].value[0].value, '-2134')
st.equal(decoded['i5'].value[1].value, '345') st.equal(decoded['i5'].value[1].value, '345')
...@@ -177,7 +193,6 @@ function testStructArrayStorage (st) { ...@@ -177,7 +193,6 @@ function testStructArrayStorage (st) {
st.equal(decoded['i5'].value[4].value, '324') st.equal(decoded['i5'].value[4].value, '324')
st.equal(decoded['i5'].value[5].value, '-2344') st.equal(decoded['i5'].value[5].value, '-2344')
st.equal(decoded['i5'].value[6].value, '3254') st.equal(decoded['i5'].value[6].value, '3254')
st.equal(decoded['idyn5'].length, '0x9') st.equal(decoded['idyn5'].length, '0x9')
st.equal(decoded['idyn5'].value[0].value, '-2134') st.equal(decoded['idyn5'].value[0].value, '-2134')
st.equal(decoded['idyn5'].value[1].value, '345') st.equal(decoded['idyn5'].value[1].value, '345')
...@@ -188,16 +203,13 @@ function testStructArrayStorage (st) { ...@@ -188,16 +203,13 @@ function testStructArrayStorage (st) {
st.equal(decoded['idyn5'].value[6].value, '3254') st.equal(decoded['idyn5'].value[6].value, '3254')
st.equal(decoded['idyn5'].value[7].value, '-254') st.equal(decoded['idyn5'].value[7].value, '-254')
st.equal(decoded['idyn5'].value[8].value, '-2354') st.equal(decoded['idyn5'].value[8].value, '-2354')
st.equal(decoded['dyn1'].length, '0x4') st.equal(decoded['dyn1'].length, '0x4')
st.equal(decoded['dyn1'].value[0].length, '0x1') st.equal(decoded['dyn1'].value[0].length, '0x1')
st.equal(decoded['dyn1'].value[0].value[0].value, '3') st.equal(decoded['dyn1'].value[0].value[0].value, '3')
st.equal(decoded['dyn1'].value[1].length, '0x3') st.equal(decoded['dyn1'].value[1].length, '0x3')
st.equal(decoded['dyn1'].value[1].value[0].value, '12') st.equal(decoded['dyn1'].value[1].value[0].value, '12')
st.equal(decoded['dyn1'].value[1].value[1].value, '-12') st.equal(decoded['dyn1'].value[1].value[1].value, '-12')
st.equal(decoded['dyn1'].value[1].value[2].value, '-1234') st.equal(decoded['dyn1'].value[1].value[2].value, '-1234')
st.equal(decoded['dyn1'].value[2].length, '0xa') st.equal(decoded['dyn1'].value[2].length, '0xa')
st.equal(decoded['dyn1'].value[2].value[0].value, '1') st.equal(decoded['dyn1'].value[2].value[0].value, '1')
st.equal(decoded['dyn1'].value[2].value[1].value, '12') st.equal(decoded['dyn1'].value[2].value[1].value, '12')
...@@ -209,11 +221,9 @@ function testStructArrayStorage (st) { ...@@ -209,11 +221,9 @@ function testStructArrayStorage (st) {
st.equal(decoded['dyn1'].value[2].value[7].value, '2') st.equal(decoded['dyn1'].value[2].value[7].value, '2')
st.equal(decoded['dyn1'].value[2].value[8].value, '-1') st.equal(decoded['dyn1'].value[2].value[8].value, '-1')
st.equal(decoded['dyn1'].value[2].value[9].value, '232432') st.equal(decoded['dyn1'].value[2].value[9].value, '232432')
st.equal(decoded['dyn1'].value[3].length, '0x2') st.equal(decoded['dyn1'].value[3].length, '0x2')
st.equal(decoded['dyn1'].value[3].value[0].value, '232432') st.equal(decoded['dyn1'].value[3].value[0].value, '232432')
st.equal(decoded['dyn1'].value[3].value[0].value, '232432') st.equal(decoded['dyn1'].value[3].value[0].value, '232432')
st.equal(decoded['dyn2'].length, '0x2') st.equal(decoded['dyn2'].length, '0x2')
st.equal(decoded['dyn2'].value[0].length, '0x4') st.equal(decoded['dyn2'].value[0].length, '0x4')
st.equal(decoded['dyn2'].value[0].value[0].value[0].value, '23') st.equal(decoded['dyn2'].value[0].value[0].value[0].value, '23')
...@@ -253,4 +263,6 @@ function testStructArrayStorage (st) { ...@@ -253,4 +263,6 @@ function testStructArrayStorage (st) {
st.equal(decoded['arrayStruct'].value[2].value[1].value.str.value, 'test_str_long test_str_lo ngtest_str_longtest_str_ longtest_str_longtest_ str_longtest_str_l ongtest_str_long') st.equal(decoded['arrayStruct'].value[2].value[1].value.str.value, 'test_str_long test_str_lo ngtest_str_longtest_str_ longtest_str_longtest_ str_longtest_str_l ongtest_str_long')
st.equal(decoded['arrayStruct'].value[2].value[2].value.i8.value, '-34') st.equal(decoded['arrayStruct'].value[2].value[2].value.i8.value, '-34')
st.equal(decoded['arrayStruct'].value[2].value[2].value.str.value, 'test_str_short') st.equal(decoded['arrayStruct'].value[2].value[2].value.str.value, 'test_str_short')
cb()
})
} }
...@@ -53,13 +53,13 @@ tape('TraceManager', function (t) { ...@@ -53,13 +53,13 @@ tape('TraceManager', function (t) {
st.end() st.end()
}) })
t.test('TraceManager.getStorageAt', function (st) { t.test('TraceManager.resolveStorage', function (st) {
var tx = util.web3.eth.getTransaction('0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51') traceManager.resolveStorage(110, '0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', {}, function (error, result) {
traceManager.getStorageAt(110, tx, function (error, result) {
if (error) { if (error) {
st.fail(error) st.fail(error)
} else { } else {
st.ok(result['0x00'] === '0x38') console.log(JSON.stringify(result))
st.ok(result['0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563'].value === '0x38')
st.end() st.end()
} }
}) })
......
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