Skip to content

Commit

Permalink
feat(levm): implement blobhash logic (#940)
Browse files Browse the repository at this point in the history
**Motivation**

The motivation is to implement the blobhash opcode, since was not
implemented yet.

**Description**

Main changes:
- Adds an atribute to store the blob hashes in Environment type
- implements blobhash logic, basicly taking an element of the vec of
blob hashes and pushing it to the stack
- To do that, add some posible errors to VMError enum

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #933
  • Loading branch information
maximopalopoli authored Oct 23, 2024
1 parent dac5303 commit 31dc6c0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/vm/levm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum VMError {
OverflowInArithmeticOp,
FatalError,
InvalidTransaction,
MissingBlobHashes,
BlobHashIndexOutOfBounds,
RevertOpcode,
SenderAccountDoesNotExist,
SenderAccountShouldNotHaveBytecode,
Expand Down
24 changes: 21 additions & 3 deletions crates/vm/levm/src/opcode_handlers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
block::LAST_AVAILABLE_BLOCK_LIMIT,
constants::{BLOB_BASE_FEE_UPDATE_FRACTION, MIN_BASE_FEE_PER_BLOB_GAS},
};
use keccak_hash::H256;

// Block Information (11)
// Opcodes: BLOCKHASH, COINBASE, TIMESTAMP, NUMBER, PREVRANDAO, GASLIMIT, CHAINID, SELFBALANCE, BASEFEE, BLOBHASH, BLOBBASEFEE
Expand Down Expand Up @@ -143,16 +144,33 @@ impl VM {
}

// BLOBHASH operation
/// Currently not tested
pub fn op_blobhash(
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::BLOBHASH)?;

// Should push in stack the blob hash
unimplemented!("when we have tx implemented");
let index = current_call_frame.stack.pop()?.as_usize();

// Ok(OpcodeSuccess::Continue)
let blob_hash: H256 = match &self.env.tx_blob_hashes {
Some(vec) => match vec.get(index) {
Some(el) => *el,
None => {
return Err(VMError::BlobHashIndexOutOfBounds);
}
},
None => {
return Err(VMError::MissingBlobHashes);
}
};

// Could not find a better way to translate from H256 to U256
let u256_blob = U256::from(blob_hash.as_bytes());

current_call_frame.stack.push(u256_blob)?;

Ok(OpcodeSuccess::Continue)
}

fn get_blob_gasprice(&mut self) -> U256 {
Expand Down
1 change: 1 addition & 0 deletions crates/vm/levm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ pub fn new_vm_with_ops_addr_bal(bytecode: Bytes, address: Address, balance: U256
state,
Default::default(),
Default::default(),
Default::default(),
)
}
3 changes: 3 additions & 0 deletions crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub struct Environment {
pub gas_price: U256,
pub block_excess_blob_gas: Option<U256>,
pub block_blob_gas_used: Option<U256>,
pub tx_blob_hashes: Option<Vec<H256>>,
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -218,6 +219,7 @@ impl VM {
db: Db,
block_blob_gas_used: Option<U256>,
block_excess_blob_gas: Option<U256>,
tx_blob_hashes: Option<Vec<H256>>,
) -> Self {
// TODO: This handles only CALL transactions.
let bytecode = db.get_account_bytecode(&to);
Expand Down Expand Up @@ -259,6 +261,7 @@ impl VM {
gas_price,
block_blob_gas_used,
block_excess_blob_gas,
tx_blob_hashes,
};

Self {
Expand Down
8 changes: 8 additions & 0 deletions crates/vm/levm/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3860,6 +3860,7 @@ fn caller_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -3901,6 +3902,7 @@ fn origin_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -3968,6 +3970,7 @@ fn address_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -4011,6 +4014,7 @@ fn selfbalance_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -4050,6 +4054,7 @@ fn callvalue_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -4088,6 +4093,7 @@ fn codesize_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -4128,6 +4134,7 @@ fn gasprice_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down Expand Up @@ -4185,6 +4192,7 @@ fn codecopy_op() {
db,
Default::default(),
Default::default(),
Default::default(),
);

let mut current_call_frame = vm.call_frames.pop().unwrap();
Expand Down

0 comments on commit 31dc6c0

Please sign in to comment.