-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add end-to-end integration test for 3PC EVM runner
Signed-off-by: Alexander Jung <[email protected]>
- Loading branch information
1 parent
2835c32
commit e13f056
Showing
9 changed files
with
1,269 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright (c) 2023 MIT Digital Currency Initiative, | ||
// | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "gtest_evm_jsonrpc_client.hpp" | ||
|
||
#include "3pc/agent/runners/evm/address.hpp" | ||
#include "3pc/agent/runners/evm/rlp.hpp" | ||
#include "3pc/agent/runners/evm/serialization.hpp" | ||
#include "3pc/agent/runners/evm/util.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
#include <thread> | ||
|
||
namespace cbdc::test { | ||
static std::string gtest_descr() { | ||
// e.g.: "GTEST: threepc_evm_end_to_end_test.native_transfer" | ||
return std::string("GTEST: ") | ||
+ ::testing::UnitTest::GetInstance() | ||
->current_test_info() | ||
->test_suite_name() | ||
+ "." | ||
+ ::testing::UnitTest::GetInstance()->current_test_info()->name(); | ||
} | ||
|
||
gtest_evm_jsonrpc_client::gtest_evm_jsonrpc_client( | ||
std::vector<std::string> endpoints, | ||
long timeout, | ||
std::shared_ptr<cbdc::logging::log> log) | ||
: cbdc::rpc::json_rpc_http_client(std::move(endpoints), | ||
timeout, | ||
std::move(log)) {} | ||
|
||
evmc::uint256be gtest_evm_jsonrpc_client::get_transaction_count( | ||
const evmc::address& addr) { | ||
std::string txcount_str; | ||
get_transaction_count_str_(addr, txcount_str); | ||
m_log->debug(gtest_descr(), | ||
std::string(__FUNCTION__) + "()", | ||
"0x" + cbdc::threepc::agent::runner::to_hex(addr), | ||
txcount_str); | ||
return cbdc::threepc::agent::runner::uint256be_from_hex(txcount_str) | ||
.value(); | ||
} | ||
|
||
void gtest_evm_jsonrpc_client::get_transaction_count_str_( | ||
const evmc::address& addr, | ||
std::string& out_txcount_str) { | ||
auto params = Json::Value(); | ||
params.append("0x" + cbdc::threepc::agent::runner::to_hex(addr)); | ||
params.append("latest"); | ||
|
||
std::atomic<bool> tx_done{false}; | ||
call("eth_getTransactionCount", | ||
std::move(params), | ||
[&tx_done, &out_txcount_str](std::optional<Json::Value> res) { | ||
ASSERT_TRUE(res.has_value()); | ||
const auto& v = res.value(); | ||
ASSERT_FALSE(v.isMember(m_json_error_key)); | ||
ASSERT_TRUE(v.isMember(m_json_result_key)); | ||
out_txcount_str = v[m_json_result_key].asString(); | ||
ASSERT_TRUE(out_txcount_str.length() > 0); | ||
|
||
tx_done = true; | ||
}); | ||
|
||
for(int cnt = 0; cnt < 20 && !tx_done; ++cnt) { | ||
ASSERT_TRUE(pump()); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
ASSERT_TRUE(tx_done); | ||
} | ||
|
||
void gtest_evm_jsonrpc_client::send_transaction( | ||
const cbdc::threepc::agent::runner::evm_tx& etx, | ||
std::string& out_txid) { | ||
const auto rlp_tx_buf = cbdc::threepc::agent::runner::tx_encode(etx); | ||
const auto rlp_tx_hex = "0x" + rlp_tx_buf.to_hex(); | ||
|
||
auto params = Json::Value(); | ||
params.append(rlp_tx_hex); | ||
|
||
std::atomic<bool> tx_done{false}; | ||
std::string txid{}; | ||
call("eth_sendRawTransaction", | ||
std::move(params), | ||
[&tx_done, &out_txid](std::optional<Json::Value> res) { | ||
ASSERT_TRUE(res.has_value()); | ||
const auto& v = res.value(); | ||
ASSERT_FALSE(v.isMember(m_json_error_key)); | ||
ASSERT_TRUE(v.isMember(m_json_result_key)); | ||
|
||
ASSERT_TRUE(v.size() == 3); | ||
ASSERT_TRUE(v.isMember("id")); | ||
ASSERT_TRUE(v["id"].isInt()); | ||
ASSERT_TRUE(v.isMember("jsonrpc")); | ||
ASSERT_TRUE(v["jsonrpc"].isString()); // e.g. "2.0" | ||
|
||
ASSERT_TRUE(v[m_json_result_key].isString()); | ||
out_txid = v[m_json_result_key].asString(); | ||
ASSERT_TRUE(out_txid.length() > 0); | ||
|
||
tx_done = true; | ||
}); | ||
|
||
for(int cnt = 0; cnt < 20 && !tx_done; ++cnt) { | ||
ASSERT_TRUE(pump()); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
ASSERT_TRUE(tx_done); | ||
} | ||
|
||
void gtest_evm_jsonrpc_client::get_transaction_receipt( | ||
const std::string& txid, | ||
Json::Value& out_tx_receipt, | ||
std::shared_ptr<cbdc::logging::log> log) { | ||
auto params = Json::Value(); | ||
params.append(txid); | ||
|
||
std::atomic<bool> tx_done{false}; | ||
call("eth_getTransactionReceipt", | ||
std::move(params), | ||
[&out_tx_receipt, &tx_done, log](std::optional<Json::Value> res) { | ||
ASSERT_TRUE(res.has_value()); | ||
auto& v = res.value(); | ||
ASSERT_FALSE(v.isMember(m_json_error_key)); | ||
ASSERT_TRUE(v.isMember(m_json_result_key)); | ||
|
||
out_tx_receipt = v[m_json_result_key]; | ||
ASSERT_TRUE(out_tx_receipt.isObject()); | ||
|
||
for(auto const& id : out_tx_receipt.getMemberNames()) { | ||
log->debug( | ||
"gtest_evm_jsonrpc_client::get_transaction_receipt() " | ||
"json::value member:", | ||
id, | ||
out_tx_receipt[id].type(), | ||
out_tx_receipt[id]); | ||
} | ||
|
||
tx_done = true; | ||
}); | ||
|
||
for(int cnt = 0; cnt < 20 && !tx_done; ++cnt) { | ||
ASSERT_TRUE(pump()); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
ASSERT_TRUE(tx_done); | ||
} | ||
|
||
void gtest_evm_jsonrpc_client::get_balance( | ||
const evmc::address& addr, | ||
std::optional<evmc::uint256be>& out_balance) { | ||
auto params = Json::Value(); | ||
params.append("0x" + cbdc::threepc::agent::runner::to_hex(addr)); | ||
|
||
std::atomic<bool> tx_done{false}; | ||
call("eth_getBalance", | ||
std::move(params), | ||
[&tx_done, &out_balance](std::optional<Json::Value> res) { | ||
ASSERT_TRUE(res.has_value()); | ||
const auto& v = res.value(); | ||
ASSERT_FALSE(v.isMember(m_json_error_key)); | ||
ASSERT_TRUE(v.isMember(m_json_result_key)); | ||
|
||
auto res_str = v[m_json_result_key].asString(); | ||
out_balance | ||
= cbdc::threepc::agent::runner::uint256be_from_hex( | ||
res_str); | ||
|
||
tx_done = true; | ||
}); | ||
|
||
for(int cnt = 0; cnt < 20 && !tx_done; ++cnt) { | ||
ASSERT_TRUE(pump()); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
|
||
ASSERT_TRUE(tx_done); | ||
ASSERT_TRUE(out_balance.has_value()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2023 MIT Digital Currency Initiative, | ||
// | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef OPENCBDC_TEST_INTEGRATION_GTEST_EVM_JSONRPC_CLIENT_H_ | ||
#define OPENCBDC_TEST_INTEGRATION_GTEST_EVM_JSONRPC_CLIENT_H_ | ||
|
||
#include "3pc/agent/runners/evm/messages.hpp" | ||
#include "util/rpc/http/json_rpc_http_client.hpp" | ||
|
||
namespace cbdc::test { | ||
|
||
class gtest_evm_jsonrpc_client : public cbdc::rpc::json_rpc_http_client { | ||
public: | ||
gtest_evm_jsonrpc_client(std::vector<std::string> endpoints, | ||
long timeout, | ||
std::shared_ptr<cbdc::logging::log> log); | ||
|
||
void send_transaction(const cbdc::threepc::agent::runner::evm_tx& tx, | ||
std::string& out_txid); | ||
|
||
void get_transaction_receipt(const std::string& txid, | ||
Json::Value& out_tx_receipt, | ||
std::shared_ptr<cbdc::logging::log> log); | ||
|
||
void get_balance(const evmc::address& addr, | ||
std::optional<evmc::uint256be>& out_balance); | ||
|
||
[[nodiscard]] evmc::uint256be | ||
get_transaction_count(const evmc::address& addr); | ||
|
||
private: | ||
static constexpr auto m_json_error_key = "error"; | ||
static constexpr auto m_json_result_key = "result"; | ||
|
||
void get_transaction_count_str_(const evmc::address& addr, | ||
std::string& out_txcount_str); | ||
}; | ||
|
||
} | ||
|
||
#endif // OPENCBDC_TEST_INTEGRATION_GTEST_EVM_JSONRPC_CLIENT_H_ |
Oops, something went wrong.