Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions test/blockchaintest/blockchaintest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class BlockchainGTest : public testing::Test
{
fs::path m_json_test_file;
evmc::VM& m_vm;
bool m_time;

public:
explicit BlockchainGTest(fs::path json_test_file, evmc::VM& vm) noexcept
: m_json_test_file{std::move(json_test_file)}, m_vm{vm}
explicit BlockchainGTest(fs::path json_test_file, evmc::VM& vm, bool time) noexcept
: m_json_test_file{std::move(json_test_file)}, m_vm{vm}, m_time{time}
{}

void TestBody() final
Expand All @@ -29,7 +30,8 @@ class BlockchainGTest : public testing::Test

try
{
evmone::test::run_blockchain_tests(evmone::test::load_blockchain_tests(f), m_vm);
evmone::test::run_blockchain_tests(
evmone::test::load_blockchain_tests(f), m_vm, m_time);
}
catch (const evmone::test::UnsupportedTestFeature& ex)
{
Expand All @@ -38,14 +40,14 @@ class BlockchainGTest : public testing::Test
}
};

void register_test(const std::string& suite_name, const fs::path& file, evmc::VM& vm)
void register_test(const std::string& suite_name, const fs::path& file, evmc::VM& vm, bool time)
{
testing::RegisterTest(suite_name.c_str(), file.stem().string().c_str(), nullptr, nullptr,
file.string().c_str(), 0,
[file, &vm]() -> testing::Test* { return new BlockchainGTest(file, vm); });
[file, &vm, time]() -> testing::Test* { return new BlockchainGTest(file, vm, time); });
}

void register_test_files(const fs::path& root, evmc::VM& vm)
void register_test_files(const fs::path& root, evmc::VM& vm, bool time)
{
if (is_directory(root))
{
Expand All @@ -57,11 +59,11 @@ void register_test_files(const fs::path& root, evmc::VM& vm)
std::ranges::sort(test_files);

for (const auto& p : test_files)
register_test(fs::relative(p, root).parent_path().string(), p, vm);
register_test(fs::relative(p, root).parent_path().string(), p, vm, time);
}
else // Treat as a file.
{
register_test(root.parent_path().string(), root, vm);
register_test(root.parent_path().string(), root, vm, time);
}
}
} // namespace
Expand All @@ -85,6 +87,9 @@ int main(int argc, char* argv[])
bool trace_flag = false;
app.add_flag("--trace", trace_flag, "Enable EVM tracing");

bool time_flag = false;
app.add_flag("--time", time_flag, "Measure last block in test execution");

CLI11_PARSE(app, argc, argv);

evmc::VM vm{evmc_create_evmone()};
Expand All @@ -93,7 +98,7 @@ int main(int argc, char* argv[])
vm.set_option("trace", "1");

for (const auto& p : paths)
register_test_files(p, vm);
register_test_files(p, vm, time_flag);

return RUN_ALL_TESTS();
}
Expand Down
2 changes: 1 addition & 1 deletion test/blockchaintest/blockchaintest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ struct BlockchainTest

std::vector<BlockchainTest> load_blockchain_tests(std::istream& input);

void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm);
void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm, bool time);

} // namespace evmone::test
43 changes: 36 additions & 7 deletions test/blockchaintest/blockchaintest_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "blockchaintest.hpp"
#include <gtest/gtest.h>

using namespace std::chrono;
using timer = high_resolution_clock;
namespace evmone::test
{

Expand Down Expand Up @@ -221,7 +223,7 @@ std::string print_state(const TestState& s)
}
} // namespace

void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm, bool time)
{
for (size_t case_index = 0; case_index != tests.size(); ++case_index)
{
Expand Down Expand Up @@ -253,6 +255,7 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
{&c.genesis_block_header, c.pre_state, c.genesis_block_header.difficulty}}}};
const auto* canonical_state = &c.pre_state;
intx::uint256 max_total_difficulty = c.genesis_block_header.difficulty;
decltype(timer::now() - timer::now()) duration;

for (size_t i = 0; i < c.test_blocks.size(); ++i)
{
Expand All @@ -276,10 +279,19 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
// Block being valid guarantees its parent was found.
assert(parent_data_it != block_data.end());
const auto& pre_state = parent_data_it->second.post_state;

auto res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions,
rev, mining_reward(rev));

TransitionResult res;
if (time && i == c.test_blocks.size() - 1)
{
const auto start_timer = timer::now();
res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev,
mining_reward(rev));
duration = timer::now() - start_timer;
}
else
{
res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev,
mining_reward(rev));
}
ASSERT_TRUE(res.requests.has_value());

block_hashes[test_block.expected_block_header.block_number] =
Expand Down Expand Up @@ -330,8 +342,19 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
assert(parent_data_it != block_data.end());
const auto& pre_state = parent_data_it->second.post_state;

const auto res = apply_block(pre_state, vm, bi, block_hashes,
test_block.transactions, rev, mining_reward(rev));
TransitionResult res;
if (time && i == c.test_blocks.size() - 1)
{
const auto start_timer = timer::now();
res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev,
mining_reward(rev));
duration = timer::now() - start_timer;
}
else
{
res = apply_block(pre_state, vm, bi, block_hashes, test_block.transactions, rev,
mining_reward(rev));
}
if (!res.requests.has_value())
continue;
if (!res.rejected.empty())
Expand Down Expand Up @@ -362,6 +385,12 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
EXPECT_TRUE(false) << "Expected block to be invalid but resulted valid";
}
}
if (time)
{
std::cout << " \t" << c.name
<< " last block time: " << duration_cast<nanoseconds>(duration).count()
<< " ns\n";
}
const auto expected_post_hash =
std::holds_alternative<TestState>(c.expectation.post_state) ?
state::mpt_hash(std::get<TestState>(c.expectation.post_state)) :
Expand Down