diff --git a/packages/rs-core-proof/src/bootstrap.rs b/packages/rs-core-proof/src/bootstrap.rs index ae6c5ebb7eb..bde254f1863 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,69 @@ 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, + progress, + records, + }) +} + +/// An envelope part-way through verification. Only [`start`] constructs it. +pub struct Verification<'a> { + chain: QuorumProofChain, + 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 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(self) -> Result> { + let state = self.chain.finish(self.progress)?; + 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..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, @@ -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 { + network: ProofNetwork, + 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, network: ProofNetwork) -> Result { + if commitment.quorum_type == network.chainlock_quorum_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,12 @@ impl QuorumProofChain { /// The chain must start from exactly `trusted`; a relay cannot choose the /// checkpoint. pub fn verify(&self, trusted: &State) -> Result { + self.finish(self.start(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); } @@ -190,28 +222,47 @@ impl QuorumProofChain { if headers > MAX_PROOF_HEADERS { 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)?, trusted.network)?; 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 { + network: trusted.network, + 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.network)?; + 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 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 { + network, + signer, + height, + .. + } = progress; let block_hash = self.target.verify(&signer, height)?; if self.coinbase.path.index != 0 { return Err(Error("coinbase inclusion")); @@ -220,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, 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}" + ); + } +} diff --git a/packages/rs-sdk-trusted-context-provider/Cargo.toml b/packages/rs-sdk-trusted-context-provider/Cargo.toml index 443651c9d15..81c4437c37e 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", @@ -86,7 +88,8 @@ 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. +# 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] 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..f4fd7736184 100644 --- a/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs +++ b/packages/rs-sdk-trusted-context-provider/src/proof_fetches.rs @@ -40,7 +40,7 @@ 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::bootstrap::{self, RecordKind, VerifiedBootstrap}; use dash_core_proof::{parse_commitment, rpc_hex, ErrorKind, State}; #[cfg(target_arch = "wasm32")] use futures::channel::oneshot; @@ -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. @@ -108,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; @@ -132,6 +145,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 +162,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), } } } @@ -304,6 +325,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. @@ -880,7 +904,7 @@ impl ProofFetches { failures.push(note(&reason)); continue; } - reply => self.judge(quorum, height, &ask, reply), + reply => self.judge(quorum, height, &ask, reply, deadline).await, }; match verdict { Verdict::Proven(proven) => { @@ -933,6 +957,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!( @@ -958,12 +984,16 @@ 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 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, @@ -989,17 +1019,25 @@ impl ProofFetches { 0 => height.saturating_sub(DKG_INTERVAL_BLOCKS), asked => asked, }; - let verified = match bootstrap::verify(body, &ask.checkpoint, needed) { - Ok(verified) => verified, - Err(error) if error.kind() == ErrorKind::StaleTarget && ask.height == 0 => { + 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(), + ) + } + 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. @@ -1036,6 +1074,54 @@ impl ProofFetches { } } + /// `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`. + /// + /// `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 = now(); + while verification.links_left() > 0 { + if now() >= deadline { + return None; + } + if let Err(error) = verification.verify_link() { + return Some(Err(error)); + } + if verification.links_left() > 0 + && now().saturating_duration_since(slice_started) >= self.timing.verify_slice + { + yield_to_executor().await; + slice_started = now(); + } + } + let verified = verification.finish(); + // A key proven after the deadline is not accepted either. + if now() >= deadline { + return None; + } + Some(verified) + } + /// 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 { @@ -1260,6 +1346,55 @@ where } } +/// Let the executor run other tasks, and its timers, before this one goes on. +/// +/// 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() { + tokio::time::sleep(EXECUTOR_YIELD).await; +} + +/// Let the browser's event loop run, with its timers, input and rendering, +/// 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 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; + 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. @@ -1293,6 +1428,10 @@ where 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 { State { @@ -1872,6 +2011,262 @@ 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) { + 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"); + 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, + ..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, + /// with a fetch deadline `deadline` from now. + pub(super) async fn judge_testnet_proof( + fetches: &ProofFetches, + quorum: QuorumId, + height: u32, + body: Vec, + deadline: Duration, + ) -> Verdict { + let ask = Ask { + checkpoint: fetches.checkpoint().clone(), + height, + }; + let reply = Ok(Reply { status: 200, body }); + fetches + .judge(quorum, height, &ask, reply, Instant::now() + deadline) + .await + } + + /// 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 verdict = + judge_testnet_proof(&fetches, quorum, height, body, Duration::from_secs(60)).await; + + assert!( + matches!(verdict, Verdict::Proven(_)), + "the fixture proves its quorum" + ); + assert!( + other_ran.load(Ordering::SeqCst), + "another task ran while the proof verified" + ); + 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() { + 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(_))) + .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. + #[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"); + } + + /// 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")] + 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"))] @@ -1879,6 +2274,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; @@ -1927,4 +2324,35 @@ 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() { + 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 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!( + matches!(verdict, Verdict::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