-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathblockchaintest_runner.cpp
More file actions
221 lines (179 loc) · 7.98 KB
/
blockchaintest_runner.cpp
File metadata and controls
221 lines (179 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#include "../state/mpt_hash.hpp"
#include "../state/rlp.hpp"
#include "../state/state.hpp"
#include "../state/system_contracts.hpp"
#include "../test/statetest/statetest.hpp"
#include "blockchaintest.hpp"
#include <gtest/gtest.h>
namespace evmone::test
{
struct RejectedTransaction
{
hash256 hash;
size_t index;
std::string message;
};
struct TransitionResult
{
std::vector<state::TransactionReceipt> receipts;
std::vector<RejectedTransaction> rejected;
int64_t gas_used;
state::BloomFilter bloom;
};
namespace
{
TransitionResult apply_block(state::State& state, evmc::VM& vm, const state::BlockInfo& block,
const std::vector<state::Transaction>& txs, evmc_revision rev,
std::optional<int64_t> block_reward)
{
state::system_call(state, block, rev, vm);
std::vector<state::Log> txs_logs;
int64_t block_gas_left = block.gas_limit;
int64_t blob_gas_left = state::BlockInfo::MAX_BLOB_GAS_PER_BLOCK;
std::vector<RejectedTransaction> rejected_txs;
std::vector<state::TransactionReceipt> receipts;
int64_t cumulative_gas_used = 0;
for (size_t i = 0; i < txs.size(); ++i)
{
const auto& tx = txs[i];
const auto computed_tx_hash = keccak256(rlp::encode(tx));
auto res = state::transition(state, block, tx, rev, vm, block_gas_left, blob_gas_left);
if (holds_alternative<std::error_code>(res))
{
const auto ec = std::get<std::error_code>(res);
rejected_txs.push_back({computed_tx_hash, i, ec.message()});
}
else
{
auto& receipt = get<state::TransactionReceipt>(res);
const auto& tx_logs = receipt.logs;
txs_logs.insert(txs_logs.end(), tx_logs.begin(), tx_logs.end());
cumulative_gas_used += receipt.gas_used;
receipt.cumulative_gas_used = cumulative_gas_used;
if (rev < EVMC_BYZANTIUM)
receipt.post_state = state::mpt_hash(TestState{state});
block_gas_left -= receipt.gas_used;
blob_gas_left -= tx.blob_gas_used();
receipts.emplace_back(std::move(receipt));
}
}
state::finalize(state, rev, block.coinbase, block_reward, block.ommers, block.withdrawals);
const auto bloom = compute_bloom_filter(receipts);
return {std::move(receipts), std::move(rejected_txs), cumulative_gas_used, bloom};
}
std::optional<int64_t> mining_reward(evmc_revision rev) noexcept
{
if (rev < EVMC_BYZANTIUM)
return 5'000000000'000000000;
if (rev < EVMC_CONSTANTINOPLE)
return 3'000000000'000000000;
if (rev < EVMC_PARIS)
return 2'000000000'000000000;
return std::nullopt;
}
std::string print_state(const TestState& s)
{
std::stringstream out;
for (const auto& [key, acc] : s)
{
out << key << " : \n";
out << "\tnonce : " << acc.nonce << "\n";
out << "\tbalance : " << hex0x(acc.balance) << "\n";
out << "\tcode : " << hex0x(acc.code) << "\n";
if (!acc.storage.empty())
{
out << "\tstorage : \n";
for (const auto& [s_key, val] : acc.storage)
{
if (!is_zero(val)) // Skip 0 values.
out << "\t\t" << s_key << " : " << hex0x(val) << "\n";
}
}
}
return out.str();
}
/// How many first/last state hashes to check in the series of block for the "partial" check.
constexpr size_t PARTIAL_STATE_HASH_CHECK_MARGIN = 5;
/// List of test names for which only perform the partial state root hash checks.
/// For tests on the list, only check state hashes for first M (early problems)
/// and last M (final check of the chain) blocks,
/// where M is defined by PARTIAL_STATE_HASH_CHECK_MARGIN.
/// Otherwise, for very long blockchain test we spend a lot of time in MPT hashing
/// because the implementation of it is very simplistic: the trie is not updated
/// along the state changes but a new trie is build from scratch for every block.
constexpr std::array PARTIAL_STATE_HASH_CHECK_TESTS{
"a test name",
};
} // namespace
void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
{
for (size_t case_index = 0; case_index != tests.size(); ++case_index)
{
const auto& c = tests[case_index];
const bool partial_state_hash_check = std::ranges::find(PARTIAL_STATE_HASH_CHECK_TESTS,
c.name) != PARTIAL_STATE_HASH_CHECK_TESTS.end();
SCOPED_TRACE(std::string{evmc::to_string(c.rev.get_revision(0))} + '/' +
std::to_string(case_index) + '/' + c.name);
// Validate the genesis block header.
EXPECT_EQ(c.genesis_block_header.block_number, 0);
EXPECT_EQ(c.genesis_block_header.gas_used, 0);
EXPECT_EQ(c.genesis_block_header.transactions_root, state::EMPTY_MPT_HASH);
EXPECT_EQ(c.genesis_block_header.receipts_root, state::EMPTY_MPT_HASH);
EXPECT_EQ(c.genesis_block_header.withdrawal_root,
c.rev.get_revision(c.genesis_block_header.timestamp) >= EVMC_SHANGHAI ?
state::EMPTY_MPT_HASH :
bytes32{});
EXPECT_EQ(c.genesis_block_header.logs_bloom, bytes_view{state::BloomFilter{}});
auto state = c.pre_state.to_intra_state();
std::unordered_map<int64_t, hash256> known_block_hashes{
{c.genesis_block_header.block_number, c.genesis_block_header.hash}};
for (size_t block_idx = 0; block_idx != c.test_blocks.size(); ++block_idx)
{
const auto& test_block = c.test_blocks[block_idx];
auto bi = test_block.block_info;
bi.known_block_hashes = known_block_hashes;
const auto rev = c.rev.get_revision(bi.timestamp);
const auto res =
apply_block(state, vm, bi, test_block.transactions, rev, mining_reward(rev));
known_block_hashes[test_block.expected_block_header.block_number] =
test_block.expected_block_header.hash;
SCOPED_TRACE(std::string{evmc::to_string(rev)} + '/' + std::to_string(case_index) +
'/' + c.name + '/' + std::to_string(test_block.block_info.number));
if (!partial_state_hash_check || block_idx < PARTIAL_STATE_HASH_CHECK_MARGIN ||
block_idx >= c.test_blocks.size() - PARTIAL_STATE_HASH_CHECK_MARGIN)
{
EXPECT_EQ(
state::mpt_hash(TestState{state}), test_block.expected_block_header.state_root);
}
if (rev >= EVMC_SHANGHAI)
{
EXPECT_EQ(state::mpt_hash(test_block.block_info.withdrawals),
test_block.expected_block_header.withdrawal_root);
}
EXPECT_EQ(state::mpt_hash(test_block.transactions),
test_block.expected_block_header.transactions_root);
EXPECT_EQ(
state::mpt_hash(res.receipts), test_block.expected_block_header.receipts_root);
EXPECT_EQ(res.gas_used, test_block.expected_block_header.gas_used);
EXPECT_EQ(
bytes_view{res.bloom}, bytes_view{test_block.expected_block_header.logs_bloom});
// TODO: Add difficulty calculation verification.
}
const TestState post{state};
const auto expected_post_hash =
std::holds_alternative<TestState>(c.expectation.post_state) ?
state::mpt_hash(std::get<TestState>(c.expectation.post_state)) :
std::get<hash256>(c.expectation.post_state);
EXPECT_TRUE(state::mpt_hash(post) == expected_post_hash)
<< "Result state:\n"
<< print_state(post)
<< (std::holds_alternative<TestState>(c.expectation.post_state) ?
"\n\nExpected state:\n" +
print_state(std::get<TestState>(c.expectation.post_state)) :
"");
}
}
} // namespace evmone::test