Skip to content

Commit

Permalink
Merge pull request #153 from ethereum-optimism/refcell/providers-refa…
Browse files Browse the repository at this point in the history
…ctor

fix(derive,plasma): Dedup ChainProvider Trait Duplication
  • Loading branch information
refcell authored Apr 27, 2024
2 parents e897bb2 + b9f85db commit 4e7b25c
Show file tree
Hide file tree
Showing 37 changed files with 595 additions and 644 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ alloy-rlp = { workspace = true, features = ["derive"] }
# Local
kona-primitives = { path = "../primitives", version = "0.0.1" }
kona-plasma = { path = "../plasma", version = "0.0.1" }
kona-providers = { path = "../providers", version = "0.0.1" }

# External
alloy-sol-types = { version = "0.7.1", default-features = false }
Expand Down Expand Up @@ -53,7 +54,7 @@ serde_json = { version = "1.0.116", default-features = false }

[features]
default = ["serde", "k256"]
serde = ["dep:serde", "kona-plasma/serde", "alloy-primitives/serde", "alloy-consensus/serde", "op-alloy-consensus/serde"]
serde = ["dep:serde", "kona-plasma/serde", "kona-providers/serde", "kona-primitives/serde", "alloy-primitives/serde", "alloy-consensus/serde", "op-alloy-consensus/serde"]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256", "op-alloy-consensus/k256"]
online = [
"dep:revm-primitives",
Expand All @@ -67,3 +68,4 @@ online = [
"revm-primitives/serde",
"revm-primitives/c-kzg",
]
test-utils = [ "kona-providers/test-utils", "kona-plasma/test-utils" ]
7 changes: 3 additions & 4 deletions crates/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ extern crate alloc;

use alloc::sync::Arc;
use core::fmt::Debug;
use traits::ChainProvider;
use kona_providers::ChainProvider;
use types::RollupConfig;

mod params;
pub use params::{
ChannelID, CHANNEL_ID_LENGTH, CONFIG_UPDATE_EVENT_VERSION_0, CONFIG_UPDATE_TOPIC,
DEPOSIT_EVENT_ABI, DEPOSIT_EVENT_ABI_HASH, DEPOSIT_EVENT_VERSION_0, DERIVATION_VERSION_0,
FRAME_OVERHEAD, MAX_CHANNEL_BANK_SIZE, MAX_FRAME_LEN, MAX_RLP_BYTES_PER_CHANNEL,
MAX_SPAN_BATCH_BYTES, SEQUENCER_FEE_VAULT_ADDRESS,
DERIVATION_VERSION_0, FRAME_OVERHEAD, MAX_CHANNEL_BANK_SIZE, MAX_FRAME_LEN,
MAX_RLP_BYTES_PER_CHANNEL, MAX_SPAN_BATCH_BYTES, SEQUENCER_FEE_VAULT_ADDRESS,
};

pub mod sources;
Expand Down
9 changes: 3 additions & 6 deletions crates/derive/src/online/alloy_providers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
//! This module contains concrete implementations of the data provider traits, using an alloy
//! provider on the backend.

use crate::{
traits::{ChainProvider, L2ChainProvider},
types::{
Block, BlockInfo, L2BlockInfo, L2ExecutionPayloadEnvelope, OpBlock, RollupConfig,
SystemConfig,
},
use crate::types::{
Block, BlockInfo, L2BlockInfo, L2ExecutionPayloadEnvelope, OpBlock, RollupConfig, SystemConfig,
};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{Header, Receipt, ReceiptWithBloom, TxEnvelope, TxType};
Expand All @@ -17,6 +13,7 @@ use alloy_transport_http::Http;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use core::num::NonZeroUsize;
use kona_providers::{ChainProvider, L2ChainProvider};
use lru::LruCache;

const CACHE_SIZE: usize = 16;
Expand Down
13 changes: 0 additions & 13 deletions crates/derive/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,9 @@ pub type ChannelID = [u8; CHANNEL_ID_LENGTH];
/// conditions, but we leave space to grow larger anyway (gas limit allows for more data).
pub const MAX_FRAME_LEN: usize = 1000;

/// Deposit log event abi signature.
pub const DEPOSIT_EVENT_ABI: &str = "TransactionDeposited(address,address,uint256,bytes)";

/// `keccak256("ConfigUpdate(uint256,uint8,bytes)")`
pub const CONFIG_UPDATE_TOPIC: B256 =
b256!("1d2b0bda21d56b8bd12d4f94ebacffdfb35f5e226f84b461103bb8beab6353be");

/// The initial version of the system config event log.
pub const CONFIG_UPDATE_EVENT_VERSION_0: B256 = B256::ZERO;

/// Deposit event abi hash.
///
/// This is the keccak256 hash of the deposit event ABI signature.
/// `keccak256("TransactionDeposited(address,address,uint256,bytes)")`
pub const DEPOSIT_EVENT_ABI_HASH: B256 =
b256!("b3813568d9991fc951961fcb4c784893574240a28925604d09fc577c55bb7c32");

/// The initial version of the deposit event log.
pub const DEPOSIT_EVENT_VERSION_0: B256 = B256::ZERO;
3 changes: 2 additions & 1 deletion crates/derive/src/sources/blobs.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//! Blob Data Source

use crate::{
traits::{AsyncIterator, BlobProvider, ChainProvider, SignedRecoverable},
traits::{AsyncIterator, BlobProvider, SignedRecoverable},
types::{BlobData, BlockInfo, IndexedBlobHash, StageError, StageResult},
};
use alloc::{boxed::Box, vec::Vec};
use alloy_consensus::{Transaction, TxEip4844Variant, TxEnvelope, TxType};
use alloy_primitives::{Address, Bytes, TxKind};
use anyhow::Result;
use async_trait::async_trait;
use kona_providers::ChainProvider;
use tracing::warn;

/// A data iterator that reads from a blob.
Expand Down
3 changes: 2 additions & 1 deletion crates/derive/src/sources/calldata.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! CallData Source

use crate::{
traits::{AsyncIterator, ChainProvider, SignedRecoverable},
traits::{AsyncIterator, SignedRecoverable},
types::{BlockInfo, StageError, StageResult},
};
use alloc::{boxed::Box, collections::VecDeque};
use alloy_consensus::{Transaction, TxEnvelope};
use alloy_primitives::{Address, Bytes, TxKind};
use async_trait::async_trait;
use kona_providers::ChainProvider;

/// A data iterator that reads from calldata.
#[derive(Debug, Clone)]
Expand Down
33 changes: 14 additions & 19 deletions crates/derive/src/sources/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

use crate::{
sources::{BlobSource, CalldataSource, DataSource, PlasmaSource},
traits::{BlobProvider, ChainProvider, DataAvailabilityProvider},
traits::{BlobProvider, DataAvailabilityProvider},
types::{BlockID, BlockInfo, RollupConfig},
};
use alloc::{boxed::Box, fmt::Debug};
use alloy_primitives::{Address, Bytes};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_plasma::traits::{ChainProvider as PlasmaChainProvider, PlasmaInputFetcher};
use kona_plasma::traits::PlasmaInputFetcher;
use kona_providers::ChainProvider;

/// A factory for creating a calldata and blob provider.
#[derive(Debug, Clone, Copy)]
pub struct DataSourceFactory<C, B, PCP, PIF, I>
pub struct DataSourceFactory<C, B, PIF, I>
where
C: ChainProvider + Clone,
C: ChainProvider + Send + Clone,
B: BlobProvider + Clone,
PCP: PlasmaChainProvider + Send + Clone,
PIF: PlasmaInputFetcher<PCP> + Clone,
PIF: PlasmaInputFetcher<C> + Clone,
I: Iterator<Item = Bytes> + Send + Clone,
{
/// The chain provider to use for the factory.
pub chain_provider: C,
/// The plasma chain provider.
pub plasma_chain_provider: PCP,
/// The plasma iterator.
pub plasma_source: I,
/// The blob provider
Expand All @@ -39,19 +37,17 @@ where
pub signer: Address,
}

impl<C, B, PCP, PIF, I> DataSourceFactory<C, B, PCP, PIF, I>
impl<C, B, PIF, I> DataSourceFactory<C, B, PIF, I>
where
C: ChainProvider + Clone + Debug,
C: ChainProvider + Send + Clone + Debug,
B: BlobProvider + Clone + Debug,
PCP: PlasmaChainProvider + Send + Clone + Debug,
PIF: PlasmaInputFetcher<PCP> + Clone + Debug,
PIF: PlasmaInputFetcher<C> + Clone + Debug,
I: Iterator<Item = Bytes> + Send + Clone,
{
/// Creates a new factory.
pub fn new(provider: C, blobs: B, pcp: PCP, pif: PIF, s: I, cfg: &RollupConfig) -> Self {
pub fn new(provider: C, blobs: B, pif: PIF, s: I, cfg: &RollupConfig) -> Self {
Self {
chain_provider: provider,
plasma_chain_provider: pcp,
plasma_source: s,
blob_provider: blobs,
plasma_input_fetcher: pif,
Expand All @@ -63,16 +59,15 @@ where
}

#[async_trait]
impl<C, B, PCP, PIF, I> DataAvailabilityProvider for DataSourceFactory<C, B, PCP, PIF, I>
impl<C, B, PIF, I> DataAvailabilityProvider for DataSourceFactory<C, B, PIF, I>
where
C: ChainProvider + Send + Sync + Clone + Debug,
B: BlobProvider + Send + Sync + Clone + Debug,
PCP: PlasmaChainProvider + Send + Sync + Clone + Debug,
PIF: PlasmaInputFetcher<PCP> + Send + Sync + Clone + Debug,
PIF: PlasmaInputFetcher<C> + Send + Sync + Clone + Debug,
I: Iterator<Item = Bytes> + Send + Sync + Clone + Debug,
{
type Item = Bytes;
type DataIter = DataSource<C, B, PCP, PIF, I>;
type DataIter = DataSource<C, B, PIF, I>;

async fn open_data(
&self,
Expand Down Expand Up @@ -102,7 +97,7 @@ where
} else if self.plasma_enabled {
let id = BlockID { hash: block_ref.hash, number: block_ref.number };
Ok(DataSource::Plasma(PlasmaSource::new(
self.plasma_chain_provider.clone(),
self.chain_provider.clone(),
self.plasma_input_fetcher.clone(),
self.plasma_source.clone(),
id,
Expand Down
6 changes: 4 additions & 2 deletions crates/derive/src/sources/plasma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use alloc::boxed::Box;
use alloy_primitives::Bytes;
use async_trait::async_trait;
use kona_plasma::{
traits::{ChainProvider, PlasmaInputFetcher},
traits::PlasmaInputFetcher,
types::{
decode_keccak256, Keccak256Commitment, PlasmaError, MAX_INPUT_SIZE, TX_DATA_VERSION_1,
},
};
use kona_primitives::block::BlockID;
use kona_providers::ChainProvider;

/// A plasma data iterator.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -188,7 +189,8 @@ mod tests {
use super::*;
use crate::stages::test_utils::{CollectingLayer, TraceStorage};
use alloc::vec;
use kona_plasma::test_utils::{TestChainProvider, TestPlasmaInputFetcher};
use kona_plasma::test_utils::TestPlasmaInputFetcher;
use kona_providers::test_utils::TestChainProvider;
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

Expand Down
17 changes: 8 additions & 9 deletions crates/derive/src/sources/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@

use crate::{
sources::{BlobSource, CalldataSource, PlasmaSource},
traits::{AsyncIterator, BlobProvider, ChainProvider},
traits::{AsyncIterator, BlobProvider},
types::StageResult,
};
use alloc::boxed::Box;
use alloy_primitives::Bytes;
use async_trait::async_trait;
use kona_plasma::traits::{ChainProvider as PlasmaChainProvider, PlasmaInputFetcher};
use kona_plasma::traits::PlasmaInputFetcher;
use kona_providers::ChainProvider;

/// An enum over the various data sources.
#[derive(Debug, Clone)]
pub enum DataSource<CP, B, PCP, PIF, I>
pub enum DataSource<CP, B, PIF, I>
where
CP: ChainProvider + Send,
B: BlobProvider + Send,
PCP: PlasmaChainProvider + Send,
PIF: PlasmaInputFetcher<PCP> + Send,
PIF: PlasmaInputFetcher<CP> + Send,
I: Iterator<Item = Bytes> + Send,
{
/// A calldata source.
Calldata(CalldataSource<CP>),
/// A blob source.
Blob(BlobSource<CP, B>),
/// A plasma source.
Plasma(PlasmaSource<PCP, PIF, I>),
Plasma(PlasmaSource<CP, PIF, I>),
}

#[async_trait]
impl<CP, B, PCP, PIF, I> AsyncIterator for DataSource<CP, B, PCP, PIF, I>
impl<CP, B, PIF, I> AsyncIterator for DataSource<CP, B, PIF, I>
where
CP: ChainProvider + Send,
B: BlobProvider + Send,
PCP: PlasmaChainProvider + Send,
PIF: PlasmaInputFetcher<PCP> + Send,
PIF: PlasmaInputFetcher<CP> + Send,
I: Iterator<Item = Bytes> + Send,
{
type Item = Bytes;
Expand Down
4 changes: 2 additions & 2 deletions crates/derive/src/stages/attributes_queue/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use super::derive_deposits;
use crate::{
params::SEQUENCER_FEE_VAULT_ADDRESS,
traits::{ChainProvider, L2ChainProvider},
types::{
BlockID, BuilderError, EcotoneTransactionBuilder, L1BlockInfoTx, L2BlockInfo,
L2PayloadAttributes, RawTransaction, RollupConfig,
Expand All @@ -12,6 +11,7 @@ use crate::{
use alloc::{boxed::Box, fmt::Debug, sync::Arc, vec, vec::Vec};
use alloy_rlp::Encodable;
use async_trait::async_trait;
use kona_providers::{ChainProvider, L2ChainProvider};

/// The [AttributesBuilder] is responsible for preparing [L2PayloadAttributes]
/// that can be used to construct an L2 Block containing only deposits.
Expand Down Expand Up @@ -173,11 +173,11 @@ mod tests {
use super::*;
use crate::{
stages::test_utils::MockSystemConfigL2Fetcher,
traits::test_utils::TestChainProvider,
types::{BlockInfo, SystemConfig},
};
use alloy_consensus::Header;
use alloy_primitives::B256;
use kona_providers::test_utils::TestChainProvider;

#[tokio::test]
async fn test_prepare_payload_block_mismatch_epoch_reset() {
Expand Down
6 changes: 2 additions & 4 deletions crates/derive/src/stages/attributes_queue/deposits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Contains a helper method to derive deposit transactions from L1 Receipts.

use crate::{
params::DEPOSIT_EVENT_ABI_HASH,
types::{decode_deposit, DepositError, RawTransaction},
};
use crate::types::{DepositError, RawTransaction};
use alloc::vec::Vec;
use alloy_consensus::Receipt;
use alloy_primitives::{Address, Log, B256};
use kona_primitives::{decode_deposit, DEPOSIT_EVENT_ABI_HASH};

/// Derive deposits for transaction receipts.
///
Expand Down
Loading

0 comments on commit 4e7b25c

Please sign in to comment.