af_price_updates/lib.rs
1//! Price update formats and supporting helpers for the Aftermath, Pyth, Pyth Lazer, and Stork
2//! oracles.
3//!
4//! This crate owns the data-format layer shared by price-update producers, relays, APIs, and
5//! consumers. It provides:
6//!
7//! - validated wrappers and parsers for binary price-update payloads;
8//! - a single-allocation serializer for Aftermath updates that leaves signing to the caller;
9//! - serializable price-update data structures where the upstream format is structured data;
10//! - feed ID types and canonical feed ID sets;
11//! - reusable sorted/deduplicated collections, process-lifetime interners, and their backing arena.
12//!
13//! Each oracle has its own module:
14//!
15//! - [`aftermath`] handles signed Aftermath updates accepted by the on-chain `af_verifier` package.
16//! - [`pyth`] handles Pyth accumulator (`PNAU`) updates served by Hermes.
17//! - [`pyth_lazer`] handles Pyth Lazer `leEcdsa` updates and binary WebSocket answers.
18//! - [`stork`] models Stork signed price updates and their BCS representation.
19//!
20//! # Validation and signing boundary
21//!
22//! Constructors validate the structure of encoded updates. They do not authenticate the source or
23//! verify embedded signatures; signed payloads are intended to be relayed to the corresponding
24//! on-chain verifier. This crate also does not create signatures—signing belongs to the service
25//! that owns the relevant key. [`aftermath::UnsignedPriceUpdates::new`] serializes an Aftermath
26//! update into the caller's buffer and exposes the digest to sign; the caller appends a signature
27//! made with any secp256k1 implementation. [`keccak::keccak256`] computes the same kind of digest
28//! for other payloads, e.g. to verify Pyth Lazer signatures. Aftermath, Pyth, and Pyth Lazer
29//! wrappers retain their original bytes and expose parsed feed views on demand.
30//!
31//! # Feed ID sets and helpers
32//!
33//! Oracle-specific `PriceFeedIdSet` types store trusted, bounded ID sets in process-lifetime
34//! interners. Equal sets share one canonical slice, making the sets [`Copy`] with constant-time
35//! equality and hashing. Dynamic input must first be sorted and deduplicated into a
36//! [`sorted_set::SortedSet`], then passed to the oracle-specific `PriceFeedIdSet::intern` method.
37//! Fixed-size arrays also implement `From`, for bounded, program-controlled values such as array
38//! literals.
39//!
40//! [`sorted_set::SortedSet`] is the general owned, `Vec`-backed alternative,
41//! [`sorted_set::SortedSetSlice`] is its zero-allocation borrowed view, and
42//! [`sorted_set::ArcSortedSet`] provides cheap clones without process-lifetime storage. The
43//! [`slice_interner`] and [`str_interner`] modules expose the lower-level canonicalization helpers,
44//! backed by [`leaking_arena`].
45//!
46//! Interned values are never freed, so memory grows with every distinct value. Do not intern
47//! untrusted input, such as feed ID sets requested by clients or arbitrary Stork asset IDs; keep
48//! such input in a [`sorted_set::SortedSet`] or another reclaimable owner, or validate it first.
49//! Intern Stork IDs explicitly with [`stork::SmallAssetId::intern`]. The `find` methods of the
50//! interned types, such as [`aftermath::PriceFeedIdSet::find`], look up existing values without
51//! interning. `find`, `is_superset`, and `matches_set` take borrowed
52//! [`sorted_set::SortedSetSlice`] references, obtained from a `SortedSet` or checked from a plain
53//! slice with [`sorted_set::SortedSetSlice::try_from_slice`].
54//!
55//! ```
56//! use af_price_updates::{
57//! aftermath::{PriceFeedId, PriceFeedIdSet},
58//! sorted_set::SortedSet,
59//! };
60//!
61//! let ids = SortedSet::from(vec![PriceFeedId(7), PriceFeedId(3), PriceFeedId(7)]);
62//! assert_eq!(ids.as_slice(), &[PriceFeedId(3), PriceFeedId(7)]);
63//! let interned = PriceFeedIdSet::intern(&ids);
64//! assert_eq!(interned.as_slice(), ids.as_slice());
65//! ```
66
67#![warn(missing_docs)]
68
69pub mod aftermath;
70mod aftermath_id_set;
71mod aftermath_writer;
72pub mod keccak;
73pub mod leaking_arena;
74mod parse_error;
75pub mod pyth;
76mod pyth_id_set;
77pub mod pyth_lazer;
78mod pyth_lazer_id_set;
79mod reader;
80pub mod slice_interner;
81pub mod sorted_set;
82pub mod stork;
83mod stork_small_id;
84pub mod str_interner;
85mod u256;
86
87pub use self::parse_error::ParseError;
88pub use self::u256::U256;