Refactor testing via IPC.

This commit is contained in:
chriseth 2016-06-09 18:54:29 +02:00
parent ad36fc3c58
commit d6e39054e0
13 changed files with 240 additions and 195 deletions

View File

@ -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})

View File

@ -22,12 +22,17 @@
#include <string>
#include <stdio.h>
#include <thread>
#include <libdevcore/CommonData.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/writer.h>
#include "IPCSocket.h"
using namespace std;
IPCSocket::IPCSocket(string const& _path): m_address(_path)
using namespace std;
using namespace dev;
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)
{
return getReply("result\":", rpcCall("eth_getCode", { makeString(_address), makeString(_blockNumber) }));
static RPCSession session(_path);
BOOST_REQUIRE_EQUAL(session.m_ipcSocket.path(), _path);
return session;
}
RPCRequest::transactionReceipt RPCRequest::eth_getTransactionReceipt(string const& _transactionHash)
string RPCSession::eth_getCode(string const& _address, string const& _blockNumber)
{
transactionReceipt receipt;
string srpcCall = rpcCall("eth_getTransactionReceipt", { makeString(_transactionHash) });
receipt.gasUsed = getReply("gasUsed\":" , srpcCall);
receipt.contractAddress = getReply("contractAddress\":" , srpcCall);
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();
return receipt;
}
string RPCRequest::eth_sendTransaction(transactionData const& _td)
string RPCSession::eth_sendTransaction(TransactionData const& _td)
{
string transaction = c_transaction;
std::map<string, string> 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<string, string> 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<string> const& _args)
void RPCSession::test_mineBlocks(int _number)
{
// Extremely complicated mechanism because sometimes the miner breaks and stops mining.
u256 startBlock = fromBigEndian<u256>(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<u256>(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<string> 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<string> 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<string, string> 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<string, string> const& _varMap)
{
std::vector<string> types;
for (std::map<std::string, std::string>::const_iterator it = _varMap.begin(); it != _varMap.end(); it++)
@ -184,15 +232,16 @@ void RPCRequest::parseString(string& _string, map<string, string> 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);
}

View File

@ -25,70 +25,76 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <jsoncpp/json/value.h>
#include <boost/test/unit_test.hpp>
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<string> const& _args);
Json::Value rpcCall(std::string const& _methodName, std::vector<std::string> const& _args = std::vector<std::string>(), 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<string, string> 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<std::string, std::string> 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<std::string> m_accounts;
};

View File

@ -25,7 +25,7 @@
#include <boost/test/unit_test.hpp>
#include <libdevcore/Hash.h>
#include <libethcore/ABI.h>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
using namespace std;

View File

@ -1,5 +0,0 @@
cmake_policy(SET CMP0015 NEW)
aux_source_directory(. SRCS)
add_sources(${SRCS})

View File

@ -33,7 +33,7 @@
#endif
#include <libdevcore/Hash.h>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
using namespace std;

View File

@ -33,7 +33,7 @@
#endif
#include <libdevcore/Hash.h>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
using namespace std;

View File

@ -1,5 +0,0 @@
cmake_policy(SET CMP0015 NEW)
aux_source_directory(. SRCS)
add_sources(${SRCS})

View File

@ -20,7 +20,7 @@
* Unit tests for the gas estimator.
*/
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
#include <libevmasm/GasMeter.h>
#include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>

View File

@ -26,7 +26,7 @@
#include <boost/test/unit_test.hpp>
#include <libdevcore/Hash.h>
#include <libsolidity/interface/Exceptions.h>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
using namespace std;

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2016
* Framework for executing Solidity contracts and testing them against C++ implementation.
*/
#include <test/libsolidity/SolidityExecutionFramework.h>
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();
}

View File

@ -39,7 +39,6 @@
namespace dev
{
namespace solidity
{
namespace test
@ -49,24 +48,7 @@ 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);
}
ExecutionFramework();
bytes const& compileAndRunWithoutCheck(
std::string const& _sourceCode,
@ -267,40 +249,9 @@ private:
}
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);
void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0);
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();
}
RPCSession& m_rpc;
std::unique_ptr<eth::SealEngineFace> m_sealEngine;
size_t m_optimizeRuns = 200;
@ -316,8 +267,6 @@ protected:
bytes m_output;
eth::LogEntries m_logs;
u256 m_gasUsed;
RPCRequest m_socket;
};
}

View File

@ -25,7 +25,7 @@
#include <memory>
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
#include <libevmasm/CommonSubexpressionEliminator.h>
#include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/Assembly.h>