tycho_simulation/evm/stream.rs
1//! Builder for configuring a multi-protocol stream.
2//!
3//! Provides a builder for creating a multi-protocol stream that produces
4//! protocol state update messages. It runs one synchronization worker per protocol
5//! and a supervisor that aggregates updates, ensuring gap‑free streaming
6//! and robust state tracking.
7//!
8//! ## Context
9//!
10//! This stream wraps a `TychoStream` from `tycho-client`. It decodes `FeedMessage`s
11//! into protocol state updates. Internally, each protocol runs in its own
12//! synchronization worker, and a supervisor aggregates their messages per block.
13//!
14//! ### Protocol Synchronization Worker
15//! A synchronization worker runs the snapshot + delta protocol from `tycho-indexer`.
16//! - It first downloads components and their snapshots.
17//! - It then streams deltas.
18//! - It reacts to new or paused components by pulling snapshots or removing them from the active
19//! set.
20//!
21//! Each worker emits snapshots and deltas to the supervisor.
22//!
23//! ### Stream Supervisor
24//! The supervisor aggregates worker messages by block and assigns sync status.
25//! - It ensures workers produce gap-free messages.
26//! - It flags late workers as `Delayed`, and marks them `Stale` if they exceed `max_missed_blocks`.
27//! - It marks workers with terminal errors as `Ended`.
28//!
29//! Aggregating by block adds small latency, since the supervisor waits briefly for
30//! all workers to emit. This latency only applies to workers in `Ready` or `Delayed`.
31//!
32//! The stream ends only when **all** workers are `Stale` or `Ended`.
33//!
34//! ## Configuration
35//!
36//! The builder lets you customize:
37//!
38//! ### Protocols
39//! Select which protocols to synchronize.
40//!
41//! ### Tokens & Minimum Token Quality
42//! Provide token metadata up front so the decoder can initialize protocol states from startup
43//! snapshots. `set_tokens` does not act as an ongoing filter — components arriving after startup
44//! include their own token metadata. To restrict processing to specific tokens, apply that filter
45//! in your consumer when reading `new_components`. New tokens arriving via stream deltas are added
46//! automatically when their quality exceeds `min_token_quality`.
47//!
48//! ### StreamEndPolicy
49//! Control when the stream ends based on worker states. By default, it ends when all
50//! workers are `Stale` or `Ended`.
51//!
52//! ## Stream
53//! The stream emits one protocol state update every `block_time`. Each update
54//! reports protocol synchronization states and any changes.
55//!
56//! The `new_components` field lists newly deployed components and their tokens.
57//!
58//! The stream aims to run indefinitely. Internal retry and reconnect logic handle
59//! most errors, so users should rarely need to restart it manually.
60//!
61//! ## Example
62//! ```no_run
63//! use tycho_common::models::Chain;
64//! use tycho_simulation::evm::stream::ProtocolStreamBuilder;
65//! use tycho_simulation::utils::load_all_tokens;
66//! use futures::StreamExt;
67//! use tycho_client::feed::component_tracker::ComponentFilter;
68//! use tycho_simulation::evm::protocol::uniswap_v2::state::UniswapV2State;
69//!
70//! #[tokio::main]
71//! async fn main() {
72//! let all_tokens = load_all_tokens(
73//! "tycho-beta.propellerheads.xyz",
74//! false,
75//! Some("sampletoken"),
76//! true,
77//! Chain::Ethereum,
78//! None,
79//! None,
80//! )
81//! .await
82//! .expect("Failed loading tokens");
83//!
84//! let protocol_stream =
85//! ProtocolStreamBuilder::new("tycho-beta.propellerheads.xyz", Chain::Ethereum)
86//! .auth_key(Some("sampletoken".to_string()))
87//! .skip_state_decode_failures(true)
88//! .exchange::<UniswapV2State>(
89//! "uniswap_v2", ComponentFilter::with_tvl_range(5.0, 10.0), None
90//! )
91//! .set_tokens(all_tokens)
92//! .await
93//! .build()
94//! .await
95//! .expect("Failed building protocol stream");
96//! tokio::pin!(protocol_stream);
97//!
98//! // Loop through block updates
99//! while let Some(msg) = protocol_stream.next().await {
100//! dbg!(msg).expect("failed decoding");
101//! }
102//! }
103//! ```
104use std::{
105 collections::{HashMap, HashSet},
106 sync::Arc,
107 time,
108};
109
110use futures::{future::Either, stream, Stream, StreamExt};
111use tokio_stream::wrappers::ReceiverStream;
112use tracing::{debug, error, warn};
113use tycho_client::{
114 feed::{
115 component_tracker::ComponentFilter, synchronizer::ComponentWithState, BlockHeader,
116 BlockSynchronizerError, FeedMessage, SynchronizerState,
117 },
118 stream::{RetryConfiguration, StreamError, TychoStreamBuilder},
119};
120use tycho_common::{
121 models::{token::Token, Chain},
122 simulation::protocol_sim::ProtocolSim,
123 traits::TxDeltaIndexer,
124 Bytes,
125};
126
127use crate::{
128 evm::{
129 decoder::{StreamDecodeError, TychoStreamDecoder},
130 override_stream::{self, StateOverrideProvider},
131 pending::PendingBlockProcessor,
132 protocol::{
133 filters::uniswap_v4_non_angstrom_hook_pool_filter,
134 native_wrapper::state::NativeWrapperState,
135 uniswap_v4::hooks::hook_handler_creator::initialize_hook_handlers,
136 },
137 },
138 protocol::{
139 errors::InvalidSnapshotError,
140 models::{DecoderContext, TryFromWithBlock, Update},
141 },
142 utils::default_blocklist,
143};
144
145const EXCHANGES_REQUIRING_FILTER: [&str; 4] = ["vm:balancer_v2", "fluid_v1", "erc4626", "ekubo_v3"];
146
147/// The client-side filter applied to exchange `name` when the caller provides none.
148///
149/// `uniswap_v4_hooks`: without `ANGSTROM_API_KEY`, Angstrom swaps cannot be encoded (encoding
150/// fetches per-block attestations from the Angstrom API), so Angstrom pools are excluded up
151/// front rather than failing every route that selects them at encoding time.
152fn default_filter_fn(name: &str) -> Option<fn(&ComponentWithState) -> bool> {
153 if name == "uniswap_v4_hooks" && std::env::var("ANGSTROM_API_KEY").is_err() {
154 warn!(
155 "ANGSTROM_API_KEY is not set: excluding Angstrom pools from '{name}'. \
156 Set the key to include them."
157 );
158 return Some(uniswap_v4_non_angstrom_hook_pool_filter);
159 }
160 None
161}
162
163#[derive(Default, Debug, Clone, Copy)]
164pub enum StreamEndPolicy {
165 /// End stream if all states are Stale or Ended (default)
166 #[default]
167 AllEndedOrStale,
168 /// End stream if any protocol ended
169 AnyEnded,
170 /// End stream if any protocol ended or is stale
171 AnyEndedOrStale,
172 /// End stream if any protocol is stale
173 AnyStale,
174}
175
176impl StreamEndPolicy {
177 fn should_end<'a>(&self, states: impl IntoIterator<Item = &'a SynchronizerState>) -> bool {
178 let mut it = states.into_iter();
179 match self {
180 StreamEndPolicy::AllEndedOrStale => false,
181 StreamEndPolicy::AnyEnded => it.any(|s| matches!(s, SynchronizerState::Ended(_))),
182 StreamEndPolicy::AnyStale => it.any(|s| matches!(s, SynchronizerState::Stale(_))),
183 StreamEndPolicy::AnyEndedOrStale => {
184 it.any(|s| matches!(s, SynchronizerState::Stale(_) | SynchronizerState::Ended(_)))
185 }
186 }
187 }
188}
189
190/// Handle returned by [`ProtocolStreamBuilder::with_step_controller`] that gives external
191/// control over when each buffered block is released for decoding.
192///
193/// Intended for complex test scenarios where the caller needs to observe what the next
194/// block contains before allowing the decoder pipeline to process it.
195///
196/// ## Drop behaviour
197///
198/// Dropping this controller ungates the stream: the gating task detects the closed trigger
199/// channel, forwards the currently-buffered block (if any), then continues passing subsequent
200/// blocks through without waiting for triggers — exactly as if step-control had never been
201/// enabled. The stream runs to its natural end.
202pub struct BlockStepController {
203 /// Sends a trigger signal to release the next buffered block.
204 trigger_tx: tokio::sync::mpsc::UnboundedSender<()>,
205 /// Watch channel containing the next buffered raw message, or `None` if no block is pending.
206 peek_rx: tokio::sync::watch::Receiver<Option<FeedMessage<BlockHeader>>>,
207}
208
209impl BlockStepController {
210 /// Releases the next buffered block for decoding and emission.
211 ///
212 /// Returns an error if the stream has already ended and the sender is disconnected.
213 pub fn trigger_next_block(&self) -> Result<(), tokio::sync::mpsc::error::SendError<()>> {
214 // Send a unit value on the trigger channel to unblock the gating task.
215 self.trigger_tx.send(())
216 }
217
218 /// Returns the currently buffered block immediately, or `None` if no block is buffered yet.
219 pub fn try_peek_next_block(&self) -> Option<FeedMessage<BlockHeader>> {
220 self.peek_rx.borrow().clone()
221 }
222
223 /// Waits until a block is buffered and returns it without consuming it.
224 ///
225 /// Returns `None` only if the stream has ended and no further blocks will arrive.
226 /// If a block is already buffered when this is called, it returns immediately.
227 pub async fn peek_next_block(&self) -> Option<FeedMessage<BlockHeader>> {
228 // Clone so we don't hold a mutable borrow on self; wait_for checks the current
229 // value first, so this returns immediately if a block is already present.
230 let mut rx = self.peek_rx.clone();
231 let guard = rx
232 .wait_for(|v| v.is_some())
233 .await
234 .ok()?;
235 guard.clone()
236 }
237}
238
239/// Builds and configures the multi protocol stream described in the [module-level docs](self).
240///
241/// See the module documentation for details on protocols, configuration options, and
242/// stream behavior.
243pub struct ProtocolStreamBuilder {
244 decoder: TychoStreamDecoder<BlockHeader>,
245 stream_builder: TychoStreamBuilder,
246 stream_end_policy: StreamEndPolicy,
247 chain: Chain,
248 pending_indexers: HashMap<String, Box<dyn TxDeltaIndexer>>,
249 /// Watch sender used to publish the currently-buffered raw block so the controller can peek
250 /// at it before triggering. `Some` iff step-control mode is active.
251 step_peek_tx: Option<tokio::sync::watch::Sender<Option<FeedMessage<BlockHeader>>>>,
252 /// Receiver half of the trigger channel. Held here until `build()` / `build_with_pending()`
253 /// transfers ownership to the gating task. `Some` iff step-control mode is active.
254 step_trigger_rx: Option<tokio::sync::mpsc::UnboundedReceiver<()>>,
255 /// State-override providers explicitly registered by the consumer, keyed by `protocol_system`.
256 /// These take precedence over the built-in default registry and are installed onto the decoder
257 /// at build time.
258 override_providers: HashMap<String, Arc<dyn StateOverrideProvider>>,
259 /// Names of all exchanges registered on the builder, used to decide which built-in override
260 /// providers to auto-register at build time.
261 registered_exchanges: HashSet<String>,
262}
263
264impl ProtocolStreamBuilder {
265 /// Creates a new builder for a multi-protocol stream.
266 ///
267 /// The shipped pool blocklist is applied by default, excluding components known to break
268 /// simulation. Use [`blocklist_components`](Self::blocklist_components) to exclude additional
269 /// components.
270 ///
271 /// See the [module-level docs](self) for full details on stream behavior and configuration.
272 pub fn new(tycho_url: &str, chain: Chain) -> Self {
273 Self {
274 decoder: TychoStreamDecoder::new(),
275 stream_builder: TychoStreamBuilder::new(tycho_url, chain)
276 .blocklisted_ids(default_blocklist()),
277 stream_end_policy: StreamEndPolicy::default(),
278 chain,
279 pending_indexers: HashMap::new(),
280 step_peek_tx: None,
281 step_trigger_rx: None,
282 override_providers: HashMap::new(),
283 registered_exchanges: HashSet::new(),
284 }
285 }
286
287 /// Adds a specific exchange to the stream.
288 ///
289 /// This configures the builder to include a new protocol synchronizer for `name`,
290 /// filtering its components according to `filter` and optionally `filter_fn`.
291 ///
292 /// The type parameter `T` specifies the decoder type for this exchange. All
293 /// component states for this exchange will be decoded into instances of `T`.
294 ///
295 /// # Parameters
296 ///
297 /// - `name`: The protocol or exchange name (e.g., `"uniswap_v4"`, `"vm:balancer_v2"`).
298 /// - `filter`: Defines the set of components to include in the stream.
299 /// - `filter_fn`: Optional custom filter function for client-side filtering of components not
300 /// expressible in `filter`.
301 ///
302 /// # Notes
303 ///
304 /// For certain protocols (e.g., `"uniswap_v4"`, `"vm:balancer_v2"`, `"vm:curve"`), omitting
305 /// `filter_fn` may cause decoding errors or incorrect results. In these cases, a proper
306 /// filter function is required to ensure correct decoding and quoting logic.
307 pub fn exchange<T>(
308 mut self,
309 name: &str,
310 filter: ComponentFilter,
311 filter_fn: Option<fn(&ComponentWithState) -> bool>,
312 ) -> Self
313 where
314 T: ProtocolSim
315 + TryFromWithBlock<ComponentWithState, BlockHeader, Error = InvalidSnapshotError>
316 + Send
317 + 'static,
318 {
319 self.stream_builder = self
320 .stream_builder
321 .exchange(name, filter);
322 self.registered_exchanges
323 .insert(name.to_string());
324 self.decoder.register_decoder::<T>(name);
325 if let Some(predicate) = filter_fn {
326 self.decoder
327 .register_filter(name, predicate);
328 } else if let Some(predicate) = default_filter_fn(name) {
329 self.decoder
330 .register_filter(name, predicate);
331 }
332
333 if EXCHANGES_REQUIRING_FILTER.contains(&name) && filter_fn.is_none() {
334 warn!(
335 "Warning: For exchange type '{}', it is necessary to set a filter function because not all pools are supported. See all filters at src/evm/protocol/filters.rs",
336 name
337 );
338 }
339
340 self
341 }
342
343 /// Adds a specific exchange to the stream with decoder context.
344 ///
345 /// This configures the builder to include a new protocol synchronizer for `name`,
346 /// filtering its components according to `filter` and optionally `filter_fn`. It also registers
347 /// the DecoderContext (this is useful to test protocols that are not live yet)
348 ///
349 /// The type parameter `T` specifies the decoder type for this exchange. All
350 /// component states for this exchange will be decoded into instances of `T`.
351 ///
352 /// # Parameters
353 ///
354 /// - `name`: The protocol or exchange name (e.g., `"uniswap_v4"`, `"vm:balancer_v2"`).
355 /// - `filter`: Defines the set of components to include in the stream.
356 /// - `filter_fn`: Optional custom filter function for client-side filtering of components not
357 /// expressible in `filter`.
358 /// - `decoder_context`: The decoder context for this exchange
359 ///
360 /// # Notes
361 ///
362 /// For certain protocols (e.g., `"uniswap_v4"`, `"vm:balancer_v2"`, `"vm:curve"`), omitting
363 /// `filter_fn` may cause decoding errors or incorrect results. In these cases, a proper
364 /// filter function is required to ensure correct decoding and quoting logic.
365 pub fn exchange_with_decoder_context<T>(
366 mut self,
367 name: &str,
368 filter: ComponentFilter,
369 filter_fn: Option<fn(&ComponentWithState) -> bool>,
370 decoder_context: DecoderContext,
371 ) -> Self
372 where
373 T: ProtocolSim
374 + TryFromWithBlock<ComponentWithState, BlockHeader, Error = InvalidSnapshotError>
375 + Send
376 + 'static,
377 {
378 self.stream_builder = self
379 .stream_builder
380 .exchange(name, filter);
381 self.registered_exchanges
382 .insert(name.to_string());
383 self.decoder
384 .register_decoder_with_context::<T>(name, decoder_context);
385 if let Some(predicate) = filter_fn {
386 self.decoder
387 .register_filter(name, predicate);
388 } else if let Some(predicate) = default_filter_fn(name) {
389 self.decoder
390 .register_filter(name, predicate);
391 }
392
393 if EXCHANGES_REQUIRING_FILTER.contains(&name) && filter_fn.is_none() {
394 warn!(
395 "Warning: For exchange type '{}', it is necessary to set a filter function because not all pools are supported. See all filters at src/evm/protocol/filters.rs",
396 name
397 );
398 }
399
400 self
401 }
402
403 /// Sets the block time interval for the stream.
404 ///
405 /// This controls how often the stream produces updates.
406 pub fn block_time(mut self, block_time: u64) -> Self {
407 self.stream_builder = self
408 .stream_builder
409 .block_time(block_time);
410 self
411 }
412
413 /// Sets the network operation timeout (deprecated).
414 ///
415 /// Use [`latency_buffer()`](Self::latency_buffer) instead for controlling latency.
416 /// This method is retained for backwards compatibility.
417 #[deprecated = "Use latency_buffer instead"]
418 pub fn timeout(mut self, timeout: u64) -> Self {
419 self.stream_builder = self.stream_builder.timeout(timeout);
420 self
421 }
422
423 /// Sets the latency buffer to aggregate same-block messages.
424 ///
425 /// This allows the supervisor to wait a short interval for all synchronizers to emit
426 /// before aggregating.
427 pub fn latency_buffer(mut self, timeout: u64) -> Self {
428 self.stream_builder = self.stream_builder.timeout(timeout);
429 self
430 }
431
432 /// Sets the maximum number of blocks a synchronizer may miss before being marked as `Stale`.
433 pub fn max_missed_blocks(mut self, n: u64) -> Self {
434 self.stream_builder = self.stream_builder.max_missed_blocks(n);
435 self
436 }
437
438 /// Sets how long a synchronizer may take to process the initial message.
439 ///
440 /// Useful for data-intensive protocols where startup decoding takes longer.
441 pub fn startup_timeout(mut self, timeout: time::Duration) -> Self {
442 self.stream_builder = self
443 .stream_builder
444 .startup_timeout(timeout);
445 self
446 }
447
448 /// Configures the stream to exclude state updates.
449 ///
450 /// This reduces bandwidth and decoding workload if protocol state is not of
451 /// interest (e.g. only process new tokens).
452 pub fn no_state(mut self, no_state: bool) -> Self {
453 self.stream_builder = self.stream_builder.no_state(no_state);
454 self
455 }
456
457 /// Sets the API key for authenticating with the Tycho server.
458 pub fn auth_key(mut self, auth_key: Option<String>) -> Self {
459 self.stream_builder = self.stream_builder.auth_key(auth_key);
460 self
461 }
462
463 /// Disables TLS/ SSL for the connection, using http and ws protocols.
464 ///
465 /// This is not recommended for production use.
466 pub fn no_tls(mut self, no_tls: bool) -> Self {
467 self.stream_builder = self.stream_builder.no_tls(no_tls);
468 self
469 }
470
471 /// Disable compression for the connection.
472 pub fn disable_compression(mut self) -> Self {
473 self.stream_builder = self
474 .stream_builder
475 .disable_compression();
476 self
477 }
478
479 /// Enables partial block updates (flashblocks).
480 pub fn enable_partial_blocks(mut self) -> Self {
481 self.stream_builder = self
482 .stream_builder
483 .enable_partial_blocks();
484 self
485 }
486
487 /// Exclude additional component IDs from all registered exchanges.
488 ///
489 /// These IDs are added to the shipped blocklist that is already applied by default (see
490 /// [`new`](Self::new)).
491 pub fn blocklist_components(mut self, ids: HashSet<String>) -> Self {
492 if !ids.is_empty() {
493 tracing::info!("Blocklisting {} components", ids.len());
494 self.stream_builder = self.stream_builder.blocklisted_ids(ids);
495 }
496 self
497 }
498
499 /// Sets the stream end policy.
500 ///
501 /// Controls when the stream should stop based on synchronizer states.
502 ///
503 /// ## Note
504 /// The stream always ends latest if all protocols are stale or ended independent of
505 /// this configuration. This allows you to end the stream earlier than that.
506 ///
507 /// See [self::StreamEndPolicy] for possible configuration options.
508 pub fn stream_end_policy(mut self, stream_end_policy: StreamEndPolicy) -> Self {
509 self.stream_end_policy = stream_end_policy;
510 self
511 }
512
513 /// Provides token metadata used to decode startup snapshots and initialize protocol states.
514 ///
515 /// This is not a stream filter — components arriving after startup include their own token
516 /// metadata. To restrict to specific tokens, filter in your consumer logic. New tokens
517 /// arriving via stream deltas are added automatically if they meet the quality threshold.
518 pub async fn set_tokens(self, tokens: HashMap<Bytes, Token>) -> Self {
519 self.decoder.set_tokens(tokens).await;
520 self
521 }
522
523 /// Skips decoding errors for component state updates.
524 ///
525 /// Allows the stream to continue processing even if some states fail to decode,
526 /// logging a warning instead of panicking.
527 pub fn skip_state_decode_failures(mut self, skip: bool) -> Self {
528 self.decoder
529 .skip_state_decode_failures(skip);
530 self
531 }
532
533 /// Sets the minimum token quality for tokens added via the stream.
534 ///
535 /// Tokens arriving in stream deltas below this threshold are ignored. Defaults to 100.
536 /// Set this to the same value used in [`load_all_tokens()`](crate::utils::load_all_tokens) to
537 /// apply consistent filtering.
538 pub fn min_token_quality(mut self, quality: u32) -> Self {
539 self.decoder.min_token_quality(quality);
540 self
541 }
542
543 /// Configures the retry policy for websocket reconnects.
544 pub fn websocket_retry_config(mut self, config: &RetryConfiguration) -> Self {
545 self.stream_builder = self
546 .stream_builder
547 .websockets_retry_config(config);
548 self
549 }
550
551 /// Configures the retry policy for state synchronization.
552 pub fn state_synchronizer_retry_config(mut self, config: &RetryConfiguration) -> Self {
553 self.stream_builder = self
554 .stream_builder
555 .state_synchronizer_retry_config(config);
556 self
557 }
558
559 pub fn get_decoder(&self) -> &TychoStreamDecoder<BlockHeader> {
560 &self.decoder
561 }
562
563 /// Registers a [`TxDeltaIndexer`] for ephemeral pending-block simulation.
564 ///
565 /// The indexer is associated with `extractor` (the protocol synchronizer name, e.g.
566 /// `"uniswap_v3"`). Use [`build_with_pending`](Self::build_with_pending) to obtain both
567 /// the confirmed stream and the pending processor.
568 ///
569 /// Returns an error if `extractor` names a VM protocol (prefix `"vm:"`), which requires
570 /// `update_engine()` and cannot be simulated natively.
571 pub fn with_pending_indexer(
572 mut self,
573 extractor: &str,
574 indexer: Box<dyn TxDeltaIndexer>,
575 ) -> Result<Self, StreamError> {
576 if extractor.starts_with("vm:") {
577 return Err(StreamError::SetUpError(format!(
578 "extractor '{extractor}' is a VM protocol; TxDeltaIndexer only supports native protocols"
579 )));
580 }
581 self.pending_indexers
582 .insert(extractor.to_string(), indexer);
583 Ok(self)
584 }
585
586 /// Enables controlled-step mode for testing.
587 ///
588 /// Returns a [`BlockStepController`] that lets the caller decide when each buffered block
589 /// is released for decoding. Call this before [`build`](Self::build) or
590 /// [`build_with_pending`](Self::build_with_pending) — both detect and wire up the gating
591 /// automatically.
592 ///
593 /// In production code, do not call this method; the stream runs at full speed.
594 pub fn with_step_controller(mut self) -> (Self, BlockStepController) {
595 let (trigger_tx, trigger_rx) = tokio::sync::mpsc::unbounded_channel::<()>();
596 let (peek_tx, peek_rx) =
597 tokio::sync::watch::channel::<Option<FeedMessage<BlockHeader>>>(None);
598
599 self.step_peek_tx = Some(peek_tx);
600 self.step_trigger_rx = Some(trigger_rx);
601
602 let controller = BlockStepController { trigger_tx, peek_rx };
603 (self, controller)
604 }
605
606 /// Spawns a background task that gates `FeedMessage` delivery.
607 ///
608 /// The task buffers each incoming message, publishes it to `peek_tx` so the
609 /// [`BlockStepController`] can inspect it, waits for a trigger, then forwards the message to
610 /// `output_tx` for the decode pipeline. If `advance_tx` is `Some`, a clone of the message is
611 /// also forwarded there (used by the pending-processor path) before the decode step.
612 /// When the input channel closes or a terminal error is received according to
613 /// `stream_end_policy`, the task exits and all output channels are dropped.
614 fn run_gating_task(
615 raw_rx: tokio::sync::mpsc::Receiver<
616 Result<FeedMessage<BlockHeader>, BlockSynchronizerError>,
617 >,
618 mut trigger_rx: tokio::sync::mpsc::UnboundedReceiver<()>,
619 peek_tx: tokio::sync::watch::Sender<Option<FeedMessage<BlockHeader>>>,
620 output_tx: tokio::sync::mpsc::Sender<FeedMessage<BlockHeader>>,
621 stream_end_policy: StreamEndPolicy,
622 ) {
623 tokio::spawn(async move {
624 let mut raw_stream = ReceiverStream::new(raw_rx);
625 loop {
626 let msg = match raw_stream.next().await {
627 Some(Ok(msg)) => msg,
628 Some(Err(e)) => {
629 error!("Block stream ended with terminal error: {e}");
630 break;
631 }
632 None => break,
633 };
634
635 if stream_end_policy.should_end(msg.sync_states.values()) {
636 error!(
637 "Block stream ended due to {:?}: {:?}",
638 stream_end_policy, msg.sync_states
639 );
640 break;
641 }
642
643 // Publish the buffered message so the caller can peek before triggering.
644 let _ = peek_tx.send(Some(msg.clone()));
645
646 // Block until the controller fires trigger_next_block(), or until it is dropped.
647 if trigger_rx.recv().await.is_none() {
648 // Controller dropped — forward the buffered message and drain the rest
649 // without gating, so the stream continues to its natural end.
650 let _ = peek_tx.send(None);
651 if output_tx.send(msg).await.is_err() {
652 break;
653 }
654 while let Some(item) = raw_stream.next().await {
655 let Ok(msg) = item else { break };
656 if stream_end_policy.should_end(msg.sync_states.values()) {
657 break;
658 }
659 if output_tx.send(msg).await.is_err() {
660 break;
661 }
662 }
663 break;
664 }
665
666 // Clear the peek slot before decoding so callers see None between blocks.
667 let _ = peek_tx.send(None);
668
669 if output_tx.send(msg).await.is_err() {
670 break;
671 }
672 }
673 });
674 }
675
676 /// Registers `provider` as the live override source for `protocol_system`.
677 ///
678 /// Explicit registrations take precedence over the built-in default registry, so this is how
679 /// you swap a venue (e.g. `vm:bopamm`) onto a different provider. Registering the same provider
680 /// for several protocols is cheap — it is shared via `Arc`, not duplicated.
681 pub fn with_override_provider(
682 mut self,
683 protocol_system: impl Into<String>,
684 provider: Arc<dyn StateOverrideProvider>,
685 ) -> Self {
686 self.override_providers
687 .insert(protocol_system.into(), provider);
688 self
689 }
690
691 /// Installs override providers onto the decoder before the stream is built.
692 ///
693 /// Explicit consumer registrations win; the built-in default registry (see
694 /// [`default_override_providers`](crate::evm::override_stream::default_override_providers))
695 /// then fills every remaining protocol it can serve.
696 fn install_override_providers(&mut self) {
697 let explicit = std::mem::take(&mut self.override_providers);
698 // Protocols eligible for a built-in default provider: registered exchanges not explicitly
699 // overridden by the consumer.
700 let uncovered = self
701 .registered_exchanges
702 .clone()
703 .into_iter()
704 .filter(|exchange| !explicit.contains_key(exchange));
705 let defaults = override_stream::default_override_providers(uncovered);
706 for (protocol_system, provider) in defaults.into_iter().chain(explicit) {
707 self.decoder
708 .set_override_provider(protocol_system, provider);
709 }
710 }
711
712 /// Builds the confirmed protocol stream and a [`PendingBlockProcessor`] that stays
713 /// in sync with it automatically.
714 ///
715 /// The stream pipeline forwards every confirmed [`FeedMessage`] to the processor via an
716 /// internal unbounded channel — it never blocks waiting for the consumer. The consumer
717 /// owns the returned `PendingBlockProcessor` exclusively and may wrap it in whatever
718 /// synchronisation primitive suits their use case (e.g. `Mutex` for shared access,
719 /// nothing for single-threaded use).
720 ///
721 /// Call [`generate_pending_update`](PendingBlockProcessor::generate_pending_update) to
722 /// simulate a candidate bundle; it drains the channel automatically before computing.
723 pub async fn build_with_pending(
724 mut self,
725 ) -> Result<
726 (impl Stream<Item = Result<Update, StreamDecodeError>>, PendingBlockProcessor),
727 StreamError,
728 > {
729 initialize_hook_handlers().map_err(|e| {
730 StreamError::SetUpError(format!("Error initializing hook handlers: {e:?}"))
731 })?;
732 self.install_override_providers();
733 let (_, rx) = self.stream_builder.build().await?;
734 let decoder = Arc::new(self.decoder);
735
736 let (advance_tx, advance_rx) =
737 tokio::sync::mpsc::unbounded_channel::<FeedMessage<BlockHeader>>();
738 let pending = PendingBlockProcessor::new(
739 self.pending_indexers,
740 decoder.clone(),
741 self.chain,
742 advance_rx,
743 );
744
745 let chain = self.chain;
746 let stream_end_policy = self.stream_end_policy;
747
748 let decode_stream: Box<dyn Stream<Item = FeedMessage<BlockHeader>> + Send + Unpin> =
749 if let (Some(peek_tx), Some(trigger_rx)) = (self.step_peek_tx, self.step_trigger_rx) {
750 let (gated_tx, gated_rx) =
751 tokio::sync::mpsc::channel::<FeedMessage<BlockHeader>>(1);
752 Self::run_gating_task(rx, trigger_rx, peek_tx, gated_tx, stream_end_policy);
753 Box::new(ReceiverStream::new(gated_rx))
754 } else {
755 let normal = ReceiverStream::new(rx)
756 .take_while(move |msg| match msg {
757 Ok(msg) => {
758 let states = msg.sync_states.values();
759 if stream_end_policy.should_end(states) {
760 error!(
761 "Block stream ended due to {:?}: {:?}",
762 stream_end_policy, msg.sync_states
763 );
764 futures::future::ready(false)
765 } else {
766 futures::future::ready(true)
767 }
768 }
769 Err(e) => {
770 error!("Block stream ended with terminal error: {e}");
771 futures::future::ready(false)
772 }
773 })
774 .map(|msg| msg.expect("Safe since stream ends if we receive an error"));
775 Box::new(Box::pin(normal))
776 };
777
778 let stream = Box::pin(decode_stream.then({
779 let decoder = decoder.clone();
780 move |msg| {
781 let decoder = decoder.clone();
782 let advance_tx = advance_tx.clone();
783 async move {
784 let _ = advance_tx.send(msg.clone());
785 decoder.decode(&msg).await.map_err(|e| {
786 debug!(msg=?msg, "Decode error: {}", e);
787 e
788 })
789 }
790 }
791 }));
792 let stream = inject_native_wrapper(stream, chain);
793 Ok((stream, pending))
794 }
795
796 /// Builds and returns the configured protocol stream.
797 ///
798 /// See the module-level docs for details on stream behavior and emitted messages.
799 /// This method applies all builder settings and starts the stream.
800 pub async fn build(
801 mut self,
802 ) -> Result<impl Stream<Item = Result<Update, StreamDecodeError>>, StreamError> {
803 initialize_hook_handlers().map_err(|e| {
804 StreamError::SetUpError(format!("Error initializing hook handlers: {e:?}"))
805 })?;
806 self.install_override_providers();
807 let (_, rx) = self.stream_builder.build().await?;
808 let decoder = Arc::new(self.decoder);
809 let chain = self.chain;
810 let stream_end_policy = self.stream_end_policy;
811
812 let decode_stream: Box<dyn Stream<Item = FeedMessage<BlockHeader>> + Send + Unpin> =
813 if let (Some(peek_tx), Some(trigger_rx)) = (self.step_peek_tx, self.step_trigger_rx) {
814 let (gated_tx, gated_rx) =
815 tokio::sync::mpsc::channel::<FeedMessage<BlockHeader>>(1);
816 Self::run_gating_task(rx, trigger_rx, peek_tx, gated_tx, stream_end_policy);
817 Box::new(ReceiverStream::new(gated_rx))
818 } else {
819 let normal = ReceiverStream::new(rx)
820 .take_while(move |msg| match msg {
821 Ok(msg) => {
822 let states = msg.sync_states.values();
823 if stream_end_policy.should_end(states) {
824 error!(
825 "Block stream ended due to {:?}: {:?}",
826 stream_end_policy, msg.sync_states
827 );
828 futures::future::ready(false)
829 } else {
830 futures::future::ready(true)
831 }
832 }
833 Err(e) => {
834 error!("Block stream ended with terminal error: {e}");
835 futures::future::ready(false)
836 }
837 })
838 .map(|msg| msg.expect("Safe since stream ends if we receive an error"));
839 Box::new(Box::pin(normal))
840 };
841
842 let stream = Box::pin(decode_stream.then({
843 let decoder = decoder.clone();
844 move |msg| {
845 let decoder = decoder.clone();
846 async move {
847 decoder.decode(&msg).await.map_err(|e| {
848 debug!(msg=?msg, "Decode error: {}", e);
849 e
850 })
851 }
852 }
853 }));
854 let stream = inject_native_wrapper(stream, chain);
855 Ok(stream)
856 }
857}
858
859/// Wraps a decoded protocol stream to inject a `NativeWrapperState` component
860/// on the first successful update.
861///
862/// Skips injection for chains where the native and wrapped-native tokens share
863/// the same address (e.g. Starknet).
864fn inject_native_wrapper(
865 inner: impl Stream<Item = Result<Update, StreamDecodeError>> + Unpin + Send + 'static,
866 chain: Chain,
867) -> impl Stream<Item = Result<Update, StreamDecodeError>> + Send {
868 let has_distinct_wrapper = chain.native_token().address != chain.wrapped_native_token().address;
869 if !has_distinct_wrapper {
870 return Either::Left(inner);
871 }
872
873 Either::Right(
874 stream::once(async move {
875 let mut inner = inner;
876 let first = inner.next().await;
877 let modified = first.into_iter().map(move |result| {
878 result.map(|mut update| {
879 let component = NativeWrapperState::component(chain);
880 let id = component.id.to_string();
881 update
882 .new_pairs
883 .insert(id.clone(), component);
884 update
885 .states
886 .insert(id, Box::new(NativeWrapperState::new(chain)));
887 debug!("Injected native_wrapper component for {chain}");
888 update
889 })
890 });
891 stream::iter(modified).chain(inner)
892 })
893 .flatten(),
894 )
895}
896
897#[cfg(test)]
898mod tests {
899 use std::collections::HashMap;
900
901 use futures::{stream, StreamExt};
902 use tycho_common::models::Chain;
903
904 use super::*;
905 use crate::protocol::models::Update;
906
907 fn empty_update(block: u64) -> Update {
908 Update::new(block, HashMap::new(), HashMap::new())
909 }
910
911 #[tokio::test]
912 async fn test_inject_native_wrapper_first_message_only() {
913 let updates = vec![Ok(empty_update(1)), Ok(empty_update(2)), Ok(empty_update(3))];
914 let input = stream::iter(updates);
915
916 let results: Vec<_> = inject_native_wrapper(input, Chain::Ethereum)
917 .collect()
918 .await;
919
920 assert_eq!(results.len(), 3);
921
922 let expected_id = NativeWrapperState::component(Chain::Ethereum)
923 .id
924 .to_string();
925
926 let first = results[0]
927 .as_ref()
928 .expect("first update ok");
929 assert!(
930 first
931 .new_pairs
932 .contains_key(&expected_id),
933 "first message should have native_wrapper component"
934 );
935 assert!(
936 first.states.contains_key(&expected_id),
937 "first message should have native_wrapper state"
938 );
939
940 let second = results[1]
941 .as_ref()
942 .expect("second update ok");
943 assert!(
944 !second
945 .new_pairs
946 .contains_key(&expected_id),
947 "second message should NOT have native_wrapper component"
948 );
949 assert!(
950 !second.states.contains_key(&expected_id),
951 "second message should NOT have native_wrapper state"
952 );
953 }
954
955 /// Verifies that `with_step_controller` returns both a modified builder and a controller.
956 ///
957 /// This test only checks that the builder method is callable and that the returned controller
958 /// compiles — it does not start any network connection.
959 #[tokio::test]
960 async fn test_with_step_controller_returns_controller() {
961 let builder = ProtocolStreamBuilder::new("tycho-beta.propellerheads.xyz", Chain::Ethereum);
962 let (_builder, controller) = builder.with_step_controller();
963 // The controller was successfully returned — verifying the public API is callable.
964 drop(controller);
965 }
966
967 /// Connects to a live Tycho instance, verifies that the stream blocks until
968 /// `trigger_next_block` is called, and that `peek_next_block` exposes the buffered message.
969 #[ignore = "requires live Tycho connection (TYCHO_AUTH_TOKEN env var)"]
970 #[tokio::test]
971 async fn test_step_controller_trigger_releases_block() {
972 use std::{env, time::Duration};
973
974 use crate::evm::protocol::uniswap_v2::state::UniswapV2State;
975
976 let auth = env::var("TYCHO_AUTH_TOKEN").expect("TYCHO_AUTH_TOKEN must be set");
977
978 // Track a single well-known pool to minimise startup latency.
979 let usdc_weth_v2 = "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc".to_string();
980 let (builder, controller) =
981 ProtocolStreamBuilder::new("tycho-beta.propellerheads.xyz", Chain::Ethereum)
982 .auth_key(Some(auth))
983 .exchange::<UniswapV2State>(
984 "uniswap_v2",
985 ComponentFilter::Ids(vec![usdc_weth_v2]),
986 None,
987 )
988 .with_step_controller();
989
990 let (stream, _pending) = builder
991 .build_with_pending()
992 .await
993 .expect("build_with_pending failed");
994 tokio::pin!(stream);
995
996 // Wait up to 60 s for the first block to arrive in the gating buffer.
997 let peeked = tokio::time::timeout(Duration::from_secs(60), controller.peek_next_block())
998 .await
999 .expect("timed out waiting for first block to buffer")
1000 .expect("stream ended before a block arrived");
1001
1002 assert!(!peeked.sync_states.is_empty(), "peeked block should carry sync states");
1003
1004 // Stream must be empty before we trigger — the gating task should be holding the block.
1005 let pre_trigger = tokio::time::timeout(Duration::from_millis(200), stream.next()).await;
1006 assert!(
1007 pre_trigger.is_err(),
1008 "stream should be blocked before trigger_next_block, got an item"
1009 );
1010
1011 // Release the block.
1012 controller
1013 .trigger_next_block()
1014 .expect("trigger_next_block failed");
1015
1016 // Stream should now yield the decoded update within one block time.
1017 let update = tokio::time::timeout(Duration::from_secs(30), stream.next())
1018 .await
1019 .expect("timed out waiting for update after trigger")
1020 .expect("stream ended unexpectedly");
1021
1022 assert!(update.is_ok(), "decoded update should be Ok, got: {:?}", update);
1023 }
1024}