Skip to content

Commit af4f012

Browse files
committed
host: Support block hashes
1 parent 35f4141 commit af4f012

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

test/state/host.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ evmc_tx_context Host::get_tx_context() const noexcept
320320

321321
bytes32 Host::get_block_hash(int64_t block_number) const noexcept
322322
{
323-
(void)block_number;
324-
// TODO: This is not properly implemented, but only single state test requires BLOCKHASH
325-
// and is fine with any value.
326-
return {};
323+
if (const auto& it = m_block.block_hashes.find(block_number); it != m_block.block_hashes.end())
324+
return it->second;
325+
else
326+
return {};
327327
}
328328

329329
void Host::emit_log(const address& addr, const uint8_t* data, size_t data_size,

test/state/state.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct BlockInfo
8787
bytes32 prev_randao;
8888
uint64_t base_fee = 0;
8989
std::vector<Withdrawal> withdrawals;
90+
std::unordered_map<int64_t, hash256> block_hashes = {};
9091
};
9192

9293
using AccessList = std::vector<std::pair<address, std::vector<bytes32>>>;

test/statetest/statetest_loader.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,17 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
181181
}
182182
}
183183

184+
std::unordered_map<int64_t, hash256> block_hashes;
185+
if (const auto block_hashes_it = j.find("blockHashes"); block_hashes_it != j.end())
186+
{
187+
for (const auto& [j_num, j_hash] : block_hashes_it->items())
188+
block_hashes[from_json<int64_t>(j_num)] = from_json<hash256>(j_hash);
189+
}
190+
184191
return {from_json<int64_t>(j.at("currentNumber")), from_json<int64_t>(j.at("currentTimestamp")),
185192
from_json<int64_t>(j.at("currentGasLimit")),
186193
from_json<evmc::address>(j.at("currentCoinbase")), difficulty, base_fee,
187-
std::move(withdrawals)};
194+
std::move(withdrawals), std::move(block_hashes)};
188195
}
189196

190197
template <>

test/unittests/statetest_loader_block_info_test.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ TEST(statetest_loader, block_info)
1717
"currentTimestamp": "0",
1818
"currentBaseFee": "7",
1919
"currentRandom": "0x00",
20-
"withdrawals": []
20+
"withdrawals": [],
21+
"blockHashes": {
22+
"0" : "0xe729de3fec21e30bea3d56adb01ed14bc107273c2775f9355afb10f594a10d9e",
23+
"1" : "0xb5eee60b45801179cbde3781b9a5dee9b3111554618c9cda3d6f7e351fd41e0b"
24+
}
2125
})";
2226

2327
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
@@ -27,6 +31,10 @@ TEST(statetest_loader, block_info)
2731
EXPECT_EQ(bi.base_fee, 7);
2832
EXPECT_EQ(bi.timestamp, 0);
2933
EXPECT_EQ(bi.number, 0);
34+
EXPECT_EQ(bi.block_hashes.at(0),
35+
0xe729de3fec21e30bea3d56adb01ed14bc107273c2775f9355afb10f594a10d9e_bytes32);
36+
EXPECT_EQ(bi.block_hashes.at(1),
37+
0xb5eee60b45801179cbde3781b9a5dee9b3111554618c9cda3d6f7e351fd41e0b_bytes32);
3038
}
3139

3240
TEST(statetest_loader, block_info_hex)

0 commit comments

Comments
 (0)