From be85bec9fa1076d8efff453acdc4b06330b582de Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:02:32 +0000 Subject: [PATCH 1/6] impl NonceTooHigh/ NonceTooLow --- crates/primitives/src/result.rs | 10 +++++++++- crates/revm/src/evm_impl.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index a1775089f2..0673f0ed09 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -102,8 +102,16 @@ pub enum InvalidTransaction { }, /// Overflow payment in transaction. OverflowPaymentInTransaction, - /// Nonce overflows in transaction, + /// Nonce overflows in transaction. NonceOverflowInTransaction, + NonceTooHigh { + tx: u64, + state: u64, + }, + NonceTooLow { + tx: u64, + state: u64, + }, /// EIP-3860: Limit and meter initcode CreateInitcodeSizeLimit, } diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index d1a1a346f2..cccf55622d 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -98,6 +98,24 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact return Err(InvalidTransaction::RejectCallerWithCode.into()); } + if self.data.env.tx.nonce.is_some() { + let state_nonce = self.data.journaled_state.state.get(&caller).unwrap().info.nonce; + let tx_nonce = self.data.env.tx.nonce.unwrap(); + if state_nonce < tx_nonce { + return Err(InvalidTransaction::NonceTooHigh { + tx: tx_nonce, + state: state_nonce, + } + .into()); + } else if state_nonce > tx_nonce { + return Err(InvalidTransaction::NonceTooLow { + tx: tx_nonce, + state: state_nonce, + } + .into()); + } + } + #[cfg(feature = "optional_balance_check")] let disable_balance_check = self.env().cfg.disable_balance_check; #[cfg(not(feature = "optional_balance_check"))] From 9159d63a08794aeba46c2b706d9aa6ae93950c49 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:06:46 +0000 Subject: [PATCH 2/6] add comment for nonce check --- crates/revm/src/evm_impl.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index cccf55622d..0a37439c27 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -98,6 +98,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact return Err(InvalidTransaction::RejectCallerWithCode.into()); } + // Check that the transaction's nonce is correct if self.data.env.tx.nonce.is_some() { let state_nonce = self.data.journaled_state.state.get(&caller).unwrap().info.nonce; let tx_nonce = self.data.env.tx.nonce.unwrap(); From 64f4f6f09ff78ff3845dd20fc0ad7d9ef6ac3895 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:27:21 +0000 Subject: [PATCH 3/6] lint build fix --- crates/revm/src/evm_impl.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 0a37439c27..06d8495886 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -100,7 +100,14 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact // Check that the transaction's nonce is correct if self.data.env.tx.nonce.is_some() { - let state_nonce = self.data.journaled_state.state.get(&caller).unwrap().info.nonce; + let state_nonce = self. + data. + journaled_state. + state. + get(&caller). + unwrap(). + info. + nonce; let tx_nonce = self.data.env.tx.nonce.unwrap(); if state_nonce < tx_nonce { return Err(InvalidTransaction::NonceTooHigh { From f20b91cfbcc09752b8bd656921759ed5cc89eb40 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:41:09 +0000 Subject: [PATCH 4/6] lint fix --- crates/revm/src/evm_impl.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 06d8495886..c4b72f4a10 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -100,14 +100,14 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact // Check that the transaction's nonce is correct if self.data.env.tx.nonce.is_some() { - let state_nonce = self. - data. - journaled_state. - state. - get(&caller). - unwrap(). - info. - nonce; + let state_nonce = self + .data + .journaled_state + .state + .get(&caller) + .unwrap() + .info + .nonce; let tx_nonce = self.data.env.tx.nonce.unwrap(); if state_nonce < tx_nonce { return Err(InvalidTransaction::NonceTooHigh { From 176488b1ac2ab31d93dc80adb7a4976108017198 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Wed, 22 Feb 2023 11:47:15 +0000 Subject: [PATCH 5/6] fix clippy --- crates/revm/src/evm_impl.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index c4b72f4a10..317be77f95 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -15,6 +15,7 @@ use alloc::vec::Vec; use core::{cmp::min, marker::PhantomData}; use revm_interpreter::{MAX_CODE_SIZE, MAX_INITCODE_SIZE}; use revm_precompile::{Precompile, Precompiles}; +use std::cmp::Ordering; pub struct EVMData<'a, DB: Database> { pub env: &'a mut Env, @@ -109,18 +110,22 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact .info .nonce; let tx_nonce = self.data.env.tx.nonce.unwrap(); - if state_nonce < tx_nonce { - return Err(InvalidTransaction::NonceTooHigh { - tx: tx_nonce, - state: state_nonce, - } - .into()); - } else if state_nonce > tx_nonce { - return Err(InvalidTransaction::NonceTooLow { - tx: tx_nonce, - state: state_nonce, - } - .into()); + match tx_nonce.cmp(&state_nonce) { + Ordering::Greater => { + return Err(InvalidTransaction::NonceTooHigh { + tx: tx_nonce, + state: state_nonce, + } + .into()); + }, + Ordering::Less => { + return Err(InvalidTransaction::NonceTooLow { + tx: tx_nonce, + state: state_nonce, + } + .into()); + }, + _ => {}, } } From 0b9103d717059938c7c9843e566f8528862d7ef3 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Wed, 22 Feb 2023 11:51:34 +0000 Subject: [PATCH 6/6] cargo fmt --- crates/revm/src/evm_impl.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 317be77f95..99e81a8b3b 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -117,15 +117,15 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact state: state_nonce, } .into()); - }, + } Ordering::Less => { return Err(InvalidTransaction::NonceTooLow { tx: tx_nonce, state: state_nonce, } .into()); - }, - _ => {}, + } + _ => {} } }