From 9908478456a6761ff53e1e387e912e468115145d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 21 Jul 2023 14:17:42 +0200 Subject: [PATCH] test: Add BLOBHASH unit tests --- test/unittests/CMakeLists.txt | 1 + test/unittests/evm_eip4844_blobhash_test.cpp | 108 +++++++++++++++++++ test/utils/bytecode.hpp | 5 + 3 files changed, 114 insertions(+) create mode 100644 test/unittests/evm_eip4844_blobhash_test.cpp diff --git a/test/unittests/CMakeLists.txt b/test/unittests/CMakeLists.txt index c7bf98fbdb..7def8d560c 100644 --- a/test/unittests/CMakeLists.txt +++ b/test/unittests/CMakeLists.txt @@ -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 diff --git a/test/unittests/evm_eip4844_blobhash_test.cpp b/test/unittests/evm_eip4844_blobhash_test.cpp new file mode 100644 index 0000000000..1a1320bf2e --- /dev/null +++ b/test/unittests/evm_eip4844_blobhash_test.cpp @@ -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); +} diff --git a/test/utils/bytecode.hpp b/test/utils/bytecode.hpp index 2743fbefb4..7ff41fd1fe 100644 --- a/test/utils/bytecode.hpp +++ b/test/utils/bytecode.hpp @@ -333,6 +333,11 @@ inline bytecode sload(bytecode index) return index + OP_SLOAD; } +inline bytecode blobhash(bytecode index) +{ + return index + OP_BLOBHASH; +} + template struct call_instruction {