Skip to content

Commit 91f2d1a

Browse files
authored
Apply morph203 changes (#4)
* apply morph203 changes * fix test * fix clippy * update spec id --------- Co-authored-by: chengwenxi <22697326+chengwenxi@users.noreply.github.com>
1 parent 028eb30 commit 91f2d1a

10 files changed

Lines changed: 64 additions & 26 deletions

File tree

crates/interpreter/src/opcode/tables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum InstructionTables<'a, H: ?Sized> {
2727
Boxed(BoxedInstructionTable<'a, H>),
2828
}
2929

30-
impl<'a, H: Host + ?Sized> InstructionTables<'a, H> {
30+
impl<H: Host + ?Sized> InstructionTables<'_, H> {
3131
/// Creates a plain instruction table for the given spec. See [`make_instruction_table`].
3232
#[inline]
3333
pub const fn new_plain<SPEC: Spec>() -> Self {

crates/precompile/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use once_cell::race::OnceBox;
4141
use std::{boxed::Box, vec::Vec};
4242

4343
pub fn calc_linear_cost_u32(len: usize, base: u64, word: u64) -> u64 {
44-
(len as u64 + 32 - 1) / 32 * word + base
44+
(len as u64).div_ceil(32) * word + base
4545
}
4646

4747
#[derive(Clone, Default, Debug)]
@@ -64,6 +64,8 @@ impl Precompiles {
6464
PrecompileSpecId::PRE_BERNOULLI => Self::pre_bernoulli(),
6565
#[cfg(feature = "morph")]
6666
PrecompileSpecId::BERNOULLI => Self::bernoulli(),
67+
#[cfg(feature = "morph")]
68+
PrecompileSpecId::MORPH203 => Self::morph203(),
6769
PrecompileSpecId::CANCUN => Self::cancun(),
6870
PrecompileSpecId::PRAGUE => Self::prague(),
6971
PrecompileSpecId::LATEST => Self::latest(),
@@ -217,6 +219,22 @@ impl Precompiles {
217219
})
218220
}
219221

222+
/// Returns precompiles for Morph
223+
#[cfg(feature = "morph")]
224+
pub fn morph203() -> &'static Self {
225+
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
226+
INSTANCE.get_or_init(|| {
227+
let mut precompiles = Self::bernoulli().clone();
228+
precompiles.extend([
229+
hash::RIPEMD160, // 0x03
230+
modexp::BERLIN, // 0x05
231+
bn128::pair::ISTANBUL, // 0x08
232+
blake2::FUN, // 0x09
233+
]);
234+
Box::new(precompiles)
235+
})
236+
}
237+
220238
/// Returns the precompiles for the latest spec.
221239
pub fn latest() -> &'static Self {
222240
Self::prague()
@@ -318,6 +336,8 @@ pub enum PrecompileSpecId {
318336
PRE_BERNOULLI,
319337
#[cfg(feature = "morph")]
320338
BERNOULLI,
339+
#[cfg(feature = "morph")]
340+
MORPH203,
321341
CANCUN,
322342
PRAGUE,
323343
LATEST,
@@ -345,6 +365,8 @@ impl PrecompileSpecId {
345365
PRE_BERNOULLI => Self::PRE_BERNOULLI,
346366
#[cfg(feature = "morph")]
347367
BERNOULLI | CURIE => Self::BERNOULLI,
368+
#[cfg(feature = "morph")]
369+
MORPH203 => Self::MORPH203,
348370
}
349371
}
350372
}

crates/primitives/src/specification.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ pub enum SpecId {
110110
/// Although the Curie update include new opcodes in Cancun, the most important change
111111
/// `EIP-4844` is not included. So we sort it before Cancun.
112112
CURIE = 19,
113-
CANCUN = 20,
114-
PRAGUE = 21,
115-
PRAGUE_EOF = 22,
113+
MORPH203 = 20, // revert Precompiles: RIPEMD-160, point evaluation, modexp, ecPairing
114+
CANCUN = 21,
115+
PRAGUE = 22,
116+
PRAGUE_EOF = 23,
116117
#[default]
117118
LATEST = u8::MAX,
118119
}
@@ -174,6 +175,8 @@ impl From<&str> for SpecId {
174175
"Bernoulli" => SpecId::BERNOULLI,
175176
#[cfg(feature = "morph")]
176177
"Curie" => SpecId::CURIE,
178+
#[cfg(feature = "morph")]
179+
"Morph203" => SpecId::MORPH203,
177180
_ => Self::LATEST,
178181
}
179182
}
@@ -220,6 +223,8 @@ impl From<SpecId> for &'static str {
220223
SpecId::BERNOULLI => "Bernoulli",
221224
#[cfg(feature = "morph")]
222225
SpecId::CURIE => "Curie",
226+
#[cfg(feature = "morph")]
227+
SpecId::MORPH203 => "Morph203",
223228
SpecId::LATEST => "Latest",
224229
}
225230
}
@@ -290,6 +295,8 @@ spec!(PRE_BERNOULLI, PreBernoulliSpec);
290295
#[cfg(feature = "morph")]
291296
spec!(BERNOULLI, BernoulliSpec);
292297
#[cfg(feature = "morph")]
298+
spec!(MORPH203, Morph203Spec);
299+
#[cfg(feature = "morph")]
293300
spec!(CURIE, CurieSpec);
294301

