From 366183d715bbe45cffee5772fde6079d54fd31db Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:39:06 -0600 Subject: [PATCH 1/9] feat(sdk): verify quorum proof chains one link at a time `bootstrap::start` decodes an envelope and makes every check before the first link; `Verification::verify_link` verifies one link, and `finish` verifies any left, then the target and the records. An async caller can yield between links instead of spending a long chain's BLS work in one poll. `verify` is now `start` followed by `finish`, so both paths make the same checks in the same order. Co-Authored-By: Claude Opus 5.5 --- packages/rs-core-proof/src/bootstrap.rs | 66 ++++++++++++++- packages/rs-core-proof/src/chain.rs | 87 ++++++++++++++++---- packages/rs-core-proof/tests/synthetic.rs | 36 ++++++++ packages/rs-core-proof/tests/verification.rs | 66 +++++++++++++++ 4 files changed, 233 insertions(+), 22 deletions(-) diff --git a/packages/rs-core-proof/src/bootstrap.rs b/packages/rs-core-proof/src/bootstrap.rs index ae6c5ebb7eb..d0ef975e699 100644 --- a/packages/rs-core-proof/src/bootstrap.rs +++ b/packages/rs-core-proof/src/bootstrap.rs @@ -1,6 +1,6 @@ //! The bootstrap envelope: a proof chain plus Merkle openings of records under //! the target's roots, as `getquorumproofchain` returns in `bootstrap_hex`. -use crate::chain::{QuorumProofChain, MAX_PROOF_BYTES}; +use crate::chain::{Progress, QuorumProofChain, MAX_PROOF_BYTES}; use crate::merkle::MerklePath; use crate::state::State; use crate::wire::{sha256d, Reader, Result}; @@ -60,16 +60,74 @@ pub fn verify<'a>( trusted: &State, minimum_height: u32, ) -> Result> { + start(bytes, trusted, minimum_height)?.finish() +} + +/// Begin verifying an envelope as [`verify`] does, so the caller can verify +/// its chain's links one at a time with [`Verification::verify_link`] and do +/// other work between them. Each link costs a BLS verification and the X11 +/// hashes of its ancestor headers. +/// +/// Decodes the envelope and makes every check that comes before the first +/// link; [`Verification::finish`] makes the rest. Verifying links one at a +/// time accepts and rejects exactly what [`verify`] does. +pub fn start<'a>( + bytes: &'a [u8], + trusted: &State, + minimum_height: u32, +) -> Result> { if bytes.len() > MAX_PROOF_BYTES { return Err(Error("bootstrap size limit")); } - let mut r = Reader::new(bytes); - let chain = QuorumProofChain::decode(r.blob(MAX_PROOF_BYTES)?)?; + let mut records = Reader::new(bytes); + let chain = QuorumProofChain::decode(records.blob(MAX_PROOF_BYTES)?)?; // Decided before any signature work: the height alone makes it useless. if chain.target.height < minimum_height { return Err(Error::STALE_TARGET); } - let state = chain.verify(trusted)?; + let progress = chain.start(trusted)?; + Ok(Verification { + chain, + trusted: trusted.clone(), + progress, + records, + }) +} + +/// An envelope part-way through verification. Only [`start`] constructs it. +pub struct Verification<'a> { + chain: QuorumProofChain, + trusted: State, + progress: Progress, + /// The envelope after the chain: its records. + records: Reader<'a>, +} + +impl<'a> Verification<'a> { + /// How many of the chain's links are still to verify. + pub fn links_left(&self) -> usize { + self.progress.links_left(&self.chain) + } + + /// Verify the next link of the chain. An error is final: the envelope is + /// invalid. + pub fn verify_link(&mut self) -> Result<()> { + self.chain.verify_link(&mut self.progress) + } + + /// Verify any links left, then the target and the records, and return + /// the verified envelope. + pub fn finish(mut self) -> Result> { + while self.links_left() > 0 { + self.verify_link()?; + } + let state = self.chain.finish(self.progress, &self.trusted)?; + open_records(self.records, state) + } +} + +/// Open the records after the chain under the verified target `state`. +fn open_records(mut r: Reader<'_>, state: State) -> Result> { let count = r.u8()?; if !(1..=MAX_RECORDS).contains(&count) { return Err(Error("projection count")); diff --git a/packages/rs-core-proof/src/chain.rs b/packages/rs-core-proof/src/chain.rs index 3b7ed0618a7..26575ee2891 100644 --- a/packages/rs-core-proof/src/chain.rs +++ b/packages/rs-core-proof/src/chain.rs @@ -156,6 +156,32 @@ impl Link { } } +/// How far [`QuorumProofChain::verify`] has got: the links verified so far +/// and the ChainLock quorum the last of them mined. +#[derive(Debug)] +pub(crate) struct Progress { + chainlock_type: u8, + signer: Commitment, + height: u32, + next_link: usize, +} + +impl Progress { + /// How many links are still to verify. + pub(crate) fn links_left(&self, chain: &QuorumProofChain) -> usize { + chain.links.len().saturating_sub(self.next_link) + } +} + +/// `commitment`, if it is of the network's ChainLock quorum type. +fn ensure_chainlock(commitment: Commitment, chainlock_type: u8) -> Result { + if commitment.quorum_type == chainlock_type { + Ok(commitment) + } else { + Err(Error("unexpected quorum type")) + } +} + /// A `DASHNC02` proof chain, as `getquorumproofchain` returns in `proof_hex`. #[derive(Clone, Debug, PartialEq, Eq)] pub struct QuorumProofChain { @@ -179,6 +205,16 @@ impl QuorumProofChain { /// The chain must start from exactly `trusted`; a relay cannot choose the /// checkpoint. pub fn verify(&self, trusted: &State) -> Result { + let mut progress = self.start(trusted)?; + while progress.next_link < self.links.len() { + self.verify_link(&mut progress)?; + } + self.finish(progress, trusted) + } + + /// The checks before any link: the anchor, the chain's size limits and + /// the seed's opening under the checkpoint's quorum root. + pub(crate) fn start(&self, trusted: &State) -> Result { if &self.anchor != trusted { return Err(Error::UNTRUSTED_CHECKPOINT); } @@ -191,27 +227,42 @@ impl QuorumProofChain { return Err(Error("ancestor budget/height")); } let chainlock_type = trusted.network.chainlock_quorum_type(); - let ensure_chainlock = |commitment: Commitment| { - if commitment.quorum_type == chainlock_type { - Ok(commitment) - } else { - Err(Error("unexpected quorum type")) - } - }; - - let mut signer = ensure_chainlock(parse_commitment(&self.seed)?)?; + let signer = ensure_chainlock(parse_commitment(&self.seed)?, chainlock_type)?; self.seed_path .verify(sha256d(&self.seed), &trusted.quorum_root)?; - let mut height = trusted.height; - for link in &self.links { - link.certificate.verify(&signer, height)?; - let next = ensure_chainlock(link.mined_commitment()?)?; - if next.quorum_hash == signer.quorum_hash { - return Err(Error("redundant signer")); - } - signer = next; - height = link.certificate.height; + Ok(Progress { + chainlock_type, + signer, + height: trusted.height, + next_link: 0, + }) + } + + /// Verify the next link: its certificate by the current signer, and the + /// ChainLock quorum it mines, which signs from then on. + pub(crate) fn verify_link(&self, progress: &mut Progress) -> Result<()> { + let link = self + .links + .get(progress.next_link) + .ok_or(Error("no link left"))?; + link.certificate.verify(&progress.signer, progress.height)?; + let next = ensure_chainlock(link.mined_commitment()?, progress.chainlock_type)?; + if next.quorum_hash == progress.signer.quorum_hash { + return Err(Error("redundant signer")); + } + progress.signer = next; + progress.height = link.certificate.height; + progress.next_link += 1; + Ok(()) + } + + /// Verify the target and its coinbase once every link has verified, and + /// return the target block's state. + pub(crate) fn finish(&self, progress: Progress, trusted: &State) -> Result { + if progress.next_link != self.links.len() { + return Err(Error("unverified links")); } + let Progress { signer, height, .. } = progress; let block_hash = self.target.verify(&signer, height)?; if self.coinbase.path.index != 0 { return Err(Error("coinbase inclusion")); diff --git a/packages/rs-core-proof/tests/synthetic.rs b/packages/rs-core-proof/tests/synthetic.rs index c65d7d657dd..67ce99b470b 100644 --- a/packages/rs-core-proof/tests/synthetic.rs +++ b/packages/rs-core-proof/tests/synthetic.rs @@ -554,3 +554,39 @@ fn should_reject_commitments_core_rejects() { ); } } + +/// A relay may send a valid chain through every ChainLock quorum since the +/// checkpoint. A caller verifying it link by link sees each link verify in +/// turn, and a bad link fails exactly when its turn comes. +#[test] +fn should_verify_a_long_chain_one_link_at_a_time() { + let quorums: Vec = (2..42).map(|seed| Quorum::new(seed, CHAINLOCK)).collect(); + let next: Vec<&Quorum> = quorums.iter().collect(); + let seed = Quorum::new(1, CHAINLOCK); + let built = build(&seed, &next, 4); + let bytes = envelope(&built.chain, 1, &[(0, &built.record)]); + + let mut verification = bootstrap::start(&bytes, &built.checkpoint, 0).unwrap(); + assert_eq!(verification.links_left(), 40); + for left in (0..40).rev() { + verification.verify_link().unwrap(); + assert_eq!(verification.links_left(), left); + } + let verified = verification.finish().unwrap(); + assert_eq!(verified.state().height, built.chain.target.height); + + // Link 20 is signed by the wrong quorum. + let mut tampered = built.chain.clone(); + tampered.links[20] = link( + &seed, + &quorums[20].commitment(), + tampered.links[20].certificate.height, + ); + let bytes = envelope(&tampered, 1, &[(0, &built.record)]); + let mut verification = bootstrap::start(&bytes, &built.checkpoint, 0).unwrap(); + for _ in 0..20 { + verification.verify_link().unwrap(); + } + assert!(verification.verify_link().is_err()); + assert!(bootstrap::verify(&bytes, &built.checkpoint, 0).is_err()); +} diff --git a/packages/rs-core-proof/tests/verification.rs b/packages/rs-core-proof/tests/verification.rs index 4c12a129954..0a805134bd0 100644 --- a/packages/rs-core-proof/tests/verification.rs +++ b/packages/rs-core-proof/tests/verification.rs @@ -214,3 +214,69 @@ fn should_verify_a_proof_signed_by_the_checkpoint_quorum_itself() { let verified = bootstrap::verify(&f.bootstrap, &f.checkpoint, f.target.height).unwrap(); assert_eq!(verified.state(), &f.target); } + +/// Verify one link at a time, as an async caller does between yields. +fn verify_stepwise<'a>( + bytes: &'a [u8], + trusted: &State, + minimum_height: u32, +) -> dash_core_proof::Result> { + let mut verification = bootstrap::start(bytes, trusted, minimum_height)?; + while verification.links_left() > 0 { + let left = verification.links_left(); + verification.verify_link()?; + assert_eq!(verification.links_left(), left - 1); + } + verification.finish() +} + +#[test] +fn should_verify_link_by_link_exactly_as_in_one_call() { + for (f, links) in [(mainnet(), 2), (testnet(), 4)] { + let started = bootstrap::start(&f.bootstrap, &f.checkpoint, 0).unwrap(); + assert_eq!(started.links_left(), links); + let whole = bootstrap::verify(&f.bootstrap, &f.checkpoint, 0).unwrap(); + let stepwise = verify_stepwise(&f.bootstrap, &f.checkpoint, 0).unwrap(); + assert_eq!(stepwise.state(), whole.state()); + assert_eq!(stepwise.records(), whole.records()); + // `finish` verifies whatever links are left. + let finished = bootstrap::start(&f.bootstrap, &f.checkpoint, 0) + .unwrap() + .finish() + .unwrap(); + assert_eq!(finished.state(), whole.state()); + } +} + +#[test] +fn should_reject_link_by_link_whatever_one_call_rejects() { + let f = testnet(); + let stale = verify_stepwise(&f.bootstrap, &f.checkpoint, f.target.height + 1).unwrap_err(); + assert_eq!( + stale.kind(), + bootstrap::verify(&f.bootstrap, &f.checkpoint, f.target.height + 1) + .unwrap_err() + .kind() + ); + for i in (0..f.bootstrap.len()).step_by(61) { + let mut corrupt = f.bootstrap.clone(); + corrupt[i] ^= 0x01; + assert_eq!( + format!( + "{:?}", + verify_stepwise(&corrupt, &f.checkpoint, 0).map(|v| v.state().clone()) + ), + format!( + "{:?}", + bootstrap::verify(&corrupt, &f.checkpoint, 0).map(|v| v.state().clone()) + ), + "flip at {i}" + ); + } + for n in (0..f.bootstrap.len()).step_by(97) { + assert!( + verify_stepwise(&f.bootstrap[..n], &f.checkpoint, 0).is_err(), + "cut at {n}" + ); + } +} From 07db1ee23cc3b9b58d5c781ae3fa6b076fdf03d9 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:39:06 -0600 Subject: [PATCH 2/9] fix(sdk): verify quorum proofs without blocking the executor A relay can answer a proof request with a valid chain through every ChainLock quorum since the state it was asked from: about 960 links on testnet today, 4 s of BLS work natively and more in a browser. The provider verified it in one poll, stalling a Tokio worker (every task on a current-thread runtime, including the fetch's own deadline) or the browser's event loop. The fetch now verifies one link at a time and yields to the executor after every 10 ms of verification: Tokio's `yield_now`, which runs due timers first, natively, and a zero-delay browser timer in wasm, so the event loop's timers, input and rendering run in between. Dropping the fetch stops verification at the next yield, so abandoned fetches leave no work behind. Fixes #5706 Co-Authored-By: Claude Opus 5.5 --- .../Cargo.toml | 5 +- .../src/proof_fetches.rs | 164 +++++++++++++++++- .../src/verified_provider.rs | 6 + 3 files changed, 168 insertions(+), 7 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/Cargo.toml b/packages/rs-sdk-trusted-context-provider/Cargo.toml index 443651c9d15..89bde6a4f54 100644 --- a/packages/rs-sdk-trusted-context-provider/Cargo.toml +++ b/packages/rs-sdk-trusted-context-provider/Cargo.toml @@ -86,8 +86,9 @@ web-sys = { version = "0.3.85", features = [ ] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -# Waits out the gap between two refreshes of the quorum lists. -tokio = { version = "1.40", features = ["time"] } +# Waits out the gap between two refreshes of the quorum lists, and yields to +# the runtime while a long proof is verified. +tokio = { version = "1.40", features = ["rt", "time"] } [target.'cfg(not(target_os = "android"))'.dependencies] reqwest = { version = "0.12", features = ["json"] } diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 0136484e2a9..660c32cd150 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -40,8 +40,8 @@ use crate::quorum_refresh::sleep; #[cfg(not(target_arch = "wasm32"))] use crate::quorum_refresh::sleep as caller_sleep; use dash_context_provider::ContextProviderError; -use dash_core_proof::bootstrap::{self, RecordKind}; -use dash_core_proof::{parse_commitment, rpc_hex, ErrorKind, State}; +use dash_core_proof::bootstrap::{self, RecordKind, VerifiedBootstrap}; +use dash_core_proof::{parse_commitment, rpc_hex, Error as ProofError, ErrorKind, State}; #[cfg(target_arch = "wasm32")] use futures::channel::oneshot; use futures::future::{self, select, BoxFuture, Either, FutureExt, Shared, WeakShared}; @@ -132,6 +132,10 @@ pub(crate) struct Timing { pub(crate) source_cooldown: Duration, /// Longest such wait. pub(crate) max_source_cooldown: Duration, + /// How long a proof is verified before the fetch yields to the + /// executor; verification resumes at the next link once other tasks + /// and timers have run. + pub(crate) verify_slice: Duration, } impl Default for Timing { @@ -145,6 +149,10 @@ impl Default for Timing { max_quorum_cooldown: Duration::from_secs(60), source_cooldown: Duration::from_secs(10), max_source_cooldown: Duration::from_secs(120), + // Well under the 50 ms at which a browser counts a task as long. + // A link costs about 4 ms natively, so an honest proof of a few + // links is verified without yielding. + verify_slice: Duration::from_millis(10), } } } @@ -880,7 +888,7 @@ impl ProofFetches { failures.push(note(&reason)); continue; } - reply => self.judge(quorum, height, &ask, reply), + reply => self.judge(quorum, height, &ask, reply).await, }; match verdict { Verdict::Proven(proven) => { @@ -958,7 +966,15 @@ impl ProofFetches { /// A valid proof whose target, together with the earlier proofs, does not /// serve `height` is still published, as evidence for later asks, but does /// not prove the key for `height`. - fn judge( + /// + /// The proof is verified one chain link at a time, yielding to the + /// executor after each [`Timing::verify_slice`]. A relay can send a valid + /// chain through every ChainLock quorum since the state it was asked from, + /// about a thousand links on testnet, seconds of BLS work. Yielding lets + /// other tasks and timers, including this fetch's deadline and the + /// browser's event loop, run meanwhile. If every caller drops the fetch, + /// verification stops at the next yield. + async fn judge( &self, quorum: QuorumId, height: u32, @@ -989,7 +1005,7 @@ impl ProofFetches { 0 => height.saturating_sub(DKG_INTERVAL_BLOCKS), asked => asked, }; - let verified = match bootstrap::verify(body, &ask.checkpoint, needed) { + let verified = match self.verify(body, &ask.checkpoint, needed).await { Ok(verified) => verified, Err(error) if error.kind() == ErrorKind::StaleTarget && ask.height == 0 => { return Verdict::BeyondTip(format!( @@ -1036,6 +1052,26 @@ impl ProofFetches { } } + /// `bootstrap::verify`, one link at a time, yielding to the executor + /// whenever a slice of verification has passed. + async fn verify<'a>( + &self, + body: &'a [u8], + checkpoint: &State, + minimum_height: u32, + ) -> Result, ProofError> { + let mut verification = bootstrap::start(body, checkpoint, minimum_height)?; + let mut slice_started = Instant::now(); + while verification.links_left() > 0 { + verification.verify_link()?; + if slice_started.elapsed() >= self.timing.verify_slice { + yield_to_executor().await; + slice_started = Instant::now(); + } + } + verification.finish() + } + /// What to ask a source next for this quorum, given what the cache holds /// for it now. fn next_ask(&self, quorum: QuorumId, height: u32, alternate: bool) -> NextAsk { @@ -1263,6 +1299,22 @@ where /// Wait for `duration` in a `Send` future, for callers outside the local /// executor. The browser timer runs in a local task that ends, clearing the /// timer, as soon as this future is dropped. +/// Let the executor run other tasks, and its timers, before this one goes on. +#[cfg(not(target_arch = "wasm32"))] +async fn yield_to_executor() { + // Tokio's yield polls the timer driver before resuming the task, so even + // on a current-thread runtime a due timer fires first. + tokio::task::yield_now().await; +} + +/// Let the browser's event loop run, with its timers, input and rendering, +/// before this task goes on. A timer, not a microtask: microtasks run before +/// the event loop moves on. +#[cfg(target_arch = "wasm32")] +async fn yield_to_executor() { + sleep(Duration::ZERO).await; +} + #[cfg(target_arch = "wasm32")] async fn caller_sleep(duration: Duration) { let (cancel, cancelled) = oneshot::channel::<()>(); @@ -1872,6 +1924,84 @@ mod tests { assert_eq!(heights[0], 100, "the checkpoint stays"); assert_eq!(heights[1], 140 - MAX_RECENT_STATES as u32 + 1); } + + /// The real testnet proof in `tests/data`, 4 links long: the fetches + /// that trust its checkpoint and verify after every link, the quorum it + /// opens, its target height and the bootstrap envelope. + pub(super) fn testnet_proof() -> (ProofFetches, QuorumId, u32, Vec) { + let fixture: serde_json::Value = + serde_json::from_str(include_str!("../tests/data/proof-testnet.json")) + .expect("fixture JSON"); + let state = |key: &str| { + serde_json::from_value::(fixture[key].clone()).expect("a fixture state") + }; + let (checkpoint, target) = (state("checkpoint"), state("target")); + let body = hex::decode(fixture["bootstrap_hex"].as_str().expect("bootstrap_hex")) + .expect("bootstrap hex"); + let verified = bootstrap::verify(&body, &checkpoint, 0).expect("the fixture verifies"); + let commitment = parse_commitment(verified.records()[0].leaf).expect("a commitment"); + let mut hash = commitment.quorum_hash; + hash.reverse(); + let quorum = (u32::from(commitment.quorum_type), hash); + let timing = Timing { + verify_slice: Duration::ZERO, + ..Timing::default() + }; + let fetches = ProofFetches::new( + quorum.0, + 600, + checkpoint, + vec![Url::parse("https://relay.example/proofs").expect("a URL")], + NonZeroUsize::new(32).expect("non-zero"), + ProofTransport::new().expect("a client"), + timing, + ); + (fetches, quorum, target.height, body) + } + + /// Judge the fixture proof as the answer to an ask for its target. + pub(super) async fn judge_testnet_proof( + fetches: &ProofFetches, + quorum: QuorumId, + height: u32, + body: Vec, + ) -> bool { + let ask = Ask { + checkpoint: fetches.checkpoint().clone(), + height, + }; + let reply = Ok(Reply { status: 200, body }); + matches!( + fetches.judge(quorum, height, &ask, reply).await, + Verdict::Proven(_) + ) + } + + /// A relay can send a valid chain through every ChainLock quorum since + /// the state it was asked from, seconds of BLS work. Verifying it must + /// not keep other tasks off a current-thread runtime, where the fetch's + /// own deadline timer runs too. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_let_other_tasks_run_while_a_proof_verifies() { + let (fetches, quorum, height, body) = testnet_proof(); + let other_ran = Arc::new(AtomicBool::new(false)); + let other = { + let other_ran = Arc::clone(&other_ran); + tokio::spawn(async move { other_ran.store(true, Ordering::SeqCst) }) + }; + + let started = Instant::now(); + let proven = judge_testnet_proof(&fetches, quorum, height, body).await; + let elapsed = started.elapsed(); + + assert!(proven, "the fixture proves its quorum"); + assert!( + other_ran.load(Ordering::SeqCst), + "another task ran while the proof verified ({elapsed:?})" + ); + other.await.expect("the other task finished"); + } } #[cfg(all(test, target_arch = "wasm32"))] @@ -1927,4 +2057,28 @@ mod browser_tests { "{result:?}" ); } + + /// In the browser, verification yields to the event loop: a timer due + /// while a proof verifies fires before verification ends. The time taken + /// is logged, for comparison with native. + #[wasm_bindgen_test] + async fn should_let_browser_timers_fire_while_a_proof_verifies() { + use std::cell::Cell; + use std::rc::Rc; + + let (fetches, quorum, height, body) = super::tests::testnet_proof(); + let fired = Rc::new(Cell::new(false)); + { + let fired = Rc::clone(&fired); + gloo_timers::callback::Timeout::new(0, move || fired.set(true)).forget(); + } + + let started = Instant::now(); + let proven = super::tests::judge_testnet_proof(&fetches, quorum, height, body).await; + let elapsed = started.elapsed(); + wasm_bindgen_test::console_log!("verified a 4-link testnet proof in {elapsed:?}"); + + assert!(proven, "the fixture proves its quorum"); + assert!(fired.get(), "the timer fired while the proof verified"); + } } diff --git a/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs b/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs index a8b066c6804..a6dbbcd79a7 100644 --- a/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs +++ b/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs @@ -120,6 +120,12 @@ pub enum VerifiedContextProviderError { /// each fetch has a 70 s deadline. Freshness of responses is the SDK's /// concern, not the provider's. /// +/// A proof is verified on the task that polls the fetch, one chain link at a +/// time, yielding to the executor every 10 ms. A link costs about 4 ms +/// natively and 10 ms in a browser. A relay that sends a long valid chain +/// slows that fetch down but does not stall other tasks, timers or a +/// browser's event loop. +/// /// Keys are proven for the Core height Platform reports. If Platform halts, /// that height stops while Core moves on; a quorum not cached before the halt /// is still proven with a proof whose target is that height, as long as the From efe7069af80ace4e7d6156867e40b96d58ed8286 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:47:11 -0600 Subject: [PATCH 3/9] refactor(sdk): let the chain's finish verify the links left `QuorumProofChain::finish` now verifies any links left before the target, so `verify` and `Verification::finish` share one loop, and the progress carries the network instead of a cloned checkpoint. The checks and their order are unchanged. Co-Authored-By: Claude Opus 5.5 --- packages/rs-core-proof/src/bootstrap.rs | 13 +++----- packages/rs-core-proof/src/chain.rs | 40 ++++++++++++------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/packages/rs-core-proof/src/bootstrap.rs b/packages/rs-core-proof/src/bootstrap.rs index d0ef975e699..bde254f1863 100644 --- a/packages/rs-core-proof/src/bootstrap.rs +++ b/packages/rs-core-proof/src/bootstrap.rs @@ -88,7 +88,6 @@ pub fn start<'a>( let progress = chain.start(trusted)?; Ok(Verification { chain, - trusted: trusted.clone(), progress, records, }) @@ -97,7 +96,6 @@ pub fn start<'a>( /// An envelope part-way through verification. Only [`start`] constructs it. pub struct Verification<'a> { chain: QuorumProofChain, - trusted: State, progress: Progress, /// The envelope after the chain: its records. records: Reader<'a>, @@ -109,19 +107,16 @@ impl<'a> Verification<'a> { self.progress.links_left(&self.chain) } - /// Verify the next link of the chain. An error is final: the envelope is - /// invalid. + /// Verify the next link of the chain. An error means the envelope is + /// invalid, except when no link is left, which is an error too. pub fn verify_link(&mut self) -> Result<()> { self.chain.verify_link(&mut self.progress) } /// Verify any links left, then the target and the records, and return /// the verified envelope. - pub fn finish(mut self) -> Result> { - while self.links_left() > 0 { - self.verify_link()?; - } - let state = self.chain.finish(self.progress, &self.trusted)?; + pub fn finish(self) -> Result> { + let state = self.chain.finish(self.progress)?; open_records(self.records, state) } } diff --git a/packages/rs-core-proof/src/chain.rs b/packages/rs-core-proof/src/chain.rs index 26575ee2891..4b051b64151 100644 --- a/packages/rs-core-proof/src/chain.rs +++ b/packages/rs-core-proof/src/chain.rs @@ -7,7 +7,7 @@ //! coinbase commits to new masternode and quorum roots. use crate::bls; use crate::merkle::MerklePath; -use crate::state::State; +use crate::state::{ProofNetwork, State}; use crate::transaction::{ coinbase_roots, mined_commitment, parse_commitment, Commitment, MAX_COMMITMENT_BYTES, MAX_TRANSACTION_BYTES, @@ -160,7 +160,7 @@ impl Link { /// and the ChainLock quorum the last of them mined. #[derive(Debug)] pub(crate) struct Progress { - chainlock_type: u8, + network: ProofNetwork, signer: Commitment, height: u32, next_link: usize, @@ -174,8 +174,8 @@ impl Progress { } /// `commitment`, if it is of the network's ChainLock quorum type. -fn ensure_chainlock(commitment: Commitment, chainlock_type: u8) -> Result { - if commitment.quorum_type == chainlock_type { +fn ensure_chainlock(commitment: Commitment, network: ProofNetwork) -> Result { + if commitment.quorum_type == network.chainlock_quorum_type() { Ok(commitment) } else { Err(Error("unexpected quorum type")) @@ -205,11 +205,7 @@ impl QuorumProofChain { /// The chain must start from exactly `trusted`; a relay cannot choose the /// checkpoint. pub fn verify(&self, trusted: &State) -> Result { - let mut progress = self.start(trusted)?; - while progress.next_link < self.links.len() { - self.verify_link(&mut progress)?; - } - self.finish(progress, trusted) + self.finish(self.start(trusted)?) } /// The checks before any link: the anchor, the chain's size limits and @@ -226,12 +222,11 @@ impl QuorumProofChain { if headers > MAX_PROOF_HEADERS { return Err(Error("ancestor budget/height")); } - let chainlock_type = trusted.network.chainlock_quorum_type(); - let signer = ensure_chainlock(parse_commitment(&self.seed)?, chainlock_type)?; + let signer = ensure_chainlock(parse_commitment(&self.seed)?, trusted.network)?; self.seed_path .verify(sha256d(&self.seed), &trusted.quorum_root)?; Ok(Progress { - chainlock_type, + network: trusted.network, signer, height: trusted.height, next_link: 0, @@ -246,7 +241,7 @@ impl QuorumProofChain { .get(progress.next_link) .ok_or(Error("no link left"))?; link.certificate.verify(&progress.signer, progress.height)?; - let next = ensure_chainlock(link.mined_commitment()?, progress.chainlock_type)?; + let next = ensure_chainlock(link.mined_commitment()?, progress.network)?; if next.quorum_hash == progress.signer.quorum_hash { return Err(Error("redundant signer")); } @@ -256,13 +251,18 @@ impl QuorumProofChain { Ok(()) } - /// Verify the target and its coinbase once every link has verified, and - /// return the target block's state. - pub(crate) fn finish(&self, progress: Progress, trusted: &State) -> Result { - if progress.next_link != self.links.len() { - return Err(Error("unverified links")); + /// Verify the links left, then the target and its coinbase, and return + /// the target block's state. + pub(crate) fn finish(&self, mut progress: Progress) -> Result { + while progress.links_left(self) > 0 { + self.verify_link(&mut progress)?; } - let Progress { signer, height, .. } = progress; + let Progress { + network, + signer, + height, + .. + } = progress; let block_hash = self.target.verify(&signer, height)?; if self.coinbase.path.index != 0 { return Err(Error("coinbase inclusion")); @@ -271,7 +271,7 @@ impl QuorumProofChain { let (masternode_root, quorum_root) = coinbase_roots(&self.coinbase.transaction, self.target.height)?; Ok(State { - network: trusted.network, + network, height: self.target.height, block_hash, masternode_root, From 03da998d28d2bb04a2c8ec0cef5c3e8855b541dc Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:47:11 -0600 Subject: [PATCH 4/9] fix(sdk): stop verifying at the fetch deadline and yield unthrottled in browsers In a browser the proof fetch runs on a local task that outlives its callers, so nothing stopped a long verification, and hidden tabs throttle timers to one wake-up a second or less: a zero-delay timer per link would stretch even an honest proof to seconds and let a hostile one hold a proving place for minutes. Verification now ends at the fetch's deadline without blaming the relay, and the browser yield resumes on a `MessageChannel` message, which is not throttled, falling back to a timer. No yield follows the last link. Co-Authored-By: Claude Opus 5.5 --- .../Cargo.toml | 2 + .../src/proof_fetches.rs | 161 +++++++++++++----- 2 files changed, 121 insertions(+), 42 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/Cargo.toml b/packages/rs-sdk-trusted-context-provider/Cargo.toml index 89bde6a4f54..08110a69ecc 100644 --- a/packages/rs-sdk-trusted-context-provider/Cargo.toml +++ b/packages/rs-sdk-trusted-context-provider/Cargo.toml @@ -76,6 +76,8 @@ web-sys = { version = "0.3.85", features = [ "AbortController", "AbortSignal", "Headers", + "MessageChannel", + "MessagePort", "ReadableStream", "Request", "RequestCache", diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 660c32cd150..4012ac6342d 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -41,7 +41,7 @@ use crate::quorum_refresh::sleep; use crate::quorum_refresh::sleep as caller_sleep; use dash_context_provider::ContextProviderError; use dash_core_proof::bootstrap::{self, RecordKind, VerifiedBootstrap}; -use dash_core_proof::{parse_commitment, rpc_hex, Error as ProofError, ErrorKind, State}; +use dash_core_proof::{parse_commitment, rpc_hex, ErrorKind, State}; #[cfg(target_arch = "wasm32")] use futures::channel::oneshot; use futures::future::{self, select, BoxFuture, Either, FutureExt, Shared, WeakShared}; @@ -49,6 +49,8 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; #[cfg(target_arch = "wasm32")] use gloo_timers::future::sleep; +#[cfg(target_arch = "wasm32")] +use js_sys::Promise; use lru::LruCache; use std::collections::BTreeMap; use std::future::Future; @@ -60,6 +62,12 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; use std::time::Duration; use url::Url; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::JsValue; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_futures::JsFuture; +#[cfg(target_arch = "wasm32")] +use web_sys::MessageChannel; use web_time::Instant; /// A quorum's BLS public key. @@ -312,6 +320,9 @@ enum Verdict { /// A proof of the relay's latest ChainLock does not reach the height: /// the relay is left out of this fetch. BeyondTip(String), + /// The fetch's deadline passed while the proof was verified. Says + /// nothing about the relay: an honest proof can be long too. + OutOfTime(String), } /// One fetch: which quorum, for which height, and its slot. @@ -888,7 +899,7 @@ impl ProofFetches { failures.push(note(&reason)); continue; } - reply => self.judge(quorum, height, &ask, reply).await, + reply => self.judge(quorum, height, &ask, reply, deadline).await, }; match verdict { Verdict::Proven(proven) => { @@ -941,6 +952,8 @@ impl ProofFetches { state.dropped = true; failures.push(note(&reason)); } + // The deadline check above ends the fetch. + Verdict::OutOfTime(reason) => failures.push(note(&reason)), } } let reason = format!( @@ -967,19 +980,15 @@ impl ProofFetches { /// serve `height` is still published, as evidence for later asks, but does /// not prove the key for `height`. /// - /// The proof is verified one chain link at a time, yielding to the - /// executor after each [`Timing::verify_slice`]. A relay can send a valid - /// chain through every ChainLock quorum since the state it was asked from, - /// about a thousand links on testnet, seconds of BLS work. Yielding lets - /// other tasks and timers, including this fetch's deadline and the - /// browser's event loop, run meanwhile. If every caller drops the fetch, - /// verification stops at the next yield. + /// The proof is verified as [`verify`](Self::verify) describes, and only + /// until `deadline`. async fn judge( &self, quorum: QuorumId, height: u32, ask: &Ask, reply: Result, + deadline: Instant, ) -> Verdict { let reply = match reply { Ok(reply) => reply, @@ -1005,17 +1014,22 @@ impl ProofFetches { 0 => height.saturating_sub(DKG_INTERVAL_BLOCKS), asked => asked, }; - let verified = match self.verify(body, &ask.checkpoint, needed).await { - Ok(verified) => verified, - Err(error) if error.kind() == ErrorKind::StaleTarget && ask.height == 0 => { + let verified = match self.verify(body, &ask.checkpoint, needed, deadline).await { + None => { + return Verdict::OutOfTime( + "the fetch's deadline passed while its proof was verified".to_string(), + ) + } + Some(Ok(verified)) => verified, + Some(Err(error)) if error.kind() == ErrorKind::StaleTarget && ask.height == 0 => { return Verdict::BeyondTip(format!( "its latest ChainLock is below core height {needed}, which {height} needs" )); } - Err(error) if error.kind() == ErrorKind::StaleTarget => { + Some(Err(error)) if error.kind() == ErrorKind::StaleTarget => { return Verdict::CannotServe(format!("proof ends below core height {needed}")); } - Err(error) => return Verdict::SourceFailed(format!("invalid proof: {error}")), + Some(Err(error)) => return Verdict::SourceFailed(format!("invalid proof: {error}")), }; let mut keys = Vec::new(); // EvoNode records are not asked for; any that come back are ignored. @@ -1052,24 +1066,43 @@ impl ProofFetches { } } - /// `bootstrap::verify`, one link at a time, yielding to the executor - /// whenever a slice of verification has passed. + /// `bootstrap::verify`, one chain link at a time, yielding to the + /// executor after each [`Timing::verify_slice`]; `None` if `deadline` + /// passes first. + /// + /// A relay can send a valid chain through every ChainLock quorum since + /// the state it was asked from: about a thousand links on testnet, + /// seconds of BLS work. Yielding lets other tasks and timers, including + /// the callers' deadlines and the browser's event loop, run meanwhile. + /// Natively, dropping the fetch stops verification at the next yield. In + /// the browser the fetch runs on a local task that outlives its callers, + /// so verification stops at `deadline`. async fn verify<'a>( &self, body: &'a [u8], checkpoint: &State, minimum_height: u32, - ) -> Result, ProofError> { - let mut verification = bootstrap::start(body, checkpoint, minimum_height)?; + deadline: Instant, + ) -> Option>> { + let mut verification = match bootstrap::start(body, checkpoint, minimum_height) { + Ok(verification) => verification, + Err(error) => return Some(Err(error)), + }; let mut slice_started = Instant::now(); while verification.links_left() > 0 { - verification.verify_link()?; - if slice_started.elapsed() >= self.timing.verify_slice { + if let Err(error) = verification.verify_link() { + return Some(Err(error)); + } + if verification.links_left() > 0 && slice_started.elapsed() >= self.timing.verify_slice + { yield_to_executor().await; + if Instant::now() >= deadline { + return None; + } slice_started = Instant::now(); } } - verification.finish() + Some(verification.finish()) } /// What to ask a source next for this quorum, given what the cache holds @@ -1296,9 +1329,6 @@ where } } -/// Wait for `duration` in a `Send` future, for callers outside the local -/// executor. The browser timer runs in a local task that ends, clearing the -/// timer, as soon as this future is dropped. /// Let the executor run other tasks, and its timers, before this one goes on. #[cfg(not(target_arch = "wasm32"))] async fn yield_to_executor() { @@ -1308,13 +1338,34 @@ async fn yield_to_executor() { } /// Let the browser's event loop run, with its timers, input and rendering, -/// before this task goes on. A timer, not a microtask: microtasks run before -/// the event loop moves on. +/// before this task goes on. +/// +/// The task resumes on a `MessageChannel` message: a task of its own, unlike +/// a microtask, which runs before the event loop moves on. Unlike a timer it +/// is not throttled in a hidden tab, where timers fire at most once a second +/// or less, which would stretch even an honest proof to seconds. A zero-delay +/// timer is the fallback where there is no `MessageChannel`. #[cfg(target_arch = "wasm32")] async fn yield_to_executor() { + if let Ok(channel) = MessageChannel::new() { + let port = channel.port1(); + let message = Promise::new(&mut |resolve, _reject| port.set_onmessage(Some(&resolve))); + let posted = channel.port2().post_message(&JsValue::UNDEFINED).is_ok(); + if posted { + let _ = JsFuture::from(message).await; + } + port.close(); + channel.port2().close(); + if posted { + return; + } + } sleep(Duration::ZERO).await; } +/// Wait for `duration` in a `Send` future, for callers outside the local +/// executor. The browser timer runs in a local task that ends, clearing the +/// timer, as soon as this future is dropped. #[cfg(target_arch = "wasm32")] async fn caller_sleep(duration: Duration) { let (cancel, cancelled) = oneshot::channel::<()>(); @@ -1959,22 +2010,23 @@ mod tests { (fetches, quorum, target.height, body) } - /// Judge the fixture proof as the answer to an ask for its target. + /// Judge the fixture proof as the answer to an ask for its target, + /// with a fetch deadline `deadline` from now. pub(super) async fn judge_testnet_proof( fetches: &ProofFetches, quorum: QuorumId, height: u32, body: Vec, - ) -> bool { + deadline: Duration, + ) -> Verdict { let ask = Ask { checkpoint: fetches.checkpoint().clone(), height, }; let reply = Ok(Reply { status: 200, body }); - matches!( - fetches.judge(quorum, height, &ask, reply).await, - Verdict::Proven(_) - ) + fetches + .judge(quorum, height, &ask, reply, Instant::now() + deadline) + .await } /// A relay can send a valid chain through every ChainLock quorum since @@ -1991,17 +2043,33 @@ mod tests { tokio::spawn(async move { other_ran.store(true, Ordering::SeqCst) }) }; - let started = Instant::now(); - let proven = judge_testnet_proof(&fetches, quorum, height, body).await; - let elapsed = started.elapsed(); + let verdict = + judge_testnet_proof(&fetches, quorum, height, body, Duration::from_secs(60)).await; - assert!(proven, "the fixture proves its quorum"); + assert!( + matches!(verdict, Verdict::Proven(_)), + "the fixture proves its quorum" + ); assert!( other_ran.load(Ordering::SeqCst), - "another task ran while the proof verified ({elapsed:?})" + "another task ran while the proof verified" ); other.await.expect("the other task finished"); } + + /// Verification stops at the fetch's deadline, without blaming the + /// relay: in the browser nothing else would stop it, and an honest proof + /// can be long too. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_stop_verifying_at_the_fetchs_deadline() { + let (fetches, quorum, height, body) = testnet_proof(); + + let verdict = judge_testnet_proof(&fetches, quorum, height, body, Duration::ZERO).await; + + assert!(matches!(verdict, Verdict::OutOfTime(_))); + assert_eq!(fetches.cached_quorum_count(), 0, "nothing was proven"); + } } #[cfg(all(test, target_arch = "wasm32"))] @@ -2009,6 +2077,8 @@ mod browser_tests { use super::*; use dash_core_proof::ProofNetwork; use js_sys::{Function, Reflect}; + use std::cell::Cell; + use std::rc::Rc; use wasm_bindgen::JsValue; use wasm_bindgen_test::wasm_bindgen_test; @@ -2063,9 +2133,6 @@ mod browser_tests { /// is logged, for comparison with native. #[wasm_bindgen_test] async fn should_let_browser_timers_fire_while_a_proof_verifies() { - use std::cell::Cell; - use std::rc::Rc; - let (fetches, quorum, height, body) = super::tests::testnet_proof(); let fired = Rc::new(Cell::new(false)); { @@ -2074,11 +2141,21 @@ mod browser_tests { } let started = Instant::now(); - let proven = super::tests::judge_testnet_proof(&fetches, quorum, height, body).await; + let verdict = super::tests::judge_testnet_proof( + &fetches, + quorum, + height, + body, + Duration::from_secs(60), + ) + .await; let elapsed = started.elapsed(); wasm_bindgen_test::console_log!("verified a 4-link testnet proof in {elapsed:?}"); - assert!(proven, "the fixture proves its quorum"); + assert!( + matches!(verdict, Verdict::Proven(_)), + "the fixture proves its quorum" + ); assert!(fired.get(), "the timer fired while the proof verified"); } } From 361a7522e8654ec18be35d880be99bc94ce94a0d Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:49:03 -0600 Subject: [PATCH 5/9] fix(sdk): close the browser yield's ports even when it is dropped Co-Authored-By: Claude Opus 5.5 --- .../src/proof_fetches.rs | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 4012ac6342d..8d4b9b6633f 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -1348,21 +1348,30 @@ async fn yield_to_executor() { #[cfg(target_arch = "wasm32")] async fn yield_to_executor() { if let Ok(channel) = MessageChannel::new() { - let port = channel.port1(); - let message = Promise::new(&mut |resolve, _reject| port.set_onmessage(Some(&resolve))); - let posted = channel.port2().post_message(&JsValue::UNDEFINED).is_ok(); - if posted { + let channel = ClosedOnDrop(channel); + let message = + Promise::new(&mut |resolve, _reject| channel.0.port1().set_onmessage(Some(&resolve))); + if channel.0.port2().post_message(&JsValue::UNDEFINED).is_ok() { let _ = JsFuture::from(message).await; - } - port.close(); - channel.port2().close(); - if posted { return; } } sleep(Duration::ZERO).await; } +/// Closes both ports of a channel when dropped, also mid-await, so an open +/// port with a handler never keeps the event loop alive. +#[cfg(target_arch = "wasm32")] +struct ClosedOnDrop(MessageChannel); + +#[cfg(target_arch = "wasm32")] +impl Drop for ClosedOnDrop { + fn drop(&mut self) { + self.0.port1().close(); + self.0.port2().close(); + } +} + /// Wait for `duration` in a `Send` future, for callers outside the local /// executor. The browser timer runs in a local task that ends, clearing the /// timer, as soon as this future is dropped. From ca3670c7e214c2e31702e72aec5c79cfb4650b02 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 14:41:34 -0600 Subject: [PATCH 6/9] fix(sdk): resume a shared fetch's verification only after the executor turns Tokio's `yield_now` is ready on its second poll whether or not its deferred wake has come. A fetch is `Shared`, and every caller's clone polls it within one turn of the callers' task, so with a few callers a whole proof was verified in one turn again. The native yield now waits for that deferred wake itself and stays pending under further polls. Co-Authored-By: Claude Opus 5.5 --- .../src/proof_fetches.rs | 109 +++++++++++++++++- 1 file changed, 106 insertions(+), 3 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 8d4b9b6633f..1aaaa3d478e 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -46,6 +46,8 @@ use dash_core_proof::{parse_commitment, rpc_hex, ErrorKind, State}; use futures::channel::oneshot; use futures::future::{self, select, BoxFuture, Either, FutureExt, Shared, WeakShared}; use futures::stream::FuturesUnordered; +#[cfg(not(target_arch = "wasm32"))] +use futures::task::{self, ArcWake, AtomicWaker}; use futures::StreamExt; #[cfg(target_arch = "wasm32")] use gloo_timers::future::sleep; @@ -60,6 +62,8 @@ use std::panic::AssertUnwindSafe; use std::pin::pin; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; +#[cfg(not(target_arch = "wasm32"))] +use std::task::{Context, Poll}; use std::time::Duration; use url::Url; #[cfg(target_arch = "wasm32")] @@ -1330,11 +1334,49 @@ where } /// Let the executor run other tasks, and its timers, before this one goes on. +/// +/// Tokio's `yield_now` defers its wake until the scheduler has polled the +/// timer driver, so even on a current-thread runtime a due timer fires +/// first. But it is ready on its second poll whether or not that wake came, +/// and a fetch is `Shared`: every caller's clone polls it within one turn of +/// the caller's task, so each would resume verification at once. This yield +/// is ready only once the deferred wake has come. #[cfg(not(target_arch = "wasm32"))] async fn yield_to_executor() { - // Tokio's yield polls the timer driver before resuming the task, so even - // on a current-thread runtime a due timer fires first. - tokio::task::yield_now().await; + let turn = Arc::new(ExecutorTurn::default()); + let mut yielded = Box::pin(tokio::task::yield_now()); + let mut asked = false; + future::poll_fn(|cx| { + turn.waker.register(cx.waker()); + if turn.came.load(Ordering::Acquire) { + return Poll::Ready(()); + } + if !asked { + asked = true; + let waker = task::waker(Arc::clone(&turn)); + // Pending the first time; the wake it defers sets `came`. + let _ = yielded.as_mut().poll(&mut Context::from_waker(&waker)); + } + Poll::Pending + }) + .await +} + +/// Whether the executor has come back to a yielding task, and the waker of +/// whichever poll asked last. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Default)] +struct ExecutorTurn { + came: AtomicBool, + waker: AtomicWaker, +} + +#[cfg(not(target_arch = "wasm32"))] +impl ArcWake for ExecutorTurn { + fn wake_by_ref(turn: &Arc) { + turn.came.store(true, Ordering::Release); + turn.waker.wake(); + } } /// Let the browser's event loop run, with its timers, input and rendering, @@ -2066,6 +2108,67 @@ mod tests { other.await.expect("the other task finished"); } + /// A fetch is shared: every caller's clone polls it within one turn of + /// the callers' task. Those polls must not resume verification before + /// the executor has run other tasks. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_let_other_tasks_run_while_many_callers_share_a_verifying_fetch() { + let (fetches, quorum, height, body) = testnet_proof(); + let shared = judge_testnet_proof(&fetches, quorum, height, body, Duration::from_secs(60)) + .map(|verdict| matches!(verdict, Verdict::Proven(_))) + .boxed() + .shared(); + let other_ran = Arc::new(AtomicBool::new(false)); + let other = { + let other_ran = Arc::clone(&other_ran); + tokio::spawn(async move { other_ran.store(true, Ordering::SeqCst) }) + }; + + let mut callers: Vec<_> = (0..30).map(|_| shared.clone()).collect(); + + // One turn of the callers' task: each caller polls its clone once. + future::poll_fn(|cx| { + for caller in &mut callers { + assert!( + caller.poll_unpin(cx).is_pending(), + "verification finished within one turn of the executor" + ); + } + Poll::Ready(()) + }) + .await; + let proven = future::join_all(callers).await; + + assert!( + proven.into_iter().all(|proven| proven), + "every caller got the key" + ); + assert!( + other_ran.load(Ordering::SeqCst), + "another task ran while the shared proof verified" + ); + other.await.expect("the other task finished"); + } + + /// Polling a yield again before the executor has come back does not + /// end it. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_not_end_a_yield_before_the_executor_comes_back() { + let mut yielded = pin!(yield_to_executor()); + future::poll_fn(|cx| { + assert!(yielded.as_mut().poll(cx).is_pending()); + assert!( + yielded.as_mut().poll(cx).is_pending(), + "polled again in the same turn" + ); + Poll::Ready(()) + }) + .await; + yielded.await; + } + /// Verification stops at the fetch's deadline, without blaming the /// relay: in the browser nothing else would stop it, and an honest proof /// can be long too. From fd725d6965beceaf10f012032a733e76f0cba5b3 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 15:16:26 -0600 Subject: [PATCH 7/9] fix(sdk): yield verification with a timer and hold every path to the deadline Outside a scheduler's context, such as a multi-thread runtime's `block_on` future, Tokio does not defer `yield_now`'s wake, so the callers sharing a fetch could still resume verification slice after slice within one turn. The native yield is now a 1 ms timer, which stays pending until the timer driver fires it however often it is polled. The deadline is now checked before every link and before a verified result is accepted, so proofs that never yield are held to it too. Co-Authored-By: Claude Opus 5.5 --- .../src/proof_fetches.rs | 113 ++++++++++-------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 1aaaa3d478e..8a204544aae 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -46,8 +46,6 @@ use dash_core_proof::{parse_commitment, rpc_hex, ErrorKind, State}; use futures::channel::oneshot; use futures::future::{self, select, BoxFuture, Either, FutureExt, Shared, WeakShared}; use futures::stream::FuturesUnordered; -#[cfg(not(target_arch = "wasm32"))] -use futures::task::{self, ArcWake, AtomicWaker}; use futures::StreamExt; #[cfg(target_arch = "wasm32")] use gloo_timers::future::sleep; @@ -62,8 +60,6 @@ use std::panic::AssertUnwindSafe; use std::pin::pin; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; -#[cfg(not(target_arch = "wasm32"))] -use std::task::{Context, Poll}; use std::time::Duration; use url::Url; #[cfg(target_arch = "wasm32")] @@ -120,6 +116,11 @@ pub(crate) const DKG_INTERVAL_BLOCKS: u32 = 24; /// How long a fetch waits before trying again to take a place to prove its /// quorum. const PLACE_RETRY: Duration = Duration::from_millis(250); +/// How long verification pauses at each yield natively: the timer driver's +/// resolution. Slices are ten times longer, so this costs a long proof about +/// a tenth more time. +#[cfg(not(target_arch = "wasm32"))] +const EXECUTOR_YIELD: Duration = Duration::from_millis(1); /// How much of an error response's body goes into the failure reason. const MAX_REASON_BODY_CHARS: usize = 120; @@ -1094,19 +1095,24 @@ impl ProofFetches { }; let mut slice_started = Instant::now(); while verification.links_left() > 0 { + if Instant::now() >= deadline { + return None; + } if let Err(error) = verification.verify_link() { return Some(Err(error)); } if verification.links_left() > 0 && slice_started.elapsed() >= self.timing.verify_slice { yield_to_executor().await; - if Instant::now() >= deadline { - return None; - } slice_started = Instant::now(); } } - Some(verification.finish()) + let verified = verification.finish(); + // A key proven after the deadline is not accepted either. + if Instant::now() >= deadline { + return None; + } + Some(verified) } /// What to ask a source next for this quorum, given what the cache holds @@ -1335,48 +1341,16 @@ where /// Let the executor run other tasks, and its timers, before this one goes on. /// -/// Tokio's `yield_now` defers its wake until the scheduler has polled the -/// timer driver, so even on a current-thread runtime a due timer fires -/// first. But it is ready on its second poll whether or not that wake came, -/// and a fetch is `Shared`: every caller's clone polls it within one turn of -/// the caller's task, so each would resume verification at once. This yield -/// is ready only once the deferred wake has come. +/// A short timer, not `tokio::task::yield_now`. A fetch is `Shared`, and every +/// caller's clone polls it within one turn of the callers' task. `yield_now` +/// is ready on its second poll, and outside a scheduler's context (a +/// multi-thread runtime's `block_on` future, another executor) its wake is +/// not even deferred, so those polls would resume verification one slice +/// after another in that turn. A timer stays pending until the runtime's +/// timer driver fires it, however often it is polled. #[cfg(not(target_arch = "wasm32"))] async fn yield_to_executor() { - let turn = Arc::new(ExecutorTurn::default()); - let mut yielded = Box::pin(tokio::task::yield_now()); - let mut asked = false; - future::poll_fn(|cx| { - turn.waker.register(cx.waker()); - if turn.came.load(Ordering::Acquire) { - return Poll::Ready(()); - } - if !asked { - asked = true; - let waker = task::waker(Arc::clone(&turn)); - // Pending the first time; the wake it defers sets `came`. - let _ = yielded.as_mut().poll(&mut Context::from_waker(&waker)); - } - Poll::Pending - }) - .await -} - -/// Whether the executor has come back to a yielding task, and the waker of -/// whichever poll asked last. -#[cfg(not(target_arch = "wasm32"))] -#[derive(Default)] -struct ExecutorTurn { - came: AtomicBool, - waker: AtomicWaker, -} - -#[cfg(not(target_arch = "wasm32"))] -impl ArcWake for ExecutorTurn { - fn wake_by_ref(turn: &Arc) { - turn.came.store(true, Ordering::Release); - turn.waker.wake(); - } + tokio::time::sleep(EXECUTOR_YIELD).await; } /// Let the browser's event loop run, with its timers, input and rendering, @@ -1447,6 +1421,8 @@ where mod tests { use super::*; use dash_core_proof::ProofNetwork; + #[cfg(not(target_arch = "wasm32"))] + use std::task::Poll; fn state(height: u32) -> State { State { @@ -2031,6 +2007,13 @@ mod tests { /// that trust its checkpoint and verify after every link, the quorum it /// opens, its target height and the bootstrap envelope. pub(super) fn testnet_proof() -> (ProofFetches, QuorumId, u32, Vec) { + testnet_proof_sliced(Duration::ZERO) + } + + /// As [`testnet_proof`], yielding after each `verify_slice`. + pub(super) fn testnet_proof_sliced( + verify_slice: Duration, + ) -> (ProofFetches, QuorumId, u32, Vec) { let fixture: serde_json::Value = serde_json::from_str(include_str!("../tests/data/proof-testnet.json")) .expect("fixture JSON"); @@ -2046,7 +2029,7 @@ mod tests { hash.reverse(); let quorum = (u32::from(commitment.quorum_type), hash); let timing = Timing { - verify_slice: Duration::ZERO, + verify_slice, ..Timing::default() }; let fetches = ProofFetches::new( @@ -2114,6 +2097,26 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[tokio::test(flavor = "current_thread")] async fn should_let_other_tasks_run_while_many_callers_share_a_verifying_fetch() { + callers_sharing_a_verifying_fetch_wait_for_the_executor().await; + } + + /// The same from a multi-thread runtime's `block_on` future, which runs + /// outside the scheduler, so Tokio does not defer its wakes. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn should_let_other_tasks_run_while_callers_share_a_fetch_from_block_on() { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .enable_all() + .build() + .expect("a runtime") + .block_on(callers_sharing_a_verifying_fetch_wait_for_the_executor()); + } + + /// 30 callers share a verifying fetch. None gets a result within one + /// turn of their task, and another task runs before verification ends. + #[cfg(not(target_arch = "wasm32"))] + async fn callers_sharing_a_verifying_fetch_wait_for_the_executor() { let (fetches, quorum, height, body) = testnet_proof(); let shared = judge_testnet_proof(&fetches, quorum, height, body, Duration::from_secs(60)) .map(|verdict| matches!(verdict, Verdict::Proven(_))) @@ -2182,6 +2185,18 @@ mod tests { assert!(matches!(verdict, Verdict::OutOfTime(_))); assert_eq!(fetches.cached_quorum_count(), 0, "nothing was proven"); } + + /// A proof verified without a yield is held to the deadline too. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_stop_verifying_at_the_deadline_without_a_yield() { + let (fetches, quorum, height, body) = testnet_proof_sliced(Duration::from_secs(3600)); + + let verdict = judge_testnet_proof(&fetches, quorum, height, body, Duration::ZERO).await; + + assert!(matches!(verdict, Verdict::OutOfTime(_))); + assert_eq!(fetches.cached_quorum_count(), 0, "nothing was proven"); + } } #[cfg(all(test, target_arch = "wasm32"))] From c99459656a755ad32dae2af08af2c8b44218d058 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 15:17:21 -0600 Subject: [PATCH 8/9] chore(sdk): drop the tokio rt feature the verification yield no longer needs Co-Authored-By: Claude Opus 5.5 --- packages/rs-sdk-trusted-context-provider/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/Cargo.toml b/packages/rs-sdk-trusted-context-provider/Cargo.toml index 08110a69ecc..81c4437c37e 100644 --- a/packages/rs-sdk-trusted-context-provider/Cargo.toml +++ b/packages/rs-sdk-trusted-context-provider/Cargo.toml @@ -88,9 +88,9 @@ web-sys = { version = "0.3.85", features = [ ] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -# Waits out the gap between two refreshes of the quorum lists, and yields to -# the runtime while a long proof is verified. -tokio = { version = "1.40", features = ["rt", "time"] } +# Waits out the gap between two refreshes of the quorum lists, and pauses +# between slices of a long proof's verification. +tokio = { version = "1.40", features = ["time"] } [target.'cfg(not(target_os = "android"))'.dependencies] reqwest = { version = "0.12", features = ["json"] } From 4a95215535d24d519d5201bb3a978c05cf84b965 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 15:43:03 -0600 Subject: [PATCH 9/9] test(sdk): cover a deadline passing mid-proof and while a proof finishes `verify` takes its clock as a parameter, `Instant::now` outside tests, so the tests can let the deadline pass at a chosen reading: during the first yield, which must stop the proof before its next link, and at the check of the finished result, which must reject it. Co-Authored-By: Claude Opus 5.5 --- .../src/proof_fetches.rs | 82 +++++++++++++++++-- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs index 8a204544aae..f4fd7736184 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -1019,7 +1019,10 @@ impl ProofFetches { 0 => height.saturating_sub(DKG_INTERVAL_BLOCKS), asked => asked, }; - let verified = match self.verify(body, &ask.checkpoint, needed, deadline).await { + let verified = match self + .verify(body, &ask.checkpoint, needed, deadline, Instant::now) + .await + { None => { return Verdict::OutOfTime( "the fetch's deadline passed while its proof was verified".to_string(), @@ -1082,34 +1085,38 @@ impl ProofFetches { /// Natively, dropping the fetch stops verification at the next yield. In /// the browser the fetch runs on a local task that outlives its callers, /// so verification stops at `deadline`. + /// + /// `now` is the clock, `Instant::now` outside tests. async fn verify<'a>( &self, body: &'a [u8], checkpoint: &State, minimum_height: u32, deadline: Instant, + now: impl Fn() -> Instant, ) -> Option>> { let mut verification = match bootstrap::start(body, checkpoint, minimum_height) { Ok(verification) => verification, Err(error) => return Some(Err(error)), }; - let mut slice_started = Instant::now(); + let mut slice_started = now(); while verification.links_left() > 0 { - if Instant::now() >= deadline { + if now() >= deadline { return None; } if let Err(error) = verification.verify_link() { return Some(Err(error)); } - if verification.links_left() > 0 && slice_started.elapsed() >= self.timing.verify_slice + if verification.links_left() > 0 + && now().saturating_duration_since(slice_started) >= self.timing.verify_slice { yield_to_executor().await; - slice_started = Instant::now(); + slice_started = now(); } } let verified = verification.finish(); // A key proven after the deadline is not accepted either. - if Instant::now() >= deadline { + if now() >= deadline { return None; } Some(verified) @@ -1422,6 +1429,8 @@ mod tests { use super::*; use dash_core_proof::ProofNetwork; #[cfg(not(target_arch = "wasm32"))] + use std::sync::atomic::AtomicUsize; + #[cfg(not(target_arch = "wasm32"))] use std::task::Poll; fn state(height: u32) -> State { @@ -2186,6 +2195,67 @@ mod tests { assert_eq!(fetches.cached_quorum_count(), 0, "nothing was proven"); } + /// Verify the fixture proof with a clock that reads a minute before + /// `deadline` until its `expire_at`th reading, and `deadline` from then + /// on. Returns whether a result came back and how often the clock was + /// read. + #[cfg(not(target_arch = "wasm32"))] + async fn verify_with_clock(verify_slice: Duration, expire_at: usize) -> (bool, usize) { + let (fetches, _, height, body) = testnet_proof_sliced(verify_slice); + let deadline = Instant::now() + Duration::from_secs(3600); + let readings = AtomicUsize::new(0); + let clock = || { + if readings.fetch_add(1, Ordering::SeqCst) + 1 >= expire_at { + deadline + } else { + deadline - Duration::from_secs(60) + } + }; + let verified = fetches + .verify(&body, fetches.checkpoint(), height, deadline, clock) + .await; + if let Some(result) = &verified { + assert!(result.is_ok(), "the fixture verifies"); + } + (verified.is_some(), readings.load(Ordering::SeqCst)) + } + + /// A deadline that passes while a proof is part-way verified, here + /// during the first yield, stops it before the next link. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_stop_a_proof_part_way_when_the_deadline_passes() { + let (verified, all) = verify_with_clock(Duration::ZERO, usize::MAX).await; + assert!(verified); + // The start, the first link's deadline and slice checks: then it + // yields, and the deadline passes. + let (verified, read) = verify_with_clock(Duration::ZERO, 4).await; + assert!(!verified, "stopped"); + assert_eq!( + read, 5, + "only the restarted slice and the next link's check" + ); + assert!( + read < all, + "the other links were not verified ({read} of {all})" + ); + } + + /// A deadline that passes while the last steps verify rejects the + /// result, valid as it is. + #[cfg(not(target_arch = "wasm32"))] + #[tokio::test(flavor = "current_thread")] + async fn should_reject_a_proof_finished_after_the_deadline() { + let (verified, all) = verify_with_clock(Duration::from_secs(3600), usize::MAX).await; + assert!(verified); + // The start, a deadline check before each of the 4 links, a slice + // check between links, and the check of the finished result, last. + assert_eq!(all, 1 + 4 + 3 + 1); + let (verified, read) = verify_with_clock(Duration::from_secs(3600), all).await; + assert!(!verified, "the result came after the deadline"); + assert_eq!(read, all); + } + /// A proof verified without a yield is held to the deadline too. #[cfg(not(target_arch = "wasm32"))] #[tokio::test(flavor = "current_thread")]