160 lines
6.3 KiB
Rust
160 lines
6.3 KiB
Rust
//! State [`tower::Service`] response types.
|
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
|
|
|
use zebra_chain::{
|
|
amount::{Amount, NonNegative},
|
|
block::{self, Block},
|
|
orchard, sapling,
|
|
transaction::{self, Transaction},
|
|
transparent,
|
|
};
|
|
|
|
// Allow *only* these unused imports, so that rustdoc link resolution
|
|
// will work with inline links.
|
|
#[allow(unused_imports)]
|
|
use crate::{ReadRequest, Request};
|
|
|
|
use crate::{service::read::AddressUtxos, TransactionLocation};
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
/// A response to a [`StateService`](crate::service::StateService) [`Request`].
|
|
pub enum Response {
|
|
/// Response to [`Request::CommitBlock`] indicating that a block was
|
|
/// successfully committed to the state.
|
|
Committed(block::Hash),
|
|
|
|
/// Response to [`Request::Depth`] with the depth of the specified block.
|
|
Depth(Option<u32>),
|
|
|
|
/// Response to [`Request::Tip`] with the current best chain tip.
|
|
Tip(Option<(block::Height, block::Hash)>),
|
|
|
|
/// Response to [`Request::BlockLocator`] with a block locator object.
|
|
BlockLocator(Vec<block::Hash>),
|
|
|
|
/// Response to [`Request::Transaction`] with the specified transaction.
|
|
Transaction(Option<Arc<Transaction>>),
|
|
|
|
/// Response to [`Request::Block`] with the specified block.
|
|
Block(Option<Arc<Block>>),
|
|
|
|
/// The response to a `AwaitUtxo` request, from any non-finalized chains, finalized chain,
|
|
/// pending unverified blocks, or blocks received after the request was sent.
|
|
Utxo(transparent::Utxo),
|
|
|
|
/// The response to a `FindBlockHashes` request.
|
|
BlockHashes(Vec<block::Hash>),
|
|
|
|
/// The response to a `FindBlockHeaders` request.
|
|
BlockHeaders(Vec<block::CountedHeader>),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
/// A response to a read-only
|
|
/// [`ReadStateService`](crate::service::ReadStateService)'s
|
|
/// [`ReadRequest`](crate::ReadRequest).
|
|
pub enum ReadResponse {
|
|
/// Response to [`ReadRequest::Tip`] with the current best chain tip.
|
|
Tip(Option<(block::Height, block::Hash)>),
|
|
|
|
/// Response to [`ReadRequest::Depth`] with the depth of the specified block.
|
|
Depth(Option<u32>),
|
|
|
|
/// Response to [`ReadRequest::Block`] with the specified block.
|
|
Block(Option<Arc<Block>>),
|
|
|
|
/// Response to [`ReadRequest::Transaction`] with the specified transaction.
|
|
Transaction(Option<(Arc<Transaction>, block::Height)>),
|
|
|
|
/// Response to [`ReadRequest::TransactionIdsForBlock`],
|
|
/// with an list of transaction hashes in block order,
|
|
/// or `None` if the block was not found.
|
|
TransactionIdsForBlock(Option<Arc<[transaction::Hash]>>),
|
|
|
|
/// Response to [`ReadRequest::BlockLocator`] with a block locator object.
|
|
BlockLocator(Vec<block::Hash>),
|
|
|
|
/// The response to a `FindBlockHashes` request.
|
|
BlockHashes(Vec<block::Hash>),
|
|
|
|
/// The response to a `FindBlockHeaders` request.
|
|
BlockHeaders(Vec<block::CountedHeader>),
|
|
|
|
/// The response to a `BestChainUtxo` request, from verified blocks in the
|
|
/// _best_ non-finalized chain, or the finalized chain.
|
|
///
|
|
/// This response is purely informational, there is no guarantee that
|
|
/// the UTXO remains unspent in the best chain.
|
|
BestChainUtxo(Option<transparent::Utxo>),
|
|
|
|
/// The response to an `AnyChainUtxo` request, from verified blocks in
|
|
/// _any_ non-finalized chain, or the finalized chain.
|
|
///
|
|
/// This response is purely informational, there is no guarantee that
|
|
/// the UTXO remains unspent in the best chain.
|
|
AnyChainUtxo(Option<transparent::Utxo>),
|
|
|
|
/// Response to [`ReadRequest::SaplingTree`] with the specified Sapling note commitment tree.
|
|
SaplingTree(Option<Arc<sapling::tree::NoteCommitmentTree>>),
|
|
|
|
/// Response to [`ReadRequest::OrchardTree`] with the specified Orchard note commitment tree.
|
|
OrchardTree(Option<Arc<orchard::tree::NoteCommitmentTree>>),
|
|
|
|
/// Response to [`ReadRequest::AddressBalance`] with the total balance of the addresses.
|
|
AddressBalance(Amount<NonNegative>),
|
|
|
|
/// Response to [`ReadRequest::TransactionIdsByAddresses`]
|
|
/// with the obtained transaction ids, in the order they appear in blocks.
|
|
AddressesTransactionIds(BTreeMap<TransactionLocation, transaction::Hash>),
|
|
|
|
/// Response to [`ReadRequest::UtxosByAddresses`] with found utxos and transaction data.
|
|
AddressUtxos(AddressUtxos),
|
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
|
/// Response to [`ReadRequest::BestChainBlockHash`](crate::ReadRequest::BestChainBlockHash) with the
|
|
/// specified block hash.
|
|
BlockHash(Option<block::Hash>),
|
|
}
|
|
|
|
/// Conversion from read-only [`ReadResponse`]s to read-write [`Response`]s.
|
|
///
|
|
/// Used to return read requests concurrently from the [`StateService`](crate::service::StateService).
|
|
impl TryFrom<ReadResponse> for Response {
|
|
type Error = &'static str;
|
|
|
|
fn try_from(response: ReadResponse) -> Result<Response, Self::Error> {
|
|
match response {
|
|
ReadResponse::Tip(height_and_hash) => Ok(Response::Tip(height_and_hash)),
|
|
ReadResponse::Depth(depth) => Ok(Response::Depth(depth)),
|
|
|
|
ReadResponse::Block(block) => Ok(Response::Block(block)),
|
|
ReadResponse::Transaction(tx_and_height) => {
|
|
Ok(Response::Transaction(tx_and_height.map(|(tx, _height)| tx)))
|
|
}
|
|
|
|
ReadResponse::AnyChainUtxo(_) => Err("ReadService does not track pending UTXOs. \
|
|
Manually unwrap the response, and handle pending UTXOs."),
|
|
|
|
ReadResponse::BlockLocator(hashes) => Ok(Response::BlockLocator(hashes)),
|
|
ReadResponse::BlockHashes(hashes) => Ok(Response::BlockHashes(hashes)),
|
|
ReadResponse::BlockHeaders(headers) => Ok(Response::BlockHeaders(headers)),
|
|
|
|
ReadResponse::TransactionIdsForBlock(_)
|
|
| ReadResponse::BestChainUtxo(_)
|
|
| ReadResponse::SaplingTree(_)
|
|
| ReadResponse::OrchardTree(_)
|
|
| ReadResponse::AddressBalance(_)
|
|
| ReadResponse::AddressesTransactionIds(_)
|
|
| ReadResponse::AddressUtxos(_) => {
|
|
Err("there is no corresponding Response for this ReadResponse")
|
|
}
|
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
|
ReadResponse::BlockHash(_) => {
|
|
Err("there is no corresponding Response for this ReadResponse")
|
|
}
|
|
}
|
|
}
|
|
}
|