2026-03-09 12:31:49 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use common::block::Block;
|
2026-07-07 23:03:46 +03:00
|
|
|
use futures::Stream;
|
2026-07-23 20:44:38 +03:00
|
|
|
use logos_blockchain_core::{
|
|
|
|
|
header::HeaderId,
|
|
|
|
|
mantle::ops::channel::{ChannelId, MsgId},
|
|
|
|
|
};
|
2026-01-29 22:20:42 +03:00
|
|
|
use logos_blockchain_key_management_system_service::keys::Ed25519Key;
|
2026-07-07 23:03:46 +03:00
|
|
|
use logos_blockchain_zone_sdk::{Slot, ZoneMessage, sequencer::WithdrawArg};
|
2026-07-22 19:33:13 +03:00
|
|
|
use tokio_util::sync::CancellationToken;
|
2026-01-29 22:20:42 +03:00
|
|
|
|
|
|
|
|
use crate::{
|
2026-07-23 20:44:38 +03:00
|
|
|
block_publisher::{BlockPublisherTrait, OnFollowSink, SequencerCheckpoint},
|
2026-04-29 14:05:23 +02:00
|
|
|
config::BedrockConfig,
|
2026-01-29 22:20:42 +03:00
|
|
|
};
|
|
|
|
|
|
2026-04-30 11:13:50 +02:00
|
|
|
pub type SequencerCoreWithMockClients = crate::SequencerCore<MockBlockPublisher>;
|
2026-01-29 22:20:42 +03:00
|
|
|
|
2026-07-23 20:44:38 +03:00
|
|
|
/// A zeroed checkpoint. Tests only assert *that* a checkpoint was persisted
|
|
|
|
|
/// alongside its effects, never what is in it.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn mock_checkpoint() -> SequencerCheckpoint {
|
|
|
|
|
SequencerCheckpoint {
|
|
|
|
|
last_msg_id: MsgId::from([0; 32]),
|
|
|
|
|
pending_txs: Vec::new(),
|
|
|
|
|
lib: HeaderId::from([0; 32]),
|
|
|
|
|
lib_slot: Slot::from(0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 22:20:42 +03:00
|
|
|
#[derive(Clone)]
|
2026-06-25 16:47:04 +03:00
|
|
|
pub struct MockBlockPublisher {
|
|
|
|
|
channel_id: ChannelId,
|
2026-07-22 19:33:13 +03:00
|
|
|
// Never cancelled: the mock driver never dies.
|
|
|
|
|
driver_cancellation: CancellationToken,
|
2026-07-07 23:03:46 +03:00
|
|
|
/// Canned channel frontier returned by [`Self::channel_tip_slot`].
|
|
|
|
|
tip_slot: Option<Slot>,
|
|
|
|
|
/// Canned finalized channel history returned by [`Self::read_channel_after`].
|
|
|
|
|
messages: Vec<(ZoneMessage, Slot)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MockBlockPublisher {
|
|
|
|
|
/// Builds a mock publisher backed by a canned channel, for reconstruction
|
|
|
|
|
/// and consistency tests. The default (via [`BlockPublisherTrait::new`])
|
|
|
|
|
/// serves an empty channel.
|
|
|
|
|
#[must_use]
|
2026-07-22 19:33:13 +03:00
|
|
|
pub fn with_canned_channel(
|
2026-07-07 23:03:46 +03:00
|
|
|
channel_id: ChannelId,
|
|
|
|
|
tip_slot: Option<Slot>,
|
|
|
|
|
messages: Vec<(ZoneMessage, Slot)>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
channel_id,
|
2026-07-22 19:33:13 +03:00
|
|
|
driver_cancellation: CancellationToken::new(),
|
2026-07-07 23:03:46 +03:00
|
|
|
tip_slot,
|
|
|
|
|
messages,
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-25 16:47:04 +03:00
|
|
|
}
|
2026-03-09 12:31:49 +00:00
|
|
|
|
|
|
|
|
impl BlockPublisherTrait for MockBlockPublisher {
|
|
|
|
|
async fn new(
|
2026-06-25 16:47:04 +03:00
|
|
|
config: &BedrockConfig,
|
2026-03-09 12:31:49 +00:00
|
|
|
_bedrock_signing_key: Ed25519Key,
|
|
|
|
|
_resubmit_interval: Duration,
|
2026-04-29 14:05:23 +02:00
|
|
|
_initial_checkpoint: Option<SequencerCheckpoint>,
|
2026-07-22 19:33:13 +03:00
|
|
|
_on_follow: OnFollowSink,
|
2026-03-09 12:31:49 +00:00
|
|
|
) -> Result<Self> {
|
2026-06-25 16:47:04 +03:00
|
|
|
Ok(Self {
|
|
|
|
|
channel_id: config.channel_id,
|
2026-07-22 19:33:13 +03:00
|
|
|
driver_cancellation: CancellationToken::new(),
|
2026-07-23 20:44:38 +03:00
|
|
|
// An existing but empty channel: `None` means *missing*, which the
|
|
|
|
|
// startup guard reads as a wiped Bedrock. Tests that want that say
|
|
|
|
|
// so via [`Self::with_canned_channel`].
|
|
|
|
|
tip_slot: Some(Slot::from(0)),
|
2026-07-07 23:03:46 +03:00
|
|
|
messages: Vec::new(),
|
2026-06-25 16:47:04 +03:00
|
|
|
})
|
2026-02-12 00:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:42:41 +03:00
|
|
|
async fn publish_block(
|
|
|
|
|
&self,
|
2026-07-22 19:33:13 +03:00
|
|
|
block: &Block,
|
2026-06-03 23:50:44 +03:00
|
|
|
_bridge_withdrawals: Vec<WithdrawArg>,
|
2026-07-23 20:44:38 +03:00
|
|
|
) -> Result<(MsgId, SequencerCheckpoint)> {
|
2026-07-22 19:33:13 +03:00
|
|
|
// Deterministic per-block id so head dedup behaves in tests.
|
|
|
|
|
//
|
|
|
|
|
// TODO: should we allow more "mockability" here?
|
2026-07-23 20:44:38 +03:00
|
|
|
Ok((MsgId::from(block.header.hash.0), mock_checkpoint()))
|
2026-02-12 00:01:00 +03:00
|
|
|
}
|
2026-06-25 16:47:04 +03:00
|
|
|
|
|
|
|
|
fn channel_id(&self) -> ChannelId {
|
|
|
|
|
self.channel_id
|
|
|
|
|
}
|
2026-07-07 23:03:46 +03:00
|
|
|
|
2026-07-22 19:33:13 +03:00
|
|
|
fn is_our_turn(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn driver_cancellation(&self) -> CancellationToken {
|
|
|
|
|
self.driver_cancellation.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 23:03:46 +03:00
|
|
|
async fn channel_tip_slot(&self) -> Result<Option<Slot>> {
|
|
|
|
|
Ok(self.tip_slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn read_channel_after(
|
|
|
|
|
&self,
|
|
|
|
|
after_slot: Option<Slot>,
|
|
|
|
|
) -> Result<impl Stream<Item = (ZoneMessage, Slot)> + '_> {
|
|
|
|
|
// Mirror `next_messages`: `after_slot` is exclusive.
|
|
|
|
|
let messages = self
|
|
|
|
|
.messages
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(move |(_, slot)| after_slot.is_none_or(|after| *slot > after))
|
|
|
|
|
.cloned();
|
|
|
|
|
Ok(futures::stream::iter(messages))
|
|
|
|
|
}
|
2026-01-29 22:20:42 +03:00
|
|
|
}
|