From 3ce760ea65469f62f0106803b1627fe9d9b563c4 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 21 May 2023 21:01:53 +0200 Subject: [PATCH 1/3] Change nonce type back to U256 --- src/backend/memory.rs | 7 ++++--- src/backend/mod.rs | 2 +- src/executor/stack/executor.rs | 4 ++-- src/executor/stack/memory.rs | 11 +++++++---- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 4910e628e..2de19a183 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -47,7 +47,7 @@ pub struct MemoryVicinity { #[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub struct MemoryAccount { /// Account nonce. - pub nonce: u64, + pub nonce: U256, /// Account balance. pub balance: U256, /// Full account storage. @@ -138,7 +138,7 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { .get(&address) .map(|a| Basic { balance: a.balance, - nonce: a.nonce, + nonce: a.nonce.into(), }) .unwrap_or_default() } @@ -210,7 +210,8 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> { } account.balance == U256::zero() - && account.nonce == 0 && account.code.is_empty() + && account.nonce == U256::zero() + && account.code.is_empty() }; if is_empty && delete_empty { diff --git a/src/backend/mod.rs b/src/backend/mod.rs index c442958c1..7cda7fd1d 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -20,7 +20,7 @@ pub struct Basic { /// Account balance. pub balance: U256, /// Account nonce. - pub nonce: u64, + pub nonce: U256, } pub use ethereum::Log; diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index 4f6f52cd2..4d30981d5 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -618,7 +618,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } /// Get account nonce. - pub fn nonce(&self, address: H160) -> u64 { + pub fn nonce(&self, address: H160) -> U256 { self.state.basic(address).nonce } @@ -734,7 +734,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> return Capture::Exit((ExitError::CreateCollision.into(), None, Vec::new())); } - if self.nonce(address) > 0 { + if self.nonce(address) > U256::zero() { let _ = self.exit_substate(StackExitKind::Failed); return Capture::Exit((ExitError::CreateCollision.into(), None, Vec::new())); } diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index d66c10335..30f7220c6 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -198,14 +198,14 @@ impl<'config> MemoryStackSubstate<'config> { return Some(false); } - if account.basic.nonce != 0 { + if account.basic.nonce != U256::zero() { return Some(false); } if let Some(code) = &account.code { return Some( account.basic.balance == U256::zero() - && account.basic.nonce == 0 + && account.basic.nonce == U256::zero() && code.is_empty(), ); } @@ -303,7 +303,10 @@ impl<'config> MemoryStackSubstate<'config> { pub fn inc_nonce(&mut self, address: H160, backend: &B) -> Result<(), ExitError> { let nonce = &mut self.account_mut(address, backend).basic.nonce; - *nonce = nonce.checked_add(1).ok_or(ExitError::MaxNonce)?; + if *nonce > U256::from(u64::MAX) { + return Err(ExitError::MaxNonce); + } + *nonce += U256::one(); Ok(()) } @@ -499,7 +502,7 @@ impl<'backend, 'config, B: Backend> StackState<'config> for MemoryStackState<'ba } self.backend.basic(address).balance == U256::zero() - && self.backend.basic(address).nonce == 0 + && self.backend.basic(address).nonce == U256::zero() && self.backend.code(address).len() == 0 } From adc660e284c0ecd4ba2839bac615b822bd6bd6e7 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 21 May 2023 21:04:40 +0200 Subject: [PATCH 2/3] Fix clippy --- src/backend/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 2de19a183..37e7718b1 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -138,7 +138,7 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { .get(&address) .map(|a| Basic { balance: a.balance, - nonce: a.nonce.into(), + nonce: a.nonce, }) .unwrap_or_default() } From 1f714a5b0045f029045177ed4536005397b950e4 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 21 May 2023 21:11:26 +0200 Subject: [PATCH 3/3] fix: should be >= --- src/executor/stack/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index 30f7220c6..957b2b3d5 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -303,7 +303,7 @@ impl<'config> MemoryStackSubstate<'config> { pub fn inc_nonce(&mut self, address: H160, backend: &B) -> Result<(), ExitError> { let nonce = &mut self.account_mut(address, backend).basic.nonce; - if *nonce > U256::from(u64::MAX) { + if *nonce >= U256::from(u64::MAX) { return Err(ExitError::MaxNonce); } *nonce += U256::one();