Skip to content

Commit

Permalink
Add Eth tail to DeriveOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
intoverflow committed Nov 30, 2023
1 parent 1591bf1 commit b9a5eb6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 60 deletions.
7 changes: 4 additions & 3 deletions host/src/bin/op-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ async fn main() -> Result<()> {
}

info!("In-memory test complete");
info!("Head: {}", output_1.head_block_hash);
for derived_hash in &output_1.derived_blocks {
info!("Derived: {}", derived_hash);
info!("Eth tail: {} {}", output_1.eth_tail.0, output_1.eth_tail.1);
info!("Op Head: {} {}", output_1.op_head.0, output_1.op_head.1);
for derived_block in &output_1.derived_op_blocks {
info!("Derived: {} {}", derived_block.0, derived_block.1);
}

// Run in the executor (if requested)
Expand Down
63 changes: 40 additions & 23 deletions lib/src/optimism/batches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,60 @@
// limitations under the License.

use core::cmp::Ordering;
use std::{cmp::Reverse, collections::BinaryHeap, io::Read};
use std::{
cmp::Reverse,
collections::{BinaryHeap, VecDeque},
io::Read,
};

use anyhow::Context;
use libflate::zlib::Decoder;
use zeth_primitives::{
batch::Batch,
rlp::{Decodable, Header},
transactions::ethereum::EthereumTxEssence,
};

use super::{channels::Channel, config::ChainConfig, derivation::State};
use super::{
batcher_transactions::BatcherTransactions,
channels::{Channel, Channels},
config::ChainConfig,
derivation::State,
epoch::BlockInput,
};

pub struct Batches<I> {
pub struct Batches {
/// Mapping of timestamps to batches
batches: BinaryHeap<Reverse<Batch>>,
pub channel_iter: I,
pub channel_iter: Channels<BatcherTransactions>,
pub state: State,
pub config: ChainConfig,
}

impl<I> Iterator for Batches<I>
where
I: Iterator<Item = Channel>,
{
impl Batches {
pub fn new(config: ChainConfig, state: State) -> Batches {
let channel_iter = Channels::new(BatcherTransactions::new(VecDeque::new()), &config);

Batches {
batches: BinaryHeap::new(),
channel_iter,
state,
config,
}
}

pub fn process(&mut self, eth_block: &BlockInput<EthereumTxEssence>) -> anyhow::Result<()> {
BatcherTransactions::process(
self.config.batch_inbox,
self.config.system_config.batch_sender,
eth_block.block_header.number,
&eth_block.transactions,
&mut self.channel_iter.batcher_tx_iter.buffer,
)
}
}

impl Iterator for Batches {
type Item = Batch;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -50,21 +81,7 @@ where
}
}

impl<I> Batches<I> {
pub fn new(channel_iter: I, state: State, config: ChainConfig) -> Batches<I> {
Batches {
batches: BinaryHeap::new(),
channel_iter,
state,
config,
}
}
}

impl<I> Batches<I>
where
I: Iterator<Item = Channel>,
{
impl Batches {
fn try_next(&mut self) -> anyhow::Result<Option<Batch>> {
let channel = self.channel_iter.next();
if let Some(channel) = channel {
Expand Down
53 changes: 19 additions & 34 deletions lib/src/optimism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ use zeth_primitives::{
Transaction, TxEssence,
},
trie::MptNode,
uint, Address, BlockHash, FixedBytes, RlpBytes, B256, U256,
uint, Address, BlockHash, BlockNumber, FixedBytes, RlpBytes, B256, U256,
};

#[cfg(not(target_os = "zkvm"))]
use log::info;

use crate::optimism::{
batcher_transactions::BatcherTransactions,
batches::Batches,
channels::Channels,
config::ChainConfig,
derivation::{BlockInfo, Epoch, State, CHAIN_SPEC},
epoch::BlockInput,
Expand Down Expand Up @@ -187,21 +185,9 @@ pub struct DeriveInput<D> {

#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Serialize)]
pub struct DeriveOutput {
pub head_block_hash: BlockHash,
pub derived_blocks: Vec<BlockHash>,
}

impl DeriveOutput {
pub fn new(head_block_hash: BlockHash) -> Self {
DeriveOutput {
head_block_hash,
derived_blocks: Vec::new(),
}
}

pub fn push(&mut self, l2_hash: BlockHash) {
self.derived_blocks.push(l2_hash);
}
pub eth_tail: (BlockNumber, BlockHash),
pub op_head: (BlockNumber, BlockHash),
pub derived_op_blocks: Vec<(BlockNumber, BlockHash)>,
}

pub struct DeriveMachine<D> {
Expand All @@ -210,7 +196,7 @@ pub struct DeriveMachine<D> {
op_block_no: u64,
op_block_seq_no: u64,
op_epoch_queue: VecDeque<Epoch>,
op_batches: Batches<Channels<BatcherTransactions>>,
op_batches: Batches,
eth_block_no: u64,
}

Expand Down Expand Up @@ -268,10 +254,8 @@ impl<D: BatcherDb> DeriveMachine<D> {
op_chain_config.system_config.l1_fee_overhead = set_l1_block_values.l1_fee_overhead;
op_chain_config.system_config.l1_fee_scalar = set_l1_block_values.l1_fee_scalar;

let channels =
Channels::new(BatcherTransactions::new(VecDeque::new()), &op_chain_config);
Batches::new(
channels,
op_chain_config,
State {
current_l1_block_number: eth_block_no,
current_l1_block_hash: eth_head_hash,
Expand All @@ -288,7 +272,6 @@ impl<D: BatcherDb> DeriveMachine<D> {
},
next_epoch: None,
},
op_chain_config,
)
};

Expand All @@ -307,7 +290,7 @@ impl<D: BatcherDb> DeriveMachine<D> {
let target_block_no =
self.derive_input.op_head_block_no + self.derive_input.op_derive_block_count;

let mut derive_output = DeriveOutput::new(self.op_head_block_hash);
let mut derived_op_blocks = Vec::new();

while self.op_block_no < target_block_no {
#[cfg(not(target_os = "zkvm"))]
Expand Down Expand Up @@ -416,15 +399,22 @@ impl<D: BatcherDb> DeriveMachine<D> {
timestamp: new_op_head.timestamp.try_into().unwrap(),
};

derive_output.push(new_op_head_hash);
derived_op_blocks.push((new_op_head.number, new_op_head_hash));

if self.op_block_no == target_block_no {
break;
}
}
}

Ok(derive_output)
Ok(DeriveOutput {
eth_tail: (
self.op_batches.state.current_l1_block_number,
self.op_batches.state.current_l1_block_hash,
),
op_head: (self.derive_input.op_head_block_no, self.op_head_block_hash),
derived_op_blocks,
})
}

fn deque_next_epoch_if_none(&mut self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -482,14 +472,9 @@ impl<D: BatcherDb> DeriveMachine<D> {
self.deque_next_epoch_if_none()?;

// Process batcher transactions
BatcherTransactions::process(
self.op_batches.config.batch_inbox,
self.op_batches.config.system_config.batch_sender,
eth_block.block_header.number,
&eth_block.transactions,
&mut self.op_batches.channel_iter.batcher_tx_iter.buffer,
)
.context("failed to create batcher transactions")?;
self.op_batches
.process(&eth_block)
.context("failed to create batcher transactions")?;

self.op_batches.state.current_l1_block_number = self.eth_block_no;
self.op_batches.state.current_l1_block_hash = eth_block_hash;
Expand Down

0 comments on commit b9a5eb6

Please sign in to comment.