-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
evmone-statetest tool with JSON test loading #479
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04e222b
statetest: Add evmone-statetest stub
chfast d77fac4
statetest: Register test files with GTest
chfast 4ad6ef5
state: Add or extend State types
chfast d045a19
statetest: Implement JSON test file loading
chfast de64e67
ci: Execute state tests
chfast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ target_sources( | |
mpt_hash.hpp | ||
mpt_hash.cpp | ||
rlp.hpp | ||
state.hpp | ||
) |
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,59 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2022 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "account.hpp" | ||
#include "hash_utils.hpp" | ||
#include <cassert> | ||
#include <optional> | ||
#include <vector> | ||
|
||
namespace evmone::state | ||
{ | ||
class State | ||
{ | ||
std::unordered_map<address, Account> m_accounts; | ||
|
||
public: | ||
/// Creates new account under the address. | ||
Account& create(const address& addr) | ||
{ | ||
const auto r = m_accounts.insert({addr, {}}); | ||
assert(r.second); | ||
return r.first->second; | ||
} | ||
}; | ||
|
||
struct BlockInfo | ||
{ | ||
int64_t number = 0; | ||
int64_t timestamp = 0; | ||
int64_t gas_limit = 0; | ||
address coinbase; | ||
bytes32 prev_randao; | ||
uint64_t base_fee = 0; | ||
}; | ||
|
||
using AccessList = std::vector<std::pair<address, std::vector<bytes32>>>; | ||
|
||
struct Transaction | ||
{ | ||
enum class Kind | ||
{ | ||
legacy, | ||
eip1559 | ||
}; | ||
|
||
Kind kind = Kind::legacy; | ||
bytes data; | ||
int64_t gas_limit; | ||
intx::uint256 max_gas_price; | ||
intx::uint256 max_priority_gas_price; | ||
address sender; | ||
std::optional<address> to; | ||
intx::uint256 value; | ||
AccessList access_list; | ||
}; | ||
} // namespace evmone::state |
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,3 @@ | ||
InheritParentConfig: true | ||
Checks: > | ||
-clang-analyzer-cplusplus.NewDeleteLeaks |
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,15 @@ | ||
# evmone: Fast Ethereum Virtual Machine implementation | ||
# Copyright 2022 The evmone Authors. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
hunter_add_package(nlohmann_json) | ||
find_package(nlohmann_json CONFIG REQUIRED) | ||
|
||
add_executable(evmone-statetest) | ||
target_link_libraries(evmone-statetest PRIVATE evmone evmone::state nlohmann_json::nlohmann_json GTest::gtest) | ||
target_sources( | ||
evmone-statetest PRIVATE | ||
statetest.hpp | ||
statetest.cpp | ||
statetest_loader.cpp | ||
) |
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,50 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2022 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "statetest.hpp" | ||
#include <gtest/gtest.h> | ||
#include <iostream> | ||
|
||
namespace | ||
{ | ||
class StateTest : public testing::Test | ||
{ | ||
fs::path m_json_test_file; | ||
|
||
public: | ||
explicit StateTest(fs::path json_test_file) noexcept | ||
: m_json_test_file{std::move(json_test_file)} | ||
{} | ||
|
||
void TestBody() final { evmone::test::load_state_test(m_json_test_file); } | ||
}; | ||
} // namespace | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); // Process GoogleTest flags. | ||
|
||
if (argc != 2) | ||
{ | ||
std::cerr << "Missing argument with the path to the tests directory\n"; | ||
return -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}, | ||
fs::recursive_directory_iterator{}, std::back_inserter(test_files), | ||
[](const fs::directory_entry& entry) { | ||
return entry.is_regular_file() && entry.path().extension() == ".json"; | ||
}); | ||
std::sort(test_files.begin(), test_files.end()); | ||
for (const auto& p : test_files) | ||
{ | ||
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); }); | ||
} | ||
|
||
return RUN_ALL_TESTS(); | ||
} |
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,63 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2022 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#pragma once | ||
|
||
#include "../state/state.hpp" | ||
#include <filesystem> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace evmone::test | ||
{ | ||
struct TestMultiTransaction : state::Transaction | ||
{ | ||
struct Indexes | ||
{ | ||
size_t input = 0; | ||
size_t gas_limit = 0; | ||
size_t value = 0; | ||
}; | ||
|
||
std::vector<state::AccessList> access_lists; | ||
std::vector<bytes> inputs; | ||
std::vector<int64_t> gas_limits; | ||
std::vector<intx::uint256> values; | ||
|
||
[[nodiscard]] Transaction get(const Indexes& indexes) const noexcept | ||
{ | ||
Transaction tx{*this}; | ||
if (!access_lists.empty()) | ||
tx.access_list = access_lists.at(indexes.input); | ||
tx.data = inputs.at(indexes.input); | ||
tx.gas_limit = gas_limits.at(indexes.gas_limit); | ||
tx.value = values.at(indexes.value); | ||
return tx; | ||
} | ||
}; | ||
|
||
struct StateTransitionTest | ||
{ | ||
struct Case | ||
{ | ||
struct Expectation | ||
{ | ||
TestMultiTransaction::Indexes indexes; | ||
hash256 state_hash; | ||
hash256 logs_hash; | ||
bool exception = false; | ||
}; | ||
|
||
evmc_revision rev; | ||
std::vector<Expectation> expectations; | ||
}; | ||
|
||
state::State pre_state; | ||
state::BlockInfo block; | ||
TestMultiTransaction multi_tx; | ||
std::vector<Case> cases; | ||
}; | ||
|
||
StateTransitionTest load_state_test(const fs::path& test_file); | ||
|
||
} // namespace evmone::test |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a bit inconsistent that
TestCase
has a vector of nestedTestCase::Case
, butStateTransitionTest
has a vector of not-nestedTestCase
.So maybe this shouln't be nested?
And renamin suggestion;
Case
=>Expectation
orCaseExpectation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually moved structs into
StateTransitionTest
. Usually I don't do this but here this looks nice.