2022-03-17 15:59:46 -07:00
|
|
|
//! State [`tower::Service`] request types.
|
|
|
|
|
2022-04-20 11:27:00 -07:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
2022-04-21 13:19:26 -07:00
|
|
|
ops::RangeInclusive,
|
2022-04-20 11:27:00 -07:00
|
|
|
sync::Arc,
|
|
|
|
};
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
|
2020-09-09 17:59:58 -07:00
|
|
|
use zebra_chain::{
|
2021-08-25 06:57:07 -07:00
|
|
|
amount::NegativeAllowed,
|
2020-09-09 17:59:58 -07:00
|
|
|
block::{self, Block},
|
2022-05-12 00:00:12 -07:00
|
|
|
serialization::SerializationError,
|
2021-08-25 06:57:07 -07:00
|
|
|
transaction,
|
|
|
|
transparent::{self, utxos_from_ordered_utxos},
|
|
|
|
value_balance::{ValueBalance, ValueBalanceError},
|
2020-09-09 17:59:58 -07:00
|
|
|
};
|
|
|
|
|
2022-04-08 15:42:05 -07:00
|
|
|
/// Allow *only* this unused import, so that rustdoc link resolution
|
|
|
|
/// will work with inline links.
|
2020-09-09 17:59:58 -07:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use crate::Response;
|
|
|
|
|
|
|
|
/// Identify a block by hash or height.
|
|
|
|
///
|
|
|
|
/// This enum implements `From` for [`block::Hash`] and [`block::Height`],
|
|
|
|
/// so it can be created using `hash.into()` or `height.into()`.
|
2020-11-01 10:49:34 -08:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2020-09-09 17:59:58 -07:00
|
|
|
pub enum HashOrHeight {
|
|
|
|
/// A block identified by hash.
|
|
|
|
Hash(block::Hash),
|
|
|
|
/// A block identified by height.
|
|
|
|
Height(block::Height),
|
|
|
|
}
|
|
|
|
|
2020-11-01 10:49:34 -08:00
|
|
|
impl HashOrHeight {
|
|
|
|
/// Unwrap the inner height or attempt to retrieve the height for a given
|
|
|
|
/// hash if one exists.
|
|
|
|
pub fn height_or_else<F>(self, op: F) -> Option<block::Height>
|
|
|
|
where
|
|
|
|
F: FnOnce(block::Hash) -> Option<block::Height>,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
HashOrHeight::Hash(hash) => op(hash),
|
|
|
|
HashOrHeight::Height(height) => Some(height),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 17:59:58 -07:00
|
|
|
impl From<block::Hash> for HashOrHeight {
|
|
|
|
fn from(hash: block::Hash) -> Self {
|
|
|
|
Self::Hash(hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<block::Height> for HashOrHeight {
|
|
|
|
fn from(hash: block::Height) -> Self {
|
|
|
|
Self::Height(hash)
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 16:42:52 -07:00
|
|
|
|
2022-05-12 00:00:12 -07:00
|
|
|
impl std::str::FromStr for HashOrHeight {
|
|
|
|
type Err = SerializationError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
s.parse()
|
|
|
|
.map(Self::Hash)
|
|
|
|
.or_else(|_| s.parse().map(Self::Height))
|
|
|
|
.map_err(|_| {
|
|
|
|
SerializationError::Parse("could not convert the input string to a hash or height")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
/// A block which has undergone semantic validation and has been prepared for
|
|
|
|
/// contextual validation.
|
|
|
|
///
|
|
|
|
/// It is the constructor's responsibility to perform semantic validation and to
|
|
|
|
/// ensure that all fields are consistent.
|
|
|
|
///
|
|
|
|
/// This structure contains data from contextual validation, which is computed in
|
|
|
|
/// the *service caller*'s task, not inside the service call itself. This allows
|
|
|
|
/// moving work out of the single-threaded state service.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct PreparedBlock {
|
|
|
|
/// The block to commit to the state.
|
|
|
|
pub block: Arc<Block>,
|
|
|
|
/// The hash of the block.
|
|
|
|
pub hash: block::Hash,
|
|
|
|
/// The height of the block.
|
|
|
|
pub height: block::Height,
|
|
|
|
/// New transparent outputs created in this block, indexed by
|
|
|
|
/// [`Outpoint`](transparent::Outpoint).
|
|
|
|
///
|
2021-07-19 06:52:32 -07:00
|
|
|
/// Each output is tagged with its transaction index in the block.
|
|
|
|
/// (The outputs of earlier transactions in a block can be spent by later
|
|
|
|
/// transactions.)
|
|
|
|
///
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
/// Note: although these transparent outputs are newly created, they may not
|
|
|
|
/// be unspent, since a later transaction in a block can spend outputs of an
|
|
|
|
/// earlier transaction.
|
2022-04-12 10:21:46 -07:00
|
|
|
///
|
|
|
|
/// This field can also contain unrelated outputs, which are ignored.
|
2021-07-19 06:52:32 -07:00
|
|
|
pub new_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
2021-08-30 17:55:39 -07:00
|
|
|
/// A precomputed list of the hashes of the transactions in this block,
|
|
|
|
/// in the same order as `block.transactions`.
|
2021-08-30 11:42:07 -07:00
|
|
|
pub transaction_hashes: Arc<[transaction::Hash]>,
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
}
|
|
|
|
|
2021-08-30 17:55:39 -07:00
|
|
|
// Some fields are pub(crate), so we can add whatever db-format-dependent
|
|
|
|
// precomputation we want here without leaking internal details.
|
|
|
|
|
2021-07-19 06:52:32 -07:00
|
|
|
/// A contextually validated block, ready to be committed directly to the finalized state with
|
|
|
|
/// no checks, if it becomes the root of the best non-finalized chain.
|
|
|
|
///
|
|
|
|
/// Used by the state service and non-finalized [`Chain`].
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2021-08-25 06:57:07 -07:00
|
|
|
pub struct ContextuallyValidBlock {
|
2022-04-12 10:21:46 -07:00
|
|
|
/// The block to commit to the state.
|
2021-07-19 06:52:32 -07:00
|
|
|
pub(crate) block: Arc<Block>,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
|
|
|
/// The hash of the block.
|
2021-07-19 06:52:32 -07:00
|
|
|
pub(crate) hash: block::Hash,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
|
|
|
/// The height of the block.
|
2021-07-19 06:52:32 -07:00
|
|
|
pub(crate) height: block::Height,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
|
|
|
/// New transparent outputs created in this block, indexed by
|
|
|
|
/// [`Outpoint`](transparent::Outpoint).
|
|
|
|
///
|
|
|
|
/// Note: although these transparent outputs are newly created, they may not
|
|
|
|
/// be unspent, since a later transaction in a block can spend outputs of an
|
|
|
|
/// earlier transaction.
|
|
|
|
///
|
|
|
|
/// This field can also contain unrelated outputs, which are ignored.
|
|
|
|
pub(crate) new_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
|
|
|
|
/// The outputs spent by this block, indexed by the [`transparent::Input`]'s
|
|
|
|
/// [`Outpoint`](transparent::Outpoint).
|
|
|
|
///
|
|
|
|
/// Note: these inputs can come from earlier transactions in this block,
|
|
|
|
/// or earlier blocks in the chain.
|
|
|
|
///
|
|
|
|
/// This field can also contain unrelated outputs, which are ignored.
|
|
|
|
pub(crate) spent_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
|
|
|
|
/// A precomputed list of the hashes of the transactions in this block,
|
|
|
|
/// in the same order as `block.transactions`.
|
2021-08-30 11:42:07 -07:00
|
|
|
pub(crate) transaction_hashes: Arc<[transaction::Hash]>,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
/// The sum of the chain value pool changes of all transactions in this block.
|
|
|
|
pub(crate) chain_value_pool_change: ValueBalance<NegativeAllowed>,
|
2021-07-19 06:52:32 -07:00
|
|
|
}
|
|
|
|
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
/// A finalized block, ready to be committed directly to the finalized state with
|
|
|
|
/// no checks.
|
|
|
|
///
|
|
|
|
/// This is exposed for use in checkpointing.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct FinalizedBlock {
|
2021-08-30 17:55:39 -07:00
|
|
|
/// The block to commit to the state.
|
|
|
|
pub block: Arc<Block>,
|
|
|
|
/// The hash of the block.
|
|
|
|
pub hash: block::Hash,
|
|
|
|
/// The height of the block.
|
|
|
|
pub height: block::Height,
|
|
|
|
/// New transparent outputs created in this block, indexed by
|
|
|
|
/// [`Outpoint`](transparent::Outpoint).
|
|
|
|
///
|
|
|
|
/// Note: although these transparent outputs are newly created, they may not
|
|
|
|
/// be unspent, since a later transaction in a block can spend outputs of an
|
|
|
|
/// earlier transaction.
|
2022-04-12 10:21:46 -07:00
|
|
|
///
|
|
|
|
/// This field can also contain unrelated outputs, which are ignored.
|
2021-07-11 19:49:33 -07:00
|
|
|
pub(crate) new_outputs: HashMap<transparent::OutPoint, transparent::Utxo>,
|
2021-08-30 17:55:39 -07:00
|
|
|
/// A precomputed list of the hashes of the transactions in this block,
|
|
|
|
/// in the same order as `block.transactions`.
|
|
|
|
pub transaction_hashes: Arc<[transaction::Hash]>,
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
impl From<&PreparedBlock> for PreparedBlock {
|
|
|
|
fn from(prepared: &PreparedBlock) -> Self {
|
|
|
|
prepared.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:55:39 -07:00
|
|
|
// Doing precomputation in these impls means that it will be done in
|
|
|
|
// the *service caller*'s task, not inside the service call itself.
|
|
|
|
// This allows moving work out of the single-threaded state service.
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
impl ContextuallyValidBlock {
|
|
|
|
/// Create a block that's ready for non-finalized [`Chain`] contextual validation,
|
|
|
|
/// using a [`PreparedBlock`] and the UTXOs it spends.
|
|
|
|
///
|
|
|
|
/// When combined, `prepared.new_outputs` and `spent_utxos` must contain
|
|
|
|
/// the [`Utxo`]s spent by every transparent input in this block,
|
|
|
|
/// including UTXOs created by earlier transactions in this block.
|
|
|
|
///
|
|
|
|
/// Note: a [`ContextuallyValidBlock`] isn't actually contextually valid until
|
|
|
|
/// [`Chain::update_chain_state_with`] returns success.
|
|
|
|
pub fn with_block_and_spent_utxos(
|
|
|
|
prepared: PreparedBlock,
|
2022-04-12 10:21:46 -07:00
|
|
|
mut spent_outputs: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
2021-08-25 06:57:07 -07:00
|
|
|
) -> Result<Self, ValueBalanceError> {
|
|
|
|
let PreparedBlock {
|
|
|
|
block,
|
|
|
|
hash,
|
|
|
|
height,
|
|
|
|
new_outputs,
|
|
|
|
transaction_hashes,
|
|
|
|
} = prepared;
|
|
|
|
|
|
|
|
// This is redundant for the non-finalized state,
|
|
|
|
// but useful to make some tests pass more easily.
|
2022-04-12 10:21:46 -07:00
|
|
|
//
|
|
|
|
// TODO: fix the tests, and stop adding unrelated outputs.
|
|
|
|
spent_outputs.extend(new_outputs.clone());
|
2021-08-25 06:57:07 -07:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
block: block.clone(),
|
|
|
|
hash,
|
|
|
|
height,
|
2022-04-12 10:21:46 -07:00
|
|
|
new_outputs,
|
|
|
|
spent_outputs: spent_outputs.clone(),
|
2021-08-25 06:57:07 -07:00
|
|
|
transaction_hashes,
|
2022-04-12 10:21:46 -07:00
|
|
|
chain_value_pool_change: block
|
|
|
|
.chain_value_pool_change(&utxos_from_ordered_utxos(spent_outputs))?,
|
2021-08-25 06:57:07 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:55:39 -07:00
|
|
|
impl FinalizedBlock {
|
|
|
|
/// Create a block that's ready to be committed to the finalized state,
|
2022-03-09 15:34:50 -08:00
|
|
|
/// using a precalculated [`block::Hash`].
|
2021-08-30 17:55:39 -07:00
|
|
|
///
|
|
|
|
/// Note: a [`FinalizedBlock`] isn't actually finalized
|
|
|
|
/// until [`Request::CommitFinalizedBlock`] returns success.
|
2022-03-09 15:34:50 -08:00
|
|
|
pub fn with_hash(block: Arc<Block>, hash: block::Hash) -> Self {
|
|
|
|
let height = block
|
|
|
|
.coinbase_height()
|
|
|
|
.expect("coinbase height was already checked");
|
2021-08-30 11:42:07 -07:00
|
|
|
let transaction_hashes: Arc<[_]> = block.transactions.iter().map(|tx| tx.hash()).collect();
|
|
|
|
let new_outputs = transparent::new_outputs(&block, &transaction_hashes);
|
2020-11-23 12:02:57 -08:00
|
|
|
|
|
|
|
Self {
|
|
|
|
block,
|
|
|
|
hash,
|
2021-03-21 19:20:44 -07:00
|
|
|
height,
|
2020-11-23 12:02:57 -08:00
|
|
|
new_outputs,
|
2020-11-24 19:55:15 -08:00
|
|
|
transaction_hashes,
|
2020-11-23 12:02:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:55:39 -07:00
|
|
|
impl From<Arc<Block>> for FinalizedBlock {
|
|
|
|
fn from(block: Arc<Block>) -> Self {
|
|
|
|
let hash = block.hash();
|
|
|
|
|
2022-03-09 15:34:50 -08:00
|
|
|
FinalizedBlock::with_hash(block, hash)
|
2021-08-30 17:55:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:52:32 -07:00
|
|
|
impl From<ContextuallyValidBlock> for FinalizedBlock {
|
|
|
|
fn from(contextually_valid: ContextuallyValidBlock) -> Self {
|
|
|
|
let ContextuallyValidBlock {
|
|
|
|
block,
|
|
|
|
hash,
|
|
|
|
height,
|
|
|
|
new_outputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
spent_outputs: _,
|
2021-07-19 06:52:32 -07:00
|
|
|
transaction_hashes,
|
2021-08-25 06:57:07 -07:00
|
|
|
chain_value_pool_change: _,
|
2021-07-19 06:52:32 -07:00
|
|
|
} = contextually_valid;
|
2022-04-12 10:21:46 -07:00
|
|
|
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
Self {
|
|
|
|
block,
|
|
|
|
hash,
|
2021-03-21 19:20:44 -07:00
|
|
|
height,
|
2022-04-12 10:21:46 -07:00
|
|
|
new_outputs: utxos_from_ordered_utxos(new_outputs),
|
2020-11-24 19:55:15 -08:00
|
|
|
transaction_hashes,
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 16:42:52 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2022-03-17 15:59:46 -07:00
|
|
|
/// A query about or modification to the chain state, via the [`StateService`].
|
2020-09-09 16:42:52 -07:00
|
|
|
pub enum Request {
|
2020-09-09 17:59:58 -07:00
|
|
|
/// Performs contextual validation of the given block, committing it to the
|
|
|
|
/// state if successful.
|
|
|
|
///
|
2020-11-12 11:43:17 -08:00
|
|
|
/// It is the caller's responsibility to perform semantic validation. This
|
|
|
|
/// request can be made out-of-order; the state service will queue it until
|
|
|
|
/// its parent is ready.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
2020-11-12 11:43:17 -08:00
|
|
|
/// Returns [`Response::Committed`] with the hash of the block when it is
|
|
|
|
/// committed to the state, or an error if the block fails contextual
|
|
|
|
/// validation or has already been committed to the state.
|
|
|
|
///
|
|
|
|
/// This request cannot be cancelled once submitted; dropping the response
|
|
|
|
/// future will have no effect on whether it is eventually processed. A
|
|
|
|
/// request to commit a block which has been queued internally but not yet
|
|
|
|
/// committed will fail the older request and replace it with the newer request.
|
2021-06-18 10:43:05 -07:00
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Block commit requests should be wrapped in a timeout, so that
|
|
|
|
/// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
|
|
|
|
/// documentation for details.
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
CommitBlock(PreparedBlock),
|
2020-09-09 17:59:58 -07:00
|
|
|
|
2020-11-12 11:43:17 -08:00
|
|
|
/// Commit a finalized block to the state, skipping all validation.
|
|
|
|
///
|
2020-09-09 17:59:58 -07:00
|
|
|
/// This is exposed for use in checkpointing, which produces finalized
|
2020-11-12 11:43:17 -08:00
|
|
|
/// blocks. It is the caller's responsibility to ensure that the block is
|
|
|
|
/// valid and final. This request can be made out-of-order; the state service
|
|
|
|
/// will queue it until its parent is ready.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
2020-11-12 11:43:17 -08:00
|
|
|
/// Returns [`Response::Committed`] with the hash of the newly committed
|
|
|
|
/// block, or an error.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
2020-11-12 11:43:17 -08:00
|
|
|
/// This request cannot be cancelled once submitted; dropping the response
|
|
|
|
/// future will have no effect on whether it is eventually processed.
|
|
|
|
/// Duplicate requests should not be made, because it is the caller's
|
|
|
|
/// responsibility to ensure that each block is valid and final.
|
2021-06-18 10:43:05 -07:00
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Block commit requests should be wrapped in a timeout, so that
|
|
|
|
/// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
|
|
|
|
/// documentation for details.
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
CommitFinalizedBlock(FinalizedBlock),
|
2020-09-09 17:59:58 -07:00
|
|
|
|
2020-11-16 12:50:57 -08:00
|
|
|
/// Computes the depth in the current best chain of the block identified by the given hash.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
2020-11-16 12:50:57 -08:00
|
|
|
/// * [`Response::Depth(Some(depth))`](Response::Depth) if the block is in the best chain;
|
2020-09-09 17:59:58 -07:00
|
|
|
/// * [`Response::Depth(None)`](Response::Depth) otherwise.
|
|
|
|
Depth(block::Hash),
|
|
|
|
|
|
|
|
/// Returns [`Response::Tip`] with the current best chain tip.
|
|
|
|
Tip,
|
|
|
|
|
2020-11-16 12:50:57 -08:00
|
|
|
/// Computes a block locator object based on the current best chain.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
|
|
|
/// Returns [`Response::BlockLocator`] with hashes starting
|
2020-11-16 12:50:57 -08:00
|
|
|
/// from the best chain tip, and following the chain of previous
|
|
|
|
/// hashes. The first hash is the best chain tip. The last hash is
|
|
|
|
/// the tip of the finalized portion of the state. Block locators
|
|
|
|
/// are not continuous - some intermediate hashes might be skipped.
|
|
|
|
///
|
|
|
|
/// If the state is empty, the block locator is also empty.
|
2020-09-09 17:59:58 -07:00
|
|
|
BlockLocator,
|
|
|
|
|
2020-11-16 12:50:57 -08:00
|
|
|
/// Looks up a transaction by hash in the current best chain.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
2020-11-16 12:50:57 -08:00
|
|
|
/// * [`Response::Transaction(Some(Arc<Transaction>))`](Response::Transaction) if the transaction is in the best chain;
|
2020-09-09 17:59:58 -07:00
|
|
|
/// * [`Response::Transaction(None)`](Response::Transaction) otherwise.
|
|
|
|
Transaction(transaction::Hash),
|
|
|
|
|
2020-11-16 12:50:57 -08:00
|
|
|
/// Looks up a block by hash or height in the current best chain.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
2020-11-16 12:50:57 -08:00
|
|
|
/// * [`Response::Block(Some(Arc<Block>))`](Response::Block) if the block is in the best chain;
|
|
|
|
/// * [`Response::Block(None)`](Response::Block) otherwise.
|
2020-09-09 17:59:58 -07:00
|
|
|
///
|
|
|
|
/// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
|
|
|
|
/// [`block::Height`] using `.into()`.
|
|
|
|
Block(HashOrHeight),
|
2020-10-14 14:06:32 -07:00
|
|
|
|
2020-11-12 11:43:17 -08:00
|
|
|
/// Request a UTXO identified by the given Outpoint, waiting until it becomes
|
|
|
|
/// available if it is unknown.
|
|
|
|
///
|
|
|
|
/// This request is purely informational, and there are no guarantees about
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
/// whether the UTXO remains unspent or is on the best chain, or any chain.
|
|
|
|
/// Its purpose is to allow asynchronous script verification.
|
2020-11-16 12:50:57 -08:00
|
|
|
///
|
2021-06-18 10:43:05 -07:00
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// UTXO requests should be wrapped in a timeout, so that
|
|
|
|
/// out-of-order and invalid requests do not hang indefinitely. See the [`crate`]
|
|
|
|
/// documentation for details.
|
2020-10-14 14:06:32 -07:00
|
|
|
AwaitUtxo(transparent::OutPoint),
|
2020-11-30 13:30:37 -08:00
|
|
|
|
|
|
|
/// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
|
|
|
|
/// Returns a list of hashes that follow that intersection, from the best chain.
|
|
|
|
///
|
|
|
|
/// If there is no matching hash in the best chain, starts from the genesis hash.
|
|
|
|
///
|
|
|
|
/// Stops the list of hashes after:
|
|
|
|
/// * adding the best tip,
|
|
|
|
/// * adding the `stop` hash to the list, if it is in the best chain, or
|
|
|
|
/// * adding 500 hashes to the list.
|
|
|
|
///
|
|
|
|
/// Returns an empty list if the state is empty.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// [`Response::BlockHashes(Vec<block::Hash>)`](Response::BlockHashes).
|
|
|
|
/// See https://en.bitcoin.it/wiki/Protocol_documentation#getblocks
|
|
|
|
FindBlockHashes {
|
|
|
|
/// Hashes of known blocks, ordered from highest height to lowest height.
|
|
|
|
known_blocks: Vec<block::Hash>,
|
|
|
|
/// Optionally, the last block hash to request.
|
|
|
|
stop: Option<block::Hash>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Finds the first hash that's in the peer's `known_blocks` and the local best chain.
|
|
|
|
/// Returns a list of headers that follow that intersection, from the best chain.
|
|
|
|
///
|
|
|
|
/// If there is no matching hash in the best chain, starts from the genesis header.
|
|
|
|
///
|
|
|
|
/// Stops the list of headers after:
|
|
|
|
/// * adding the best tip,
|
|
|
|
/// * adding the header matching the `stop` hash to the list, if it is in the best chain, or
|
|
|
|
/// * adding 160 headers to the list.
|
|
|
|
///
|
|
|
|
/// Returns an empty list if the state is empty.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// [`Response::BlockHeaders(Vec<block::Header>)`](Response::BlockHeaders).
|
|
|
|
/// See https://en.bitcoin.it/wiki/Protocol_documentation#getheaders
|
|
|
|
FindBlockHeaders {
|
|
|
|
/// Hashes of known blocks, ordered from highest height to lowest height.
|
|
|
|
known_blocks: Vec<block::Hash>,
|
|
|
|
/// Optionally, the hash of the last header to request.
|
|
|
|
stop: Option<block::Hash>,
|
|
|
|
},
|
2020-09-09 16:42:52 -07:00
|
|
|
}
|
2022-03-17 15:59:46 -07:00
|
|
|
|
2022-04-13 01:48:13 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2022-03-17 15:59:46 -07:00
|
|
|
/// A read-only query about the chain state, via the [`ReadStateService`].
|
|
|
|
pub enum ReadRequest {
|
|
|
|
/// Looks up a block by hash or height in the current best chain.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// * [`Response::Block(Some(Arc<Block>))`](Response::Block) if the block is in the best chain;
|
|
|
|
/// * [`Response::Block(None)`](Response::Block) otherwise.
|
|
|
|
///
|
|
|
|
/// Note: the [`HashOrHeight`] can be constructed from a [`block::Hash`] or
|
|
|
|
/// [`block::Height`] using `.into()`.
|
|
|
|
Block(HashOrHeight),
|
|
|
|
|
|
|
|
/// Looks up a transaction by hash in the current best chain.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// * [`Response::Transaction(Some(Arc<Transaction>))`](Response::Transaction) if the transaction is in the best chain;
|
|
|
|
/// * [`Response::Transaction(None)`](Response::Transaction) otherwise.
|
|
|
|
Transaction(transaction::Hash),
|
2022-04-13 01:48:13 -07:00
|
|
|
|
2022-04-21 13:19:26 -07:00
|
|
|
/// Looks up the balance of a set of transparent addresses.
|
|
|
|
///
|
|
|
|
/// Returns an [`Amount`] with the total balance of the set of addresses.
|
|
|
|
AddressBalance(HashSet<transparent::Address>),
|
|
|
|
|
2022-05-12 00:00:12 -07:00
|
|
|
/// Looks up a Sapling note commitment tree either by a hash or height.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// * [`ReadResponse::SaplingTree(Some(Arc<NoteCommitmentTree>))`](crate::ReadResponse::SaplingTree)
|
|
|
|
/// if the corresponding block contains a Sapling note commitment tree.
|
|
|
|
/// * [`ReadResponse::SaplingTree(None)`](crate::ReadResponse::SaplingTree) otherwise.
|
|
|
|
SaplingTree(HashOrHeight),
|
|
|
|
|
|
|
|
/// Looks up an Orchard note commitment tree either by a hash or height.
|
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
|
|
|
/// * [`ReadResponse::OrchardTree(Some(Arc<NoteCommitmentTree>))`](crate::ReadResponse::OrchardTree)
|
|
|
|
/// if the corresponding block contains a Sapling note commitment tree.
|
|
|
|
/// * [`ReadResponse::OrchardTree(None)`](crate::ReadResponse::OrchardTree) otherwise.
|
|
|
|
OrchardTree(HashOrHeight),
|
|
|
|
|
|
|
|
/// Looks up transaction hashes that were sent or received from addresses,
|
2022-04-21 13:19:26 -07:00
|
|
|
/// in an inclusive blockchain height range.
|
2022-04-13 01:48:13 -07:00
|
|
|
///
|
|
|
|
/// Returns
|
|
|
|
///
|
2022-04-21 13:19:26 -07:00
|
|
|
/// * A set of transaction hashes.
|
2022-04-13 01:48:13 -07:00
|
|
|
/// * An empty vector if no transactions were found for the given arguments.
|
|
|
|
///
|
2022-04-21 13:19:26 -07:00
|
|
|
/// Returned txids are in the order they appear in blocks,
|
|
|
|
/// which ensures that they are topologically sorted
|
2022-04-13 01:48:13 -07:00
|
|
|
/// (i.e. parent txids will appear before child txids).
|
2022-04-21 13:19:26 -07:00
|
|
|
TransactionIdsByAddresses {
|
|
|
|
/// The requested addresses.
|
|
|
|
addresses: HashSet<transparent::Address>,
|
2022-04-20 11:27:00 -07:00
|
|
|
|
2022-04-21 13:19:26 -07:00
|
|
|
/// The blocks to be queried for transactions.
|
|
|
|
height_range: RangeInclusive<block::Height>,
|
|
|
|
},
|
2022-04-24 20:00:52 -07:00
|
|
|
|
|
|
|
/// Looks up utxos for the provided addresses.
|
|
|
|
///
|
|
|
|
/// Returns a type with found utxos and transaction information.
|
|
|
|
UtxosByAddresses(HashSet<transparent::Address>),
|
2022-03-17 15:59:46 -07:00
|
|
|
}
|