diff --git a/package.json b/package.json index 3447a6196e..96ff02086d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,10 @@ "tape": "^4.6.0", "web3": "^0.15.3", "yo-yo": "^1.2.1", - "yo-yoify": "^3.1.0" + "yo-yoify": "^3.1.0", + "ethereumjs-block": "^1.2.2", + "ethereumjs-tx": "^1.1.1", + "ethereumjs-vm": "^2.0.1" }, "scripts": { "start_node": "./runNode.sh", diff --git a/test/solidity/contracts/intLocal.js b/test/solidity/contracts/intLocal.js new file mode 100644 index 0000000000..53f0957752 --- /dev/null +++ b/test/solidity/contracts/intLocal.js @@ -0,0 +1,24 @@ +'use strict' + +module.exports = { + contract: ` + contract intLocal { + function set () { + uint8 ui8 = 130; + uint16 ui16 = 456; + uint32 ui32 = 4356; + uint64 ui64 = 3543543543; + uint128 ui128 = 234567; + uint256 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; + } + } +`} diff --git a/test/solidity/localDecoder.js b/test/solidity/localDecoder.js new file mode 100644 index 0000000000..b6c2fc8758 --- /dev/null +++ b/test/solidity/localDecoder.js @@ -0,0 +1,92 @@ +'use strict' +var tape = require('tape') +var compiler = require('solc') +var intLocal = require('./contracts/intLocal') +var TraceManager = require('../../src/trace/traceManager') +var CodeManager = require('../../src/code/codeManager') +var VM = require('ethereumjs-vm') +var Tx = require('ethereumjs-tx') +var Block = require('ethereumjs-block') +var BN = require('ethereumjs-util').BN +var utileth = require('ethereumjs-util') +var Web3Providers = require('../../src/web3Provider/web3Providers') +var traceHelper = require('../../src/helpers/traceHelper') +var util = require('../../src/helpers/global') +var LocalDecoder = require('../../src/solidity/localDecoder') + +tape('solidity', function (t) { + t.test('storage decoder', function (st) { + var privateKey = new Buffer('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', 'hex') + var address = utileth.privateToAddress(privateKey) + var vm = initVM(st, address) + var output = compiler.compile(intLocal.contract, 0) + + sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, output.contracts['intLocal'].bytecode, function (error, txHash) { + if (error) { + st.fail(error) + } else { + util.web3.getTransaction(txHash, function (error, tx) { + if (error) { + st.fail(error) + } else { + tx.to = traceHelper.contractCreationToken('0') + var traceManager = new TraceManager() + var codeManager = new CodeManager(traceManager) + var locals = new LocalDecoder(output, codeManager, traceManager.traceAnalyser.event) + traceManager.resolveTrace(tx, function (error, result) { + if (error) { + st.fail(error) + } else { + console.log(locals) + } + }) + } + }) + } + }) + }) +}) + +function initVM (st, address) { + var vm = new VM({ + enableHomestead: true, + activatePrecompiles: true + }) + vm.stateManager.putAccountBalance(address, 'f00000000000000001', function cb () {}) + var web3Providers = new Web3Providers() + web3Providers.addVM('VM', vm) + web3Providers.get('VM', function (error, obj) { + if (error) { + var mes = 'provider TEST not defined' + console.log(mes) + st.fail(mes) + } else { + util.web3 = obj + st.end() + } + }) + return vm +} + +function sendTx (vm, from, to, value, data, cb) { + var tx = new Tx({ + nonce: new BN(from.nonce++), + gasPrice: new BN(1), + gasLimit: new BN(3000000, 10), + to: to, + value: new BN(value, 10), + data: new Buffer(data, 'hex') + }) + tx.sign(from.privateKey) + var block = new Block({ + header: { + timestamp: new Date().getTime() / 1000 | 0, + number: 0 + }, + transactions: [], + uncleHeaders: [] + }) + vm.runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}, function (error, result) { + cb(error, utileth.bufferToHex(tx.hash())) + }) +} diff --git a/test/tests.js b/test/tests.js index 1f8bd4c59c..bcdab7bd47 100644 --- a/test/tests.js +++ b/test/tests.js @@ -10,4 +10,5 @@ require('./sourceMappingDecoder.js') require('./solidity/decodeInfo.js') require('./solidity/storageLocation.js') require('./solidity/storageDecoder.js') +require('./solidity/localDecoder.js')