2022-03-15 12:50:28 -07:00
|
|
|
//! [`Chain`] implements a single non-finalized blockchain,
|
|
|
|
//! starting at the finalized tip.
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
2022-04-19 06:34:53 -07:00
|
|
|
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
|
2022-04-21 13:19:26 -07:00
|
|
|
ops::{Deref, RangeInclusive},
|
2022-03-17 11:48:13 -07:00
|
|
|
sync::Arc,
|
2022-07-21 16:17:34 -07:00
|
|
|
time::Instant,
|
2020-11-09 10:05:18 -08:00
|
|
|
};
|
|
|
|
|
2022-02-21 17:17:21 -08:00
|
|
|
use mset::MultiSet;
|
2021-07-14 05:06:43 -07:00
|
|
|
use tracing::instrument;
|
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
use zebra_chain::{
|
2022-04-14 06:34:31 -07:00
|
|
|
amount::{Amount, NegativeAllowed, NonNegative},
|
2022-04-21 13:19:26 -07:00
|
|
|
block::{self, Height},
|
2022-07-21 16:17:34 -07:00
|
|
|
fmt::humantime_milliseconds,
|
2021-08-25 06:57:07 -07:00
|
|
|
history_tree::HistoryTree,
|
|
|
|
orchard,
|
2022-07-22 09:19:11 -07:00
|
|
|
parallel::tree::NoteCommitmentTrees,
|
2021-08-25 06:57:07 -07:00
|
|
|
parameters::Network,
|
|
|
|
primitives::Groth16Proof,
|
2022-03-17 11:48:13 -07:00
|
|
|
sapling, sprout,
|
2021-08-25 06:57:07 -07:00
|
|
|
transaction::Transaction::*,
|
2022-03-17 11:48:13 -07:00
|
|
|
transaction::{self, Transaction},
|
2021-08-25 06:57:07 -07:00
|
|
|
transparent,
|
|
|
|
value_balance::ValueBalance,
|
2021-08-11 06:42:40 -07:00
|
|
|
work::difficulty::PartialCumulativeWork,
|
2020-11-09 10:05:18 -08:00
|
|
|
};
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
use crate::{
|
2022-09-06 02:32:54 -07:00
|
|
|
request::Treestate, service::check, ContextuallyValidBlock, HashOrHeight, OutputLocation,
|
|
|
|
TransactionLocation, ValidateContextError,
|
2022-04-12 10:21:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
use self::index::TransparentTransfers;
|
|
|
|
|
|
|
|
pub mod index;
|
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-07-29 06:37:18 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2020-11-09 10:05:18 -08:00
|
|
|
pub struct Chain {
|
2022-06-13 18:22:16 -07:00
|
|
|
// The function `eq_internal_state` must be updated every time a field is added to [`Chain`].
|
2022-04-12 10:21:46 -07:00
|
|
|
/// The configured network for this chain.
|
2021-08-11 06:42:40 -07:00
|
|
|
network: Network,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
2021-07-19 06:52:32 -07:00
|
|
|
/// The contextually valid blocks which form this non-finalized partial chain, in height order.
|
|
|
|
pub(crate) blocks: BTreeMap<block::Height, ContextuallyValidBlock>,
|
|
|
|
|
|
|
|
/// An index of block heights for each block hash in `blocks`.
|
2020-11-09 10:05:18 -08:00
|
|
|
pub height_by_hash: HashMap<block::Hash, block::Height>,
|
2022-04-12 10:21:46 -07:00
|
|
|
|
|
|
|
/// An index of [`TransactionLocation`]s for each transaction hash in `blocks`.
|
2022-06-30 16:00:23 -07:00
|
|
|
pub tx_loc_by_hash: HashMap<transaction::Hash, TransactionLocation>,
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The [`transparent::Utxo`]s created by `blocks`.
|
2021-07-19 06:52:32 -07:00
|
|
|
///
|
|
|
|
/// Note that these UTXOs may not be unspent.
|
|
|
|
/// Outputs can be spent by later transactions or blocks in the chain.
|
2022-04-12 10:21:46 -07:00
|
|
|
//
|
|
|
|
// TODO: replace OutPoint with OutputLocation?
|
|
|
|
pub(crate) created_utxos: HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The [`transparent::OutPoint`]s spent by `blocks`,
|
2021-07-19 06:52:32 -07:00
|
|
|
/// including those created by earlier transactions or blocks in the chain.
|
2021-07-22 16:40:15 -07:00
|
|
|
pub(crate) spent_utxos: HashSet<transparent::OutPoint>,
|
2021-07-14 15:23:54 -07:00
|
|
|
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The Sprout note commitment tree of the tip of this [`Chain`],
|
2021-11-30 02:05:58 -08:00
|
|
|
/// including all finalized notes, and the non-finalized notes in this chain.
|
2022-07-14 17:39:41 -07:00
|
|
|
pub(super) sprout_note_commitment_tree: Arc<sprout::tree::NoteCommitmentTree>,
|
2022-03-14 22:18:18 -07:00
|
|
|
/// The Sprout note commitment tree for each anchor.
|
|
|
|
/// This is required for interstitial states.
|
|
|
|
pub(crate) sprout_trees_by_anchor:
|
2022-07-14 17:39:41 -07:00
|
|
|
HashMap<sprout::tree::Root, Arc<sprout::tree::NoteCommitmentTree>>,
|
2022-09-06 02:32:54 -07:00
|
|
|
/// The Sprout note commitment tree for each height.
|
|
|
|
pub(crate) sprout_trees_by_height:
|
|
|
|
BTreeMap<block::Height, Arc<sprout::tree::NoteCommitmentTree>>,
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The Sapling note commitment tree of the tip of this [`Chain`],
|
2021-08-25 06:57:07 -07:00
|
|
|
/// including all finalized notes, and the non-finalized notes in this chain.
|
2022-07-14 17:39:41 -07:00
|
|
|
pub(super) sapling_note_commitment_tree: Arc<sapling::tree::NoteCommitmentTree>,
|
2022-03-14 22:18:18 -07:00
|
|
|
/// The Sapling note commitment tree for each height.
|
2022-07-14 17:39:41 -07:00
|
|
|
pub(crate) sapling_trees_by_height:
|
|
|
|
BTreeMap<block::Height, Arc<sapling::tree::NoteCommitmentTree>>,
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The Orchard note commitment tree of the tip of this [`Chain`],
|
2021-08-25 06:57:07 -07:00
|
|
|
/// including all finalized notes, and the non-finalized notes in this chain.
|
2022-07-14 17:39:41 -07:00
|
|
|
pub(super) orchard_note_commitment_tree: Arc<orchard::tree::NoteCommitmentTree>,
|
2022-03-14 22:18:18 -07:00
|
|
|
/// The Orchard note commitment tree for each height.
|
2022-07-14 17:39:41 -07:00
|
|
|
pub(crate) orchard_trees_by_height:
|
|
|
|
BTreeMap<block::Height, Arc<orchard::tree::NoteCommitmentTree>>,
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The ZIP-221 history tree of the tip of this [`Chain`],
|
2021-08-25 06:57:07 -07:00
|
|
|
/// including all finalized blocks, and the non-finalized `blocks` in this chain.
|
2022-07-22 09:19:11 -07:00
|
|
|
pub(crate) history_tree: Arc<HistoryTree>,
|
2022-08-28 23:59:25 -07:00
|
|
|
pub(crate) history_trees_by_height: BTreeMap<block::Height, Arc<HistoryTree>>,
|
2021-07-29 06:37:18 -07:00
|
|
|
|
2021-11-30 02:05:58 -08:00
|
|
|
/// The Sprout anchors created by `blocks`.
|
2022-02-21 17:17:21 -08:00
|
|
|
pub(crate) sprout_anchors: MultiSet<sprout::tree::Root>,
|
2021-11-30 02:05:58 -08:00
|
|
|
/// The Sprout anchors created by each block in `blocks`.
|
2021-12-09 08:50:26 -08:00
|
|
|
pub(crate) sprout_anchors_by_height: BTreeMap<block::Height, sprout::tree::Root>,
|
2021-07-29 06:37:18 -07:00
|
|
|
/// The Sapling anchors created by `blocks`.
|
2022-02-21 17:17:21 -08:00
|
|
|
pub(crate) sapling_anchors: MultiSet<sapling::tree::Root>,
|
2021-08-25 06:57:07 -07:00
|
|
|
/// The Sapling anchors created by each block in `blocks`.
|
2021-11-30 08:05:35 -08:00
|
|
|
pub(crate) sapling_anchors_by_height: BTreeMap<block::Height, sapling::tree::Root>,
|
2021-07-29 06:37:18 -07:00
|
|
|
/// The Orchard anchors created by `blocks`.
|
2022-02-21 17:17:21 -08:00
|
|
|
pub(crate) orchard_anchors: MultiSet<orchard::tree::Root>,
|
2021-08-25 06:57:07 -07:00
|
|
|
/// The Orchard anchors created by each block in `blocks`.
|
2021-11-30 08:05:35 -08:00
|
|
|
pub(crate) orchard_anchors_by_height: BTreeMap<block::Height, orchard::tree::Root>,
|
2021-07-29 06:37:18 -07:00
|
|
|
|
|
|
|
/// The Sprout nullifiers revealed by `blocks`.
|
2021-07-14 15:23:54 -07:00
|
|
|
pub(super) sprout_nullifiers: HashSet<sprout::Nullifier>,
|
2021-07-29 06:37:18 -07:00
|
|
|
/// The Sapling nullifiers revealed by `blocks`.
|
2021-07-14 15:23:54 -07:00
|
|
|
pub(super) sapling_nullifiers: HashSet<sapling::Nullifier>,
|
2021-07-29 06:37:18 -07:00
|
|
|
/// The Orchard nullifiers revealed by `blocks`.
|
2021-07-14 15:23:54 -07:00
|
|
|
pub(super) orchard_nullifiers: HashSet<orchard::Nullifier>,
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
/// Partial transparent address index data from `blocks`.
|
|
|
|
pub(super) partial_transparent_transfers: HashMap<transparent::Address, TransparentTransfers>,
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
/// The cumulative work represented by `blocks`.
|
|
|
|
///
|
|
|
|
/// Since the best chain is determined by the largest cumulative work,
|
|
|
|
/// the work represented by finalized blocks can be ignored,
|
|
|
|
/// because they are common to all non-finalized chains.
|
2021-07-14 15:23:54 -07:00
|
|
|
pub(super) partial_cumulative_work: PartialCumulativeWork,
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2022-06-13 18:22:16 -07:00
|
|
|
/// The chain value pool balances of the tip of this [`Chain`],
|
2021-08-25 06:57:07 -07:00
|
|
|
/// including the block value pool changes from all finalized blocks,
|
|
|
|
/// and the non-finalized blocks in this chain.
|
|
|
|
///
|
|
|
|
/// When a new chain is created from the finalized tip,
|
|
|
|
/// it is initialized with the finalized tip chain value pool balances.
|
|
|
|
pub(crate) chain_value_pools: ValueBalance<NonNegative>,
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chain {
|
2021-08-11 06:42:40 -07:00
|
|
|
/// Create a new Chain with the given trees and network.
|
2021-07-29 06:37:18 -07:00
|
|
|
pub(crate) fn new(
|
2021-08-11 06:42:40 -07:00
|
|
|
network: Network,
|
2022-07-14 17:39:41 -07:00
|
|
|
sprout_note_commitment_tree: Arc<sprout::tree::NoteCommitmentTree>,
|
|
|
|
sapling_note_commitment_tree: Arc<sapling::tree::NoteCommitmentTree>,
|
|
|
|
orchard_note_commitment_tree: Arc<orchard::tree::NoteCommitmentTree>,
|
2022-07-22 09:19:11 -07:00
|
|
|
history_tree: Arc<HistoryTree>,
|
2021-08-25 06:57:07 -07:00
|
|
|
finalized_tip_chain_value_pools: ValueBalance<NonNegative>,
|
2021-07-29 06:37:18 -07:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-08-11 06:42:40 -07:00
|
|
|
network,
|
2021-07-29 06:37:18 -07:00
|
|
|
blocks: Default::default(),
|
|
|
|
height_by_hash: Default::default(),
|
2022-06-30 16:00:23 -07:00
|
|
|
tx_loc_by_hash: Default::default(),
|
2021-07-29 06:37:18 -07:00
|
|
|
created_utxos: Default::default(),
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_note_commitment_tree,
|
2021-07-29 06:37:18 -07:00
|
|
|
sapling_note_commitment_tree,
|
|
|
|
orchard_note_commitment_tree,
|
|
|
|
spent_utxos: Default::default(),
|
2022-02-21 17:17:21 -08:00
|
|
|
sprout_anchors: MultiSet::new(),
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_anchors_by_height: Default::default(),
|
2022-01-18 12:18:49 -08:00
|
|
|
sprout_trees_by_anchor: Default::default(),
|
2022-09-06 02:32:54 -07:00
|
|
|
sprout_trees_by_height: Default::default(),
|
2022-02-21 17:17:21 -08:00
|
|
|
sapling_anchors: MultiSet::new(),
|
2021-07-29 06:37:18 -07:00
|
|
|
sapling_anchors_by_height: Default::default(),
|
2022-03-14 22:18:18 -07:00
|
|
|
sapling_trees_by_height: Default::default(),
|
2022-02-21 17:17:21 -08:00
|
|
|
orchard_anchors: MultiSet::new(),
|
2021-07-29 06:37:18 -07:00
|
|
|
orchard_anchors_by_height: Default::default(),
|
2022-03-14 22:18:18 -07:00
|
|
|
orchard_trees_by_height: Default::default(),
|
2021-07-29 06:37:18 -07:00
|
|
|
sprout_nullifiers: Default::default(),
|
|
|
|
sapling_nullifiers: Default::default(),
|
|
|
|
orchard_nullifiers: Default::default(),
|
2022-04-12 10:21:46 -07:00
|
|
|
partial_transparent_transfers: Default::default(),
|
2021-07-29 06:37:18 -07:00
|
|
|
partial_cumulative_work: Default::default(),
|
2021-08-11 06:42:40 -07:00
|
|
|
history_tree,
|
2022-08-28 23:59:25 -07:00
|
|
|
history_trees_by_height: Default::default(),
|
2021-08-25 06:57:07 -07:00
|
|
|
chain_value_pools: finalized_tip_chain_value_pools,
|
2021-07-29 06:37:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 15:23:54 -07:00
|
|
|
/// Is the internal state of `self` the same as `other`?
|
|
|
|
///
|
|
|
|
/// [`Chain`] has custom [`Eq`] and [`Ord`] implementations based on proof of work,
|
|
|
|
/// which are used to select the best chain. So we can't derive [`Eq`] for [`Chain`].
|
|
|
|
///
|
|
|
|
/// Unlike the custom trait impls, this method returns `true` if the entire internal state
|
|
|
|
/// of two chains is equal.
|
|
|
|
///
|
|
|
|
/// If the internal states are different, it returns `false`,
|
|
|
|
/// even if the blocks in the two chains are equal.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn eq_internal_state(&self, other: &Chain) -> bool {
|
|
|
|
// blocks, heights, hashes
|
|
|
|
self.blocks == other.blocks &&
|
|
|
|
self.height_by_hash == other.height_by_hash &&
|
2022-06-30 16:00:23 -07:00
|
|
|
self.tx_loc_by_hash == other.tx_loc_by_hash &&
|
2021-07-14 15:23:54 -07:00
|
|
|
|
|
|
|
// transparent UTXOs
|
|
|
|
self.created_utxos == other.created_utxos &&
|
|
|
|
self.spent_utxos == other.spent_utxos &&
|
|
|
|
|
2021-07-29 06:37:18 -07:00
|
|
|
// note commitment trees
|
2021-11-30 02:05:58 -08:00
|
|
|
self.sprout_note_commitment_tree.root() == other.sprout_note_commitment_tree.root() &&
|
2022-01-18 12:18:49 -08:00
|
|
|
self.sprout_trees_by_anchor == other.sprout_trees_by_anchor &&
|
2022-09-06 02:32:54 -07:00
|
|
|
self.sprout_trees_by_height == other.sprout_trees_by_height &&
|
2021-07-29 06:37:18 -07:00
|
|
|
self.sapling_note_commitment_tree.root() == other.sapling_note_commitment_tree.root() &&
|
2022-08-28 23:59:25 -07:00
|
|
|
self.sapling_trees_by_height == other.sapling_trees_by_height &&
|
2021-07-29 06:37:18 -07:00
|
|
|
self.orchard_note_commitment_tree.root() == other.orchard_note_commitment_tree.root() &&
|
2022-08-28 23:59:25 -07:00
|
|
|
self.orchard_trees_by_height == other.orchard_trees_by_height &&
|
2021-07-29 06:37:18 -07:00
|
|
|
|
2022-08-28 23:59:25 -07:00
|
|
|
// history trees
|
|
|
|
self.history_tree == other.history_tree &&
|
|
|
|
self.history_trees_by_height == other.history_trees_by_height &&
|
2021-08-11 06:42:40 -07:00
|
|
|
|
2021-07-14 15:23:54 -07:00
|
|
|
// anchors
|
2021-11-30 02:05:58 -08:00
|
|
|
self.sprout_anchors == other.sprout_anchors &&
|
|
|
|
self.sprout_anchors_by_height == other.sprout_anchors_by_height &&
|
2021-07-14 15:23:54 -07:00
|
|
|
self.sapling_anchors == other.sapling_anchors &&
|
2021-07-29 06:37:18 -07:00
|
|
|
self.sapling_anchors_by_height == other.sapling_anchors_by_height &&
|
2021-07-14 15:23:54 -07:00
|
|
|
self.orchard_anchors == other.orchard_anchors &&
|
2021-07-29 06:37:18 -07:00
|
|
|
self.orchard_anchors_by_height == other.orchard_anchors_by_height &&
|
2021-07-14 15:23:54 -07:00
|
|
|
|
|
|
|
// nullifiers
|
|
|
|
self.sprout_nullifiers == other.sprout_nullifiers &&
|
|
|
|
self.sapling_nullifiers == other.sapling_nullifiers &&
|
|
|
|
self.orchard_nullifiers == other.orchard_nullifiers &&
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
// transparent address indexes
|
|
|
|
self.partial_transparent_transfers == other.partial_transparent_transfers &&
|
|
|
|
|
2021-07-14 15:23:54 -07:00
|
|
|
// proof of work
|
2021-08-25 06:57:07 -07:00
|
|
|
self.partial_cumulative_work == other.partial_cumulative_work &&
|
|
|
|
|
|
|
|
// chain value pool balances
|
|
|
|
self.chain_value_pools == other.chain_value_pools
|
2021-07-14 15:23:54 -07:00
|
|
|
}
|
|
|
|
|
2021-07-09 07:22:15 -07:00
|
|
|
/// Push a contextually valid non-finalized block into this chain as the new tip.
|
|
|
|
///
|
2022-03-10 02:02:19 -08:00
|
|
|
/// If the block is invalid, drops this chain, and returns an error.
|
2021-08-25 06:57:07 -07:00
|
|
|
///
|
|
|
|
/// Note: a [`ContextuallyValidBlock`] isn't actually contextually valid until
|
2022-06-13 18:22:16 -07:00
|
|
|
/// [`Self::update_chain_tip_with`] returns success.
|
2020-11-21 10:41:53 -08:00
|
|
|
#[instrument(level = "debug", skip(self, block), fields(block = %block.block))]
|
2022-03-10 02:02:19 -08:00
|
|
|
pub fn push(mut self, block: ContextuallyValidBlock) -> Result<Chain, ValidateContextError> {
|
2020-11-09 10:05:18 -08:00
|
|
|
// update cumulative data members
|
2022-03-10 02:02:19 -08:00
|
|
|
self.update_chain_tip_with(&block)?;
|
2022-03-09 18:44:39 -08:00
|
|
|
|
2020-11-24 19:30:29 -08:00
|
|
|
tracing::debug!(block = %block.block, "adding block to chain");
|
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.blocks.insert(block.height, block);
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2022-03-10 02:02:19 -08:00
|
|
|
Ok(self)
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
/// Pops the lowest height block of the non-finalized portion of a chain,
|
|
|
|
/// and returns it with its associated treestate.
|
2020-11-21 10:41:53 -08:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-09-06 02:32:54 -07:00
|
|
|
pub(crate) fn pop_root(&mut self) -> (ContextuallyValidBlock, Treestate) {
|
|
|
|
// Obtain the lowest height.
|
2022-04-14 06:34:31 -07:00
|
|
|
let block_height = self.non_finalized_root_height();
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
// Obtain the treestate associated with the block being finalized.
|
|
|
|
let treestate = self
|
|
|
|
.treestate(block_height.into())
|
|
|
|
.expect("The treestate must be present for the root height.");
|
|
|
|
|
|
|
|
// Remove the lowest height block from `self.blocks`.
|
2020-11-09 10:05:18 -08:00
|
|
|
let block = self
|
|
|
|
.blocks
|
|
|
|
.remove(&block_height)
|
|
|
|
.expect("only called while blocks is populated");
|
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
// Update cumulative data members.
|
2021-08-25 06:57:07 -07:00
|
|
|
self.revert_chain_with(&block, RevertPosition::Root);
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
(block, treestate)
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2022-04-14 06:34:31 -07:00
|
|
|
/// Returns the height of the chain root.
|
|
|
|
pub fn non_finalized_root_height(&self) -> block::Height {
|
2020-11-09 10:05:18 -08:00
|
|
|
self.blocks
|
|
|
|
.keys()
|
|
|
|
.next()
|
|
|
|
.cloned()
|
|
|
|
.expect("only called while blocks is populated")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fork a chain at the block with the given hash, if it is part of this
|
|
|
|
/// chain.
|
2021-07-29 06:37:18 -07:00
|
|
|
///
|
2022-07-14 17:39:41 -07:00
|
|
|
/// The passed trees must match the trees of the finalized tip. They are
|
|
|
|
/// extended by the commitments from the newly forked chain up to the passed
|
|
|
|
/// `fork_tip`.
|
2022-06-27 23:22:07 -07:00
|
|
|
#[allow(clippy::unwrap_in_result)]
|
2021-07-29 06:37:18 -07:00
|
|
|
pub fn fork(
|
|
|
|
&self,
|
|
|
|
fork_tip: block::Hash,
|
2022-07-14 17:39:41 -07:00
|
|
|
sprout_note_commitment_tree: Arc<sprout::tree::NoteCommitmentTree>,
|
|
|
|
sapling_note_commitment_tree: Arc<sapling::tree::NoteCommitmentTree>,
|
|
|
|
orchard_note_commitment_tree: Arc<orchard::tree::NoteCommitmentTree>,
|
2022-07-22 09:19:11 -07:00
|
|
|
history_tree: Arc<HistoryTree>,
|
2021-07-29 06:37:18 -07:00
|
|
|
) -> Result<Option<Self>, ValidateContextError> {
|
2020-11-09 10:05:18 -08:00
|
|
|
if !self.height_by_hash.contains_key(&fork_tip) {
|
2021-06-29 20:52:31 -07:00
|
|
|
return Ok(None);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:42:40 -07:00
|
|
|
let mut forked = self.with_trees(
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_note_commitment_tree,
|
2021-08-11 06:42:40 -07:00
|
|
|
sapling_note_commitment_tree,
|
|
|
|
orchard_note_commitment_tree,
|
|
|
|
history_tree,
|
|
|
|
);
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Revert blocks above the fork
|
2020-11-09 10:05:18 -08:00
|
|
|
while forked.non_finalized_tip_hash() != fork_tip {
|
|
|
|
forked.pop_tip();
|
|
|
|
}
|
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Rebuild trees from the finalized tip, because we haven't implemented reverts yet.
|
|
|
|
forked.rebuild_trees_parallel()?;
|
|
|
|
|
|
|
|
Ok(Some(forked))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rebuild the note commitment and history trees after a chain fork,
|
|
|
|
/// starting from the finalized tip trees, using parallel `rayon` threads.
|
|
|
|
///
|
|
|
|
/// Note commitments and history trees are not removed from the Chain during a fork,
|
|
|
|
/// because we don't support that operation yet. Instead, we recreate the tree
|
|
|
|
/// from the finalized tip.
|
|
|
|
///
|
|
|
|
/// TODO: remove trees and anchors above the fork, to save CPU time (#4794)
|
|
|
|
#[allow(clippy::unwrap_in_result)]
|
|
|
|
pub fn rebuild_trees_parallel(&mut self) -> Result<(), ValidateContextError> {
|
2022-07-21 16:17:34 -07:00
|
|
|
let start_time = Instant::now();
|
2022-07-22 09:19:11 -07:00
|
|
|
let rebuilt_block_count = self.blocks.len();
|
|
|
|
let fork_height = self.non_finalized_tip_height();
|
|
|
|
let fork_tip = self.non_finalized_tip_hash();
|
2022-07-21 16:17:34 -07:00
|
|
|
|
|
|
|
info!(
|
|
|
|
?rebuilt_block_count,
|
|
|
|
?fork_height,
|
|
|
|
?fork_tip,
|
|
|
|
"starting to rebuild note commitment trees after a non-finalized chain fork",
|
|
|
|
);
|
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Prepare data for parallel execution
|
|
|
|
let block_list = self
|
|
|
|
.blocks
|
|
|
|
.iter()
|
|
|
|
.map(|(height, block)| (*height, block.block.clone()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// TODO: use NoteCommitmentTrees to store the trees as well?
|
|
|
|
let mut nct = NoteCommitmentTrees {
|
|
|
|
sprout: self.sprout_note_commitment_tree.clone(),
|
|
|
|
sapling: self.sapling_note_commitment_tree.clone(),
|
|
|
|
orchard: self.orchard_note_commitment_tree.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut note_result = None;
|
|
|
|
let mut history_result = None;
|
|
|
|
|
|
|
|
// Run 4 tasks in parallel:
|
|
|
|
// - sprout, sapling, and orchard tree updates and (redundant) root calculations
|
|
|
|
// - history tree updates
|
|
|
|
rayon::in_place_scope_fifo(|scope| {
|
|
|
|
// Spawns a separate rayon task for each note commitment tree.
|
|
|
|
//
|
|
|
|
// TODO: skip the unused root calculations? (redundant after #4794)
|
|
|
|
note_result = Some(nct.update_trees_parallel_list(block_list));
|
2022-07-14 17:39:41 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
scope.spawn_fifo(|_scope| {
|
|
|
|
history_result = Some(self.rebuild_history_tree());
|
|
|
|
});
|
|
|
|
});
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
note_result.expect("scope has already finished")?;
|
|
|
|
history_result.expect("scope has already finished")?;
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Update the note commitment trees in the chain.
|
|
|
|
self.sprout_note_commitment_tree = nct.sprout;
|
|
|
|
self.sapling_note_commitment_tree = nct.sapling;
|
|
|
|
self.orchard_note_commitment_tree = nct.orchard;
|
|
|
|
|
|
|
|
let rebuild_time = start_time.elapsed();
|
|
|
|
let rebuild_time_per_block =
|
|
|
|
rebuild_time / rebuilt_block_count.try_into().expect("fits in u32");
|
|
|
|
info!(
|
|
|
|
rebuild_time = ?humantime_milliseconds(rebuild_time),
|
|
|
|
rebuild_time_per_block = ?humantime_milliseconds(rebuild_time_per_block),
|
|
|
|
?rebuilt_block_count,
|
|
|
|
?fork_height,
|
|
|
|
?fork_tip,
|
|
|
|
"finished rebuilding note commitment trees after a non-finalized chain fork",
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-11 06:42:40 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// Rebuild the history tree after a chain fork.
|
|
|
|
///
|
|
|
|
/// TODO: remove trees and anchors above the fork, to save CPU time (#4794)
|
|
|
|
#[allow(clippy::unwrap_in_result)]
|
|
|
|
pub fn rebuild_history_tree(&mut self) -> Result<(), ValidateContextError> {
|
|
|
|
for block in self.blocks.values() {
|
2021-08-11 06:42:40 -07:00
|
|
|
// Note that anchors don't need to be recreated since they are already
|
|
|
|
// handled in revert_chain_state_with.
|
2022-07-22 09:19:11 -07:00
|
|
|
let sapling_root = self
|
2021-08-11 06:42:40 -07:00
|
|
|
.sapling_anchors_by_height
|
|
|
|
.get(&block.height)
|
|
|
|
.expect("Sapling anchors must exist for pre-fork blocks");
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
let orchard_root = self
|
2021-08-11 06:42:40 -07:00
|
|
|
.orchard_anchors_by_height
|
|
|
|
.get(&block.height)
|
|
|
|
.expect("Orchard anchors must exist for pre-fork blocks");
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
let history_tree_mut = Arc::make_mut(&mut self.history_tree);
|
|
|
|
history_tree_mut.push(
|
2021-08-11 06:42:40 -07:00
|
|
|
self.network,
|
|
|
|
block.block.clone(),
|
|
|
|
*sapling_root,
|
|
|
|
*orchard_root,
|
|
|
|
)?;
|
2021-07-29 06:37:18 -07:00
|
|
|
}
|
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-07-21 16:17:34 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// Returns the [`Network`] for this chain.
|
|
|
|
pub fn network(&self) -> Network {
|
|
|
|
self.network
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2022-03-17 11:48:13 -07:00
|
|
|
/// Returns the [`ContextuallyValidBlock`] with [`block::Hash`] or
|
2022-03-16 17:37:44 -07:00
|
|
|
/// [`Height`](zebra_chain::block::Height), if it exists in this chain.
|
2022-03-15 12:50:28 -07:00
|
|
|
pub fn block(&self, hash_or_height: HashOrHeight) -> Option<&ContextuallyValidBlock> {
|
|
|
|
let height =
|
|
|
|
hash_or_height.height_or_else(|hash| self.height_by_hash.get(&hash).cloned())?;
|
|
|
|
|
|
|
|
self.blocks.get(&height)
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:48:13 -07:00
|
|
|
/// Returns the [`Transaction`] with [`transaction::Hash`], if it exists in this chain.
|
2022-03-24 02:45:37 -07:00
|
|
|
pub fn transaction(
|
|
|
|
&self,
|
|
|
|
hash: transaction::Hash,
|
|
|
|
) -> Option<(&Arc<Transaction>, block::Height)> {
|
2022-06-30 16:00:23 -07:00
|
|
|
self.tx_loc_by_hash.get(&hash).map(|tx_loc| {
|
2022-04-12 10:21:46 -07:00
|
|
|
(
|
|
|
|
&self.blocks[&tx_loc.height].block.transactions[tx_loc.index.as_usize()],
|
|
|
|
tx_loc.height,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the [`Transaction`] at [`TransactionLocation`], if it exists in this chain.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn transaction_by_loc(&self, tx_loc: TransactionLocation) -> Option<&Arc<Transaction>> {
|
|
|
|
self.blocks
|
|
|
|
.get(&tx_loc.height)?
|
|
|
|
.block
|
|
|
|
.transactions
|
|
|
|
.get(tx_loc.index.as_usize())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the [`transaction::Hash`] for the transaction at [`TransactionLocation`],
|
|
|
|
/// if it exists in this chain.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn transaction_hash_by_loc(
|
|
|
|
&self,
|
|
|
|
tx_loc: TransactionLocation,
|
|
|
|
) -> Option<&transaction::Hash> {
|
|
|
|
self.blocks
|
|
|
|
.get(&tx_loc.height)?
|
|
|
|
.transaction_hashes
|
|
|
|
.get(tx_loc.index.as_usize())
|
2022-03-17 11:48:13 -07:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:26:17 -07:00
|
|
|
/// Returns the [`block::Hash`] for `height`, if it exists in this chain.
|
|
|
|
pub fn hash_by_height(&self, height: Height) -> Option<block::Hash> {
|
|
|
|
let hash = self.blocks.get(&height)?.hash;
|
|
|
|
|
|
|
|
Some(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the [`Height`] for `hash`, if it exists in this chain.
|
|
|
|
pub fn height_by_hash(&self, hash: block::Hash) -> Option<Height> {
|
|
|
|
self.height_by_hash.get(&hash).cloned()
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:35:37 -07:00
|
|
|
/// Returns the non-finalized tip block height and hash.
|
|
|
|
pub fn non_finalized_tip(&self) -> (Height, block::Hash) {
|
2022-04-14 06:34:31 -07:00
|
|
|
(
|
|
|
|
self.non_finalized_tip_height(),
|
2022-09-13 17:35:37 -07:00
|
|
|
self.non_finalized_tip_hash(),
|
2022-04-14 06:34:31 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
/// Returns the Sprout
|
|
|
|
/// [`NoteCommitmentTree`](sprout::tree::NoteCommitmentTree) specified by a
|
|
|
|
/// [`HashOrHeight`], if it exists in the non-finalized [`Chain`].
|
|
|
|
pub fn sprout_tree(
|
|
|
|
&self,
|
|
|
|
hash_or_height: HashOrHeight,
|
|
|
|
) -> Option<Arc<sprout::tree::NoteCommitmentTree>> {
|
|
|
|
let height =
|
|
|
|
hash_or_height.height_or_else(|hash| self.height_by_hash.get(&hash).cloned())?;
|
|
|
|
|
|
|
|
self.sprout_trees_by_height.get(&height).cloned()
|
|
|
|
}
|
|
|
|
|
2022-05-12 00:00:12 -07:00
|
|
|
/// Returns the Sapling
|
|
|
|
/// [`NoteCommitmentTree`](sapling::tree::NoteCommitmentTree) specified by a
|
2022-09-06 02:32:54 -07:00
|
|
|
/// [`HashOrHeight`], if it exists in the non-finalized [`Chain`].
|
2022-05-12 00:00:12 -07:00
|
|
|
pub fn sapling_tree(
|
|
|
|
&self,
|
|
|
|
hash_or_height: HashOrHeight,
|
2022-07-14 17:39:41 -07:00
|
|
|
) -> Option<Arc<sapling::tree::NoteCommitmentTree>> {
|
2022-05-12 00:00:12 -07:00
|
|
|
let height =
|
|
|
|
hash_or_height.height_or_else(|hash| self.height_by_hash.get(&hash).cloned())?;
|
|
|
|
|
2022-07-14 17:39:41 -07:00
|
|
|
self.sapling_trees_by_height.get(&height).cloned()
|
2022-05-12 00:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Orchard
|
|
|
|
/// [`NoteCommitmentTree`](orchard::tree::NoteCommitmentTree) specified by a
|
2022-09-06 02:32:54 -07:00
|
|
|
/// [`HashOrHeight`], if it exists in the non-finalized [`Chain`].
|
2022-05-12 00:00:12 -07:00
|
|
|
pub fn orchard_tree(
|
|
|
|
&self,
|
|
|
|
hash_or_height: HashOrHeight,
|
2022-07-14 17:39:41 -07:00
|
|
|
) -> Option<Arc<orchard::tree::NoteCommitmentTree>> {
|
2022-05-12 00:00:12 -07:00
|
|
|
let height =
|
|
|
|
hash_or_height.height_or_else(|hash| self.height_by_hash.get(&hash).cloned())?;
|
|
|
|
|
2022-07-14 17:39:41 -07:00
|
|
|
self.orchard_trees_by_height.get(&height).cloned()
|
2022-05-12 00:00:12 -07:00
|
|
|
}
|
|
|
|
|
2022-09-06 02:32:54 -07:00
|
|
|
/// Returns the [`HistoryTree`] specified by a [`HashOrHeight`], if it
|
|
|
|
/// exists in the non-finalized [`Chain`].
|
|
|
|
pub fn history_tree(&self, hash_or_height: HashOrHeight) -> Option<Arc<HistoryTree>> {
|
|
|
|
let height =
|
|
|
|
hash_or_height.height_or_else(|hash| self.height_by_hash.get(&hash).cloned())?;
|
|
|
|
|
|
|
|
self.history_trees_by_height.get(&height).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn treestate(&self, hash_or_height: HashOrHeight) -> Option<Treestate> {
|
|
|
|
let sprout_tree = self.sprout_tree(hash_or_height)?;
|
|
|
|
let sapling_tree = self.sapling_tree(hash_or_height)?;
|
|
|
|
let orchard_tree = self.orchard_tree(hash_or_height)?;
|
|
|
|
let history_tree = self.history_tree(hash_or_height)?;
|
|
|
|
|
|
|
|
Some(Treestate::new(
|
|
|
|
sprout_tree,
|
|
|
|
sapling_tree,
|
|
|
|
orchard_tree,
|
|
|
|
history_tree,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:44:39 -08:00
|
|
|
/// Returns the block hash of the tip block.
|
2020-11-09 10:05:18 -08:00
|
|
|
pub fn non_finalized_tip_hash(&self) -> block::Hash {
|
|
|
|
self.blocks
|
|
|
|
.values()
|
|
|
|
.next_back()
|
|
|
|
.expect("only called while blocks is populated")
|
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
|
|
|
.hash
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2022-04-14 06:34:31 -07:00
|
|
|
/// Returns the non-finalized root block hash and height.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn non_finalized_root(&self) -> (block::Hash, block::Height) {
|
|
|
|
(
|
|
|
|
self.non_finalized_root_hash(),
|
|
|
|
self.non_finalized_root_height(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:44:39 -08:00
|
|
|
/// Returns the block hash of the non-finalized root block.
|
|
|
|
pub fn non_finalized_root_hash(&self) -> block::Hash {
|
|
|
|
self.blocks
|
|
|
|
.values()
|
|
|
|
.next()
|
|
|
|
.expect("only called while blocks is populated")
|
|
|
|
.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the block hash of the `n`th block from the non-finalized root.
|
|
|
|
///
|
2022-04-14 06:34:31 -07:00
|
|
|
/// This is the block at `non_finalized_root_height() + n`.
|
2022-03-09 18:44:39 -08:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn non_finalized_nth_hash(&self, n: usize) -> Option<block::Hash> {
|
|
|
|
self.blocks.values().nth(n).map(|block| block.hash)
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
/// Remove the highest height block of the non-finalized portion of a chain.
|
|
|
|
fn pop_tip(&mut self) {
|
|
|
|
let block_height = self.non_finalized_tip_height();
|
|
|
|
|
|
|
|
let block = self
|
|
|
|
.blocks
|
|
|
|
.remove(&block_height)
|
|
|
|
.expect("only called while blocks is populated");
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
!self.blocks.is_empty(),
|
|
|
|
"Non-finalized chains must have at least one block to be valid"
|
|
|
|
);
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
self.revert_chain_with(&block, RevertPosition::Tip);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
/// Return the non-finalized tip height for this chain.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if called while the chain is empty,
|
|
|
|
/// or while the chain is updating its internal state with the first block.
|
2020-11-09 10:05:18 -08:00
|
|
|
pub fn non_finalized_tip_height(&self) -> block::Height {
|
2021-08-25 06:57:07 -07:00
|
|
|
self.max_block_height()
|
2020-11-09 10:05:18 -08:00
|
|
|
.expect("only called while blocks is populated")
|
|
|
|
}
|
2020-11-06 14:31:25 -08:00
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
/// Return the non-finalized tip height for this chain,
|
|
|
|
/// or `None` if `self.blocks` is empty.
|
|
|
|
fn max_block_height(&self) -> Option<block::Height> {
|
|
|
|
self.blocks.keys().next_back().cloned()
|
|
|
|
}
|
|
|
|
|
2022-03-11 05:58:22 -08:00
|
|
|
/// Return the non-finalized tip block for this chain,
|
|
|
|
/// or `None` if `self.blocks` is empty.
|
|
|
|
pub fn tip_block(&self) -> Option<&ContextuallyValidBlock> {
|
|
|
|
self.blocks.values().next_back()
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:44:39 -08:00
|
|
|
/// Returns true if the non-finalized part of this chain is empty.
|
2020-11-06 14:31:25 -08:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.blocks.is_empty()
|
|
|
|
}
|
2021-07-22 16:40:15 -07:00
|
|
|
|
2022-03-09 18:44:39 -08:00
|
|
|
/// Returns the non-finalized length of this chain.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
2021-07-22 16:40:15 -07:00
|
|
|
/// Returns the unspent transaction outputs (UTXOs) in this non-finalized chain.
|
|
|
|
///
|
|
|
|
/// Callers should also check the finalized state for available UTXOs.
|
|
|
|
/// If UTXOs remain unspent when a block is finalized, they are stored in the finalized state,
|
|
|
|
/// and removed from the relevant chain(s).
|
2022-04-12 10:21:46 -07:00
|
|
|
pub fn unspent_utxos(&self) -> HashMap<transparent::OutPoint, transparent::OrderedUtxo> {
|
2021-07-22 16:40:15 -07:00
|
|
|
let mut unspent_utxos = self.created_utxos.clone();
|
2022-09-15 21:13:26 -07:00
|
|
|
unspent_utxos.retain(|outpoint, _utxo| !self.spent_utxos.contains(outpoint));
|
2022-04-12 10:21:46 -07:00
|
|
|
|
2021-07-22 16:40:15 -07:00
|
|
|
unspent_utxos
|
|
|
|
}
|
2021-07-29 06:37:18 -07:00
|
|
|
|
2022-09-15 21:13:26 -07:00
|
|
|
/// Returns the [`transparent::Utxo`] pointed to by the given
|
|
|
|
/// [`transparent::OutPoint`] if it was created by this chain.
|
|
|
|
///
|
|
|
|
/// UTXOs are returned regardless of whether they have been spent.
|
|
|
|
pub fn created_utxo(&self, outpoint: &transparent::OutPoint) -> Option<transparent::Utxo> {
|
|
|
|
if let Some(utxo) = self.created_utxos.get(outpoint) {
|
|
|
|
return Some(utxo.utxo.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-04-14 06:34:31 -07:00
|
|
|
// Address index queries
|
|
|
|
|
|
|
|
/// Returns the transparent transfers for `addresses` in this non-finalized chain.
|
|
|
|
///
|
|
|
|
/// If none of the addresses have an address index, returns an empty iterator.
|
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Callers should apply the returned indexes to the corresponding finalized state indexes.
|
|
|
|
///
|
|
|
|
/// The combined result will only be correct if the chains match.
|
|
|
|
/// The exact type of match varies by query.
|
|
|
|
pub fn partial_transparent_indexes<'a>(
|
|
|
|
&'a self,
|
|
|
|
addresses: &'a HashSet<transparent::Address>,
|
|
|
|
) -> impl Iterator<Item = &TransparentTransfers> {
|
|
|
|
addresses
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.flat_map(|address| self.partial_transparent_transfers.get(&address))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the transparent balance change for `addresses` in this non-finalized chain.
|
|
|
|
///
|
|
|
|
/// If the balance doesn't change for any of the addresses, returns zero.
|
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Callers should apply this balance change to the finalized state balance for `addresses`.
|
|
|
|
///
|
|
|
|
/// The total balance will only be correct if this partial chain matches the finalized state.
|
|
|
|
/// Specifically, the root of this partial chain must be a child block of the finalized tip.
|
|
|
|
pub fn partial_transparent_balance_change(
|
|
|
|
&self,
|
|
|
|
addresses: &HashSet<transparent::Address>,
|
|
|
|
) -> Amount<NegativeAllowed> {
|
|
|
|
let balance_change: Result<Amount<NegativeAllowed>, _> = self
|
|
|
|
.partial_transparent_indexes(addresses)
|
|
|
|
.map(|transfers| transfers.balance())
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
balance_change.expect(
|
|
|
|
"unexpected amount overflow: value balances are valid, so partial sum should be valid",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:34:53 -07:00
|
|
|
/// Returns the transparent UTXO changes for `addresses` in this non-finalized chain.
|
|
|
|
///
|
|
|
|
/// If the UTXOs don't change for any of the addresses, returns empty lists.
|
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Callers should apply these non-finalized UTXO changes to the finalized state UTXOs.
|
|
|
|
///
|
|
|
|
/// The UTXOs will only be correct if the non-finalized chain matches or overlaps with
|
|
|
|
/// the finalized state.
|
|
|
|
///
|
|
|
|
/// Specifically, a block in the partial chain must be a child block of the finalized tip.
|
|
|
|
/// (But the child block does not have to be the partial chain root.)
|
|
|
|
pub fn partial_transparent_utxo_changes(
|
|
|
|
&self,
|
|
|
|
addresses: &HashSet<transparent::Address>,
|
|
|
|
) -> (
|
|
|
|
BTreeMap<OutputLocation, transparent::Output>,
|
|
|
|
BTreeSet<OutputLocation>,
|
|
|
|
) {
|
|
|
|
let created_utxos = self
|
|
|
|
.partial_transparent_indexes(addresses)
|
|
|
|
.flat_map(|transfers| transfers.created_utxos())
|
|
|
|
.map(|(out_loc, output)| (*out_loc, output.clone()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let spent_utxos = self
|
|
|
|
.partial_transparent_indexes(addresses)
|
|
|
|
.flat_map(|transfers| transfers.spent_utxos())
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
(created_utxos, spent_utxos)
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:19:26 -07:00
|
|
|
/// Returns the [`transaction::Hash`]es used by `addresses` to receive or spend funds,
|
|
|
|
/// in the non-finalized chain, filtered using the `query_height_range`.
|
2022-04-19 06:34:53 -07:00
|
|
|
///
|
|
|
|
/// If none of the addresses receive or spend funds in this partial chain, returns an empty list.
|
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Callers should combine these non-finalized transactions with the finalized state transactions.
|
|
|
|
///
|
|
|
|
/// The transaction IDs will only be correct if the non-finalized chain matches or overlaps with
|
|
|
|
/// the finalized state.
|
|
|
|
///
|
|
|
|
/// Specifically, a block in the partial chain must be a child block of the finalized tip.
|
|
|
|
/// (But the child block does not have to be the partial chain root.)
|
|
|
|
///
|
|
|
|
/// This condition does not apply if there is only one address.
|
|
|
|
/// Since address transactions are only appended by blocks,
|
|
|
|
/// and the finalized state query reads them in order,
|
|
|
|
/// it is impossible to get inconsistent transactions for a single address.
|
|
|
|
pub fn partial_transparent_tx_ids(
|
|
|
|
&self,
|
|
|
|
addresses: &HashSet<transparent::Address>,
|
2022-04-21 13:19:26 -07:00
|
|
|
query_height_range: RangeInclusive<Height>,
|
2022-04-19 06:34:53 -07:00
|
|
|
) -> BTreeMap<TransactionLocation, transaction::Hash> {
|
|
|
|
self.partial_transparent_indexes(addresses)
|
2022-06-30 16:00:23 -07:00
|
|
|
.flat_map(|transfers| {
|
|
|
|
transfers.tx_ids(&self.tx_loc_by_hash, query_height_range.clone())
|
|
|
|
})
|
2022-04-19 06:34:53 -07:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cloning
|
|
|
|
|
2021-07-29 06:37:18 -07:00
|
|
|
/// Clone the Chain but not the history and note commitment trees, using
|
|
|
|
/// the specified trees instead.
|
|
|
|
///
|
|
|
|
/// Useful when forking, where the trees are rebuilt anyway.
|
|
|
|
fn with_trees(
|
|
|
|
&self,
|
2022-07-14 17:39:41 -07:00
|
|
|
sprout_note_commitment_tree: Arc<sprout::tree::NoteCommitmentTree>,
|
|
|
|
sapling_note_commitment_tree: Arc<sapling::tree::NoteCommitmentTree>,
|
|
|
|
orchard_note_commitment_tree: Arc<orchard::tree::NoteCommitmentTree>,
|
2022-07-22 09:19:11 -07:00
|
|
|
history_tree: Arc<HistoryTree>,
|
2021-07-29 06:37:18 -07:00
|
|
|
) -> Self {
|
|
|
|
Chain {
|
2021-08-11 06:42:40 -07:00
|
|
|
network: self.network,
|
2021-07-29 06:37:18 -07:00
|
|
|
blocks: self.blocks.clone(),
|
|
|
|
height_by_hash: self.height_by_hash.clone(),
|
2022-06-30 16:00:23 -07:00
|
|
|
tx_loc_by_hash: self.tx_loc_by_hash.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
created_utxos: self.created_utxos.clone(),
|
|
|
|
spent_utxos: self.spent_utxos.clone(),
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_note_commitment_tree,
|
2022-03-14 22:18:18 -07:00
|
|
|
sprout_trees_by_anchor: self.sprout_trees_by_anchor.clone(),
|
2022-09-06 02:32:54 -07:00
|
|
|
sprout_trees_by_height: self.sprout_trees_by_height.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
sapling_note_commitment_tree,
|
2022-03-14 22:18:18 -07:00
|
|
|
sapling_trees_by_height: self.sapling_trees_by_height.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
orchard_note_commitment_tree,
|
2022-03-14 22:18:18 -07:00
|
|
|
orchard_trees_by_height: self.orchard_trees_by_height.clone(),
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_anchors: self.sprout_anchors.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
sapling_anchors: self.sapling_anchors.clone(),
|
|
|
|
orchard_anchors: self.orchard_anchors.clone(),
|
2021-11-30 02:05:58 -08:00
|
|
|
sprout_anchors_by_height: self.sprout_anchors_by_height.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
sapling_anchors_by_height: self.sapling_anchors_by_height.clone(),
|
|
|
|
orchard_anchors_by_height: self.orchard_anchors_by_height.clone(),
|
|
|
|
sprout_nullifiers: self.sprout_nullifiers.clone(),
|
|
|
|
sapling_nullifiers: self.sapling_nullifiers.clone(),
|
|
|
|
orchard_nullifiers: self.orchard_nullifiers.clone(),
|
2022-04-12 10:21:46 -07:00
|
|
|
partial_transparent_transfers: self.partial_transparent_transfers.clone(),
|
2021-07-29 06:37:18 -07:00
|
|
|
partial_cumulative_work: self.partial_cumulative_work,
|
2021-08-11 06:42:40 -07:00
|
|
|
history_tree,
|
2022-08-28 23:59:25 -07:00
|
|
|
history_trees_by_height: self.history_trees_by_height.clone(),
|
2021-08-25 06:57:07 -07:00
|
|
|
chain_value_pools: self.chain_value_pools,
|
2021-07-29 06:37:18 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// Update the chain tip with the `contextually_valid` block,
|
|
|
|
/// running note commitment tree updates in parallel with other updates.
|
|
|
|
///
|
|
|
|
/// Used to implement `update_chain_tip_with::<ContextuallyValidBlock>`.
|
|
|
|
#[instrument(skip(self, contextually_valid), fields(block = %contextually_valid.block))]
|
|
|
|
#[allow(clippy::unwrap_in_result)]
|
|
|
|
fn update_chain_tip_with_block_parallel(
|
|
|
|
&mut self,
|
|
|
|
contextually_valid: &ContextuallyValidBlock,
|
|
|
|
) -> Result<(), ValidateContextError> {
|
|
|
|
let height = contextually_valid.height;
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Prepare data for parallel execution
|
|
|
|
//
|
|
|
|
// TODO: use NoteCommitmentTrees to store the trees as well?
|
|
|
|
let mut nct = NoteCommitmentTrees {
|
|
|
|
sprout: self.sprout_note_commitment_tree.clone(),
|
|
|
|
sapling: self.sapling_note_commitment_tree.clone(),
|
|
|
|
orchard: self.orchard_note_commitment_tree.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tree_result = None;
|
|
|
|
let mut partial_result = None;
|
|
|
|
|
|
|
|
// Run 4 tasks in parallel:
|
|
|
|
// - sprout, sapling, and orchard tree updates and root calculations
|
|
|
|
// - the rest of the Chain updates
|
|
|
|
rayon::in_place_scope_fifo(|scope| {
|
|
|
|
// Spawns a separate rayon task for each note commitment tree
|
|
|
|
tree_result = Some(nct.update_trees_parallel(&contextually_valid.block.clone()));
|
|
|
|
|
|
|
|
scope.spawn_fifo(|_scope| {
|
|
|
|
partial_result =
|
|
|
|
Some(self.update_chain_tip_with_block_except_trees(contextually_valid));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tree_result.expect("scope has already finished")?;
|
|
|
|
partial_result.expect("scope has already finished")?;
|
|
|
|
|
|
|
|
// Update the note commitment trees in the chain.
|
|
|
|
self.sprout_note_commitment_tree = nct.sprout;
|
|
|
|
self.sapling_note_commitment_tree = nct.sapling;
|
|
|
|
self.orchard_note_commitment_tree = nct.orchard;
|
|
|
|
|
|
|
|
// Do the Chain updates with data dependencies on note commitment tree updates
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Update the note commitment trees indexed by height.
|
2022-09-06 02:32:54 -07:00
|
|
|
self.sprout_trees_by_height
|
|
|
|
.insert(height, self.sprout_note_commitment_tree.clone());
|
2022-07-22 09:19:11 -07:00
|
|
|
self.sapling_trees_by_height
|
|
|
|
.insert(height, self.sapling_note_commitment_tree.clone());
|
|
|
|
self.orchard_trees_by_height
|
|
|
|
.insert(height, self.orchard_note_commitment_tree.clone());
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// Having updated all the note commitment trees and nullifier sets in
|
|
|
|
// this block, the roots of the note commitment trees as of the last
|
|
|
|
// transaction are the treestates of this block.
|
|
|
|
//
|
|
|
|
// Use the previously cached roots, which were calculated in parallel.
|
|
|
|
let sprout_root = self.sprout_note_commitment_tree.root();
|
|
|
|
self.sprout_anchors.insert(sprout_root);
|
|
|
|
self.sprout_anchors_by_height.insert(height, sprout_root);
|
|
|
|
self.sprout_trees_by_anchor
|
|
|
|
.insert(sprout_root, self.sprout_note_commitment_tree.clone());
|
|
|
|
let sapling_root = self.sapling_note_commitment_tree.root();
|
|
|
|
self.sapling_anchors.insert(sapling_root);
|
|
|
|
self.sapling_anchors_by_height.insert(height, sapling_root);
|
2020-11-09 10:05:18 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
let orchard_root = self.orchard_note_commitment_tree.root();
|
|
|
|
self.orchard_anchors.insert(orchard_root);
|
|
|
|
self.orchard_anchors_by_height.insert(height, orchard_root);
|
|
|
|
|
|
|
|
// TODO: update the history trees in a rayon thread, if they show up in CPU profiles
|
|
|
|
let history_tree_mut = Arc::make_mut(&mut self.history_tree);
|
|
|
|
history_tree_mut.push(
|
|
|
|
self.network,
|
|
|
|
contextually_valid.block.clone(),
|
|
|
|
sapling_root,
|
|
|
|
orchard_root,
|
|
|
|
)?;
|
|
|
|
|
2022-08-28 23:59:25 -07:00
|
|
|
self.history_trees_by_height
|
|
|
|
.insert(height, self.history_tree.clone());
|
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the chain tip with the `contextually_valid` block,
|
|
|
|
/// except for the note commitment and history tree updates.
|
|
|
|
///
|
|
|
|
/// Used to implement `update_chain_tip_with::<ContextuallyValidBlock>`.
|
2021-07-19 06:52:32 -07:00
|
|
|
#[instrument(skip(self, contextually_valid), fields(block = %contextually_valid.block))]
|
2022-06-27 23:22:07 -07:00
|
|
|
#[allow(clippy::unwrap_in_result)]
|
2022-07-22 09:19:11 -07:00
|
|
|
fn update_chain_tip_with_block_except_trees(
|
2021-06-29 20:52:31 -07:00
|
|
|
&mut self,
|
2021-07-19 06:52:32 -07:00
|
|
|
contextually_valid: &ContextuallyValidBlock,
|
2021-06-29 20:52:31 -07:00
|
|
|
) -> Result<(), ValidateContextError> {
|
2022-04-12 10:21:46 -07:00
|
|
|
let (
|
|
|
|
block,
|
|
|
|
hash,
|
|
|
|
height,
|
|
|
|
new_outputs,
|
|
|
|
spent_outputs,
|
|
|
|
transaction_hashes,
|
|
|
|
chain_value_pool_change,
|
|
|
|
) = (
|
2021-07-19 06:52:32 -07:00
|
|
|
contextually_valid.block.as_ref(),
|
|
|
|
contextually_valid.hash,
|
|
|
|
contextually_valid.height,
|
|
|
|
&contextually_valid.new_outputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
&contextually_valid.spent_outputs,
|
2021-07-19 06:52:32 -07:00
|
|
|
&contextually_valid.transaction_hashes,
|
2021-08-25 06:57:07 -07:00
|
|
|
&contextually_valid.chain_value_pool_change,
|
2020-11-24 19:55:15 -08:00
|
|
|
);
|
2020-11-09 10:05:18 -08:00
|
|
|
|
|
|
|
// add hash to height_by_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
|
|
|
let prior_height = self.height_by_hash.insert(hash, height);
|
2020-11-09 10:05:18 -08:00
|
|
|
assert!(
|
|
|
|
prior_height.is_none(),
|
|
|
|
"block heights must be unique within a single chain"
|
|
|
|
);
|
|
|
|
|
|
|
|
// add work to partial cumulative work
|
|
|
|
let block_work = block
|
|
|
|
.header
|
|
|
|
.difficulty_threshold
|
|
|
|
.to_work()
|
|
|
|
.expect("work has already been validated");
|
|
|
|
self.partial_cumulative_work += block_work;
|
|
|
|
|
|
|
|
// for each transaction in block
|
2020-11-24 19:55:15 -08:00
|
|
|
for (transaction_index, (transaction, transaction_hash)) in block
|
|
|
|
.transactions
|
|
|
|
.iter()
|
|
|
|
.zip(transaction_hashes.iter().cloned())
|
|
|
|
.enumerate()
|
|
|
|
{
|
2021-06-01 00:53:13 -07:00
|
|
|
let (
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
joinsplit_data,
|
|
|
|
sapling_shielded_data_per_spend_anchor,
|
|
|
|
sapling_shielded_data_shared_anchor,
|
|
|
|
orchard_shielded_data,
|
|
|
|
) = match transaction.deref() {
|
2021-03-03 13:56:41 -08:00
|
|
|
V4 {
|
2020-11-09 10:05:18 -08:00
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2020-11-09 10:05:18 -08:00
|
|
|
joinsplit_data,
|
2021-03-31 14:34:25 -07:00
|
|
|
sapling_shielded_data,
|
2020-11-09 10:05:18 -08:00
|
|
|
..
|
2022-04-12 10:21:46 -07:00
|
|
|
} => (inputs, outputs, joinsplit_data, sapling_shielded_data, &None, &None),
|
2021-06-01 00:53:13 -07:00
|
|
|
V5 {
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
sapling_shielded_data,
|
|
|
|
orchard_shielded_data,
|
|
|
|
..
|
|
|
|
} => (
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
&None,
|
|
|
|
&None,
|
|
|
|
sapling_shielded_data,
|
|
|
|
orchard_shielded_data,
|
|
|
|
),
|
2021-03-03 13:56:41 -08:00
|
|
|
V1 { .. } | V2 { .. } | V3 { .. } => unreachable!(
|
2021-06-01 00:53:13 -07:00
|
|
|
"older transaction versions only exist in finalized blocks, because of the mandatory canopy checkpoint",
|
2020-11-09 10:05:18 -08:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2022-06-30 16:00:23 -07:00
|
|
|
// add key `transaction.hash` and value `(height, tx_index)` to `tx_loc_by_hash`
|
2022-04-12 10:21:46 -07:00
|
|
|
let transaction_location = TransactionLocation::from_usize(height, transaction_index);
|
2020-11-09 10:05:18 -08:00
|
|
|
let prior_pair = self
|
2022-06-30 16:00:23 -07:00
|
|
|
.tx_loc_by_hash
|
2022-04-12 10:21:46 -07:00
|
|
|
.insert(transaction_hash, transaction_location);
|
|
|
|
assert_eq!(
|
|
|
|
prior_pair, None,
|
2020-11-09 10:05:18 -08:00
|
|
|
"transactions must be unique within a single chain"
|
|
|
|
);
|
|
|
|
|
2022-05-19 19:22:01 -07:00
|
|
|
// add the utxos this produced
|
2022-04-12 10:21:46 -07:00
|
|
|
self.update_chain_tip_with(&(outputs, &transaction_hash, new_outputs))?;
|
2022-05-19 19:22:01 -07:00
|
|
|
// delete the utxos this consumed
|
2022-04-12 10:21:46 -07:00
|
|
|
self.update_chain_tip_with(&(inputs, &transaction_hash, spent_outputs))?;
|
2021-06-01 00:53:13 -07:00
|
|
|
|
|
|
|
// add the shielded data
|
2021-08-25 06:57:07 -07:00
|
|
|
self.update_chain_tip_with(joinsplit_data)?;
|
|
|
|
self.update_chain_tip_with(sapling_shielded_data_per_spend_anchor)?;
|
|
|
|
self.update_chain_tip_with(sapling_shielded_data_shared_anchor)?;
|
|
|
|
self.update_chain_tip_with(orchard_shielded_data)?;
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
2021-06-29 20:52:31 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
// update the chain value pool balances
|
|
|
|
self.update_chain_tip_with(chain_value_pool_change)?;
|
2022-03-14 22:18:18 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 08:05:35 -08:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// The revert position being performed on a chain.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
enum RevertPosition {
|
|
|
|
/// The chain root is being reverted via [`Chain::pop_root`], when a block
|
|
|
|
/// is finalized.
|
|
|
|
Root,
|
2021-08-11 06:42:40 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// The chain tip is being reverted via [`Chain::pop_tip`],
|
|
|
|
/// when a chain is forked.
|
|
|
|
Tip,
|
|
|
|
}
|
2021-07-29 06:37:18 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// Helper trait to organize inverse operations done on the [`Chain`] type.
|
|
|
|
///
|
|
|
|
/// Used to overload update and revert methods, based on the type of the argument,
|
|
|
|
/// and the position of the removed block in the chain.
|
|
|
|
///
|
|
|
|
/// This trait was motivated by the length of the `push`, [`Chain::pop_root`],
|
|
|
|
/// and [`Chain::pop_tip`] functions, and fear that it would be easy to
|
|
|
|
/// introduce bugs when updating them, unless the code was reorganized to keep
|
|
|
|
/// related operations adjacent to each other.
|
|
|
|
trait UpdateWith<T> {
|
|
|
|
/// When `T` is added to the chain tip,
|
|
|
|
/// update [`Chain`] cumulative data members to add data that are derived from `T`.
|
|
|
|
fn update_chain_tip_with(&mut self, _: &T) -> Result<(), ValidateContextError>;
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2022-07-22 09:19:11 -07:00
|
|
|
/// When `T` is removed from `position` in the chain,
|
|
|
|
/// revert [`Chain`] cumulative data members to remove data that are derived from `T`.
|
|
|
|
fn revert_chain_with(&mut self, _: &T, position: RevertPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateWith<ContextuallyValidBlock> for Chain {
|
|
|
|
#[instrument(skip(self, contextually_valid), fields(block = %contextually_valid.block))]
|
|
|
|
#[allow(clippy::unwrap_in_result)]
|
|
|
|
fn update_chain_tip_with(
|
|
|
|
&mut self,
|
|
|
|
contextually_valid: &ContextuallyValidBlock,
|
|
|
|
) -> Result<(), ValidateContextError> {
|
|
|
|
self.update_chain_tip_with_block_parallel(contextually_valid)
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 06:52:32 -07:00
|
|
|
#[instrument(skip(self, contextually_valid), fields(block = %contextually_valid.block))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn revert_chain_with(
|
|
|
|
&mut self,
|
|
|
|
contextually_valid: &ContextuallyValidBlock,
|
|
|
|
position: RevertPosition,
|
|
|
|
) {
|
2022-04-12 10:21:46 -07:00
|
|
|
let (
|
|
|
|
block,
|
|
|
|
hash,
|
|
|
|
height,
|
|
|
|
new_outputs,
|
|
|
|
spent_outputs,
|
|
|
|
transaction_hashes,
|
|
|
|
chain_value_pool_change,
|
|
|
|
) = (
|
2021-07-19 06:52:32 -07:00
|
|
|
contextually_valid.block.as_ref(),
|
|
|
|
contextually_valid.hash,
|
2021-07-29 06:37:18 -07:00
|
|
|
contextually_valid.height,
|
2021-07-19 06:52:32 -07:00
|
|
|
&contextually_valid.new_outputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
&contextually_valid.spent_outputs,
|
2021-07-19 06:52:32 -07:00
|
|
|
&contextually_valid.transaction_hashes,
|
2021-08-25 06:57:07 -07:00
|
|
|
&contextually_valid.chain_value_pool_change,
|
2020-11-24 19:55:15 -08:00
|
|
|
);
|
2020-11-09 10:05:18 -08:00
|
|
|
|
|
|
|
// remove the blocks hash from `height_by_hash`
|
|
|
|
assert!(
|
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.height_by_hash.remove(&hash).is_some(),
|
2021-07-14 05:06:43 -07:00
|
|
|
"hash must be present if block was added to chain"
|
2020-11-09 10:05:18 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// remove work from partial_cumulative_work
|
|
|
|
let block_work = block
|
|
|
|
.header
|
|
|
|
.difficulty_threshold
|
|
|
|
.to_work()
|
|
|
|
.expect("work has already been validated");
|
|
|
|
self.partial_cumulative_work -= block_work;
|
|
|
|
|
2021-08-11 06:42:40 -07:00
|
|
|
// Note: the history tree is not modified in this method.
|
|
|
|
// This method is called on two scenarios:
|
|
|
|
// - When popping the root: the history tree does not change.
|
|
|
|
// - When popping the tip: the history tree is rebuilt in fork().
|
2022-08-28 23:59:25 -07:00
|
|
|
//
|
|
|
|
// However, `history_trees_by_height` is reverted.
|
|
|
|
self.history_trees_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("History tree must be present if block was added to chain");
|
2021-08-11 06:42:40 -07:00
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
// for each transaction in block
|
2020-11-24 19:55:15 -08:00
|
|
|
for (transaction, transaction_hash) in
|
|
|
|
block.transactions.iter().zip(transaction_hashes.iter())
|
|
|
|
{
|
2021-06-01 00:53:13 -07:00
|
|
|
let (
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
joinsplit_data,
|
|
|
|
sapling_shielded_data_per_spend_anchor,
|
|
|
|
sapling_shielded_data_shared_anchor,
|
|
|
|
orchard_shielded_data,
|
|
|
|
) = match transaction.deref() {
|
2021-03-03 13:56:41 -08:00
|
|
|
V4 {
|
2020-11-09 10:05:18 -08:00
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2020-11-09 10:05:18 -08:00
|
|
|
joinsplit_data,
|
2021-03-31 14:34:25 -07:00
|
|
|
sapling_shielded_data,
|
2020-11-09 10:05:18 -08:00
|
|
|
..
|
2022-04-12 10:21:46 -07:00
|
|
|
} => (inputs, outputs, joinsplit_data, sapling_shielded_data, &None, &None),
|
2021-06-01 00:53:13 -07:00
|
|
|
V5 {
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
sapling_shielded_data,
|
|
|
|
orchard_shielded_data,
|
|
|
|
..
|
|
|
|
} => (
|
|
|
|
inputs,
|
2022-04-12 10:21:46 -07:00
|
|
|
outputs,
|
2021-06-01 00:53:13 -07:00
|
|
|
&None,
|
|
|
|
&None,
|
|
|
|
sapling_shielded_data,
|
|
|
|
orchard_shielded_data,
|
|
|
|
),
|
2021-03-03 13:56:41 -08:00
|
|
|
V1 { .. } | V2 { .. } | V3 { .. } => unreachable!(
|
2021-06-01 00:53:13 -07:00
|
|
|
"older transaction versions only exist in finalized blocks, because of the mandatory canopy checkpoint",
|
2020-11-09 10:05:18 -08:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
// remove the utxos this produced
|
|
|
|
self.revert_chain_with(&(outputs, transaction_hash, new_outputs), position);
|
2022-05-19 19:22:01 -07:00
|
|
|
// reset the utxos this consumed
|
2022-04-12 10:21:46 -07:00
|
|
|
self.revert_chain_with(&(inputs, transaction_hash, spent_outputs), position);
|
|
|
|
|
2022-06-30 16:00:23 -07:00
|
|
|
// remove `transaction.hash` from `tx_loc_by_hash`
|
2020-11-09 10:05:18 -08:00
|
|
|
assert!(
|
2022-06-30 16:00:23 -07:00
|
|
|
self.tx_loc_by_hash.remove(transaction_hash).is_some(),
|
2021-07-14 05:06:43 -07:00
|
|
|
"transactions must be present if block was added to chain"
|
2020-11-09 10:05:18 -08:00
|
|
|
);
|
|
|
|
|
2021-06-01 00:53:13 -07:00
|
|
|
// remove the shielded data
|
2021-08-25 06:57:07 -07:00
|
|
|
self.revert_chain_with(joinsplit_data, position);
|
|
|
|
self.revert_chain_with(sapling_shielded_data_per_spend_anchor, position);
|
|
|
|
self.revert_chain_with(sapling_shielded_data_shared_anchor, position);
|
|
|
|
self.revert_chain_with(orchard_shielded_data, position);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
2021-08-25 06:57:07 -07:00
|
|
|
|
2021-11-30 02:05:58 -08:00
|
|
|
let anchor = self
|
|
|
|
.sprout_anchors_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Sprout anchor must be present if block was added to chain");
|
|
|
|
assert!(
|
|
|
|
self.sprout_anchors.remove(&anchor),
|
|
|
|
"Sprout anchor must be present if block was added to chain"
|
|
|
|
);
|
2022-01-18 12:18:49 -08:00
|
|
|
if !self.sprout_anchors.contains(&anchor) {
|
|
|
|
self.sprout_trees_by_anchor.remove(&anchor);
|
|
|
|
}
|
2022-09-06 02:32:54 -07:00
|
|
|
self.sprout_trees_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Sprout note commitment tree must be present if block was added to chain");
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2021-07-29 06:37:18 -07:00
|
|
|
let anchor = self
|
|
|
|
.sapling_anchors_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Sapling anchor must be present if block was added to chain");
|
|
|
|
assert!(
|
|
|
|
self.sapling_anchors.remove(&anchor),
|
|
|
|
"Sapling anchor must be present if block was added to chain"
|
|
|
|
);
|
2022-03-14 22:18:18 -07:00
|
|
|
self.sapling_trees_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Sapling note commitment tree must be present if block was added to chain");
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2021-07-29 06:37:18 -07:00
|
|
|
let anchor = self
|
|
|
|
.orchard_anchors_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Orchard anchor must be present if block was added to chain");
|
|
|
|
assert!(
|
|
|
|
self.orchard_anchors.remove(&anchor),
|
|
|
|
"Orchard anchor must be present if block was added to chain"
|
|
|
|
);
|
2022-03-14 22:18:18 -07:00
|
|
|
self.orchard_trees_by_height
|
|
|
|
.remove(&height)
|
|
|
|
.expect("Orchard note commitment tree must be present if block was added to chain");
|
2021-08-25 06:57:07 -07:00
|
|
|
|
|
|
|
// revert the chain value pool balances, if needed
|
|
|
|
self.revert_chain_with(chain_value_pool_change, position);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
// Created UTXOs
|
|
|
|
//
|
|
|
|
// TODO: replace arguments with a struct
|
|
|
|
impl
|
|
|
|
UpdateWith<(
|
|
|
|
// The outputs from a transaction in this block
|
|
|
|
&Vec<transparent::Output>,
|
|
|
|
// The hash of the transaction that the outputs are from
|
|
|
|
&transaction::Hash,
|
|
|
|
// The UTXOs for all outputs created by this transaction (or block)
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
)> for Chain
|
|
|
|
{
|
2022-06-27 23:22:07 -07:00
|
|
|
#[allow(clippy::unwrap_in_result)]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn update_chain_tip_with(
|
2021-06-29 20:52:31 -07:00
|
|
|
&mut self,
|
2022-04-12 10:21:46 -07:00
|
|
|
&(created_outputs, creating_tx_hash, block_created_outputs): &(
|
|
|
|
&Vec<transparent::Output>,
|
|
|
|
&transaction::Hash,
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
),
|
2021-06-29 20:52:31 -07:00
|
|
|
) -> Result<(), ValidateContextError> {
|
2022-04-12 10:21:46 -07:00
|
|
|
for output_index in 0..created_outputs.len() {
|
|
|
|
let outpoint = transparent::OutPoint {
|
|
|
|
hash: *creating_tx_hash,
|
|
|
|
index: output_index.try_into().expect("valid indexes fit in u32"),
|
|
|
|
};
|
|
|
|
let created_utxo = block_created_outputs
|
|
|
|
.get(&outpoint)
|
|
|
|
.expect("new_outputs contains all created UTXOs");
|
|
|
|
|
|
|
|
// Update the chain's created UTXOs
|
|
|
|
let previous_entry = self.created_utxos.insert(outpoint, created_utxo.clone());
|
|
|
|
assert_eq!(
|
|
|
|
previous_entry, None,
|
|
|
|
"unexpected created output: duplicate update or duplicate UTXO",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update the address index with this UTXO
|
|
|
|
if let Some(receiving_address) = created_utxo.utxo.output.address(self.network) {
|
|
|
|
let address_transfers = self
|
|
|
|
.partial_transparent_transfers
|
|
|
|
.entry(receiving_address)
|
|
|
|
.or_default();
|
|
|
|
|
2022-04-14 06:34:31 -07:00
|
|
|
address_transfers.update_chain_tip_with(&(&outpoint, created_utxo))?;
|
2022-04-12 10:21:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 20:52:31 -07:00
|
|
|
Ok(())
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
fn revert_chain_with(
|
2021-07-11 19:49:33 -07:00
|
|
|
&mut self,
|
2022-04-12 10:21:46 -07:00
|
|
|
&(created_outputs, creating_tx_hash, block_created_outputs): &(
|
|
|
|
&Vec<transparent::Output>,
|
|
|
|
&transaction::Hash,
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
),
|
|
|
|
position: RevertPosition,
|
2021-07-11 19:49:33 -07:00
|
|
|
) {
|
2022-04-12 10:21:46 -07:00
|
|
|
for output_index in 0..created_outputs.len() {
|
|
|
|
let outpoint = transparent::OutPoint {
|
|
|
|
hash: *creating_tx_hash,
|
|
|
|
index: output_index.try_into().expect("valid indexes fit in u32"),
|
|
|
|
};
|
|
|
|
let created_utxo = block_created_outputs
|
|
|
|
.get(&outpoint)
|
|
|
|
.expect("new_outputs contains all created UTXOs");
|
|
|
|
|
|
|
|
// Revert the chain's created UTXOs
|
|
|
|
let removed_entry = self.created_utxos.remove(&outpoint);
|
|
|
|
assert!(
|
|
|
|
removed_entry.is_some(),
|
|
|
|
"unexpected revert of created output: duplicate revert or duplicate UTXO",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Revert the address index for this UTXO
|
|
|
|
if let Some(receiving_address) = created_utxo.utxo.output.address(self.network) {
|
|
|
|
let address_transfers = self
|
|
|
|
.partial_transparent_transfers
|
|
|
|
.get_mut(&receiving_address)
|
|
|
|
.expect("block has previously been applied to the chain");
|
|
|
|
|
2022-04-14 06:34:31 -07:00
|
|
|
address_transfers.revert_chain_with(&(&outpoint, created_utxo), position);
|
2022-04-12 10:21:46 -07:00
|
|
|
|
|
|
|
// Remove this transfer if it is now empty
|
|
|
|
if address_transfers.is_empty() {
|
|
|
|
self.partial_transparent_transfers
|
|
|
|
.remove(&receiving_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
// Transparent inputs
|
|
|
|
//
|
|
|
|
// TODO: replace arguments with a struct
|
|
|
|
impl
|
|
|
|
UpdateWith<(
|
|
|
|
// The inputs from a transaction in this block
|
|
|
|
&Vec<transparent::Input>,
|
|
|
|
// The hash of the transaction that the inputs are from
|
2022-05-19 19:22:01 -07:00
|
|
|
// (not the transaction the spent output was created by)
|
2022-04-12 10:21:46 -07:00
|
|
|
&transaction::Hash,
|
|
|
|
// The outputs for all inputs spent in this transaction (or block)
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
)> for Chain
|
|
|
|
{
|
2021-08-25 06:57:07 -07:00
|
|
|
fn update_chain_tip_with(
|
2021-06-29 20:52:31 -07:00
|
|
|
&mut self,
|
2022-04-12 10:21:46 -07:00
|
|
|
&(spending_inputs, spending_tx_hash, spent_outputs): &(
|
|
|
|
&Vec<transparent::Input>,
|
|
|
|
&transaction::Hash,
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
),
|
2021-06-29 20:52:31 -07:00
|
|
|
) -> Result<(), ValidateContextError> {
|
2022-04-12 10:21:46 -07:00
|
|
|
for spending_input in spending_inputs.iter() {
|
|
|
|
let spent_outpoint = if let Some(spent_outpoint) = spending_input.outpoint() {
|
|
|
|
spent_outpoint
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Index the spent outpoint in the chain
|
|
|
|
let first_spend = self.spent_utxos.insert(spent_outpoint);
|
|
|
|
assert!(
|
|
|
|
first_spend,
|
|
|
|
"unexpected duplicate spent output: should be checked earlier"
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: fix tests to supply correct spent outputs, then turn this into an expect()
|
|
|
|
let spent_output = if let Some(spent_output) = spent_outputs.get(&spent_outpoint) {
|
|
|
|
spent_output
|
|
|
|
} else if !cfg!(test) {
|
|
|
|
panic!("unexpected missing spent output: all spent outputs must be indexed");
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Index the spent output for the address
|
|
|
|
if let Some(spending_address) = spent_output.utxo.output.address(self.network) {
|
|
|
|
let address_transfers = self
|
|
|
|
.partial_transparent_transfers
|
|
|
|
.entry(spending_address)
|
|
|
|
.or_default();
|
|
|
|
|
|
|
|
address_transfers.update_chain_tip_with(&(
|
|
|
|
spending_input,
|
|
|
|
spending_tx_hash,
|
|
|
|
spent_output,
|
|
|
|
))?;
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 10:21:46 -07:00
|
|
|
|
2021-06-29 20:52:31 -07:00
|
|
|
Ok(())
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2022-04-12 10:21:46 -07:00
|
|
|
fn revert_chain_with(
|
|
|
|
&mut self,
|
|
|
|
&(spending_inputs, spending_tx_hash, spent_outputs): &(
|
|
|
|
&Vec<transparent::Input>,
|
|
|
|
&transaction::Hash,
|
|
|
|
&HashMap<transparent::OutPoint, transparent::OrderedUtxo>,
|
|
|
|
),
|
|
|
|
position: RevertPosition,
|
|
|
|
) {
|
|
|
|
for spending_input in spending_inputs.iter() {
|
|
|
|
let spent_outpoint = if let Some(spent_outpoint) = spending_input.outpoint() {
|
|
|
|
spent_outpoint
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Revert the spent outpoint in the chain
|
|
|
|
let spent_outpoint_was_removed = self.spent_utxos.remove(&spent_outpoint);
|
|
|
|
assert!(
|
|
|
|
spent_outpoint_was_removed,
|
|
|
|
"spent_utxos must be present if block was added to chain"
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: fix tests to supply correct spent outputs, then turn this into an expect()
|
|
|
|
let spent_output = if let Some(spent_output) = spent_outputs.get(&spent_outpoint) {
|
|
|
|
spent_output
|
|
|
|
} else if !cfg!(test) {
|
|
|
|
panic!(
|
|
|
|
"unexpected missing reverted spent output: all spent outputs must be indexed"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Revert the spent output for the address
|
|
|
|
if let Some(receiving_address) = spent_output.utxo.output.address(self.network) {
|
|
|
|
let address_transfers = self
|
|
|
|
.partial_transparent_transfers
|
|
|
|
.get_mut(&receiving_address)
|
|
|
|
.expect("block has previously been applied to the chain");
|
|
|
|
|
|
|
|
address_transfers
|
|
|
|
.revert_chain_with(&(spending_input, spending_tx_hash, spent_output), position);
|
|
|
|
|
|
|
|
// Remove this transfer if it is now empty
|
|
|
|
if address_transfers.is_empty() {
|
|
|
|
self.partial_transparent_transfers
|
|
|
|
.remove(&receiving_address);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateWith<Option<transaction::JoinSplitData<Groth16Proof>>> for Chain {
|
|
|
|
#[instrument(skip(self, joinsplit_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn update_chain_tip_with(
|
2020-11-09 10:05:18 -08:00
|
|
|
&mut self,
|
|
|
|
joinsplit_data: &Option<transaction::JoinSplitData<Groth16Proof>>,
|
2021-06-29 20:52:31 -07:00
|
|
|
) -> Result<(), ValidateContextError> {
|
2020-11-09 10:05:18 -08:00
|
|
|
if let Some(joinsplit_data) = joinsplit_data {
|
2022-07-22 09:19:11 -07:00
|
|
|
// We do note commitment tree updates in parallel rayon threads.
|
2021-11-30 02:05:58 -08:00
|
|
|
|
2021-07-14 05:06:43 -07:00
|
|
|
check::nullifier::add_to_non_finalized_chain_unique(
|
|
|
|
&mut self.sprout_nullifiers,
|
|
|
|
joinsplit_data.nullifiers(),
|
|
|
|
)?;
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
2021-06-29 20:52:31 -07:00
|
|
|
Ok(())
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2021-07-14 05:06:43 -07:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if any nullifier is missing from the chain when we try to remove it.
|
|
|
|
///
|
|
|
|
/// See [`check::nullifier::remove_from_non_finalized_chain`] for details.
|
2020-11-09 10:05:18 -08:00
|
|
|
#[instrument(skip(self, joinsplit_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn revert_chain_with(
|
2020-11-09 10:05:18 -08:00
|
|
|
&mut self,
|
|
|
|
joinsplit_data: &Option<transaction::JoinSplitData<Groth16Proof>>,
|
2021-08-25 06:57:07 -07:00
|
|
|
_position: RevertPosition,
|
2020-11-09 10:05:18 -08:00
|
|
|
) {
|
|
|
|
if let Some(joinsplit_data) = joinsplit_data {
|
2022-07-21 16:17:34 -07:00
|
|
|
// Note commitments are not removed from the Chain during a fork,
|
|
|
|
// because we don't support that operation yet. Instead, we
|
|
|
|
// recreate the tree from the finalized tip in Chain::fork().
|
|
|
|
//
|
|
|
|
// TODO: remove trees and anchors above the fork, to save CPU time (#4794)
|
|
|
|
|
2021-07-14 05:06:43 -07:00
|
|
|
check::nullifier::remove_from_non_finalized_chain(
|
|
|
|
&mut self.sprout_nullifiers,
|
|
|
|
joinsplit_data.nullifiers(),
|
|
|
|
);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:34:25 -07:00
|
|
|
impl<AnchorV> UpdateWith<Option<sapling::ShieldedData<AnchorV>>> for Chain
|
|
|
|
where
|
|
|
|
AnchorV: sapling::AnchorVariant + Clone,
|
|
|
|
{
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
#[instrument(skip(self, sapling_shielded_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn update_chain_tip_with(
|
2021-04-27 06:37:53 -07:00
|
|
|
&mut self,
|
|
|
|
sapling_shielded_data: &Option<sapling::ShieldedData<AnchorV>>,
|
2021-06-29 20:52:31 -07:00
|
|
|
) -> Result<(), ValidateContextError> {
|
2021-04-27 06:37:53 -07:00
|
|
|
if let Some(sapling_shielded_data) = sapling_shielded_data {
|
2022-07-22 09:19:11 -07:00
|
|
|
// We do note commitment tree updates in parallel rayon threads.
|
2021-07-29 06:37:18 -07:00
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
check::nullifier::add_to_non_finalized_chain_unique(
|
|
|
|
&mut self.sapling_nullifiers,
|
|
|
|
sapling_shielded_data.nullifiers(),
|
|
|
|
)?;
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
2021-06-29 20:52:31 -07:00
|
|
|
Ok(())
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if any nullifier is missing from the chain when we try to remove it.
|
|
|
|
///
|
|
|
|
/// See [`check::nullifier::remove_from_non_finalized_chain`] for details.
|
|
|
|
#[instrument(skip(self, sapling_shielded_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn revert_chain_with(
|
2021-04-27 06:37:53 -07:00
|
|
|
&mut self,
|
|
|
|
sapling_shielded_data: &Option<sapling::ShieldedData<AnchorV>>,
|
2021-08-25 06:57:07 -07:00
|
|
|
_position: RevertPosition,
|
2021-04-27 06:37:53 -07:00
|
|
|
) {
|
|
|
|
if let Some(sapling_shielded_data) = sapling_shielded_data {
|
2022-07-21 16:17:34 -07:00
|
|
|
// Note commitments are not removed from the Chain during a fork,
|
|
|
|
// because we don't support that operation yet. Instead, we
|
|
|
|
// recreate the tree from the finalized tip in Chain::fork().
|
|
|
|
//
|
|
|
|
// TODO: remove trees and anchors above the fork, to save CPU time (#4794)
|
2021-07-29 06:37:18 -07:00
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
check::nullifier::remove_from_non_finalized_chain(
|
|
|
|
&mut self.sapling_nullifiers,
|
|
|
|
sapling_shielded_data.nullifiers(),
|
|
|
|
);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:53:13 -07:00
|
|
|
impl UpdateWith<Option<orchard::ShieldedData>> for Chain {
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
#[instrument(skip(self, orchard_shielded_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn update_chain_tip_with(
|
2021-06-29 20:52:31 -07:00
|
|
|
&mut self,
|
|
|
|
orchard_shielded_data: &Option<orchard::ShieldedData>,
|
|
|
|
) -> Result<(), ValidateContextError> {
|
2021-06-01 00:53:13 -07:00
|
|
|
if let Some(orchard_shielded_data) = orchard_shielded_data {
|
2022-07-22 09:19:11 -07:00
|
|
|
// We do note commitment tree updates in parallel rayon threads.
|
2021-07-29 06:37:18 -07:00
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
check::nullifier::add_to_non_finalized_chain_unique(
|
|
|
|
&mut self.orchard_nullifiers,
|
|
|
|
orchard_shielded_data.nullifiers(),
|
|
|
|
)?;
|
2021-06-01 00:53:13 -07:00
|
|
|
}
|
2021-06-29 20:52:31 -07:00
|
|
|
Ok(())
|
2021-06-01 00:53:13 -07:00
|
|
|
}
|
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if any nullifier is missing from the chain when we try to remove it.
|
|
|
|
///
|
|
|
|
/// See [`check::nullifier::remove_from_non_finalized_chain`] for details.
|
|
|
|
#[instrument(skip(self, orchard_shielded_data))]
|
2021-08-25 06:57:07 -07:00
|
|
|
fn revert_chain_with(
|
|
|
|
&mut self,
|
|
|
|
orchard_shielded_data: &Option<orchard::ShieldedData>,
|
|
|
|
_position: RevertPosition,
|
|
|
|
) {
|
2021-06-01 00:53:13 -07:00
|
|
|
if let Some(orchard_shielded_data) = orchard_shielded_data {
|
2022-07-21 16:17:34 -07:00
|
|
|
// Note commitments are not removed from the Chain during a fork,
|
|
|
|
// because we don't support that operation yet. Instead, we
|
|
|
|
// recreate the tree from the finalized tip in Chain::fork().
|
|
|
|
//
|
|
|
|
// TODO: remove trees and anchors above the fork, to save CPU time (#4794)
|
2021-07-29 06:37:18 -07:00
|
|
|
|
Reject duplicate Sapling and Orchard nullifiers (#2497)
* Add sapling and orchard duplicate nullifier errors
* Reject duplicate finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is already in the finalized state.
* Reject duplicate non-finalized sapling and orchard nullifiers
Reject duplicate sapling and orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block, or
* an earlier block in the non-finalized chain.
* Refactor sprout nullifier tests to remove common code
* Add sapling nullifier tests
Test that the state rejects duplicate sapling nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Add orchard nullifier tests
Test that the state rejects duplicate orchard nullifiers in a new block,
when the block is added to a non-finalized chain,
and the duplicate nullifier is in:
* the same shielded data,
* the same transaction,
* the same block,
* an earlier block in the non-finalized chain, or
* the finalized state.
* Check for specific nullifiers in the state in tests
* Replace slices with vectors in arguments
* Remove redundant code and variables
* Simplify sapling TransferData tests
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
* Remove an extra :
* Remove redundant vec!
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-07-19 17:39:05 -07:00
|
|
|
check::nullifier::remove_from_non_finalized_chain(
|
|
|
|
&mut self.orchard_nullifiers,
|
|
|
|
orchard_shielded_data.nullifiers(),
|
|
|
|
);
|
2021-06-01 00:53:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:57:07 -07:00
|
|
|
impl UpdateWith<ValueBalance<NegativeAllowed>> for Chain {
|
|
|
|
fn update_chain_tip_with(
|
|
|
|
&mut self,
|
|
|
|
block_value_pool_change: &ValueBalance<NegativeAllowed>,
|
|
|
|
) -> Result<(), ValidateContextError> {
|
|
|
|
match self
|
|
|
|
.chain_value_pools
|
|
|
|
.add_chain_value_pool_change(*block_value_pool_change)
|
|
|
|
{
|
|
|
|
Ok(chain_value_pools) => self.chain_value_pools = chain_value_pools,
|
|
|
|
Err(value_balance_error) => Err(ValidateContextError::AddValuePool {
|
|
|
|
value_balance_error,
|
|
|
|
chain_value_pools: self.chain_value_pools,
|
|
|
|
block_value_pool_change: *block_value_pool_change,
|
|
|
|
// assume that the current block is added to `blocks` after `update_chain_tip_with`
|
|
|
|
height: self.max_block_height().and_then(|height| height + 1),
|
|
|
|
})?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Revert the chain state using a block chain value pool change.
|
|
|
|
///
|
|
|
|
/// When forking from the tip, subtract the block's chain value pool change.
|
|
|
|
///
|
|
|
|
/// When finalizing the root, leave the chain value pool balances unchanged.
|
2022-06-13 18:22:16 -07:00
|
|
|
/// [`Self::chain_value_pools`] tracks the chain value pools for all
|
|
|
|
/// finalized blocks, and the non-finalized blocks in this chain. So
|
|
|
|
/// finalizing the root doesn't change the set of blocks it tracks.
|
2021-08-25 06:57:07 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the chain pool value balance is invalid
|
|
|
|
/// after we subtract the block value pool change.
|
|
|
|
fn revert_chain_with(
|
|
|
|
&mut self,
|
|
|
|
block_value_pool_change: &ValueBalance<NegativeAllowed>,
|
|
|
|
position: RevertPosition,
|
|
|
|
) {
|
|
|
|
use std::ops::Neg;
|
|
|
|
|
|
|
|
if position == RevertPosition::Tip {
|
|
|
|
self.chain_value_pools = self
|
|
|
|
.chain_value_pools
|
|
|
|
.add_chain_value_pool_change(block_value_pool_change.neg())
|
|
|
|
.expect("reverting the tip will leave the pools in a previously valid state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
impl Ord for Chain {
|
2022-06-13 18:22:16 -07:00
|
|
|
/// Chain order for the [`NonFinalizedState`][1]'s `chain_set`.
|
|
|
|
///
|
2021-07-14 15:23:54 -07:00
|
|
|
/// Chains with higher cumulative Proof of Work are [`Ordering::Greater`],
|
|
|
|
/// breaking ties using the tip block hash.
|
|
|
|
///
|
2022-06-13 18:22:16 -07:00
|
|
|
/// Despite the consensus rules, Zebra uses the tip block hash as a
|
|
|
|
/// tie-breaker. Zebra blocks are downloaded in parallel, so download
|
|
|
|
/// timestamps may not be unique. (And Zebra currently doesn't track
|
|
|
|
/// download times, because [`Block`](block::Block)s are immutable.)
|
2021-07-14 15:23:54 -07:00
|
|
|
///
|
|
|
|
/// This departure from the consensus rules may delay network convergence,
|
|
|
|
/// for as long as the greater hash belongs to the later mined block.
|
|
|
|
/// But Zebra nodes should converge as soon as the tied work is broken.
|
|
|
|
///
|
|
|
|
/// "At a given point in time, each full validator is aware of a set of candidate blocks.
|
|
|
|
/// These form a tree rooted at the genesis block, where each node in the tree
|
|
|
|
/// refers to its parent via the hashPrevBlock block header field.
|
|
|
|
///
|
|
|
|
/// A path from the root toward the leaves of the tree consisting of a sequence
|
|
|
|
/// of one or more valid blocks consistent with consensus rules,
|
|
|
|
/// is called a valid block chain.
|
|
|
|
///
|
|
|
|
/// In order to choose the best valid block chain in its view of the overall block tree,
|
|
|
|
/// a node sums the work ... of all blocks in each valid block chain,
|
|
|
|
/// and considers the valid block chain with greatest total work to be best.
|
|
|
|
///
|
|
|
|
/// To break ties between leaf blocks, a node will prefer the block that it received first.
|
|
|
|
///
|
|
|
|
/// The consensus protocol is designed to ensure that for any given block height,
|
|
|
|
/// the vast majority of nodes should eventually agree on their best valid block chain
|
|
|
|
/// up to that height."
|
|
|
|
///
|
2022-05-30 13:12:11 -07:00
|
|
|
/// <https://zips.z.cash/protocol/protocol.pdf#blockchain>
|
2021-07-14 15:23:54 -07:00
|
|
|
///
|
2021-10-01 08:26:06 -07:00
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// `Chain::cmp` is used in a `BTreeSet`, so the fields accessed by `cmp` must not have
|
|
|
|
/// interior mutability.
|
|
|
|
///
|
2021-07-14 15:23:54 -07:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If two chains compare equal.
|
|
|
|
///
|
2022-06-13 18:22:16 -07:00
|
|
|
/// This panic enforces the [`NonFinalizedState::chain_set`][2] unique chain invariant.
|
2021-07-14 15:23:54 -07:00
|
|
|
///
|
|
|
|
/// If the chain set contains duplicate chains, the non-finalized state might
|
|
|
|
/// handle new blocks or block finalization incorrectly.
|
2022-06-13 18:22:16 -07:00
|
|
|
///
|
|
|
|
/// [1]: super::NonFinalizedState
|
|
|
|
/// [2]: super::NonFinalizedState::chain_set
|
2020-11-09 10:05:18 -08:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
if self.partial_cumulative_work != other.partial_cumulative_work {
|
|
|
|
self.partial_cumulative_work
|
|
|
|
.cmp(&other.partial_cumulative_work)
|
|
|
|
} else {
|
|
|
|
let self_hash = self
|
|
|
|
.blocks
|
|
|
|
.values()
|
|
|
|
.last()
|
|
|
|
.expect("always at least 1 element")
|
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
|
|
|
.hash;
|
2020-11-09 10:05:18 -08:00
|
|
|
|
|
|
|
let other_hash = other
|
|
|
|
.blocks
|
|
|
|
.values()
|
|
|
|
.last()
|
|
|
|
.expect("always at least 1 element")
|
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
|
|
|
.hash;
|
2020-11-09 10:05:18 -08:00
|
|
|
|
|
|
|
// This comparison is a tie-breaker within the local node, so it does not need to
|
|
|
|
// be consistent with the ordering on `ExpandedDifficulty` and `block::Hash`.
|
|
|
|
match self_hash.0.cmp(&other_hash.0) {
|
|
|
|
Ordering::Equal => unreachable!("Chain tip block hashes are always unique"),
|
|
|
|
ordering => ordering,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 15:23:54 -07:00
|
|
|
|
|
|
|
impl PartialOrd for Chain {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Chain {
|
2022-06-13 18:22:16 -07:00
|
|
|
/// Chain equality for [`NonFinalizedState::chain_set`][1], using proof of
|
|
|
|
/// work, then the tip block hash as a tie-breaker.
|
2021-07-14 15:23:54 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If two chains compare equal.
|
|
|
|
///
|
|
|
|
/// See [`Chain::cmp`] for details.
|
2022-06-13 18:22:16 -07:00
|
|
|
///
|
|
|
|
/// [1]: super::NonFinalizedState::chain_set
|
2021-07-14 15:23:54 -07:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.partial_cmp(other) == Some(Ordering::Equal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Chain {}
|