Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/optimism/flashblocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use consensus::FlashBlockConsensusClient;
mod payload;
pub use payload::PendingFlashBlock;
mod sequence;
pub use sequence::FlashBlockCompleteSequence;
pub use sequence::{FlashBlockCompleteSequence, FlashBlockPendingSequence};

mod service;
mod worker;
Expand Down
34 changes: 21 additions & 13 deletions crates/optimism/flashblocks/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const FLASHBLOCK_SEQUENCE_CHANNEL_SIZE: usize = 128;

/// An ordered B-tree keeping the track of a sequence of [`FlashBlock`]s by their indices.
#[derive(Debug)]
pub(crate) struct FlashBlockPendingSequence<T> {
pub struct FlashBlockPendingSequence<T> {
/// tracks the individual flashblocks in order
///
/// With a blocktime of 2s and flashblock tick-rate of 200ms plus one extra flashblock per new
Expand All @@ -29,15 +29,16 @@ impl<T> FlashBlockPendingSequence<T>
where
T: SignedTransaction,
{
pub(crate) fn new() -> Self {
/// Create a new pending sequence.
pub fn new() -> Self {
// Note: if the channel is full, send will not block but rather overwrite the oldest
// messages. Order is preserved.
let (tx, _) = broadcast::channel(FLASHBLOCK_SEQUENCE_CHANNEL_SIZE);
Self { inner: BTreeMap::new(), block_broadcaster: tx, state_root: None }
}

/// Gets a subscriber to the flashblock sequences produced.
pub(crate) fn subscribe_block_sequence(&self) -> FlashBlockCompleteSequenceRx {
pub fn subscribe_block_sequence(&self) -> FlashBlockCompleteSequenceRx {
self.block_broadcaster.subscribe()
}

Expand Down Expand Up @@ -70,7 +71,7 @@ where
/// Inserts a new block into the sequence.
///
/// A [`FlashBlock`] with index 0 resets the set.
pub(crate) fn insert(&mut self, flashblock: FlashBlock) -> eyre::Result<()> {
pub fn insert(&mut self, flashblock: FlashBlock) -> eyre::Result<()> {
if flashblock.index == 0 {
trace!(number=%flashblock.block_number(), "Tracking new flashblock sequence");

Expand All @@ -93,7 +94,7 @@ where
}

/// Set state root
pub(crate) const fn set_state_root(&mut self, state_root: Option<B256>) {
pub const fn set_state_root(&mut self, state_root: Option<B256>) {
self.state_root = state_root;
}

Expand All @@ -103,9 +104,7 @@ where
/// the sequence
///
/// Note: flashblocks start at `index 0`.
pub(crate) fn ready_transactions(
&self,
) -> impl Iterator<Item = WithEncoded<Recovered<T>>> + '_ {
pub fn ready_transactions(&self) -> impl Iterator<Item = WithEncoded<Recovered<T>>> + '_ {
self.inner
.values()
.enumerate()
Expand All @@ -117,31 +116,40 @@ where
}

/// Returns the first block number
pub(crate) fn block_number(&self) -> Option<u64> {
pub fn block_number(&self) -> Option<u64> {
Some(self.inner.values().next()?.block().metadata.block_number)
}

/// Returns the payload base of the first tracked flashblock.
pub(crate) fn payload_base(&self) -> Option<ExecutionPayloadBaseV1> {
pub fn payload_base(&self) -> Option<ExecutionPayloadBaseV1> {
self.inner.values().next()?.block().base.clone()
}

/// Returns the number of tracked flashblocks.
pub(crate) fn count(&self) -> usize {
pub fn count(&self) -> usize {
self.inner.len()
}

/// Returns the reference to the last flashblock.
pub(crate) fn last_flashblock(&self) -> Option<&FlashBlock> {
pub fn last_flashblock(&self) -> Option<&FlashBlock> {
self.inner.last_key_value().map(|(_, b)| &b.block)
}

/// Returns the current/latest flashblock index in the sequence
pub(crate) fn index(&self) -> Option<u64> {
pub fn index(&self) -> Option<u64> {
Some(self.inner.values().last()?.block().index)
}
}

impl<T> Default for FlashBlockPendingSequence<T>
where
T: SignedTransaction,
{
fn default() -> Self {
Self::new()
}
}

/// A complete sequence of flashblocks, often corresponding to a full block.
/// Ensure invariants of a complete flashblocks sequence.
#[derive(Debug, Clone)]
Expand Down
Loading