Skip to content

Commit

Permalink
fix: do not save balance when did not change
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed May 24, 2024
1 parent e1d795a commit 69a7a71
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
15 changes: 8 additions & 7 deletions src/eth/evm/revm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ fn parse_revm_state(revm_state: RevmState, mut execution_changes: ExecutionChang
);
let (account_created, account_touched) = (revm_account.is_created(), revm_account.is_touched());

// parse revm to internal representation
// parse revm types to stratus primitives
let mut account: Account = (revm_address, revm_account.info).into();
let account_modified_slots: Vec<Slot> = revm_account
.storage
Expand All @@ -384,7 +384,7 @@ fn parse_revm_state(revm_state: RevmState, mut execution_changes: ExecutionChang
})
.collect();

// status: created
// handle created accounts (contracts)
if account_created {
// parse bytecode slots
let slot_indexes: HashSet<SlotAccess> = match account.bytecode {
Expand All @@ -395,16 +395,17 @@ fn parse_revm_state(revm_state: RevmState, mut execution_changes: ExecutionChang
account.add_bytecode_slot_index(index);
}

execution_changes.insert(account.address, ExecutionAccountChanges::from_modified_values(account, account_modified_slots));
// track account
let account_changes = ExecutionAccountChanges::from_modified_values(account, account_modified_slots);
execution_changes.insert(account_changes.address, account_changes);
}
// status: touched
// handle touched accounts (everything else that is not a contract)
else if account_touched {
let Some(touched_account) = execution_changes.get_mut(&address) else {
let Some(account_changes) = execution_changes.get_mut(&address) else {
tracing::error!(keys = ?execution_changes.keys(), %address, "account touched, but not loaded by evm");
// TODO: panic! only when in dev-mode or try to refactor to avoid panic!
panic!("Account '{}' was expected to be loaded by EVM, but it was not", address);
};
touched_account.apply_modifications(account, account_modified_slots);
account_changes.apply_modifications(account, account_modified_slots);
}
}
execution_changes
Expand Down
24 changes: 9 additions & 15 deletions src/eth/primitives/execution_account_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::SlotIndexes;
use crate::eth::primitives::Wei;
use crate::ext::not;

/// Changes that happened to an account during a transaction.
#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -74,20 +73,20 @@ impl ExecutionAccountChanges {
/// Updates an existing account state with changes that happened during the transaction.
pub fn apply_modifications(&mut self, modified_account: Account, modified_slots: Vec<Slot>) {
// update nonce if modified
let nonce_modified = match self.nonce.take_original_ref() {
Some(nonce) => *nonce != modified_account.nonce,
let is_nonce_modified = match self.nonce.take_original_ref() {
Some(original_nonce) => *original_nonce != modified_account.nonce,
None => true,
};
if nonce_modified {
if is_nonce_modified {
self.nonce.set_modified(modified_account.nonce);
}

// update balance if modified
let balance_modified = match self.balance.take_modified_ref() {
Some(balance) => *balance != modified_account.balance,
let is_balance_modified = match self.balance.take_original_ref() {
Some(original_balance) => *original_balance != modified_account.balance,
None => true,
};
if balance_modified {
if is_balance_modified {
self.balance.set_modified(modified_account.balance);
}

Expand All @@ -105,17 +104,12 @@ impl ExecutionAccountChanges {
}

/// Checks if the account was created by this transaction.
pub fn is_account_creation(&self) -> bool {
pub fn is_creation(&self) -> bool {
self.new_account
}

/// Checks if the account was updated by this transaction (it must already exist).
pub fn is_account_update(&self) -> bool {
not(self.new_account)
}

/// Checks if the Nonce, Balance or Bytecode are modified
pub fn is_account_change(&self) -> bool {
/// Checks if nonce, balance or bytecode were modified.
pub fn is_changed(&self) -> bool {
self.nonce.is_modified() || self.balance.is_modified() || self.bytecode.is_modified()
}
}
2 changes: 1 addition & 1 deletion src/eth/storage/csv/csv_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl CsvExporter {

fn export_account_changes(&mut self, now: String, number: BlockNumber, changes: Vec<ExecutionAccountChanges>) -> anyhow::Result<()> {
for change in changes {
if change.is_account_creation() {
if change.is_creation() {
self.accounts_id.value += 1;
let change_bytecode = change.bytecode.take_ref().and_then(|x| x.clone().map(to_bytea)).unwrap_or(NULL.to_string());
let row = [
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/rocks/rocks_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl RocksStorageState {

let account_changes_future = tokio::task::spawn_blocking(move || {
for change in changes_clone_for_accounts {
if change.is_account_change() {
if change.is_changed() {
let address: AddressRocksdb = change.address.into();
let mut account_info_entry = accounts.entry_or_insert_with(address, AccountRocksdb::default);
if let Some(nonce) = change.nonce.clone().take_modified() {
Expand Down

0 comments on commit 69a7a71

Please sign in to comment.