diff --git a/crates/l2/proposer/errors.rs b/crates/l2/proposer/errors.rs index d33cbe7319..3eae74dac3 100644 --- a/crates/l2/proposer/errors.rs +++ b/crates/l2/proposer/errors.rs @@ -80,6 +80,8 @@ pub enum CommitterError { FailedToParseLastCommittedBlock(#[from] FromStrRadixErr), #[error("Committer failed retrieve block from storage: {0}")] FailedToRetrieveBlockFromStorage(#[from] StoreError), + #[error("Committer failed retrieve data from storage")] + FailedToRetrieveDataFromStorage, #[error("Committer failed to generate blobs bundle: {0}")] FailedToGenerateBlobsBundle(#[from] BlobsBundleError), #[error("Committer failed to get information from storage")] diff --git a/crates/l2/proposer/l1_committer.rs b/crates/l2/proposer/l1_committer.rs index 3633d15cf5..c157c0ba08 100644 --- a/crates/l2/proposer/l1_committer.rs +++ b/crates/l2/proposer/l1_committer.rs @@ -17,7 +17,7 @@ use ethrex_core::{ }, Address, H256, U256, }; -use ethrex_storage::Store; +use ethrex_storage::{error::StoreError, Store}; use ethrex_vm::{evm_state, execute_block, get_state_transitions}; use keccak_hash::keccak; use secp256k1::SecretKey; @@ -132,7 +132,7 @@ impl Committer { deposits, )?; - let blobs_bundle = self.generate_blobs_bundle(state_diff.clone())?; + let blobs_bundle = self.generate_blobs_bundle(&state_diff)?; let head_block_hash = block_to_commit.hash(); match self @@ -242,18 +242,36 @@ impl Committer { let account_updates = get_state_transitions(&mut state); let mut modified_accounts = HashMap::new(); - account_updates.iter().for_each(|account_update| { + for account_update in &account_updates { + let prev_nonce = match state + .database() + .ok_or(CommitterError::FailedToRetrieveDataFromStorage)? + // If we want the state_diff of a batch, we will have to change the -1 with the `batch_size` + // and we may have to keep track of the latestCommittedBlock (last block of the batch), + // the batch_size and the latestCommittedBatch in the contract. + .get_account_info(block.header.number - 1, account_update.address) + .map_err(StoreError::from)? + { + Some(acc) => acc.nonce, + None => 0, + }; + modified_accounts.insert( account_update.address, AccountStateDiff { new_balance: account_update.info.clone().map(|info| info.balance), - nonce_diff: account_update.info.clone().map(|info| info.nonce as u16), + nonce_diff: (account_update + .info + .clone() + .ok_or(CommitterError::FailedToRetrieveDataFromStorage)? + .nonce + - prev_nonce) as u16, storage: account_update.added_storage.clone().into_iter().collect(), bytecode: account_update.code.clone(), bytecode_hash: None, }, ); - }); + } let state_diff = StateDiff { modified_accounts, @@ -287,7 +305,7 @@ impl Committer { /// Generate the blob bundle necessary for the EIP-4844 transaction. pub fn generate_blobs_bundle( &self, - state_diff: StateDiff, + state_diff: &StateDiff, ) -> Result { let blob_data = state_diff.encode().map_err(CommitterError::from)?; diff --git a/crates/l2/proposer/state_diff.rs b/crates/l2/proposer/state_diff.rs index 9661813104..c246bb2852 100644 --- a/crates/l2/proposer/state_diff.rs +++ b/crates/l2/proposer/state_diff.rs @@ -8,7 +8,7 @@ use super::errors::StateDiffError; #[derive(Clone)] pub struct AccountStateDiff { pub new_balance: Option, - pub nonce_diff: Option, + pub nonce_diff: u16, pub storage: Vec<(H256, U256)>, pub bytecode: Option, pub bytecode_hash: Option, @@ -125,9 +125,9 @@ impl AccountStateDiff { encoded.extend_from_slice(buf); } - if let Some(nonce_diff) = self.nonce_diff { + if self.nonce_diff != 0 { r#type += AccountStateDiffType::NonceDiff as u8; - encoded.extend(nonce_diff.to_be_bytes()); + encoded.extend(self.nonce_diff.to_be_bytes()); } if !self.storage.is_empty() {