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