Skip to content

Commit

Permalink
feat: optional nonce check (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann authored Mar 14, 2024
1 parent 250edd8 commit fe841be
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 2 additions & 0 deletions crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dev = [
"optional_gas_refund",
"optional_no_base_fee",
"optional_beneficiary_reward",
"optional_nonce_check",
]
memory_limit = ["revm-primitives/memory_limit"]
optional_balance_check = ["revm-primitives/optional_balance_check"]
Expand All @@ -56,3 +57,4 @@ optional_eip3607 = ["revm-primitives/optional_eip3607"]
optional_gas_refund = ["revm-primitives/optional_gas_refund"]
optional_no_base_fee = ["revm-primitives/optional_no_base_fee"]
optional_beneficiary_reward = ["revm-primitives/optional_beneficiary_reward"]
optional_nonce_check = ["revm-primitives/optional_nonce_check"]
2 changes: 2 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dev = [
"optional_gas_refund",
"optional_no_base_fee",
"optional_beneficiary_reward",
"optional_nonce_check",
]
memory_limit = []
optional_balance_check = []
Expand All @@ -87,6 +88,7 @@ optional_eip3607 = []
optional_gas_refund = []
optional_no_base_fee = []
optional_beneficiary_reward = []
optional_nonce_check = []

# See comments in `revm-precompile`
c-kzg = ["dep:c-kzg", "dep:once_cell", "dep:derive_more"]
35 changes: 26 additions & 9 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,18 @@ impl Env {
}

// Check that the transaction's nonce is correct
if let Some(tx) = self.tx.nonce {
let state = account.info.nonce;
match tx.cmp(&state) {
Ordering::Greater => {
return Err(InvalidTransaction::NonceTooHigh { tx, state });
}
Ordering::Less => {
return Err(InvalidTransaction::NonceTooLow { tx, state });
if !self.cfg.is_nonce_check_disabled() {
if let Some(tx) = self.tx.nonce {
let state = account.info.nonce;
match tx.cmp(&state) {
Ordering::Greater => {
return Err(InvalidTransaction::NonceTooHigh { tx, state });
}
Ordering::Less => {
return Err(InvalidTransaction::NonceTooLow { tx, state });
}
_ => {}
}
_ => {}
}
}

Expand Down Expand Up @@ -299,6 +301,9 @@ pub struct CfgEnv {
/// By default, it is set to `false`.
#[cfg(feature = "optional_beneficiary_reward")]
pub disable_beneficiary_reward: bool,
/// Skip nonce checks if true.
#[cfg(feature = "optional_nonce_check")]
pub disable_nonce_check: bool,
}

impl CfgEnv {
Expand Down Expand Up @@ -361,6 +366,16 @@ impl CfgEnv {
pub fn is_beneficiary_reward_disabled(&self) -> bool {
false
}

#[cfg(feature = "optional_nonce_check")]
pub fn is_nonce_check_disabled(&self) -> bool {
self.disable_nonce_check
}

#[cfg(not(feature = "optional_nonce_check"))]
pub fn is_nonce_check_disabled(&self) -> bool {
false
}
}

impl Default for CfgEnv {
Expand All @@ -385,6 +400,8 @@ impl Default for CfgEnv {
disable_base_fee: false,
#[cfg(feature = "optional_beneficiary_reward")]
disable_beneficiary_reward: false,
#[cfg(feature = "optional_nonce_check")]
disable_nonce_check: false,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ dev = [
"optional_gas_refund",
"optional_no_base_fee",
"optional_beneficiary_reward",
"optional_nonce_check",
]
memory_limit = ["revm-interpreter/memory_limit"]
optional_balance_check = ["revm-interpreter/optional_balance_check"]
Expand All @@ -98,6 +99,7 @@ optional_eip3607 = ["revm-interpreter/optional_eip3607"]
optional_gas_refund = ["revm-interpreter/optional_gas_refund"]
optional_no_base_fee = ["revm-interpreter/optional_no_base_fee"]
optional_beneficiary_reward = ["revm-interpreter/optional_beneficiary_reward"]
optional_nonce_check = ["revm-interpreter/optional_nonce_check"]

# See comments in `revm-precompile`
secp256k1 = ["revm-precompile/secp256k1"]
Expand Down

0 comments on commit fe841be

Please sign in to comment.