From 1f0b58c6491cc45b818e2efcf919ac67ef7c0b88 Mon Sep 17 00:00:00 2001 From: Joonatan Saarhelo Date: Mon, 13 May 2024 15:53:21 +0200 Subject: [PATCH] get gas costs from zkevm_opcode_defs directly --- benches/nested_near_call.rs | 14 +++--- src/decode.rs | 57 +++++++++++++------------ src/instruction_handlers/binop.rs | 6 +-- src/instruction_handlers/context.rs | 42 +++++++++--------- src/instruction_handlers/event.rs | 10 ++--- src/instruction_handlers/far_call.rs | 6 +-- src/instruction_handlers/heap_access.rs | 16 +++---- src/instruction_handlers/jump.rs | 5 +-- src/instruction_handlers/near_call.rs | 6 +-- src/instruction_handlers/nop.rs | 8 ++-- src/instruction_handlers/pointer.rs | 6 +-- src/instruction_handlers/precompiles.rs | 6 +-- src/instruction_handlers/ret.rs | 19 +++------ src/instruction_handlers/storage.rs | 14 +++--- tests/panic.rs | 8 ++-- tests/stipend.rs | 22 +++++++--- 16 files changed, 120 insertions(+), 125 deletions(-) diff --git a/benches/nested_near_call.rs b/benches/nested_near_call.rs index a5cfa6cc..57acd522 100644 --- a/benches/nested_near_call.rs +++ b/benches/nested_near_call.rs @@ -1,6 +1,6 @@ use divan::{black_box, Bencher}; use vm2::{ - addressing_modes::{Immediate1, Immediate2, Register, Register1, Register2}, + addressing_modes::{Arguments, Immediate1, Immediate2, Register, Register1, Register2}, initial_decommit, testworld::TestWorld, Instruction, @@ -17,7 +17,7 @@ fn nested_near_call(bencher: Bencher) { Register1(Register::new(0)), Immediate1(0), Immediate2(0), - Always, + Arguments::new(Always, 10), )], vec![], ); @@ -33,7 +33,7 @@ fn nested_near_call(bencher: Bencher) { program, Address::zero(), vec![], - 80_000_000, + 10_000_000, vm2::Settings { default_aa_code_hash: [0; 32], evm_interpreter_code_hash: [0; 32], @@ -49,19 +49,19 @@ fn nested_near_call(bencher: Bencher) { fn nested_near_call_with_storage_write(bencher: Bencher) { let program = Program::new( vec![ - Instruction::from_ergs_left(Register1(Register::new(1)), Always), + Instruction::from_ergs_left(Register1(Register::new(1)), Arguments::new(Always, 0)), Instruction::from_sstore( // always use same storage slot to get a warm write discount Register1(Register::new(0)), Register2(Register::new(1)), - Always, + Arguments::new(Always, 0), ), Instruction::from_near_call( // zero means pass all gas Register1(Register::new(0)), Immediate1(0), Immediate2(0), - Always, + Arguments::new(Always, 10), ), ], vec![], @@ -78,7 +78,7 @@ fn nested_near_call_with_storage_write(bencher: Bencher) { program, Address::zero(), vec![], - 80_000_000, + 10_000_000, vm2::Settings { default_aa_code_hash: [0; 32], evm_interpreter_code_hash: [0; 32], diff --git a/src/decode.rs b/src/decode.rs index 06f835d0..237d5ca0 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -67,6 +67,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { zkevm_opcode_defs::Condition::Ne => crate::Predicate::IfNotEQ, zkevm_opcode_defs::Condition::GtOrLt => crate::Predicate::IfGtOrLT, }; + let arguments = Arguments::new(predicate, parsed.variant.ergs_price()); let stack_in = RegisterAndImmediate { immediate: parsed.imm_0, @@ -112,7 +113,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { src2, out, $snd, - predicate, + arguments, parsed.variant.flags[SWAP_OPERANDS_FLAG_IDX_FOR_ARITH_OPCODES], parsed.variant.flags[SET_FLAGS_FLAG_IDX], ) @@ -125,7 +126,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { src1, src2, out, - predicate, + arguments, parsed.variant.flags[SWAP_OPERANDS_FLAG_IDX_FOR_PTR_OPCODE], ) }; @@ -147,34 +148,34 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { zkevm_opcode_defs::ShiftOpcode::Rol => binop!(RotateLeft, ()), zkevm_opcode_defs::ShiftOpcode::Ror => binop!(RotateRight, ()), }, - zkevm_opcode_defs::Opcode::Jump(_) => Instruction::from_jump(src1, predicate), + zkevm_opcode_defs::Opcode::Jump(_) => Instruction::from_jump(src1, arguments), zkevm_opcode_defs::Opcode::Context(x) => match x { zkevm_opcode_defs::ContextOpcode::This => { - Instruction::from_this(out.try_into().unwrap(), predicate) + Instruction::from_this(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::Caller => { - Instruction::from_caller(out.try_into().unwrap(), predicate) + Instruction::from_caller(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::CodeAddress => { - Instruction::from_code_address(out.try_into().unwrap(), predicate) + Instruction::from_code_address(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::ErgsLeft => { - Instruction::from_ergs_left(out.try_into().unwrap(), predicate) + Instruction::from_ergs_left(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::GetContextU128 => { - Instruction::from_context_u128(out.try_into().unwrap(), predicate) + Instruction::from_context_u128(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::SetContextU128 => { - Instruction::from_set_context_u128(src1.try_into().unwrap(), predicate) + Instruction::from_set_context_u128(src1.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::Sp => { - Instruction::from_context_sp(out.try_into().unwrap(), predicate) + Instruction::from_context_sp(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::Meta => { - Instruction::from_context_meta(out.try_into().unwrap(), predicate) + Instruction::from_context_meta(out.try_into().unwrap(), arguments) } zkevm_opcode_defs::ContextOpcode::IncrementTxNumber => { - Instruction::from_increment_tx_number(predicate) + Instruction::from_increment_tx_number(arguments) } x => unimplemented_instruction(zkevm_opcode_defs::Opcode::Context(x)), }, @@ -188,7 +189,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { Register1(Register::new(parsed.src0_reg_idx)), Immediate1(parsed.imm_0), Immediate2(parsed.imm_1), - predicate, + arguments, ), zkevm_opcode_defs::Opcode::FarCall(kind) => { let constructor = match kind { @@ -207,7 +208,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { src2, Immediate1(parsed.imm_0), parsed.variant.flags[FAR_CALL_STATIC_FLAG_IDX], - predicate, + arguments, ) } zkevm_opcode_defs::Opcode::Ret(kind) => { @@ -219,40 +220,40 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { }; match kind { zkevm_opcode_defs::RetOpcode::Ok => { - Instruction::from_ret(src1.try_into().unwrap(), label, predicate) + Instruction::from_ret(src1.try_into().unwrap(), label, arguments) } zkevm_opcode_defs::RetOpcode::Revert => { - Instruction::from_revert(src1.try_into().unwrap(), label, predicate) + Instruction::from_revert(src1.try_into().unwrap(), label, arguments) } - zkevm_opcode_defs::RetOpcode::Panic => Instruction::from_panic(label, predicate), + zkevm_opcode_defs::RetOpcode::Panic => Instruction::from_panic(label, arguments), } } zkevm_opcode_defs::Opcode::Log(x) => match x { zkevm_opcode_defs::LogOpcode::StorageRead => Instruction::from_sload( src1.try_into().unwrap(), out.try_into().unwrap(), - predicate, + arguments, ), zkevm_opcode_defs::LogOpcode::StorageWrite => { - Instruction::from_sstore(src1.try_into().unwrap(), src2, predicate) + Instruction::from_sstore(src1.try_into().unwrap(), src2, arguments) } zkevm_opcode_defs::LogOpcode::ToL1Message => Instruction::from_l2_to_l1_message( src1.try_into().unwrap(), src2, parsed.variant.flags[FIRST_MESSAGE_FLAG_IDX], - predicate, + arguments, ), zkevm_opcode_defs::LogOpcode::Event => Instruction::from_event( src1.try_into().unwrap(), src2, parsed.variant.flags[FIRST_MESSAGE_FLAG_IDX], - predicate, + arguments, ), zkevm_opcode_defs::LogOpcode::PrecompileCall => Instruction::from_precompile_call( src1.try_into().unwrap(), src2, out.try_into().unwrap(), - predicate, + arguments, ), x => unimplemented_instruction(zkevm_opcode_defs::Opcode::Log(x)), }, @@ -263,33 +264,33 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { src1.try_into().unwrap(), out.try_into().unwrap(), increment.then_some(out2), - predicate, + arguments, ), zkevm_opcode_defs::UMAOpcode::HeapWrite => Instruction::from_store::( src1.try_into().unwrap(), src2, increment.then_some(out.try_into().unwrap()), - predicate, + arguments, is_bootloader, ), zkevm_opcode_defs::UMAOpcode::AuxHeapRead => Instruction::from_load::( src1.try_into().unwrap(), out.try_into().unwrap(), increment.then_some(out2), - predicate, + arguments, ), zkevm_opcode_defs::UMAOpcode::AuxHeapWrite => Instruction::from_store::( src1.try_into().unwrap(), src2, increment.then_some(out.try_into().unwrap()), - predicate, + arguments, false, ), zkevm_opcode_defs::UMAOpcode::FatPointerRead => Instruction::from_load_pointer( src1.try_into().unwrap(), out.try_into().unwrap(), increment.then_some(out2), - predicate, + arguments, ), zkevm_opcode_defs::UMAOpcode::StaticMemoryRead => unimplemented_instruction( zkevm_opcode_defs::Opcode::UMA(zkevm_opcode_defs::UMAOpcode::StaticMemoryRead), @@ -316,7 +317,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction { } else { no_sp_movement }, - predicate, + arguments, ) } } diff --git a/src/instruction_handlers/binop.rs b/src/instruction_handlers/binop.rs index 5b94a5f5..0800786c 100644 --- a/src/instruction_handlers/binop.rs +++ b/src/instruction_handlers/binop.rs @@ -6,7 +6,7 @@ use crate::{ Source, }, instruction::{Instruction, InstructionResult}, - predication::{Flags, Predicate}, + predication::Flags, VirtualMachine, }; use u256::U256; @@ -208,13 +208,13 @@ impl Instruction { src2: Register2, out: AnyDestination, out2: ::Destination, - predicate: Predicate, + arguments: Arguments, swap: bool, set_flags: bool, ) -> Self { Self { handler: monomorphize!(binop [Op] match_source src1 match_destination out match_boolean swap match_boolean set_flags), - arguments: Arguments::new(predicate, 6) + arguments: arguments .write_source(&src1) .write_source(&src2) .write_destination(&out) diff --git a/src/instruction_handlers/context.rs b/src/instruction_handlers/context.rs index 3a173e2c..66c68212 100644 --- a/src/instruction_handlers/context.rs +++ b/src/instruction_handlers/context.rs @@ -7,7 +7,7 @@ use crate::{ decommit::address_into_u256, instruction::InstructionResult, state::State, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; use u256::U256; use zkevm_opcode_defs::VmMetaParameters; @@ -106,44 +106,44 @@ fn increment_tx_number( } impl Instruction { - fn from_context(out: Register1, predicate: Predicate) -> Self { + fn from_context(out: Register1, arguments: Arguments) -> Self { Self { handler: context::, - arguments: Arguments::new(predicate, 5).write_destination(&out), + arguments: arguments.write_destination(&out), } } - pub fn from_this(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_this(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_caller(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_caller(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_code_address(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_code_address(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_ergs_left(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_ergs_left(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_context_u128(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_context_u128(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_context_sp(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_context_sp(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_context_meta(out: Register1, predicate: Predicate) -> Self { - Self::from_context::(out, predicate) + pub fn from_context_meta(out: Register1, arguments: Arguments) -> Self { + Self::from_context::(out, arguments) } - pub fn from_set_context_u128(src: Register1, predicate: Predicate) -> Self { + pub fn from_set_context_u128(src: Register1, arguments: Arguments) -> Self { Self { handler: set_context_u128, - arguments: Arguments::new(predicate, 5).write_source(&src), + arguments: arguments.write_source(&src), } } - pub fn from_increment_tx_number(predicate: Predicate) -> Self { + pub fn from_increment_tx_number(arguments: Arguments) -> Self { Self { handler: increment_tx_number, - arguments: Arguments::new(predicate, 5), + arguments, } } } diff --git a/src/instruction_handlers/event.rs b/src/instruction_handlers/event.rs index f73c38cf..f32ad6bc 100644 --- a/src/instruction_handlers/event.rs +++ b/src/instruction_handlers/event.rs @@ -3,7 +3,7 @@ use crate::{ addressing_modes::{Arguments, Immediate1, Register1, Register2, Source}, instruction::InstructionResult, modified_world::{Event, L2ToL1Log}, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; use u256::H160; use zkevm_opcode_defs::ADDRESS_EVENT_WRITER; @@ -58,11 +58,11 @@ impl Instruction { key: Register1, value: Register2, is_first: bool, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { handler: event, - arguments: Arguments::new(predicate, 34) + arguments: arguments .write_source(&key) .write_source(&value) .write_source(&Immediate1(is_first.into())), @@ -73,11 +73,11 @@ impl Instruction { key: Register1, value: Register2, is_service: bool, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { handler: l2_to_l1, - arguments: Arguments::new(predicate, 109) + arguments: arguments .write_source(&key) .write_source(&value) .write_source(&Immediate1(is_service.into())), diff --git a/src/instruction_handlers/far_call.rs b/src/instruction_handlers/far_call.rs index d581e03d..a0ecceba 100644 --- a/src/instruction_handlers/far_call.rs +++ b/src/instruction_handlers/far_call.rs @@ -5,7 +5,7 @@ use crate::{ fat_pointer::FatPointer, instruction::InstructionResult, predication::Flags, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; use u256::U256; use zkevm_opcode_defs::{ @@ -223,11 +223,11 @@ impl Instruction { src2: Register2, error_handler: Immediate1, is_static: bool, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { handler: monomorphize!(far_call [MODE] match_boolean is_static), - arguments: Arguments::new(predicate, 183) + arguments: arguments .write_source(&src1) .write_source(&src2) .write_source(&error_handler), diff --git a/src/instruction_handlers/heap_access.rs b/src/instruction_handlers/heap_access.rs index 421134f4..af7c1bd6 100644 --- a/src/instruction_handlers/heap_access.rs +++ b/src/instruction_handlers/heap_access.rs @@ -9,7 +9,7 @@ use crate::{ fat_pointer::FatPointer, instruction::InstructionResult, state::State, - ExecutionEnd, Instruction, Predicate, VirtualMachine, + ExecutionEnd, Instruction, VirtualMachine, }; use u256::U256; use zkevm_opcode_defs::system_params::NEW_KERNEL_FRAME_MEMORY_STIPEND; @@ -180,11 +180,9 @@ impl Instruction { src: RegisterOrImmediate, out: Register1, incremented_out: Option, - predicate: Predicate, + arguments: Arguments, ) -> Self { - let mut arguments = Arguments::new(predicate, 7) - .write_source(&src) - .write_destination(&out); + let mut arguments = arguments.write_source(&src).write_destination(&out); let increment = incremented_out.is_some(); if let Some(out2) = incremented_out { @@ -202,13 +200,13 @@ impl Instruction { src1: RegisterOrImmediate, src2: Register2, incremented_out: Option, - predicate: Predicate, + arguments: Arguments, should_hook: bool, ) -> Self { let increment = incremented_out.is_some(); Self { handler: monomorphize!(store [H] match_reg_imm src1 match_boolean increment match_boolean should_hook), - arguments: Arguments::new(predicate, 13) + arguments: arguments .write_source(&src1) .write_source(&src2) .write_destination(&incremented_out), @@ -220,12 +218,12 @@ impl Instruction { src: Register1, out: Register1, incremented_out: Option, - predicate: Predicate, + arguments: Arguments, ) -> Self { let increment = incremented_out.is_some(); Self { handler: monomorphize!(load_pointer match_boolean increment), - arguments: Arguments::new(predicate, 7) + arguments: arguments .write_source(&src) .write_destination(&out) .write_destination(&incremented_out), diff --git a/src/instruction_handlers/jump.rs b/src/instruction_handlers/jump.rs index 5b7cbd4a..1d03050e 100644 --- a/src/instruction_handlers/jump.rs +++ b/src/instruction_handlers/jump.rs @@ -5,7 +5,6 @@ use crate::{ RelativeStack, Source, }, instruction::{Instruction, InstructionResult}, - predication::Predicate, VirtualMachine, }; @@ -28,10 +27,10 @@ fn jump( use super::monomorphization::*; impl Instruction { - pub fn from_jump(source: AnySource, predicate: Predicate) -> Self { + pub fn from_jump(source: AnySource, arguments: Arguments) -> Self { Self { handler: monomorphize!(jump match_source source), - arguments: Arguments::new(predicate, 6).write_source(&source), + arguments: arguments.write_source(&source), } } } diff --git a/src/instruction_handlers/near_call.rs b/src/instruction_handlers/near_call.rs index 21242a3b..88bec1ec 100644 --- a/src/instruction_handlers/near_call.rs +++ b/src/instruction_handlers/near_call.rs @@ -2,7 +2,7 @@ use crate::{ addressing_modes::{Arguments, Immediate1, Immediate2, Register1, Source}, instruction::InstructionResult, predication::Flags, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; fn near_call(vm: &mut VirtualMachine, instruction: *const Instruction) -> InstructionResult { @@ -34,11 +34,11 @@ impl Instruction { gas: Register1, destination: Immediate1, error_handler: Immediate2, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { handler: near_call, - arguments: Arguments::new(predicate, 25) + arguments: arguments .write_source(&gas) .write_source(&destination) .write_source(&error_handler), diff --git a/src/instruction_handlers/nop.rs b/src/instruction_handlers/nop.rs index 89b81d26..2083b1e2 100644 --- a/src/instruction_handlers/nop.rs +++ b/src/instruction_handlers/nop.rs @@ -2,7 +2,7 @@ use super::common::instruction_boilerplate; use crate::{ addressing_modes::{destination_stack_address, AdvanceStackPointer, Arguments, Source}, instruction::InstructionResult, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; fn nop(vm: &mut VirtualMachine, instruction: *const Instruction) -> InstructionResult { @@ -21,13 +21,11 @@ impl Instruction { pub fn from_nop( pop: AdvanceStackPointer, push: AdvanceStackPointer, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { handler: nop, - arguments: Arguments::new(predicate, 6) - .write_source(&pop) - .write_destination(&push), + arguments: arguments.write_source(&pop).write_destination(&push), } } } diff --git a/src/instruction_handlers/pointer.rs b/src/instruction_handlers/pointer.rs index 4f15b1b2..40650a90 100644 --- a/src/instruction_handlers/pointer.rs +++ b/src/instruction_handlers/pointer.rs @@ -6,7 +6,7 @@ use crate::{ }, fat_pointer::FatPointer, instruction::InstructionResult, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; use u256::U256; @@ -96,12 +96,12 @@ impl Instruction { src1: AnySource, src2: Register2, out: AnyDestination, - predicate: Predicate, + arguments: Arguments, swap: bool, ) -> Self { Self { handler: monomorphize!(ptr [Op] match_source src1 match_destination out match_boolean swap), - arguments: Arguments::new(predicate, 6) + arguments: arguments .write_source(&src1) .write_source(&src2) .write_destination(&out), diff --git a/src/instruction_handlers/precompiles.rs b/src/instruction_handlers/precompiles.rs index 22115979..332cfedc 100644 --- a/src/instruction_handlers/precompiles.rs +++ b/src/instruction_handlers/precompiles.rs @@ -3,7 +3,7 @@ use crate::{ addressing_modes::{Arguments, Destination, Register1, Register2, Source}, instruction::InstructionResult, state::Heaps, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; use u256::U256; use zk_evm_abstractions::{ @@ -134,10 +134,10 @@ impl Instruction { abi: Register1, burn: Register2, out: Register1, - predicate: Predicate, + arguments: Arguments, ) -> Self { Self { - arguments: Arguments::new(predicate, 6) + arguments: arguments .write_source(&abi) .write_source(&burn) .write_destination(&out), diff --git a/src/instruction_handlers/ret.rs b/src/instruction_handlers/ret.rs index 90897565..896516a4 100644 --- a/src/instruction_handlers/ret.rs +++ b/src/instruction_handlers/ret.rs @@ -177,6 +177,7 @@ pub const INVALID_INSTRUCTION: Instruction = Instruction { arguments: Arguments::new(Predicate::Always, INVALID_INSTRUCTION_COST), }; +const RETURN_COST: u32 = 5; pub const PANIC: Instruction = Instruction { handler: ret::<{ ReturnType::Panic as u8 }, false>, arguments: Arguments::new(Predicate::Always, RETURN_COST), @@ -195,37 +196,31 @@ pub(crate) fn free_panic(vm: &mut VirtualMachine) -> InstructionResult { ret::<{ ReturnType::Panic as u8 }, false>(vm, &PANIC) } -const RETURN_COST: u32 = 5; - use super::monomorphization::*; impl Instruction { - pub fn from_ret(src1: Register1, label: Option, predicate: Predicate) -> Self { + pub fn from_ret(src1: Register1, label: Option, arguments: Arguments) -> Self { let to_label = label.is_some(); const RETURN_TYPE: u8 = ReturnType::Normal as u8; Self { handler: monomorphize!(ret [RETURN_TYPE] match_boolean to_label), - arguments: Arguments::new(predicate, RETURN_COST) - .write_source(&src1) - .write_source(&label), + arguments: arguments.write_source(&src1).write_source(&label), } } - pub fn from_revert(src1: Register1, label: Option, predicate: Predicate) -> Self { + pub fn from_revert(src1: Register1, label: Option, arguments: Arguments) -> Self { let to_label = label.is_some(); const RETURN_TYPE: u8 = ReturnType::Revert as u8; Self { handler: monomorphize!(ret [RETURN_TYPE] match_boolean to_label), - arguments: Arguments::new(predicate, RETURN_COST) - .write_source(&src1) - .write_source(&label), + arguments: arguments.write_source(&src1).write_source(&label), } } - pub fn from_panic(label: Option, predicate: Predicate) -> Self { + pub fn from_panic(label: Option, arguments: Arguments) -> Self { let to_label = label.is_some(); const RETURN_TYPE: u8 = ReturnType::Panic as u8; Self { handler: monomorphize!(ret [RETURN_TYPE] match_boolean to_label), - arguments: Arguments::new(predicate, RETURN_COST).write_source(&label), + arguments: arguments.write_source(&label), } } diff --git a/src/instruction_handlers/storage.rs b/src/instruction_handlers/storage.rs index 72ef87c8..a9dfdef6 100644 --- a/src/instruction_handlers/storage.rs +++ b/src/instruction_handlers/storage.rs @@ -7,7 +7,7 @@ use crate::{ Arguments, Destination, Register1, Register2, Source, SLOAD_COST, SSTORE_COST, }, instruction::InstructionResult, - Instruction, Predicate, VirtualMachine, + Instruction, VirtualMachine, }; fn sstore(vm: &mut VirtualMachine, instruction: *const Instruction) -> InstructionResult { @@ -46,24 +46,20 @@ fn sload(vm: &mut VirtualMachine, instruction: *const Instruction) -> Instructio impl Instruction { #[inline(always)] - pub fn from_sstore(src1: Register1, src2: Register2, predicate: Predicate) -> Self { + pub fn from_sstore(src1: Register1, src2: Register2, arguments: Arguments) -> Self { Self { handler: sstore, - arguments: Arguments::new(predicate, SSTORE_COST) - .write_source(&src1) - .write_source(&src2), + arguments: arguments.write_source(&src1).write_source(&src2), } } } impl Instruction { #[inline(always)] - pub fn from_sload(src: Register1, dst: Register1, predicate: Predicate) -> Self { + pub fn from_sload(src: Register1, dst: Register1, arguments: Arguments) -> Self { Self { handler: sload, - arguments: Arguments::new(predicate, SLOAD_COST) - .write_source(&src) - .write_destination(&dst), + arguments: arguments.write_source(&src).write_destination(&dst), } } } diff --git a/tests/panic.rs b/tests/panic.rs index 7eb2a559..33de337d 100644 --- a/tests/panic.rs +++ b/tests/panic.rs @@ -1,6 +1,6 @@ use proptest::prelude::*; use vm2::{ - addressing_modes::{Immediate1, Immediate2, Register, Register1}, + addressing_modes::{Arguments, Immediate1, Immediate2, Register, Register1}, initial_decommit, testworld::TestWorld, ExecutionEnd, Instruction, Predicate, Program, VirtualMachine, @@ -15,15 +15,15 @@ proptest! { Register1(Register::new(0)), Immediate1(1), Immediate2(0xFFFF), - Predicate::Always, + Arguments::new(Predicate::Always, 25), ), - Instruction::from_panic(Some(Immediate1(label)), Predicate::Always), + Instruction::from_panic(Some(Immediate1(label)), Arguments::new(Predicate::Always, 5)), ]; for _ in 0..98 { instructions.push(Instruction::from_ret( Register1(Register::new(0)), None, - vm2::Predicate::Always, + Arguments::new(Predicate::Always, 5), )); } diff --git a/tests/stipend.rs b/tests/stipend.rs index 766ea91f..4256de9d 100644 --- a/tests/stipend.rs +++ b/tests/stipend.rs @@ -2,7 +2,7 @@ use u256::U256; use vm2::{ address_into_u256, addressing_modes::{ - CodePage, Immediate1, Register, Register1, Register2, RegisterAndImmediate, + Arguments, CodePage, Immediate1, Register, Register1, Register2, RegisterAndImmediate, }, initial_decommit, instruction_handlers::{Add, CallingMode}, @@ -33,7 +33,7 @@ fn test_scenario(gas_to_pass: u32) -> (ExecutionEnd, u32) { Register2(r0), Register1(r1).into(), (), - Predicate::Always, + Arguments::new(Predicate::Always, 6), false, false, ), @@ -46,7 +46,7 @@ fn test_scenario(gas_to_pass: u32) -> (ExecutionEnd, u32) { Register2(r0), Register1(r2).into(), (), - Predicate::Always, + Arguments::new(Predicate::Always, 6), false, false, ), @@ -56,9 +56,13 @@ fn test_scenario(gas_to_pass: u32) -> (ExecutionEnd, u32) { // crash on error Immediate1(0xFFFF), false, - Predicate::Always, + Arguments::new(Predicate::Always, 200), + ), + Instruction::from_ret( + Register1(Register::new(0)), + None, + Arguments::new(Predicate::Always, 5), ), - Instruction::from_ret(Register1(Register::new(0)), None, Predicate::Always), ], vec![abi, ethereum_address.into()], ); @@ -70,11 +74,15 @@ fn test_scenario(gas_to_pass: u32) -> (ExecutionEnd, u32) { Register2(r0), Register1(r0).into(), (), - Predicate::Always, + Arguments::new(Predicate::Always, 6), false, false, ), - Instruction::from_ret(Register1(Register::new(0)), None, Predicate::Always), + Instruction::from_ret( + Register1(Register::new(0)), + None, + Arguments::new(Predicate::Always, 5), + ), ], vec![], );