295302
#[cfg(not(any(feature = "optimism", feature = "morph")))]
@@ -534,6 +541,10 @@ macro_rules! spec_to_generic {
534541
use $crate::BernoulliSpec as SPEC;
535542
$e
536543
}
544+
$crate::SpecId::MORPH203 => {
545+
use $crate::Morph203Spec as SPEC;
546+
$e
547+
}
537548
$crate::SpecId::CURIE => {
538549
use $crate::CurieSpec as SPEC;
539550
$e
@@ -578,6 +589,8 @@ mod tests {
578589
#[cfg(feature = "morph")]
579590
spec_to_generic!(BERNOULLI, assert_eq!(SPEC::SPEC_ID, BERNOULLI));
580591
#[cfg(feature = "morph")]
592+
spec_to_generic!(MORPH203, assert_eq!(SPEC::SPEC_ID, MORPH203));
593+
#[cfg(feature = "morph")]
581594
spec_to_generic!(CURIE, assert_eq!(SPEC::SPEC_ID, CURIE));
582595
spec_to_generic!(CANCUN, assert_eq!(SPEC::SPEC_ID, CANCUN));
583596
#[cfg(feature = "optimism")]

crates/revm/src/context/context_precompiles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<DB: Database> ContextPrecompiles<DB> {
9595

9696
/// Returns precompiles addresses.
9797
#[inline]
98-
pub fn addresses<'a>(&'a self) -> Box<dyn ExactSizeIterator<Item = &Address> + 'a> {
98+
pub fn addresses<'a>(&'a self) -> Box<dyn ExactSizeIterator<Item = &'a Address> + 'a> {
9999
match self.inner {
100100
PrecompilesCow::StaticRef(inner) => Box::new(inner.addresses()),
101101
PrecompilesCow::Owned(ref inner) => Box::new(inner.keys()),

crates/revm/src/context/inner_evm_context.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ impl<DB: Database> InnerEvmContext<DB> {
227227
.map(|acc| (acc.info.code_size, acc.is_cold))
228228
}
229229

230-
/// Get code hash of address.
231-
///
232-
233230
/// Get code hash of address.
234231
///
235232
/// In case of EOF account it will return `EOF_MAGIC_HASH`

crates/revm/src/handler/handle_types/execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> ExecutionHandler<'a, EXT, DB> {
162162
}
163163
}
164164

165-
impl<'a, EXT, DB: Database> ExecutionHandler<'a, EXT, DB> {
165+
impl<EXT, DB: Database> ExecutionHandler<'_, EXT, DB> {
166166
/// Executes single frame.
167167
#[inline]
168168
pub fn execute_frame(

crates/revm/src/handler/handle_types/post_execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> PostExecutionHandler<'a, EXT, DB> {
7474
}
7575
}
7676

77-
impl<'a, EXT, DB: Database> PostExecutionHandler<'a, EXT, DB> {
77+
impl<EXT, DB: Database> PostExecutionHandler<'_, EXT, DB> {
7878
/// Calculate final refund
7979
pub fn refund(&self, context: &mut Context<EXT, DB>, gas: &mut Gas, eip7702_refund: i64) {
8080
(self.refund)(context, gas, eip7702_refund)

crates/revm/src/handler/handle_types/pre_execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> PreExecutionHandler<'a, EXT, DB> {
4545
}
4646
}
4747

48-
impl<'a, EXT, DB: Database> PreExecutionHandler<'a, EXT, DB> {
48+
impl<EXT, DB: Database> PreExecutionHandler<'_, EXT, DB> {
4949
/// Deduct caller to its limit.
5050
pub fn deduct_caller(&self, context: &mut Context<EXT, DB>) -> Result<(), EVMError<DB::Error>> {
5151
(self.deduct_caller)(context)

crates/revm/src/handler/handle_types/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> ValidationHandler<'a, EXT, DB> {
3939
}
4040
}
4141

42-
impl<'a, EXT, DB: Database> ValidationHandler<'a, EXT, DB> {
42+
impl<EXT, DB: Database> ValidationHandler<'_, EXT, DB> {
4343
/// Validate env.
4444
pub fn env(&self, env: &Env) -> Result<(), EVMError<DB::Error>> {
4545
(self.env)(env)

crates/revm/src/handler/mainnet/validation.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,34 @@ pub fn validate_initial_tx_gas<SPEC: Spec, DB: Database>(
4949
.map(|l| l.len() as u64)
5050
.unwrap_or_default();
5151

52-
let mut initial_gas_spend = gas::validate_initial_tx_gas(
52+
#[cfg(not(feature = "morph"))]
53+
let initial_gas_spend = gas::validate_initial_tx_gas(
5354
SPEC::SPEC_ID,
5455
input,
5556
is_create,
5657
access_list,
5758
authorization_list_num,
5859
);
5960

61+
#[cfg(feature = "morph")]
62+
let initial_gas_spend = {
63+
let gas = gas::validate_initial_tx_gas(
64+
SPEC::SPEC_ID,
65+
input,
66+
is_create,
67+
access_list,
68+
authorization_list_num,
69+
);
70+
if gas > env.tx.gas_limit && env.tx.morph.is_l1_msg {
71+
env.tx.gas_limit
72+
} else {
73+
gas
74+
}
75+
};
76+
6077
// Additional check to see if limit is big enough to cover initial gas.
6178
if initial_gas_spend > env.tx.gas_limit {
62-
cfg_if::cfg_if! {
63-
if #[cfg(not(feature = "morph"))] {
64-
return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into());
65-
} else {
66-
// reset initial gas spend for l1 message to ensure execution doesn't fail
67-
if env.tx.morph.is_l1_msg {
68-
initial_gas_spend = env.tx.gas_limit
69-
} else {
70-
return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into());
71-
}
72-
}
73-
}
79+
return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into());
7480
}
7581
Ok(initial_gas_spend)
7682
}

0 commit comments

Comments
 (0)