Skip to content

Commit

Permalink
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcashier committed Jan 23, 2024
2 parents ecd68ff + 6867af4 commit 4c18859
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 113 deletions.
27 changes: 17 additions & 10 deletions host/src/bin/op-compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,29 @@ async fn main() -> anyhow::Result<()> {
let eth_head = derive_machine
.derive_input
.db
.get_eth_block_header(eth_head_no)
.context("could not fetch eth head")?;
.get_full_eth_block(eth_head_no)
.context("could not fetch eth head")?
.block_header
.clone();
let derive_output = derive_machine.derive().context("could not derive")?;
let eth_tail = derive_machine
.derive_input
.db
.get_eth_block_header(derive_output.eth_tail.0)
.context("could not fetch eth tail")?;
.get_full_eth_block(derive_output.eth_tail.0)
.context("could not fetch eth tail")?
.block_header
.clone();
let mut eth_chain = vec![eth_head];
for block_no in (eth_head_no + 1)..eth_tail.number {
let eth_block = derive_machine
.derive_input
.db
.get_eth_block_header(block_no)
.context("could not fetch eth block")?;
eth_chain.push(eth_block);
eth_chain.push(
derive_machine
.derive_input
.db
.get_full_eth_block(block_no)
.context("could not fetch eth block")?
.block_header
.clone(),
);
}
eth_chain.push(eth_tail);

Expand Down
2 changes: 1 addition & 1 deletion host/src/bin/op-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn main() -> Result<()> {
let output_mem = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input.clone())
.context("Could not create derive machine")?
.derive()
.unwrap();
.context("could not derive")?;
assert_eq!(output, output_mem);
}

Expand Down
26 changes: 7 additions & 19 deletions lib/src/host/rpc_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn op_cache_path(cache: &Option<PathBuf>, block_no: u64) -> Option<PathBuf> {
.map(|dir| cache_file_path(dir, "optimism", block_no, "json.gz"))
}

#[derive(Clone)]
pub struct RpcDb {
eth_rpc_url: Option<String>,
op_rpc_url: Option<String>,
Expand Down Expand Up @@ -75,6 +74,10 @@ impl RpcDb {
}

impl BatcherDb for RpcDb {
fn validate(&self) -> anyhow::Result<()> {
Ok(())
}

fn get_full_op_block(
&mut self,
block_no: u64,
Expand Down Expand Up @@ -117,7 +120,7 @@ impl BatcherDb for RpcDb {
fn get_full_eth_block(
&mut self,
block_no: u64,
) -> anyhow::Result<BlockInput<EthereumTxEssence>> {
) -> anyhow::Result<&BlockInput<EthereumTxEssence>> {
let query = BlockQuery { block_no };
let mut provider = new_provider(
eth_cache_path(&self.cache, block_no),
Expand Down Expand Up @@ -157,23 +160,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) -> anyhow::Result<Header> {
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)
}
}
163 changes: 89 additions & 74 deletions lib/src/optimism/batcher_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::collections::HashMap;

use anyhow::{ensure, Result};
use anyhow::{ensure, Context, Result};
use serde::{Deserialize, Serialize};
use zeth_primitives::{
block::Header,
Expand All @@ -40,10 +40,10 @@ pub struct BlockInput<E: TxEssence> {
}

pub trait BatcherDb {
fn validate(&self) -> Result<()>;
fn get_full_op_block(&mut self, block_no: u64) -> Result<BlockInput<OptimismTxEssence>>;
fn get_op_block_header(&mut self, block_no: u64) -> Result<Header>;
fn get_full_eth_block(&mut self, block_no: u64) -> Result<BlockInput<EthereumTxEssence>>;
fn get_eth_block_header(&mut self, block_no: u64) -> Result<Header>;
fn get_full_eth_block(&mut self, block_no: u64) -> Result<&BlockInput<EthereumTxEssence>>;
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -72,92 +72,107 @@ 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.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");
}

Ok(op_block)
}
for (block_no, eth_block) in &self.full_eth_block {
ensure!(
*block_no == eth_block.block_header.number,
"Block number mismatch"
);

fn get_op_block_header(&mut self, block_no: u64) -> Result<Header> {
let op_block = self.op_block_header.remove(&block_no).unwrap();
assert_eq!(block_no, op_block.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!"
);
}

Ok(op_block)
// 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,
&eth_block.block_header.logs_bloom,
);
let can_contain_config = system_config::can_contain(
&OPTIMISM_CHAIN_SPEC.system_config_contract,
&eth_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_eth_block(&mut self, block_no: u64) -> Result<BlockInput<EthereumTxEssence>> {
let eth_block = self.full_eth_block.remove(&block_no).unwrap();
assert_eq!(block_no, eth_block.block_header.number);
fn get_full_op_block(&mut self, block_no: u64) -> Result<BlockInput<OptimismTxEssence>> {
let op_block = self.full_op_block.remove(&block_no).unwrap();

// 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!"
);
}
Ok(op_block)
}

// 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,
&eth_block.block_header.logs_bloom,
);
let can_contain_config = system_config::can_contain(
&OPTIMISM_CHAIN_SPEC.system_config_contract,
&eth_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"
);
}
fn get_op_block_header(&mut self, block_no: u64) -> Result<Header> {
let op_block = self
.op_block_header
.remove(&block_no)
.context("not or no longer in db")?;

Ok(eth_block)
Ok(op_block)
}

fn get_eth_block_header(&mut self, block_no: u64) -> Result<Header> {
let eth_block = self.eth_block_header.remove(&block_no).unwrap();
assert_eq!(block_no, eth_block.number);
fn get_full_eth_block(&mut self, block_no: u64) -> Result<&BlockInput<EthereumTxEssence>> {
let eth_block = self.full_eth_block.get(&block_no).unwrap();

Ok(eth_block)
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/optimism/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ use super::system_config::SystemConfig;
pub struct ChainConfig {
/// The initial system config value
pub system_config: SystemConfig,
// The L1 attributes depositor address
/// The L1 attributes depositor address
pub l1_attributes_depositor: Address,
/// The L1 attributes contract
pub l1_attributes_contract: Address,
/// The L2 address accumulating any transaction priority fee
pub sequencer_fee_vault: Address,
/// The batch inbox address
pub batch_inbox: Address,
/// The deposit contract address
Expand Down Expand Up @@ -57,6 +59,7 @@ impl ChainConfig {
},
l1_attributes_depositor: address!("deaddeaddeaddeaddeaddeaddeaddeaddead0001"),
l1_attributes_contract: address!("4200000000000000000000000000000000000015"),
sequencer_fee_vault: address!("4200000000000000000000000000000000000011"),
batch_inbox: address!("ff00000000000000000000000000000000000010"),
deposit_contract: address!("bEb5Fc579115071764c7423A4f12eDde41f106Ed"),
system_config_contract: address!("229047fed2591dbec1eF1118d64F7aF3dB9EB290"),
Expand Down
Loading

0 comments on commit 4c18859

Please sign in to comment.