Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(l2): use nonce diff in state diff #1210

Merged
merged 9 commits into from
Nov 28, 2024
2 changes: 2 additions & 0 deletions crates/l2/proposer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
30 changes: 24 additions & 6 deletions crates/l2/proposer/l1_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<BlobsBundle, CommitterError> {
let blob_data = state_diff.encode().map_err(CommitterError::from)?;

Expand Down
6 changes: 3 additions & 3 deletions crates/l2/proposer/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::errors::StateDiffError;
#[derive(Clone)]
pub struct AccountStateDiff {
pub new_balance: Option<U256>,
pub nonce_diff: Option<u16>,
pub nonce_diff: u16,
pub storage: Vec<(H256, U256)>,
pub bytecode: Option<Bytes>,
pub bytecode_hash: Option<H256>,
Expand Down Expand Up @@ -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() {
Expand Down