Create getBlockByNumber RPC method

This commit is contained in:
Alex Beregszaszi 2017-02-09 18:41:51 +00:00
parent 4cf44f1b41
commit 702ee20a01
3 changed files with 11 additions and 4 deletions

View File

@ -127,13 +127,13 @@ void ExecutionFramework::sendEther(Address const& _to, u256 const& _value)
size_t ExecutionFramework::currentTimestamp() size_t ExecutionFramework::currentTimestamp()
{ {
auto latestBlock = m_rpc.rpcCall("eth_getBlockByNumber", {"\"latest\"", "false"}); auto latestBlock = m_rpc.eth_getBlockByNumber("latest", false);
return size_t(u256(latestBlock.get("timestamp", "invalid").asString())); return size_t(u256(latestBlock.get("timestamp", "invalid").asString()));
} }
size_t ExecutionFramework::blockTimestamp(u256 _number) size_t ExecutionFramework::blockTimestamp(u256 _number)
{ {
auto latestBlock = m_rpc.rpcCall("eth_getBlockByNumber", {toString(_number), "false"}); auto latestBlock = m_rpc.eth_getBlockByNumber(toString(_number), false);
return size_t(u256(latestBlock.get("timestamp", "invalid").asString())); return size_t(u256(latestBlock.get("timestamp", "invalid").asString()));
} }

View File

@ -131,6 +131,12 @@ string RPCSession::eth_getCode(string const& _address, string const& _blockNumbe
return rpcCall("eth_getCode", { quote(_address), quote(_blockNumber) }).asString(); return rpcCall("eth_getCode", { quote(_address), quote(_blockNumber) }).asString();
} }
Json::Value RPCSession::eth_getBlockByNumber(string const& _blockNumber, bool _fullObjects)
{
// NOTE: to_string() converts bool to 0 or 1
return rpcCall("eth_getBlockByNumber", { quote(_blockNumber), _fullObjects ? "true" : "false" });
}
RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string const& _transactionHash) RPCSession::TransactionReceipt RPCSession::eth_getTransactionReceipt(string const& _transactionHash)
{ {
TransactionReceipt receipt; TransactionReceipt receipt;
@ -290,9 +296,9 @@ Json::Value RPCSession::rpcCall(string const& _methodName, vector<string> const&
request += "],\"id\":" + to_string(m_rpcSequence) + "}"; request += "],\"id\":" + to_string(m_rpcSequence) + "}";
++m_rpcSequence; ++m_rpcSequence;
//cout << "Request: " << request << endl; // cout << "Request: " << request << endl;
string reply = m_ipcSocket.sendRequest(request); string reply = m_ipcSocket.sendRequest(request);
//cout << "Reply: " << reply << endl; // cout << "Reply: " << reply << endl;
Json::Value result; Json::Value result;
BOOST_REQUIRE(Json::Reader().parse(reply, result, false)); BOOST_REQUIRE(Json::Reader().parse(reply, result, false));

View File

@ -98,6 +98,7 @@ public:
static RPCSession& instance(std::string const& _path); static RPCSession& instance(std::string const& _path);
std::string eth_getCode(std::string const& _address, std::string const& _blockNumber); std::string eth_getCode(std::string const& _address, std::string const& _blockNumber);
Json::Value eth_getBlockByNumber(std::string const& _blockNumber, bool _fullObjects);
std::string eth_call(TransactionData const& _td, std::string const& _blockNumber); std::string eth_call(TransactionData const& _td, std::string const& _blockNumber);
TransactionReceipt eth_getTransactionReceipt(std::string const& _transactionHash); TransactionReceipt eth_getTransactionReceipt(std::string const& _transactionHash);
std::string eth_sendTransaction(TransactionData const& _transactionData); std::string eth_sendTransaction(TransactionData const& _transactionData);