From d0c335a1d9b84c6cb17f0526037367f912220840 Mon Sep 17 00:00:00 2001 From: Tim Carstens Date: Mon, 22 Jan 2024 14:00:07 -0800 Subject: [PATCH] Simplify BatcherDb --- host/src/bin/op-derive.rs | 25 ++--- lib/src/optimism/batcher_db.rs | 161 +++++++++++++++++---------------- lib/src/optimism/mod.rs | 8 +- 3 files changed, 98 insertions(+), 96 deletions(-) diff --git a/host/src/bin/op-derive.rs b/host/src/bin/op-derive.rs index d7d00cf6..c426351e 100644 --- a/host/src/bin/op-derive.rs +++ b/host/src/bin/op-derive.rs @@ -354,6 +354,10 @@ impl RpcDb { } impl BatcherDb for RpcDb { + fn validate(&self) -> Result<()> { + Ok(()) + } + fn get_full_op_block(&mut self, block_no: u64) -> Result> { let mut provider = new_provider( op_cache_path(&self.cache, block_no), @@ -390,7 +394,7 @@ impl BatcherDb for RpcDb { Ok(header) } - fn get_full_eth_block(&mut self, block_no: u64) -> Result> { + fn get_full_eth_block(&mut self, block_no: u64) -> Result<&BlockInput> { let query = BlockQuery { block_no }; let mut provider = new_provider( eth_cache_path(&self.cache, block_no), @@ -430,23 +434,8 @@ impl BatcherDb for RpcDb { receipts, } }; - self.mem_db.full_eth_block.insert(block_no, block.clone()); + self.mem_db.full_eth_block.insert(block_no, block); provider.save()?; - Ok(block) - } - - fn get_eth_block_header(&mut self, block_no: u64) -> Result
{ - let mut provider = new_provider( - eth_cache_path(&self.cache, block_no), - self.eth_rpc_url.clone(), - )?; - let header: Header = provider - .get_partial_block(&BlockQuery { block_no })? - .try_into()?; - self.mem_db - .eth_block_header - .insert(block_no, header.clone()); - provider.save()?; - Ok(header) + self.mem_db.get_full_eth_block(block_no) } } diff --git a/lib/src/optimism/batcher_db.rs b/lib/src/optimism/batcher_db.rs index b00b71c6..d88b2297 100644 --- a/lib/src/optimism/batcher_db.rs +++ b/lib/src/optimism/batcher_db.rs @@ -40,10 +40,10 @@ pub struct BlockInput { } pub trait BatcherDb { + fn validate(&self) -> Result<()>; fn get_full_op_block(&mut self, block_no: u64) -> Result>; fn get_op_block_header(&mut self, block_no: u64) -> Result
; - fn get_full_eth_block(&mut self, block_no: u64) -> Result>; - fn get_eth_block_header(&mut self, block_no: u64) -> Result
; + fn get_full_eth_block(&mut self, block_no: u64) -> Result<&BlockInput>; } #[derive(Debug, Clone, Deserialize, Serialize)] @@ -72,28 +72,92 @@ 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.remove(&block_no).unwrap(); - assert_eq!(block_no, op_block.block_header.number); - - // Validate tx list - { - let mut tx_trie = MptNode::default(); - for (tx_no, tx) in op_block.transactions.iter().enumerate() { - let trie_key = tx_no.to_rlp(); - tx_trie.insert_rlp(&trie_key, tx)?; + fn validate(&self) -> Result<()> { + for (block_no, op_block) in &self.full_op_block { + ensure!( + *block_no == op_block.block_header.number, + "Block number mismatch" + ); + + // Validate tx list + { + let mut tx_trie = MptNode::default(); + for (tx_no, tx) in op_block.transactions.iter().enumerate() { + let trie_key = tx_no.to_rlp(); + tx_trie.insert_rlp(&trie_key, tx)?; + } + ensure!( + tx_trie.hash() == op_block.block_header.transactions_root, + "Invalid op block transaction data!" + ); } + + // Validate receipts ensure!( - tx_trie.hash() == op_block.block_header.transactions_root, - "Invalid op block transaction data!" + op_block.receipts.is_none(), + "Op blocks should not contain receipts" ); } - // Validate receipts - ensure!( - op_block.receipts.is_none(), - "Op blocks should not contain receipts" - ); + for (block_no, op_block) in &self.op_block_header { + ensure!(*block_no == op_block.number, "Block number mismatch"); + } + + for (block_no, eth_block) in &self.full_eth_block { + ensure!( + *block_no == eth_block.block_header.number, + "Block number mismatch" + ); + + // Validate tx list + { + let mut tx_trie = MptNode::default(); + for (tx_no, tx) in eth_block.transactions.iter().enumerate() { + let trie_key = tx_no.to_rlp(); + tx_trie.insert_rlp(&trie_key, tx)?; + } + ensure!( + tx_trie.hash() == eth_block.block_header.transactions_root, + "Invalid eth block transaction data!" + ); + } + + // Validate receipts + if eth_block.receipts.is_some() { + let mut receipt_trie = MptNode::default(); + for (tx_no, receipt) in eth_block.receipts.as_ref().unwrap().iter().enumerate() { + let trie_key = tx_no.to_rlp(); + receipt_trie.insert_rlp(&trie_key, receipt)?; + } + ensure!( + receipt_trie.hash() == eth_block.block_header.receipts_root, + "Invalid eth block receipt data!" + ); + } else { + let can_contain_deposits = deposits::can_contain( + &OPTIMISM_CHAIN_SPEC.deposit_contract, + ð_block.block_header.logs_bloom, + ); + let can_contain_config = system_config::can_contain( + &OPTIMISM_CHAIN_SPEC.system_config_contract, + ð_block.block_header.logs_bloom, + ); + ensure!( + !can_contain_deposits, + "Eth block has no receipts, but bloom filter indicates it has deposits" + ); + ensure!( + !can_contain_config, + "Eth block has no receipts, but bloom filter indicates it has config updates" + ); + } + } + + Ok(()) + } + + fn get_full_op_block(&mut self, block_no: u64) -> Result> { + let op_block = self.full_op_block.remove(&block_no).unwrap(); Ok(op_block) } @@ -103,67 +167,12 @@ impl BatcherDb for MemDb { .op_block_header .remove(&block_no) .context("not or no longer in db")?; - assert_eq!(block_no, op_block.number); Ok(op_block) } - fn get_full_eth_block(&mut self, block_no: u64) -> Result> { - let eth_block = self.full_eth_block.remove(&block_no).unwrap(); - assert_eq!(block_no, eth_block.block_header.number); - - // Validate tx list - { - let mut tx_trie = MptNode::default(); - for (tx_no, tx) in eth_block.transactions.iter().enumerate() { - let trie_key = tx_no.to_rlp(); - tx_trie.insert_rlp(&trie_key, tx)?; - } - ensure!( - tx_trie.hash() == eth_block.block_header.transactions_root, - "Invalid eth block transaction data!" - ); - } - - // Validate receipts - if eth_block.receipts.is_some() { - let mut receipt_trie = MptNode::default(); - for (tx_no, receipt) in eth_block.receipts.as_ref().unwrap().iter().enumerate() { - let trie_key = tx_no.to_rlp(); - receipt_trie.insert_rlp(&trie_key, receipt)?; - } - ensure!( - receipt_trie.hash() == eth_block.block_header.receipts_root, - "Invalid eth block receipt data!" - ); - } else { - let can_contain_deposits = deposits::can_contain( - &OPTIMISM_CHAIN_SPEC.deposit_contract, - ð_block.block_header.logs_bloom, - ); - let can_contain_config = system_config::can_contain( - &OPTIMISM_CHAIN_SPEC.system_config_contract, - ð_block.block_header.logs_bloom, - ); - ensure!( - !can_contain_deposits, - "Eth block has no receipts, but bloom filter indicates it has deposits" - ); - ensure!( - !can_contain_config, - "Eth block has no receipts, but bloom filter indicates it has config updates" - ); - } - - Ok(eth_block) - } - - fn get_eth_block_header(&mut self, block_no: u64) -> Result
{ - let eth_block = self - .eth_block_header - .remove(&block_no) - .context("not or no longer in db")?; - assert_eq!(block_no, eth_block.number); + fn get_full_eth_block(&mut self, block_no: u64) -> Result<&BlockInput> { + let eth_block = self.full_eth_block.get(&block_no).unwrap(); Ok(eth_block) } diff --git a/lib/src/optimism/mod.rs b/lib/src/optimism/mod.rs index d4b744da..7ba7ca35 100644 --- a/lib/src/optimism/mod.rs +++ b/lib/src/optimism/mod.rs @@ -105,6 +105,8 @@ pub struct DeriveMachine { impl DeriveMachine { /// Creates a new instance of DeriveMachine. pub fn new(chain_config: &ChainConfig, mut derive_input: DeriveInput) -> Result { + derive_input.db.validate()?; + let op_block_no = derive_input.op_head_block_no; // read system config from op_head (seq_no/epoch_no..etc) @@ -279,8 +281,10 @@ impl DeriveMachine { let l1_epoch_header = self .derive_input .db - .get_eth_block_header(op_batch.essence.epoch_num) - .context("eth block not found")?; + .get_full_eth_block(op_batch.essence.epoch_num) + .context("eth block not found")? + .block_header + .clone(); ensure!( new_op_head.mix_hash == l1_epoch_header.mix_hash, "Invalid op block mix hash"