Skip to content

Commit 478319c

Browse files
committed
add clz opcode
misc reuse code
1 parent 2a72ef1 commit 478319c

File tree

10 files changed

+46
-92
lines changed

10 files changed

+46
-92
lines changed

crates/config/src/configuration.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ build_config! {
201201
(cip145_fix_transition_height, (Option<u64>), None)
202202
// For test only
203203
(align_evm_transition_height, (u64), u64::MAX)
204+
// V3.1
205+
(eip7939_transition_number, (Option<u64>), None)
204206

205207

206208
// Mining section.
@@ -1531,7 +1533,7 @@ impl Configuration {
15311533
.unwrap_or(default_transition_time);
15321534

15331535
//
1534-
// 7702 hardfork (V2.6)
1536+
// 7702 hardfork (V3.0)
15351537
//
15361538
set_conf!(
15371539
self.raw_conf.eoa_code_transition_height.unwrap_or(default_transition_time);
@@ -1548,6 +1550,12 @@ impl Configuration {
15481550
}
15491551
params.transition_heights.align_evm =
15501552
self.raw_conf.align_evm_transition_height;
1553+
1554+
// hardfork (V3.1)
1555+
params.transition_numbers.eip7939 = self
1556+
.raw_conf
1557+
.eip7939_transition_number
1558+
.unwrap_or(default_transition_time);
15511559
}
15521560
}
15531561

crates/execution/executor/src/context.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -533,27 +533,6 @@ impl<'a> ContextTrait for Context<'a> {
533533

534534
fn depth(&self) -> usize { self.depth }
535535

536-
// fn trace_next_instruction(
537-
// &mut self, _pc: usize, _instruction: u8, _current_gas: U256,
538-
// ) -> bool {
539-
// // TODO
540-
// false
541-
// }
542-
543-
// fn trace_prepare_execute(
544-
// &mut self, _pc: usize, _instruction: u8, _gas_cost: U256,
545-
// _mem_written: Option<(usize, usize)>,
546-
// _store_written: Option<(U256, U256)>,
547-
// ) {
548-
// // TODO
549-
// }
550-
551-
// fn trace_executed(
552-
// &mut self, _gas_used: U256, _stack_push: &[U256], _mem: &[u8],
553-
// ) {
554-
// // TODO
555-
// }
556-
557536
fn trace_step(&mut self, interpreter: &dyn vm::InterpreterInfo) {
558537
self.tracer.step(interpreter);
559538
}

crates/execution/executor/src/spec.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ pub struct TransitionsBlockNumber {
115115
pub cip144: BlockNumber,
116116
/// CIP-145: Fix Receipts upon `NotEnoughBalance` Error
117117
pub cip145: BlockNumber,
118+
/// EIP-7939: Count Leading Zeros Instruction
119+
pub eip7939: BlockHeight,
118120
}
119121

120122
#[derive(Default, Debug, Clone)]
@@ -229,6 +231,7 @@ impl CommonParams {
229231
spec.cip_c2_fix = height >= self.transition_heights.cip_c2_fix;
230232
spec.cancun_opcodes = number >= self.transition_numbers.cancun_opcodes;
231233
spec.align_evm = height >= self.transition_heights.align_evm && cip645;
234+
spec.eip7939 = number >= self.transition_numbers.eip7939;
232235

233236
spec.overwrite_gas_plan_by_cip();
234237

crates/execution/vm-interpreter/src/factory.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
// See http://www.gnu.org/licenses/
2020

2121
//! Evm factory.
22-
use super::{interpreter::SharedCache, vmtype::VMType};
22+
use super::{
23+
interpreter::{Interpreter, SharedCache},
24+
vmtype::VMType,
25+
};
2326
use cfx_types::U256;
2427
#[cfg(test)]
2528
use cfx_vm_types::CallType;
@@ -40,7 +43,6 @@ impl Factory {
4043
pub fn create(
4144
&self, params: ActionParams, spec: &Spec, depth: usize,
4245
) -> Box<dyn Exec> {
43-
use super::interpreter::Interpreter;
4446
// Assert there is only one type. Parity Ethereum is dead and no more
4547
// types will be added.
4648
match self.evm {

crates/execution/vm-interpreter/src/instructions.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ enum_with_from_u8! {
106106
SHR = 0x1c,
107107
#[doc = "arithmetic shift right operation"]
108108
SAR = 0x1d,
109+
#[doc = "count leading zeros"]
110+
CLZ = 0x1e,
109111

110112
#[doc = "compute SHA3-256 hash"]
111113
SHA3 = 0x20,
@@ -380,6 +382,9 @@ impl Instruction {
380382
if instruction == Some(BASEFEE) && !spec.cip1559 {
381383
instruction = None;
382384
}
385+
if instruction == Some(CLZ) && !spec.eip7939 {
386+
instruction = None;
387+
}
383388
return instruction;
384389
}
385390

@@ -426,9 +431,13 @@ impl Instruction {
426431
}
427432

428433
/// Returns the instruction info.
429-
pub fn info<const CANCUN: bool>(&self, cip645: bool) -> &InstructionInfo {
434+
pub fn info<const CANCUN: bool>(
435+
&self, cip645: bool, eip7939: bool,
436+
) -> &InstructionInfo {
430437
let instrs = if !CANCUN {
431438
&*INSTRUCTIONS
439+
} else if eip7939 {
440+
&*INSTRUCTIONS_EIP7939
432441
} else if !cip645 {
433442
&*INSTRUCTIONS_CANCUN
434443
} else {
@@ -672,6 +681,13 @@ lazy_static! {
672681

673682
arr
674683
};
684+
685+
pub static ref INSTRUCTIONS_EIP7939: [Option<InstructionInfo>; 0x100] = {
686+
let mut arr = *INSTRUCTIONS_CIP645;
687+
arr[CLZ as usize] = Some(InstructionInfo::new("CLZ", 1, 1, GasPriceTier::Low));
688+
689+
arr
690+
};
675691
}
676692

677693
/// Maximal number of topics for log instructions

crates/execution/vm-interpreter/src/interpreter/mod.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -568,19 +568,6 @@ impl<Cost: CostType, const CANCUN: bool> Interpreter<Cost, CANCUN> {
568568
Instruction::from_u8_versioned(opcode, context.spec());
569569
self.reader.position += 1;
570570

571-
// TODO: make compile-time removable if too much of a
572-
// performance hit.
573-
// self.do_trace = self.do_trace
574-
// && context.trace_next_instruction(
575-
// self.reader.position - 1,
576-
// opcode,
577-
// self.gasometer
578-
// .as_mut()
579-
// .expect(GASOMETER_PROOF)
580-
// .current_gas
581-
// .as_u256(),
582-
// );
583-
584571
let instruction = match instruction {
585572
Some(i) => i,
586573
None => {
@@ -592,8 +579,10 @@ impl<Cost: CostType, const CANCUN: bool> Interpreter<Cost, CANCUN> {
592579
}
593580
};
594581

595-
let info =
596-
instruction.info::<CANCUN>(context.spec().cip645.opcode_update);
582+
let info = instruction.info::<CANCUN>(
583+
context.spec().cip645.opcode_update,
584+
context.spec().eip7939,
585+
);
597586
self.last_stack_ret_len = info.ret;
598587
if let Err(e) = self.verify_instruction(context, instruction, info) {
599588
return Err(InterpreterResult::Done(Err(e)));
@@ -612,15 +601,6 @@ impl<Cost: CostType, const CANCUN: bool> Interpreter<Cost, CANCUN> {
612601
Ok(t) => t,
613602
Err(e) => return Err(InterpreterResult::Done(Err(e))),
614603
};
615-
// if self.do_trace {
616-
// context.trace_prepare_execute(
617-
// self.reader.position - 1,
618-
// opcode,
619-
// requirements.gas_cost.as_u256(),
620-
// Self::mem_written(instruction, &self.stack),
621-
// Self::store_written(instruction, &self.stack),
622-
// );
623-
// }
624604

625605
if let Err(e) = gasometer.verify_gas(&requirements.gas_cost) {
626606
return Err(InterpreterResult::Done(Err(e)));
@@ -1324,19 +1304,6 @@ impl<Cost: CostType, const CANCUN: bool> Interpreter<Cost, CANCUN> {
13241304
let b = self.stack.pop_back();
13251305
self.stack.push(
13261306
if !b.is_zero() {
1327-
// match b {
1328-
// ONE => a,
1329-
// TWO => a >> 1,
1330-
// TWO_POW_5 => a >> 5,
1331-
// TWO_POW_8 => a >> 8,
1332-
// TWO_POW_16 => a >> 16,
1333-
// TWO_POW_24 => a >> 24,
1334-
// TWO_POW_64 => a >> 64,
1335-
// TWO_POW_96 => a >> 96,
1336-
// TWO_POW_224 => a >> 224,
1337-
// TWO_POW_248 => a >> 248,
1338-
// _ => a / b,
1339-
// }
13401307
if b == ONE {
13411308
a
13421309
} else if b == TWO {
@@ -1582,6 +1549,10 @@ impl<Cost: CostType, const CANCUN: bool> Interpreter<Cost, CANCUN> {
15821549
};
15831550
self.stack.push(result);
15841551
}
1552+
instructions::CLZ => {
1553+
let value = self.stack.pop_back();
1554+
self.stack.push(U256::from(value.leading_zeros()))
1555+
}
15851556
};
15861557
Ok(InstructionResult::Ok)
15871558
}

crates/execution/vm-interpreter/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
#[macro_use]
66
extern crate log;
7+
#[macro_use]
8+
extern crate lazy_static;
79

810
mod evm;
911
#[macro_use]
1012
pub mod factory;
1113
pub mod instructions;
1214
mod interpreter;
1315
mod vmtype;
14-
#[macro_use]
15-
extern crate lazy_static;
1616

1717
#[cfg(test)]
1818
mod tests;

crates/execution/vm-types/src/context.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,6 @@ pub trait Context {
173173

174174
fn is_warm_storage_entry(&self, key: &H256) -> Result<bool>;
175175

176-
// /// Decide if any more operations should be traced. Passthrough for the
177-
// VM /// trace.
178-
// fn trace_next_instruction(
179-
// &mut self, _pc: usize, _instruction: u8, _current_gas: U256,
180-
// ) -> bool {
181-
// false
182-
// }
183-
184-
// /// Prepare to trace an operation. Passthrough for the VM trace.
185-
// fn trace_prepare_execute(
186-
// &mut self, _pc: usize, _instruction: u8, _gas_cost: U256,
187-
// _mem_written: Option<(usize, usize)>,
188-
// _store_written: Option<(U256, U256)>,
189-
// ) {
190-
// }
191-
192-
// /// Trace the finalised execution of a single instruction.
193-
// fn trace_executed(
194-
// &mut self, _gas_used: U256, _stack_push: &[U256], _mem: &[u8],
195-
// ) {
196-
// }
197-
198176
fn trace_step(&mut self, interpreter: &dyn InterpreterInfo) {
199177
let _ = interpreter;
200178
}

crates/execution/vm-types/src/spec.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ pub struct Spec {
204204
pub eip7623: bool,
205205
pub align_evm: bool,
206206
pub cip_c2_fix: bool,
207+
/// EIP-7939: Count Leading Zeros Instruction
208+
pub eip7939: bool,
207209
}
208210

209211
/// Represents the feature flags for CIP-645 implementation.
@@ -415,6 +417,7 @@ impl Spec {
415417
eip7623: false,
416418
cip_c2_fix: false,
417419
align_evm: false,
420+
eip7939: false,
418421
}
419422
}
420423

crates/execution/vm-types/src/tests.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,6 @@ impl Context for MockContext {
249249
// reentrancy check.
250250
fn is_static_or_reentrancy(&self) -> bool { self.is_static }
251251

252-
// fn trace_next_instruction(
253-
// &mut self, _pc: usize, _instruction: u8, _gas: U256,
254-
// ) -> bool {
255-
// self.tracing
256-
// }
257-
258252
fn space(&self) -> Space { Space::Native }
259253

260254
fn blockhash_source(&self) -> BlockHashSource { BlockHashSource::Env }

0 commit comments

Comments
 (0)