Skip to content

Commit

Permalink
perf: simplify basic account in mv memory
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise committed Aug 20, 2024
1 parent d9d3bb7 commit 131d274
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type BuildIdentityHasher = BuildHasherDefault<IdentityHasher>;
// matches & potentially dangerous mismatch mistakes.
#[derive(Debug, Clone)]
enum MemoryValue {
Basic(Option<AccountBasic>),
Basic(AccountBasic),
CodeHash(Option<B256>),
Storage(U256),
// We lazily update the beneficiary balance to avoid continuous
Expand Down
6 changes: 2 additions & 4 deletions src/pevm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,10 @@ pub fn execute_revm_parallel<S: Storage + Send + Sync, C: PevmChain + Send + Syn
for (tx_idx, memory_entry) in write_history {
match memory_entry {
MemoryEntry::Data(_, MemoryValue::Basic(info)) => {
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;
Expand Down
21 changes: 11 additions & 10 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
})),
}),
));
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 131d274

Please sign in to comment.