Skip to content

Commit

Permalink
fmt +clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
FredCoen committed Jan 29, 2025
1 parent abf2eeb commit 74222aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/instruction_table_override/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub fn transact_custom_opcode<DB: Database, CTX: EthContext + DatabaseGetter<Dat
ctx: &mut CTX,
) -> Result<ResultAndState<HaltReason>, EVMError<<DB as Database>::Error, InvalidTransaction>> {
CustomOpcodeHandler::<CTX, _>::new().run(ctx)
}
}
16 changes: 5 additions & 11 deletions examples/instruction_table_override/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use crate::instructions::CustomInstructionExecutor;
use revm::{
context_interface::{
result::HaltReason,
CfgGetter,
},
handler::{
EthContext, EthError, EthFrame, EthHandler,
EthPrecompileProvider, FrameContext,
},
context_interface::{result::HaltReason, CfgGetter},
handler::{EthContext, EthError, EthFrame, EthHandler, EthPrecompileProvider, FrameContext},
interpreter::{interpreter::EthInterpreter, Host},
precompile::PrecompileErrors,
};
use crate::instructions::CustomInstructionExecutor;

// Our custom handler
pub struct CustomOpcodeHandler<CTX, ERROR> {
Expand All @@ -25,7 +19,7 @@ impl<CTX, ERROR> CustomOpcodeHandler<CTX, ERROR> {
}
}

impl<CTX: CfgGetter + Host, ERROR: From<PrecompileErrors>> Default
impl<CTX: CfgGetter + Host, ERROR: From<PrecompileErrors>> Default
for CustomOpcodeHandler<CTX, ERROR>
{
fn default() -> Self {
Expand All @@ -45,4 +39,4 @@ where
type Frame =
EthFrame<CTX, ERROR, EthInterpreter, FrameContext<Self::Precompiles, Self::Instructions>>;
type HaltReason = HaltReason;
}
}
10 changes: 6 additions & 4 deletions examples/instruction_table_override/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use revm::{
handler::instructions::InstructionExecutor,
interpreter::{
gas, interpreter_types::{LoopControl, StackTrait}, popn_top, table::{make_instruction_table, InstructionTable}, Host, Interpreter, InterpreterAction, InterpreterTypes
gas,
interpreter_types::{LoopControl, StackTrait},
popn_top,
table::{make_instruction_table, InstructionTable},
Host, Interpreter, InterpreterAction, InterpreterTypes,
},
primitives::U256,
};
Expand All @@ -24,7 +28,6 @@ where
}
}


impl<WIRE, HOST> CustomInstructionExecutor<WIRE, HOST>
where
WIRE: InterpreterTypes,
Expand All @@ -46,7 +49,6 @@ where
}
}


impl<IT, CTX> InstructionExecutor for CustomInstructionExecutor<IT, CTX>
where
IT: InterpreterTypes,
Expand All @@ -73,4 +75,4 @@ where
fn default() -> Self {
Self::new()
}
}
}
37 changes: 19 additions & 18 deletions examples/instruction_table_override/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
use database::{BenchmarkDB, FFADDRESS};
use revm::{
context::Context, database_interface::EmptyDB, primitives::{bytes::Bytes, Address, TxKind}, state::Bytecode
context::Context,
primitives::TxKind,
state::Bytecode,
};
use std::str::FromStr;

use crate::exec::transact_custom_opcode;

pub mod exec;
pub mod handler;
pub mod instructions;
fn main() {
let bytecode = Bytecode::new_legacy([
0x60, 0x01, // PUSH1 1 -> value to clz
0x5f, // CLZ -> should be 255
0x60, 0x00, // PUSH1 0 -> memory starting position
0x52, // MSTORE -> store 255 to memory at location 0
0x60, 0x20, // PUSH1 32 (length to return)
0x60, 0x00, // PUSH1 0 (memory position)
0xf3, // RETURN
].into());
let bytecode = Bytecode::new_legacy(
[
0x60, 0x01, // PUSH1 1 -> value to clz
0x5f, // CLZ -> should be 255
0x60, 0x00, // PUSH1 0 -> memory starting position
0x52, // MSTORE -> store 255 to memory at location 0
0x60, 0x20, // PUSH1 32 (length to return)
0x60, 0x00, // PUSH1 0 (memory position)
0xf3, // RETURN
]
.into(),
);

let zero_address = Address::default();

let mut ctx = Context::builder()
.with_db(BenchmarkDB::new_bytecode(bytecode)) // Store bytecode in DB
let mut ctx = Context::builder()
.with_db(BenchmarkDB::new_bytecode(bytecode)) // Store bytecode in DB
.modify_tx_chained(|tx| {
tx.kind = TxKind::Call(FFADDRESS); // Call the address where bytecode is stored
tx.kind = TxKind::Call(FFADDRESS); // Call the address where bytecode is stored
});

let result = transact_custom_opcode(&mut ctx).expect("execution failed");
println!("Should return 0xff (255 in decimal) which is the clz of 256 bit value 0x01. The actual return is: {}", result.result.output().unwrap());

}
}

0 comments on commit 74222aa

Please sign in to comment.