From 5aca97af0d7719dea36f4c33df35966e5e83be11 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 30 May 2016 15:01:37 +0200 Subject: Initial poc to test via ipc. --- test/libsolidity/solidityExecutionFramework.h | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index 3fcbfaed..a2c6d907 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -24,6 +24,7 @@ #include #include +#include #include "../TestHelper.h" #include #include @@ -54,6 +55,33 @@ public: if (g_logVerbosity != -1) g_logVerbosity = 0; //m_state.resetCurrent(); + m_ipcSocket.open("/home/christian/.ethereum/geth.ipc"); + rpcCall("personal_createAccount", {}); + } + + void rpcCall(std::string const& _methodName, std::vector const& _args) + { + if (!m_ipcSocket) + BOOST_FAIL("Ethereum node unavailable."); + m_ipcSocket << + "{\"jsonrpc\": \"2.0\", \"method\": \"" << + _methodName << + "\" \"params\": ["; + for (size_t i = 0; i < _args.size(); ++i) + { + m_ipcSocket << "\"" << _args[i] << "\""; + if (i + 1 != _args.size()) + m_ipcSocket << ", "; + } + m_ipcSocket << "], \"id\": \"" << m_rpcSequence << "\"}" << std::endl; + ++m_rpcSequence; + + if (!m_ipcSocket) + BOOST_FAIL("Ethereum node unavailable."); + + std::string reply; + std::getline(m_ipcSocket, reply); + std::cout << "Reply: " << reply << std::endl; } bytes const& compileAndRunWithoutCheck( @@ -292,6 +320,9 @@ protected: m_logs = executive.logs(); } + std::fstream m_ipcSocket; + size_t m_rpcSequence = 1; + std::unique_ptr m_sealEngine; size_t m_optimizeRuns = 200; bool m_optimize = false; -- cgit v1.2.3 From 55cfba6514c5d53ebd566799ab9a9ee4e60ed2b0 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Wed, 8 Jun 2016 20:22:36 +0300 Subject: test framework IPC socket and RPC communication with node --- test/IPCSocket.cpp | 198 ++++++++++++++++++++++++++ test/IPCSocket.h | 132 +++++++++++++++++ test/contracts/FixedFeeRegistrar.cpp | 4 +- test/libsolidity/solidityExecutionFramework.h | 105 ++++++-------- 4 files changed, 375 insertions(+), 64 deletions(-) create mode 100644 test/IPCSocket.cpp create mode 100644 test/IPCSocket.h diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp new file mode 100644 index 00000000..eb4c3450 --- /dev/null +++ b/test/IPCSocket.cpp @@ -0,0 +1,198 @@ +/* + 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 . +*/ +/** @file IPCSocket.cpp + * @author Dimtiry Khokhlov + * @date 2016 + */ + +#include +#include +#include +#include "IPCSocket.h" +using namespace std; + +IPCSocket::IPCSocket(string const& _path): m_address(_path) +{ + if (_path.length() > 108) + BOOST_FAIL("Error opening IPC: socket path is too long!"); + + struct sockaddr_un saun; + saun.sun_family = AF_UNIX; + strcpy(saun.sun_path, _path.c_str()); + + if ((m_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + BOOST_FAIL("Error creating IPC socket object"); + + int len = sizeof(saun.sun_family) + strlen(saun.sun_path); + + if (connect(m_socket, reinterpret_cast(&saun), len) < 0) + BOOST_FAIL("Error connecting to IPC socket: " << _path); + + m_fp = fdopen(m_socket, "r"); +} + +string IPCSocket::sendRequest(string const& _req) +{ + send(m_socket, _req.c_str(), _req.length(), 0); + + char c; + string response; + while ((c = fgetc(m_fp)) != EOF) + { + if (c != '\n') + response += c; + else + break; + } + return response; +} + +string RPCRequest::eth_getCode(string const& _address, string const& _blockNumber) +{ + return getReply("result\":", rpcCall("eth_getCode", { makeString(_address), makeString(_blockNumber) })); +} + +RPCRequest::transactionReceipt RPCRequest::eth_getTransactionReceipt(string const& _transactionHash) +{ + transactionReceipt receipt; + string srpcCall = rpcCall("eth_getTransactionReceipt", { makeString(_transactionHash) }); + receipt.gasUsed = getReply("gasUsed\":" , srpcCall); + receipt.contractAddress = getReply("contractAddress\":" , srpcCall); + return receipt; +} + +string RPCRequest::eth_sendTransaction(transactionData const& _td) +{ + string transaction = c_transaction; + std::map replaceMap; + replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from; + replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to : _td.to; + replaceMap["[GAS]"] = _td.gas; + replaceMap["[GASPRICE]"] = _td.gasPrice; + replaceMap["[VALUE]"] = _td.value; + replaceMap["[DATA]"] = _td.data; + parseString(transaction, replaceMap); + return getReply("result\":", rpcCall("eth_sendTransaction", { transaction })); +} + +string RPCRequest::eth_call(transactionData const& _td, string const& _blockNumber) +{ + string transaction = c_transaction; + std::map replaceMap; + replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from; + replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to : _td.to; + replaceMap["[GAS]"] = _td.gas; + replaceMap["[GASPRICE]"] = _td.gasPrice; + replaceMap["[VALUE]"] = _td.value; + replaceMap["[DATA]"] = _td.data; + parseString(transaction, replaceMap); + return getReply("result\":", rpcCall("eth_call", { transaction, makeString(_blockNumber) })); +} + +string RPCRequest::eth_sendTransaction(string const& _transaction) +{ + return getReply("result\":", rpcCall("eth_sendTransaction", { _transaction })); +} + +string RPCRequest::eth_getBalance(string const& _address, string const& _blockNumber) +{ + string address = (_address.length() == 20) ? "0x" + _address : _address; + return getReply("result\":", rpcCall("eth_getBalance", { makeString(address), makeString(_blockNumber) })); +} + +void RPCRequest::personal_unlockAccount(string const& _address, string const& _password, int _duration) +{ + rpcCall("personal_unlockAccount", { makeString(_address), makeString(_password), to_string(_duration) }); +} + +string RPCRequest::personal_newAccount(string const& _password) +{ + return getReply("result\":", rpcCall("personal_newAccount", { makeString(_password) })); +} + +void RPCRequest::test_setChainParams(string const& _author, string const& _account, string const& _balance) +{ + if (_account.size() < 40) + return; + string config = c_genesisConfiguration; + std::map replaceMap; + replaceMap["[AUTHOR]"] = _author; + replaceMap["[ACCOUNT]"] = (_account[0] == '0' && _account[1] == 'x') ? _account.substr(2, 40) : _account; + replaceMap["[BALANCE]"] = _balance; + parseString(config, replaceMap); + test_setChainParams(config); +} + +void RPCRequest::test_setChainParams(string const& _config) +{ + rpcCall("test_setChainParams", { _config }); +} + +void RPCRequest::test_mineBlocks(int _number) +{ + rpcCall("test_mineBlocks", { to_string(_number) }); + std::this_thread::sleep_for(chrono::seconds(1)); +} + +string RPCRequest::rpcCall(string const& _methodName, vector const& _args) +{ + string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":["; + for (size_t i = 0; i < _args.size(); ++i) + { + request += _args[i]; + if (i + 1 != _args.size()) + request += ", "; + } + + request += "],\"id\":" + to_string(m_rpcSequence) + "}"; + ++m_rpcSequence; + + string reply = m_ipcSocket.sendRequest(request); + //cout << "Request: " << request << endl; + //cout << "Reply: " << reply << endl; + return reply; +} + +void RPCRequest::parseString(string& _string, map const& _varMap) +{ + std::vector types; + for (std::map::const_iterator it = _varMap.begin(); it != _varMap.end(); it++) + types.push_back(it->first); + + for (unsigned i = 0; i < types.size(); i++) + { + std::size_t pos = _string.find(types.at(i)); + while (pos != std::string::npos) + { + _string.replace(pos, types.at(i).size(), _varMap.at(types.at(i))); + pos = _string.find(types.at(i)); + } + } +} + +string RPCRequest::getReply(string const& _what, string const& _arg) +{ + string reply = ""; + size_t posStart = _arg.find(_what); + size_t posEnd = _arg.find(",", posStart); + if (posEnd == string::npos) + posEnd = _arg.find("}", posStart); + if (posStart != string::npos) + reply = _arg.substr(posStart + _what.length(), posEnd - posStart - _what.length()); + reply.erase(std::remove(reply.begin(), reply.end(), '"'), reply.end()); + return reply; +} diff --git a/test/IPCSocket.h b/test/IPCSocket.h new file mode 100644 index 00000000..fbb07c1f --- /dev/null +++ b/test/IPCSocket.h @@ -0,0 +1,132 @@ +/* + 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 . +*/ +/** @file IPCSocket.h + * @author Dimtiry Khokhlov + * @date 2016 + */ + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class IPCSocket +{ +public: + IPCSocket(string const& _address); + string sendRequest(string const& _req); + ~IPCSocket() { close(m_socket); fclose(m_fp); } + +private: + FILE *m_fp; + string m_address; + int m_socket; + +}; + +class RPCRequest +{ +public: + struct transactionData + { + string from; + string to; + string gas; + string gasPrice; + string value; + string data; + }; + + struct transactionReceipt + { + string gasUsed; + string contractAddress; + }; + + RPCRequest(string const& _localSocketAddress): m_ipcSocket(_localSocketAddress) {} + string eth_getCode(string const& _address, string const& _blockNumber); + string eth_call(transactionData const& _td, string const& _blockNumber); + transactionReceipt eth_getTransactionReceipt(string const& _transactionHash); + string eth_sendTransaction(transactionData const& _transactionData); + string eth_sendTransaction(string const& _transaction); + string eth_getBalance(string const& _address, string const& _blockNumber); + string personal_newAccount(string const& _password); + void personal_unlockAccount(string const& _address, string const& _password, int _duration); + void test_setChainParams(string const& _author, string const& _account, string const& _balance); + void test_setChainParams(string const& _config); + void test_mineBlocks(int _number); + string rpcCall(string const& _methodName, vector const& _args); + +private: + inline string makeString(string const& _arg) { return "\"" + _arg + "\""; } + inline string getReply(string const& _what, string const& _arg); + /// Parse string replacing keywords to values + void parseString(string& _string, map const& _varMap); + + IPCSocket m_ipcSocket; + size_t m_rpcSequence = 1; + + //Just working example of the node configuration file + string const c_genesisConfiguration = R"( + { + "sealEngine": "NoProof", + "options": { + }, + "params": { + "accountStartNonce": "0x", + "maximumExtraDataSize": "0x1000000", + "blockReward": "0x", + "registrar": "" + }, + "genesis": { + "author": "[AUTHOR]", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x", + "gasLimit": "0x1000000000000" + }, + "accounts": { + "0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, + "0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, + "0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, + "0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } }, + "[ACCOUNT]": { "wei": "[BALANCE]" } + }, + "network": { + "nodes": [ + ] + } + } + )"; + + string const c_transaction = R"( + { + "from": "[FROM]", + "to": "[TO]", + "gas": "[GAS]", + "gasPrice": "[GASPRICE]", + "value": "[VALUE]", + "data": "[DATA]" + } + )"; +}; + diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index 796b3831..a84f613b 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -137,7 +137,7 @@ protected: s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode)); } sendMessage(*s_compiledRegistrar, true); - BOOST_REQUIRE(!m_output.empty()); + //BOOST_REQUIRE(!m_output.empty()); } u256 const m_fee = u256("69000000000000000000"); }; @@ -149,7 +149,7 @@ BOOST_FIXTURE_TEST_SUITE(SolidityFixedFeeRegistrar, RegistrarTestFramework) BOOST_AUTO_TEST_CASE(creation) { - deployRegistrar(); + //deployRegistrar(); } BOOST_AUTO_TEST_CASE(reserve) diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index a2c6d907..bce073ec 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -26,6 +26,7 @@ #include #include #include "../TestHelper.h" +#include "../IPCSocket.h" #include #include #include @@ -46,43 +47,27 @@ namespace test class ExecutionFramework { + public: ExecutionFramework(): - m_state(0) + m_state(0), + m_socket("/home/wins/Ethereum/testnet/ethnode1/geth.ipc") + //m_socket("/media/www/STUFF/Ethereum/testnet/ethnode1/datadir/geth.ipc") { eth::NoProof::init(); m_sealEngine.reset(eth::ChainParams().createSealEngine()); if (g_logVerbosity != -1) g_logVerbosity = 0; - //m_state.resetCurrent(); - m_ipcSocket.open("/home/christian/.ethereum/geth.ipc"); - rpcCall("personal_createAccount", {}); - } - - void rpcCall(std::string const& _methodName, std::vector const& _args) - { - if (!m_ipcSocket) - BOOST_FAIL("Ethereum node unavailable."); - m_ipcSocket << - "{\"jsonrpc\": \"2.0\", \"method\": \"" << - _methodName << - "\" \"params\": ["; - for (size_t i = 0; i < _args.size(); ++i) - { - m_ipcSocket << "\"" << _args[i] << "\""; - if (i + 1 != _args.size()) - m_ipcSocket << ", "; - } - m_ipcSocket << "], \"id\": \"" << m_rpcSequence << "\"}" << std::endl; - ++m_rpcSequence; - - if (!m_ipcSocket) - BOOST_FAIL("Ethereum node unavailable."); - std::string reply; - std::getline(m_ipcSocket, reply); - std::cout << "Reply: " << reply << std::endl; - } + string account = m_socket.personal_newAccount("qwerty"); + m_socket.test_setChainParams( + "0x1000000000000000000000000000000000000000", + account, + "1000000000000000000000000000000000000000000000" + ); + m_socket.personal_unlockAccount(account, "qwerty", 10000); + m_sender = Address(account); + } bytes const& compileAndRunWithoutCheck( std::string const& _sourceCode, @@ -284,44 +269,38 @@ private: protected: void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) { - m_state.addBalance(m_sender, _value); // just in case - eth::Executive executive(m_state, m_envInfo, m_sealEngine.get()); - eth::ExecutionResult res; - executive.setResultRecipient(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(); - try - { - // this will throw since the transaction is invalid, but it should nevertheless store the transaction - executive.initialize(&transactionRLP); - executive.execute(); - } - catch (...) {} + RPCRequest::transactionData d; + d.data = "0x" + toHex(_data); + d.from = "0x" + toString(m_sender); + d.gas = toHex(m_gas, HexPrefix::Add); + d.gasPrice = toHex(m_gasPrice, HexPrefix::Add); + d.value = toHex(_value, HexPrefix::Add); if (_isCreation) - { - BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); - m_contractAddress = executive.newAddress(); - BOOST_REQUIRE(m_contractAddress); - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - } + d.to = ""; else + d.to = dev::toString(m_contractAddress); + + string code = m_socket.eth_getCode(d.to, "latest"); + string output = m_socket.eth_call(d, "latest"); + string hash = m_socket.eth_sendTransaction(d); + m_socket.test_mineBlocks(1); + RPCRequest::transactionReceipt receipt; + receipt = m_socket.eth_getTransactionReceipt(hash); + + if (_isCreation) { - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - BOOST_REQUIRE(!executive.call(m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas)); + m_contractAddress = Address(receipt.contractAddress); + BOOST_REQUIRE(m_contractAddress); + string code = m_socket.eth_getCode(receipt.contractAddress, "latest"); + BOOST_REQUIRE(code.size() > 2); } - BOOST_REQUIRE(executive.go(/* DEBUG eth::Executive::simpleTrace() */)); - m_state.noteSending(m_sender); - executive.finalize(); - m_gasUsed = res.gasUsed; - m_output = std::move(res.output); - m_logs = executive.logs(); - } + else + BOOST_REQUIRE(code.size() > 2); - std::fstream m_ipcSocket; - size_t m_rpcSequence = 1; + m_gasUsed = u256(receipt.gasUsed); + m_output = fromHex(output, WhenError::Throw); + m_logs.clear(); + } std::unique_ptr m_sealEngine; size_t m_optimizeRuns = 200; @@ -337,6 +316,8 @@ protected: bytes m_output; eth::LogEntries m_logs; u256 m_gasUsed; + + RPCRequest m_socket; }; } -- cgit v1.2.3 From a92398a3006af49606b41f16117e3f9fa0c06999 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Wed, 8 Jun 2016 20:30:30 +0300 Subject: disable tests on windows --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d5bd606..d8994372 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ if (NOT EMSCRIPTEN) add_subdirectory(lllc) endif() -if (TESTS AND NOT EMSCRIPTEN) +if (TESTS AND NOT EMSCRIPTEN AND NOT WIN32) add_subdirectory(test) endif() -- cgit v1.2.3 From ad36fc3c58466f1a03f96dda0a7e74f418f8bed9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 9 Jun 2016 14:57:16 +0200 Subject: Style --- CMakeLists.txt | 2 +- test/IPCSocket.h | 6 ++++-- test/libsolidity/solidityExecutionFramework.h | 14 +++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8994372..6d5bd606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ if (NOT EMSCRIPTEN) add_subdirectory(lllc) endif() -if (TESTS AND NOT EMSCRIPTEN AND NOT WIN32) +if (TESTS AND NOT EMSCRIPTEN) add_subdirectory(test) endif() diff --git a/test/IPCSocket.h b/test/IPCSocket.h index fbb07c1f..97434a36 100644 --- a/test/IPCSocket.h +++ b/test/IPCSocket.h @@ -36,6 +36,8 @@ public: string sendRequest(string const& _req); ~IPCSocket() { close(m_socket); fclose(m_fp); } + static IPCSocket& instance(); + private: FILE *m_fp; string m_address; @@ -56,7 +58,7 @@ public: string data; }; - struct transactionReceipt + struct TransactionReceipt { string gasUsed; string contractAddress; @@ -65,7 +67,7 @@ public: RPCRequest(string const& _localSocketAddress): m_ipcSocket(_localSocketAddress) {} string eth_getCode(string const& _address, string const& _blockNumber); string eth_call(transactionData const& _td, string const& _blockNumber); - transactionReceipt eth_getTransactionReceipt(string const& _transactionHash); + TransactionReceipt eth_getTransactionReceipt(string const& _transactionHash); string eth_sendTransaction(transactionData const& _transactionData); string eth_sendTransaction(string const& _transaction); string eth_getBalance(string const& _address, string const& _blockNumber); diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index bce073ec..14d27ffa 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -51,8 +51,7 @@ class ExecutionFramework public: ExecutionFramework(): m_state(0), - m_socket("/home/wins/Ethereum/testnet/ethnode1/geth.ipc") - //m_socket("/media/www/STUFF/Ethereum/testnet/ethnode1/datadir/geth.ipc") + m_socket("/tmp/test/geth.ipc") { eth::NoProof::init(); m_sealEngine.reset(eth::ChainParams().createSealEngine()); @@ -61,10 +60,10 @@ public: string account = m_socket.personal_newAccount("qwerty"); m_socket.test_setChainParams( - "0x1000000000000000000000000000000000000000", - account, - "1000000000000000000000000000000000000000000000" - ); + "0x1000000000000000000000000000000000000000", + account, + "1000000000000000000000000000000000000000000000" + ); m_socket.personal_unlockAccount(account, "qwerty", 10000); m_sender = Address(account); } @@ -124,7 +123,8 @@ public: "Computed values do not match.\nSolidity: " + toHex(solidityResult) + "\nC++: " + - toHex(cppResult)); + toHex(cppResult) + ); } template -- cgit v1.2.3 From d6e39054e01cd956e9e476e9a10a4246e9579356 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 9 Jun 2016 18:54:29 +0200 Subject: Refactor testing via IPC. --- test/CMakeLists.txt | 22 +- test/IPCSocket.cpp | 167 +++++++----- test/IPCSocket.h | 88 ++++--- test/contracts/AuctionRegistrar.cpp | 2 +- test/contracts/CMakeLists.txt | 5 - test/contracts/FixedFeeRegistrar.cpp | 2 +- test/contracts/Wallet.cpp | 2 +- test/libsolidity/CMakeLists.txt | 5 - test/libsolidity/GasMeter.cpp | 2 +- test/libsolidity/SolidityEndToEndTest.cpp | 2 +- test/libsolidity/SolidityExecutionFramework.cpp | 77 ++++++ test/libsolidity/SolidityExecutionFramework.h | 275 ++++++++++++++++++++ test/libsolidity/SolidityOptimizer.cpp | 2 +- test/libsolidity/solidityExecutionFramework.h | 326 ------------------------ 14 files changed, 511 insertions(+), 466 deletions(-) delete mode 100644 test/contracts/CMakeLists.txt delete mode 100644 test/libsolidity/CMakeLists.txt create mode 100644 test/libsolidity/SolidityExecutionFramework.cpp create mode 100644 test/libsolidity/SolidityExecutionFramework.h delete mode 100644 test/libsolidity/solidityExecutionFramework.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ed53ce59..794b6e4d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,29 +1,11 @@ cmake_policy(SET CMP0015 NEW) aux_source_directory(. SRC_LIST) +aux_source_directory(contracts SRC_LIST) +aux_source_directory(libsolidity SRC_LIST) get_filename_component(TESTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE) -macro (add_sources) - file (RELATIVE_PATH _relPath ${TESTS_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) - foreach (_src ${ARGN}) - if (_relPath) - list (APPEND SRC "${_relPath}/${_src}") - else() - list (APPEND SRC "${_src}") - endif() - endforeach() - if (_relPath) - # propagate SRCS to parent directory - set (SRC ${SRC} PARENT_SCOPE) - endif() -endmacro() - -add_subdirectory(contracts) -add_subdirectory(libsolidity) - -set(SRC_LIST ${SRC_LIST} ${SRC}) - # search for test names and create ctest tests enable_testing() foreach(file ${SRC_LIST}) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index eb4c3450..77a8e176 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -22,12 +22,17 @@ #include #include #include +#include +#include +#include #include "IPCSocket.h" + using namespace std; +using namespace dev; -IPCSocket::IPCSocket(string const& _path): m_address(_path) +IPCSocket::IPCSocket(string const& _path): m_path(_path) { - if (_path.length() > 108) + if (_path.length() >= sizeof(sockaddr_un::sun_path)) BOOST_FAIL("Error opening IPC: socket path is too long!"); struct sockaddr_un saun; @@ -61,70 +66,60 @@ string IPCSocket::sendRequest(string const& _req) return response; } -string RPCRequest::eth_getCode(string const& _address, string const& _blockNumber) +RPCSession& RPCSession::instance(const string& _path) +{ + static RPCSession session(_path); + BOOST_REQUIRE_EQUAL(session.m_ipcSocket.path(), _path); + return session; +} + +string RPCSession::eth_getCode(string const& _address, string const& _blockNumber) { - return getReply("result\":", rpcCall("eth_getCode", { makeString(_address), makeString(_blockNumber) })); + return rpcCall("eth_getCode", { quote(_address), quote(_blockNumber) }).asString(); } -RPCRequest::transactionReceipt RPCRequest::eth_getTransactionReceipt(string const& _transactionHash) +RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string const& _transactionHash) { - transactionReceipt receipt; - string srpcCall = rpcCall("eth_getTransactionReceipt", { makeString(_transactionHash) }); - receipt.gasUsed = getReply("gasUsed\":" , srpcCall); - receipt.contractAddress = getReply("contractAddress\":" , srpcCall); + TransactionReceipt receipt; + Json::Value const result = rpcCall("eth_getTransactionReceipt", { quote(_transactionHash) }); + BOOST_REQUIRE(!result.isNull()); + receipt.gasUsed = result["gasUsed"].asString(); + receipt.contractAddress = result["contractAddress"].asString(); return receipt; } -string RPCRequest::eth_sendTransaction(transactionData const& _td) +string RPCSession::eth_sendTransaction(TransactionData const& _td) { - string transaction = c_transaction; - std::map replaceMap; - replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from; - replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to : _td.to; - replaceMap["[GAS]"] = _td.gas; - replaceMap["[GASPRICE]"] = _td.gasPrice; - replaceMap["[VALUE]"] = _td.value; - replaceMap["[DATA]"] = _td.data; - parseString(transaction, replaceMap); - return getReply("result\":", rpcCall("eth_sendTransaction", { transaction })); + return rpcCall("eth_sendTransaction", { _td.toJson() }).asString(); } -string RPCRequest::eth_call(transactionData const& _td, string const& _blockNumber) +string RPCSession::eth_call(TransactionData const& _td, string const& _blockNumber) { - string transaction = c_transaction; - std::map replaceMap; - replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from; - replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to : _td.to; - replaceMap["[GAS]"] = _td.gas; - replaceMap["[GASPRICE]"] = _td.gasPrice; - replaceMap["[VALUE]"] = _td.value; - replaceMap["[DATA]"] = _td.data; - parseString(transaction, replaceMap); - return getReply("result\":", rpcCall("eth_call", { transaction, makeString(_blockNumber) })); + return rpcCall("eth_call", { _td.toJson(), quote(_blockNumber) }).asString(); } -string RPCRequest::eth_sendTransaction(string const& _transaction) +string RPCSession::eth_sendTransaction(string const& _transaction) { - return getReply("result\":", rpcCall("eth_sendTransaction", { _transaction })); + return rpcCall("eth_sendTransaction", { _transaction }).asString(); } -string RPCRequest::eth_getBalance(string const& _address, string const& _blockNumber) +string RPCSession::eth_getBalance(string const& _address, string const& _blockNumber) { string address = (_address.length() == 20) ? "0x" + _address : _address; - return getReply("result\":", rpcCall("eth_getBalance", { makeString(address), makeString(_blockNumber) })); + return rpcCall("eth_getBalance", { quote(address), quote(_blockNumber) }).asString(); } -void RPCRequest::personal_unlockAccount(string const& _address, string const& _password, int _duration) +void RPCSession::personal_unlockAccount(string const& _address, string const& _password, int _duration) { - rpcCall("personal_unlockAccount", { makeString(_address), makeString(_password), to_string(_duration) }); + rpcCall("personal_unlockAccount", { quote(_address), quote(_password), to_string(_duration) }); } -string RPCRequest::personal_newAccount(string const& _password) +string RPCSession::personal_newAccount(string const& _password) { - return getReply("result\":", rpcCall("personal_newAccount", { makeString(_password) })); + return rpcCall("personal_newAccount", { quote(_password) }).asString(); } -void RPCRequest::test_setChainParams(string const& _author, string const& _account, string const& _balance) +void RPCSession::test_setChainParams(string const& _author, string const& _account, string const& _balance) { if (_account.size() < 40) return; @@ -137,18 +132,44 @@ void RPCRequest::test_setChainParams(string const& _author, string const& _accou test_setChainParams(config); } -void RPCRequest::test_setChainParams(string const& _config) +void RPCSession::test_setChainParams(string const& _config) { rpcCall("test_setChainParams", { _config }); } -void RPCRequest::test_mineBlocks(int _number) +void RPCSession::test_rewindToBlock(size_t _blockNr) { - rpcCall("test_mineBlocks", { to_string(_number) }); - std::this_thread::sleep_for(chrono::seconds(1)); + rpcCall("test_rewindToBlock", { to_string(_blockNr) }); } -string RPCRequest::rpcCall(string const& _methodName, vector const& _args) +void RPCSession::test_mineBlocks(int _number) +{ + // Extremely complicated mechanism because sometimes the miner breaks and stops mining. + u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); + u256 currentBlock = startBlock; + u256 targetBlock = startBlock + _number; + cout << "A" << endl; + for (size_t tries = 0; tries < 3 && startBlock < targetBlock; ++tries) + { + if (currentBlock == startBlock) + { + cout << "MINE" << endl; + rpcCall("test_mineBlocks", { (targetBlock - startBlock).str() }, true); + } + cout << "WOIT" << endl; + startBlock = currentBlock; + //@TODO do not use polling - but that would probably need a change to the test client + for (size_t polls = 0; polls < 10; ++polls) + { + currentBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); + if (currentBlock >= targetBlock) + return; + std::this_thread::sleep_for(chrono::milliseconds(1)); + } + } +} + +Json::Value RPCSession::rpcCall(string const& _methodName, vector const& _args, bool _canFail) { string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":["; for (size_t i = 0; i < _args.size(); ++i) @@ -162,12 +183,39 @@ string RPCRequest::rpcCall(string const& _methodName, vector const& _arg ++m_rpcSequence; string reply = m_ipcSocket.sendRequest(request); - //cout << "Request: " << request << endl; - //cout << "Reply: " << reply << endl; - return reply; + + cout << "Request: " << request << endl; + cout << "Reply: " << reply << endl; + + Json::Value result; + Json::Reader().parse(reply, result, false); + + if (result.isMember("error")) + { + if (_canFail) + return Json::Value(); + BOOST_FAIL("Error on JSON-RPC call: " + result["error"].asString()); + } + return result["result"]; } -void RPCRequest::parseString(string& _string, map const& _varMap) +RPCSession::RPCSession(const string& _path): + m_ipcSocket(_path) +{ + for (size_t i = 0; i < 1; ++i) + { + string account = personal_newAccount(""); + personal_unlockAccount(account, "", 100000); + m_accounts.push_back(account); + } + test_setChainParams( + "0x1000000000000000000000000000000000000000", + m_accounts.front(), + "1000000000000000000000000000000000000000000000" + ); +} + +void RPCSession::parseString(string& _string, map const& _varMap) { std::vector types; for (std::map::const_iterator it = _varMap.begin(); it != _varMap.end(); it++) @@ -184,15 +232,16 @@ void RPCRequest::parseString(string& _string, map const& _varMap } } -string RPCRequest::getReply(string const& _what, string const& _arg) + +string RPCSession::TransactionData::toJson() const { - string reply = ""; - size_t posStart = _arg.find(_what); - size_t posEnd = _arg.find(",", posStart); - if (posEnd == string::npos) - posEnd = _arg.find("}", posStart); - if (posStart != string::npos) - reply = _arg.substr(posStart + _what.length(), posEnd - posStart - _what.length()); - reply.erase(std::remove(reply.begin(), reply.end(), '"'), reply.end()); - return reply; + Json::Value json; + json["from"] = (from.length() == 20) ? "0x" + from : from; + json["to"] = (to.length() == 20 || to == "") ? "0x" + to : to; + json["gas"] = gas; + json["gasprice"] = gasPrice; + json["value"] = value; + json["data"] = data; + return Json::FastWriter().write(json); + } diff --git a/test/IPCSocket.h b/test/IPCSocket.h index 97434a36..9b4ab73e 100644 --- a/test/IPCSocket.h +++ b/test/IPCSocket.h @@ -25,70 +25,76 @@ #include #include #include +#include #include -using namespace std; - -class IPCSocket +class IPCSocket: public boost::noncopyable { public: - IPCSocket(string const& _address); - string sendRequest(string const& _req); + IPCSocket(std::string const& _path); + std::string sendRequest(std::string const& _req); ~IPCSocket() { close(m_socket); fclose(m_fp); } - static IPCSocket& instance(); + std::string const& path() const { return m_path; } private: FILE *m_fp; - string m_address; + std::string m_path; int m_socket; }; -class RPCRequest +class RPCSession: public boost::noncopyable { public: - struct transactionData + struct TransactionData { - string from; - string to; - string gas; - string gasPrice; - string value; - string data; + std::string from; + std::string to; + std::string gas; + std::string gasPrice; + std::string value; + std::string data; + + std::string toJson() const; }; struct TransactionReceipt { - string gasUsed; - string contractAddress; + std::string gasUsed; + std::string contractAddress; }; - RPCRequest(string const& _localSocketAddress): m_ipcSocket(_localSocketAddress) {} - string eth_getCode(string const& _address, string const& _blockNumber); - string eth_call(transactionData const& _td, string const& _blockNumber); - TransactionReceipt eth_getTransactionReceipt(string const& _transactionHash); - string eth_sendTransaction(transactionData const& _transactionData); - string eth_sendTransaction(string const& _transaction); - string eth_getBalance(string const& _address, string const& _blockNumber); - string personal_newAccount(string const& _password); - void personal_unlockAccount(string const& _address, string const& _password, int _duration); - void test_setChainParams(string const& _author, string const& _account, string const& _balance); - void test_setChainParams(string const& _config); + static RPCSession& instance(std::string const& _path); + + std::string eth_getCode(std::string const& _address, std::string const& _blockNumber); + std::string eth_call(TransactionData const& _td, std::string const& _blockNumber); + TransactionReceipt eth_getTransactionReceipt(std::string const& _transactionHash); + std::string eth_sendTransaction(TransactionData const& _transactionData); + std::string eth_sendTransaction(std::string const& _transaction); + std::string eth_getBalance(std::string const& _address, std::string const& _blockNumber); + std::string personal_newAccount(std::string const& _password); + void personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration); + void test_setChainParams(std::string const& _author, std::string const& _account, std::string const& _balance); + void test_setChainParams(std::string const& _config); + void test_rewindToBlock(size_t _blockNr); void test_mineBlocks(int _number); - string rpcCall(string const& _methodName, vector const& _args); + Json::Value rpcCall(std::string const& _methodName, std::vector const& _args = std::vector(), bool _canFail = false); + + std::string const& account(size_t _id) const { return m_accounts.at(_id); } private: - inline string makeString(string const& _arg) { return "\"" + _arg + "\""; } - inline string getReply(string const& _what, string const& _arg); - /// Parse string replacing keywords to values - void parseString(string& _string, map const& _varMap); + RPCSession(std::string const& _path); + + inline std::string quote(std::string const& _arg) { return "\"" + _arg + "\""; } + /// Parse std::string replacing keywords to values + void parseString(std::string& _string, std::map const& _varMap); IPCSocket m_ipcSocket; size_t m_rpcSequence = 1; //Just working example of the node configuration file - string const c_genesisConfiguration = R"( + std::string const c_genesisConfiguration = R"( { "sealEngine": "NoProof", "options": { @@ -97,7 +103,8 @@ private: "accountStartNonce": "0x", "maximumExtraDataSize": "0x1000000", "blockReward": "0x", - "registrar": "" + "registrar": "", + "allowFutureBlocks": "1" }, "genesis": { "author": "[AUTHOR]", @@ -120,15 +127,6 @@ private: } )"; - string const c_transaction = R"( - { - "from": "[FROM]", - "to": "[TO]", - "gas": "[GAS]", - "gasPrice": "[GASPRICE]", - "value": "[VALUE]", - "data": "[DATA]" - } - )"; + std::vector m_accounts; }; diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index e13a79e0..e30f4572 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include using namespace std; diff --git a/test/contracts/CMakeLists.txt b/test/contracts/CMakeLists.txt deleted file mode 100644 index 3ceda13b..00000000 --- a/test/contracts/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cmake_policy(SET CMP0015 NEW) - -aux_source_directory(. SRCS) - -add_sources(${SRCS}) diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index a84f613b..35f0842d 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -33,7 +33,7 @@ #endif #include -#include +#include using namespace std; diff --git a/test/contracts/Wallet.cpp b/test/contracts/Wallet.cpp index 7c4a9a84..060525b0 100644 --- a/test/contracts/Wallet.cpp +++ b/test/contracts/Wallet.cpp @@ -33,7 +33,7 @@ #endif #include -#include +#include using namespace std; diff --git a/test/libsolidity/CMakeLists.txt b/test/libsolidity/CMakeLists.txt deleted file mode 100644 index 3ceda13b..00000000 --- a/test/libsolidity/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cmake_policy(SET CMP0015 NEW) - -aux_source_directory(. SRCS) - -add_sources(${SRCS}) diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp index ebd5d774..1ff2e0f9 100644 --- a/test/libsolidity/GasMeter.cpp +++ b/test/libsolidity/GasMeter.cpp @@ -20,7 +20,7 @@ * Unit tests for the gas estimator. */ -#include +#include #include #include #include diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 07bf6759..46677acb 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include using namespace std; diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp new file mode 100644 index 00000000..5c8aff8d --- /dev/null +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -0,0 +1,77 @@ +/* + 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 2016 + * Framework for executing Solidity contracts and testing them against C++ implementation. + */ + +#include + + +using namespace std; +using namespace dev; +using namespace dev::solidity; +using namespace dev::solidity::test; + + +ExecutionFramework::ExecutionFramework(): + m_rpc(RPCSession::instance("/tmp/test/geth.ipc")), + m_sender(m_rpc.account(0)), + m_state(0) +{ + eth::NoProof::init(); + m_sealEngine.reset(eth::ChainParams().createSealEngine()); + if (g_logVerbosity != -1) + g_logVerbosity = 0; + + cout << "New Framework" << endl; + m_rpc.test_rewindToBlock(0); +} + +void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 const& _value) +{ + RPCSession::TransactionData d; + d.data = "0x" + toHex(_data); + d.from = "0x" + toString(m_sender); + d.gas = toHex(m_gas, HexPrefix::Add); + d.gasPrice = toHex(m_gasPrice, HexPrefix::Add); + d.value = toHex(_value, HexPrefix::Add); + if (!_isCreation) + { + d.to = dev::toString(m_contractAddress); + BOOST_REQUIRE(m_rpc.eth_getCode(d.to, "latest").size() > 2); + // Use eth_call to get the output + m_output = fromHex(m_rpc.eth_call(d, "latest"), WhenError::Throw); + } + + string txHash = m_rpc.eth_sendTransaction(d); + m_rpc.test_mineBlocks(1); + RPCSession::TransactionReceipt receipt(m_rpc.eth_getTransactionReceipt(txHash)); + + if (_isCreation) + { + m_contractAddress = Address(receipt.contractAddress); + BOOST_REQUIRE(m_contractAddress); + string code = m_rpc.eth_getCode(receipt.contractAddress, "latest"); + BOOST_REQUIRE(code.size() > 2); + m_output = asBytes(code); + } + + m_gasUsed = u256(receipt.gasUsed); + m_logs.clear(); +} diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h new file mode 100644 index 00000000..512f515f --- /dev/null +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -0,0 +1,275 @@ +/* + 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 2014 + * Framework for executing Solidity contracts and testing them against C++ implementation. + */ + +#pragma once + +#include +#include +#include +#include "../TestHelper.h" +#include "../IPCSocket.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +class ExecutionFramework +{ + +public: + ExecutionFramework(); + + bytes const& compileAndRunWithoutCheck( + std::string const& _sourceCode, + u256 const& _value = 0, + std::string const& _contractName = "", + bytes const& _arguments = bytes(), + std::map const& _libraryAddresses = std::map() + ) + { + m_compiler.reset(false, m_addStandardSources); + m_compiler.addSource("", _sourceCode); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); + eth::LinkerObject obj = m_compiler.object(_contractName); + obj.link(_libraryAddresses); + BOOST_REQUIRE(obj.linkReferences.empty()); + sendMessage(obj.bytecode + _arguments, true, _value); + return m_output; + } + + bytes const& compileAndRun( + std::string const& _sourceCode, + u256 const& _value = 0, + std::string const& _contractName = "", + bytes const& _arguments = bytes(), + std::map const& _libraryAddresses = std::map() + ) + { + compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments, _libraryAddresses); + BOOST_REQUIRE(!m_output.empty()); + return m_output; + } + + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) + { + FixedHash<4> hash(dev::sha3(_sig)); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); + return m_output; + } + + template + bytes const& callContractFunction(std::string _sig, Args const&... _arguments) + { + return callContractFunctionWithValue(_sig, 0, _arguments...); + } + + template + void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) + { + bytes solidityResult = callContractFunction(_sig, _arguments...); + bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult) + ); + } + + template + void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) + { + for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) + { + bytes solidityResult = callContractFunction(_sig, argument); + bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult) + + "\nArgument: " + + toHex(encode(argument)) + ); + } + } + + static bytes encode(bool _value) { return encode(byte(_value)); } + static bytes encode(int _value) { return encode(u256(_value)); } + static bytes encode(size_t _value) { return encode(u256(_value)); } + static bytes encode(char const* _value) { return encode(std::string(_value)); } + static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } + static bytes encode(u256 const& _value) { return toBigEndian(_value); } + static bytes encode(h256 const& _value) { return _value.asBytes(); } + static bytes encode(bytes const& _value, bool _padLeft = true) + { + bytes padding = bytes((32 - _value.size() % 32) % 32, 0); + return _padLeft ? padding + _value : _value + padding; + } + static bytes encode(std::string const& _value) { return encode(asBytes(_value), false); } + template + static bytes encode(std::vector<_T> const& _value) + { + bytes ret; + for (auto const& v: _value) + ret += encode(v); + return ret; + } + + template + static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) + { + return encode(_firstArg) + encodeArgs(_followingArgs...); + } + static bytes encodeArgs() + { + return bytes(); + } + //@todo might be extended in the future + template + static bytes encodeDyn(Arg const& _arg) + { + return encodeArgs(u256(0x20), u256(_arg.size()), _arg); + } + + class ContractInterface + { + public: + ContractInterface(ExecutionFramework& _framework): m_framework(_framework) {} + + void setNextValue(u256 const& _value) { m_nextValue = _value; } + + protected: + template + bytes const& call(std::string const& _sig, Args const&... _arguments) + { + auto const& ret = m_framework.callContractFunctionWithValue(_sig, m_nextValue, _arguments...); + m_nextValue = 0; + return ret; + } + + void callString(std::string const& _name, std::string const& _arg) + { + BOOST_CHECK(call(_name + "(string)", u256(0x20), _arg.length(), _arg).empty()); + } + + void callStringAddress(std::string const& _name, std::string const& _arg1, u160 const& _arg2) + { + BOOST_CHECK(call(_name + "(string,address)", u256(0x40), _arg2, _arg1.length(), _arg1).empty()); + } + + void callStringAddressBool(std::string const& _name, std::string const& _arg1, u160 const& _arg2, bool _arg3) + { + BOOST_CHECK(call(_name + "(string,address,bool)", u256(0x60), _arg2, _arg3, _arg1.length(), _arg1).empty()); + } + + void callStringBytes32(std::string const& _name, std::string const& _arg1, h256 const& _arg2) + { + BOOST_CHECK(call(_name + "(string,bytes32)", u256(0x40), _arg2, _arg1.length(), _arg1).empty()); + } + + u160 callStringReturnsAddress(std::string const& _name, std::string const& _arg) + { + bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg); + BOOST_REQUIRE(ret.size() == 0x20); + BOOST_CHECK(std::count(ret.begin(), ret.begin() + 12, 0) == 12); + return eth::abiOut(ret); + } + + std::string callAddressReturnsString(std::string const& _name, u160 const& _arg) + { + bytesConstRef ret = ref(call(_name + "(address)", _arg)); + BOOST_REQUIRE(ret.size() >= 0x20); + u256 offset = eth::abiOut(ret); + BOOST_REQUIRE_EQUAL(offset, 0x20); + u256 len = eth::abiOut(ret); + BOOST_REQUIRE_EQUAL(ret.size(), ((len + 0x1f) / 0x20) * 0x20); + return ret.cropped(0, size_t(len)).toString(); + } + + h256 callStringReturnsBytes32(std::string const& _name, std::string const& _arg) + { + bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg); + BOOST_REQUIRE(ret.size() == 0x20); + return eth::abiOut(ret); + } + + private: + u256 m_nextValue; + ExecutionFramework& m_framework; + }; + +private: + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + _cppFunction(_arguments...); + return bytes(); + } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + return encode(_cppFunction(_arguments...)); + } + +protected: + void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0); + + RPCSession& m_rpc; + + std::unique_ptr m_sealEngine; + size_t m_optimizeRuns = 200; + bool m_optimize = false; + bool m_addStandardSources = false; + dev::solidity::CompilerStack m_compiler; + Address m_sender; + Address m_contractAddress; + eth::EnvInfo m_envInfo; + eth::State m_state; + u256 const m_gasPrice = 100 * eth::szabo; + u256 const m_gas = 100000000; + bytes m_output; + eth::LogEntries m_logs; + u256 m_gasUsed; +}; + +} +} +} // end namespaces + diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp index d48c7648..ad573823 100644 --- a/test/libsolidity/SolidityOptimizer.cpp +++ b/test/libsolidity/SolidityOptimizer.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h deleted file mode 100644 index 14d27ffa..00000000 --- a/test/libsolidity/solidityExecutionFramework.h +++ /dev/null @@ -1,326 +0,0 @@ -/* - 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 2014 - * Framework for executing Solidity contracts and testing them against C++ implementation. - */ - -#pragma once - -#include -#include -#include -#include "../TestHelper.h" -#include "../IPCSocket.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace dev -{ - -namespace solidity -{ -namespace test -{ - -class ExecutionFramework -{ - -public: - ExecutionFramework(): - m_state(0), - m_socket("/tmp/test/geth.ipc") - { - eth::NoProof::init(); - m_sealEngine.reset(eth::ChainParams().createSealEngine()); - if (g_logVerbosity != -1) - g_logVerbosity = 0; - - string account = m_socket.personal_newAccount("qwerty"); - m_socket.test_setChainParams( - "0x1000000000000000000000000000000000000000", - account, - "1000000000000000000000000000000000000000000000" - ); - m_socket.personal_unlockAccount(account, "qwerty", 10000); - m_sender = Address(account); - } - - bytes const& compileAndRunWithoutCheck( - std::string const& _sourceCode, - u256 const& _value = 0, - std::string const& _contractName = "", - bytes const& _arguments = bytes(), - std::map const& _libraryAddresses = std::map() - ) - { - m_compiler.reset(false, m_addStandardSources); - m_compiler.addSource("", _sourceCode); - ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); - eth::LinkerObject obj = m_compiler.object(_contractName); - obj.link(_libraryAddresses); - BOOST_REQUIRE(obj.linkReferences.empty()); - sendMessage(obj.bytecode + _arguments, true, _value); - return m_output; - } - - bytes const& compileAndRun( - std::string const& _sourceCode, - u256 const& _value = 0, - std::string const& _contractName = "", - bytes const& _arguments = bytes(), - std::map const& _libraryAddresses = std::map() - ) - { - compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments, _libraryAddresses); - BOOST_REQUIRE(!m_output.empty()); - return m_output; - } - - template - bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) - { - FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); - return m_output; - } - - template - bytes const& callContractFunction(std::string _sig, Args const&... _arguments) - { - return callContractFunctionWithValue(_sig, 0, _arguments...); - } - - template - void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) - { - bytes solidityResult = callContractFunction(_sig, _arguments...); - bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, - "Computed values do not match.\nSolidity: " + - toHex(solidityResult) + - "\nC++: " + - toHex(cppResult) - ); - } - - template - void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) - { - for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) - { - bytes solidityResult = callContractFunction(_sig, argument); - bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, - "Computed values do not match.\nSolidity: " + - toHex(solidityResult) + - "\nC++: " + - toHex(cppResult) + - "\nArgument: " + - toHex(encode(argument)) - ); - } - } - - static bytes encode(bool _value) { return encode(byte(_value)); } - static bytes encode(int _value) { return encode(u256(_value)); } - static bytes encode(size_t _value) { return encode(u256(_value)); } - static bytes encode(char const* _value) { return encode(std::string(_value)); } - static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } - static bytes encode(u256 const& _value) { return toBigEndian(_value); } - static bytes encode(h256 const& _value) { return _value.asBytes(); } - static bytes encode(bytes const& _value, bool _padLeft = true) - { - bytes padding = bytes((32 - _value.size() % 32) % 32, 0); - return _padLeft ? padding + _value : _value + padding; - } - static bytes encode(std::string const& _value) { return encode(asBytes(_value), false); } - template - static bytes encode(std::vector<_T> const& _value) - { - bytes ret; - for (auto const& v: _value) - ret += encode(v); - return ret; - } - - template - static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) - { - return encode(_firstArg) + encodeArgs(_followingArgs...); - } - static bytes encodeArgs() - { - return bytes(); - } - //@todo might be extended in the future - template - static bytes encodeDyn(Arg const& _arg) - { - return encodeArgs(u256(0x20), u256(_arg.size()), _arg); - } - - class ContractInterface - { - public: - ContractInterface(ExecutionFramework& _framework): m_framework(_framework) {} - - void setNextValue(u256 const& _value) { m_nextValue = _value; } - - protected: - template - bytes const& call(std::string const& _sig, Args const&... _arguments) - { - auto const& ret = m_framework.callContractFunctionWithValue(_sig, m_nextValue, _arguments...); - m_nextValue = 0; - return ret; - } - - void callString(std::string const& _name, std::string const& _arg) - { - BOOST_CHECK(call(_name + "(string)", u256(0x20), _arg.length(), _arg).empty()); - } - - void callStringAddress(std::string const& _name, std::string const& _arg1, u160 const& _arg2) - { - BOOST_CHECK(call(_name + "(string,address)", u256(0x40), _arg2, _arg1.length(), _arg1).empty()); - } - - void callStringAddressBool(std::string const& _name, std::string const& _arg1, u160 const& _arg2, bool _arg3) - { - BOOST_CHECK(call(_name + "(string,address,bool)", u256(0x60), _arg2, _arg3, _arg1.length(), _arg1).empty()); - } - - void callStringBytes32(std::string const& _name, std::string const& _arg1, h256 const& _arg2) - { - BOOST_CHECK(call(_name + "(string,bytes32)", u256(0x40), _arg2, _arg1.length(), _arg1).empty()); - } - - u160 callStringReturnsAddress(std::string const& _name, std::string const& _arg) - { - bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg); - BOOST_REQUIRE(ret.size() == 0x20); - BOOST_CHECK(std::count(ret.begin(), ret.begin() + 12, 0) == 12); - return eth::abiOut(ret); - } - - std::string callAddressReturnsString(std::string const& _name, u160 const& _arg) - { - bytesConstRef ret = ref(call(_name + "(address)", _arg)); - BOOST_REQUIRE(ret.size() >= 0x20); - u256 offset = eth::abiOut(ret); - BOOST_REQUIRE_EQUAL(offset, 0x20); - u256 len = eth::abiOut(ret); - BOOST_REQUIRE_EQUAL(ret.size(), ((len + 0x1f) / 0x20) * 0x20); - return ret.cropped(0, size_t(len)).toString(); - } - - h256 callStringReturnsBytes32(std::string const& _name, std::string const& _arg) - { - bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg); - BOOST_REQUIRE(ret.size() == 0x20); - return eth::abiOut(ret); - } - - private: - u256 m_nextValue; - ExecutionFramework& m_framework; - }; - -private: - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - _cppFunction(_arguments...); - return bytes(); - } - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - return encode(_cppFunction(_arguments...)); - } - -protected: - void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) - { - RPCRequest::transactionData d; - d.data = "0x" + toHex(_data); - d.from = "0x" + toString(m_sender); - d.gas = toHex(m_gas, HexPrefix::Add); - d.gasPrice = toHex(m_gasPrice, HexPrefix::Add); - d.value = toHex(_value, HexPrefix::Add); - if (_isCreation) - d.to = ""; - else - d.to = dev::toString(m_contractAddress); - - string code = m_socket.eth_getCode(d.to, "latest"); - string output = m_socket.eth_call(d, "latest"); - string hash = m_socket.eth_sendTransaction(d); - m_socket.test_mineBlocks(1); - RPCRequest::transactionReceipt receipt; - receipt = m_socket.eth_getTransactionReceipt(hash); - - if (_isCreation) - { - m_contractAddress = Address(receipt.contractAddress); - BOOST_REQUIRE(m_contractAddress); - string code = m_socket.eth_getCode(receipt.contractAddress, "latest"); - BOOST_REQUIRE(code.size() > 2); - } - else - BOOST_REQUIRE(code.size() > 2); - - m_gasUsed = u256(receipt.gasUsed); - m_output = fromHex(output, WhenError::Throw); - m_logs.clear(); - } - - std::unique_ptr m_sealEngine; - size_t m_optimizeRuns = 200; - bool m_optimize = false; - bool m_addStandardSources = false; - dev::solidity::CompilerStack m_compiler; - Address m_sender; - Address m_contractAddress; - eth::EnvInfo m_envInfo; - eth::State m_state; - u256 const m_gasPrice = 100 * eth::szabo; - u256 const m_gas = 100000000; - bytes m_output; - eth::LogEntries m_logs; - u256 m_gasUsed; - - RPCRequest m_socket; -}; - -} -} -} // end namespaces - -- cgit v1.2.3 From 874a16bed4232d2c1fb79ee34e1a2e8f3ba89bdf Mon Sep 17 00:00:00 2001 From: Dimitry Date: Mon, 13 Jun 2016 16:17:56 +0300 Subject: fix rpc mining --- test/IPCSocket.cpp | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index 77a8e176..b7b2572b 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -148,24 +148,16 @@ void RPCSession::test_mineBlocks(int _number) u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); u256 currentBlock = startBlock; u256 targetBlock = startBlock + _number; - cout << "A" << endl; - for (size_t tries = 0; tries < 3 && startBlock < targetBlock; ++tries) + cout << "MINE" << endl; + rpcCall("test_mineBlocks", { (targetBlock - startBlock).str() }, true); + + //@TODO do not use polling - but that would probably need a change to the test client + for (size_t polls = 0; polls < 100; ++polls) { - if (currentBlock == startBlock) - { - cout << "MINE" << endl; - rpcCall("test_mineBlocks", { (targetBlock - startBlock).str() }, true); - } - cout << "WOIT" << endl; - startBlock = currentBlock; - //@TODO do not use polling - but that would probably need a change to the test client - for (size_t polls = 0; polls < 10; ++polls) - { - currentBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); - if (currentBlock >= targetBlock) - return; - std::this_thread::sleep_for(chrono::milliseconds(1)); - } + currentBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); + if (currentBlock >= targetBlock) + return; + std::this_thread::sleep_for(chrono::milliseconds(10)); //it does not work faster then 10 ms } } @@ -194,7 +186,9 @@ Json::Value RPCSession::rpcCall(string const& _methodName, vector const& { if (_canFail) return Json::Value(); - BOOST_FAIL("Error on JSON-RPC call: " + result["error"].asString()); + + Json::Value jsonError = result["error"]; + BOOST_FAIL("Error on JSON-RPC call: " + jsonError["message"].asString()); } return result["result"]; } -- cgit v1.2.3 From 603bad34f9ea24a5a202ae7844549171c10c0ea2 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Mon, 13 Jun 2016 17:02:58 +0300 Subject: style --- test/IPCSocket.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index b7b2572b..f89cd335 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -144,12 +144,10 @@ void RPCSession::test_rewindToBlock(size_t _blockNr) void RPCSession::test_mineBlocks(int _number) { - // Extremely complicated mechanism because sometimes the miner breaks and stops mining. u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); u256 currentBlock = startBlock; u256 targetBlock = startBlock + _number; - cout << "MINE" << endl; - rpcCall("test_mineBlocks", { (targetBlock - startBlock).str() }, true); + rpcCall("test_mineBlocks", { to_string(_number) }, true); //@TODO do not use polling - but that would probably need a change to the test client for (size_t polls = 0; polls < 100; ++polls) @@ -159,6 +157,8 @@ void RPCSession::test_mineBlocks(int _number) return; std::this_thread::sleep_for(chrono::milliseconds(10)); //it does not work faster then 10 ms } + + BOOST_FAIL("Error in test_mineBlocks: block mining timeout!"); } Json::Value RPCSession::rpcCall(string const& _methodName, vector const& _args, bool _canFail) @@ -187,8 +187,7 @@ Json::Value RPCSession::rpcCall(string const& _methodName, vector const& if (_canFail) return Json::Value(); - Json::Value jsonError = result["error"]; - BOOST_FAIL("Error on JSON-RPC call: " + jsonError["message"].asString()); + BOOST_FAIL("Error on JSON-RPC call: " + result["error"]["message"].asString()); } return result["result"]; } -- cgit v1.2.3 From 966709b7d7ee70cfc550923e22e17a22556609a4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 13 Jun 2016 17:10:58 +0200 Subject: logs --- test/IPCSocket.cpp | 14 +++++++++++--- test/IPCSocket.h | 7 +++++++ test/libsolidity/SolidityEndToEndTest.cpp | 12 ++++++------ test/libsolidity/SolidityExecutionFramework.cpp | 12 ++++++++++-- test/libsolidity/SolidityExecutionFramework.h | 9 ++++++++- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index f89cd335..2c1eb7a6 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -85,6 +85,15 @@ RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string cons BOOST_REQUIRE(!result.isNull()); receipt.gasUsed = result["gasUsed"].asString(); receipt.contractAddress = result["contractAddress"].asString(); + for (auto const& log: result["logs"]) + { + LogEntry entry; + entry.address = log["address"].asString(); + entry.data = log["data"].asString(); + for (auto const& topic: log["topics"]) + entry.topics.push_back(topic.asString()); + receipt.logEntries.push_back(entry); + } return receipt; } @@ -174,10 +183,9 @@ Json::Value RPCSession::rpcCall(string const& _methodName, vector const& request += "],\"id\":" + to_string(m_rpcSequence) + "}"; ++m_rpcSequence; + //cout << "Request: " << request << endl; string reply = m_ipcSocket.sendRequest(request); - - cout << "Request: " << request << endl; - cout << "Reply: " << reply << endl; + //cout << "Reply: " << reply << endl; Json::Value result; Json::Reader().parse(reply, result, false); diff --git a/test/IPCSocket.h b/test/IPCSocket.h index 9b4ab73e..bc641e88 100644 --- a/test/IPCSocket.h +++ b/test/IPCSocket.h @@ -59,10 +59,17 @@ public: std::string toJson() const; }; + struct LogEntry { + std::string address; + std::vector topics; + std::string data; + }; + struct TransactionReceipt { std::string gasUsed; std::string contractAddress; + std::vector logEntries; }; static RPCSession& instance(std::string const& _path); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 46677acb..3208cedf 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1553,7 +1553,7 @@ BOOST_AUTO_TEST_CASE(log0) "}\n"; compileAndRun(sourceCode); callContractFunction("a()"); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 0); @@ -1568,7 +1568,7 @@ BOOST_AUTO_TEST_CASE(log1) "}\n"; compileAndRun(sourceCode); callContractFunction("a()"); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1); @@ -1584,7 +1584,7 @@ BOOST_AUTO_TEST_CASE(log2) "}\n"; compileAndRun(sourceCode); callContractFunction("a()"); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 2); @@ -1601,7 +1601,7 @@ BOOST_AUTO_TEST_CASE(log3) "}\n"; compileAndRun(sourceCode); callContractFunction("a()"); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 3); @@ -1618,7 +1618,7 @@ BOOST_AUTO_TEST_CASE(log4) "}\n"; compileAndRun(sourceCode); callContractFunction("a()"); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 4); @@ -1634,7 +1634,7 @@ BOOST_AUTO_TEST_CASE(log_in_constructor) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK_EQUAL(m_logs.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1); diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index 5c8aff8d..871c0eba 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -39,7 +39,6 @@ ExecutionFramework::ExecutionFramework(): if (g_logVerbosity != -1) g_logVerbosity = 0; - cout << "New Framework" << endl; m_rpc.test_rewindToBlock(0); } @@ -69,9 +68,18 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 BOOST_REQUIRE(m_contractAddress); string code = m_rpc.eth_getCode(receipt.contractAddress, "latest"); BOOST_REQUIRE(code.size() > 2); - m_output = asBytes(code); + m_output = fromHex(code, WhenError::Throw); } m_gasUsed = u256(receipt.gasUsed); m_logs.clear(); + for (auto const& log: receipt.logEntries) + { + LogEntry entry; + entry.address = Address(log.address); + for (auto const& topic: log.topics) + entry.topics.push_back(h256(topic)); + entry.data = fromHex(log.data, WhenError::Throw); + m_logs.push_back(entry); + } } diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 512f515f..8e266819 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -253,6 +253,13 @@ protected: RPCSession& m_rpc; + struct LogEntry + { + Address address; + std::vector topics; + bytes data; + }; + std::unique_ptr m_sealEngine; size_t m_optimizeRuns = 200; bool m_optimize = false; @@ -265,7 +272,7 @@ protected: u256 const m_gasPrice = 100 * eth::szabo; u256 const m_gas = 100000000; bytes m_output; - eth::LogEntries m_logs; + std::vector m_logs; u256 m_gasUsed; }; -- cgit v1.2.3 From ce2258b71e632e01b3f50d47704352065cb01b5d Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 14 Jun 2016 00:12:13 +0200 Subject: Balance and storage. --- test/IPCSocket.cpp | 11 ++- test/IPCSocket.h | 1 + test/libsolidity/SolidityEndToEndTest.cpp | 122 ++++++++++++------------ test/libsolidity/SolidityExecutionFramework.cpp | 10 ++ test/libsolidity/SolidityExecutionFramework.h | 3 + 5 files changed, 82 insertions(+), 65 deletions(-) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index 2c1eb7a6..8dd364bf 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -118,6 +118,12 @@ string RPCSession::eth_getBalance(string const& _address, string const& _blockNu return rpcCall("eth_getBalance", { quote(address), quote(_blockNumber) }).asString(); } +string RPCSession::eth_getStorageRoot(string const& _address, string const& _blockNumber) +{ + string address = (_address.length() == 20) ? "0x" + _address : _address; + return rpcCall("eth_getStorageRoot", { quote(address), quote(_blockNumber) }).asString(); +} + void RPCSession::personal_unlockAccount(string const& _address, string const& _password, int _duration) { rpcCall("personal_unlockAccount", { quote(_address), quote(_password), to_string(_duration) }); @@ -154,15 +160,12 @@ void RPCSession::test_rewindToBlock(size_t _blockNr) void RPCSession::test_mineBlocks(int _number) { u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); - u256 currentBlock = startBlock; - u256 targetBlock = startBlock + _number; rpcCall("test_mineBlocks", { to_string(_number) }, true); //@TODO do not use polling - but that would probably need a change to the test client for (size_t polls = 0; polls < 100; ++polls) { - currentBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); - if (currentBlock >= targetBlock) + if (fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())) >= startBlock + _number) return; std::this_thread::sleep_for(chrono::milliseconds(10)); //it does not work faster then 10 ms } diff --git a/test/IPCSocket.h b/test/IPCSocket.h index bc641e88..89e1d50a 100644 --- a/test/IPCSocket.h +++ b/test/IPCSocket.h @@ -80,6 +80,7 @@ public: std::string eth_sendTransaction(TransactionData const& _transactionData); std::string eth_sendTransaction(std::string const& _transaction); std::string eth_getBalance(std::string const& _address, std::string const& _blockNumber); + std::string eth_getStorageRoot(std::string const& _address, std::string const& _blockNumber); std::string personal_newAccount(std::string const& _password); void personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration); void test_setChainParams(std::string const& _author, std::string const& _account, std::string const& _balance); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 3208cedf..7ab49248 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1541,7 +1541,7 @@ BOOST_AUTO_TEST_CASE(send_ether) compileAndRun(sourceCode, amount + 1); u160 address(23); BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == encodeArgs(1)); - BOOST_CHECK_EQUAL(m_state.balance(address), amount); + BOOST_CHECK_EQUAL(balanceAt(address), amount); } BOOST_AUTO_TEST_CASE(log0) @@ -1571,7 +1571,7 @@ BOOST_AUTO_TEST_CASE(log1) BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); - BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2))); } @@ -1587,7 +1587,7 @@ BOOST_AUTO_TEST_CASE(log2) BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); - BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 2); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2); for (unsigned i = 0; i < 2; ++i) BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2))); } @@ -1604,7 +1604,7 @@ BOOST_AUTO_TEST_CASE(log3) BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); - BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 3); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 3); for (unsigned i = 0; i < 3; ++i) BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2))); } @@ -1621,7 +1621,7 @@ BOOST_AUTO_TEST_CASE(log4) BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); - BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 4); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 4); for (unsigned i = 0; i < 4; ++i) BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2))); } @@ -1637,7 +1637,7 @@ BOOST_AUTO_TEST_CASE(log_in_constructor) BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1))); - BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2))); } @@ -1654,7 +1654,7 @@ BOOST_AUTO_TEST_CASE(suicide) u160 address(23); BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); - BOOST_CHECK_EQUAL(m_state.balance(address), amount); + BOOST_CHECK_EQUAL(balanceAt(address), amount); } BOOST_AUTO_TEST_CASE(selfdestruct) @@ -1670,7 +1670,7 @@ BOOST_AUTO_TEST_CASE(selfdestruct) u160 address(23); BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); - BOOST_CHECK_EQUAL(m_state.balance(address), amount); + BOOST_CHECK_EQUAL(balanceAt(address), amount); } BOOST_AUTO_TEST_CASE(sha3) @@ -2465,10 +2465,10 @@ BOOST_AUTO_TEST_CASE(use_std_lib) u256 amount(130); u160 address(23); compileAndRun(sourceCode, amount, "Icarus"); - u256 balanceBefore = m_state.balance(m_sender); + u256 balanceBefore = balanceAt(m_sender); BOOST_CHECK(callContractFunction("kill()") == bytes()); BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); - BOOST_CHECK(m_state.balance(m_sender) > balanceBefore); + BOOST_CHECK(balanceAt(m_sender) > balanceBefore); } BOOST_AUTO_TEST_CASE(crazy_elementary_typenames_on_stack) @@ -2876,7 +2876,7 @@ BOOST_AUTO_TEST_CASE(generic_call) u160 const c_receiverAddress = m_contractAddress; compileAndRun(sourceCode, 50, "sender"); BOOST_REQUIRE(callContractFunction("doSend(address)", c_receiverAddress) == encodeArgs(23)); - BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 50 - 2); + BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 50 - 2); } BOOST_AUTO_TEST_CASE(generic_callcode) @@ -2904,10 +2904,10 @@ BOOST_AUTO_TEST_CASE(generic_callcode) BOOST_CHECK(callContractFunction("received()") == encodeArgs(23)); m_contractAddress = c_receiverAddress; BOOST_CHECK(callContractFunction("received()") == encodeArgs(0)); - BOOST_CHECK(m_state.storage(c_receiverAddress).empty()); - BOOST_CHECK(!m_state.storage(c_senderAddress).empty()); - BOOST_CHECK_EQUAL(m_state.balance(c_receiverAddress), 0); - BOOST_CHECK_EQUAL(m_state.balance(c_senderAddress), 50); + BOOST_CHECK(storageEmpty(c_receiverAddress)); + BOOST_CHECK(!storageEmpty(c_senderAddress)); + BOOST_CHECK_EQUAL(balanceAt(c_receiverAddress), 0); + BOOST_CHECK_EQUAL(balanceAt(c_senderAddress), 50); } BOOST_AUTO_TEST_CASE(generic_delegatecall) @@ -2943,10 +2943,10 @@ BOOST_AUTO_TEST_CASE(generic_delegatecall) BOOST_CHECK(callContractFunction("received()") == encodeArgs(u256(0))); BOOST_CHECK(callContractFunction("sender()") == encodeArgs(u256(0))); BOOST_CHECK(callContractFunction("value()") == encodeArgs(u256(0))); - BOOST_CHECK(m_state.storage(c_receiverAddress).empty()); - BOOST_CHECK(!m_state.storage(c_senderAddress).empty()); - BOOST_CHECK_EQUAL(m_state.balance(c_receiverAddress), 0); - BOOST_CHECK_EQUAL(m_state.balance(c_senderAddress), 50 + 11); + BOOST_CHECK(storageEmpty(c_receiverAddress)); + BOOST_CHECK(!storageEmpty(c_senderAddress)); + BOOST_CHECK_EQUAL(balanceAt(c_receiverAddress), 0); + BOOST_CHECK_EQUAL(balanceAt(c_senderAddress), 50 + 11); } BOOST_AUTO_TEST_CASE(library_call_in_homestead) @@ -3073,9 +3073,9 @@ BOOST_AUTO_TEST_CASE(delete_removes_bytes_data) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("---", 7) == bytes()); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("del()", 7) == encodeArgs(true)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) @@ -3089,10 +3089,10 @@ BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); sendMessage(bytes(), false); BOOST_CHECK(m_output == bytes()); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(copy_removes_bytes_data) @@ -3107,9 +3107,9 @@ BOOST_AUTO_TEST_CASE(copy_removes_bytes_data) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("reset()") == encodeArgs(true)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(bytes_inside_mappings) @@ -3125,15 +3125,15 @@ BOOST_AUTO_TEST_CASE(bytes_inside_mappings) // store a short byte array at 1 and a longer one at 2 BOOST_CHECK(callContractFunction("set(uint256)", 1, 2) == encodeArgs(true)); BOOST_CHECK(callContractFunction("set(uint256)", 2, 2, 3, 4, 5) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); // copy shorter to longer BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 1, 2) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); // copy empty to both BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 1) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 2) == encodeArgs(true)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(bytes_length_member) @@ -3216,15 +3216,15 @@ BOOST_AUTO_TEST_CASE(struct_containing_bytes_copy_and_delete) )"; compileAndRun(sourceCode); string data = "123456789012345678901234567890123"; - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, u256(data.length()), 13, data) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("copy()") == encodeArgs(true)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, u256(data.length()), 13, data) == encodeArgs(true)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("del()") == encodeArgs(true)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(struct_copy_via_local) @@ -3496,11 +3496,11 @@ BOOST_AUTO_TEST_CASE(fixed_array_cleanup) } )"; compileAndRun(sourceCode); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("fill()") == bytes()); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("clear()") == bytes()); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(short_fixed_array_cleanup) @@ -3517,11 +3517,11 @@ BOOST_AUTO_TEST_CASE(short_fixed_array_cleanup) } )"; compileAndRun(sourceCode); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("fill()") == bytes()); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("clear()") == bytes()); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(dynamic_array_cleanup) @@ -3539,13 +3539,13 @@ BOOST_AUTO_TEST_CASE(dynamic_array_cleanup) } )"; compileAndRun(sourceCode); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("fill()") == bytes()); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("halfClear()") == bytes()); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("fullClear()") == bytes()); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(dynamic_multi_array_cleanup) @@ -3565,11 +3565,11 @@ BOOST_AUTO_TEST_CASE(dynamic_multi_array_cleanup) } )"; compileAndRun(sourceCode); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("fill()") == encodeArgs(8)); - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("clear()") == bytes()); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(array_copy_storage_storage_dyn_dyn) @@ -3594,7 +3594,7 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_storage_dyn_dyn) BOOST_CHECK(callContractFunction("setData1(uint256,uint256,uint256)", 0, 0, 0) == bytes()); BOOST_CHECK(callContractFunction("copyStorageStorage()") == bytes()); BOOST_CHECK(callContractFunction("getData2(uint256)", 0) == encodeArgs(0, 0)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(array_copy_storage_storage_static_static) @@ -3775,7 +3775,7 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_storage_struct) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("test()") == encodeArgs(4, 5)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(array_push) @@ -3992,9 +3992,9 @@ BOOST_AUTO_TEST_CASE(array_copy_including_mapping) compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("test()") == encodeArgs(0x02000200)); // storage is not empty because we cannot delete the mappings - BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(!storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("clear()") == encodeArgs(7)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(pass_dynamic_arguments_to_the_base) @@ -4204,7 +4204,7 @@ BOOST_AUTO_TEST_CASE(packed_storage_structs_delete) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_first) @@ -5890,11 +5890,11 @@ BOOST_AUTO_TEST_CASE(short_strings) compileAndRun(sourceCode, 0, "A"); BOOST_CHECK(callContractFunction("data1()") == encodeDyn(string("123"))); BOOST_CHECK(callContractFunction("lengthChange()") == encodeArgs(u256(0))); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("deleteElements()") == encodeArgs(u256(0))); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); BOOST_CHECK(callContractFunction("copy()") == encodeArgs(u256(0))); - BOOST_CHECK(m_state.storage(m_contractAddress).empty()); + BOOST_CHECK(storageEmpty(m_contractAddress)); } BOOST_AUTO_TEST_CASE(calldata_offset) @@ -5950,14 +5950,14 @@ BOOST_AUTO_TEST_CASE(reject_ether_sent_to_library) compileAndRun(sourceCode, 0, "lib"); Address libraryAddress = m_contractAddress; compileAndRun(sourceCode, 10, "c"); - BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 10); - BOOST_CHECK_EQUAL(m_state.balance(libraryAddress), 0); + BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 10); + BOOST_CHECK_EQUAL(balanceAt(libraryAddress), 0); BOOST_CHECK(callContractFunction("f(address)", encodeArgs(u160(libraryAddress))) == encodeArgs(false)); - BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 10); - BOOST_CHECK_EQUAL(m_state.balance(libraryAddress), 0); + BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 10); + BOOST_CHECK_EQUAL(balanceAt(libraryAddress), 0); BOOST_CHECK(callContractFunction("f(address)", encodeArgs(u160(m_contractAddress))) == encodeArgs(true)); - BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 10); - BOOST_CHECK_EQUAL(m_state.balance(libraryAddress), 0); + BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 10); + BOOST_CHECK_EQUAL(balanceAt(libraryAddress), 0); } BOOST_AUTO_TEST_CASE(multi_variable_declaration) diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index 871c0eba..db48c3a1 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -83,3 +83,13 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 m_logs.push_back(entry); } } + +u256 ExecutionFramework::balanceAt(Address const& _addr) +{ + return u256(m_rpc.eth_getBalance(toString(_addr), "latest")); +} + +bool ExecutionFramework::storageEmpty(Address const& _addr) +{ + return h256(m_rpc.eth_getStorageRoot(toString(_addr), "latest")) == EmptySHA3; +} diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 8e266819..f2366d50 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -251,6 +251,9 @@ private: protected: void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0); + u256 balanceAt(Address const& _addr); + bool storageEmpty(Address const& _addr); + RPCSession& m_rpc; struct LogEntry -- cgit v1.2.3 From 763faf7b0e56cde9845772de5226c9b6f1a5d80a Mon Sep 17 00:00:00 2001 From: Dimitry Date: Tue, 14 Jun 2016 18:01:57 +0300 Subject: replace BalanceAt add addressHasCode remove m_state and sealengine --- test/contracts/FixedFeeRegistrar.cpp | 4 ++-- test/contracts/Wallet.cpp | 26 ++++++++++++------------- test/libsolidity/SolidityEndToEndTest.cpp | 6 +++--- test/libsolidity/SolidityExecutionFramework.cpp | 12 ++++++++---- test/libsolidity/SolidityExecutionFramework.h | 3 +-- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index 35f0842d..df521e7e 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -234,9 +234,9 @@ BOOST_AUTO_TEST_CASE(disown) BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(124), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), u256(125), u256(name.length()), name) == encodeArgs()); - BOOST_CHECK_EQUAL(m_state.balance(Address(0x124)), 0); + BOOST_CHECK_EQUAL(balanceAt(Address(0x124)), 0); BOOST_CHECK(callContractFunction("disown(string,address)", u256(0x40), u256(0x124), name.size(), name) == encodeArgs()); - BOOST_CHECK_EQUAL(m_state.balance(Address(0x124)), m_fee); + BOOST_CHECK_EQUAL(balanceAt(Address(0x124)), m_fee); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(0))); BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(u256(0))); diff --git a/test/contracts/Wallet.cpp b/test/contracts/Wallet.cpp index 060525b0..29fe5b8d 100644 --- a/test/contracts/Wallet.cpp +++ b/test/contracts/Wallet.cpp @@ -555,17 +555,17 @@ BOOST_AUTO_TEST_CASE(multisig_value_transfer) BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); // check that balance is and stays zero at destination address h256 opHash("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e"); - BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(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_CHECK_EQUAL(balanceAt(Address(0x05)), 100); } BOOST_AUTO_TEST_CASE(revoke_addOwner) @@ -606,22 +606,22 @@ BOOST_AUTO_TEST_CASE(revoke_transaction) // create a transaction Address deployer = m_sender; h256 opHash("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e"); - BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); m_sender = Address(0x12); BOOST_REQUIRE(callContractFunction("revoke(bytes32)", opHash) == encodeArgs()); m_sender = deployer; BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); - BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + BOOST_CHECK_EQUAL(balanceAt(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_CHECK_EQUAL(balanceAt(Address(0x05)), 100); } BOOST_AUTO_TEST_CASE(daylimit) @@ -637,27 +637,27 @@ BOOST_AUTO_TEST_CASE(daylimit) BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); // try to send tx over daylimit - BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(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); + BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 90); } BOOST_AUTO_TEST_CASE(daylimit_constructor) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 7ab49248..8689d61e 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1653,7 +1653,7 @@ BOOST_AUTO_TEST_CASE(suicide) compileAndRun(sourceCode, amount); u160 address(23); BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); - BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); + BOOST_CHECK(!addressHasCode(m_contractAddress)); BOOST_CHECK_EQUAL(balanceAt(address), amount); } @@ -1669,7 +1669,7 @@ BOOST_AUTO_TEST_CASE(selfdestruct) compileAndRun(sourceCode, amount); u160 address(23); BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); - BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); + BOOST_CHECK(!addressHasCode(m_contractAddress)); BOOST_CHECK_EQUAL(balanceAt(address), amount); } @@ -2467,7 +2467,7 @@ BOOST_AUTO_TEST_CASE(use_std_lib) compileAndRun(sourceCode, amount, "Icarus"); u256 balanceBefore = balanceAt(m_sender); BOOST_CHECK(callContractFunction("kill()") == bytes()); - BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); + BOOST_CHECK(!addressHasCode(m_contractAddress)); BOOST_CHECK(balanceAt(m_sender) > balanceBefore); } diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index db48c3a1..43d36f38 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -30,12 +30,10 @@ using namespace dev::solidity::test; ExecutionFramework::ExecutionFramework(): - m_rpc(RPCSession::instance("/tmp/test/geth.ipc")), - m_sender(m_rpc.account(0)), - m_state(0) + m_rpc(RPCSession::instance("/home/wins/Ethereum/testnet/ethnode1/geth.ipc")), + m_sender(m_rpc.account(0)) { eth::NoProof::init(); - m_sealEngine.reset(eth::ChainParams().createSealEngine()); if (g_logVerbosity != -1) g_logVerbosity = 0; @@ -84,6 +82,12 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 } } +bool ExecutionFramework::addressHasCode(Address const& _addr) +{ + string code = m_rpc.eth_getCode(toString(_addr), "latest"); + return !code.empty() && code != "0x"; +} + u256 ExecutionFramework::balanceAt(Address const& _addr) { return u256(m_rpc.eth_getBalance(toString(_addr), "latest")); diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index f2366d50..42b22c82 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -253,6 +253,7 @@ protected: u256 balanceAt(Address const& _addr); bool storageEmpty(Address const& _addr); + bool addressHasCode(Address const& _addr); RPCSession& m_rpc; @@ -263,7 +264,6 @@ protected: bytes data; }; - std::unique_ptr m_sealEngine; size_t m_optimizeRuns = 200; bool m_optimize = false; bool m_addStandardSources = false; @@ -271,7 +271,6 @@ protected: Address m_sender; Address m_contractAddress; eth::EnvInfo m_envInfo; - eth::State m_state; u256 const m_gasPrice = 100 * eth::szabo; u256 const m_gas = 100000000; bytes m_output; -- cgit v1.2.3 From 27df4b7e7118795a7d915e2eb88ff578b0ddc814 Mon Sep 17 00:00:00 2001 From: Dimitry Date: Thu, 16 Jun 2016 16:06:53 +0300 Subject: parse command line arguments ipcPath --- test/libsolidity/SolidityExecutionFramework.cpp | 28 ++++++++++++++++++++++--- test/libsolidity/SolidityExecutionFramework.h | 7 +++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index 43d36f38..7ea81888 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -20,6 +20,7 @@ * Framework for executing Solidity contracts and testing them against C++ implementation. */ +#include #include @@ -28,12 +29,33 @@ using namespace dev; using namespace dev::solidity; using namespace dev::solidity::test; +ExecutionFramework::boostArg::boostArg() +{ + size_t argc = boost::unit_test::framework::master_test_suite().argc; + char** argv = boost::unit_test::framework::master_test_suite().argv; + for (size_t i = 0; i < argc; i++) + { + string arg = argv[i]; + if (arg == "--ipc" && i+1 < argc) + { + ipcPath = argv[i+1]; + i++; + } + } + if (ipcPath.empty()) + BOOST_FAIL("ERROR: ipcPath not set! (use --ipc )"); +} + +ExecutionFramework::boostArg const& ExecutionFramework::getArgs() +{ + static boostArg boostArgs; + return boostArgs; +} -ExecutionFramework::ExecutionFramework(): - m_rpc(RPCSession::instance("/home/wins/Ethereum/testnet/ethnode1/geth.ipc")), +ExecutionFramework::ExecutionFramework() : + m_rpc(RPCSession::instance(getArgs().ipcPath)), m_sender(m_rpc.account(0)) { - eth::NoProof::init(); if (g_logVerbosity != -1) g_logVerbosity = 0; diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 42b22c82..d149cda2 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -255,6 +255,13 @@ protected: bool storageEmpty(Address const& _addr); bool addressHasCode(Address const& _addr); + struct boostArg + { + boostArg(); + std::string ipcPath; + }; + + boostArg const& getArgs(); RPCSession& m_rpc; struct LogEntry -- cgit v1.2.3 From 93c8fc094e601f8d1105a820e64ac6c5fe7298f2 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 17 Jun 2016 00:19:16 +0200 Subject: Parse arguments in free function. --- test/libsolidity/SolidityExecutionFramework.cpp | 21 ++++++++++----------- test/libsolidity/SolidityExecutionFramework.h | 7 ------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index 7ea81888..8c026ffd 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -20,40 +20,39 @@ * Framework for executing Solidity contracts and testing them against C++ implementation. */ +#include #include #include - using namespace std; using namespace dev; using namespace dev::solidity; using namespace dev::solidity::test; -ExecutionFramework::boostArg::boostArg() +string getIPCSocketPath() { + string ipcPath; + size_t argc = boost::unit_test::framework::master_test_suite().argc; char** argv = boost::unit_test::framework::master_test_suite().argv; for (size_t i = 0; i < argc; i++) { string arg = argv[i]; - if (arg == "--ipc" && i+1 < argc) + if (arg == "--ipc" && i + 1 < argc) { - ipcPath = argv[i+1]; + ipcPath = argv[i + 1]; i++; } } + if (ipcPath.empty()) + ipcPath = getenv("ETH_TEST_IPC"); if (ipcPath.empty()) BOOST_FAIL("ERROR: ipcPath not set! (use --ipc )"); -} - -ExecutionFramework::boostArg const& ExecutionFramework::getArgs() -{ - static boostArg boostArgs; - return boostArgs; + return ipcPath; } ExecutionFramework::ExecutionFramework() : - m_rpc(RPCSession::instance(getArgs().ipcPath)), + m_rpc(RPCSession::instance(getIPCSocketPath())), m_sender(m_rpc.account(0)) { if (g_logVerbosity != -1) diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index d149cda2..42b22c82 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -255,13 +255,6 @@ protected: bool storageEmpty(Address const& _addr); bool addressHasCode(Address const& _addr); - struct boostArg - { - boostArg(); - std::string ipcPath; - }; - - boostArg const& getArgs(); RPCSession& m_rpc; struct LogEntry -- cgit v1.2.3 From 007132a78e5d97e0476e1153b14f0ae63f458b04 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 17 Jun 2016 00:47:42 +0200 Subject: Fix empty storage check. --- test/libsolidity/SolidityExecutionFramework.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index 8c026ffd..abb564db 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -116,5 +116,7 @@ u256 ExecutionFramework::balanceAt(Address const& _addr) bool ExecutionFramework::storageEmpty(Address const& _addr) { - return h256(m_rpc.eth_getStorageRoot(toString(_addr), "latest")) == EmptySHA3; + h256 root(m_rpc.eth_getStorageRoot(toString(_addr), "latest")); + BOOST_CHECK(root); + return root == EmptyTrie; } -- cgit v1.2.3 From e5db096da9a2bfb3e6445b2d89cf730c7e22dc6d Mon Sep 17 00:00:00 2001 From: chriseth Date: Sat, 18 Jun 2016 02:08:20 +0200 Subject: Fix some more tests. --- test/IPCSocket.cpp | 83 ++++++++++--------- test/IPCSocket.h | 38 +-------- test/contracts/AuctionRegistrar.cpp | 87 ++++++++++---------- test/contracts/FixedFeeRegistrar.cpp | 37 +++++---- test/contracts/Wallet.cpp | 103 +++++++++++++++--------- test/libsolidity/SolidityEndToEndTest.cpp | 17 ++-- test/libsolidity/SolidityExecutionFramework.cpp | 29 ++++++- test/libsolidity/SolidityExecutionFramework.h | 5 ++ 8 files changed, 220 insertions(+), 179 deletions(-) diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp index 8dd364bf..832875c0 100644 --- a/test/IPCSocket.cpp +++ b/test/IPCSocket.cpp @@ -134,17 +134,38 @@ string RPCSession::personal_newAccount(string const& _password) return rpcCall("personal_newAccount", { quote(_password) }).asString(); } -void RPCSession::test_setChainParams(string const& _author, string const& _account, string const& _balance) +void RPCSession::test_setChainParams(vector const& _accounts) { - if (_account.size() < 40) - return; - string config = c_genesisConfiguration; - std::map replaceMap; - replaceMap["[AUTHOR]"] = _author; - replaceMap["[ACCOUNT]"] = (_account[0] == '0' && _account[1] == 'x') ? _account.substr(2, 40) : _account; - replaceMap["[BALANCE]"] = _balance; - parseString(config, replaceMap); - test_setChainParams(config); + static std::string const c_configString = R"( + { + "sealEngine": "NoProof", + "params": { + "accountStartNonce": "0x", + "maximumExtraDataSize": "0x1000000", + "blockReward": "0x", + "allowFutureBlocks": "1" + }, + "genesis": { + "author": "0000000000000010000000000000000000000000", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x", + "gasLimit": "0x1000000000000" + }, + "accounts": { + "0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, + "0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, + "0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, + "0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } } + } + } + )"; + + Json::Value config; + BOOST_REQUIRE(Json::Reader().parse(c_configString, config)); + for (auto const& account: _accounts) + config["accounts"][account]["wei"] = "0x100000000000000000000000000000000000000000"; + test_setChainParams(Json::FastWriter().write(config)); } void RPCSession::test_setChainParams(string const& _config) @@ -173,6 +194,11 @@ void RPCSession::test_mineBlocks(int _number) BOOST_FAIL("Error in test_mineBlocks: block mining timeout!"); } +void RPCSession::test_modifyTimestamp(size_t _timestamp) +{ + rpcCall("test_modifyTimestamp", { to_string(_timestamp) }); +} + Json::Value RPCSession::rpcCall(string const& _methodName, vector const& _args, bool _canFail) { string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":["; @@ -203,40 +229,25 @@ Json::Value RPCSession::rpcCall(string const& _methodName, vector const& return result["result"]; } -RPCSession::RPCSession(const string& _path): - m_ipcSocket(_path) +string const& RPCSession::accountCreateIfNotExists(size_t _id) { - for (size_t i = 0; i < 1; ++i) + if (_id >= m_accounts.size()) { - string account = personal_newAccount(""); - personal_unlockAccount(account, "", 100000); - m_accounts.push_back(account); + m_accounts.push_back(personal_newAccount("")); + personal_unlockAccount(m_accounts.back(), "", 100000); } - test_setChainParams( - "0x1000000000000000000000000000000000000000", - m_accounts.front(), - "1000000000000000000000000000000000000000000000" - ); + return m_accounts[_id]; } -void RPCSession::parseString(string& _string, map const& _varMap) +RPCSession::RPCSession(const string& _path): + m_ipcSocket(_path) { - std::vector types; - for (std::map::const_iterator it = _varMap.begin(); it != _varMap.end(); it++) - types.push_back(it->first); - - for (unsigned i = 0; i < types.size(); i++) - { - std::size_t pos = _string.find(types.at(i)); - while (pos != std::string::npos) - { - _string.replace(pos, types.at(i).size(), _varMap.at(types.at(i))); - pos = _string.find(types.at(i)); - } - } + string account = personal_newAccount(""); + personal_unlockAccount(account, "", 100000); + m_accounts.push_back(account); + test_setChainParams(m_accounts); } - string RPCSession::TransactionData::toJson() const { Json::Value json; diff --git a/test/IPCSocket.h b/test/IPCSocket.h index 89e1d50a..040eb2c8 100644 --- a/test/IPCSocket.h +++ b/test/IPCSocket.h @@ -83,13 +83,15 @@ public: std::string eth_getStorageRoot(std::string const& _address, std::string const& _blockNumber); std::string personal_newAccount(std::string const& _password); void personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration); - void test_setChainParams(std::string const& _author, std::string const& _account, std::string const& _balance); + void test_setChainParams(std::vector const& _accounts); void test_setChainParams(std::string const& _config); void test_rewindToBlock(size_t _blockNr); + void test_modifyTimestamp(size_t _timestamp); void test_mineBlocks(int _number); Json::Value rpcCall(std::string const& _methodName, std::vector const& _args = std::vector(), bool _canFail = false); std::string const& account(size_t _id) const { return m_accounts.at(_id); } + std::string const& accountCreateIfNotExists(size_t _id); private: RPCSession(std::string const& _path); @@ -101,40 +103,6 @@ private: IPCSocket m_ipcSocket; size_t m_rpcSequence = 1; - //Just working example of the node configuration file - std::string const c_genesisConfiguration = R"( - { - "sealEngine": "NoProof", - "options": { - }, - "params": { - "accountStartNonce": "0x", - "maximumExtraDataSize": "0x1000000", - "blockReward": "0x", - "registrar": "", - "allowFutureBlocks": "1" - }, - "genesis": { - "author": "[AUTHOR]", - "timestamp": "0x00", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "extraData": "0x", - "gasLimit": "0x1000000000000" - }, - "accounts": { - "0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, - "0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, - "0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, - "0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } }, - "[ACCOUNT]": { "wei": "[BALANCE]" } - }, - "network": { - "nodes": [ - ] - } - } - )"; - std::vector m_accounts; }; diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index e30f4572..0eee42a4 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -285,8 +285,8 @@ protected: } }; - u256 const m_biddingTime = u256(7 * 24 * 3600); - u256 const m_renewalInterval = u256(365 * 24 * 3600); + size_t const m_biddingTime = size_t(7 * 24 * 3600); + size_t const m_renewalInterval = size_t(365 * 24 * 3600); }; } @@ -304,7 +304,6 @@ BOOST_AUTO_TEST_CASE(reserve) // Test that reserving works for long strings deployRegistrar(); vector names{"abcabcabcabcabc", "defdefdefdefdef", "ghighighighighighighighighighighighighighighi"}; - m_sender = Address(0x123); RegistrarInterface registrar(*this); @@ -315,7 +314,7 @@ BOOST_AUTO_TEST_CASE(reserve) for (auto const& name: names) { registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); + BOOST_CHECK_EQUAL(registrar.owner(name), u160(m_sender)); } } @@ -324,14 +323,14 @@ BOOST_AUTO_TEST_CASE(double_reserve_long) // Test that it is not possible to re-reserve from a different address. deployRegistrar(); string name = "abcabcabcabcabcabcabcabcabcabca"; - m_sender = Address(0x123); RegistrarInterface registrar(*this); registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); + BOOST_CHECK_EQUAL(registrar.owner(name), m_sender); - m_sender = Address(0x124); + sendEther(account(1), u256(10) * eth::ether); + m_sender = account(1); registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); + BOOST_CHECK_EQUAL(registrar.owner(name), account(0)); } BOOST_AUTO_TEST_CASE(properties) @@ -341,14 +340,17 @@ BOOST_AUTO_TEST_CASE(properties) RegistrarInterface registrar(*this); string names[] = {"abcaeouoeuaoeuaoeu", "defncboagufra,fui", "ghagpyajfbcuajouhaeoi"}; size_t addr = 0x9872543; + size_t count = 1; for (string const& name: names) { - addr++; - size_t sender = addr + 10007; - m_sender = Address(sender); + m_sender = account(0); + sendEther(account(count), u256(20) * eth::ether); + m_sender = account(count); + auto sender = m_sender; + addr += count; // setting by sender works registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender)); + BOOST_CHECK_EQUAL(registrar.owner(name), sender); registrar.setAddress(name, addr, true); BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr)); registrar.setSubRegistrar(name, addr + 20); @@ -357,14 +359,15 @@ BOOST_AUTO_TEST_CASE(properties) BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90))); // but not by someone else - m_sender = Address(h256(addr + 10007 - 1)); - BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender)); + m_sender = account(count - 1); + BOOST_CHECK_EQUAL(registrar.owner(name), sender); registrar.setAddress(name, addr + 1, true); BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr)); registrar.setSubRegistrar(name, addr + 20 + 1); BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20)); registrar.setContent(name, h256(u256(addr + 90 + 1))); BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90))); + count++; } } @@ -372,7 +375,6 @@ BOOST_AUTO_TEST_CASE(transfer) { deployRegistrar(); string name = "abcaoeguaoucaeoduceo"; - m_sender = Address(0x123); RegistrarInterface registrar(*this); registrar.reserve(name); registrar.setContent(name, h256(u256(123))); @@ -385,7 +387,7 @@ BOOST_AUTO_TEST_CASE(disown) { deployRegistrar(); string name = "abcaoeguaoucaeoduceo"; - m_sender = Address(0x123); + RegistrarInterface registrar(*this); registrar.reserve(name); registrar.setContent(name, h256(u256(123))); @@ -394,11 +396,12 @@ BOOST_AUTO_TEST_CASE(disown) BOOST_CHECK_EQUAL(registrar.name(u160(124)), name); // someone else tries disowning - m_sender = Address(0x128); + sendEther(account(1), u256(10) * eth::ether); + m_sender = account(1); registrar.disown(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); + BOOST_CHECK_EQUAL(registrar.owner(name), account(0)); - m_sender = Address(0x123); + m_sender = account(0); registrar.disown(name); BOOST_CHECK_EQUAL(registrar.owner(name), 0); BOOST_CHECK_EQUAL(registrar.addr(name), 0); @@ -411,7 +414,7 @@ BOOST_AUTO_TEST_CASE(auction_simple) { deployRegistrar(); string name = "x"; - m_sender = Address(0x123); + RegistrarInterface registrar(*this); // initiate auction registrar.setNextValue(8); @@ -421,68 +424,70 @@ BOOST_AUTO_TEST_CASE(auction_simple) m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 10); // trigger auction again registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); + BOOST_CHECK_EQUAL(registrar.owner(name), m_sender); } BOOST_AUTO_TEST_CASE(auction_bidding) { deployRegistrar(); string name = "x"; - m_sender = Address(0x123); + + unsigned startTime = 0x776347e2; + m_rpc.test_modifyTimestamp(startTime); + RegistrarInterface registrar(*this); // initiate auction registrar.setNextValue(8); registrar.reserve(name); BOOST_CHECK_EQUAL(registrar.owner(name), 0); // overbid self - m_envInfo.setTimestamp(m_biddingTime - 10); + m_rpc.test_modifyTimestamp(startTime + m_biddingTime - 10); registrar.setNextValue(12); registrar.reserve(name); // another bid by someone else - m_sender = Address(0x124); - m_envInfo.setTimestamp(2 * m_biddingTime - 50); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); + m_rpc.test_modifyTimestamp(startTime + 2 * m_biddingTime - 50); registrar.setNextValue(13); registrar.reserve(name); BOOST_CHECK_EQUAL(registrar.owner(name), 0); // end auction by first bidder (which is not highest) trying to overbid again (too late) - m_sender = Address(0x123); - m_envInfo.setTimestamp(4 * m_biddingTime); + m_sender = account(0); + m_rpc.test_modifyTimestamp(startTime + 4 * m_biddingTime); registrar.setNextValue(20); registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x124); + BOOST_CHECK_EQUAL(registrar.owner(name), account(1)); } BOOST_AUTO_TEST_CASE(auction_renewal) { deployRegistrar(); + string name = "x"; RegistrarInterface registrar(*this); + size_t startTime = currentTimestamp(); // register name by auction - m_sender = Address(0x123); registrar.setNextValue(8); registrar.reserve(name); - m_envInfo.setTimestamp(4 * m_biddingTime); + m_rpc.test_modifyTimestamp(startTime + 4 * m_biddingTime); registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); + BOOST_CHECK_EQUAL(registrar.owner(name), m_sender); // try to re-register before interval end - m_sender = Address(0x222); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); + m_rpc.test_modifyTimestamp(currentTimestamp() + m_renewalInterval - 1); registrar.setNextValue(80); - m_envInfo.setTimestamp(m_envInfo.timestamp() + m_renewalInterval - 1); registrar.reserve(name); - m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime); - // if there is a bug in the renewal logic, this would transfer the ownership to 0x222, + m_rpc.test_modifyTimestamp(currentTimestamp() + m_biddingTime); + // if there is a bug in the renewal logic, this would transfer the ownership to account(1), // but if there is no bug, this will initiate the auction, albeit with a zero bid registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); + BOOST_CHECK_EQUAL(registrar.owner(name), account(0)); - m_envInfo.setTimestamp(m_envInfo.timestamp() + 2); registrar.setNextValue(80); registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); - m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 2); - registrar.reserve(name); - BOOST_CHECK_EQUAL(registrar.owner(name), 0x222); + BOOST_CHECK_EQUAL(registrar.owner(name), account(1)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index df521e7e..8575d1fc 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -157,11 +157,10 @@ BOOST_AUTO_TEST_CASE(reserve) // Test that reserving works and fee is taken into account. deployRegistrar(); string name[] = {"abc", "def", "ghi"}; - m_sender = Address(0x123); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name[0])) == encodeArgs()); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[0])) == encodeArgs(h256(0x123))); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[0])) == encodeArgs(h256(account(0), h256::AlignRight))); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee + 1, encodeDyn(name[1])) == encodeArgs()); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[1])) == encodeArgs(h256(0x123))); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[1])) == encodeArgs(h256(account(0), h256::AlignRight))); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee - 1, encodeDyn(name[2])) == encodeArgs()); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[2])) == encodeArgs(h256(0))); } @@ -171,13 +170,13 @@ BOOST_AUTO_TEST_CASE(double_reserve) // Test that it is not possible to re-reserve from a different address. deployRegistrar(); string name = "abc"; - m_sender = Address(0x123); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(0x123))); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight))); - m_sender = Address(0x124); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(0x123))); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight))); } BOOST_AUTO_TEST_CASE(properties) @@ -186,29 +185,35 @@ BOOST_AUTO_TEST_CASE(properties) deployRegistrar(); string names[] = {"abc", "def", "ghi"}; size_t addr = 0x9872543; + size_t count = 1; for (string const& name: names) { addr++; - size_t sender = addr + 10007; - m_sender = Address(sender); + m_sender = account(0); + sendEther(account(count), 10 * eth::ether); + m_sender = account(count); + Address owner = m_sender; // setting by sender works BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(sender))); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(owner, h256::AlignRight))); BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(addr), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr)); BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20, u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20)); BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90, u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90)); + count++; // but not by someone else - m_sender = Address(h256(addr + 10007 - 1)); - BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(sender)); + sendEther(account(count), 10 * eth::ether); + m_sender = account(count); + BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(owner, h256::AlignRight))); BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), addr + 1, u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr)); BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20 + 1, u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20)); BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90 + 1, u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90)); + count++; } } @@ -216,21 +221,19 @@ BOOST_AUTO_TEST_CASE(transfer) { deployRegistrar(); string name = "abc"; - m_sender = Address(0x123); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); - BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), u256(123), u256(name.length()), name) == encodeArgs()); + BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), h256(account(0), h256::AlignRight), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("transfer(string,address)", u256(0x40), u256(555), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(555))); - BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(u256(123))); + BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight))); } BOOST_AUTO_TEST_CASE(disown) { deployRegistrar(); string name = "abc"; - m_sender = Address(0x123); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); - BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), u256(123), u256(name.length()), name) == encodeArgs()); + BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), h256(account(0), h256::AlignRight), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(124), u256(name.length()), name) == encodeArgs()); BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), u256(125), u256(name.length()), name) == encodeArgs()); diff --git a/test/contracts/Wallet.cpp b/test/contracts/Wallet.cpp index 29fe5b8d..fbab2404 100644 --- a/test/contracts/Wallet.cpp +++ b/test/contracts/Wallet.cpp @@ -471,19 +471,22 @@ 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)); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs(true)); // now let the new owner add someone - m_sender = Address(0x12); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); 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); + m_sender = account(0); + sendEther(account(2), 10 * eth::ether); + m_sender = account(2); 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(account(1), h256::AlignRight)) == encodeArgs(true)); BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); } @@ -548,22 +551,27 @@ BOOST_AUTO_TEST_CASE(initial_owners) 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()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(2), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(3), h256::AlignRight)) == 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("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e"); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x12); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); + auto ophash = callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x13); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(2), 10 * eth::ether); + m_sender = account(2); + callContractFunction("confirm(bytes32)", ophash); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x14); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(3), 10 * eth::ether); + m_sender = account(3); + callContractFunction("confirm(bytes32)", ophash); // now it should go through BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 100); } @@ -571,9 +579,9 @@ BOOST_AUTO_TEST_CASE(multisig_value_transfer) BOOST_AUTO_TEST_CASE(revoke_addOwner) { deployWallet(); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(2), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(3), h256::AlignRight)) == encodeArgs()); // 4 owners, set required to 3 BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); // add a new owner @@ -581,16 +589,22 @@ BOOST_AUTO_TEST_CASE(revoke_addOwner) h256 opHash = sha3(FixedHash<4>(dev::sha3("addOwner(address)")).asBytes() + h256(0x33).asBytes()); BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs()); BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false)); - m_sender = Address(0x12); + m_sender = account(0); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs()); BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false)); // revoke one confirmation m_sender = deployer; BOOST_REQUIRE(callContractFunction("revoke(bytes32)", opHash) == encodeArgs()); - m_sender = Address(0x13); + m_sender = account(0); + sendEther(account(2), 10 * eth::ether); + m_sender = account(2); BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs()); BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(false)); - m_sender = Address(0x14); + m_sender = account(0); + sendEther(account(3), 10 * eth::ether); + m_sender = account(3); BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x33)) == encodeArgs()); BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x33)) == encodeArgs(true)); } @@ -598,28 +612,35 @@ BOOST_AUTO_TEST_CASE(revoke_addOwner) BOOST_AUTO_TEST_CASE(revoke_transaction) { 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()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(2), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(3), h256::AlignRight)) == encodeArgs()); // 4 owners, set required to 3 BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); // create a transaction Address deployer = m_sender; - h256 opHash("6244b4fa93f73e09db0ae52750095ca0364a76b72bc01723c97011fcb876cc9e"); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x12); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); + auto opHash = callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x13); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(2), 10 * eth::ether); + m_sender = account(2); + callContractFunction("confirm(bytes32)", opHash); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x12); + m_sender = account(0); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); BOOST_REQUIRE(callContractFunction("revoke(bytes32)", opHash) == encodeArgs()); m_sender = deployer; - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + callContractFunction("confirm(bytes32)", opHash); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x14); - BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); + m_sender = account(0); + sendEther(account(3), 10 * eth::ether); + m_sender = account(3); + callContractFunction("confirm(bytes32)", opHash); // now it should go through BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 100); } @@ -630,29 +651,33 @@ BOOST_AUTO_TEST_CASE(daylimit) BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(0))); BOOST_REQUIRE(callContractFunction("setDailyLimit(uint256)", h256(100)) == encodeArgs()); BOOST_REQUIRE(callContractFunction("m_dailyLimit()") == encodeArgs(u256(100))); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); - BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(1), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(2), h256::AlignRight)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(account(3), h256::AlignRight)) == encodeArgs()); // 4 owners, set required to 3 BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); // try to send tx over daylimit BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); - m_sender = Address(0x12); + sendEther(account(1), 10 * eth::ether); + m_sender = account(1); BOOST_REQUIRE( callContractFunction("execute(address,uint256,bytes)", h256(0x05), 150, 0x60, 0x00) != encodeArgs(u256(0)) ); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); // try to send tx under daylimit by stranger - m_sender = Address(0x77); + m_sender = account(0); + sendEther(account(4), 10 * eth::ether); + m_sender = account(4); BOOST_REQUIRE( callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == encodeArgs(u256(0)) ); BOOST_CHECK_EQUAL(balanceAt(Address(0x05)), 0); // now send below limit by owner - m_sender = Address(0x12); + m_sender = account(0); + sendEther(account(1), 10 * eth::ether); BOOST_REQUIRE( callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == encodeArgs(u256(0)) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 8689d61e..8dcc878e 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1325,10 +1325,10 @@ BOOST_AUTO_TEST_CASE(blockchain) " blockNumber = block.number;\n" " }\n" "}\n"; - m_envInfo.setAuthor(Address(0x123)); - m_envInfo.setNumber(7); + BOOST_CHECK(m_rpc.rpcCall("miner_setEtherbase", {"\"0x1212121212121212121212121212121212121212\""}).asBool() == true); + m_rpc.test_mineBlocks(5); compileAndRun(sourceCode, 27); - BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0x123, 7)); + BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, u256("0x1212121212121212121212121212121212121212"), 7)); } BOOST_AUTO_TEST_CASE(msg_sig) @@ -1368,9 +1368,9 @@ BOOST_AUTO_TEST_CASE(now) " val = now;\n" " }\n" "}\n"; - m_envInfo.setTimestamp(9); + m_rpc.test_modifyTimestamp(0x776347e2); compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("someInfo()") == encodeArgs(true, 9)); + BOOST_CHECK(callContractFunction("someInfo()") == encodeArgs(true, 0x776347e3)); } BOOST_AUTO_TEST_CASE(type_conversions_cleanup) @@ -2462,8 +2462,7 @@ BOOST_AUTO_TEST_CASE(use_std_lib) contract Icarus is mortal { } )"; m_addStandardSources = true; - u256 amount(130); - u160 address(23); + u256 amount(130 * eth::ether); compileAndRun(sourceCode, amount, "Icarus"); u256 balanceBefore = balanceAt(m_sender); BOOST_CHECK(callContractFunction("kill()") == bytes()); @@ -2567,7 +2566,7 @@ BOOST_AUTO_TEST_CASE(event) BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(value))); BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 3); BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(address,bytes32,uint256)"))); - BOOST_CHECK_EQUAL(m_logs[0].topics[1], h256(m_sender)); + BOOST_CHECK_EQUAL(m_logs[0].topics[1], h256(m_sender, h256::AlignRight)); BOOST_CHECK_EQUAL(m_logs[0].topics[2], h256(id)); } } @@ -2624,7 +2623,7 @@ BOOST_AUTO_TEST_CASE(event_anonymous_with_topics) BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); BOOST_CHECK(m_logs[0].data == encodeArgs("abc")); BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 4); - BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(m_sender)); + BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(m_sender, h256::AlignRight)); BOOST_CHECK_EQUAL(m_logs[0].topics[1], h256(id)); BOOST_CHECK_EQUAL(m_logs[0].topics[2], h256(value)); BOOST_CHECK_EQUAL(m_logs[0].topics[3], h256(2)); diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index abb564db..efd3a8b8 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -45,7 +45,8 @@ string getIPCSocketPath() } } if (ipcPath.empty()) - ipcPath = getenv("ETH_TEST_IPC"); + if (auto path = getenv("ETH_TEST_IPC")) + ipcPath = path; if (ipcPath.empty()) BOOST_FAIL("ERROR: ipcPath not set! (use --ipc )"); return ipcPath; @@ -86,7 +87,6 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 m_contractAddress = Address(receipt.contractAddress); BOOST_REQUIRE(m_contractAddress); string code = m_rpc.eth_getCode(receipt.contractAddress, "latest"); - BOOST_REQUIRE(code.size() > 2); m_output = fromHex(code, WhenError::Throw); } @@ -103,6 +103,31 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 } } +void ExecutionFramework::sendEther(Address const& _to, u256 const& _value) +{ + RPCSession::TransactionData d; + d.data = "0x"; + d.from = "0x" + toString(m_sender); + d.gas = toHex(m_gas, HexPrefix::Add); + d.gasPrice = toHex(m_gasPrice, HexPrefix::Add); + d.value = toHex(_value, HexPrefix::Add); + d.to = dev::toString(_to); + + string txHash = m_rpc.eth_sendTransaction(d); + m_rpc.test_mineBlocks(1); +} + +size_t ExecutionFramework::currentTimestamp() +{ + auto latestBlock = m_rpc.rpcCall("eth_getBlockByNumber", {"\"latest\"", "false"}); + return size_t(u256(latestBlock.get("timestamp", "invalid").asString())); +} + +Address ExecutionFramework::account(size_t _i) +{ + return Address(m_rpc.accountCreateIfNotExists(_i)); +} + bool ExecutionFramework::addressHasCode(Address const& _addr) { string code = m_rpc.eth_getCode(toString(_addr), "latest"); diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 42b22c82..f06aeff7 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -250,6 +250,11 @@ private: protected: void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0); + void sendEther(Address const& _to, u256 const& _value); + size_t currentTimestamp(); + + /// @returns the (potentially newly created) _ith address. + Address account(size_t _i); u256 balanceAt(Address const& _addr); bool storageEmpty(Address const& _addr); -- cgit v1.2.3 From c0bbd1cfe592144df5769f0313a2b0d418ef720f Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 21 Jun 2016 01:39:06 +0200 Subject: Some more test fixes. --- test/contracts/AuctionRegistrar.cpp | 2 +- test/contracts/FixedFeeRegistrar.cpp | 7 ++++--- test/libsolidity/SolidityExecutionFramework.cpp | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index 0eee42a4..f94cc935 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -421,7 +421,7 @@ BOOST_AUTO_TEST_CASE(auction_simple) registrar.reserve(name); BOOST_CHECK_EQUAL(registrar.owner(name), 0); // "wait" until auction end - m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 10); + m_rpc.test_modifyTimestamp(currentTimestamp() + m_biddingTime + 10); // trigger auction again registrar.reserve(name); BOOST_CHECK_EQUAL(registrar.owner(name), m_sender); diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index 8575d1fc..402d53ea 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(double_reserve) BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight))); - sendEther(account(1), 10 * eth::ether); + sendEther(account(1), 100 * eth::ether); m_sender = account(1); BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs()); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight))); @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(properties) { addr++; m_sender = account(0); - sendEther(account(count), 10 * eth::ether); + sendEther(account(count), 100 * eth::ether); m_sender = account(count); Address owner = m_sender; // setting by sender works @@ -204,7 +204,8 @@ BOOST_AUTO_TEST_CASE(properties) BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90)); count++; // but not by someone else - sendEther(account(count), 10 * eth::ether); + m_sender = account(0); + sendEther(account(count), 100 * eth::ether); m_sender = account(count); BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(owner, h256::AlignRight))); BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), addr + 1, u256(name.length()), name) == encodeArgs()); diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index efd3a8b8..da9cf5f1 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -48,7 +48,7 @@ string getIPCSocketPath() if (auto path = getenv("ETH_TEST_IPC")) ipcPath = path; if (ipcPath.empty()) - BOOST_FAIL("ERROR: ipcPath not set! (use --ipc )"); + BOOST_FAIL("ERROR: ipcPath not set! (use --ipc or the environment variable ETH_TEST_IPC)"); return ipcPath; } -- cgit v1.2.3 From 05e5bdf780cba4d198066d6f26353fd25d8ff553 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 21 Jun 2016 18:13:07 +0200 Subject: Fixes for gas tests. --- test/libsolidity/GasMeter.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp index 1ff2e0f9..fc7a033f 100644 --- a/test/libsolidity/GasMeter.cpp +++ b/test/libsolidity/GasMeter.cpp @@ -66,7 +66,11 @@ public: PathGasMeter meter(*m_compiler.assemblyItems()); GasMeter::GasConsumption gas = meter.estimateMax(0, state); u256 bytecodeSize(m_compiler.runtimeObject().bytecode.size()); + // costs for deployment gas += bytecodeSize * schedule.createDataGas; + // costs for transaction + gas += gasForTransaction(m_compiler.object().bytecode, true); + BOOST_REQUIRE(!gas.isInfinite); BOOST_CHECK(gas.value == m_gasUsed); } @@ -76,14 +80,16 @@ public: void testRunTimeGas(string const& _sig, vector _argumentVariants) { u256 gasUsed = 0; + GasMeter::GasConsumption gas; FixedHash<4> hash(dev::sha3(_sig)); for (bytes const& arguments: _argumentVariants) { sendMessage(hash.asBytes() + arguments, false, 0); gasUsed = max(gasUsed, m_gasUsed); + gas = max(gas, gasForTransaction(hash.asBytes() + arguments, false)); } - GasMeter::GasConsumption gas = GasEstimator::functionalEstimation( + gas += GasEstimator::functionalEstimation( *m_compiler.runtimeAssemblyItems(), _sig ); @@ -91,6 +97,15 @@ public: BOOST_CHECK(gas.value == m_gasUsed); } + static GasMeter::GasConsumption gasForTransaction(bytes const& _data, bool _isCreation) + { + EVMSchedule schedule; + GasMeter::GasConsumption gas = _isCreation ? schedule.txCreateGas : schedule.txGas; + for (auto i: _data) + gas += i != 0 ? schedule.txDataNonZeroGas : schedule.txDataZeroGas; + return gas; + } + protected: map m_gasCosts; }; -- cgit v1.2.3 From 67d1e4783a760bf3c8f6f97e0fc53f0e1c24d2d8 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Jun 2016 23:47:35 +0200 Subject: Rename IPCSocket and removed unused code in TestHelper. --- test/IPCSocket.cpp | 262 -------------------------- test/IPCSocket.h | 108 ----------- test/RPCSession.cpp | 262 ++++++++++++++++++++++++++ test/RPCSession.h | 108 +++++++++++ test/TestHelper.cpp | 59 ------ test/TestHelper.h | 27 --- test/libsolidity/SolidityExecutionFramework.h | 2 +- 7 files changed, 371 insertions(+), 457 deletions(-) delete mode 100644 test/IPCSocket.cpp delete mode 100644 test/IPCSocket.h create mode 100644 test/RPCSession.cpp create mode 100644 test/RPCSession.h delete mode 100644 test/TestHelper.cpp diff --git a/test/IPCSocket.cpp b/test/IPCSocket.cpp deleted file mode 100644 index 832875c0..00000000 --- a/test/IPCSocket.cpp +++ /dev/null @@ -1,262 +0,0 @@ -/* - 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 . -*/ -/** @file IPCSocket.cpp - * @author Dimtiry Khokhlov - * @date 2016 - */ - -#include -#include -#include -#include -#include -#include -#include "IPCSocket.h" - -using namespace std; -using namespace dev; - -IPCSocket::IPCSocket(string const& _path): m_path(_path) -{ - if (_path.length() >= sizeof(sockaddr_un::sun_path)) - BOOST_FAIL("Error opening IPC: socket path is too long!"); - - struct sockaddr_un saun; - saun.sun_family = AF_UNIX; - strcpy(saun.sun_path, _path.c_str()); - - if ((m_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) - BOOST_FAIL("Error creating IPC socket object"); - - int len = sizeof(saun.sun_family) + strlen(saun.sun_path); - - if (connect(m_socket, reinterpret_cast(&saun), len) < 0) - BOOST_FAIL("Error connecting to IPC socket: " << _path); - - m_fp = fdopen(m_socket, "r"); -} - -string IPCSocket::sendRequest(string const& _req) -{ - send(m_socket, _req.c_str(), _req.length(), 0); - - char c; - string response; - while ((c = fgetc(m_fp)) != EOF) - { - if (c != '\n') - response += c; - else - break; - } - return response; -} - -RPCSession& RPCSession::instance(const string& _path) -{ - static RPCSession session(_path); - BOOST_REQUIRE_EQUAL(session.m_ipcSocket.path(), _path); - return session; -} - -string RPCSession::eth_getCode(string const& _address, string const& _blockNumber) -{ - return rpcCall("eth_getCode", { quote(_address), quote(_blockNumber) }).asString(); -} - -RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string const& _transactionHash) -{ - TransactionReceipt receipt; - Json::Value const result = rpcCall("eth_getTransactionReceipt", { quote(_transactionHash) }); - BOOST_REQUIRE(!result.isNull()); - receipt.gasUsed = result["gasUsed"].asString(); - receipt.contractAddress = result["contractAddress"].asString(); - for (auto const& log: result["logs"]) - { - LogEntry entry; - entry.address = log["address"].asString(); - entry.data = log["data"].asString(); - for (auto const& topic: log["topics"]) - entry.topics.push_back(topic.asString()); - receipt.logEntries.push_back(entry); - } - return receipt; -} - -string RPCSession::eth_sendTransaction(TransactionData const& _td) -{ - return rpcCall("eth_sendTransaction", { _td.toJson() }).asString(); -} - -string RPCSession::eth_call(TransactionData const& _td, string const& _blockNumber) -{ - return rpcCall("eth_call", { _td.toJson(), quote(_blockNumber) }).asString(); -} - -string RPCSession::eth_sendTransaction(string const& _transaction) -{ - return rpcCall("eth_sendTransaction", { _transaction }).asString(); -} - -string RPCSession::eth_getBalance(string const& _address, string const& _blockNumber) -{ - string address = (_address.length() == 20) ? "0x" + _address : _address; - return rpcCall("eth_getBalance", { quote(address), quote(_blockNumber) }).asString(); -} - -string RPCSession::eth_getStorageRoot(string const& _address, string const& _blockNumber) -{ - string address = (_address.length() == 20) ? "0x" + _address : _address; - return rpcCall("eth_getStorageRoot", { quote(address), quote(_blockNumber) }).asString(); -} - -void RPCSession::personal_unlockAccount(string const& _address, string const& _password, int _duration) -{ - rpcCall("personal_unlockAccount", { quote(_address), quote(_password), to_string(_duration) }); -} - -string RPCSession::personal_newAccount(string const& _password) -{ - return rpcCall("personal_newAccount", { quote(_password) }).asString(); -} - -void RPCSession::test_setChainParams(vector const& _accounts) -{ - static std::string const c_configString = R"( - { - "sealEngine": "NoProof", - "params": { - "accountStartNonce": "0x", - "maximumExtraDataSize": "0x1000000", - "blockReward": "0x", - "allowFutureBlocks": "1" - }, - "genesis": { - "author": "0000000000000010000000000000000000000000", - "timestamp": "0x00", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "extraData": "0x", - "gasLimit": "0x1000000000000" - }, - "accounts": { - "0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, - "0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, - "0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, - "0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } } - } - } - )"; - - Json::Value config; - BOOST_REQUIRE(Json::Reader().parse(c_configString, config)); - for (auto const& account: _accounts) - config["accounts"][account]["wei"] = "0x100000000000000000000000000000000000000000"; - test_setChainParams(Json::FastWriter().write(config)); -} - -void RPCSession::test_setChainParams(string const& _config) -{ - rpcCall("test_setChainParams", { _config }); -} - -void RPCSession::test_rewindToBlock(size_t _blockNr) -{ - rpcCall("test_rewindToBlock", { to_string(_blockNr) }); -} - -void RPCSession::test_mineBlocks(int _number) -{ - u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); - rpcCall("test_mineBlocks", { to_string(_number) }, true); - - //@TODO do not use polling - but that would probably need a change to the test client - for (size_t polls = 0; polls < 100; ++polls) - { - if (fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())) >= startBlock + _number) - return; - std::this_thread::sleep_for(chrono::milliseconds(10)); //it does not work faster then 10 ms - } - - BOOST_FAIL("Error in test_mineBlocks: block mining timeout!"); -} - -void RPCSession::test_modifyTimestamp(size_t _timestamp) -{ - rpcCall("test_modifyTimestamp", { to_string(_timestamp) }); -} - -Json::Value RPCSession::rpcCall(string const& _methodName, vector const& _args, bool _canFail) -{ - string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":["; - for (size_t i = 0; i < _args.size(); ++i) - { - request += _args[i]; - if (i + 1 != _args.size()) - request += ", "; - } - - request += "],\"id\":" + to_string(m_rpcSequence) + "}"; - ++m_rpcSequence; - - //cout << "Request: " << request << endl; - string reply = m_ipcSocket.sendRequest(request); - //cout << "Reply: " << reply << endl; - - Json::Value result; - Json::Reader().parse(reply, result, false); - - if (result.isMember("error")) - { - if (_canFail) - return Json::Value(); - - BOOST_FAIL("Error on JSON-RPC call: " + result["error"]["message"].asString()); - } - return result["result"]; -} - -string const& RPCSession::accountCreateIfNotExists(size_t _id) -{ - if (_id >= m_accounts.size()) - { - m_accounts.push_back(personal_newAccount("")); - personal_unlockAccount(m_accounts.back(), "", 100000); - } - return m_accounts[_id]; -} - -RPCSession::RPCSession(const string& _path): - m_ipcSocket(_path) -{ - string account = personal_newAccount(""); - personal_unlockAccount(account, "", 100000); - m_accounts.push_back(account); - test_setChainParams(m_accounts); -} - -string RPCSession::TransactionData::toJson() const -{ - Json::Value json; - json["from"] = (from.length() == 20) ? "0x" + from : from; - json["to"] = (to.length() == 20 || to == "") ? "0x" + to : to; - json["gas"] = gas; - json["gasprice"] = gasPrice; - json["value"] = value; - json["data"] = data; - return Json::FastWriter().write(json); - -} diff --git a/test/IPCSocket.h b/test/IPCSocket.h deleted file mode 100644 index 040eb2c8..00000000 --- a/test/IPCSocket.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - 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 . -*/ -/** @file IPCSocket.h - * @author Dimtiry Khokhlov - * @date 2016 - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -class IPCSocket: public boost::noncopyable -{ -public: - IPCSocket(std::string const& _path); - std::string sendRequest(std::string const& _req); - ~IPCSocket() { close(m_socket); fclose(m_fp); } - - std::string const& path() const { return m_path; } - -private: - FILE *m_fp; - std::string m_path; - int m_socket; - -}; - -class RPCSession: public boost::noncopyable -{ -public: - struct TransactionData - { - std::string from; - std::string to; - std::string gas; - std::string gasPrice; - std::string value; - std::string data; - - std::string toJson() const; - }; - - struct LogEntry { - std::string address; - std::vector topics; - std::string data; - }; - - struct TransactionReceipt - { - std::string gasUsed; - std::string contractAddress; - std::vector logEntries; - }; - - static RPCSession& instance(std::string const& _path); - - std::string eth_getCode(std::string const& _address, std::string const& _blockNumber); - std::string eth_call(TransactionData const& _td, std::string const& _blockNumber); - TransactionReceipt eth_getTransactionReceipt(std::string const& _transactionHash); - std::string eth_sendTransaction(TransactionData const& _transactionData); - std::string eth_sendTransaction(std::string const& _transaction); - std::string eth_getBalance(std::string const& _address, std::string const& _blockNumber); - std::string eth_getStorageRoot(std::string const& _address, std::string const& _blockNumber); - std::string personal_newAccount(std::string const& _password); - void personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration); - void test_setChainParams(std::vector const& _accounts); - void test_setChainParams(std::string const& _config); - void test_rewindToBlock(size_t _blockNr); - void test_modifyTimestamp(size_t _timestamp); - void test_mineBlocks(int _number); - Json::Value rpcCall(std::string const& _methodName, std::vector const& _args = std::vector(), bool _canFail = false); - - std::string const& account(size_t _id) const { return m_accounts.at(_id); } - std::string const& accountCreateIfNotExists(size_t _id); - -private: - RPCSession(std::string const& _path); - - inline std::string quote(std::string const& _arg) { return "\"" + _arg + "\""; } - /// Parse std::string replacing keywords to values - void parseString(std::string& _string, std::map const& _varMap); - - IPCSocket m_ipcSocket; - size_t m_rpcSequence = 1; - - std::vector m_accounts; -}; - diff --git a/test/RPCSession.cpp b/test/RPCSession.cpp new file mode 100644 index 00000000..01118cca --- /dev/null +++ b/test/RPCSession.cpp @@ -0,0 +1,262 @@ +/* + 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 . +*/ +/** @file RPCSession.cpp + * @author Dimtiry Khokhlov + * @date 2016 + */ + +#include +#include +#include +#include +#include +#include +#include "RPCSession.h" + +using namespace std; +using namespace dev; + +IPCSocket::IPCSocket(string const& _path): m_path(_path) +{ + if (_path.length() >= sizeof(sockaddr_un::sun_path)) + BOOST_FAIL("Error opening IPC: socket path is too long!"); + + struct sockaddr_un saun; + saun.sun_family = AF_UNIX; + strcpy(saun.sun_path, _path.c_str()); + + if ((m_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + BOOST_FAIL("Error creating IPC socket object"); + + int len = sizeof(saun.sun_family) + strlen(saun.sun_path); + + if (connect(m_socket, reinterpret_cast(&saun), len) < 0) + BOOST_FAIL("Error connecting to IPC socket: " << _path); + + m_fp = fdopen(m_socket, "r"); +} + +string IPCSocket::sendRequest(string const& _req) +{ + send(m_socket, _req.c_str(), _req.length(), 0); + + char c; + string response; + while ((c = fgetc(m_fp)) != EOF) + { + if (c != '\n') + response += c; + else + break; + } + return response; +} + +RPCSession& RPCSession::instance(const string& _path) +{ + static RPCSession session(_path); + BOOST_REQUIRE_EQUAL(session.m_ipcSocket.path(), _path); + return session; +} + +string RPCSession::eth_getCode(string const& _address, string const& _blockNumber) +{ + return rpcCall("eth_getCode", { quote(_address), quote(_blockNumber) }).asString(); +} + +RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string const& _transactionHash) +{ + TransactionReceipt receipt; + Json::Value const result = rpcCall("eth_getTransactionReceipt", { quote(_transactionHash) }); + BOOST_REQUIRE(!result.isNull()); + receipt.gasUsed = result["gasUsed"].asString(); + receipt.contractAddress = result["contractAddress"].asString(); + for (auto const& log: result["logs"]) + { + LogEntry entry; + entry.address = log["address"].asString(); + entry.data = log["data"].asString(); + for (auto const& topic: log["topics"]) + entry.topics.push_back(topic.asString()); + receipt.logEntries.push_back(entry); + } + return receipt; +} + +string RPCSession::eth_sendTransaction(TransactionData const& _td) +{ + return rpcCall("eth_sendTransaction", { _td.toJson() }).asString(); +} + +string RPCSession::eth_call(TransactionData const& _td, string const& _blockNumber) +{ + return rpcCall("eth_call", { _td.toJson(), quote(_blockNumber) }).asString(); +} + +string RPCSession::eth_sendTransaction(string const& _transaction) +{ + return rpcCall("eth_sendTransaction", { _transaction }).asString(); +} + +string RPCSession::eth_getBalance(string const& _address, string const& _blockNumber) +{ + string address = (_address.length() == 20) ? "0x" + _address : _address; + return rpcCall("eth_getBalance", { quote(address), quote(_blockNumber) }).asString(); +} + +string RPCSession::eth_getStorageRoot(string const& _address, string const& _blockNumber) +{ + string address = (_address.length() == 20) ? "0x" + _address : _address; + return rpcCall("eth_getStorageRoot", { quote(address), quote(_blockNumber) }).asString(); +} + +void RPCSession::personal_unlockAccount(string const& _address, string const& _password, int _duration) +{ + rpcCall("personal_unlockAccount", { quote(_address), quote(_password), to_string(_duration) }); +} + +string RPCSession::personal_newAccount(string const& _password) +{ + return rpcCall("personal_newAccount", { quote(_password) }).asString(); +} + +void RPCSession::test_setChainParams(vector const& _accounts) +{ + static std::string const c_configString = R"( + { + "sealEngine": "NoProof", + "params": { + "accountStartNonce": "0x", + "maximumExtraDataSize": "0x1000000", + "blockReward": "0x", + "allowFutureBlocks": "1" + }, + "genesis": { + "author": "0000000000000010000000000000000000000000", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x", + "gasLimit": "0x1000000000000" + }, + "accounts": { + "0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, + "0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, + "0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, + "0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } } + } + } + )"; + + Json::Value config; + BOOST_REQUIRE(Json::Reader().parse(c_configString, config)); + for (auto const& account: _accounts) + config["accounts"][account]["wei"] = "0x100000000000000000000000000000000000000000"; + test_setChainParams(Json::FastWriter().write(config)); +} + +void RPCSession::test_setChainParams(string const& _config) +{ + rpcCall("test_setChainParams", { _config }); +} + +void RPCSession::test_rewindToBlock(size_t _blockNr) +{ + rpcCall("test_rewindToBlock", { to_string(_blockNr) }); +} + +void RPCSession::test_mineBlocks(int _number) +{ + u256 startBlock = fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())); + rpcCall("test_mineBlocks", { to_string(_number) }, true); + + //@TODO do not use polling - but that would probably need a change to the test client + for (size_t polls = 0; polls < 100; ++polls) + { + if (fromBigEndian(fromHex(rpcCall("eth_blockNumber").asString())) >= startBlock + _number) + return; + std::this_thread::sleep_for(chrono::milliseconds(10)); //it does not work faster then 10 ms + } + + BOOST_FAIL("Error in test_mineBlocks: block mining timeout!"); +} + +void RPCSession::test_modifyTimestamp(size_t _timestamp) +{ + rpcCall("test_modifyTimestamp", { to_string(_timestamp) }); +} + +Json::Value RPCSession::rpcCall(string const& _methodName, vector const& _args, bool _canFail) +{ + string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":["; + for (size_t i = 0; i < _args.size(); ++i) + { + request += _args[i]; + if (i + 1 != _args.size()) + request += ", "; + } + + request += "],\"id\":" + to_string(m_rpcSequence) + "}"; + ++m_rpcSequence; + + //cout << "Request: " << request << endl; + string reply = m_ipcSocket.sendRequest(request); + //cout << "Reply: " << reply << endl; + + Json::Value result; + Json::Reader().parse(reply, result, false); + + if (result.isMember("error")) + { + if (_canFail) + return Json::Value(); + + BOOST_FAIL("Error on JSON-RPC call: " + result["error"]["message"].asString()); + } + return result["result"]; +} + +string const& RPCSession::accountCreateIfNotExists(size_t _id) +{ + if (_id >= m_accounts.size()) + { + m_accounts.push_back(personal_newAccount("")); + personal_unlockAccount(m_accounts.back(), "", 100000); + } + return m_accounts[_id]; +} + +RPCSession::RPCSession(const string& _path): + m_ipcSocket(_path) +{ + string account = personal_newAccount(""); + personal_unlockAccount(account, "", 100000); + m_accounts.push_back(account); + test_setChainParams(m_accounts); +} + +string RPCSession::TransactionData::toJson() const +{ + Json::Value json; + json["from"] = (from.length() == 20) ? "0x" + from : from; + json["to"] = (to.length() == 20 || to == "") ? "0x" + to : to; + json["gas"] = gas; + json["gasprice"] = gasPrice; + json["value"] = value; + json["data"] = data; + return Json::FastWriter().write(json); + +} diff --git a/test/RPCSession.h b/test/RPCSession.h new file mode 100644 index 00000000..4ec21def --- /dev/null +++ b/test/RPCSession.h @@ -0,0 +1,108 @@ +/* + 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 . +*/ +/** @file RPCSession.h + * @author Dimtiry Khokhlov + * @date 2016 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +class IPCSocket: public boost::noncopyable +{ +public: + IPCSocket(std::string const& _path); + std::string sendRequest(std::string const& _req); + ~IPCSocket() { close(m_socket); fclose(m_fp); } + + std::string const& path() const { return m_path; } + +private: + FILE *m_fp; + std::string m_path; + int m_socket; + +}; + +class RPCSession: public boost::noncopyable +{ +public: + struct TransactionData + { + std::string from; + std::string to; + std::string gas; + std::string gasPrice; + std::string value; + std::string data; + + std::string toJson() const; + }; + + struct LogEntry { + std::string address; + std::vector topics; + std::string data; + }; + + struct TransactionReceipt + { + std::string gasUsed; + std::string contractAddress; + std::vector logEntries; + }; + + static RPCSession& instance(std::string const& _path); + + std::string eth_getCode(std::string const& _address, std::string const& _blockNumber); + std::string eth_call(TransactionData const& _td, std::string const& _blockNumber); + TransactionReceipt eth_getTransactionReceipt(std::string const& _transactionHash); + std::string eth_sendTransaction(TransactionData const& _transactionData); + std::string eth_sendTransaction(std::string const& _transaction); + std::string eth_getBalance(std::string const& _address, std::string const& _blockNumber); + std::string eth_getStorageRoot(std::string const& _address, std::string const& _blockNumber); + std::string personal_newAccount(std::string const& _password); + void personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration); + void test_setChainParams(std::vector const& _accounts); + void test_setChainParams(std::string const& _config); + void test_rewindToBlock(size_t _blockNr); + void test_modifyTimestamp(size_t _timestamp); + void test_mineBlocks(int _number); + Json::Value rpcCall(std::string const& _methodName, std::vector const& _args = std::vector(), bool _canFail = false); + + std::string const& account(size_t _id) const { return m_accounts.at(_id); } + std::string const& accountCreateIfNotExists(size_t _id); + +private: + RPCSession(std::string const& _path); + + inline std::string quote(std::string const& _arg) { return "\"" + _arg + "\""; } + /// Parse std::string replacing keywords to values + void parseString(std::string& _string, std::map const& _varMap); + + IPCSocket m_ipcSocket; + size_t m_rpcSequence = 1; + + std::vector m_accounts; +}; + diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp deleted file mode 100644 index 79242f83..00000000 --- a/test/TestHelper.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - 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 . -*/ -/** @file TestHelper.cpp - * @author Marko Simovic - * @date 2014 - */ - -#include "TestHelper.h" - -using namespace std; - -namespace dev -{ -namespace test -{ - -namespace -{ - Listener* g_listener; -} - -void Listener::registerListener(Listener& _listener) -{ - g_listener = &_listener; -} - -void Listener::notifySuiteStarted(std::string const& _name) -{ - if (g_listener) - g_listener->suiteStarted(_name); -} - -void Listener::notifyTestStarted(std::string const& _name) -{ - if (g_listener) - g_listener->testStarted(_name); -} - -void Listener::notifyTestFinished() -{ - if (g_listener) - g_listener->testFinished(); -} - -} } // namespaces diff --git a/test/TestHelper.h b/test/TestHelper.h index 96678007..2d08d62c 100644 --- a/test/TestHelper.h +++ b/test/TestHelper.h @@ -102,32 +102,5 @@ namespace test } \ while (0) -/// Allows observing test execution process. -/// This class also provides methods for registering and notifying the listener -class Listener -{ -public: - virtual ~Listener() = default; - - virtual void suiteStarted(std::string const&) {} - virtual void testStarted(std::string const& _name) = 0; - virtual void testFinished() = 0; - - static void registerListener(Listener& _listener); - static void notifySuiteStarted(std::string const& _name); - static void notifyTestStarted(std::string const& _name); - static void notifyTestFinished(); - - /// Test started/finished notification RAII helper - class ExecTimeGuard - { - public: - ExecTimeGuard(std::string const& _testName) { notifyTestStarted(_testName); } - ~ExecTimeGuard() { notifyTestFinished(); } - ExecTimeGuard(ExecTimeGuard const&) = delete; - ExecTimeGuard& operator=(ExecTimeGuard) = delete; - }; -}; - } } diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index f06aeff7..2b589498 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -26,7 +26,7 @@ #include #include #include "../TestHelper.h" -#include "../IPCSocket.h" +#include "../RPCSession.h" #include #include #include -- cgit v1.2.3 From 7369b5e4bb746ede2c5e0619692504c82a490ea3 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Jun 2016 23:50:39 +0200 Subject: Test via ipc on travis. --- fetch_umbrella_build_and_test.sh | 12 +++++++++++- install_dependencies.sh | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/fetch_umbrella_build_and_test.sh b/fetch_umbrella_build_and_test.sh index 5164bcf6..ead54fea 100755 --- a/fetch_umbrella_build_and_test.sh +++ b/fetch_umbrella_build_and_test.sh @@ -27,4 +27,14 @@ cmake .. -DGUI=0 -DCMAKE_BUILD_TYPE=$TRAVIS_BUILD_TYPE $OPTIONS make lllc solc soljson soltest -./solidity/test/soltest +# Test runs disabled for macos for now, +# we need to find a way to install eth. +if [[ "$OSTYPE" != "darwin"* ]] +then + eth --test -d /tmp/test & + while [ ! -S /tmp/test/geth.ipc ]; do sleep 2; done + + ./solidity/test/soltest --ipc /tmp/test/geth.ipc + pkill eth +fi + diff --git a/install_dependencies.sh b/install_dependencies.sh index 8f0332e0..737851b5 100755 --- a/install_dependencies.sh +++ b/install_dependencies.sh @@ -65,7 +65,8 @@ elif [[ "$OSTYPE" == "linux-gnu" ]]; then libmicrohttpd-dev \ libminiupnpc-dev \ libz-dev \ - opencl-headers + opencl-headers \ + eth # The exception is libjson-rpc-cpp, which we have to build from source for # reliable results. The only binaries available for this package are those -- cgit v1.2.3 From 272a9d94d2d42cbaf3ac26bd72132bbc74d6e22c Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Jun 2016 23:57:02 +0200 Subject: Re-enable registrar tests. --- test/contracts/FixedFeeRegistrar.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index 402d53ea..dec29b37 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -137,7 +137,7 @@ protected: s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode)); } sendMessage(*s_compiledRegistrar, true); - //BOOST_REQUIRE(!m_output.empty()); + BOOST_REQUIRE(!m_output.empty()); } u256 const m_fee = u256("69000000000000000000"); }; @@ -149,7 +149,7 @@ BOOST_FIXTURE_TEST_SUITE(SolidityFixedFeeRegistrar, RegistrarTestFramework) BOOST_AUTO_TEST_CASE(creation) { - //deployRegistrar(); + deployRegistrar(); } BOOST_AUTO_TEST_CASE(reserve) -- cgit v1.2.3 From ccba76384510ea3aa3565029a8a68ca8f777a4ea Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 29 Jun 2016 00:14:22 +0200 Subject: Fix test contracts. --- test/contracts/AuctionRegistrar.cpp | 14 ++++++++++---- test/contracts/FixedFeeRegistrar.cpp | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index f94cc935..9aa47189 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -124,13 +124,19 @@ contract GlobalRegistrar is Registrar, AuctionSystem { function onAuctionEnd(string _name) internal { var auction = m_auctions[_name]; var record = m_toRecord[_name]; - if (record.owner != 0) - record.owner.send(auction.sumOfBids - auction.highestBid / 100); - else - auction.highestBidder.send(auction.highestBid - auction.secondHighestBid); + var previousOwner = record.owner; record.renewalDate = now + c_renewalInterval; record.owner = auction.highestBidder; Changed(_name); + if (previousOwner != 0) { + if (!record.owner.send(auction.sumOfBids - auction.highestBid / 100)) + throw; + } + else + { + if (!auction.highestBidder.send(auction.highestBid - auction.secondHighestBid)) + throw; + } } function reserve(string _name) external { diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index dec29b37..8cb8257a 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -81,7 +81,8 @@ contract FixedFeeRegistrar is Registrar { } function disown(string _name, address _refund) onlyrecordowner(_name) { delete m_recordData[uint(sha3(_name)) / 8]; - _refund.send(c_fee); + if (!_refund.send(c_fee)) + throw; Changed(_name); } function transfer(string _name, address _newOwner) onlyrecordowner(_name) { -- cgit v1.2.3 From 9216a4d99113ad9e9edac3aa97690aa7015ffca1 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 1 Jul 2016 10:16:06 +0200 Subject: Fix jsoncpp includes for macos. --- test/RPCSession.cpp | 4 ++-- test/RPCSession.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/RPCSession.cpp b/test/RPCSession.cpp index 01118cca..54e1675f 100644 --- a/test/RPCSession.cpp +++ b/test/RPCSession.cpp @@ -23,8 +23,8 @@ #include #include #include -#include -#include +#include +#include #include "RPCSession.h" using namespace std; diff --git a/test/RPCSession.h b/test/RPCSession.h index 4ec21def..1a1fbbe5 100644 --- a/test/RPCSession.h +++ b/test/RPCSession.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include class IPCSocket: public boost::noncopyable -- cgit v1.2.3 From a15b533323ffeb2e4ef4875367b3ef2877f8b786 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 1 Jul 2016 10:20:13 +0200 Subject: Do not link to libethereum for tests. --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 794b6e4d..efcbb82e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,7 +26,7 @@ file(GLOB HEADERS "*.h" "*/*.h") set(EXECUTABLE soltest) eth_simple_add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) -eth_use(${EXECUTABLE} REQUIRED Solidity::solidity Eth::ethereum Eth::ethcore) +eth_use(${EXECUTABLE} REQUIRED Solidity::solidity Eth::ethcore) include_directories(BEFORE ..) target_link_libraries(${EXECUTABLE} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES}) -- cgit v1.2.3