Skip to content

Commit

Permalink
Store deposits in Epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
intoverflow committed Nov 30, 2023
1 parent 65d04d8 commit 1591bf1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 59 deletions.
10 changes: 5 additions & 5 deletions lib/src/optimism/batches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions lib/src/optimism/derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Transaction<OptimismTxEssence>>,
}

#[derive(Debug, Clone, Default)]
Expand Down
89 changes: 37 additions & 52 deletions lib/src/optimism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Default for MemDb {

impl BatcherDb for MemDb {
fn get_full_op_block(&mut self, block_no: u64) -> Result<BlockInput<OptimismTxEssence>> {
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
Expand All @@ -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<Header> {
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<BlockInput<EthereumTxEssence>> {
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
Expand Down Expand Up @@ -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<Header> {
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)
}
}

Expand Down Expand Up @@ -284,6 +284,7 @@ impl<D: BatcherDb> DeriveMachine<D> {
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,
},
Expand Down Expand Up @@ -343,10 +344,24 @@ impl<D: BatcherDb> DeriveMachine<D> {
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()?;

Expand All @@ -369,12 +384,7 @@ impl<D: BatcherDb> DeriveMachine<D> {
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();

Expand Down Expand Up @@ -449,25 +459,28 @@ impl<D: BatcherDb> DeriveMachine<D> {
);
}

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
.update(&self.op_batches.config.system_config_contract, &eth_block)
.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, &eth_block)?,
});
self.deque_next_epoch_if_none()?;

// Process batcher transactions
BatcherTransactions::process(
self.op_batches.config.batch_inbox,
Expand All @@ -485,34 +498,6 @@ impl<D: BatcherDb> DeriveMachine<D> {
Ok(())
}

fn derive_deposit_transactions(
&mut self,
op_batch: &Batch,
) -> Result<Vec<Transaction<OptimismTxEssence>>> {
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<OptimismTxEssence> {
let batcher_hash = {
let all_zero: FixedBytes<12> = FixedBytes([0_u8; 12]);
Expand Down

0 comments on commit 1591bf1

Please sign in to comment.