Skip to content

Commit

Permalink
feat(kona-providers): In-memory Chain Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Aug 23, 2024
1 parent 187c835 commit 904c90e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,6 @@ reth-tracing = { git = "https://github.com/paradigmxyz/reth", version = "1.0.5"
tracing = "0.1.0"
eyre = "0.6.12"
clap = "4"
async-trait = "0.1.81"
hashbrown = "0.14.5"
parking_lot = "0.12.3"
4 changes: 4 additions & 0 deletions crates/kona-providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
reth.workspace = true
async-trait.workspace = true
parking_lot.workspace = true
eyre.workspace = true
kona-derive.workspace = true

[features]
Expand Down
66 changes: 66 additions & 0 deletions crates/kona-providers/src/chain_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Chain Provider
use alloc::{collections::vec_deque::VecDeque, sync::Arc};
use hashbrown::HashMap;

use async_trait::async_trait;
use eyre::Result;
use kona_derive::{
traits::ChainProvider,
types::{
alloy_primitives::B256, BlockID, BlockInfo, Header, Receipt, Signed, TxEip1559, TxEip2930,
TxEip4844, TxEip4844Variant, TxEnvelope, TxLegacy,
},
};
use parking_lot::RwLock;
use reth::{primitives::Transaction, providers::Chain};

/// An in-memory [ChainProvider] that stores chain data,
/// meant to be shared between multiple readers.
///
/// This provider is implemented with `FIFOMap`s to avoid
/// storing an unbounded amount of data.
#[derive(Debug, Clone)]
pub struct InMemoryChainProvider(Arc<RwLock<InMemoryChainProviderInner>>);

impl InMemoryChainProvider {
/// Create a new [InMemoryChainProvider] with the given capacity.
pub fn with_capacity(cap: usize) -> Self {
Self(Arc::new(RwLock::new(InMemoryChainProviderInner::with_capacity(cap))))
}

/// Commits Chain state to the provider.
pub fn commit(&mut self, chain: Arc<Chain>) {
self.0.write().commit(chain);
}

/// Inserts the L2 genesis [BlockID] into the provider.
pub fn insert_l2_genesis_block(&mut self, block: BlockID) {
self.0.write().insert_l2_genesis_block(block);
}
}

/// The inner state of an [InMemoryChainProvider].
#[derive(Debug)]
pub struct InMemoryChainProviderInner {
/// The maximum number of items to store in the provider.
/// This is used to prevent unbounded memory usage.
capacity: usize,

/// The order in which keys were inserted into the provider.
/// This is used to evict the oldest items when the provider
/// reaches its capacity.
key_order: VecDeque<B256>,

/// Maps [B256] hash to [Header].
hash_to_header: HashMap<B256, Header>,

/// Maps [B256] hash to [BlockInfo].
hash_to_block_info: HashMap<B256, BlockInfo>,

/// Maps [B256] hash to [Vec]<[Receipt]>.
hash_to_receipts: HashMap<B256, Vec<Receipt>>,

/// Maps a [B256] hash to a [Vec]<[TxEnvelope]>.
hash_to_txs: HashMap<B256, Vec<TxEnvelope>>,
}
3 changes: 3 additions & 0 deletions crates/kona-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ extern crate alloc;

/// Re-export kona's derivation traits
pub use kona_derive::traits::*;

pub mod chain_provider;
pub use chain_provider::{InMemoryChainProvider, InnerChainProvider};

0 comments on commit 904c90e

Please sign in to comment.