Skip to content

Commit

Permalink
chore(protocol): Replace use of OpBlock with alloy type generic ove…
Browse files Browse the repository at this point in the history
…r transaction (op-rs#47)
  • Loading branch information
emhane authored and sergerad committed Jan 23, 2025
1 parent 9f0c4b2 commit 0f499e4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 53 deletions.
1 change: 1 addition & 0 deletions crates/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true

[dependencies]
# Workspace
maili-common.workspace = true
maili-genesis.workspace = true

# OP Alloy
Expand Down
54 changes: 27 additions & 27 deletions crates/protocol/src/batch/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use alloc::vec::Vec;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::FixedBytes;
use maili_common::{DepositTxEnvelope, DEPOSIT_TX_TYPE_ID};
use maili_genesis::RollupConfig;
use op_alloy_consensus::DEPOSIT_TX_TYPE_ID;
use tracing::{info, warn};

use crate::{
Expand Down Expand Up @@ -497,7 +497,7 @@ mod tests {
use alloy_eips::BlockNumHash;
use alloy_primitives::{b256, Bytes};
use maili_genesis::ChainGenesis;
use op_alloy_consensus::OpBlock;
use op_alloy_consensus::{OpBlock, OpTxEnvelope, OpTxType};
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

Expand Down Expand Up @@ -569,7 +569,7 @@ mod tests {
let l1_blocks = vec![];
let l2_safe_head = L2BlockInfo::default();
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let batch = SpanBatch::default();
assert_eq!(
batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &inclusion_block, &mut fetcher).await,
Expand All @@ -590,7 +590,7 @@ mod tests {
let l1_blocks = vec![BlockInfo::default()];
let l2_safe_head = L2BlockInfo::default();
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let batch = SpanBatch::default();
assert_eq!(
batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &inclusion_block, &mut fetcher).await,
Expand Down Expand Up @@ -630,7 +630,7 @@ mod tests {
let l1_blocks = vec![block];
let l2_safe_head = L2BlockInfo::default();
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, ..Default::default() };
let batch = SpanBatch { batches: vec![first], ..Default::default() };
assert_eq!(
Expand All @@ -657,7 +657,7 @@ mod tests {
let l1_blocks = vec![block];
let l2_safe_head = L2BlockInfo::default();
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let batch = SpanBatch { batches: vec![first], ..Default::default() };
assert_eq!(
Expand Down Expand Up @@ -688,7 +688,7 @@ mod tests {
..Default::default()
};
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 21, ..Default::default() };
let batch = SpanBatch { batches: vec![first], ..Default::default() };
assert_eq!(
Expand Down Expand Up @@ -716,7 +716,7 @@ mod tests {
..Default::default()
};
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let batch = SpanBatch { batches: vec![first], ..Default::default() };
assert_eq!(
Expand Down Expand Up @@ -912,7 +912,7 @@ mod tests {
..Default::default()
};
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 11, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 21, ..Default::default() };
let batch = SpanBatch { batches: vec![first, second], ..Default::default() };
Expand All @@ -939,7 +939,7 @@ mod tests {
..Default::default()
};
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 8, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
let batch = SpanBatch { batches: vec![first, second], ..Default::default() };
Expand All @@ -966,7 +966,7 @@ mod tests {
..Default::default()
};
let inclusion_block = BlockInfo::default();
let mut fetcher: TestBatchValidator = TestBatchValidator::default();
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator::default();
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
let batch = SpanBatch { batches: vec![first, second], ..Default::default() };
Expand Down Expand Up @@ -999,7 +999,7 @@ mod tests {
l1_origin: BlockNumHash { number: 9, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
fetcher.short_circuit = true;
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
Expand Down Expand Up @@ -1045,7 +1045,7 @@ mod tests {
},
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1095,7 +1095,7 @@ mod tests {
l1_origin: BlockNumHash { number: 8, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1154,7 +1154,7 @@ mod tests {
l1_origin: BlockNumHash { number: 9, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1210,7 +1210,7 @@ mod tests {
l1_origin: BlockNumHash { number: 9, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1263,7 +1263,7 @@ mod tests {
l1_origin: BlockNumHash { number: 14, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1322,7 +1322,7 @@ mod tests {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 20, ..Default::default() };
let second = SpanBatchElement { epoch_num: 10, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1378,7 +1378,7 @@ mod tests {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 20, transactions: vec![] };
let second = SpanBatchElement { epoch_num: 10, timestamp: 20, transactions: vec![] };
Expand Down Expand Up @@ -1437,7 +1437,7 @@ mod tests {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement {
epoch_num: 10,
Expand Down Expand Up @@ -1506,7 +1506,7 @@ mod tests {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement {
epoch_num: 10,
Expand Down Expand Up @@ -1571,9 +1571,9 @@ mod tests {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let filler_bytes = Bytes::copy_from_slice(&[EIP1559_TX_TYPE_ID]);
let filler_bytes = Bytes::copy_from_slice(&[OpTxType::Eip1559 as u8]);
let first = SpanBatchElement {
epoch_num: 10,
timestamp: 20,
Expand Down Expand Up @@ -1636,7 +1636,7 @@ mod tests {
l1_origin: BlockNumHash { number: 9, ..Default::default() },
..Default::default()
};
let mut fetcher: TestBatchValidator =
let mut fetcher: TestBatchValidator<OpTxEnvelope> =
TestBatchValidator { blocks: vec![l2_block], ..Default::default() };
let first = SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() };
let second = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
Expand Down Expand Up @@ -1697,7 +1697,7 @@ mod tests {
withdrawals: None,
},
};
let mut fetcher: TestBatchValidator = TestBatchValidator {
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator {
blocks: vec![l2_block],
op_blocks: vec![block],
..Default::default()
Expand Down Expand Up @@ -1771,7 +1771,7 @@ mod tests {
withdrawals: None,
},
};
let mut fetcher: TestBatchValidator = TestBatchValidator {
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator {
blocks: vec![l2_block],
op_blocks: vec![block],
..Default::default()
Expand Down Expand Up @@ -1847,7 +1847,7 @@ mod tests {
withdrawals: None,
},
};
let mut fetcher: TestBatchValidator = TestBatchValidator {
let mut fetcher: TestBatchValidator<OpTxEnvelope> = TestBatchValidator {
blocks: vec![l2_block],
op_blocks: vec![block],
..Default::default()
Expand Down
20 changes: 12 additions & 8 deletions crates/protocol/src/batch/traits.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! Traits for working with protocol types.
use alloc::boxed::Box;
use alloy_consensus::BlockBody;
use alloy_network::primitives::{BlockTransactions, BlockTransactionsKind};
use alloy_provider::Provider;
use alloy_transport::TransportError;
use alloc::{boxed::Box, string::ToString};
use alloy_consensus::{Block, Typed2718};
use alloy_eips::eip2718::Encodable2718;
use async_trait::async_trait;
use core::fmt::Display;
use op_alloy_consensus::OpBlock;
use maili_common::DepositTxEnvelope;

use crate::L2BlockInfo;

Expand All @@ -17,15 +15,21 @@ pub trait BatchValidationProvider {
/// The error type for the [BatchValidationProvider].
type Error: Display + ToString;

/// Signed (except for deposit) transaction.
type Transaction: DepositTxEnvelope + Encodable2718 + Typed2718;

/// Returns the [L2BlockInfo] given a block number.
///
/// Errors if the block does not exist.
async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo, Self::Error>;

/// Returns the [OpBlock] for a given number.
/// Returns the OP [Block] for a given number.
///
/// Errors if no block is available for the given block number.
async fn block_by_number(&mut self, number: u64) -> Result<OpBlock, Self::Error>;
async fn block_by_number(
&mut self,
number: u64,
) -> Result<Block<Self::Transaction>, Self::Error>;
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
Expand Down
11 changes: 7 additions & 4 deletions crates/protocol/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{DecodeError, L1BlockInfoTx};
use alloy_consensus::{Block, Transaction, Typed2718};
use alloy_eips::{eip2718::Eip2718Error, BlockNumHash};
use alloy_primitives::B256;
use maili_common::DepositTxEnvelope;
use maili_genesis::ChainGenesis;
use op_alloy_consensus::OpBlock;

/// Block Header Info
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -126,10 +126,13 @@ impl L2BlockInfo {
}

/// Constructs an [L2BlockInfo] from a given OP [Block] and [ChainGenesis].
pub fn from_block_and_genesis(
block: &OpBlock,
pub fn from_block_and_genesis<T>(
block: &Block<T>,
genesis: &ChainGenesis,
) -> Result<Self, FromBlockError> {
) -> Result<Self, FromBlockError>
where
T: DepositTxEnvelope + Typed2718,
{
let block_info = BlockInfo::from(block);

let (l1_origin, sequence_number) = if block_info.number == genesis.l2.number {
Expand Down
31 changes: 23 additions & 8 deletions crates/protocol/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Test utilities for the protocol crate.
use alloc::{boxed::Box, format, string::String, sync::Arc, vec::Vec};
use alloy_consensus::{Block, Typed2718};
use alloy_eips::eip2718::Encodable2718;
use async_trait::async_trait;
use op_alloy_consensus::OpBlock;
use maili_common::DepositTxEnvelope;
use spin::Mutex;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::{layer::Context, Layer};
Expand All @@ -21,26 +23,30 @@ pub enum TestBatchValidatorError {
}

/// An [TestBatchValidator] implementation for testing.
#[derive(Default, Debug, Clone)]
pub struct TestBatchValidator {
#[derive(Debug, Clone)]
pub struct TestBatchValidator<T> {
/// Blocks
pub blocks: Vec<L2BlockInfo>,
/// Short circuit the block return to be the first block.
pub short_circuit: bool,
/// Blocks
pub op_blocks: Vec<OpBlock>,
pub op_blocks: Vec<Block<T>>,
}

impl TestBatchValidator {
impl<T> TestBatchValidator<T> {
/// Creates a new []TestBatchValidator with the given origin and batches.
pub const fn new(blocks: Vec<L2BlockInfo>, op_blocks: Vec<OpBlock>) -> Self {
pub const fn new(blocks: Vec<L2BlockInfo>, op_blocks: Vec<Block<T>>) -> Self {
Self { blocks, short_circuit: false, op_blocks }
}
}

#[async_trait]
impl BatchValidationProvider for TestBatchValidator {
impl<T> BatchValidationProvider for TestBatchValidator<T>
where
T: DepositTxEnvelope + Encodable2718 + Typed2718 + Clone,
{
type Error = TestBatchValidatorError;
type Transaction = T;

async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo, Self::Error> {
if self.short_circuit {
Expand All @@ -57,7 +63,10 @@ impl BatchValidationProvider for TestBatchValidator {
.ok_or_else(|| TestBatchValidatorError::BlockNotFound)
}

async fn block_by_number(&mut self, number: u64) -> Result<OpBlock, Self::Error> {
async fn block_by_number(
&mut self,
number: u64,
) -> Result<Block<Self::Transaction>, Self::Error> {
self.op_blocks
.iter()
.find(|p| p.header.number == number)
Expand All @@ -66,6 +75,12 @@ impl BatchValidationProvider for TestBatchValidator {
}
}

impl<T> Default for TestBatchValidator<T> {
fn default() -> Self {
Self { blocks: Vec::new(), short_circuit: false, op_blocks: Vec::new() }
}
}

/// The storage for the collected traces.
#[derive(Debug, Default, Clone)]
pub struct TraceStorage(pub Arc<Mutex<Vec<(Level, String)>>>);
Expand Down
Loading

0 comments on commit 0f499e4

Please sign in to comment.