Skip to content

Commit

Permalink
Add blockchain tests loading and running unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Sep 27, 2023
1 parent 3f9d548 commit 1efc06c
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 3 deletions.
7 changes: 4 additions & 3 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_executable(evmone-unittests)
target_sources(
evmone-unittests PRIVATE
analysis_test.cpp
blockchaintest_loader_test.cpp
bytecode_test.cpp
eof_test.cpp
eof_validation_test.cpp
Expand Down Expand Up @@ -56,17 +57,17 @@ target_sources(
statetest_withdrawals_test.cpp
tracing_test.cpp
)
target_link_libraries(evmone-unittests PRIVATE evmone evmone::evmmax evmone::state evmone::statetestutils testutils evmc::instructions GTest::gtest GTest::gtest_main)
target_link_libraries(evmone-unittests PRIVATE evmone evmone::blockchaintestutils evmone::evmmax evmone::state evmone::statetestutils testutils evmc::instructions GTest::gtest GTest::gtest_main)
target_include_directories(evmone-unittests PRIVATE ${evmone_private_include_dir})

gtest_discover_tests(evmone-unittests TEST_PREFIX ${PROJECT_NAME}/unittests/)

option(EVMONE_EVM_TEST_TOOL "Enable EVM unit testing tool for EVMC implementations (not maintained)" OFF)
if(EVMONE_EVM_TEST_TOOL)
if (EVMONE_EVM_TEST_TOOL)
# The evm-test tool that contains the all evm-unittests and loads VMs as EVMC modules.
add_executable(evm-test main.cpp)
target_link_libraries(evm-test PRIVATE evm-unittests testutils evmc::evmc evmc::loader GTest::gtest)
endif()
endif ()

# Provide the project version to selected source files.
set_source_files_properties(
Expand Down
133 changes: 133 additions & 0 deletions test/unittests/blockchaintest_loader_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <gmock/gmock.h>
#include <test/blockchaintest/blockchaintest.hpp>
#include <test/utils/utils.hpp>

using namespace evmone;
using namespace evmone::test;
using namespace testing;

TEST(json_loader, TestBlock)
{
std::istringstream input{R"({
"000-fork=Shanghai-fill_stack": {
"blocks": [
{
"blockHeader": {
"parentHash": "0xe1bcc830589216abdc79cb3075f06f7b133f7b0cf257ecb346da33c354099700",
"uncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"coinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"stateRoot": "0x07b66de4268c3c26af1346a37fd0bb1584401981aafbedaa7837593030b5f968",
"transactionsTrie": "0x2bad57b8521a8d2a492526aecdb0e1244a14e1bc52809a046ac46a863ed9e54d",
"receiptTrie": "0xc227e1c29620a6496364056ce59ec4f51ed6e7bc56425e213d0195f84544c2c3",
"bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x00",
"number": "0x01",
"gasLimit": "0x016345785d8a0000",
"gasUsed": "0xbc5f",
"timestamp": "0x03e8",
"extraData": "0x00",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"baseFeePerGas": "0x07",
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"hash": "0x01de610f00331cea813e8143d51eb44ca352cdd90c602bb4b4bcf3c6cf9d5531"
},
"transactions": [
{
"type": "0x00",
"chainId": "0x01",
"nonce": "0x00",
"gasPrice": "0x0a",
"gasLimit": "0x0186a0",
"to": "0x0000000000000000000000000000000000000100",
"value": "0x00",
"data": "0x",
"v": "0x25",
"r": "0x86ddb9352affa90c20d71652b049404d8abcc6575e8e4a2c0bb9aa73fad9001c",
"s": "0x1bb0d685e5589862ae3d2b083be59c4f754c326800dbc82712e9f81eebf2f61d",
"sender": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"
}
],
"uncleHeaders": [],
"withdrawals": []
}
],
"genesisBlockHeader": {
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"uncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"coinbase": "0x0000000000000000000000000000000000000000",
"stateRoot": "0x6b1356f9f8d3bf201a9aa2c44f836ab60c92a349385625da90dce80eb6ecad44",
"transactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x00",
"number": "0x00",
"gasLimit": "0x016345785d8a0000",
"gasUsed": "0x00",
"timestamp": "0x00",
"extraData": "0x00",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"baseFeePerGas": "0x07",
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"hash": "0xe1bcc830589216abdc79cb3075f06f7b133f7b0cf257ecb346da33c354099700"
},
"lastblockhash": "0x01de610f00331cea813e8143d51eb44ca352cdd90c602bb4b4bcf3c6cf9d5531",
"network": "Shanghai",
"pre": {
"0x0000000000000000000000000000000000000100": {
"nonce": "0x00",
"balance": "0x00",
"code": "0x5f",
"storage": {}
},
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
"nonce": "0x00",
"balance": "0x3635c9adc5dea00000",
"code": "0x",
"storage": {}
}
},
"postState": {
"0x0000000000000000000000000000000000000100": {
"nonce": "0x00",
"balance": "0x00",
"code": "0x5f",
"storage": {
"0x00": "0x01"
}
},
"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba": {
"nonce": "0x00",
"balance": "0x02351d",
"code": "0x",
"storage": {}
}
},
"sealEngine": "NoProof"
}
})"};

auto s = input.str().size();
(void)s;
const auto btt = load_blockchain_test(input);

EXPECT_EQ(btt.cases.size(), 1);
EXPECT_EQ(btt.cases[0].test_blocks.size(), 1);
EXPECT_EQ(btt.cases[0].rev, evmc_revision::EVMC_SHANGHAI);
EXPECT_EQ(btt.cases[0].name, "000-fork=Shanghai-fill_stack");
EXPECT_EQ(btt.cases[0].genesis_block_header.timestamp, 0);
EXPECT_EQ(btt.cases[0].genesis_block_header.gas_limit, 0x016345785d8a0000);
EXPECT_EQ(btt.cases[0].genesis_block_header.base_fee_per_gas, 0x07);

EXPECT_EQ(btt.cases[0].test_blocks[0].transactions[0].type, state::Transaction::Type::legacy);
EXPECT_EQ(btt.cases[0].test_blocks[0].transactions[0].sender,
0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b_address);
EXPECT_EQ(btt.cases[0].test_blocks[0].block_info.gas_limit, 0x016345785d8a0000);
EXPECT_EQ(btt.cases[0].test_blocks[0].block_info.number, 1);
EXPECT_EQ(btt.cases[0].test_blocks[0].block_info.timestamp, 0x03e8);
}

0 comments on commit 1efc06c

Please sign in to comment.