From 0d8d4063090abfe5dfda1c911c63c6e3bb114325 Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Tue, 3 Sep 2024 15:29:44 +0200 Subject: [PATCH] feat: blobhash opcode (#889) --- crates/evm/src/gas.cairo | 3 +++ .../src/instructions/block_information.cairo | 25 +++++++++++++++++++ crates/evm/src/interpreter.cairo | 4 +++ 3 files changed, 32 insertions(+) diff --git a/crates/evm/src/gas.cairo b/crates/evm/src/gas.cairo index 7d7c63e80..0b4f630cd 100644 --- a/crates/evm/src/gas.cairo +++ b/crates/evm/src/gas.cairo @@ -52,6 +52,9 @@ const INITCODE_WORD_COST: u128 = 2; const CALL_STIPEND: u128 = 2300; +// EIP-4844 +pub const BLOB_HASH_COST: u128 = 3; + /// Defines the gas cost and stipend for executing call opcodes. /// /// # Struct fields diff --git a/crates/evm/src/instructions/block_information.cairo b/crates/evm/src/instructions/block_information.cairo index 84eeb1573..c163de3fa 100644 --- a/crates/evm/src/instructions/block_information.cairo +++ b/crates/evm/src/instructions/block_information.cairo @@ -126,6 +126,16 @@ impl BlockInformation of BlockInformationTrait { self.stack.push(self.env.gas_price.into()) } + /// 0x49 - BLOBHASH + /// Returns the value of the blob hash of the current block + /// Always returns Zero in the context of Kakarot + /// # Specification: https://www.evm.codes/#49?fork=cancun + fn exec_blobhash(ref self: VM) -> Result<(), EVMError> { + self.charge_gas(gas::BLOB_HASH_COST)?; + + self.stack.push(0) + } + /// 0x4A - BLOBBASEFEE /// Returns the value of the blob base-fee of the current block /// Always returns Zero in the context of Kakarot @@ -333,6 +343,20 @@ mod tests { assert(result == 0x00, 'stack top should be zero'); } + #[test] + fn test_blobhash_should_return_zero() { + // Given + let mut vm = VMBuilderTrait::new_with_presets().build(); + + // When + vm.exec_blobhash().unwrap(); + + // Then + assert_eq!(vm.stack.len(), 1); + assert_eq!(vm.stack.peek().unwrap(), 0); + } + + #[test] fn test_blobbasefee_should_return_zero() { // Given @@ -346,6 +370,7 @@ mod tests { assert(vm.stack.peek().unwrap() == 0, 'stack top should be 0'); } + // ************************************************************************* // 0x41: COINBASE // ************************************************************************* diff --git a/crates/evm/src/interpreter.cairo b/crates/evm/src/interpreter.cairo index 42222db5f..f0dc036cc 100644 --- a/crates/evm/src/interpreter.cairo +++ b/crates/evm/src/interpreter.cairo @@ -444,6 +444,10 @@ impl EVMImpl of EVMTrait { // BASEFEE return self.exec_basefee(); } + if opcode == 73 { + // BLOBHASH + return self.exec_blobhash(); + } if opcode == 74 { // BLOBBASEFEE return self.exec_blobbasefee();