Skip to main content

tycho_simulation/price_level_stream/
stream.rs

1use std::{
2    collections::{HashMap, HashSet},
3    pin::Pin,
4    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
5};
6
7use async_stream::stream;
8use num_bigint::BigUint;
9use tokio_stream::{Stream, StreamExt};
10use tycho_common::{models::token::Token, Bytes};
11
12use super::{
13    config::{
14        canonical_pamm_address, default_denied_pamms, default_served_pamms, PriceLevelStreamConfig,
15        DEFAULT_AUTO_DETECTED_GAS_COST,
16    },
17    titan::{
18        self, ConnectionSettings, TitanQuoteStreamMessage, TITAN_PRICE_LEVEL_URL,
19        TITAN_PRICE_LEVEL_URL_ENV, TITAN_QUOTE_STREAM_URL, TITAN_QUOTE_STREAM_URL_ENV,
20    },
21    tracker::{FreshnessTracker, Now, TrackerSettings, DEFAULT_STALE_AFTER},
22};
23use crate::protocol::models::Update;
24
25/// Static attribute under which each emitted component carries its pAMM venue address.
26pub const PAMM_ADDRESS_ATTRIBUTE: &str = "pamm_address";
27
28/// The longest [`stale_after`](PriceLevelStreamBuilder::stale_after) the builder accepts;
29/// longer values are capped to it. Quotes target the block being built, so serving quotes for
30/// longer than this is never intended, and deadlines this far ahead stay representable on the
31/// monotonic clock.
32pub const MAX_STALE_AFTER: Duration = Duration::from_secs(3600);
33
34/// Builds a stream of [`Update`]s from the Titan pAMM price level WebSocket.
35///
36/// A new builder serves no pAMMs: register the known venues via
37/// [`with_known_pamms`](Self::with_known_pamms), individual ones via
38/// [`add_pamm`](Self::add_pamm), or opt into serving unknown streamed venues via
39/// [`auto_detect`](Self::auto_detect); [`with_tokens`](Self::with_tokens) provides the token
40/// metadata pairs are interpreted with.
41///
42/// One component is emitted per (pAMM, token pair), identified by the concatenation
43/// `pamm ++ token0 ++ token1` (tokens sorted ascending), under the protocol system
44/// `fallback:{pamm}` — or `pricelevelstream:{pamm}` after
45/// [`without_fallback_router`](Self::without_fallback_router). The venue address is exposed
46/// through the [`PAMM_ADDRESS_ATTRIBUTE`] static attribute for downstream encoding.
47pub struct PriceLevelStreamBuilder {
48    /// The venues to serve, keyed by canonical public identity.
49    registry: HashMap<Bytes, PriceLevelStreamConfig>,
50    /// The venues not to serve, by canonical public identity.
51    denied: HashSet<Bytes>,
52    tokens: HashMap<Bytes, Token>,
53    url: Option<String>,
54    /// See [`quote_stream_endpoint`](Self::quote_stream_endpoint).
55    quote_stream_url: Option<String>,
56    /// See [`without_simulation_overrides`](Self::without_simulation_overrides).
57    simulation_overrides: bool,
58    auto_detect: bool,
59    auto_detected_gas_cost: Option<BigUint>,
60    connection: ConnectionSettings,
61    /// Whether components are emitted under the `fallback:` family, executed through
62    /// `TychoFallbackRouter`, instead of the direct `pricelevelstream:` family.
63    fallback_router: bool,
64    /// See [`stale_after`](Self::stale_after).
65    stale_after: Duration,
66    /// See [`without_quote_guard`](Self::without_quote_guard).
67    quote_guard: bool,
68}
69
70impl Default for PriceLevelStreamBuilder {
71    fn default() -> Self {
72        Self {
73            registry: HashMap::new(),
74            denied: HashSet::new(),
75            tokens: HashMap::new(),
76            url: None,
77            quote_stream_url: None,
78            simulation_overrides: true,
79            auto_detect: false,
80            auto_detected_gas_cost: None,
81            connection: ConnectionSettings::default(),
82            fallback_router: true,
83            stale_after: DEFAULT_STALE_AFTER,
84            quote_guard: true,
85        }
86    }
87}
88
89impl PriceLevelStreamBuilder {
90    pub fn new() -> Self {
91        Self::default()
92    }
93
94    /// Enables serving pAMMs that are not registered via
95    /// [`with_known_pamms`](Self::with_known_pamms) or [`add_pamm`](Self::add_pamm)
96    /// (disabled by default).
97    ///
98    /// When enabled, any unknown streamed venue — except denied ones (see
99    /// [`deny_pamm`](Self::deny_pamm)) — is served under its full lowercase hex address
100    /// as the name, with the default gas cost. A venue's protocol system therefore changes from
101    /// the address form (`pricelevelstream:{0xaddress}`) to a name (`pricelevelstream:{name}`)
102    /// once it gets registered — via [`add_pamm`](Self::add_pamm) or a release's
103    /// [`default_served_pamms`] recognizing it; the name-independent identifiers — the component id
104    /// and the [`PAMM_ADDRESS_ATTRIBUTE`] — stay stable across such renames.
105    pub fn auto_detect(mut self, enabled: bool) -> Self {
106        self.auto_detect = enabled;
107        self
108    }
109
110    /// Overrides the per-swap gas cost that auto-detected pAMMs (see
111    /// [`auto_detect`](Self::auto_detect)) are served with. Defaults to the maximum over the
112    /// known venue profiles, as the conservative choice. Registered venues are unaffected —
113    /// their gas cost comes from their [`PriceLevelStreamConfig`].
114    pub fn auto_detected_gas_cost(mut self, gas_cost: BigUint) -> Self {
115        self.auto_detected_gas_cost = Some(gas_cost);
116        self
117    }
118
119    /// Overrides the stream endpoint, e.g. to connect to a closer Titan region than the default
120    /// (see <https://docs.titanbuilder.xyz/propamms/takers>). Without it, the
121    /// `TITAN_PAMM_PRICE_LEVEL_URL` environment variable is used when set, else the built-in
122    /// default.
123    ///
124    /// This sets the price level socket only; the quote stream socket has its own
125    /// [`quote_stream_endpoint`](Self::quote_stream_endpoint) and its own default. To change
126    /// region, set both: a region's quote stream matches that region's price levels, and nobody
127    /// has tested whether it matches another region's. `build` logs a WARN when only one of the
128    /// two is customized.
129    pub fn endpoint(mut self, url: impl Into<String>) -> Self {
130        self.url = Some(url.into());
131        self
132    }
133
134    /// Sets the endpoint of the quote stream socket, which carries the account state Titan priced
135    /// the books on. Point it at the same Titan region as [`endpoint`](Self::endpoint). Without
136    /// it, the stream uses the `TITAN_PAMM_QUOTE_STREAM_URL` environment variable when set, else
137    /// the built-in default. [`without_simulation_overrides`](Self::without_simulation_overrides)
138    /// wins in either call order: with it, this setting has no effect.
139    pub fn quote_stream_endpoint(mut self, url: impl Into<String>) -> Self {
140        self.quote_stream_url = Some(url.into());
141        self
142    }
143
144    /// Emits states without simulation overrides, and connects no quote stream socket.
145    ///
146    /// By default the stream also reads Titan's quote stream, which publishes the account state
147    /// Titan simulated each venue on to price its books, and attaches it to the matching states
148    /// (see [`simulation_overrides`](super::state::PriceLevelStreamState::simulation_overrides)).
149    /// A consumer that never simulates a swap with `eth_call` does not need it; opting out spares
150    /// the second connection and the memory the held entries use. Quoting is the same either way:
151    /// overrides never change a quote.
152    pub fn without_simulation_overrides(mut self) -> Self {
153        self.simulation_overrides = false;
154        self
155    }
156
157    /// Overrides how long a single connection attempt may take before it is aborted and retried
158    /// (default: 10s), so a hung TCP/TLS handshake cannot block the stream forever. Applies to
159    /// both Titan sockets, which are connected and maintained independently.
160    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
161        self.connection.connect_timeout = timeout;
162        self
163    }
164
165    /// Overrides the longest gap between parsed Titan messages tolerated before the connection is
166    /// treated as dead and re-established (default: 10s). Titan pushes one message per second and
167    /// sends no keepalives, so a multi-second silence means a stalled or half-open connection.
168    /// Control frames and unparsable text do not reset this timeout, and neither does the time
169    /// the consumer spends between polls: the gap is measured while the stream waits on the
170    /// socket. Applies to both Titan sockets, each measured on its own traffic.
171    pub fn read_idle_timeout(mut self, timeout: Duration) -> Self {
172        self.connection.read_idle_timeout = timeout;
173        self
174    }
175
176    /// Overrides the cap on the exponential reconnect backoff of `2^attempt` seconds
177    /// (default: 32s). Applies to both Titan sockets, each backing off on its own failures.
178    pub fn max_backoff(mut self, max_backoff: Duration) -> Self {
179        self.connection.max_backoff = max_backoff;
180        self
181    }
182
183    /// Registers a pAMM to be served under the given configuration, overriding any default,
184    /// denied, or auto-detected one for the same venue.
185    ///
186    /// The configured address may be either of a venue's addresses — the one Titan streams its
187    /// books under or its canonical public identity (see
188    /// [`PAMM_ADDRESS_ALIASES`](super::config::PAMM_ADDRESS_ALIASES)) — because it is normalized
189    /// to the public identity, which is what the emitted components then carry. Between
190    /// [`add_pamm`](Self::add_pamm) and [`deny_pamm`](Self::deny_pamm) for the same venue, the
191    /// later call wins, whichever of its addresses each named; the defaults applied by
192    /// [`with_known_pamms`](Self::with_known_pamms) never override either, in any call order.
193    pub fn add_pamm(mut self, config: PriceLevelStreamConfig) -> Self {
194        let config =
195            PriceLevelStreamConfig { address: canonical_pamm_address(&config.address), ..config };
196        self.denied.remove(&config.address);
197        self.registry
198            .insert(config.address.clone(), config);
199        self
200    }
201
202    /// Excludes a venue from being served: drops its current registration (default or explicit)
203    /// and blocks auto-detecting it.
204    ///
205    /// As for [`add_pamm`](Self::add_pamm), `address` may be either of the venue's addresses, so
206    /// denying a venue by its public identity also denies the books it streams under another
207    /// one. Between [`add_pamm`](Self::add_pamm) and [`deny_pamm`](Self::deny_pamm) for the same
208    /// venue, the later call wins; the defaults applied by
209    /// [`with_known_pamms`](Self::with_known_pamms) never override either, in any call order —
210    /// so denying a venue from the default set works whether the denial comes before or after
211    /// [`with_known_pamms`](Self::with_known_pamms).
212    pub fn deny_pamm(mut self, address: Bytes) -> Self {
213        let address = canonical_pamm_address(&address);
214        self.registry.remove(&address);
215        self.denied.insert(address);
216        self
217    }
218
219    /// Applies what is known about the streamed venues: registers the known-good ones
220    /// ([`default_served_pamms`]) to be served and denies the known-bad ones
221    /// ([`default_denied_pamms`]) — venues that stream quotes but whose swaps are not executable.
222    ///
223    /// These defaults never override an explicit [`add_pamm`](Self::add_pamm) or
224    /// [`deny_pamm`](Self::deny_pamm) for the same venue, regardless of call order.
225    pub fn with_known_pamms(mut self) -> Self {
226        for config in default_served_pamms() {
227            let config = PriceLevelStreamConfig {
228                address: canonical_pamm_address(&config.address),
229                ..config
230            };
231            if self.denied.contains(&config.address) {
232                continue;
233            }
234            self.registry
235                .entry(config.address.clone())
236                .or_insert(config);
237        }
238        for address in default_denied_pamms() {
239            let address = canonical_pamm_address(&address);
240            if self.registry.contains_key(&address) {
241                continue;
242            }
243            self.denied.insert(address);
244        }
245        self
246    }
247
248    /// Provides the token metadata used to build components and interpret amounts, and the set of
249    /// assets the stream may price at all: a book is served only if both addresses it names are
250    /// present here, and those tokens supply the decimals its price levels are converted with.
251    /// Books naming any other asset are skipped.
252    pub fn with_tokens(mut self, tokens: HashMap<Bytes, Token>) -> Self {
253        self.tokens = tokens;
254        self
255    }
256
257    /// Keeps every venue on the direct `pricelevelstream:{name}` path, so swaps execute on the
258    /// venues themselves and a stale maker quote reverts the route.
259    ///
260    /// By default components are emitted under `fallback:{name}`, so tycho-execution routes
261    /// their swaps through `TychoFallbackRouter`. Opt out when the direct call is what you want
262    /// to measure or execute.
263    pub fn without_fallback_router(mut self) -> Self {
264        self.fallback_router = false;
265        self
266    }
267
268    /// Overrides how long a component stays served after the last accepted message that carried
269    /// it (default: 24s, two slots). A component no accepted message has carried for this long
270    /// turns stale and is emitted in `removed_pairs`; the next accepted message carrying it
271    /// re-adds it in `new_pairs`. Messages whose `ts` is this old or older are rejected.
272    /// Independent of this setting, a state refuses to quote once its message is one slot old
273    /// (see [`without_quote_guard`](Self::without_quote_guard)).
274    ///
275    /// Values above [`MAX_STALE_AFTER`] are capped to it. A value shorter than the age messages
276    /// arrive with rejects every message as `too_old`, which the
277    /// `price_level_stream_frames_rejected_total` counter and a WARN log show.
278    pub fn stale_after(mut self, duration: Duration) -> Self {
279        self.stale_after = duration.min(MAX_STALE_AFTER);
280        self
281    }
282
283    /// Emits states that never refuse to quote.
284    ///
285    /// By default every emitted state refuses `spot_price`, `get_amount_out` and `get_limits`
286    /// once its message is one slot ([`QUOTE_TTL`](super::state::QUOTE_TTL)) old: Titan prices
287    /// the block being built, so after one slot the quotes were priced for a block already built
288    /// and may no longer match what the venue fills. Opt out only for a consumer that quotes a
289    /// state more than one slot after it arrived by design — a batch simulator, a validation
290    /// harness — and that accepts a quote the venue may no longer fill. The component still
291    /// turns stale and is removed after [`stale_after`](Self::stale_after), which then becomes
292    /// the only bound on how old served quotes can be. Never disable it on a live router.
293    pub fn without_quote_guard(mut self) -> Self {
294        self.quote_guard = false;
295        self
296    }
297
298    /// Consumes the builder and opens the stream.
299    ///
300    /// Components are emitted under `fallback:{name}`, so tycho-execution routes their swaps
301    /// through `TychoFallbackRouter`, which retries a reverted pAMM swap — a stale maker quote
302    /// reverts in any simulation against a mined block — on the fallback pool the solver names.
303    /// [`without_fallback_router`](Self::without_fallback_router) keeps them on the direct
304    /// `pricelevelstream:` path.
305    ///
306    /// The stream connects the price level socket, and the quote stream socket unless
307    /// [`without_simulation_overrides`](Self::without_simulation_overrides) turned it off, on the
308    /// first poll. It reconnects both for as long as it is polled; neither ends on its own, and
309    /// dropping the stream closes both. A quote stream message produces no update of its own: the
310    /// tracker holds its entries until a later price level message's books name them. A quote
311    /// stream that is refused, silent or malformed costs the states their overrides and nothing
312    /// else: the stream never waits on the quote stream socket to serve a price level message or
313    /// a stale removal.
314    ///
315    /// Every accepted message yields an update with the states of the served pairs it carries,
316    /// with `new_pairs` for pairs not currently served. The update does not mention pairs the
317    /// message does not carry, so consumers keep their previous state. A component no accepted
318    /// message has carried for [`stale_after`](Self::stale_after) turns stale and is emitted in
319    /// `removed_pairs`, together with every other component turning stale at that instant, and
320    /// re-added by the next accepted message carrying it. Messages that are too old, from the
321    /// future, out of order, or whose block regresses or jumps more than one block per elapsed
322    /// slot plus 2 are rejected without effect. Messages that contain no served pAMM produce no
323    /// update. Books naming an address absent from the provided token metadata are skipped, as is
324    /// any one book side whose levels do not state a usable price function — leaving that
325    /// direction unquotable.
326    ///
327    /// Every update carries the block its message targets, and a removal carries the newest
328    /// accepted block. Block numbers never decrease while something is served. Once every
329    /// component has turned stale, the next accepted message is judged as a first message and may
330    /// carry a lower block than the removal did: that is how the stream recovers from a message
331    /// with an implausible block, so consumers must not rely on the block number to order
332    /// updates across such a gap. See the [module documentation](super) for the full contract.
333    pub fn build(self) -> impl Stream<Item = Update> + Send {
334        let Self {
335            registry,
336            denied,
337            tokens,
338            url,
339            quote_stream_url,
340            simulation_overrides,
341            auto_detect,
342            auto_detected_gas_cost,
343            connection,
344            fallback_router,
345            stale_after,
346            quote_guard,
347        } = self;
348        if registry.is_empty() && !auto_detect {
349            tracing::warn!(
350                "No pAMMs registered and auto-detection is off; the stream will never produce \
351                 an update"
352            );
353        }
354        if tokens.is_empty() {
355            tracing::warn!(
356                "No token metadata provided; every streamed book will be skipped and the stream \
357                 will never produce an update"
358            );
359        }
360        let url = url.unwrap_or_else(|| {
361            std::env::var(TITAN_PRICE_LEVEL_URL_ENV)
362                .unwrap_or_else(|_| TITAN_PRICE_LEVEL_URL.to_string())
363        });
364        let quote_stream_url = simulation_overrides.then(|| {
365            quote_stream_url.unwrap_or_else(|| {
366                std::env::var(TITAN_QUOTE_STREAM_URL_ENV)
367                    .unwrap_or_else(|_| TITAN_QUOTE_STREAM_URL.to_string())
368            })
369        });
370        if let Some(quote_stream_url) = &quote_stream_url {
371            if (url == TITAN_PRICE_LEVEL_URL) != (quote_stream_url == TITAN_QUOTE_STREAM_URL) {
372                tracing::warn!(
373                    price_level_url = url,
374                    quote_stream_url,
375                    "Only one of the two Titan endpoints is customized; the quote stream of one \
376                     region may not match the price levels of another, which costs states their \
377                     simulation overrides"
378                );
379            }
380        }
381        let auto_detected_gas_cost =
382            auto_detected_gas_cost.unwrap_or_else(|| BigUint::from(DEFAULT_AUTO_DETECTED_GAS_COST));
383        let mut tracker = FreshnessTracker::new(TrackerSettings {
384            registry,
385            denied,
386            tokens,
387            auto_detect,
388            auto_detected_gas_cost,
389            stale_after,
390            via_fallback_router: fallback_router,
391            quote_guard,
392            simulation_overrides,
393        });
394
395        stream! {
396            let messages = titan::messages(url, connection);
397            tokio::pin!(messages);
398            // A stream that never yields stands in for the quote stream socket when it is off.
399            let mut quote_stream_messages: Pin<
400                Box<dyn Stream<Item = TitanQuoteStreamMessage> + Send>,
401            > = match quote_stream_url {
402                Some(url) => {
403                    Box::pin(titan::messages::<TitanQuoteStreamMessage>(url, connection))
404                }
405                None => Box::pin(tokio_stream::pending()),
406            };
407            loop {
408                let deadline = tracker.stale_deadline();
409                let sleep_until_deadline = tokio::time::sleep_until(
410                    deadline.map_or_else(tokio::time::Instant::now, tokio::time::Instant::from_std),
411                );
412                let update = tokio::select! {
413                    Some(message) = messages.next() => {
414                        tracker.on_price_level_message(message, now())
415                    }
416                    Some(message) = quote_stream_messages.next() => {
417                        tracker.on_quote_stream_message(message);
418                        None
419                    }
420                    () = sleep_until_deadline, if deadline.is_some() => {
421                        tracker.on_stale_deadline(timer_now())
422                    }
423                };
424                if let Some(update) = update {
425                    yield update;
426                }
427            }
428        }
429    }
430}
431
432/// The clocks a message is judged against: the wall clock for Titan's `ts`, and the
433/// timer's clock for every deadline. This is the only place the stream reads a clock; the
434/// tracker only ever sees the values it is handed.
435fn now() -> Now {
436    Now { wall_nanos: wall_clock_nanos(), monotonic: timer_now() }
437}
438
439/// Nanoseconds since the Unix epoch. A system clock unrepresentable as unix nanoseconds yields
440/// 0, which rejects every message as `in_future`.
441fn wall_clock_nanos() -> u64 {
442    let since_epoch = SystemTime::now()
443        .duration_since(UNIX_EPOCH)
444        .ok()
445        .and_then(|since_epoch| u64::try_from(since_epoch.as_nanos()).ok());
446    match since_epoch {
447        Some(wall_nanos) => wall_nanos,
448        None => {
449            tracing::error!(
450                "System clock unrepresentable as unix nanoseconds; every price level message will \
451                 be rejected as in_future"
452            );
453            0
454        }
455    }
456}
457
458/// The clock the deadline timer runs on. Deadlines are set and swept with it so that a sweep
459/// fired by the timer finds the component due, also under `tokio::time::pause()`, where the
460/// runtime's virtual time and `std::time::Instant` diverge.
461fn timer_now() -> Instant {
462    tokio::time::Instant::now().into_std()
463}
464
465#[cfg(test)]
466mod tests {
467    use std::{
468        pin::Pin,
469        str::FromStr,
470        sync::{
471            atomic::{AtomicBool, AtomicUsize, Ordering},
472            Arc,
473        },
474        time::Duration,
475    };
476
477    use futures::{future::BoxFuture, SinkExt};
478    use num_bigint::BigUint;
479    use rstest::rstest;
480    use tokio_tungstenite::tungstenite::Message;
481
482    use super::{
483        super::{
484            config::{default_denied_pamms, PriceLevelStreamConfig},
485            state::PriceLevelStreamState,
486            telemetry::{
487                recorded::{counter_value, record_async},
488                RECONNECTS,
489            },
490            test_support::{
491                address, captured_message_text, captured_quote_stream_text, captured_tokens,
492                fermiswap, matching_message_text, message_text, message_then_repeat, token,
493                token_map, tokens, wall_nanos_now, FakeConnection, FakeTitan, KIPSELI,
494                KIPSELI_STREAMED, PAMM, USDC, WBTC, WETH,
495            },
496        },
497        *,
498    };
499
500    #[test]
501    fn explicit_add_and_deny_are_last_wins() {
502        let address = Bytes::from_str(PAMM).unwrap();
503        let custom =
504            || PriceLevelStreamConfig::new("custom", Bytes::from_str(PAMM).unwrap(), 1u64.into());
505
506        let builder = PriceLevelStreamBuilder::new()
507            .add_pamm(custom())
508            .deny_pamm(address.clone());
509        assert!(!builder.registry.contains_key(&address));
510        assert!(builder.denied.contains(&address));
511
512        let builder = PriceLevelStreamBuilder::new()
513            .deny_pamm(address.clone())
514            .add_pamm(custom());
515        assert_eq!(builder.registry[&address].protocol, "custom");
516        assert!(builder.denied.is_empty());
517    }
518
519    #[test]
520    fn quote_guard_is_on_unless_opted_out() {
521        assert!(PriceLevelStreamBuilder::new().quote_guard);
522        assert!(
523            !PriceLevelStreamBuilder::new()
524                .without_quote_guard()
525                .quote_guard
526        );
527    }
528
529    /// Short timings for a test: `stale_after` 2 s, `read_idle_timeout` 100 ms, `max_backoff`
530    /// 20 ms, reading price level messages from `fake`.
531    ///
532    /// It leaves the quote stream socket at its default, Titan's live endpoint, so no test builds
533    /// on it directly: tests use [`fast_timings`], which turns that socket off, or
534    /// [`two_socket_timings`], which points it at a second fake.
535    fn timings(fake: &FakeTitan) -> PriceLevelStreamBuilder {
536        PriceLevelStreamBuilder::new()
537            .endpoint(fake.url())
538            .stale_after(STALE_AFTER)
539            .connect_timeout(Duration::from_secs(1))
540            .read_idle_timeout(Duration::from_millis(100))
541            .max_backoff(Duration::from_millis(20))
542    }
543
544    /// [`timings`] with no quote stream socket, for everything that socket has no part in.
545    fn fast_timings(fake: &FakeTitan) -> PriceLevelStreamBuilder {
546        timings(fake).without_simulation_overrides()
547    }
548
549    /// [`timings`] with the quote stream socket pointed at a second fake: the default shape of the
550    /// stream, with both endpoints local.
551    fn two_socket_timings(prices: &FakeTitan, overrides: &FakeTitan) -> PriceLevelStreamBuilder {
552        timings(prices).quote_stream_endpoint(overrides.url())
553    }
554
555    /// A [`fast_timings`] builder serving the FermiSwap venue with the bound assets' metadata.
556    fn fast_builder(fake: &FakeTitan) -> PriceLevelStreamBuilder {
557        fast_timings(fake)
558            .add_pamm(fermiswap())
559            .with_tokens(tokens())
560    }
561
562    /// The `stale_after` of [`fast_builder`]: long enough that a loaded runner does not reject
563    /// the first message as too old, short enough that a removal arrives within [`WAIT`].
564    const STALE_AFTER: Duration = Duration::from_secs(2);
565
566    /// How long a test waits for one update.
567    const WAIT: Duration = Duration::from_secs(5);
568
569    async fn next_within(
570        stream: &mut Pin<&mut impl Stream<Item = Update>>,
571        limit: Duration,
572    ) -> Option<Update> {
573        tokio::time::timeout(limit, stream.next())
574            .await
575            .ok()
576            .flatten()
577    }
578
579    /// Waits for the first update and checks that it adds the one served component.
580    async fn expect_first_update(stream: &mut Pin<&mut impl Stream<Item = Update>>) -> Update {
581        let first = next_within(stream, WAIT)
582            .await
583            .expect("first update");
584        assert_eq!(first.new_pairs.len(), 1);
585        assert!(first.removed_pairs.is_empty());
586        first
587    }
588
589    /// Waits up to [`WAIT`] for an update that removes components, skipping the updates that
590    /// only refresh served ones; an update that re-adds a component before the removal fails.
591    async fn expect_removal(stream: &mut Pin<&mut impl Stream<Item = Update>>) -> Update {
592        tokio::time::timeout(WAIT, async {
593            loop {
594                let update = stream
595                    .next()
596                    .await
597                    .expect("stream ended");
598                if !update.removed_pairs.is_empty() {
599                    return update;
600                }
601                assert!(update.new_pairs.is_empty(), "component re-added before the removal");
602            }
603        })
604        .await
605        .expect("removal")
606    }
607
608    fn assert_removal_only(update: &Update, expected_removed: usize) {
609        assert!(update.states.is_empty());
610        assert!(update.new_pairs.is_empty());
611        assert!(update.sync_states.is_empty());
612        assert!(update.is_partial);
613        assert_eq!(update.removed_pairs.len(), expected_removed);
614    }
615
616    fn fresh_message() -> Message {
617        Message::Text(message_text(100, wall_nanos_now()).into())
618    }
619
620    /// Sends one fresh message on the first connection only; later connections stay silent.
621    async fn first_connection_sends_one_message(index: usize, mut socket: FakeConnection) {
622        if index == 0 {
623            let _ = socket.send(fresh_message()).await;
624        }
625        std::future::pending::<()>().await;
626    }
627
628    #[tokio::test]
629    async fn silence_past_stale_after_removes_every_served_component() {
630        let fake = FakeTitan::spawn(first_connection_sends_one_message).await;
631        let stream = fast_builder(&fake).build();
632        tokio::pin!(stream);
633
634        expect_first_update(&mut stream).await;
635        // The stream reconnects on idle timeout, but the later connections send no message, so no
636        // component deadline moves.
637        let removal = next_within(&mut stream, WAIT)
638            .await
639            .expect("removal");
640
641        assert_removal_only(&removal, 1);
642        assert_eq!(removal.block_number, 100);
643        assert!(fake.connections.load(Ordering::SeqCst) >= 2, "no reconnect on idle timeout");
644    }
645
646    #[tokio::test]
647    async fn repeated_immediate_closes_remove_within_stale_after() {
648        let fake = FakeTitan::spawn(|index, mut socket| async move {
649            if index == 0 {
650                let _ = socket.send(fresh_message()).await;
651                tokio::time::sleep(Duration::from_millis(20)).await;
652            }
653            let _ = socket.close(None).await;
654        })
655        .await;
656        let stream = fast_builder(&fake).build();
657        tokio::pin!(stream);
658
659        expect_first_update(&mut stream).await;
660        let removal = next_within(&mut stream, WAIT)
661            .await
662            .expect("removal");
663
664        assert_removal_only(&removal, 1);
665    }
666
667    #[test]
668    fn refused_reconnects_remove_within_stale_after() {
669        let (removal, snapshot) = record_async(async {
670            let mut fake = FakeTitan::spawn(|_, mut socket| async move {
671                let _ = socket.send(fresh_message()).await;
672                tokio::time::sleep(Duration::from_millis(20)).await;
673                let _ = socket.close(None).await;
674            })
675            .await;
676            let stream = fast_builder(&fake).build();
677            tokio::pin!(stream);
678
679            expect_first_update(&mut stream).await;
680            // From here every connect is refused at TCP level. The fake closes 20 ms in and the
681            // backoff is capped at 20 ms, so one reconnect can be accepted before `shutdown`
682            // drops the listener; its fresh message refreshes the component without re-adding it.
683            fake.shutdown();
684            expect_removal(&mut stream).await
685        });
686
687        assert_removal_only(&removal, 1);
688        assert!(
689            counter_value(
690                &snapshot,
691                RECONNECTS,
692                &[("reason", "connect_failed"), ("socket", "price_levels")]
693            ) >= 1,
694            "no connect was refused"
695        );
696    }
697
698    #[tokio::test]
699    async fn replayed_messages_remove_within_stale_after_and_never_re_add() {
700        // One message, with one `ts`, replayed every 50 ms forever.
701        let replay = fresh_message();
702        let fake = FakeTitan::spawn(message_then_repeat(
703            replay.clone(),
704            replay,
705            Duration::from_millis(50),
706        ))
707        .await;
708        let stream = fast_builder(&fake)
709            .read_idle_timeout(Duration::from_secs(5))
710            .build();
711        tokio::pin!(stream);
712
713        expect_first_update(&mut stream).await;
714        // Replays are accepted while fresh but cannot extend the deadline; the deadline fires
715        // even though a message is ready on every poll.
716        let removal = expect_removal(&mut stream).await;
717
718        assert_removal_only(&removal, 1);
719        // Every later replay is too old to be accepted: nothing comes back.
720        assert!(next_within(&mut stream, Duration::from_millis(500))
721            .await
722            .is_none());
723    }
724
725    #[tokio::test]
726    async fn fresh_message_after_removal_re_adds_the_component() {
727        let fake = FakeTitan::spawn(|_, mut socket| async move {
728            let _ = socket.send(fresh_message()).await;
729            tokio::time::sleep(STALE_AFTER + Duration::from_millis(500)).await;
730            let _ = socket
731                .send(Message::Text(message_text(101, wall_nanos_now()).into()))
732                .await;
733            std::future::pending::<()>().await;
734        })
735        .await;
736        let stream = fast_builder(&fake)
737            .read_idle_timeout(Duration::from_secs(5))
738            .build();
739        tokio::pin!(stream);
740
741        expect_first_update(&mut stream).await;
742        let removal = next_within(&mut stream, WAIT)
743            .await
744            .expect("removal");
745        let re_added = next_within(&mut stream, WAIT)
746            .await
747            .expect("re-add");
748
749        assert_removal_only(&removal, 1);
750        assert_eq!(re_added.new_pairs.len(), 1);
751        assert!(re_added.removed_pairs.is_empty());
752        assert_eq!(re_added.block_number, 101);
753    }
754
755    #[tokio::test]
756    async fn messages_are_forwarded_without_waiting_on_timers() {
757        let fake = FakeTitan::spawn(fresh_message_every(Duration::from_millis(50))).await;
758        let stream = fast_builder(&fake)
759            .stale_after(Duration::from_secs(24))
760            .build();
761        tokio::pin!(stream);
762
763        expect_first_update(&mut stream).await;
764        for _ in 0..5 {
765            let update = next_within(&mut stream, Duration::from_secs(1))
766                .await
767                .expect("steady-state message");
768            assert!(update.removed_pairs.is_empty());
769        }
770    }
771
772    #[tokio::test]
773    async fn no_connection_before_first_poll() {
774        let fake = FakeTitan::spawn(first_connection_sends_one_message).await;
775        let stream = fast_builder(&fake).build();
776        tokio::pin!(stream);
777
778        tokio::time::sleep(Duration::from_millis(150)).await;
779
780        assert_eq!(fake.connections.load(Ordering::SeqCst), 0, "connected before first poll");
781    }
782
783    #[tokio::test]
784    async fn drop_closes_the_socket() {
785        let server_saw_close = Arc::new(AtomicBool::new(false));
786        let fake = {
787            let server_saw_close = server_saw_close.clone();
788            FakeTitan::spawn(move |_, mut socket| {
789                let server_saw_close = server_saw_close.clone();
790                async move {
791                    let _ = socket.send(fresh_message()).await;
792                    // Read until the client goes away.
793                    while let Some(Ok(message)) = socket.next().await {
794                        if matches!(message, Message::Close(_)) {
795                            break;
796                        }
797                    }
798                    server_saw_close.store(true, Ordering::SeqCst);
799                }
800            })
801            .await
802        };
803        // `Box::pin` rather than `tokio::pin!`, so that `drop` below drops the stream itself
804        // rather than a `Pin<&mut _>` pointing at a value that outlives the assertion.
805        let mut stream = Box::pin(
806            fast_builder(&fake)
807                .read_idle_timeout(Duration::from_secs(5))
808                .build(),
809        );
810        expect_first_update(&mut stream.as_mut()).await;
811
812        drop(stream);
813        tokio::time::sleep(Duration::from_millis(200)).await;
814
815        assert!(server_saw_close.load(Ordering::SeqCst), "socket not closed on drop");
816        assert_eq!(fake.connections.load(Ordering::SeqCst), 1, "reconnected after drop");
817    }
818
819    /// `Instant + Duration` panics on overflow, so an absurd `stale_after` must be capped before
820    /// the first accepted message computes a deadline from it.
821    #[tokio::test]
822    async fn stale_after_above_the_cap_still_serves() {
823        assert_eq!(
824            PriceLevelStreamBuilder::new()
825                .stale_after(Duration::MAX)
826                .stale_after,
827            MAX_STALE_AFTER
828        );
829        let fake = FakeTitan::spawn(first_connection_sends_one_message).await;
830        let stream = fast_builder(&fake)
831            .stale_after(Duration::MAX)
832            .build();
833        tokio::pin!(stream);
834
835        expect_first_update(&mut stream).await;
836    }
837
838    #[tokio::test]
839    async fn without_quote_guard_emits_states_that_never_expire() {
840        let fake = FakeTitan::spawn(first_connection_sends_one_message).await;
841        let stream = fast_builder(&fake)
842            .without_quote_guard()
843            .build();
844        tokio::pin!(stream);
845
846        let first = expect_first_update(&mut stream).await;
847
848        let state = first
849            .states
850            .values()
851            .next()
852            .expect("one state")
853            .as_any()
854            .downcast_ref::<PriceLevelStreamState>()
855            .expect("price level state");
856        assert!(state.quotable_until().is_none());
857    }
858
859    /// A [`FakeTitan`] handler that sends a message with a current `ts` every `interval` until the
860    /// socket closes.
861    fn fresh_message_every(
862        interval: Duration,
863    ) -> impl Fn(usize, FakeConnection) -> BoxFuture<'static, ()> + Send + Sync + 'static {
864        move |_, mut socket| {
865            Box::pin(async move {
866                while socket
867                    .send(fresh_message())
868                    .await
869                    .is_ok()
870                {
871                    tokio::time::sleep(interval).await;
872                }
873            })
874        }
875    }
876
877    /// The fallback router path is the default; `without_fallback_router` is the way off it.
878    #[test]
879    fn fallback_router_is_on_unless_opted_out() {
880        assert!(PriceLevelStreamBuilder::new().fallback_router);
881        assert!(
882            !PriceLevelStreamBuilder::new()
883                .without_fallback_router()
884                .fallback_router
885        );
886    }
887
888    #[test]
889    fn defaults_never_override_explicit_calls() {
890        // Denying a venue from the default set works in either call order.
891        let fermiswap_router = Bytes::from_str(PAMM).unwrap();
892        for builder in [
893            PriceLevelStreamBuilder::new()
894                .deny_pamm(fermiswap_router.clone())
895                .with_known_pamms(),
896            PriceLevelStreamBuilder::new()
897                .with_known_pamms()
898                .deny_pamm(fermiswap_router.clone()),
899        ] {
900            assert!(!builder
901                .registry
902                .contains_key(&fermiswap_router));
903            assert!(builder
904                .denied
905                .contains(&fermiswap_router));
906            // The other defaults are unaffected.
907            assert!(!builder.registry.is_empty());
908        }
909
910        // Registering a venue from the default deny set works in either call order. Any one of
911        // them exercises that; the set is empty while every streamed venue is executable.
912        let Some(denied_venue) = default_denied_pamms().pop() else { return };
913        let custom = || PriceLevelStreamConfig::new("custom", denied_venue.clone(), 1u64.into());
914        for builder in [
915            PriceLevelStreamBuilder::new()
916                .add_pamm(custom())
917                .with_known_pamms(),
918            PriceLevelStreamBuilder::new()
919                .with_known_pamms()
920                .add_pamm(custom()),
921        ] {
922            assert_eq!(builder.registry[&denied_venue].protocol, "custom");
923            assert!(!builder.denied.contains(&denied_venue));
924        }
925    }
926
927    #[test]
928    fn with_known_pamms_registers_known_venues() {
929        // PAMM is the FermiSwap router, one of the default venues.
930        let fermiswap_router = Bytes::from_str(PAMM).unwrap();
931
932        let builder = PriceLevelStreamBuilder::new();
933        assert!(builder.registry.is_empty());
934        assert!(builder.denied.is_empty());
935
936        let builder = builder.with_known_pamms();
937        assert_eq!(builder.registry[&fermiswap_router].protocol, "fermiswap");
938        // The known-bad venues get denied alongside, and never overlap the served defaults.
939        assert_eq!(
940            builder.denied,
941            default_denied_pamms()
942                .into_iter()
943                .collect()
944        );
945        assert!(builder.denied.is_disjoint(
946            &builder
947                .registry
948                .keys()
949                .cloned()
950                .collect()
951        ));
952
953        // An `add_pamm` entry wins over the default for the same address, in either call order.
954        let custom =
955            || PriceLevelStreamConfig::new("custom", fermiswap_router.clone(), BigUint::from(1u64));
956        for builder in [
957            PriceLevelStreamBuilder::new()
958                .add_pamm(custom())
959                .with_known_pamms(),
960            PriceLevelStreamBuilder::new()
961                .with_known_pamms()
962                .add_pamm(custom()),
963        ] {
964            assert_eq!(builder.registry[&fermiswap_router].protocol, "custom");
965            assert_eq!(builder.registry[&fermiswap_router].gas_cost, BigUint::from(1u64));
966        }
967    }
968
969    /// The families this stream emits are the ones tycho-execution resolves an encoder for. A
970    /// drift between the two makes every route through a pAMM fail to encode.
971    #[test]
972    fn families_match_the_execution_side_prefixes() {
973        use tycho_execution::encoding::evm::{FALLBACK_PREFIX, PRICE_LEVEL_STREAM_PREFIX};
974
975        use super::super::config::{FALLBACK_FAMILY, PRICE_LEVEL_STREAM_FAMILY};
976
977        assert_eq!(format!("{PRICE_LEVEL_STREAM_FAMILY}:"), PRICE_LEVEL_STREAM_PREFIX);
978        assert_eq!(format!("{FALLBACK_FAMILY}:"), FALLBACK_PREFIX);
979    }
980
981    /// A registration may name either of a venue's addresses: the streamed one normalizes to the
982    /// public identity, which the registry is keyed by and the configuration then carries, so the
983    /// components of an aliased venue execute against the public identity either way.
984    #[rstest]
985    #[case::by_public_identity(KIPSELI)]
986    #[case::by_streamed_address(KIPSELI_STREAMED)]
987    fn registering_a_venue_registers_its_public_identity(#[case] registered: &str) {
988        let public = address(KIPSELI);
989
990        let builder = PriceLevelStreamBuilder::new().add_pamm(PriceLevelStreamConfig::new(
991            "custom",
992            address(registered),
993            1u64.into(),
994        ));
995
996        assert_eq!(builder.registry[&public].protocol, "custom");
997        assert_eq!(builder.registry[&public].address, public);
998        assert_eq!(builder.registry.len(), 1);
999    }
1000
1001    /// Denial is on the public identity too, so denying a venue by the address its swaps go to
1002    /// also drops the default registration of the books it streams under another address.
1003    #[rstest]
1004    #[case::by_public_identity(KIPSELI)]
1005    #[case::by_streamed_address(KIPSELI_STREAMED)]
1006    fn denying_a_venue_denies_its_public_identity(#[case] denied: &str) {
1007        let public = address(KIPSELI);
1008
1009        let builder = PriceLevelStreamBuilder::new()
1010            .with_known_pamms()
1011            .deny_pamm(address(denied));
1012
1013        assert!(!builder.registry.contains_key(&public));
1014        assert!(!builder
1015            .registry
1016            .contains_key(&address(KIPSELI_STREAMED)));
1017        assert!(builder.denied.contains(&public));
1018        // The other default venues are unaffected.
1019        assert!(builder
1020            .registry
1021            .contains_key(&address(PAMM)));
1022    }
1023
1024    /// Mixed calls naming different addresses of one venue land on one canonical key, so the
1025    /// later call still wins — as it does when both name the same address.
1026    #[rstest]
1027    #[case::add_public_then_deny_streamed(KIPSELI, KIPSELI_STREAMED)]
1028    #[case::add_streamed_then_deny_public(KIPSELI_STREAMED, KIPSELI)]
1029    fn mixed_address_forms_are_last_wins(#[case] added: &str, #[case] denied: &str) {
1030        let public = address(KIPSELI);
1031        let custom = || PriceLevelStreamConfig::new("custom", address(added), 1u64.into());
1032
1033        let builder = PriceLevelStreamBuilder::new()
1034            .add_pamm(custom())
1035            .deny_pamm(address(denied));
1036        assert!(builder.registry.is_empty());
1037        assert!(builder.denied.contains(&public));
1038
1039        let builder = PriceLevelStreamBuilder::new()
1040            .deny_pamm(address(denied))
1041            .add_pamm(custom());
1042        assert_eq!(builder.registry[&public].protocol, "custom");
1043        assert!(builder.denied.is_empty());
1044    }
1045
1046    /// Synthetic: a token that calls itself `WBTC` at an address no default binding names.
1047    const IMPOSTOR_WBTC: &str = "0x00000000000000000000000000000000deadbeef";
1048
1049    /// Sends one message with a current `ts`, of Kipseli's captured books, on the first connection
1050    /// only.
1051    /// The books arrive under the address Titan streams them by, so serving them at all also
1052    /// exercises the alias on the live path.
1053    async fn first_connection_sends_captured_books(index: usize, mut socket: FakeConnection) {
1054        if index == 0 {
1055            let message = captured_message_text("Kipseli", 100, wall_nanos_now());
1056            let _ = socket
1057                .send(Message::Text(message.into()))
1058                .await;
1059        }
1060        std::future::pending::<()>().await;
1061    }
1062
1063    /// The address Titan streams is the identity, so an impostor token calling itself `WBTC` is
1064    /// never priced and never blocks the real asset. Kipseli quoted WBTC/USDC and WETH/USDC in
1065    /// the capture, so the WETH/USDC component must survive every case.
1066    ///
1067    /// Every case admits USDC and WETH plus the `WBTC`-named tokens of its first parameter, as
1068    /// `(address, decimals)`; the second is the address expected to be priced for the book's
1069    /// WBTC leg, or `None` when the book stays unserved.
1070    #[rstest]
1071    #[case::canonical_asset(vec![(WBTC, 8)], Some(WBTC))]
1072    #[case::impostor_only(vec![(IMPOSTOR_WBTC, 18)], None)]
1073    #[case::both_present(vec![(WBTC, 8), (IMPOSTOR_WBTC, 18)], Some(WBTC))]
1074    #[tokio::test]
1075    async fn an_impostor_symbol_is_never_priced(
1076        #[case] bitcoins: Vec<(&str, u32)>,
1077        #[case] expected_btc: Option<&str>,
1078    ) {
1079        let admitted = [token(USDC, "USDC", 6), token(WETH, "WETH", 18)]
1080            .into_iter()
1081            .chain(
1082                bitcoins
1083                    .into_iter()
1084                    .map(|(address, decimals)| token(address, "WBTC", decimals)),
1085            );
1086        let fake = FakeTitan::spawn(first_connection_sends_captured_books).await;
1087        let stream = fast_timings(&fake)
1088            .add_pamm(PriceLevelStreamConfig::new("kipseli", address(KIPSELI), 1u64.into()))
1089            .with_tokens(token_map(admitted))
1090            .build();
1091        tokio::pin!(stream);
1092
1093        let update = next_within(&mut stream, WAIT)
1094            .await
1095            .expect("first update");
1096
1097        // Every served component, as the addresses of its token pair in component order.
1098        let mut served: Vec<Vec<String>> = update
1099            .new_pairs
1100            .values()
1101            .map(|component| {
1102                component
1103                    .tokens
1104                    .iter()
1105                    .map(|token| token.address.to_string())
1106                    .collect()
1107            })
1108            .collect();
1109        served.sort();
1110        let mut expected = vec![vec![USDC.to_string(), WETH.to_string()]];
1111        if let Some(btc) = expected_btc {
1112            let mut pair = vec![btc.to_string(), USDC.to_string()];
1113            pair.sort();
1114            expected.push(pair);
1115        }
1116        expected.sort();
1117        assert_eq!(served, expected);
1118        // Whichever assets were priced, the venue executes against its public identity.
1119        for component in update.new_pairs.values() {
1120            assert_eq!(component.static_attributes[PAMM_ADDRESS_ATTRIBUTE], address(KIPSELI));
1121        }
1122    }
1123
1124    /// Traffic that is not a parsed message keeps neither the socket nor the component alive: the
1125    /// idle timeout reconnects, every reconnect resends the same message, which refreshes the
1126    /// component but cannot move its deadline, and the removal follows.
1127    #[rstest]
1128    #[case::ping_only(Message::Ping(Vec::new().into()))]
1129    #[case::malformed_text(Message::Text("nonsense".into()))]
1130    #[tokio::test]
1131    async fn traffic_that_is_not_a_message_removes_within_stale_after(#[case] filler: Message) {
1132        let fake = FakeTitan::spawn(message_then_repeat(
1133            fresh_message(),
1134            filler,
1135            Duration::from_millis(10),
1136        ))
1137        .await;
1138        let stream = fast_builder(&fake).build();
1139        tokio::pin!(stream);
1140
1141        expect_first_update(&mut stream).await;
1142        let removal = expect_removal(&mut stream).await;
1143
1144        assert_removal_only(&removal, 1);
1145        assert!(fake.connections.load(Ordering::SeqCst) >= 2, "no reconnect on idle timeout");
1146    }
1147
1148    /// How long a two-socket test waits for the quote stream socket to have been read before it
1149    /// sends the price level message whose books name its entries. Both sockets connect on the
1150    /// first poll, so this orders the two messages rather than racing them.
1151    const SOCKET_HEAD_START: Duration = Duration::from_millis(250);
1152
1153    /// A builder serving the known venues, which are every venue of the 2026-10-16 captures,
1154    /// reading both captures from `prices` and `overrides`. Registered up front rather than
1155    /// auto-detected, because entries are only stored for venues already served when they arrive.
1156    fn capture_builder(prices: &FakeTitan, overrides: &FakeTitan) -> PriceLevelStreamBuilder {
1157        two_socket_timings(prices, overrides)
1158            .with_known_pamms()
1159            .with_tokens(captured_tokens())
1160    }
1161
1162    /// The price level capture whose books match the captured quote stream message, with its `ts`
1163    /// set to now so that it passes the freshness checks.
1164    fn matching_message() -> Message {
1165        Message::Text(matching_message_text(100, wall_nanos_now()).into())
1166    }
1167
1168    /// The captured quote stream message, verbatim: matching reads no clock, so its timestamps can
1169    /// stay as captured.
1170    fn quote_stream_message() -> Message {
1171        Message::Text(captured_quote_stream_text().into())
1172    }
1173
1174    /// A [`FakeTitan`] handler that sends `message` after `delay` on the first connection only,
1175    /// then holds the socket open: a message the stream must see exactly once, at a known point
1176    /// relative to the other socket's.
1177    fn send_once_after(
1178        delay: Duration,
1179        message: Message,
1180    ) -> impl Fn(usize, FakeConnection) -> BoxFuture<'static, ()> + Send + Sync + 'static {
1181        move |index, mut socket| {
1182            let message = message.clone();
1183            Box::pin(async move {
1184                if index == 0 {
1185                    tokio::time::sleep(delay).await;
1186                    let _ = socket.send(message).await;
1187                }
1188                std::future::pending::<()>().await;
1189            })
1190        }
1191    }
1192
1193    /// How many connections `fake` has accepted.
1194    fn connections(fake: &FakeTitan) -> usize {
1195        fake.connections.load(Ordering::SeqCst)
1196    }
1197
1198    /// The states of `update` that carry simulation overrides.
1199    fn states_with_overrides(update: &Update) -> Vec<&PriceLevelStreamState> {
1200        update
1201            .states
1202            .values()
1203            .map(|state| {
1204                state
1205                    .as_any()
1206                    .downcast_ref::<PriceLevelStreamState>()
1207                    .expect("price level state")
1208            })
1209            .filter(|state| state.simulation_overrides().is_some())
1210            .collect()
1211    }
1212
1213    /// Both captures, read from two local sockets with the quote stream message arriving first:
1214    /// the 27 served pairs whose venue's entry carries the pair in the book's slot come out with
1215    /// overrides, and the other 3 without. The count comes from the fixtures themselves, worked
1216    /// out independently of this code.
1217    #[tokio::test]
1218    async fn a_quote_stream_message_attaches_overrides_to_the_books_it_priced() {
1219        let overrides =
1220            FakeTitan::spawn(send_once_after(Duration::ZERO, quote_stream_message())).await;
1221        let prices = FakeTitan::spawn(send_once_after(SOCKET_HEAD_START, matching_message())).await;
1222        let stream = capture_builder(&prices, &overrides)
1223            .read_idle_timeout(Duration::from_secs(5))
1224            .build();
1225        tokio::pin!(stream);
1226
1227        let update = next_within(&mut stream, WAIT)
1228            .await
1229            .expect("first update");
1230
1231        assert_eq!(update.states.len(), 30);
1232        let with_overrides = states_with_overrides(&update);
1233        assert_eq!(with_overrides.len(), 27);
1234        for state in with_overrides {
1235            let overrides = state
1236                .simulation_overrides()
1237                .expect("overrides");
1238            assert!(!overrides.accounts.is_empty(), "overrides with no account");
1239            assert_eq!(overrides.block_number, Some(26_201_954));
1240        }
1241    }
1242
1243    /// An entry that arrives after the books it priced changes nothing already served: the update
1244    /// the consumer holds keeps the states it was given. The next message with the same books gets
1245    /// overrides, which shows the first one's lack of them is about ordering, not matching.
1246    #[tokio::test]
1247    async fn a_late_quote_stream_message_leaves_served_states_without_overrides() {
1248        let overrides =
1249            FakeTitan::spawn(send_once_after(SOCKET_HEAD_START, quote_stream_message())).await;
1250        let prices = FakeTitan::spawn(move |index, mut socket| async move {
1251            if index == 0 {
1252                let _ = socket.send(matching_message()).await;
1253                tokio::time::sleep(2 * SOCKET_HEAD_START).await;
1254                let _ = socket.send(matching_message()).await;
1255            }
1256            std::future::pending::<()>().await;
1257        })
1258        .await;
1259        let stream = capture_builder(&prices, &overrides)
1260            .read_idle_timeout(Duration::from_secs(5))
1261            .build();
1262        tokio::pin!(stream);
1263
1264        let first = next_within(&mut stream, WAIT).await;
1265        let second = next_within(&mut stream, WAIT).await;
1266
1267        let first = first.expect("first update");
1268        assert_eq!(first.new_pairs.len(), 30);
1269        assert!(states_with_overrides(&first).is_empty(), "overrides before any entry arrived");
1270        assert_eq!(states_with_overrides(&second.expect("second update")).len(), 27);
1271    }
1272
1273    /// Opting out connects no quote stream socket at all, so no state gets overrides and nothing
1274    /// is read from an endpoint the consumer never wanted, whichever of the opt-out and
1275    /// `quote_stream_endpoint` is called first.
1276    #[rstest]
1277    #[case::opt_out_after_the_endpoint(false)]
1278    #[case::opt_out_before_the_endpoint(true)]
1279    #[tokio::test]
1280    async fn without_simulation_overrides_connects_no_quote_stream_socket(
1281        #[case] opt_out_first: bool,
1282    ) {
1283        let overrides =
1284            FakeTitan::spawn(send_once_after(Duration::ZERO, quote_stream_message())).await;
1285        let prices = FakeTitan::spawn(send_once_after(SOCKET_HEAD_START, matching_message())).await;
1286        let builder = if opt_out_first {
1287            timings(&prices)
1288                .without_simulation_overrides()
1289                .quote_stream_endpoint(overrides.url())
1290        } else {
1291            timings(&prices)
1292                .quote_stream_endpoint(overrides.url())
1293                .without_simulation_overrides()
1294        };
1295        let stream = builder
1296            .with_known_pamms()
1297            .with_tokens(captured_tokens())
1298            .read_idle_timeout(Duration::from_secs(5))
1299            .build();
1300        tokio::pin!(stream);
1301
1302        let update = next_within(&mut stream, WAIT)
1303            .await
1304            .expect("first update");
1305
1306        assert_eq!(update.states.len(), 30);
1307        assert!(states_with_overrides(&update).is_empty());
1308        assert_eq!(connections(&overrides), 0, "connected the quote stream socket");
1309    }
1310
1311    /// Whatever the quote stream socket does, the price level socket keeps serving: the states lose
1312    /// their overrides and nothing else. A silent socket reconnects on the idle timeout, malformed
1313    /// text is skipped and then reconnected for the same reason, a close is reconnected, and a
1314    /// flood is read without starving the other two arms of the select. In every case the first
1315    /// update arrives and the stale removal that follows the price level socket's silence still
1316    /// fires on time.
1317    ///
1318    /// `connects` is the least number of connections the quote stream socket must have made by
1319    /// then: 2 for the cases that test recovery, so a socket that gives up after one failure fails
1320    /// them; 1 for the flood, whose messages parse and keep the connection alive.
1321    #[rstest]
1322    #[case::silent(None, 2)]
1323    #[case::malformed(Some(Message::Text("nonsense".into())), 2)]
1324    #[case::closed(Some(Message::Close(None)), 2)]
1325    #[case::flooding(Some(quote_stream_message()), 1)]
1326    #[tokio::test]
1327    async fn the_price_level_socket_survives_any_quote_stream_socket(
1328        #[case] repeat: Option<Message>,
1329        #[case] connects: usize,
1330    ) {
1331        let overrides = FakeTitan::spawn(move |_, mut socket| {
1332            let repeat = repeat.clone();
1333            async move {
1334                while let Some(message) = repeat.clone() {
1335                    if socket.send(message).await.is_err() {
1336                        return;
1337                    }
1338                    tokio::time::sleep(Duration::from_millis(5)).await;
1339                }
1340                std::future::pending::<()>().await;
1341            }
1342        })
1343        .await;
1344        let prices = FakeTitan::spawn(first_connection_sends_one_message).await;
1345        let stream = two_socket_timings(&prices, &overrides)
1346            .add_pamm(fermiswap())
1347            .with_tokens(tokens())
1348            .build();
1349        tokio::pin!(stream);
1350
1351        expect_first_update(&mut stream).await;
1352        let removal = expect_removal(&mut stream).await;
1353
1354        assert_removal_only(&removal, 1);
1355        assert!(
1356            connections(&overrides) >= connects,
1357            "the quote stream socket connected {} times, expected at least {connects}",
1358            connections(&overrides)
1359        );
1360    }
1361
1362    /// A refused quote stream socket does not stall the price level socket, which serves and
1363    /// expires on its own schedule.
1364    #[tokio::test]
1365    async fn a_refused_quote_stream_socket_does_not_stall_the_price_level_socket() {
1366        let mut overrides =
1367            FakeTitan::spawn(send_once_after(Duration::ZERO, quote_stream_message())).await;
1368        let url = overrides.url();
1369        overrides.shutdown();
1370        let prices = FakeTitan::spawn(first_connection_sends_one_message).await;
1371        let stream = timings(&prices)
1372            .quote_stream_endpoint(url)
1373            .add_pamm(fermiswap())
1374            .with_tokens(tokens())
1375            .build();
1376        tokio::pin!(stream);
1377
1378        expect_first_update(&mut stream).await;
1379        let removal = expect_removal(&mut stream).await;
1380
1381        assert_removal_only(&removal, 1);
1382    }
1383
1384    /// A quote stream socket whose handshake never completes does not stall the price level
1385    /// socket, which serves and expires on its own schedule. The quote stream socket's connect
1386    /// timeout fires and it is retried; without the timeout the stream would wait on the hung
1387    /// handshake for as long as the peer keeps the connection open.
1388    #[tokio::test]
1389    async fn a_hung_quote_stream_socket_does_not_stall_the_price_level_socket() {
1390        let overrides = FakeTitan::spawn_hung().await;
1391        let prices = FakeTitan::spawn(first_connection_sends_one_message).await;
1392        let stream = two_socket_timings(&prices, &overrides)
1393            .connect_timeout(Duration::from_millis(200))
1394            .add_pamm(fermiswap())
1395            .with_tokens(tokens())
1396            .build();
1397        tokio::pin!(stream);
1398
1399        expect_first_update(&mut stream).await;
1400        let removal = expect_removal(&mut stream).await;
1401
1402        assert_removal_only(&removal, 1);
1403        assert!(
1404            connections(&overrides) >= 2,
1405            "the hung socket was connected to {} times, expected a retry",
1406            connections(&overrides)
1407        );
1408    }
1409
1410    /// Isolation holds the other way too: with the price level socket refused the stream yields
1411    /// nothing, and the quote stream socket is still read. The captured quote stream message names
1412    /// venues this stream does not serve, which it counts as it reads them.
1413    #[test]
1414    fn a_refused_price_level_socket_does_not_stall_the_quote_stream_socket() {
1415        let (update, snapshot) = record_async(async {
1416            let overrides = FakeTitan::spawn(message_then_repeat(
1417                quote_stream_message(),
1418                quote_stream_message(),
1419                Duration::from_millis(20),
1420            ))
1421            .await;
1422            let mut prices = FakeTitan::spawn(first_connection_sends_one_message).await;
1423            prices.shutdown();
1424            let stream = two_socket_timings(&prices, &overrides)
1425                .add_pamm(fermiswap())
1426                .with_tokens(tokens())
1427                .build();
1428            tokio::pin!(stream);
1429            next_within(&mut stream, Duration::from_millis(500)).await
1430        });
1431
1432        assert!(update.is_none(), "an update without a price level message");
1433        assert!(
1434            counter_value(&snapshot, "price_level_stream_unserved_venue_entries_total", &[]) > 0,
1435            "the quote stream socket was never read"
1436        );
1437    }
1438
1439    /// Dropping the stream closes both sockets, and neither is reconnected afterwards.
1440    #[tokio::test]
1441    async fn drop_closes_both_sockets() {
1442        let closed = Arc::new(AtomicUsize::new(0));
1443        let spawn_watching = |closed: Arc<AtomicUsize>, first: Message| async move {
1444            FakeTitan::spawn(move |_, mut socket| {
1445                let closed = closed.clone();
1446                let first = first.clone();
1447                async move {
1448                    let _ = socket.send(first).await;
1449                    while let Some(Ok(message)) = socket.next().await {
1450                        if matches!(message, Message::Close(_)) {
1451                            break;
1452                        }
1453                    }
1454                    closed.fetch_add(1, Ordering::SeqCst);
1455                }
1456            })
1457            .await
1458        };
1459        let overrides = spawn_watching(closed.clone(), quote_stream_message()).await;
1460        let prices = spawn_watching(closed.clone(), matching_message()).await;
1461        let mut stream = Box::pin(
1462            capture_builder(&prices, &overrides)
1463                .read_idle_timeout(Duration::from_secs(5))
1464                .build(),
1465        );
1466        next_within(&mut stream.as_mut(), WAIT)
1467            .await
1468            .expect("first update");
1469
1470        drop(stream);
1471        tokio::time::sleep(Duration::from_millis(200)).await;
1472
1473        assert_eq!(closed.load(Ordering::SeqCst), 2, "a socket was not closed on drop");
1474        assert_eq!(connections(&prices), 1, "reconnected after drop");
1475        assert_eq!(connections(&overrides), 1, "reconnected after drop");
1476    }
1477}