Skip to content

Commit

Permalink
get gas costs from zkevm_opcode_defs directly
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed May 13, 2024
1 parent 789f657 commit 1f0b58c
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 125 deletions.
14 changes: 7 additions & 7 deletions benches/nested_near_call.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,7 +17,7 @@ fn nested_near_call(bencher: Bencher) {
Register1(Register::new(0)),
Immediate1(0),
Immediate2(0),
Always,
Arguments::new(Always, 10),
)],
vec![],
);
Expand All @@ -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],
Expand All @@ -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![],
Expand All @@ -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],
Expand Down
57 changes: 29 additions & 28 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
)
Expand All @@ -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],
)
};
Expand All @@ -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)),
},
Expand All @@ -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 {
Expand All @@ -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) => {
Expand All @@ -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)),
},
Expand All @@ -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::<Heap>(
src1.try_into().unwrap(),
src2,
increment.then_some(out.try_into().unwrap()),
predicate,
arguments,
is_bootloader,
),
zkevm_opcode_defs::UMAOpcode::AuxHeapRead => Instruction::from_load::<AuxHeap>(
src1.try_into().unwrap(),
out.try_into().unwrap(),
increment.then_some(out2),
predicate,
arguments,
),
zkevm_opcode_defs::UMAOpcode::AuxHeapWrite => Instruction::from_store::<AuxHeap>(
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),
Expand All @@ -316,7 +317,7 @@ fn decode(raw: u64, is_bootloader: bool) -> Instruction {
} else {
no_sp_movement
},
predicate,
arguments,
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/instruction_handlers/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Source,
},
instruction::{Instruction, InstructionResult},
predication::{Flags, Predicate},
predication::Flags,
VirtualMachine,
};
use u256::U256;
Expand Down Expand Up @@ -208,13 +208,13 @@ impl Instruction {
src2: Register2,
out: AnyDestination,
out2: <Op::Out2 as SecondOutput>::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)
Expand Down
42 changes: 21 additions & 21 deletions src/instruction_handlers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,44 +106,44 @@ fn increment_tx_number(
}

impl Instruction {
fn from_context<Op: ContextOp>(out: Register1, predicate: Predicate) -> Self {
fn from_context<Op: ContextOp>(out: Register1, arguments: Arguments) -> Self {
Self {
handler: context::<Op>,
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::<This>(out, predicate)
pub fn from_this(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<This>(out, arguments)
}
pub fn from_caller(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<Caller>(out, predicate)
pub fn from_caller(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<Caller>(out, arguments)
}
pub fn from_code_address(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<CodeAddress>(out, predicate)
pub fn from_code_address(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<CodeAddress>(out, arguments)
}
pub fn from_ergs_left(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<ErgsLeft>(out, predicate)
pub fn from_ergs_left(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<ErgsLeft>(out, arguments)
}
pub fn from_context_u128(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<U128>(out, predicate)
pub fn from_context_u128(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<U128>(out, arguments)
}
pub fn from_context_sp(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<SP>(out, predicate)
pub fn from_context_sp(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<SP>(out, arguments)
}
pub fn from_context_meta(out: Register1, predicate: Predicate) -> Self {
Self::from_context::<Meta>(out, predicate)
pub fn from_context_meta(out: Register1, arguments: Arguments) -> Self {
Self::from_context::<Meta>(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,
}
}
}
10 changes: 5 additions & 5 deletions src/instruction_handlers/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())),
Expand All @@ -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())),
Expand Down
Loading

0 comments on commit 1f0b58c

Please sign in to comment.