From 1591bf13d7cb7f24af35f3f0193680a87beeb24b Mon Sep 17 00:00:00 2001 From: Timothy Carstens Date: Wed, 29 Nov 2023 21:10:04 -0800 Subject: [PATCH] Store deposits in Epoch --- lib/src/optimism/batches.rs | 10 ++-- lib/src/optimism/derivation.rs | 8 ++- lib/src/optimism/mod.rs | 89 ++++++++++++++-------------------- 3 files changed, 48 insertions(+), 59 deletions(-) diff --git a/lib/src/optimism/batches.rs b/lib/src/optimism/batches.rs index 2ee1cd2b..28b340c7 100644 --- a/lib/src/optimism/batches.rs +++ b/lib/src/optimism/batches.rs @@ -107,8 +107,8 @@ where let batch = if derived_batch.is_none() { let current_l1_block = self.state.current_l1_block_number; let safe_head = self.state.safe_head; - let epoch = self.state.epoch; - let next_epoch = self.state.next_epoch; + let epoch = &self.state.epoch; + let next_epoch = &self.state.next_epoch; let seq_window_size = self.config.seq_window_size; if let Some(next_epoch) = next_epoch { @@ -141,8 +141,8 @@ where } fn batch_status(&self, batch: &Batch) -> BatchStatus { - let epoch = self.state.epoch; - let next_epoch = self.state.next_epoch; + let epoch = &self.state.epoch; + let next_epoch = &self.state.next_epoch; let head = self.state.safe_head; let next_timestamp = head.timestamp + self.config.blocktime; @@ -187,7 +187,7 @@ where let batch_origin = if batch.essence.epoch_num == epoch.number { Some(epoch) } else if batch.essence.epoch_num == epoch.number + 1 { - next_epoch + next_epoch.as_ref() } else { #[cfg(not(target_os = "zkvm"))] log::debug!("invalid batch origin epoch number"); diff --git a/lib/src/optimism/derivation.rs b/lib/src/optimism/derivation.rs index 9a1248b4..50e909e1 100644 --- a/lib/src/optimism/derivation.rs +++ b/lib/src/optimism/derivation.rs @@ -13,7 +13,10 @@ // limitations under the License. use serde::{Deserialize, Serialize}; -use zeth_primitives::{BlockHash, BlockNumber, B256, U256}; +use zeth_primitives::{ + transactions::{optimism::OptimismTxEssence, Transaction}, + BlockHash, BlockNumber, B256, U256, +}; use super::config::ChainConfig; @@ -28,12 +31,13 @@ pub struct BlockInfo { } /// L1 epoch block -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Epoch { pub number: BlockNumber, pub hash: B256, pub timestamp: u64, pub base_fee_per_gas: U256, + pub deposits: Vec>, } #[derive(Debug, Clone, Default)] diff --git a/lib/src/optimism/mod.rs b/lib/src/optimism/mod.rs index d5031f21..2ca213bd 100644 --- a/lib/src/optimism/mod.rs +++ b/lib/src/optimism/mod.rs @@ -103,7 +103,7 @@ impl Default for MemDb { impl BatcherDb for MemDb { fn get_full_op_block(&mut self, block_no: u64) -> Result> { - let op_block = self.full_op_block.get(&block_no).unwrap(); + let op_block = self.full_op_block.remove(&block_no).unwrap(); assert_eq!(block_no, op_block.block_header.number); // Validate tx list @@ -118,18 +118,18 @@ impl BatcherDb for MemDb { } } - Ok(op_block.clone()) + Ok(op_block) } fn get_op_block_header(&mut self, block_no: u64) -> Result
{ - let op_block = self.op_block_header.get(&block_no).unwrap(); + let op_block = self.op_block_header.remove(&block_no).unwrap(); assert_eq!(block_no, op_block.number); - Ok(op_block.clone()) + Ok(op_block) } fn get_full_eth_block(&mut self, block_no: u64) -> Result> { - let eth_block = self.full_eth_block.get(&block_no).unwrap(); + let eth_block = self.full_eth_block.remove(&block_no).unwrap(); assert_eq!(block_no, eth_block.block_header.number); // Validate tx list @@ -167,14 +167,14 @@ impl BatcherDb for MemDb { assert!(!can_contain_config); } - Ok(eth_block.clone()) + Ok(eth_block) } fn get_eth_block_header(&mut self, block_no: u64) -> Result
{ - let eth_block = self.eth_block_header.get(&block_no).unwrap(); + let eth_block = self.eth_block_header.remove(&block_no).unwrap(); assert_eq!(block_no, eth_block.number); - Ok(eth_block.clone()) + Ok(eth_block) } } @@ -284,6 +284,7 @@ impl DeriveMachine { hash: eth_head_hash, timestamp: eth_head.timestamp.try_into().unwrap(), base_fee_per_gas: eth_head.base_fee_per_gas, + deposits: Vec::new(), }, next_epoch: None, }, @@ -343,10 +344,24 @@ impl DeriveMachine { let deposits = if op_batch.essence.epoch_num == self.op_batches.state.epoch.number + 1 { self.op_block_seq_no = 0; - Some(self.derive_deposit_transactions(&op_batch)?) + self.op_batches.state.epoch = self + .op_batches + .state + .next_epoch + .take() + .expect("dequeued future batch without next epoch!"); + + self.op_batches + .state + .epoch + .deposits + .iter() + .map(|tx| tx.to_rlp()) + .collect() } else { self.op_block_seq_no += 1; - None + + Vec::new() }; self.deque_next_epoch_if_none()?; @@ -369,12 +384,7 @@ impl DeriveMachine { let system_tx = self.derive_system_transaction(&op_batch); let derived_transactions: Vec<_> = once(system_tx.to_rlp()) - .chain( - deposits - .unwrap_or_default() - .into_iter() - .map(|tx| tx.to_rlp()), - ) + .chain(deposits) .chain(op_batch.essence.transactions.iter().map(|tx| tx.to_vec())) .collect(); @@ -449,18 +459,11 @@ impl DeriveMachine { ); } - self.op_epoch_queue.push_back(Epoch { - number: self.eth_block_no, - hash: eth_block_hash, - timestamp: eth_block.block_header.timestamp.try_into().unwrap(), - base_fee_per_gas: eth_block.block_header.base_fee_per_gas, - }); - self.deque_next_epoch_if_none()?; - // Update the system config if eth_block.receipts.is_some() { #[cfg(not(target_os = "zkvm"))] info!("Process config"); + self.op_batches .config .system_config @@ -468,6 +471,16 @@ impl DeriveMachine { .context("failed to update system config")?; } + // Enqueue epoch + self.op_epoch_queue.push_back(Epoch { + number: self.eth_block_no, + hash: eth_block_hash, + timestamp: eth_block.block_header.timestamp.try_into().unwrap(), + base_fee_per_gas: eth_block.block_header.base_fee_per_gas, + deposits: deposits::extract_transactions(&self.op_batches.config, ð_block)?, + }); + self.deque_next_epoch_if_none()?; + // Process batcher transactions BatcherTransactions::process( self.op_batches.config.batch_inbox, @@ -485,34 +498,6 @@ impl DeriveMachine { Ok(()) } - fn derive_deposit_transactions( - &mut self, - op_batch: &Batch, - ) -> Result>> { - self.op_batches.state.epoch = self - .op_batches - .state - .next_epoch - .take() - .expect("dequeued future batch without next epoch!"); - - assert_eq!( - self.op_batches.state.epoch.number, - op_batch.essence.epoch_num - ); - - let deposit_block_input = self - .derive_input - .db - .get_full_eth_block(self.op_batches.state.epoch.number) - .context("block not found")?; - - Ok(deposits::extract_transactions( - &self.op_batches.config, - &deposit_block_input, - )?) - } - fn derive_system_transaction(&mut self, op_batch: &Batch) -> Transaction { let batcher_hash = { let all_zero: FixedBytes<12> = FixedBytes([0_u8; 12]);