From 131d2745d3305fce8096d1fe52674d62753ab50e Mon Sep 17 00:00:00 2001 From: Hai Nguyen Quang Date: Tue, 20 Aug 2024 22:03:36 +0700 Subject: [PATCH] perf: simplify basic account in mv memory --- src/lib.rs | 2 +- src/pevm.rs | 6 ++---- src/vm.rs | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8ceabea4..02dacd68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,7 @@ type BuildIdentityHasher = BuildHasherDefault; // matches & potentially dangerous mismatch mistakes. #[derive(Debug, Clone)] enum MemoryValue { - Basic(Option), + Basic(AccountBasic), CodeHash(Option), Storage(U256), // We lazily update the beneficiary balance to avoid continuous diff --git a/src/pevm.rs b/src/pevm.rs index c35c799b..90e4cd17 100644 --- a/src/pevm.rs +++ b/src/pevm.rs @@ -267,12 +267,10 @@ pub fn execute_revm_parallel { - if let Some(info) = info { - balance = info.balance; - nonce = info.nonce; - } // TODO: Assert that there must be no self-destructed // accounts here. + balance = info.balance; + nonce = info.nonce; } MemoryEntry::Data(_, MemoryValue::LazyRecipient(addition)) => { balance += addition; diff --git a/src/vm.rs b/src/vm.rs index 59ee01f1..c258d55a 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -295,10 +295,11 @@ impl<'a, S: Storage, C: PevmChain> Database for VmDb<'a, S, C> { new_origins.push(origin); match value { MemoryValue::Basic(basic) => { - if basic.is_none() { - return Err(ReadError::SelfDestructedAccount); - } - final_account.clone_from(basic); + // TODO: Return [ReadError::SelfDestructedAccount] if [basic] is + // empty? + // For now we are betting on [code_hash] triggering the sequential + // fallback when we read a self-destructed contract. + final_account = Some(basic.clone()); break; } MemoryValue::LazyRecipient(addition) => { @@ -598,7 +599,9 @@ impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> { let mut lazy_addresses = NewLazyAddresses::new(); for (address, account) in result_and_state.state.iter() { if account.is_selfdestructed() { - write_set.push((self.hash_basic(address), MemoryValue::Basic(None))); + // TODO: Write an empty basic account to [write_set]? + // For now we are betting on [code_hash] triggering the sequential + // fallback when we read a self-destructed contract. write_set.push(( self.hasher.hash_one(MemoryLocation::CodeHash(*address)), MemoryValue::CodeHash(None), @@ -638,10 +641,10 @@ impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> { } else { write_set.push(( account_location_hash, - MemoryValue::Basic(Some(AccountBasic { + MemoryValue::Basic(AccountBasic { balance: account.info.balance, nonce: account.info.nonce, - })), + }), )); } } @@ -744,9 +747,7 @@ impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> { .find(|(location, _)| location == &recipient) { match value { - MemoryValue::Basic(basic) => { - basic.get_or_insert(AccountBasic::default()).balance += amount - } + MemoryValue::Basic(basic) => basic.balance += amount, MemoryValue::LazySender(addition) => *addition -= amount, MemoryValue::LazyRecipient(addition) => *addition += amount, _ => unreachable!(), // TODO: Better error handling