From d2bc79b9eaec10e22d3c266dbbf40a767e67d764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 27 May 2015 13:23:00 +0200 Subject: [PATCH 01/23] Change VM interface to return a copy of output. From ecd5108afcaeceb52a7b2276beefced575fca5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 08:56:21 +0200 Subject: [PATCH 02/23] Change the way execution results are collected. Changes handling ExecutionResult by Executive. From now execution results are collected on if a storage for results (ExecutionResult) is provided to an Executiove instance up front. This change allow better output management for calls - VM interface improved. --- libsolidity/solidityExecutionFramework.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 0f80e9f59..ea277421a 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -135,6 +135,8 @@ protected: { m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); + eth::ExecutionResult res; + executive.setResultRef(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); bytes transactionRLP = t.rlp(); @@ -161,7 +163,7 @@ protected: m_state.noteSending(m_sender); executive.finalize(); m_gasUsed = executive.gasUsed(); - m_output = executive.out().toVector(); + m_output = std::move(res.output); // FIXME: Looks like Framework needs ExecutiveResult embedded m_logs = executive.logs(); } From 77dd832403a7f4dc11c60b25f6eec786a80c6e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 11:56:28 +0200 Subject: [PATCH 03/23] Rename Executive::setResultRef -> collectResult. --- libsolidity/solidityExecutionFramework.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index ea277421a..8b8d851a2 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -136,7 +136,7 @@ protected: m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); eth::ExecutionResult res; - executive.setResultRef(res); + executive.collectResult(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); bytes transactionRLP = t.rlp(); From 5bca09ed2ab0b04b6fe17ba27120f5a1adca5205 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 7 Jun 2015 09:06:06 +0200 Subject: [PATCH 04/23] eth_newPendingTransactionFilter From 58f2e59e2aa8c20b09cc4e1da0dcb0fd8caea12b Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 7 Jun 2015 09:29:46 +0200 Subject: [PATCH 05/23] Squashed 'libjsqrc/ethereumjs/' changes from 16861fc..ca46cb5 ca46cb5 updated examples aff3497 updated icap example 9fa9b16 gulp 61f1ba6 Merge pull request #224 from alexvandesande/prefix-name-reorg 448dd30 Merge branch 'master' into develop 7753724 build files fc3dc7a build files c9ebd7e version 0.5.0 448cf03 Merge branch 'master' into develop 43e8f0e Merge pull request #207 from ethereum/icap 0a56733 updated icap example e67e705 Merge pull request #223 from ethereum/revert-222-master f229f4e Revert "sync sendTransaction returning tx address" ca58837 Merge pull request #222 from jesuscript/master 66a2b6c sync sendTransaction returning tx address b19e46c updated "deposit" method description, updated icap example contract abi fbb9a41 Merge branch 'develop' into icap 3bb6e4f sha3 backward compatibility. #205 d0be181 fixed const functions calls handling errors 858d0c6 lint 95aabe3 sendIBANTransaction && tests 5866f08 milli should have two l's bacb03c Rename Kwei to kwei, added support for some SI base units for ether dfd5060 use "official" namereg, updated examples d8ad2b7 Merge branch 'develop' into icap 3fb420f Merge branch 'master' into develop ea4d66e updated examples e6209c6 Merge branch 'master' into develop 71ae809 version 0.4.3 92e2a2f Merge branch 'master' into develop d03bec6 decoding of empty array, fixed #210, fixed #211 9abf38a fixed encoding of empty arrays 2ad458c Merge pull request #212 from ethereum/estimateGas be2e93f build d4bf850 fixed typo 0594e7f add estimateGas to contract methods and fixed sendTransaction return value 55c4653 test/isIBAN.js e9483a6 icap.html example 6fb04d8 namereg example allows to register custom names 60c9bf8 removed natspec example, added namereg example cd773fc updated docs 4af0085 web3.eth.namereg contract, icap in progress 02556ea removed unnecessary file d320552 crypto-js integrated into project adf91df sha3 init git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: ca46cb5c94da4d37e9f4a5b8f6c0d117b72668d7 --- batch.js | 4 - coder.decodeParam.js | 2 + coder.encodeParam.js | 2 + contract.js | 308 ++++++++++++++------------------ sha3.js | 17 ++ utils.isIBAN.js | 32 ++++ utils.toWei.js | 8 + web3.eth.sendIBANTransaction.js | 49 +++++ web3.sha3.js | 16 -- 9 files changed, 240 insertions(+), 198 deletions(-) create mode 100644 sha3.js create mode 100644 utils.isIBAN.js create mode 100644 web3.eth.sendIBANTransaction.js delete mode 100644 web3.sha3.js diff --git a/batch.js b/batch.js index 69ae8fd58..f368a5d74 100644 --- a/batch.js +++ b/batch.js @@ -58,10 +58,6 @@ describe('lib/web3/batch', function () { var address = '0x0000000000000000000000000000000000000000'; var result = '0x126'; var result2 = '0x0000000000000000000000000000000000000000000000000000000000000123'; - var signature = '0x001122334455'; - - // TODO: fix this, maybe in browser sha3? - provider.injectResult(signature); var counter = 0; var callback = function (err, r) { diff --git a/coder.decodeParam.js b/coder.decodeParam.js index 23b0228eb..959e96cf2 100644 --- a/coder.decodeParam.js +++ b/coder.decodeParam.js @@ -24,6 +24,8 @@ describe('lib/solidity/coder', function () { test({ type: 'bytes', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000009' + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000000'}); test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000003'}); diff --git a/coder.encodeParam.js b/coder.encodeParam.js index 60d1c618e..55ff657c2 100644 --- a/coder.encodeParam.js +++ b/coder.encodeParam.js @@ -24,6 +24,8 @@ describe('lib/solidity/coder', function () { test({ type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000009' + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'int[]', value: [], expected: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000000'}); test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000003'}); diff --git a/contract.js b/contract.js index a46a8cab3..00f9cbcc8 100644 --- a/contract.js +++ b/contract.js @@ -5,6 +5,7 @@ var FakeHttpProvider = require('./helpers/FakeHttpProvider'); var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2'); var utils = require('../lib/utils/utils'); var BigNumber = require('bignumber.js'); +var sha3 = require('../lib/utils/sha3'); var desc = [{ "name": "balance(address)", @@ -60,34 +61,28 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); // reset different polls - var sha3 = '0x5131231231231231231231'; - provider.injectResult(sha3); + var signature = 'Changed(address,uint256,uint256,uint256)'; var step = 0; provider.injectValidation(function (payload) { if (step === 0) { step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('Changed(address,uint256,uint256,uint256)')); - } else if (step === 1) { - step = 2; provider.injectResult(3); assert.equal(payload.jsonrpc, '2.0'); assert.equal(payload.method, 'eth_newFilter'); assert.deepEqual(payload.params[0], { topics: [ - sha3, + '0x' + sha3(signature), '0x0000000000000000000000001234567890123456789012345678901234567890', null ], address: '0x1234567890123456789012345678901234567890' }); - } else if (step === 2) { - step = 3; + } else if (step === 1) { + step = 2; provider.injectResult([{ address: address, topics: [ - sha3, + '0x' + sha3(signature), '0x0000000000000000000000001234567890123456789012345678901234567890', '0x0000000000000000000000000000000000000000000000000000000000000001' ], @@ -97,11 +92,11 @@ describe('web3.eth.contract', function () { }]); assert.equal(payload.jsonrpc, '2.0'); assert.equal(payload.method, 'eth_getFilterLogs'); - } else if (step === 3 && utils.isArray(payload)) { + } else if (step === 2 && utils.isArray(payload)) { provider.injectBatchResults([[{ address: address, topics: [ - sha3, + '0x' + sha3(signature), '0x0000000000000000000000001234567890123456789012345678901234567890', '0x0000000000000000000000000000000000000000000000000000000000000001' ], @@ -135,53 +130,37 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032'); + var signature = 'balance(address)' var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('balance(address)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_call'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + '0000000000000000000000001234567890123456789012345678901234567890', - to: address - }, 'latest']); - } + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890', + to: address + }, 'latest']); }); var contract = web3.eth.contract(desc).at(address); - contract.balance(address); + var r = contract.balance(address); + assert.deepEqual(new BigNumber(0x32), r); }); it('should sendTransaction to contract function', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + var signature = 'send(address,uint256)'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('send(address,uint256)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_sendTransaction'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000001234567890123456789012345678901234567890' + - '0000000000000000000000000000000000000000000000000000000000000011' , - to: address - }]); - } + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address + }]); }); var contract = web3.eth.contract(desc).at(address); @@ -194,30 +173,23 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032'); + var signature = 'balance(address)'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('balance(address)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_call'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + '0000000000000000000000001234567890123456789012345678901234567890', - to: address, - from: address, - gas: '0xc350' - }, 'latest']); - } + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890', + to: address, + from: address, + gas: '0xc350' + }, 'latest']); }); var contract = web3.eth.contract(desc).at(address); - contract.balance(address, {from: address, gas: 50000}); + var r = contract.balance(address, {from: address, gas: 50000}); + assert.deepEqual(new BigNumber(0x32), r); }); @@ -226,30 +198,23 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032'); + var signature = 'balance(address)'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('balance(address)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_call'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + '0000000000000000000000001234567890123456789012345678901234567890', - to: address, - from: address, - gas: '0xc350' - }, 'latest']); - } + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890', + to: address, + from: address, + gas: '0xc350' + }, 'latest']); }); var contract = web3.eth.contract(desc).at(address); - contract.balance.call(address, {from: address, gas: 50000}); + var r = contract.balance.call(address, {from: address, gas: 50000}); + assert.deepEqual(new BigNumber(0x32), r); }); @@ -257,29 +222,20 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + var signature = 'send(address,uint256)'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('send(address,uint256)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_sendTransaction'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000001234567890123456789012345678901234567890' + - '0000000000000000000000000000000000000000000000000000000000000011' , - to: address, - from: address, - gas: '0xc350', - gasPrice: '0xbb8', - value: '0x2710' - }]); - } + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address, + from: address, + gas: '0xc350', + gasPrice: '0xbb8', + value: '0x2710' + }]); }); var contract = web3.eth.contract(desc).at(address); @@ -291,29 +247,20 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + var signature = 'send(address,uint256)'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('send(address,uint256)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_sendTransaction'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000001234567890123456789012345678901234567890' + - '0000000000000000000000000000000000000000000000000000000000000011' , - to: address, - from: address, - gas: '0xc350', - gasPrice: '0xbb8', - value: '0x2710' - }]); - } + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address, + from: address, + gas: '0xc350', + gasPrice: '0xbb8', + value: '0x2710' + }]); }); var contract = web3.eth.contract(desc).at(address); @@ -325,29 +272,20 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; var address = '0x1234567890123456789012345678901234567890'; - provider.injectResult(sha3); - var step = 0; + var signature = 'send(address,uint256)'; provider.injectValidation(function (payload) { - if (step === 0) { - step = 1; - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, 'web3_sha3'); - assert.equal(payload.params[0], web3.fromAscii('send(address,uint256)')); - } else if (step === 1) { - assert.equal(payload.method, 'eth_sendTransaction'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000001234567890123456789012345678901234567890' + - '0000000000000000000000000000000000000000000000000000000000000011' , - to: address, - from: address, - gas: '0xc350', - gasPrice: '0xbb8', - value: '0x2710' - }]); - } + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address, + from: address, + gas: '0xc350', + gasPrice: '0xbb8', + value: '0x2710' + }]); }); var contract = web3.eth.contract(desc).at(address); @@ -358,32 +296,52 @@ describe('web3.eth.contract', function () { }); }); + it('should explicitly estimateGas with optional params', function () { + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + web3.reset(); + var signature = 'send(address,uint256)'; + var address = '0x1234567890123456789012345678901234567890'; + provider.injectValidation(function (payload) { + assert.equal(payload.method, 'eth_estimateGas'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address, + from: address, + gas: '0xc350', + gasPrice: '0xbb8', + value: '0x2710' + }]); + }); + + var contract = web3.eth.contract(desc).at(address); + + contract.send.estimateGas(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}); + }); + it('should call testArr method and properly parse result', function () { var provider = new FakeHttpProvider2(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + var signature = 'testArr(int[])'; var address = '0x1234567890123456789012345678901234567890'; provider.injectResultList([{ - result: sha3 - }, { result: '0x0000000000000000000000000000000000000000000000000000000000000005' }]); - var step = 0; + provider.injectValidation(function (payload) { - if (step === 1) { // getting sha3 is first - assert.equal(payload.method, 'eth_call'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000000000000000000000000000000000000000000020' + - '0000000000000000000000000000000000000000000000000000000000000001' + - '0000000000000000000000000000000000000000000000000000000000000003', - to: address - }, - 'latest' + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003', + to: address + }, + 'latest' ]); - } - step++; }); var contract = web3.eth.contract(desc).at(address); @@ -396,28 +354,22 @@ describe('web3.eth.contract', function () { var provider = new FakeHttpProvider2(); web3.setProvider(provider); web3.reset(); - var sha3 = '0x5131231231231231231231'; + var signature = 'testArr(int[])'; var address = '0x1234567890123456789012345678901234567890'; provider.injectResultList([{ - result: sha3 - }, { result: '0x0000000000000000000000000000000000000000000000000000000000000005' }]); - var step = 0; provider.injectValidation(function (payload) { - if (step === 1) { // getting sha3 is first - assert.equal(payload.method, 'eth_call'); - assert.deepEqual(payload.params, [{ - data: sha3.slice(0, 10) + - '0000000000000000000000000000000000000000000000000000000000000020' + - '0000000000000000000000000000000000000000000000000000000000000001' + - '0000000000000000000000000000000000000000000000000000000000000003', - to: address - }, - 'latest' - ]); - } - step++; + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x' + sha3(signature).slice(0, 8) + + '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003', + to: address + }, + 'latest' + ]); }); var contract = web3.eth.contract(desc).at(address); diff --git a/sha3.js b/sha3.js new file mode 100644 index 000000000..e349887e7 --- /dev/null +++ b/sha3.js @@ -0,0 +1,17 @@ +var chai = require('chai'); +var assert = chai.assert; +var sha3 = require('../lib/utils/sha3'); +var web3 = require('../index'); + +describe('lib/utils/sha3', function () { + var test = function (v, e) { + it('should encode ' + v + ' to ' + e, function () { + assert.equal(sha3(v), e); + }); + }; + + test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); + test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); + test(web3.fromAscii('test123'), 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); +}); + diff --git a/utils.isIBAN.js b/utils.isIBAN.js new file mode 100644 index 000000000..194ccaa23 --- /dev/null +++ b/utils.isIBAN.js @@ -0,0 +1,32 @@ +var chai = require('chai'); +var utils = require('../lib/utils/utils.js'); +var assert = chai.assert; + +var tests = [ + { obj: function () {}, is: false}, + { obj: new Function(), is: false}, + { obj: 'function', is: false}, + { obj: {}, is: false}, + { obj: '[]', is: false}, + { obj: '[1, 2]', is: false}, + { obj: '{}', is: false}, + { obj: '{"a": 123, "b" :3,}', is: false}, + { obj: '{"c" : 2}', is: false}, + { obj: 'XE81ETHXREGGAVOFYORK', is: true}, + { obj: 'XE81ETCXREGGAVOFYORK', is: false}, + { obj: 'XE81ETHXREGGAVOFYORKD', is: false}, + { obj: 'XE81ETHXREGGaVOFYORK', is: false}, + { obj: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: true}, + { obj: 'XD7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: false} +]; + +describe('lib/utils/utils', function () { + describe('isIBAN', function () { + tests.forEach(function (test) { + it('shoud test if value ' + test.obj + ' is iban: ' + test.is, function () { + assert.equal(utils.isIBAN(test.obj), test.is); + }); + }); + }); +}); + diff --git a/utils.toWei.js b/utils.toWei.js index 3bb0997c6..55b6c9328 100644 --- a/utils.toWei.js +++ b/utils.toWei.js @@ -19,6 +19,14 @@ describe('lib/utils/utils', function () { assert.equal(utils.toWei(1, 'gether'), '1000000000000000000000000000'); assert.equal(utils.toWei(1, 'tether'), '1000000000000000000000000000000'); + assert.equal(utils.toWei(1, 'kwei'), utils.toWei(1, 'femtoether')); + assert.equal(utils.toWei(1, 'babbage'), utils.toWei(1, 'picoether')); + assert.equal(utils.toWei(1, 'shannon'), utils.toWei(1, 'nanoether')); + assert.equal(utils.toWei(1, 'szabo'), utils.toWei(1, 'microether')); + assert.equal(utils.toWei(1, 'finney'), utils.toWei(1, 'milliether')); + assert.equal(utils.toWei(1, 'milli'), utils.toWei(1, 'milliether')); + assert.equal(utils.toWei(1, 'milli'), utils.toWei(1000, 'micro')); + assert.throws(function () {utils.toWei(1, 'wei1');}, Error); }); }); diff --git a/web3.eth.sendIBANTransaction.js b/web3.eth.sendIBANTransaction.js new file mode 100644 index 000000000..9d478e13f --- /dev/null +++ b/web3.eth.sendIBANTransaction.js @@ -0,0 +1,49 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2'); + +describe('web3.eth.sendIBANTransaction', function () { + it('should send transaction', function () { + + var iban = 'XE81ETHXREGGAVOFYORK'; + var address = '0x1234567890123456789012345678901234500000'; + var exAddress = '0x1234567890123456789012345678901234567890' + + var provider = new FakeHttpProvider2(); + web3.setProvider(provider); + web3.reset(); + + provider.injectResultList([{ + result: exAddress + }, { + result: '' + }]); + + var step = 0; + provider.injectValidation(function (payload) { + if (step === 0) { + step++; + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: '0x3b3b57de5852454700000000000000000000000000000000000000000000000000000000', + to: web3.eth.namereg.address + }, "latest"]); + + return; + } + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: '0xb214faa54741564f46594f524b0000000000000000000000000000000000000000000000', + from: address, + to: exAddress, + value: payload.params[0].value // don't check this + }]); + }); + + web3.eth.sendIBANTransaction(address, iban, 10000); + + }); +}); + diff --git a/web3.sha3.js b/web3.sha3.js deleted file mode 100644 index 0ae104962..000000000 --- a/web3.sha3.js +++ /dev/null @@ -1,16 +0,0 @@ -var BigNumber = require('bignumber.js'); -var web3 = require('../index'); -var testMethod = require('./helpers/test.method.js'); - -var method = 'sha3'; - -var tests = [{ - args: ['myString'], - formattedArgs: ['myString'], - result: '0x319319f831983198319881', - formattedResult: '0x319319f831983198319881', - call: 'web3_'+ method -}]; - -testMethod.runTests(null, method, tests); - From aea36494f0ffb142a91b667ed5b19e21adb7568a Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 8 Jun 2015 09:42:04 +0200 Subject: [PATCH 06/23] new Ex methods From b556f00dfbe88dd741d45de9e74ff063c9bddc8d Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 8 Jun 2015 10:26:07 +0200 Subject: [PATCH 07/23] codeHash in ExtVM From 8c66813683d176ae47bf87c691a924efca8627eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 8 Jun 2015 10:45:42 +0200 Subject: [PATCH 08/23] Rename Executive::collectResult -> setResultRecipient. --- libsolidity/solidityExecutionFramework.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 133b23604..a36e0b4ed 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -149,7 +149,7 @@ protected: m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); eth::ExecutionResult res; - executive.collectResult(res); + executive.setResultRecipient(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : From a16ec4471ca78d93e79df2e6cdd77a5656fde441 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 8 Jun 2015 11:24:00 +0200 Subject: [PATCH 09/23] add precompiled contracts with prefixed 0's and retune gas limit From 46eb7b08d9fe52f73895bae46648a79cea15c64e Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 8 Jun 2015 11:47:57 +0200 Subject: [PATCH 10/23] added test Conflicts: test/libsolidity/SolidityEndToEndTest.cpp --- libsolidity/SolidityEndToEndTest.cpp | 51 ++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 89ed81e23..04ad40d75 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -567,7 +567,7 @@ BOOST_AUTO_TEST_CASE(strings) BOOST_AUTO_TEST_CASE(empty_string_on_stack) { char const* sourceCode = "contract test {\n" - " function run(bytes0 empty, uint8 inp) returns(uint16 a, bytes0 b, bytes4 c) {\n" + " function run(string empty, uint8 inp) external returns(uint16 a, string b, bytes4 c) {\n" " var x = \"abc\";\n" " var y = \"\";\n" " var z = inp;\n" @@ -3786,25 +3786,25 @@ BOOST_AUTO_TEST_CASE(packed_storage_structs_delete) BOOST_CHECK(m_state.storage(m_contractAddress).empty()); } -BOOST_AUTO_TEST_CASE(packed_storage_structs_with_bytes0) +BOOST_AUTO_TEST_CASE(packed_storage_structs_with_empty_string) { char const* sourceCode = R"( - contract C { - struct str { uint8 a; bytes0 b; uint8 c; } - uint8 a; - bytes0 x; - uint8 b; - str data; - function test() returns (bool) { - a = 2; - b = 3; - data.a = 4; - data.c = 5; - delete x; - delete data.b; - return a == 2 && b == 3 && data.a == 4 && data.c == 5; - } + contract C { + struct str { uint8 a; string b; uint8 c; } + uint8 a; + uint8 b; + str data; + function test() returns (bool) { + a = 2; + b = 3; + var x = ""; + data.a = 4; + data.c = 5; + delete x; + delete data.b; + return a == 2 && b == 3 && data.a == 4 && data.c == 5; } + } )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); @@ -4172,6 +4172,23 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) BOOST_CHECK(compileAndRunWthoutCheck(sourceCode, 0, "A").empty()); } +BOOST_AUTO_TEST_CASE(empty_string) +{ + char const* sourceCode = R"( + contract Foo { + var sEmpty = ""; + string sStateVar = "text"; + function Foo() + { + var sLocal = ""; + sEmpty = sLocal; + sLocal = s; + } + } + )"; + compileAndRun(sourceCode, 0, "Foo"); +} + BOOST_AUTO_TEST_CASE(positive_integers_to_signed) { char const* sourceCode = R"( From 9425cfacf8b783d40f30b0171a5e29a163e54032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 8 Jun 2015 12:09:24 +0200 Subject: [PATCH 11/23] Remove pessimising moves. From d38258e466b03cb094abdfd01c133443983f0aed Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 8 Jun 2015 12:39:07 +0200 Subject: [PATCH 12/23] all precompiled contracts tests as CALLCODE From 88ba0c4eea68c9c78dbcd95153039ca9d9bb7919 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 8 Jun 2015 12:54:11 +0200 Subject: [PATCH 13/23] add sec80 test as provided by sec From 1ac5f3c04d3df03df4ae2997c1a2ea0ced9f1311 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 8 Jun 2015 13:08:22 +0200 Subject: [PATCH 14/23] removed byte0 keyword --- libsolidity/SolidityEndToEndTest.cpp | 31 ------------------- libsolidity/SolidityNameAndTypeResolution.cpp | 3 +- 2 files changed, 1 insertion(+), 33 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 04ad40d75..a2c5556d1 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -564,20 +564,6 @@ BOOST_AUTO_TEST_CASE(strings) BOOST_CHECK(callContractFunction("pipeThrough(bytes2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true)); } -BOOST_AUTO_TEST_CASE(empty_string_on_stack) -{ - char const* sourceCode = "contract test {\n" - " function run(string empty, uint8 inp) external returns(uint16 a, string b, bytes4 c) {\n" - " var x = \"abc\";\n" - " var y = \"\";\n" - " var z = inp;\n" - " a = z; b = y; c = x;" - " }\n" - "}\n"; - compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(bytes0,uint8)", string(), byte(0x02)) == encodeArgs(0x2, string(""), string("abc\0"))); -} - BOOST_AUTO_TEST_CASE(inc_dec_operators) { char const* sourceCode = R"( @@ -4172,23 +4158,6 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) BOOST_CHECK(compileAndRunWthoutCheck(sourceCode, 0, "A").empty()); } -BOOST_AUTO_TEST_CASE(empty_string) -{ - char const* sourceCode = R"( - contract Foo { - var sEmpty = ""; - string sStateVar = "text"; - function Foo() - { - var sLocal = ""; - sEmpty = sLocal; - sLocal = s; - } - } - )"; - compileAndRun(sourceCode, 0, "Foo"); -} - BOOST_AUTO_TEST_CASE(positive_integers_to_signed) { char const* sourceCode = R"( diff --git a/libsolidity/SolidityNameAndTypeResolution.cpp b/libsolidity/SolidityNameAndTypeResolution.cpp index 73bbcb162..ca89f42bc 100644 --- a/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1579,7 +1579,6 @@ BOOST_AUTO_TEST_CASE(test_fromElementaryTypeName) BOOST_CHECK(*Type::fromElementaryTypeName(Token::UInt256) == *make_shared(256, IntegerType::Modifier::Unsigned)); BOOST_CHECK(*Type::fromElementaryTypeName(Token::Byte) == *make_shared(1)); - BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes0) == *make_shared(0)); BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes1) == *make_shared(1)); BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes2) == *make_shared(2)); BOOST_CHECK(*Type::fromElementaryTypeName(Token::Bytes3) == *make_shared(3)); @@ -1673,7 +1672,7 @@ BOOST_AUTO_TEST_CASE(bytes0_array) bytes0[] illegalArray; } )"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); + BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError); } BOOST_AUTO_TEST_CASE(overloaded_function_cannot_resolve) From 18d8f58970752f1a3c74bef3bf74ba403581e9dd Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 8 Jun 2015 16:30:13 +0200 Subject: [PATCH 15/23] move test from stSpecialTest to transaction test From 31fb199acbd9ae8d9d7e5183ce3debd594158837 Mon Sep 17 00:00:00 2001 From: chriseth Date: Sat, 6 Jun 2015 00:57:51 +0200 Subject: [PATCH 16/23] Use dynamic memory for argument encoding. --- libsolidity/SolidityEndToEndTest.cpp | 51 ++++++++++++++++++- libsolidity/SolidityNameAndTypeResolution.cpp | 10 ---- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 89ed81e23..73f7d60d6 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -2396,7 +2396,7 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data) callContractFunction("deposit()"); BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); - BOOST_CHECK(m_logs[0].data == encodeArgs(10, 4, 15) + FixedHash<4>(dev::sha3("deposit()")).asBytes()); + BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 4) + FixedHash<4>(dev::sha3("deposit()")).asBytes()); BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(uint256,bytes,uint256)"))); } @@ -2420,7 +2420,7 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data_from_storage) callContractFunction("deposit()"); BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); - BOOST_CHECK(m_logs[0].data == encodeArgs(10, 3, 15) + asBytes("ABC")); + BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 3) + asBytes("ABC")); BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(uint256,bytes,uint256)"))); } @@ -2531,6 +2531,27 @@ BOOST_AUTO_TEST_CASE(sha3_with_bytes) BOOST_CHECK(callContractFunction("foo()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(iterated_sha3_with_bytes) +{ + char const* sourceCode = R"( + contract c { + bytes data; + function foo() returns (bytes32) + { + data.length = 3; + data[0] = "x"; + data[1] = "y"; + data[2] = "z"; + return sha3("b", sha3(data), "a"); + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("foo()") == encodeArgs( + u256(dev::sha3(bytes{'b'} + dev::sha3("xyz").asBytes() + bytes{'a'})) + )); +} + BOOST_AUTO_TEST_CASE(generic_call) { char const* sourceCode = R"**( @@ -4209,6 +4230,32 @@ BOOST_AUTO_TEST_CASE(failing_send) BOOST_REQUIRE(callContractFunction("callHelper(address)", c_helperAddress) == encodeArgs(true, 20)); } +BOOST_AUTO_TEST_CASE(reusing_memory) +{ + // Invoke some features that use memory and test that they do not interfere with each other. + char const* sourceCode = R"( + contract Helper { + uint public flag; + function Helper(uint x) { + flag = x; + } + } + contract Main { + mapping(uint => uint) map; + function f(uint x) returns (uint) { + map[x] = x; + return (new Helper(uint(sha3(this.g(map[x]))))).flag(); + } + function g(uint a) returns (uint) + { + return map[a]; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + BOOST_REQUIRE(callContractFunction("f(uint256)", 0x34) == encodeArgs(dev::sha3(dev::toBigEndian(u256(0x34))))); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/libsolidity/SolidityNameAndTypeResolution.cpp b/libsolidity/SolidityNameAndTypeResolution.cpp index 73bbcb162..111637f43 100644 --- a/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/libsolidity/SolidityNameAndTypeResolution.cpp @@ -558,16 +558,6 @@ BOOST_AUTO_TEST_CASE(function_external_call_not_allowed_conversion) BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); } -// todo delete when implemented -BOOST_AUTO_TEST_CASE(arrays_in_internal_functions) -{ - char const* text = R"( - contract Test { - function foo(address[] addresses) {} - })"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); -} - BOOST_AUTO_TEST_CASE(function_internal_allowed_conversion) { char const* text = R"( From 4967535f8a9b46edfa029793e9d38125afd886cf Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Tue, 9 Jun 2015 11:55:19 +0200 Subject: [PATCH 17/23] cleaned up the tests --- libsolidity/SolidityEndToEndTest.cpp | 38 +++++++------------ libsolidity/SolidityNameAndTypeResolution.cpp | 10 ----- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index a2c5556d1..fdd865e74 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -564,6 +564,20 @@ BOOST_AUTO_TEST_CASE(strings) BOOST_CHECK(callContractFunction("pipeThrough(bytes2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true)); } +BOOST_AUTO_TEST_CASE(empty_string_on_stack) +{ + char const* sourceCode = R"( + contract test { + function run() external returns(bytes2 ret) { + var y = ""; + ret = y; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("run()") == encodeArgs(byte(0x00))); +} + BOOST_AUTO_TEST_CASE(inc_dec_operators) { char const* sourceCode = R"( @@ -3772,30 +3786,6 @@ BOOST_AUTO_TEST_CASE(packed_storage_structs_delete) BOOST_CHECK(m_state.storage(m_contractAddress).empty()); } -BOOST_AUTO_TEST_CASE(packed_storage_structs_with_empty_string) -{ - char const* sourceCode = R"( - contract C { - struct str { uint8 a; string b; uint8 c; } - uint8 a; - uint8 b; - str data; - function test() returns (bool) { - a = 2; - b = 3; - var x = ""; - data.a = 4; - data.c = 5; - delete x; - delete data.b; - return a == 2 && b == 3 && data.a == 4 && data.c == 5; - } - } - )"; - compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); -} - BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_first) { char const* sourceCode = R"( diff --git a/libsolidity/SolidityNameAndTypeResolution.cpp b/libsolidity/SolidityNameAndTypeResolution.cpp index ca89f42bc..b078fe6a2 100644 --- a/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1665,16 +1665,6 @@ BOOST_AUTO_TEST_CASE(local_const_variable) BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError); } -BOOST_AUTO_TEST_CASE(bytes0_array) -{ - char const* text = R"( - contract Foo { - bytes0[] illegalArray; - } - )"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError); -} - BOOST_AUTO_TEST_CASE(overloaded_function_cannot_resolve) { char const* sourceCode = R"( From e9a73acba9660903081f21ef0fef92a134c2d5a4 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 9 Jun 2015 13:21:56 +0200 Subject: [PATCH 18/23] udpated eth_compileSolidity From 8f2f69f4907bff447b2370aa408b7464c814b157 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Mon, 8 Jun 2015 22:28:49 +0300 Subject: [PATCH 19/23] stPrecompiledContracts from transaction test --- TestHelper.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/TestHelper.cpp b/TestHelper.cpp index f7da0238e..873ea21e2 100644 --- a/TestHelper.cpp +++ b/TestHelper.cpp @@ -344,6 +344,16 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost) m_TestObject["out"] = (_output.size() > 4096 && !Options::get().fulloutput) ? "#" + toString(_output.size()) : toHex(_output, 2, HexPrefix::Add); + // compare expected output with post output + if (m_TestObject.count("expectOut") > 0) + { + std::string warning = "Check State: Error! Unexpected output: " + m_TestObject["out"].get_str() + " Expected: " + m_TestObject["expectOut"].get_str(); + if (Options::get().checkState) + BOOST_CHECK_MESSAGE((m_TestObject["out"].get_str() == m_TestObject["expectOut"].get_str()), warning); + else + BOOST_WARN_MESSAGE((m_TestObject["out"].get_str() == m_TestObject["expectOut"].get_str()), warning); + } + // export logs m_TestObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries()); From da5eca2f95e545bd587480a39c296037865ce8c2 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Tue, 9 Jun 2015 16:35:35 +0300 Subject: [PATCH 20/23] Precomp. Contracts from Transaction: one more test From 7c4324eb5fcee546f809c81b29d2aac5a079d2fc Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 10 Jun 2015 17:51:24 +0200 Subject: [PATCH 21/23] Added missing source locations for new memory management code. --- libsolidity/Assembly.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libsolidity/Assembly.cpp b/libsolidity/Assembly.cpp index ccc4bf811..fd4bbcf6d 100644 --- a/libsolidity/Assembly.cpp +++ b/libsolidity/Assembly.cpp @@ -105,8 +105,8 @@ BOOST_AUTO_TEST_CASE(location_test) shared_ptr n = make_shared("source"); AssemblyItems items = compileContract(sourceCode); vector locations = - vector(11, SourceLocation(2, 75, n)) + - vector(12, SourceLocation(20, 72, n)) + + vector(17, SourceLocation(2, 75, n)) + + vector(14, SourceLocation(20, 72, n)) + vector{SourceLocation(42, 51, n), SourceLocation(65, 67, n)} + vector(4, SourceLocation(58, 67, n)) + vector(3, SourceLocation(20, 72, n)); From 67299b63522d30b2d83957a4198a282cfd33a90f Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 10 Jun 2015 17:37:17 +0200 Subject: [PATCH 22/23] Wallet tests. --- libsolidity/SolidityWallet.cpp | 491 +++++++++++++++++++++++++++++++++ 1 file changed, 491 insertions(+) create mode 100644 libsolidity/SolidityWallet.cpp diff --git a/libsolidity/SolidityWallet.cpp b/libsolidity/SolidityWallet.cpp new file mode 100644 index 000000000..09820b87b --- /dev/null +++ b/libsolidity/SolidityWallet.cpp @@ -0,0 +1,491 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** + * @author Christian + * @date 2015 + * Tests for a (comparatively) complex multisig wallet contract. + */ + +#include +#include +#include +#include +#include + +using namespace std; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +static char const* walletCode = R"DELIMITER( +//sol Wallet +// Multi-sig, daily-limited account proxy/wallet. +// @authors: +// Gav Wood +// inheritable "property" contract that enables methods to be protected by requiring the acquiescence of either a +// single, or, crucially, each of a number of, designated owners. +// usage: +// use modifiers onlyowner (just own owned) or onlymanyowners(hash), whereby the same hash must be provided by +// some number (specified in constructor) of the set of owners (specified in the constructor, modifiable) before the +// interior is executed. +contract multiowned { + // struct for the status of a pending operation. + struct PendingState { + uint yetNeeded; + uint ownersDone; + uint index; + } + // this contract only has five types of events: it can accept a confirmation, in which case + // we record owner and operation (hash) alongside it. + event Confirmation(address owner, bytes32 operation); + event Revoke(address owner, bytes32 operation); + // some others are in the case of an owner changing. + event OwnerChanged(address oldOwner, address newOwner); + event OwnerAdded(address newOwner); + event OwnerRemoved(address oldOwner); + // the last one is emitted if the required signatures change + event RequirementChanged(uint newRequirement); + // constructor is given number of sigs required to do protected "onlymanyowners" transactions + // as well as the selection of addresses capable of confirming them. + function multiowned() { + m_required = 1; + m_numOwners = 1; + m_owners[m_numOwners] = uint(msg.sender); + m_ownerIndex[uint(msg.sender)] = m_numOwners; + } + // simple single-sig function modifier. + modifier onlyowner { + if (isOwner(msg.sender)) + _ + } + // multi-sig function modifier: the operation must have an intrinsic hash in order + // that later attempts can be realised as the same underlying operation and + // thus count as confirmations. + modifier onlymanyowners(bytes32 _operation) { + if (confirmed(_operation)) + _ + } + // Revokes a prior confirmation of the given operation + function revoke(bytes32 _operation) external { + uint ownerIndex = m_ownerIndex[uint(msg.sender)]; + // make sure they're an owner + if (ownerIndex == 0) return; + uint ownerIndexBit = 2**ownerIndex; + var pending = m_pending[_operation]; + if (pending.ownersDone & ownerIndexBit > 0) { + pending.yetNeeded++; + pending.ownersDone -= ownerIndexBit; + Revoke(msg.sender, _operation); + } + } + function confirmed(bytes32 _operation) internal returns (bool) { + // determine what index the present sender is: + uint ownerIndex = m_ownerIndex[uint(msg.sender)]; + // make sure they're an owner + if (ownerIndex == 0) return; + + var pending = m_pending[_operation]; + // if we're not yet working on this operation, switch over and reset the confirmation status. + if (pending.yetNeeded == 0) { + // reset count of confirmations needed. + pending.yetNeeded = m_required; + // reset which owners have confirmed (none) - set our bitmap to 0. + pending.ownersDone = 0; + pending.index = m_pendingIndex.length++; + m_pendingIndex[pending.index] = _operation; + } + // determine the bit to set for this owner. + uint ownerIndexBit = 2**ownerIndex; + // make sure we (the message sender) haven't confirmed this operation previously. + if (pending.ownersDone & ownerIndexBit == 0) { + Confirmation(msg.sender, _operation); + // ok - check if count is enough to go ahead. + if (pending.yetNeeded <= 1) { + // enough confirmations: reset and run interior. + delete m_pendingIndex[m_pending[_operation].index]; + delete m_pending[_operation]; + return true; + } + else + { + // not enough: record that this owner in particular confirmed. + pending.yetNeeded--; + pending.ownersDone |= ownerIndexBit; + } + } + } + // Replaces an owner `_from` with another `_to`. + function changeOwner(address _from, address _to) onlymanyowners(sha3(msg.data)) external { + if (isOwner(_to)) return; + uint ownerIndex = m_ownerIndex[uint(_from)]; + if (ownerIndex == 0) return; + + clearPending(); + m_owners[ownerIndex] = uint(_to); + m_ownerIndex[uint(_from)] = 0; + m_ownerIndex[uint(_to)] = ownerIndex; + OwnerChanged(_from, _to); + } + function addOwner(address _owner) onlymanyowners(sha3(msg.data)) external { + if (isOwner(_owner)) return; + + clearPending(); + if (m_numOwners >= c_maxOwners) + reorganizeOwners(); + if (m_numOwners >= c_maxOwners) + return; + m_numOwners++; + m_owners[m_numOwners] = uint(_owner); + m_ownerIndex[uint(_owner)] = m_numOwners; + OwnerAdded(_owner); + } + function removeOwner(address _owner) onlymanyowners(sha3(msg.data)) external { + uint ownerIndex = m_ownerIndex[uint(_owner)]; + if (ownerIndex == 0) return; + if (m_required > m_numOwners - 1) return; + + m_owners[ownerIndex] = 0; + m_ownerIndex[uint(_owner)] = 0; + clearPending(); + reorganizeOwners(); //make sure m_numOwner is equal to the number of owners and always points to the optimal free slot + OwnerRemoved(_owner); + } + function reorganizeOwners() private returns (bool) { + uint free = 1; + while (free < m_numOwners) + { + while (free < m_numOwners && m_owners[free] != 0) free++; + while (m_numOwners > 1 && m_owners[m_numOwners] == 0) m_numOwners--; + if (free < m_numOwners && m_owners[m_numOwners] != 0 && m_owners[free] == 0) + { + m_owners[free] = m_owners[m_numOwners]; + m_ownerIndex[m_owners[free]] = free; + m_owners[m_numOwners] = 0; + } + } + } + function clearPending() internal { + uint length = m_pendingIndex.length; + for (uint i = 0; i < length; ++i) + if (m_pendingIndex[i] != 0) + delete m_pending[m_pendingIndex[i]]; + delete m_pendingIndex; + } + function changeRequirement(uint _newRequired) onlymanyowners(sha3(msg.data)) external { + if (_newRequired > m_numOwners) return; + m_required = _newRequired; + clearPending(); + RequirementChanged(_newRequired); + } + function isOwner(address _addr) returns (bool) { + return m_ownerIndex[uint(_addr)] > 0; + } + + // the number of owners that must confirm the same operation before it is run. + uint m_required; + // pointer used to find a free slot in m_owners + uint m_numOwners; + // list of owners + uint[256] m_owners; + uint constant c_maxOwners = 250; + // index on the list of owners to allow reverse lookup + mapping(uint => uint) m_ownerIndex; + // the ongoing operations. + mapping(bytes32 => PendingState) m_pending; + bytes32[] m_pendingIndex; +} + +// inheritable "property" contract that enables methods to be protected by placing a linear limit (specifiable) +// on a particular resource per calendar day. is multiowned to allow the limit to be altered. resource that method +// uses is specified in the modifier. +contract daylimit is multiowned { + // constructor - just records the present day's index. + function daylimit() { + m_lastDay = today(); + } + // (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today. + function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external { + m_dailyLimit = _newLimit; + } + // (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today. + function resetSpentToday() onlymanyowners(sha3(msg.data)) external { + m_spentToday = 0; + } + // checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and + // returns true. otherwise just returns false. + function underLimit(uint _value) internal onlyowner returns (bool) { + // reset the spend limit if we're on a different day to last time. + if (today() > m_lastDay) { + m_spentToday = 0; + m_lastDay = today(); + } + // check to see if there's enough left - if so, subtract and return true. + if (m_spentToday + _value >= m_spentToday && m_spentToday + _value <= m_dailyLimit) { + m_spentToday += _value; + return true; + } + return false; + } + // simple modifier for daily limit. + modifier limitedDaily(uint _value) { + if (underLimit(_value)) + _ + } + // determines today's index. + function today() private constant returns (uint) { return now / 1 days; } + uint m_spentToday; + uint m_dailyLimit; + uint m_lastDay; +} +// interface contract for multisig proxy contracts; see below for docs. +contract multisig { + event Deposit(address from, uint value); + event SingleTransact(address owner, uint value, address to, bytes data); + event MultiTransact(address owner, bytes32 operation, uint value, address to, bytes data); + event ConfirmationNeeded(bytes32 operation, address initiator, uint value, address to, bytes data); + function changeOwner(address _from, address _to) external; + function execute(address _to, uint _value, bytes _data) external returns (bytes32); + function confirm(bytes32 _h) returns (bool); +} +// usage: +// bytes32 h = Wallet(w).from(oneOwner).transact(to, value, data); +// Wallet(w).from(anotherOwner).confirm(h); +contract Wallet is multisig, multiowned, daylimit { + // Transaction structure to remember details of transaction lest it need be saved for a later call. + struct Transaction { + address to; + uint value; + bytes data; + } + // logged events: + // Funds has arrived into the wallet (record how much). + event Deposit(address from, uint value); + // Single transaction going out of the wallet (record who signed for it, how much, and to whom it's going). + event SingleTransact(address owner, uint value, address to, bytes data); + // constructor - just pass on the owner arra to the multiowned. + event Created(); + function Wallet() { + Created(); + } + // kills the contract sending everything to `_to`. + function kill(address _to) onlymanyowners(sha3(msg.data)) external { + suicide(_to); + } + // gets called when no other function matches + function() { + // just being sent some cash? + if (msg.value > 0) + Deposit(msg.sender, msg.value); + } + // Outside-visible transact entry point. Executes transacion immediately if below daily spend limit. + // If not, goes into multisig process. We provide a hash on return to allow the sender to provide + // shortcuts for the other confirmations (allowing them to avoid replicating the _to, _value + // and _data arguments). They still get the option of using them if they want, anyways. + function execute(address _to, uint _value, bytes _data) onlyowner external returns (bytes32 _r) { + // first, take the opportunity to check that we're under the daily limit. + if (underLimit(_value)) { + SingleTransact(msg.sender, _value, _to, _data); + // yes - just execute the call. + _to.call.value(_value)(_data); + return 0; + } + // determine our operation hash. + _r = sha3(msg.data); + if (!confirm(_r) && m_txs[_r].to == 0) { + m_txs[_r].to = _to; + m_txs[_r].value = _value; + m_txs[_r].data = _data; + ConfirmationNeeded(_r, msg.sender, _value, _to, _data); + } + } + // confirm a transaction through just the hash. we use the previous transactions map, m_txs, in order + // to determine the body of the transaction from the hash provided. + function confirm(bytes32 _h) onlymanyowners(_h) returns (bool) { + if (m_txs[_h].to != 0) { + m_txs[_h].to.call.value(m_txs[_h].value)(m_txs[_h].data); + MultiTransact(msg.sender, _h, m_txs[_h].value, m_txs[_h].to, m_txs[_h].data); + delete m_txs[_h]; + return true; + } + } + function clearPending() internal { + uint length = m_pendingIndex.length; + for (uint i = 0; i < length; ++i) + delete m_txs[m_pendingIndex[i]]; + super.clearPending(); + } + // pending transactions we have at present. + mapping (bytes32 => Transaction) m_txs; +} +)DELIMITER"; + +static unique_ptr s_compiledWallet; + +class WalletTestFramework: public ExecutionFramework +{ +protected: + void deployWallet(u256 const& _value = 0) + { + if (!s_compiledWallet) + { + m_optimize = true; + m_compiler.reset(false, m_addStandardSources); + m_compiler.addSource("", walletCode); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); + s_compiledWallet.reset(new bytes(m_compiler.getBytecode("Wallet"))); + } + sendMessage(*s_compiledWallet, true, _value); + BOOST_REQUIRE(!m_output.empty()); + } +}; + +/// This is a test suite that tests optimised code! +BOOST_FIXTURE_TEST_SUITE(SolidityWallet, WalletTestFramework) + +BOOST_AUTO_TEST_CASE(creation) +{ + deployWallet(200); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(m_sender, h256::AlignRight)) == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("isOwner(address)", ~h256(m_sender, h256::AlignRight)) == encodeArgs(false)); +} + +BOOST_AUTO_TEST_CASE(add_owners) +{ + deployWallet(200); + Address originalOwner = m_sender; + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); + // now let the new owner add someone + m_sender = Address(0x12); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); + // and check that a non-owner cannot add a new owner + m_sender = Address(0x50); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x20)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x20)) == encodeArgs(false)); + // finally check that all the owners are there + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(originalOwner, h256::AlignRight)) == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); +} + +BOOST_AUTO_TEST_CASE(change_owners) +{ + deployWallet(200); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("changeOwner(address,address)", h256(0x12), h256(0x13)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(false)); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); +} + +BOOST_AUTO_TEST_CASE(remove_owner) +{ + deployWallet(200); + // add 10 owners + for (unsigned i = 0; i < 10; ++i) + { + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); + } + // check they are there again + for (unsigned i = 0; i < 10; ++i) + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); + // remove the odd owners + for (unsigned i = 0; i < 10; ++i) + if (i % 2 == 1) + BOOST_REQUIRE(callContractFunction("removeOwner(address)", h256(0x12 + i)) == encodeArgs()); + // check the result + for (unsigned i = 0; i < 10; ++i) + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(i % 2 == 0)); + // add them again + for (unsigned i = 0; i < 10; ++i) + if (i % 2 == 1) + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs()); + // check everyone is there + for (unsigned i = 0; i < 10; ++i) + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); +} + +BOOST_AUTO_TEST_CASE(multisig_value_transfer) +{ + deployWallet(200); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); + // 4 owners, set required to 3 + BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); + // check that balance is and stays zero at destination address + h256 opHash("f916231db11c12e0142dc51f23632bc655de87c63f83fc928c443e90f7aa364a"); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + m_sender = Address(0x12); + BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + m_sender = Address(0x13); + BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + m_sender = Address(0x14); + BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + // now it should go through + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 100); +} + +BOOST_AUTO_TEST_CASE(daylimit) +{ + deployWallet(200); + BOOST_REQUIRE(callContractFunction("setDailyLimit(uint256)", h256(100)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); + // 4 owners, set required to 3 + BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); + + // try to send tx over daylimit + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + m_sender = Address(0x12); + BOOST_REQUIRE( + callContractFunction("execute(address,uint256,bytes)", h256(0x05), 150, 0x60, 0x00) != + encodeArgs(u256(0)) + ); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + // try to send tx under daylimit by stranger + m_sender = Address(0x77); + BOOST_REQUIRE( + callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == + encodeArgs(u256(0)) + ); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + // now send below limit by owner + m_sender = Address(0x12); + BOOST_REQUIRE( + callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == + encodeArgs(u256(0)) + ); + BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 90); +} + +//@todo test data calls + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} // end namespaces From 41db3a6162cdb0d9450de6df604d93b65847c212 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Thu, 11 Jun 2015 18:53:29 +0300 Subject: [PATCH 23/23] expectOut fill section to vmTests --- TestHelper.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TestHelper.cpp b/TestHelper.cpp index 873ea21e2..733ccb6d0 100644 --- a/TestHelper.cpp +++ b/TestHelper.cpp @@ -352,6 +352,8 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost) BOOST_CHECK_MESSAGE((m_TestObject["out"].get_str() == m_TestObject["expectOut"].get_str()), warning); else BOOST_WARN_MESSAGE((m_TestObject["out"].get_str() == m_TestObject["expectOut"].get_str()), warning); + + m_TestObject.erase(m_TestObject.find("expectOut")); } // export logs