Skip to content

Commit 9908478

Browse files
committed
test: Add BLOBHASH unit tests
1 parent 8bd47d2 commit 9908478

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

test/unittests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_sources(
2020
evm_eip3198_basefee_test.cpp
2121
evm_eip3855_push0_test.cpp
2222
evm_eip3860_initcode_test.cpp
23+
evm_eip4844_blobhash_test.cpp
2324
evm_eof_test.cpp
2425
evm_eof_calls_test.cpp
2526
evm_eof_function_test.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// evmone: Fast Ethereum Virtual Machine implementation
2+
// Copyright 2023 The evmone Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
/// This file contains EVM unit tests for the BLOBHASH instruction from EIP-4844
6+
/// https://eips.ethereum.org/EIPS/eip-4844
7+
8+
#include "evm_fixture.hpp"
9+
10+
using namespace evmc::literals;
11+
using evmone::test::evm;
12+
13+
TEST_P(evm, blobhash_undefined)
14+
{
15+
rev = EVMC_SHANGHAI;
16+
execute(blobhash(0));
17+
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);
18+
}
19+
20+
TEST_P(evm, blobhash_empty)
21+
{
22+
rev = EVMC_CANCUN;
23+
execute(blobhash(0) + ret_top());
24+
EXPECT_OUTPUT_INT(0);
25+
26+
execute(blobhash(1) + ret_top());
27+
EXPECT_OUTPUT_INT(0);
28+
29+
execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
30+
ret_top());
31+
EXPECT_OUTPUT_INT(0);
32+
}
33+
34+
TEST_P(evm, blobhash_one)
35+
{
36+
rev = EVMC_CANCUN;
37+
38+
const std::array blob_hashes{
39+
0x01feeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeed_bytes32};
40+
41+
host.tx_context.blob_hashes = blob_hashes.data();
42+
host.tx_context.blob_hashes_count = blob_hashes.size();
43+
44+
execute(blobhash(0) + ret_top());
45+
EXPECT_STATUS(EVMC_SUCCESS);
46+
EXPECT_EQ(output, blob_hashes[0]);
47+
48+
execute(blobhash(1) + ret_top());
49+
EXPECT_OUTPUT_INT(0);
50+
51+
execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
52+
ret_top());
53+
EXPECT_OUTPUT_INT(0);
54+
}
55+
56+
TEST_P(evm, blobhash_two)
57+
{
58+
rev = EVMC_CANCUN;
59+
60+
const std::array blob_hashes{
61+
0x0100000000000000000000000000000000000000000000000000000000000001_bytes32,
62+
0x0100000000000000000000000000000000000000000000000000000000000002_bytes32};
63+
64+
host.tx_context.blob_hashes = blob_hashes.data();
65+
host.tx_context.blob_hashes_count = blob_hashes.size();
66+
67+
for (size_t i = 0; i < blob_hashes.size(); ++i)
68+
{
69+
execute(blobhash(i) + ret_top());
70+
EXPECT_STATUS(EVMC_SUCCESS);
71+
EXPECT_EQ(output, blob_hashes[i]);
72+
}
73+
74+
execute(blobhash(blob_hashes.size()) + ret_top());
75+
EXPECT_OUTPUT_INT(0);
76+
77+
execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
78+
ret_top());
79+
EXPECT_OUTPUT_INT(0);
80+
}
81+
82+
TEST_P(evm, blobhash_invalid_hash_version)
83+
{
84+
rev = EVMC_CANCUN;
85+
86+
// The BLOBHASH instruction does not care about the hash version,
87+
// it will return whatever is in the array.
88+
const std::array blob_hashes{
89+
0x0000000000000000000000000000000000000000000000000000000000000000_bytes32,
90+
0x0200000000000000000000000000000000000000000000000000000000000000_bytes32};
91+
92+
host.tx_context.blob_hashes = blob_hashes.data();
93+
host.tx_context.blob_hashes_count = blob_hashes.size();
94+
95+
for (size_t i = 0; i < blob_hashes.size(); ++i)
96+
{
97+
execute(blobhash(i) + ret_top());
98+
EXPECT_STATUS(EVMC_SUCCESS);
99+
EXPECT_EQ(output, blob_hashes[i]);
100+
}
101+
102+
execute(blobhash(blob_hashes.size()) + ret_top());
103+
EXPECT_OUTPUT_INT(0);
104+
105+
execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
106+
ret_top());
107+
EXPECT_OUTPUT_INT(0);
108+
}

test/utils/bytecode.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ inline bytecode sload(bytecode index)
333333
return index + OP_SLOAD;
334334
}
335335

336+
inline bytecode blobhash(bytecode index)
337+
{
338+
return index + OP_BLOBHASH;
339+
}
340+
336341
template <Opcode kind>
337342
struct call_instruction
338343
{

0 commit comments

Comments
 (0)