Skip to content

Commit

Permalink
test: Add BLOBHASH unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 1, 2023
1 parent 8bd47d2 commit 9908478
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ target_sources(
evm_eip3198_basefee_test.cpp
evm_eip3855_push0_test.cpp
evm_eip3860_initcode_test.cpp
evm_eip4844_blobhash_test.cpp
evm_eof_test.cpp
evm_eof_calls_test.cpp
evm_eof_function_test.cpp
Expand Down
108 changes: 108 additions & 0 deletions test/unittests/evm_eip4844_blobhash_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

/// This file contains EVM unit tests for the BLOBHASH instruction from EIP-4844
/// https://eips.ethereum.org/EIPS/eip-4844

#include "evm_fixture.hpp"

using namespace evmc::literals;
using evmone::test::evm;

TEST_P(evm, blobhash_undefined)
{
rev = EVMC_SHANGHAI;
execute(blobhash(0));
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);
}

TEST_P(evm, blobhash_empty)
{
rev = EVMC_CANCUN;
execute(blobhash(0) + ret_top());
EXPECT_OUTPUT_INT(0);

execute(blobhash(1) + ret_top());
EXPECT_OUTPUT_INT(0);

execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
ret_top());
EXPECT_OUTPUT_INT(0);
}

TEST_P(evm, blobhash_one)
{
rev = EVMC_CANCUN;

const std::array blob_hashes{
0x01feeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeed_bytes32};

host.tx_context.blob_hashes = blob_hashes.data();
host.tx_context.blob_hashes_count = blob_hashes.size();

execute(blobhash(0) + ret_top());
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(output, blob_hashes[0]);

execute(blobhash(1) + ret_top());
EXPECT_OUTPUT_INT(0);

execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
ret_top());
EXPECT_OUTPUT_INT(0);
}

TEST_P(evm, blobhash_two)
{
rev = EVMC_CANCUN;

const std::array blob_hashes{
0x0100000000000000000000000000000000000000000000000000000000000001_bytes32,
0x0100000000000000000000000000000000000000000000000000000000000002_bytes32};

host.tx_context.blob_hashes = blob_hashes.data();
host.tx_context.blob_hashes_count = blob_hashes.size();

for (size_t i = 0; i < blob_hashes.size(); ++i)
{
execute(blobhash(i) + ret_top());
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(output, blob_hashes[i]);
}

execute(blobhash(blob_hashes.size()) + ret_top());
EXPECT_OUTPUT_INT(0);

execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
ret_top());
EXPECT_OUTPUT_INT(0);
}

TEST_P(evm, blobhash_invalid_hash_version)
{
rev = EVMC_CANCUN;

// The BLOBHASH instruction does not care about the hash version,
// it will return whatever is in the array.
const std::array blob_hashes{
0x0000000000000000000000000000000000000000000000000000000000000000_bytes32,
0x0200000000000000000000000000000000000000000000000000000000000000_bytes32};

host.tx_context.blob_hashes = blob_hashes.data();
host.tx_context.blob_hashes_count = blob_hashes.size();

for (size_t i = 0; i < blob_hashes.size(); ++i)
{
execute(blobhash(i) + ret_top());
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(output, blob_hashes[i]);
}

execute(blobhash(blob_hashes.size()) + ret_top());
EXPECT_OUTPUT_INT(0);

execute(blobhash(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_bytes32) +
ret_top());
EXPECT_OUTPUT_INT(0);
}
5 changes: 5 additions & 0 deletions test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ inline bytecode sload(bytecode index)
return index + OP_SLOAD;
}

inline bytecode blobhash(bytecode index)
{
return index + OP_BLOBHASH;
}

template <Opcode kind>
struct call_instruction
{
Expand Down

0 comments on commit 9908478

Please sign in to comment.