Skip to content

Commit

Permalink
statetest: Implement state test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Oct 20, 2022
1 parent c07ca83 commit 134eddc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/statetest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ target_sources(
statetest.hpp
statetest.cpp
statetest_loader.cpp
statetest_runner.cpp
)
16 changes: 12 additions & 4 deletions test/statetest/statetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "statetest.hpp"
#include <evmone/evmone.h>
#include <gtest/gtest.h>
#include <iostream>

Expand All @@ -11,13 +12,17 @@ namespace
class StateTest : public testing::Test
{
fs::path m_json_test_file;
evmc::VM& m_vm;

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

void TestBody() final { evmone::test::load_state_test(m_json_test_file); }
void TestBody() final
{
evmone::test::run_state_test(evmone::test::load_state_test(m_json_test_file), m_vm);
}
};
} // namespace

Expand All @@ -31,6 +36,8 @@ int main(int argc, char* argv[])
return -1;
}

evmc::VM vm{evmc_create_evmone(), {{"O", "0"}, /*{"trace", "1"}*/}};

std::vector<fs::path> test_files;
const fs::path root_test_dir{argv[1]};
std::copy_if(fs::recursive_directory_iterator{root_test_dir},
Expand All @@ -43,7 +50,8 @@ int main(int argc, char* argv[])
{
const auto d = fs::relative(p, root_test_dir);
testing::RegisterTest(d.parent_path().string().c_str(), d.stem().string().c_str(), nullptr,
nullptr, p.string().c_str(), 0, [p]() -> testing::Test* { return new StateTest(p); });
nullptr, p.string().c_str(), 0,
[p, &vm]() -> testing::Test* { return new StateTest(p, vm); });
}

return RUN_ALL_TESTS();
Expand Down
2 changes: 2 additions & 0 deletions test/statetest/statetest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ struct StateTransitionTest

StateTransitionTest load_state_test(const fs::path& test_file);

void run_state_test(const StateTransitionTest& test, evmc::VM& vm);

} // namespace evmone::test
47 changes: 47 additions & 0 deletions test/statetest/statetest_runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "../state/mpt_hash.hpp"
#include "../state/rlp.hpp"
#include "statetest.hpp"
#include <gtest/gtest.h>

namespace evmone::state
{
/// Defines how to RLP-encode a Log. This is only needed to compute "logs hash".
inline bytes rlp_encode(const Log& log)
{
return rlp::encode_tuple(log.addr, log.topics, log.data);
}
} // namespace evmone::state

namespace evmone::test
{
void run_state_test(const StateTransitionTest& test, evmc::VM& vm)
{
for (const auto& [rev, cases] : test.cases)
{
for (size_t case_index = 0; case_index != cases.size(); ++case_index)
{
SCOPED_TRACE(std::string{evmc::to_string(rev)} + '/' + std::to_string(case_index));
// if (rev != EVMC_FRONTIER)
// continue;
// if (case_index != 3)
// continue;

const auto& expected = cases[case_index];
const auto tx = test.multi_tx.get(expected.indexes);
auto state = test.pre_state;

const auto tx_logs = state::transition(state, test.block, tx, rev, vm);
if (tx_logs.has_value())
EXPECT_EQ(keccak256(rlp::encode(*tx_logs)), expected.logs_hash);
else
EXPECT_TRUE(expected.exception);

EXPECT_EQ(state::mpt_hash(state.get_accounts()), expected.state_hash);
}
}
}
} // namespace evmone::test

0 comments on commit 134eddc

Please sign in to comment.