zebra_state/request.rs
1//! State [`tower::Service`] request types.
2
3use std::{
4 collections::{HashMap, HashSet},
5 ops::{Add, Deref, DerefMut, RangeInclusive},
6 pin::Pin,
7 sync::Arc,
8};
9
10use tower::{BoxError, Service, ServiceExt};
11use zebra_chain::{
12 amount::{DeferredPoolBalanceChange, NegativeAllowed},
13 block::{self, Block, HeightDiff},
14 diagnostic::{task::WaitForPanics, CodeTimer},
15 history_tree::HistoryTree,
16 orchard,
17 parallel::tree::NoteCommitmentTrees,
18 sapling,
19 serialization::SerializationError,
20 sprout,
21 subtree::{NoteCommitmentSubtree, NoteCommitmentSubtreeIndex},
22 transaction::{self, UnminedTx},
23 transparent::{self, utxos_from_ordered_utxos},
24 value_balance::{ValueBalance, ValueBalanceError},
25};
26
27/// Allow *only* these unused imports, so that rustdoc link resolution
28/// will work with inline links.
29#[allow(unused_imports)]
30use crate::{
31 constants::{MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS},
32 ReadResponse, Response,
33};
34use crate::{
35 error::{CommitCheckpointVerifiedError, InvalidateError, LayeredStateError, ReconsiderError},
36 CommitSemanticallyVerifiedError,
37};
38
39/// Identify a spend by a transparent outpoint or revealed nullifier.
40///
41/// This enum implements `From` for [`transparent::OutPoint`], [`sprout::Nullifier`],
42/// [`sapling::Nullifier`], and [`orchard::Nullifier`].
43#[derive(Copy, Clone, Debug, PartialEq, Eq)]
44#[cfg(feature = "indexer")]
45pub enum Spend {
46 /// A spend identified by a [`transparent::OutPoint`].
47 OutPoint(transparent::OutPoint),
48 /// A spend identified by a [`sprout::Nullifier`].
49 Sprout(sprout::Nullifier),
50 /// A spend identified by a [`sapling::Nullifier`].
51 Sapling(sapling::Nullifier),
52 /// A spend identified by a [`orchard::Nullifier`].
53 Orchard(orchard::Nullifier),
54}
55
56#[cfg(feature = "indexer")]
57impl From<transparent::OutPoint> for Spend {
58 fn from(outpoint: transparent::OutPoint) -> Self {
59 Self::OutPoint(outpoint)
60 }
61}
62
63#[cfg(feature = "indexer")]
64impl From<sprout::Nullifier> for Spend {
65 fn from(sprout_nullifier: sprout::Nullifier) -> Self {
66 Self::Sprout(sprout_nullifier)
67 }
68}
69
70#[cfg(feature = "indexer")]
71impl From<sapling::Nullifier> for Spend {
72 fn from(sapling_nullifier: sapling::Nullifier) -> Self {
73 Self::Sapling(sapling_nullifier)
74 }
75}
76
77#[cfg(feature = "indexer")]
78impl From<orchard::Nullifier> for Spend {
79 fn from(orchard_nullifier: orchard::Nullifier) -> Self {
80 Self::Orchard(orchard_nullifier)
81 }
82}
83
84/// Identify a block by hash or height.
85///
86/// This enum implements `From` for [`block::Hash`] and [`block::Height`],
87/// so it can be created using `hash.into()` or `height.into()`.
88#[derive(Copy, Clone, Debug, PartialEq, Eq)]
89pub enum HashOrHeight {
90 /// A block identified by hash.
91 Hash(block::Hash),
92 /// A block identified by height.
93 Height(block::Height),
94}
95
96impl HashOrHeight {
97 /// Unwrap the inner height or attempt to retrieve the height for a given
98 /// hash if one exists.
99 pub fn height_or_else<F>(self, op: F) -> Option<block::Height>
100 where
101 F: FnOnce(block::Hash) -> Option<block::Height>,
102 {
103 match self {
104 HashOrHeight::Hash(hash) => op(hash),
105 HashOrHeight::Height(height) => Some(height),
106 }
107 }
108
109 /// Unwrap the inner hash or attempt to retrieve the hash for a given
110 /// height if one exists.
111 ///
112 /// # Consensus
113 ///
114 /// In the non-finalized state, a height can have multiple valid hashes.
115 /// We typically use the hash that is currently on the best chain.
116 pub fn hash_or_else<F>(self, op: F) -> Option<block::Hash>
117 where
118 F: FnOnce(block::Height) -> Option<block::Hash>,
119 {
120 match self {
121 HashOrHeight::Hash(hash) => Some(hash),
122 HashOrHeight::Height(height) => op(height),
123 }
124 }
125
126 /// Returns the hash if this is a [`HashOrHeight::Hash`].
127 pub fn hash(&self) -> Option<block::Hash> {
128 if let HashOrHeight::Hash(hash) = self {
129 Some(*hash)
130 } else {
131 None
132 }
133 }
134
135 /// Returns the height if this is a [`HashOrHeight::Height`].
136 pub fn height(&self) -> Option<block::Height> {
137 if let HashOrHeight::Height(height) = self {
138 Some(*height)
139 } else {
140 None
141 }
142 }
143
144 /// Constructs a new [`HashOrHeight`] from a string containing a hash or a positive or negative
145 /// height.
146 ///
147 /// When the provided `hash_or_height` contains a negative height, the `tip_height` parameter
148 /// needs to be `Some` since height `-1` points to the tip.
149 pub fn new(hash_or_height: &str, tip_height: Option<block::Height>) -> Result<Self, String> {
150 hash_or_height
151 .parse()
152 .map(Self::Hash)
153 .or_else(|_| hash_or_height.parse().map(Self::Height))
154 .or_else(|_| {
155 hash_or_height
156 .parse()
157 .map_err(|_| "could not parse negative height")
158 .and_then(|d: HeightDiff| {
159 if d.is_negative() {
160 {
161 Ok(HashOrHeight::Height(
162 tip_height
163 .ok_or("missing tip height")?
164 .add(d)
165 .ok_or("underflow when adding negative height to tip")?
166 .next()
167 .map_err(|_| "height -1 needs to point to tip")?,
168 ))
169 }
170 } else {
171 Err("height was not negative")
172 }
173 })
174 })
175 .map_err(|_| {
176 "parse error: could not convert the input string to a hash or height".to_string()
177 })
178 }
179}
180
181impl std::fmt::Display for HashOrHeight {
182 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183 match self {
184 HashOrHeight::Hash(hash) => write!(f, "{hash}"),
185 HashOrHeight::Height(height) => write!(f, "{}", height.0),
186 }
187 }
188}
189
190impl From<block::Hash> for HashOrHeight {
191 fn from(hash: block::Hash) -> Self {
192 Self::Hash(hash)
193 }
194}
195
196impl From<block::Height> for HashOrHeight {
197 fn from(height: block::Height) -> Self {
198 Self::Height(height)
199 }
200}
201
202impl From<(block::Height, block::Hash)> for HashOrHeight {
203 fn from((_height, hash): (block::Height, block::Hash)) -> Self {
204 // Hash is more specific than height for the non-finalized state
205 hash.into()
206 }
207}
208
209impl From<(block::Hash, block::Height)> for HashOrHeight {
210 fn from((hash, _height): (block::Hash, block::Height)) -> Self {
211 hash.into()
212 }
213}
214
215impl std::str::FromStr for HashOrHeight {
216 type Err = SerializationError;
217
218 fn from_str(s: &str) -> Result<Self, Self::Err> {
219 s.parse()
220 .map(Self::Hash)
221 .or_else(|_| s.parse().map(Self::Height))
222 .map_err(|_| {
223 SerializationError::Parse("could not convert the input string to a hash or height")
224 })
225 }
226}
227
228/// A block which has undergone semantic validation and has been prepared for
229/// contextual validation.
230///
231/// It is the constructor's responsibility to perform semantic validation and to
232/// ensure that all fields are consistent.
233///
234/// This structure contains data from contextual validation, which is computed in
235/// the *service caller*'s task, not inside the service call itself. This allows
236/// moving work out of the single-threaded state service.
237#[derive(Clone, Debug, PartialEq, Eq)]
238pub struct SemanticallyVerifiedBlock {
239 /// The block to commit to the state.
240 pub block: Arc<Block>,
241 /// The hash of the block.
242 pub hash: block::Hash,
243 /// The height of the block.
244 pub height: block::Height,
245 /// New transparent outputs created in this block, indexed by
246 /// [`OutPoint`](transparent::OutPoint).
247 ///
248 /// Each output is tagged with its transaction index in the block.
249 /// (The outputs of earlier transactions in a block can be spent by later
250 /// transactions.)
251 ///
252 /// Note: although these transparent outputs are newly created, they may not
253 /// be unspent, since a later transaction in a block can spend outputs of an
254 /// earlier transaction.
255 ///
256 /// This field can also contain unrelated outputs, which are ignored.
257 pub new_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
258 /// A precomputed list of the hashes of the transactions in this block,
259 /// in the same order as `block.transactions`.
260 pub transaction_hashes: Arc<[transaction::Hash]>,
261}
262
263/// A block ready to be committed directly to the finalized state with
264/// a small number of checks if compared with a `ContextuallyVerifiedBlock`.
265///
266/// This is exposed for use in checkpointing.
267///
268/// Note: The difference between a `CheckpointVerifiedBlock` and a `ContextuallyVerifiedBlock` is
269/// that the `CheckpointVerifier` doesn't bind the transaction authorizing data to the
270/// `ChainHistoryBlockTxAuthCommitmentHash`, but the `NonFinalizedState` and `FinalizedState` do.
271#[derive(Clone, Debug, PartialEq, Eq)]
272pub struct CheckpointVerifiedBlock(pub(crate) SemanticallyVerifiedBlock);
273
274// Some fields are pub(crate), so we can add whatever db-format-dependent
275// precomputation we want here without leaking internal details.
276
277/// A contextually verified block, ready to be committed directly to the finalized state with no
278/// checks, if it becomes the root of the best non-finalized chain.
279///
280/// Used by the state service and non-finalized `Chain`.
281///
282/// Note: The difference between a `CheckpointVerifiedBlock` and a `ContextuallyVerifiedBlock` is
283/// that the `CheckpointVerifier` doesn't bind the transaction authorizing data to the
284/// `ChainHistoryBlockTxAuthCommitmentHash`, but the `NonFinalizedState` and `FinalizedState` do.
285#[derive(Clone, Debug, PartialEq, Eq)]
286pub struct ContextuallyVerifiedBlock {
287 /// The block to commit to the state.
288 pub(crate) block: Arc<Block>,
289
290 /// The hash of the block.
291 pub(crate) hash: block::Hash,
292
293 /// The height of the block.
294 pub(crate) height: block::Height,
295
296 /// New transparent outputs created in this block, indexed by
297 /// [`OutPoint`](transparent::OutPoint).
298 ///
299 /// Note: although these transparent outputs are newly created, they may not
300 /// be unspent, since a later transaction in a block can spend outputs of an
301 /// earlier transaction.
302 ///
303 /// This field can also contain unrelated outputs, which are ignored.
304 pub(crate) new_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
305
306 /// The outputs spent by this block, indexed by the [`transparent::Input`]'s
307 /// [`OutPoint`](transparent::OutPoint).
308 ///
309 /// Note: these inputs can come from earlier transactions in this block,
310 /// or earlier blocks in the chain.
311 ///
312 /// This field can also contain unrelated outputs, which are ignored.
313 pub(crate) spent_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
314
315 /// A precomputed list of the hashes of the transactions in this block,
316 /// in the same order as `block.transactions`.
317 pub(crate) transaction_hashes: Arc<[transaction::Hash]>,
318
319 /// The sum of the chain value pool changes of all transactions in this block.
320 pub(crate) chain_value_pool_change: ValueBalance<NegativeAllowed>,
321}
322
323/// Wraps note commitment trees and the history tree together.
324///
325/// The default instance represents the treestate that corresponds to the genesis block.
326#[derive(Clone, Debug, Default, Eq, PartialEq)]
327pub struct Treestate {
328 /// Note commitment trees.
329 pub note_commitment_trees: NoteCommitmentTrees,
330 /// History tree.
331 pub history_tree: Arc<HistoryTree>,
332}
333
334impl Treestate {
335 #[allow(missing_docs)]
336 pub(crate) fn new(
337 sprout: Arc<sprout::tree::NoteCommitmentTree>,
338 sapling: Arc<sapling::tree::NoteCommitmentTree>,
339 orchard: Arc<orchard::tree::NoteCommitmentTree>,
340 sapling_subtree: Option<NoteCommitmentSubtree<sapling_crypto::Node>>,
341 orchard_subtree: Option<NoteCommitmentSubtree<orchard::tree::Node>>,
342 history_tree: Arc<HistoryTree>,
343 ) -> Self {
344 Self {
345 note_commitment_trees: NoteCommitmentTrees {
346 sprout,
347 sapling,
348 sapling_subtree,
349 orchard,
350 orchard_subtree,
351 },
352 history_tree,
353 }
354 }
355}
356
357/// Contains a block ready to be committed.
358///
359/// Zebra's state service passes this `enum` over to the finalized state
360/// when committing a block.
361#[allow(missing_docs, clippy::large_enum_variant)]
362pub enum FinalizableBlock {
363 Checkpoint {
364 checkpoint_verified: CheckpointVerifiedBlock,
365 },
366 Contextual {
367 contextually_verified: ContextuallyVerifiedBlock,
368 treestate: Treestate,
369 },
370}
371
372/// Contains a block with all its associated data that the finalized state can commit to its
373/// database.
374///
375/// Note that it's the constructor's responsibility to ensure that all data is valid and verified.
376pub struct FinalizedBlock {
377 /// The block to commit to the state.
378 pub(super) block: Arc<Block>,
379 /// The hash of the block.
380 pub(super) hash: block::Hash,
381 /// The height of the block.
382 pub(super) height: block::Height,
383 /// New transparent outputs created in this block, indexed by
384 /// [`OutPoint`](transparent::OutPoint).
385 pub(super) new_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
386 /// A precomputed list of the hashes of the transactions in this block, in the same order as
387 /// `block.transactions`.
388 pub(super) transaction_hashes: Arc<[transaction::Hash]>,
389 /// The tresstate associated with the block.
390 pub(super) treestate: Treestate,
391 /// This block's deferred pool value balance change.
392 pub(super) deferred_pool_balance_change: DeferredPoolBalanceChange,
393}
394
395impl FinalizedBlock {
396 /// Constructs [`FinalizedBlock`] from [`CheckpointVerifiedBlock`] and its [`Treestate`].
397 pub fn from_checkpoint_verified(
398 block: CheckpointVerifiedBlock,
399 treestate: Treestate,
400 deferred_pool_balance_change: DeferredPoolBalanceChange,
401 ) -> Self {
402 Self::from_semantically_verified(
403 SemanticallyVerifiedBlock::from(block),
404 treestate,
405 deferred_pool_balance_change,
406 )
407 }
408
409 /// Constructs [`FinalizedBlock`] from [`ContextuallyVerifiedBlock`] and its [`Treestate`].
410 pub fn from_contextually_verified(
411 block: ContextuallyVerifiedBlock,
412 treestate: Treestate,
413 deferred_pool_balance_change: DeferredPoolBalanceChange,
414 ) -> Self {
415 Self::from_semantically_verified(
416 SemanticallyVerifiedBlock::from(block),
417 treestate,
418 deferred_pool_balance_change,
419 )
420 }
421
422 /// Constructs [`FinalizedBlock`] from [`SemanticallyVerifiedBlock`] and its [`Treestate`].
423 fn from_semantically_verified(
424 block: SemanticallyVerifiedBlock,
425 treestate: Treestate,
426 deferred_pool_balance_change: DeferredPoolBalanceChange,
427 ) -> Self {
428 Self {
429 block: block.block,
430 hash: block.hash,
431 height: block.height,
432 new_outputs: block.new_outputs,
433 transaction_hashes: block.transaction_hashes,
434 treestate,
435 deferred_pool_balance_change,
436 }
437 }
438}
439
440impl FinalizableBlock {
441 /// Create a new [`FinalizableBlock`] given a [`ContextuallyVerifiedBlock`].
442 pub fn new(contextually_verified: ContextuallyVerifiedBlock, treestate: Treestate) -> Self {
443 Self::Contextual {
444 contextually_verified,
445 treestate,
446 }
447 }
448
449 #[cfg(test)]
450 /// Extract a [`Block`] from a [`FinalizableBlock`] variant.
451 pub fn inner_block(&self) -> Arc<Block> {
452 match self {
453 FinalizableBlock::Checkpoint {
454 checkpoint_verified,
455 } => checkpoint_verified.block.clone(),
456 FinalizableBlock::Contextual {
457 contextually_verified,
458 ..
459 } => contextually_verified.block.clone(),
460 }
461 }
462}
463
464impl From<CheckpointVerifiedBlock> for FinalizableBlock {
465 fn from(checkpoint_verified: CheckpointVerifiedBlock) -> Self {
466 Self::Checkpoint {
467 checkpoint_verified,
468 }
469 }
470}
471
472impl From<Arc<Block>> for FinalizableBlock {
473 fn from(block: Arc<Block>) -> Self {
474 Self::from(CheckpointVerifiedBlock::from(block))
475 }
476}
477
478impl From<&SemanticallyVerifiedBlock> for SemanticallyVerifiedBlock {
479 fn from(semantically_verified: &SemanticallyVerifiedBlock) -> Self {
480 semantically_verified.clone()
481 }
482}
483
484// Doing precomputation in these impls means that it will be done in
485// the *service caller*'s task, not inside the service call itself.
486// This allows moving work out of the single-threaded state service.
487
488impl ContextuallyVerifiedBlock {
489 /// Create a block that's ready for non-finalized `Chain` contextual validation,
490 /// using a [`SemanticallyVerifiedBlock`] and the UTXOs it spends.
491 ///
492 /// When combined, `semantically_verified.new_outputs` and `spent_utxos` must contain
493 /// the [`Utxo`](transparent::Utxo)s spent by every transparent input in this block,
494 /// including UTXOs created by earlier transactions in this block.
495 ///
496 /// Note: a [`ContextuallyVerifiedBlock`] isn't actually contextually valid until
497 /// [`Chain::push()`](crate::service::non_finalized_state::Chain::push) returns success.
498 pub fn with_block_and_spent_utxos(
499 semantically_verified: SemanticallyVerifiedBlock,
500 mut spent_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
501 deferred_pool_balance_change: DeferredPoolBalanceChange,
502 ) -> Result<Self, ValueBalanceError> {
503 let SemanticallyVerifiedBlock {
504 block,
505 hash,
506 height,
507 new_outputs,
508 transaction_hashes,
509 } = semantically_verified;
510
511 // This is redundant for the non-finalized state,
512 // but useful to make some tests pass more easily.
513 //
514 // TODO: fix the tests, and stop adding unrelated outputs.
515 spent_outputs.extend(new_outputs.clone());
516
517 Ok(Self {
518 block: block.clone(),
519 hash,
520 height,
521 new_outputs,
522 spent_outputs: spent_outputs.clone(),
523 transaction_hashes,
524 chain_value_pool_change: block.chain_value_pool_change(
525 &utxos_from_ordered_utxos(spent_outputs),
526 deferred_pool_balance_change,
527 )?,
528 })
529 }
530}
531
532impl CheckpointVerifiedBlock {
533 /// Creates a [`CheckpointVerifiedBlock`] from [`Block`] with optional deferred balance and
534 /// optional pre-computed hash.
535 pub fn new(block: Arc<Block>, hash: Option<block::Hash>) -> Self {
536 Self::with_hash(block.clone(), hash.unwrap_or(block.hash()))
537 }
538 /// Creates a block that's ready to be committed to the finalized state,
539 /// using a precalculated [`block::Hash`].
540 ///
541 /// Note: a [`CheckpointVerifiedBlock`] isn't actually finalized
542 /// until [`Request::CommitCheckpointVerifiedBlock`] returns success.
543 pub fn with_hash(block: Arc<Block>, hash: block::Hash) -> Self {
544 Self(SemanticallyVerifiedBlock::with_hash(block, hash))
545 }
546}
547
548impl SemanticallyVerifiedBlock {
549 /// Creates [`SemanticallyVerifiedBlock`] from [`Block`] and [`block::Hash`].
550 pub fn with_hash(block: Arc<Block>, hash: block::Hash) -> Self {
551 let height = block
552 .coinbase_height()
553 .expect("semantically verified block should have a coinbase height");
554 let transaction_hashes: Arc<[_]> = block.transactions.iter().map(|tx| tx.hash()).collect();
555 let new_outputs = transparent::new_ordered_outputs(&block, &transaction_hashes);
556
557 Self {
558 block,
559 hash,
560 height,
561 new_outputs,
562 transaction_hashes,
563 }
564 }
565}
566
567impl From<Arc<Block>> for CheckpointVerifiedBlock {
568 fn from(block: Arc<Block>) -> Self {
569 CheckpointVerifiedBlock(SemanticallyVerifiedBlock::from(block))
570 }
571}
572
573impl From<Arc<Block>> for SemanticallyVerifiedBlock {
574 fn from(block: Arc<Block>) -> Self {
575 let hash = block.hash();
576 let height = block
577 .coinbase_height()
578 .expect("semantically verified block should have a coinbase height");
579 let transaction_hashes: Arc<[_]> = block.transactions.iter().map(|tx| tx.hash()).collect();
580 let new_outputs = transparent::new_ordered_outputs(&block, &transaction_hashes);
581
582 Self {
583 block,
584 hash,
585 height,
586 new_outputs,
587 transaction_hashes,
588 }
589 }
590}
591
592impl From<ContextuallyVerifiedBlock> for SemanticallyVerifiedBlock {
593 fn from(valid: ContextuallyVerifiedBlock) -> Self {
594 Self {
595 block: valid.block,
596 hash: valid.hash,
597 height: valid.height,
598 new_outputs: valid.new_outputs,
599 transaction_hashes: valid.transaction_hashes,
600 }
601 }
602}
603
604impl From<FinalizedBlock> for SemanticallyVerifiedBlock {
605 fn from(finalized: FinalizedBlock) -> Self {
606 Self {
607 block: finalized.block,
608 hash: finalized.hash,
609 height: finalized.height,
610 new_outputs: finalized.new_outputs,
611 transaction_hashes: finalized.transaction_hashes,
612 }
613 }
614}
615
616impl From<CheckpointVerifiedBlock> for SemanticallyVerifiedBlock {
617 fn from(checkpoint_verified: CheckpointVerifiedBlock) -> Self {
618 checkpoint_verified.0
619 }
620}
621
622impl Deref for CheckpointVerifiedBlock {
623 type Target = SemanticallyVerifiedBlock;
624
625 fn deref(&self) -> &Self::Target {
626 &self.0
627 }
628}
629impl DerefMut for CheckpointVerifiedBlock {
630 fn deref_mut(&mut self) -> &mut Self::Target {
631 &mut self.0
632 }
633}
634
635/// Helper trait for convenient access to expected response and error types.
636pub trait MappedRequest: Sized + Send + 'static {
637 /// Expected response type for this state request.
638 type MappedResponse;
639 /// Expected error type for this state request.
640 type Error: std::error::Error + std::fmt::Display + 'static;
641
642 /// Maps the request type to a [`Request`].
643 fn map_request(self) -> Request;
644
645 /// Maps the expected [`Response`] variant for this request to the mapped response type.
646 fn map_response(response: Response) -> Self::MappedResponse;
647
648 /// Accepts a state service to call, maps this request to a [`Request`], waits for the state to be ready,
649 /// calls the state with the mapped request, then maps the success or error response to the expected response
650 /// or error type for this request.
651 ///
652 /// Returns a [`Result<MappedResponse, LayeredServicesError<RequestError>>`].
653 #[allow(async_fn_in_trait)]
654 async fn mapped_oneshot<State>(
655 self,
656 state: &mut State,
657 ) -> Result<Self::MappedResponse, LayeredStateError<Self::Error>>
658 where
659 State: Service<Request, Response = Response, Error = BoxError>,
660 State::Future: Send,
661 {
662 let response = state.ready().await?.call(self.map_request()).await?;
663 Ok(Self::map_response(response))
664 }
665}
666
667/// Performs contextual validation of the given semantically verified block,
668/// committing it to the state if successful.
669///
670/// See the [`crate`] documentation and [`Request::CommitSemanticallyVerifiedBlock`] for details.
671pub struct CommitSemanticallyVerifiedBlockRequest(pub SemanticallyVerifiedBlock);
672
673impl MappedRequest for CommitSemanticallyVerifiedBlockRequest {
674 type MappedResponse = block::Hash;
675 type Error = CommitSemanticallyVerifiedError;
676
677 fn map_request(self) -> Request {
678 Request::CommitSemanticallyVerifiedBlock(self.0)
679 }
680
681 fn map_response(response: Response) -> Self::MappedResponse {
682 match response {
683 Response::Committed(hash) => hash,
684 _ => unreachable!("wrong response variant for request"),
685 }
686 }
687}
688
689/// Commit a checkpointed block to the state
690///
691/// See the [`crate`] documentation and [`Request::CommitCheckpointVerifiedBlock`] for details.
692#[allow(dead_code)]
693pub struct CommitCheckpointVerifiedBlockRequest(pub CheckpointVerifiedBlock);
694
695impl MappedRequest for CommitCheckpointVerifiedBlockRequest {
696 type MappedResponse = block::Hash;
697 type Error = CommitCheckpointVerifiedError;
698
699 fn map_request(self) -> Request {
700 Request::CommitCheckpointVerifiedBlock(self.0)
701 }
702
703 fn map_response(response: Response) -> Self::MappedResponse {
704 match response {
705 Response::Committed(hash) => hash,
706 _ => unreachable!("wrong response variant for request"),
707 }
708 }
709}
710
711/// Request to invalidate a block in the state.
712///
713/// See the [`crate`] documentation and [`Request::InvalidateBlock`] for details.
714#[allow(dead_code)]
715pub struct InvalidateBlockRequest(pub block::Hash);
716
717impl MappedRequest for InvalidateBlockRequest {
718 type MappedResponse = block::Hash;
719 type Error = InvalidateError;
720
721 fn map_request(self) -> Request {
722 Request::InvalidateBlock(self.0)
723 }
724
725 fn map_response(response: Response) -> Self::MappedResponse {
726 match response {
727 Response::Invalidated(hash) => hash,
728 _ => unreachable!("wrong response variant for request"),
729 }
730 }
731}
732
733/// Request to reconsider a previously invalidated block and re-commit it to the state.
734///
735/// See the [`crate`] documentation and [`Request::ReconsiderBlock`] for details.
736#[allow(dead_code)]
737pub struct ReconsiderBlockRequest(pub block::Hash);
738
739impl MappedRequest for ReconsiderBlockRequest {
740 type MappedResponse = Vec<block::Hash>;
741 type Error = ReconsiderError;
742
743 fn map_request(self) -> Request {
744 Request::ReconsiderBlock(self.0)
745 }
746
747 fn map_response(response: Response) -> Self::MappedResponse {
748 match response {
749 Response::Reconsidered(hashes) => hashes,
750 _ => unreachable!("wrong response variant for request"),
751 }
752 }
753}
754
755#[derive(Clone, Debug, PartialEq, Eq)]
756/// A query about or modification to the chain state, via the
757/// [`StateService`](crate::service::StateService).
758pub enum Request {
759 /// Performs contextual validation of the given semantically verified block,
760 /// committing it to the state if successful.
761 ///
762 /// This request can be made out-of-order; the state service will queue it
763 /// until its parent is ready.
764 ///
765 /// Returns [`Response::Committed`] with the hash of the block when it is
766 /// committed to the state, or a [`CommitSemanticallyVerifiedError`][0] if
767 /// the block fails contextual validation or otherwise could not be committed.
768 ///
769 /// This request cannot be cancelled once submitted; dropping the response
770 /// future will have no effect on whether it is eventually processed. A
771 /// request to commit a block which has been queued internally but not yet
772 /// committed will fail the older request and replace it with the newer request.
773 ///
774 /// # Correctness
775 ///
776 /// Block commit requests should be wrapped in a timeout, so that
777 /// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
778 /// documentation for details.
779 ///
780 /// [0]: (crate::error::CommitSemanticallyVerifiedError)
781 CommitSemanticallyVerifiedBlock(SemanticallyVerifiedBlock),
782
783 /// Commit a checkpointed block to the state, skipping most but not all
784 /// contextual validation.
785 ///
786 /// This is exposed for use in checkpointing, which produces checkpoint vefified
787 /// blocks. This request can be made out-of-order; the state service will queue
788 /// it until its parent is ready.
789 ///
790 /// Returns [`Response::Committed`] with the hash of the newly committed
791 /// block, or a [`CommitCheckpointVerifiedError`][0] if the block could not be
792 /// committed to the state.
793 ///
794 /// This request cannot be cancelled once submitted; dropping the response
795 /// future will have no effect on whether it is eventually processed.
796 /// Duplicate requests will replace the older duplicate, and return an error
797 /// in its response future.
798 ///
799 /// # Note
800 ///
801 /// [`SemanticallyVerifiedBlock`], [`ContextuallyVerifiedBlock`] and
802 /// [`CheckpointVerifiedBlock`] are an internal Zebra implementation detail.
803 /// There is no difference between these blocks on the Zcash network, or in Zebra's
804 /// network or syncer implementations.
805 ///
806 /// # Consensus
807 ///
808 /// Checkpointing is allowed under the Zcash "social consensus" rules.
809 /// Zebra checkpoints both settled network upgrades, and blocks past the rollback limit.
810 /// (By the time Zebra release is tagged, its final checkpoint is typically hours or days old.)
811 ///
812 /// > A network upgrade is settled on a given network when there is a social consensus
813 /// > that it has activated with a given activation block hash. A full validator that
814 /// > potentially risks Mainnet funds or displays Mainnet transaction information to a user
815 /// > MUST do so only for a block chain that includes the activation block of the most
816 /// > recent settled network upgrade, with the corresponding activation block hash.
817 /// > ...
818 /// > A full validator MAY impose a limit on the number of blocks it will “roll back”
819 /// > when switching from one best valid block chain to another that is not a descendent.
820 /// > For `zcashd` and `zebra` this limit is 100 blocks.
821 ///
822 /// <https://zips.z.cash/protocol/protocol.pdf#blockchain>
823 ///
824 /// # Correctness
825 ///
826 /// Block commit requests should be wrapped in a timeout, so that
827 /// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
828 /// documentation for details.
829 ///
830 /// [0]: (crate::error::CommitCheckpointVerifiedError)
831 CommitCheckpointVerifiedBlock(CheckpointVerifiedBlock),
832
833 /// Computes the depth in the current best chain of the block identified by the given hash.
834 ///
835 /// Returns
836 ///
837 /// * [`Response::Depth(Some(depth))`](Response::Depth) if the block is in the best chain;
838 /// * [`Response::Depth(None)`](Response::Depth) otherwise.
839 Depth(block::Hash),
840
841 /// Returns [`Response::Tip(Option<(Height, block::Hash)>)`](Response::Tip)
842 /// with the current best chain tip.
843 Tip,
844
845 /// Computes a block locator object based on the current best chain.
846 ///
847 /// Returns [`Response::BlockLocator`] with hashes starting
848 /// from the best chain tip, and following the chain of previous
849 /// hashes. The first hash is the best chain tip. The last hash is
850 /// the tip of the finalized portion of the state. Block locators
851 /// are not continuous - some intermediate hashes might be skipped.
852 ///
853 /// If the state is empty, the block locator is also empty.
854 BlockLocator,
855
856 /// Looks up a transaction by hash in the current best chain.
857 ///
858 /// Returns
859 ///
860 /// * [`Response::Transaction(Some(Arc<Transaction>))`](Response::Transaction) if the transaction is in the best chain;
861 /// * [`Response::Transaction(None)`](Response::Transaction) otherwise.
862 Transaction(transaction::Hash),
863
864 /// Looks up a transaction by hash in any chain.
865 ///
866 /// Returns
867 ///
868 /// * [`Response::AnyChainTransaction(Some(AnyTx))`](Response::AnyChainTransaction)
869 /// if the transaction is in any chain;
870 /// * [`Response::AnyChainTransaction(None)`](Response::AnyChainTransaction)
871 /// otherwise.
872 AnyChainTransaction(transaction::Hash),
873
874 /// Looks up a UTXO identified by the given [`OutPoint`](transparent::OutPoint),
875 /// returning `None` immediately if it is unknown.
876 ///
877 /// Checks verified blocks in the finalized chain and the _best_ non-finalized chain.
878 UnspentBestChainUtxo(transparent::OutPoint),
879
880 /// Looks up a block by hash or height in the current best chain.
881 ///
882 /// Returns
883 ///
884 /// * [`Response::Block(Some(Arc<Block>))`](Response::Block) if the block is in the best chain;
885 /// * [`Response::Block(None)`](Response::Block) otherwise.
886 ///
887 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
888 /// [`block::Height`] using `.into()`.
889 Block(HashOrHeight),
890
891 /// Looks up a block by hash in any current chain or by height in the current best chain.
892 ///
893 /// Returns
894 ///
895 /// * [`Response::Block(Some(Arc<Block>))`](Response::Block) if the block hash is in any chain, or,
896 /// if the block height is in the best chain;
897 /// * [`Response::Block(None)`](Response::Block) otherwise.
898 ///
899 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
900 /// [`block::Height`] using `.into()`.
901 AnyChainBlock(HashOrHeight),
902
903 //// Same as Block, but also returns serialized block size.
904 ////
905 /// Returns
906 ///
907 /// * [`ReadResponse::BlockAndSize(Some((Arc<Block>, usize)))`](ReadResponse::BlockAndSize) if the block is in the best chain;
908 /// * [`ReadResponse::BlockAndSize(None)`](ReadResponse::BlockAndSize) otherwise.
909 BlockAndSize(HashOrHeight),
910
911 /// Looks up a block header by hash or height in the current best chain.
912 ///
913 /// Returns
914 ///
915 /// [`Response::BlockHeader(block::Header)`](Response::BlockHeader).
916 ///
917 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
918 /// [`block::Height`] using `.into()`.
919 BlockHeader(HashOrHeight),
920
921 /// Request a UTXO identified by the given [`OutPoint`](transparent::OutPoint),
922 /// waiting until it becomes available if it is unknown.
923 ///
924 /// Checks the finalized chain, all non-finalized chains, queued unverified blocks,
925 /// and any blocks that arrive at the state after the request future has been created.
926 ///
927 /// This request is purely informational, and there are no guarantees about
928 /// whether the UTXO remains unspent or is on the best chain, or any chain.
929 /// Its purpose is to allow asynchronous script verification or to wait until
930 /// the UTXO arrives in the state before validating dependent transactions.
931 ///
932 /// # Correctness
933 ///
934 /// UTXO requests should be wrapped in a timeout, so that
935 /// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
936 /// documentation for details.
937 ///
938 /// Outdated requests are pruned on a regular basis.
939 AwaitUtxo(transparent::OutPoint),
940
941 /// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
942 /// Returns a list of hashes that follow that intersection, from the best chain.
943 ///
944 /// If there is no matching hash in the best chain, starts from the genesis hash.
945 ///
946 /// Stops the list of hashes after:
947 /// * adding the best tip,
948 /// * adding the `stop` hash to the list, if it is in the best chain, or
949 /// * adding 500 hashes to the list.
950 ///
951 /// Returns an empty list if the state is empty.
952 ///
953 /// Returns
954 ///
955 /// [`Response::BlockHashes(Vec<block::Hash>)`](Response::BlockHashes).
956 /// See <https://en.bitcoin.it/wiki/Protocol_documentation#getblocks>
957 FindBlockHashes {
958 /// Hashes of known blocks, ordered from highest height to lowest height.
959 known_blocks: Vec<block::Hash>,
960 /// Optionally, the last block hash to request.
961 stop: Option<block::Hash>,
962 },
963
964 /// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
965 /// Returns a list of headers that follow that intersection, from the best chain.
966 ///
967 /// If there is no matching hash in the best chain, starts from the genesis header.
968 ///
969 /// Stops the list of headers after:
970 /// * adding the best tip,
971 /// * adding the header matching the `stop` hash to the list, if it is in the best chain, or
972 /// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS`] headers to the list.
973 ///
974 /// Returns an empty list if the state is empty.
975 ///
976 /// Returns
977 ///
978 /// [`Response::BlockHeaders(Vec<block::Header>)`](Response::BlockHeaders).
979 /// See <https://en.bitcoin.it/wiki/Protocol_documentation#getheaders>
980 FindBlockHeaders {
981 /// Hashes of known blocks, ordered from highest height to lowest height.
982 known_blocks: Vec<block::Hash>,
983 /// Optionally, the hash of the last header to request.
984 stop: Option<block::Hash>,
985 },
986
987 /// Contextually validates anchors and nullifiers of a transaction on the best chain
988 ///
989 /// Returns [`Response::ValidBestChainTipNullifiersAndAnchors`]
990 CheckBestChainTipNullifiersAndAnchors(UnminedTx),
991
992 /// Calculates the median-time-past for the *next* block on the best chain.
993 ///
994 /// Returns [`Response::BestChainNextMedianTimePast`] when successful.
995 BestChainNextMedianTimePast,
996
997 /// Looks up a block hash by height in the current best chain.
998 ///
999 /// Returns
1000 ///
1001 /// * [`Response::BlockHash(Some(hash))`](Response::BlockHash) if the block is in the best chain;
1002 /// * [`Response::BlockHash(None)`](Response::BlockHash) otherwise.
1003 BestChainBlockHash(block::Height),
1004
1005 /// Checks if a block is present anywhere in the state service.
1006 /// Looks up `hash` in block queues as well as the finalized chain and all non-finalized chains.
1007 ///
1008 /// Returns [`Response::KnownBlock(Some(Location))`](Response::KnownBlock) if the block is in the best state service.
1009 /// Returns [`Response::KnownBlock(None)`](Response::KnownBlock) otherwise.
1010 KnownBlock(block::Hash),
1011
1012 /// Invalidates a block in the non-finalized state with the provided hash if one is present, removing it and
1013 /// its child blocks, and rejecting it during contextual validation if it's resubmitted to the state.
1014 ///
1015 /// Returns [`Response::Invalidated`] with the hash of the invalidated block,
1016 /// or a [`InvalidateError`][0] if the block was not found, the state is still
1017 /// committing checkpointed blocks, or the request could not be processed.
1018 ///
1019 /// [0]: (crate::error::InvalidateError)
1020 InvalidateBlock(block::Hash),
1021
1022 /// Reconsiders a previously invalidated block in the non-finalized state with the provided hash if one is present.
1023 ///
1024 /// Returns [`Response::Reconsidered`] with the hash of the reconsidered block,
1025 /// or a [`ReconsiderError`][0] if the block was not previously invalidated,
1026 /// its parent chain is missing, or the state is not ready to process the request.
1027 ///
1028 /// [0]: (crate::error::ReconsiderError)
1029 ReconsiderBlock(block::Hash),
1030
1031 /// Performs contextual validation of the given block, but does not commit it to the state.
1032 ///
1033 /// Returns [`Response::ValidBlockProposal`] when successful.
1034 /// See `[ReadRequest::CheckBlockProposalValidity]` for details.
1035 CheckBlockProposalValidity(SemanticallyVerifiedBlock),
1036}
1037
1038impl Request {
1039 /// Returns a [`&'static str`](str) name of the variant representing this value.
1040 pub fn variant_name(&self) -> &'static str {
1041 match self {
1042 Request::CommitSemanticallyVerifiedBlock(_) => "commit_semantically_verified_block",
1043 Request::CommitCheckpointVerifiedBlock(_) => "commit_checkpoint_verified_block",
1044 Request::AwaitUtxo(_) => "await_utxo",
1045 Request::Depth(_) => "depth",
1046 Request::Tip => "tip",
1047 Request::BlockLocator => "block_locator",
1048 Request::Transaction(_) => "transaction",
1049 Request::AnyChainTransaction(_) => "any_chain_transaction",
1050 Request::UnspentBestChainUtxo { .. } => "unspent_best_chain_utxo",
1051 Request::Block(_) => "block",
1052 Request::AnyChainBlock(_) => "any_chain_block",
1053 Request::BlockAndSize(_) => "block_and_size",
1054 Request::BlockHeader(_) => "block_header",
1055 Request::FindBlockHashes { .. } => "find_block_hashes",
1056 Request::FindBlockHeaders { .. } => "find_block_headers",
1057 Request::CheckBestChainTipNullifiersAndAnchors(_) => {
1058 "best_chain_tip_nullifiers_anchors"
1059 }
1060 Request::BestChainNextMedianTimePast => "best_chain_next_median_time_past",
1061 Request::BestChainBlockHash(_) => "best_chain_block_hash",
1062 Request::KnownBlock(_) => "known_block",
1063 Request::InvalidateBlock(_) => "invalidate_block",
1064 Request::ReconsiderBlock(_) => "reconsider_block",
1065 Request::CheckBlockProposalValidity(_) => "check_block_proposal_validity",
1066 }
1067 }
1068
1069 /// Counts metric for StateService call
1070 pub fn count_metric(&self) {
1071 metrics::counter!(
1072 "state.requests",
1073 "service" => "state",
1074 "type" => self.variant_name()
1075 )
1076 .increment(1);
1077 }
1078}
1079
1080#[derive(Clone, Debug, PartialEq, Eq)]
1081/// A read-only query about the chain state, via the
1082/// [`ReadStateService`](crate::service::ReadStateService).
1083pub enum ReadRequest {
1084 /// Returns [`ReadResponse::UsageInfo(num_bytes: u64)`](ReadResponse::UsageInfo)
1085 /// with the current disk space usage in bytes.
1086 UsageInfo,
1087
1088 /// Returns [`ReadResponse::Tip(Option<(Height, block::Hash)>)`](ReadResponse::Tip)
1089 /// with the current best chain tip.
1090 Tip,
1091
1092 /// Returns [`ReadResponse::TipPoolValues(Option<(Height, block::Hash, ValueBalance)>)`](ReadResponse::TipPoolValues)
1093 /// with the pool values of the current best chain tip.
1094 TipPoolValues,
1095
1096 /// Looks up the block info after a block by hash or height in the current best chain.
1097 ///
1098 /// * [`ReadResponse::BlockInfo(Some(pool_values))`](ReadResponse::BlockInfo) if the block is in the best chain;
1099 /// * [`ReadResponse::BlockInfo(None)`](ReadResponse::BlockInfo) otherwise.
1100 BlockInfo(HashOrHeight),
1101
1102 /// Computes the depth in the current best chain of the block identified by the given hash.
1103 ///
1104 /// Returns
1105 ///
1106 /// * [`ReadResponse::Depth(Some(depth))`](ReadResponse::Depth) if the block is in the best chain;
1107 /// * [`ReadResponse::Depth(None)`](ReadResponse::Depth) otherwise.
1108 Depth(block::Hash),
1109
1110 /// Looks up a block by hash or height in the current best chain.
1111 ///
1112 /// Returns
1113 ///
1114 /// * [`ReadResponse::Block(Some(Arc<Block>))`](ReadResponse::Block) if the block is in the best chain;
1115 /// * [`ReadResponse::Block(None)`](ReadResponse::Block) otherwise.
1116 ///
1117 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
1118 /// [`block::Height`] using `.into()`.
1119 Block(HashOrHeight),
1120
1121 /// Looks up a block by hash in any current chain or by height in the current best chain.
1122 ///
1123 /// Returns
1124 ///
1125 /// * [`ReadResponse::Block(Some(Arc<Block>))`](ReadResponse::Block) if the block hash is in any chain, or
1126 /// if the block height is in any chain, checking the best chain first
1127 /// followed by side chains in order from most to least work.
1128 /// * [`ReadResponse::Block(None)`](ReadResponse::Block) otherwise.
1129 ///
1130 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
1131 /// [`block::Height`] using `.into()`.
1132 AnyChainBlock(HashOrHeight),
1133
1134 //// Same as Block, but also returns serialized block size.
1135 ////
1136 /// Returns
1137 ///
1138 /// * [`ReadResponse::BlockAndSize(Some((Arc<Block>, usize)))`](ReadResponse::BlockAndSize) if the block is in the best chain;
1139 /// * [`ReadResponse::BlockAndSize(None)`](ReadResponse::BlockAndSize) otherwise.
1140 BlockAndSize(HashOrHeight),
1141
1142 /// Looks up a block header by hash or height in the current best chain.
1143 ///
1144 /// Returns
1145 ///
1146 /// [`Response::BlockHeader(block::Header)`](Response::BlockHeader).
1147 ///
1148 /// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
1149 /// [`block::Height`] using `.into()`.
1150 BlockHeader(HashOrHeight),
1151
1152 /// Looks up a transaction by hash in the current best chain.
1153 ///
1154 /// Returns
1155 ///
1156 /// * [`ReadResponse::Transaction(Some(Arc<Transaction>))`](ReadResponse::Transaction) if the transaction is in the best chain;
1157 /// * [`ReadResponse::Transaction(None)`](ReadResponse::Transaction) otherwise.
1158 Transaction(transaction::Hash),
1159
1160 /// Looks up a transaction by hash in any chain.
1161 ///
1162 /// Returns
1163 ///
1164 /// * [`ReadResponse::AnyChainTransaction(Some(AnyTx))`](ReadResponse::AnyChainTransaction)
1165 /// if the transaction is in any chain;
1166 /// * [`ReadResponse::AnyChainTransaction(None)`](ReadResponse::AnyChainTransaction)
1167 /// otherwise.
1168 AnyChainTransaction(transaction::Hash),
1169
1170 /// Looks up the transaction IDs for a block, using a block hash or height.
1171 ///
1172 /// Returns
1173 ///
1174 /// * An ordered list of transaction hashes, or
1175 /// * `None` if the block was not found.
1176 ///
1177 /// Note: Each block has at least one transaction: the coinbase transaction.
1178 ///
1179 /// Returned txids are in the order they appear in the block.
1180 TransactionIdsForBlock(HashOrHeight),
1181
1182 /// Looks up the transaction IDs for a block, using a block hash or height,
1183 /// for any chain.
1184 ///
1185 /// Returns
1186 ///
1187 /// * An ordered list of transaction hashes and a flag indicating whether
1188 /// the block is in the best chain, or
1189 /// * `None` if the block was not found.
1190 ///
1191 /// Note: Each block has at least one transaction: the coinbase transaction.
1192 ///
1193 /// Returned txids are in the order they appear in the block.
1194 AnyChainTransactionIdsForBlock(HashOrHeight),
1195
1196 /// Looks up a UTXO identified by the given [`OutPoint`](transparent::OutPoint),
1197 /// returning `None` immediately if it is unknown.
1198 ///
1199 /// Checks verified blocks in the finalized chain and the _best_ non-finalized chain.
1200 UnspentBestChainUtxo(transparent::OutPoint),
1201
1202 /// Looks up a UTXO identified by the given [`OutPoint`](transparent::OutPoint),
1203 /// returning `None` immediately if it is unknown.
1204 ///
1205 /// Checks verified blocks in the finalized chain and _all_ non-finalized chains.
1206 ///
1207 /// This request is purely informational, there is no guarantee that
1208 /// the UTXO remains unspent in the best chain.
1209 AnyChainUtxo(transparent::OutPoint),
1210
1211 /// Computes a block locator object based on the current best chain.
1212 ///
1213 /// Returns [`ReadResponse::BlockLocator`] with hashes starting
1214 /// from the best chain tip, and following the chain of previous
1215 /// hashes. The first hash is the best chain tip. The last hash is
1216 /// the tip of the finalized portion of the state. Block locators
1217 /// are not continuous - some intermediate hashes might be skipped.
1218 ///
1219 /// If the state is empty, the block locator is also empty.
1220 BlockLocator,
1221
1222 /// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
1223 /// Returns a list of hashes that follow that intersection, from the best chain.
1224 ///
1225 /// If there is no matching hash in the best chain, starts from the genesis hash.
1226 ///
1227 /// Stops the list of hashes after:
1228 /// * adding the best tip,
1229 /// * adding the `stop` hash to the list, if it is in the best chain, or
1230 /// * adding [`MAX_FIND_BLOCK_HASHES_RESULTS`] hashes to the list.
1231 ///
1232 /// Returns an empty list if the state is empty.
1233 ///
1234 /// Returns
1235 ///
1236 /// [`ReadResponse::BlockHashes(Vec<block::Hash>)`](ReadResponse::BlockHashes).
1237 /// See <https://en.bitcoin.it/wiki/Protocol_documentation#getblocks>
1238 FindBlockHashes {
1239 /// Hashes of known blocks, ordered from highest height to lowest height.
1240 known_blocks: Vec<block::Hash>,
1241 /// Optionally, the last block hash to request.
1242 stop: Option<block::Hash>,
1243 },
1244
1245 /// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
1246 /// Returns a list of headers that follow that intersection, from the best chain.
1247 ///
1248 /// If there is no matching hash in the best chain, starts from the genesis header.
1249 ///
1250 /// Stops the list of headers after:
1251 /// * adding the best tip,
1252 /// * adding the header matching the `stop` hash to the list, if it is in the best chain, or
1253 /// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS`] headers to the list.
1254 ///
1255 /// Returns an empty list if the state is empty.
1256 ///
1257 /// Returns
1258 ///
1259 /// [`ReadResponse::BlockHeaders(Vec<block::Header>)`](ReadResponse::BlockHeaders).
1260 /// See <https://en.bitcoin.it/wiki/Protocol_documentation#getheaders>
1261 FindBlockHeaders {
1262 /// Hashes of known blocks, ordered from highest height to lowest height.
1263 known_blocks: Vec<block::Hash>,
1264 /// Optionally, the hash of the last header to request.
1265 stop: Option<block::Hash>,
1266 },
1267
1268 /// Looks up a Sapling note commitment tree either by a hash or height.
1269 ///
1270 /// Returns
1271 ///
1272 /// * [`ReadResponse::SaplingTree(Some(Arc<NoteCommitmentTree>))`](crate::ReadResponse::SaplingTree)
1273 /// if the corresponding block contains a Sapling note commitment tree.
1274 /// * [`ReadResponse::SaplingTree(None)`](crate::ReadResponse::SaplingTree) otherwise.
1275 SaplingTree(HashOrHeight),
1276
1277 /// Looks up an Orchard note commitment tree either by a hash or height.
1278 ///
1279 /// Returns
1280 ///
1281 /// * [`ReadResponse::OrchardTree(Some(Arc<NoteCommitmentTree>))`](crate::ReadResponse::OrchardTree)
1282 /// if the corresponding block contains a Sapling note commitment tree.
1283 /// * [`ReadResponse::OrchardTree(None)`](crate::ReadResponse::OrchardTree) otherwise.
1284 OrchardTree(HashOrHeight),
1285
1286 /// Returns a list of Sapling note commitment subtrees by their indexes, starting at
1287 /// `start_index`, and returning up to `limit` subtrees.
1288 ///
1289 /// Returns
1290 ///
1291 /// * [`ReadResponse::SaplingSubtree(BTreeMap<_, NoteCommitmentSubtreeData<_>>))`](crate::ReadResponse::SaplingSubtrees)
1292 /// * An empty list if there is no subtree at `start_index`.
1293 SaplingSubtrees {
1294 /// The index of the first 2^16-leaf subtree to return.
1295 start_index: NoteCommitmentSubtreeIndex,
1296 /// The maximum number of subtree values to return.
1297 limit: Option<NoteCommitmentSubtreeIndex>,
1298 },
1299
1300 /// Returns a list of Orchard note commitment subtrees by their indexes, starting at
1301 /// `start_index`, and returning up to `limit` subtrees.
1302 ///
1303 /// Returns
1304 ///
1305 /// * [`ReadResponse::OrchardSubtree(BTreeMap<_, NoteCommitmentSubtreeData<_>>))`](crate::ReadResponse::OrchardSubtrees)
1306 /// * An empty list if there is no subtree at `start_index`.
1307 OrchardSubtrees {
1308 /// The index of the first 2^16-leaf subtree to return.
1309 start_index: NoteCommitmentSubtreeIndex,
1310 /// The maximum number of subtree values to return.
1311 limit: Option<NoteCommitmentSubtreeIndex>,
1312 },
1313
1314 /// Looks up the balance of a set of transparent addresses.
1315 ///
1316 /// Returns an [`Amount`](zebra_chain::amount::Amount) with the total
1317 /// balance of the set of addresses.
1318 AddressBalance(HashSet<transparent::Address>),
1319
1320 /// Looks up transaction hashes that were sent or received from addresses,
1321 /// in an inclusive blockchain height range.
1322 ///
1323 /// Returns
1324 ///
1325 /// * An ordered, unique map of transaction locations and hashes.
1326 /// * An empty map if no transactions were found for the given arguments.
1327 ///
1328 /// Returned txids are in the order they appear in blocks,
1329 /// which ensures that they are topologically sorted
1330 /// (i.e. parent txids will appear before child txids).
1331 TransactionIdsByAddresses {
1332 /// The requested addresses.
1333 addresses: HashSet<transparent::Address>,
1334
1335 /// The blocks to be queried for transactions.
1336 height_range: RangeInclusive<block::Height>,
1337 },
1338
1339 /// Looks up a spending transaction id by its spent transparent input.
1340 ///
1341 /// Returns [`ReadResponse::TransactionId`] with the hash of the transaction
1342 /// that spent the output at the provided [`transparent::OutPoint`].
1343 #[cfg(feature = "indexer")]
1344 SpendingTransactionId(Spend),
1345
1346 /// Looks up utxos for the provided addresses.
1347 ///
1348 /// Returns a type with found utxos and transaction information.
1349 UtxosByAddresses(HashSet<transparent::Address>),
1350
1351 /// Contextually validates anchors and nullifiers of a transaction on the best chain
1352 ///
1353 /// Returns [`ReadResponse::ValidBestChainTipNullifiersAndAnchors`].
1354 CheckBestChainTipNullifiersAndAnchors(UnminedTx),
1355
1356 /// Calculates the median-time-past for the *next* block on the best chain.
1357 ///
1358 /// Returns [`ReadResponse::BestChainNextMedianTimePast`] when successful.
1359 BestChainNextMedianTimePast,
1360
1361 /// Looks up a block hash by height in the current best chain.
1362 ///
1363 /// Returns
1364 ///
1365 /// * [`ReadResponse::BlockHash(Some(hash))`](ReadResponse::BlockHash) if the block is in the best chain;
1366 /// * [`ReadResponse::BlockHash(None)`](ReadResponse::BlockHash) otherwise.
1367 BestChainBlockHash(block::Height),
1368
1369 /// Get state information from the best block chain.
1370 ///
1371 /// Returns [`ReadResponse::ChainInfo(info)`](ReadResponse::ChainInfo) where `info` is a
1372 /// [`zebra-state::GetBlockTemplateChainInfo`](zebra-state::GetBlockTemplateChainInfo)` structure containing
1373 /// best chain state information.
1374 ChainInfo,
1375
1376 /// Get the average solution rate in the best chain.
1377 ///
1378 /// Returns [`ReadResponse::SolutionRate`]
1379 SolutionRate {
1380 /// The number of blocks to calculate the average difficulty for.
1381 num_blocks: usize,
1382 /// Optionally estimate the network solution rate at the time when this height was mined.
1383 /// Otherwise, estimate at the current tip height.
1384 height: Option<block::Height>,
1385 },
1386
1387 /// Performs contextual validation of the given block, but does not commit it to the state.
1388 ///
1389 /// It is the caller's responsibility to perform semantic validation.
1390 /// (The caller does not need to check proof of work for block proposals.)
1391 ///
1392 /// Returns [`ReadResponse::ValidBlockProposal`] when successful, or an error if
1393 /// the block fails contextual validation.
1394 CheckBlockProposalValidity(SemanticallyVerifiedBlock),
1395
1396 /// Returns [`ReadResponse::TipBlockSize(usize)`](ReadResponse::TipBlockSize)
1397 /// with the current best chain tip block size in bytes.
1398 TipBlockSize,
1399
1400 /// Returns [`ReadResponse::NonFinalizedBlocksListener`] with a channel receiver
1401 /// allowing the caller to listen for new blocks in the non-finalized state.
1402 NonFinalizedBlocksListener,
1403
1404 /// Returns `true` if the transparent output is spent in the best chain,
1405 /// or `false` if it is unspent.
1406 IsTransparentOutputSpent(transparent::OutPoint),
1407}
1408
1409impl ReadRequest {
1410 /// Returns a [`&'static str`](str) name of the variant representing this value.
1411 pub fn variant_name(&self) -> &'static str {
1412 match self {
1413 ReadRequest::UsageInfo => "usage_info",
1414 ReadRequest::Tip => "tip",
1415 ReadRequest::TipPoolValues => "tip_pool_values",
1416 ReadRequest::BlockInfo(_) => "block_info",
1417 ReadRequest::Depth(_) => "depth",
1418 ReadRequest::Block(_) => "block",
1419 ReadRequest::AnyChainBlock(_) => "any_chain_block",
1420 ReadRequest::BlockAndSize(_) => "block_and_size",
1421 ReadRequest::BlockHeader(_) => "block_header",
1422 ReadRequest::Transaction(_) => "transaction",
1423 ReadRequest::AnyChainTransaction(_) => "any_chain_transaction",
1424 ReadRequest::TransactionIdsForBlock(_) => "transaction_ids_for_block",
1425 ReadRequest::AnyChainTransactionIdsForBlock(_) => "any_chain_transaction_ids_for_block",
1426 ReadRequest::UnspentBestChainUtxo { .. } => "unspent_best_chain_utxo",
1427 ReadRequest::AnyChainUtxo { .. } => "any_chain_utxo",
1428 ReadRequest::BlockLocator => "block_locator",
1429 ReadRequest::FindBlockHashes { .. } => "find_block_hashes",
1430 ReadRequest::FindBlockHeaders { .. } => "find_block_headers",
1431 ReadRequest::SaplingTree { .. } => "sapling_tree",
1432 ReadRequest::OrchardTree { .. } => "orchard_tree",
1433 ReadRequest::SaplingSubtrees { .. } => "sapling_subtrees",
1434 ReadRequest::OrchardSubtrees { .. } => "orchard_subtrees",
1435 ReadRequest::AddressBalance { .. } => "address_balance",
1436 ReadRequest::TransactionIdsByAddresses { .. } => "transaction_ids_by_addresses",
1437 ReadRequest::UtxosByAddresses(_) => "utxos_by_addresses",
1438 ReadRequest::CheckBestChainTipNullifiersAndAnchors(_) => {
1439 "best_chain_tip_nullifiers_anchors"
1440 }
1441 ReadRequest::BestChainNextMedianTimePast => "best_chain_next_median_time_past",
1442 ReadRequest::BestChainBlockHash(_) => "best_chain_block_hash",
1443 #[cfg(feature = "indexer")]
1444 ReadRequest::SpendingTransactionId(_) => "spending_transaction_id",
1445 ReadRequest::ChainInfo => "chain_info",
1446 ReadRequest::SolutionRate { .. } => "solution_rate",
1447 ReadRequest::CheckBlockProposalValidity(_) => "check_block_proposal_validity",
1448 ReadRequest::TipBlockSize => "tip_block_size",
1449 ReadRequest::NonFinalizedBlocksListener => "non_finalized_blocks_listener",
1450 ReadRequest::IsTransparentOutputSpent(_) => "is_transparent_output_spent",
1451 }
1452 }
1453
1454 /// Counts metric for ReadStateService call
1455 pub fn count_metric(&self) {
1456 metrics::counter!(
1457 "state.requests",
1458 "service" => "read_state",
1459 "type" => self.variant_name()
1460 )
1461 .increment(1);
1462 }
1463}
1464
1465/// Conversion from read-write [`Request`]s to read-only [`ReadRequest`]s.
1466///
1467/// Used to dispatch read requests concurrently from the [`StateService`](crate::service::StateService).
1468impl TryFrom<Request> for ReadRequest {
1469 type Error = &'static str;
1470
1471 fn try_from(request: Request) -> Result<ReadRequest, Self::Error> {
1472 match request {
1473 Request::Tip => Ok(ReadRequest::Tip),
1474 Request::Depth(hash) => Ok(ReadRequest::Depth(hash)),
1475 Request::BestChainNextMedianTimePast => Ok(ReadRequest::BestChainNextMedianTimePast),
1476 Request::BestChainBlockHash(hash) => Ok(ReadRequest::BestChainBlockHash(hash)),
1477
1478 Request::Block(hash_or_height) => Ok(ReadRequest::Block(hash_or_height)),
1479 Request::AnyChainBlock(hash_or_height) => {
1480 Ok(ReadRequest::AnyChainBlock(hash_or_height))
1481 }
1482 Request::BlockAndSize(hash_or_height) => Ok(ReadRequest::BlockAndSize(hash_or_height)),
1483 Request::BlockHeader(hash_or_height) => Ok(ReadRequest::BlockHeader(hash_or_height)),
1484 Request::Transaction(tx_hash) => Ok(ReadRequest::Transaction(tx_hash)),
1485 Request::AnyChainTransaction(tx_hash) => Ok(ReadRequest::AnyChainTransaction(tx_hash)),
1486 Request::UnspentBestChainUtxo(outpoint) => {
1487 Ok(ReadRequest::UnspentBestChainUtxo(outpoint))
1488 }
1489
1490 Request::BlockLocator => Ok(ReadRequest::BlockLocator),
1491 Request::FindBlockHashes { known_blocks, stop } => {
1492 Ok(ReadRequest::FindBlockHashes { known_blocks, stop })
1493 }
1494 Request::FindBlockHeaders { known_blocks, stop } => {
1495 Ok(ReadRequest::FindBlockHeaders { known_blocks, stop })
1496 }
1497
1498 Request::CheckBestChainTipNullifiersAndAnchors(tx) => {
1499 Ok(ReadRequest::CheckBestChainTipNullifiersAndAnchors(tx))
1500 }
1501
1502 Request::CommitSemanticallyVerifiedBlock(_)
1503 | Request::CommitCheckpointVerifiedBlock(_)
1504 | Request::InvalidateBlock(_)
1505 | Request::ReconsiderBlock(_) => Err("ReadService does not write blocks"),
1506
1507 Request::AwaitUtxo(_) => Err("ReadService does not track pending UTXOs. \
1508 Manually convert the request to ReadRequest::AnyChainUtxo, \
1509 and handle pending UTXOs"),
1510
1511 Request::KnownBlock(_) => Err("ReadService does not track queued blocks"),
1512
1513 Request::CheckBlockProposalValidity(semantically_verified) => Ok(
1514 ReadRequest::CheckBlockProposalValidity(semantically_verified),
1515 ),
1516 }
1517 }
1518}
1519
1520#[derive(Debug)]
1521/// A convenience type for spawning blocking tokio tasks with
1522/// a timer in the scope of a provided span.
1523pub struct TimedSpan {
1524 timer: CodeTimer,
1525 span: tracing::Span,
1526}
1527
1528impl TimedSpan {
1529 /// Creates a new [`TimedSpan`].
1530 pub fn new(timer: CodeTimer, span: tracing::Span) -> Self {
1531 Self { timer, span }
1532 }
1533
1534 /// Spawns a blocking tokio task in scope of the `span` field.
1535 #[track_caller]
1536 pub fn spawn_blocking<T: Send + 'static>(
1537 mut self,
1538 f: impl FnOnce() -> Result<T, BoxError> + Send + 'static,
1539 ) -> Pin<Box<dyn futures::Future<Output = Result<T, BoxError>> + Send>> {
1540 let location = std::panic::Location::caller();
1541 // # Performance
1542 //
1543 // Allow other async tasks to make progress while concurrently reading blocks from disk.
1544
1545 // The work is done in the future.
1546 tokio::task::spawn_blocking(move || {
1547 self.span.in_scope(move || {
1548 let result = f();
1549 self.timer
1550 .finish_inner(Some(location.file()), Some(location.line()), "");
1551 result
1552 })
1553 })
1554 .wait_for_panics()
1555 }
1556}