2020-11-09 10:05:18 -08:00
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
|
|
|
collections::{BTreeMap, HashMap, HashSet},
|
|
|
|
ops::Deref,
|
|
|
|
};
|
|
|
|
|
|
|
|
use tracing::{debug_span, instrument, trace};
|
|
|
|
use zebra_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
|
|
|
block, primitives::Groth16Proof, sapling, sprout, transaction, transparent,
|
2020-11-09 10:05:18 -08:00
|
|
|
work::difficulty::PartialCumulativeWork,
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:02:57 -08:00
|
|
|
use crate::{PreparedBlock, Utxo};
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
|
2020-11-09 10:05:18 -08:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct 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
|
|
|
pub blocks: BTreeMap<block::Height, PreparedBlock>,
|
2020-11-09 10:05:18 -08:00
|
|
|
pub height_by_hash: HashMap<block::Hash, block::Height>,
|
|
|
|
pub tx_by_hash: HashMap<transaction::Hash, (block::Height, usize)>,
|
|
|
|
|
2020-11-23 12:02:57 -08:00
|
|
|
pub created_utxos: HashMap<transparent::OutPoint, Utxo>,
|
2020-11-09 10:05:18 -08:00
|
|
|
spent_utxos: HashSet<transparent::OutPoint>,
|
|
|
|
sprout_anchors: HashSet<sprout::tree::Root>,
|
|
|
|
sapling_anchors: HashSet<sapling::tree::Root>,
|
|
|
|
sprout_nullifiers: HashSet<sprout::Nullifier>,
|
|
|
|
sapling_nullifiers: HashSet<sapling::Nullifier>,
|
|
|
|
partial_cumulative_work: PartialCumulativeWork,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Chain {
|
|
|
|
/// Push a contextually valid non-finalized block into a chain as the new tip.
|
2020-11-21 10:41:53 -08:00
|
|
|
#[instrument(level = "debug", skip(self, block), fields(block = %block.block))]
|
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
|
|
|
pub fn push(&mut self, block: PreparedBlock) {
|
2020-11-09 10:05:18 -08:00
|
|
|
// update cumulative data members
|
|
|
|
self.update_chain_state_with(&block);
|
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);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the lowest height block of the non-finalized portion of a chain.
|
2020-11-21 10:41:53 -08:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
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
|
|
|
pub fn pop_root(&mut self) -> PreparedBlock {
|
2020-11-09 10:05:18 -08:00
|
|
|
let block_height = self.lowest_height();
|
|
|
|
|
|
|
|
// remove the lowest height block from self.blocks
|
|
|
|
let block = self
|
|
|
|
.blocks
|
|
|
|
.remove(&block_height)
|
|
|
|
.expect("only called while blocks is populated");
|
|
|
|
|
|
|
|
// update cumulative data members
|
|
|
|
self.revert_chain_state_with(&block);
|
|
|
|
|
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
|
|
|
// return the prepared block
|
2020-11-09 10:05:18 -08:00
|
|
|
block
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lowest_height(&self) -> block::Height {
|
|
|
|
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.
|
|
|
|
pub fn fork(&self, fork_tip: block::Hash) -> Option<Self> {
|
|
|
|
if !self.height_by_hash.contains_key(&fork_tip) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut forked = self.clone();
|
|
|
|
|
|
|
|
while forked.non_finalized_tip_hash() != fork_tip {
|
|
|
|
forked.pop_tip();
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(forked)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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"
|
|
|
|
);
|
|
|
|
|
|
|
|
self.revert_chain_state_with(&block);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn non_finalized_tip_height(&self) -> block::Height {
|
|
|
|
*self
|
|
|
|
.blocks
|
|
|
|
.keys()
|
|
|
|
.next_back()
|
|
|
|
.expect("only called while blocks is populated")
|
|
|
|
}
|
2020-11-06 14:31:25 -08:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.blocks.is_empty()
|
|
|
|
}
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper trait to organize inverse operations done on the `Chain` type. Used to
|
|
|
|
/// overload the `update_chain_state_with` and `revert_chain_state_with` methods
|
|
|
|
/// based on the type of the argument.
|
|
|
|
///
|
|
|
|
/// This trait was motivated by the length of the `push` and `pop_root` 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 eachother.
|
|
|
|
trait UpdateWith<T> {
|
|
|
|
/// Update `Chain` cumulative data members to add data that are derived from
|
|
|
|
/// `T`
|
|
|
|
fn update_chain_state_with(&mut self, _: &T);
|
|
|
|
|
|
|
|
/// Update `Chain` cumulative data members to remove data that are derived
|
|
|
|
/// from `T`
|
|
|
|
fn revert_chain_state_with(&mut self, _: &T);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
impl UpdateWith<PreparedBlock> for Chain {
|
|
|
|
fn update_chain_state_with(&mut self, prepared: &PreparedBlock) {
|
2020-11-24 19:55:15 -08:00
|
|
|
let (block, hash, height, transaction_hashes) = (
|
|
|
|
prepared.block.as_ref(),
|
|
|
|
prepared.hash,
|
|
|
|
prepared.height,
|
|
|
|
&prepared.transaction_hashes,
|
|
|
|
);
|
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-03-03 13:56:41 -08:00
|
|
|
use transaction::Transaction::*;
|
2021-03-31 14:34:25 -07:00
|
|
|
let (inputs, joinsplit_data, sapling_shielded_data) = match transaction.deref() {
|
2021-03-03 13:56:41 -08:00
|
|
|
V4 {
|
2020-11-09 10:05:18 -08:00
|
|
|
inputs,
|
|
|
|
joinsplit_data,
|
2021-03-31 14:34:25 -07:00
|
|
|
sapling_shielded_data,
|
2020-11-09 10:05:18 -08:00
|
|
|
..
|
2021-03-31 14:34:25 -07:00
|
|
|
} => (inputs, joinsplit_data, sapling_shielded_data),
|
2021-03-03 13:56:41 -08:00
|
|
|
V5 { .. } => unimplemented!("v5 transaction format as specified in ZIP-225"),
|
|
|
|
V1 { .. } | V2 { .. } | V3 { .. } => unreachable!(
|
2020-11-09 10:05:18 -08:00
|
|
|
"older transaction versions only exist in finalized blocks pre sapling",
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// add key `transaction.hash` and value `(height, tx_index)` to `tx_by_hash`
|
|
|
|
let prior_pair = self
|
|
|
|
.tx_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
|
|
|
.insert(transaction_hash, (height, transaction_index));
|
2020-11-09 10:05:18 -08:00
|
|
|
assert!(
|
|
|
|
prior_pair.is_none(),
|
|
|
|
"transactions must be unique within a single chain"
|
|
|
|
);
|
|
|
|
|
|
|
|
// add the utxos this produced
|
2020-11-23 12:02:57 -08:00
|
|
|
self.update_chain_state_with(&prepared.new_outputs);
|
2020-11-09 10:05:18 -08:00
|
|
|
// add the utxos this consumed
|
|
|
|
self.update_chain_state_with(inputs);
|
|
|
|
// add sprout anchor and nullifiers
|
|
|
|
self.update_chain_state_with(joinsplit_data);
|
|
|
|
// add sapling anchor and nullifier
|
2021-03-31 14:34:25 -07:00
|
|
|
self.update_chain_state_with(sapling_shielded_data);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
state: introduce PreparedBlock, FinalizedBlock
This change introduces two new types:
- `PreparedBlock`, representing a block which has undergone semantic
validation and has been prepared for contextual validation;
- `FinalizedBlock`, representing a block which is ready to be finalized
immediately;
and changes the `Request::CommitBlock`,`Request::CommitFinalizedBlock`
variants to use these types instead of their previous fields.
This change solves the problem of passing data between semantic
validation and contextual validation, and cleans up the state code by
allowing it to pass around a bundle of data. Previously, the state code
just passed around an `Arc<Block>`, which forced it to needlessly
recompute block hashes and other data, and was incompatible with the
already-known but not-yet-implemented data transfer requirements, namely
passing in the Sprout and Sapling anchors computed during contextual
validation.
This commit propagates the `PreparedBlock` and `FinalizedBlock` types
through the state code but only uses their data opportunistically, e.g.,
changing .hash() computations to use the precomputed hash. In the
future, these structures can be extended to pass data through the
verification pipeline for reuse as appropriate. For instance, these
changes allow the sprout and sapling anchors to be propagated through
the state.
2020-11-21 01:16:14 -08:00
|
|
|
#[instrument(skip(self, prepared), fields(block = %prepared.block))]
|
|
|
|
fn revert_chain_state_with(&mut self, prepared: &PreparedBlock) {
|
2020-11-24 19:55:15 -08:00
|
|
|
let (block, hash, transaction_hashes) = (
|
|
|
|
prepared.block.as_ref(),
|
|
|
|
prepared.hash,
|
|
|
|
&prepared.transaction_hashes,
|
|
|
|
);
|
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(),
|
2020-11-09 10:05:18 -08:00
|
|
|
"hash must be present if block was"
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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-03-03 13:56:41 -08:00
|
|
|
use transaction::Transaction::*;
|
2021-03-31 14:34:25 -07:00
|
|
|
let (inputs, joinsplit_data, sapling_shielded_data) = match transaction.deref() {
|
2021-03-03 13:56:41 -08:00
|
|
|
V4 {
|
2020-11-09 10:05:18 -08:00
|
|
|
inputs,
|
|
|
|
joinsplit_data,
|
2021-03-31 14:34:25 -07:00
|
|
|
sapling_shielded_data,
|
2020-11-09 10:05:18 -08:00
|
|
|
..
|
2021-03-31 14:34:25 -07:00
|
|
|
} => (inputs, joinsplit_data, sapling_shielded_data),
|
2021-03-03 13:56:41 -08:00
|
|
|
V5 { .. } => unimplemented!("v5 transaction format as specified in ZIP-225"),
|
|
|
|
V1 { .. } | V2 { .. } | V3 { .. } => unreachable!(
|
2020-11-09 10:05:18 -08:00
|
|
|
"older transaction versions only exist in finalized blocks pre sapling",
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// remove `transaction.hash` from `tx_by_hash`
|
|
|
|
assert!(
|
2020-11-24 19:55:15 -08:00
|
|
|
self.tx_by_hash.remove(transaction_hash).is_some(),
|
2020-11-09 10:05:18 -08:00
|
|
|
"transactions must be present if block was"
|
|
|
|
);
|
|
|
|
|
|
|
|
// remove the utxos this produced
|
2020-11-23 12:02:57 -08:00
|
|
|
self.revert_chain_state_with(&prepared.new_outputs);
|
2020-11-09 10:05:18 -08:00
|
|
|
// remove the utxos this consumed
|
|
|
|
self.revert_chain_state_with(inputs);
|
|
|
|
// remove sprout anchor and nullifiers
|
|
|
|
self.revert_chain_state_with(joinsplit_data);
|
|
|
|
// remove sapling anchor and nullfier
|
2021-03-31 14:34:25 -07:00
|
|
|
self.revert_chain_state_with(sapling_shielded_data);
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:02:57 -08:00
|
|
|
impl UpdateWith<HashMap<transparent::OutPoint, Utxo>> for Chain {
|
|
|
|
fn update_chain_state_with(&mut self, utxos: &HashMap<transparent::OutPoint, Utxo>) {
|
|
|
|
self.created_utxos
|
|
|
|
.extend(utxos.iter().map(|(k, v)| (*k, v.clone())));
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
|
2020-11-23 12:02:57 -08:00
|
|
|
fn revert_chain_state_with(&mut self, utxos: &HashMap<transparent::OutPoint, Utxo>) {
|
|
|
|
self.created_utxos
|
|
|
|
.retain(|outpoint, _| !utxos.contains_key(outpoint));
|
2020-11-09 10:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateWith<Vec<transparent::Input>> for Chain {
|
|
|
|
fn update_chain_state_with(&mut self, inputs: &Vec<transparent::Input>) {
|
|
|
|
for consumed_utxo in inputs {
|
|
|
|
match consumed_utxo {
|
|
|
|
transparent::Input::PrevOut { outpoint, .. } => {
|
|
|
|
self.spent_utxos.insert(*outpoint);
|
|
|
|
}
|
|
|
|
transparent::Input::Coinbase { .. } => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn revert_chain_state_with(&mut self, inputs: &Vec<transparent::Input>) {
|
|
|
|
for consumed_utxo in inputs {
|
|
|
|
match consumed_utxo {
|
|
|
|
transparent::Input::PrevOut { outpoint, .. } => {
|
|
|
|
assert!(
|
|
|
|
self.spent_utxos.remove(outpoint),
|
|
|
|
"spent_utxos must be present if block was"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
transparent::Input::Coinbase { .. } => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateWith<Option<transaction::JoinSplitData<Groth16Proof>>> for Chain {
|
|
|
|
#[instrument(skip(self, joinsplit_data))]
|
|
|
|
fn update_chain_state_with(
|
|
|
|
&mut self,
|
|
|
|
joinsplit_data: &Option<transaction::JoinSplitData<Groth16Proof>>,
|
|
|
|
) {
|
|
|
|
if let Some(joinsplit_data) = joinsplit_data {
|
|
|
|
for sprout::JoinSplit { nullifiers, .. } in joinsplit_data.joinsplits() {
|
|
|
|
let span = debug_span!("revert_chain_state_with", ?nullifiers);
|
|
|
|
let _entered = span.enter();
|
|
|
|
trace!("Adding sprout nullifiers.");
|
|
|
|
self.sprout_nullifiers.insert(nullifiers[0]);
|
|
|
|
self.sprout_nullifiers.insert(nullifiers[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, joinsplit_data))]
|
|
|
|
fn revert_chain_state_with(
|
|
|
|
&mut self,
|
|
|
|
joinsplit_data: &Option<transaction::JoinSplitData<Groth16Proof>>,
|
|
|
|
) {
|
|
|
|
if let Some(joinsplit_data) = joinsplit_data {
|
|
|
|
for sprout::JoinSplit { nullifiers, .. } in joinsplit_data.joinsplits() {
|
|
|
|
let span = debug_span!("revert_chain_state_with", ?nullifiers);
|
|
|
|
let _entered = span.enter();
|
|
|
|
trace!("Removing sprout nullifiers.");
|
|
|
|
assert!(
|
|
|
|
self.sprout_nullifiers.remove(&nullifiers[0]),
|
|
|
|
"nullifiers must be present if block was"
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
self.sprout_nullifiers.remove(&nullifiers[1]),
|
|
|
|
"nullifiers must be present if block was"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:34:25 -07:00
|
|
|
impl<AnchorV> UpdateWith<Option<sapling::ShieldedData<AnchorV>>> for Chain
|
|
|
|
where
|
|
|
|
AnchorV: sapling::AnchorVariant + Clone,
|
|
|
|
{
|
|
|
|
fn update_chain_state_with(&mut self, shielded_data: &Option<sapling::ShieldedData<AnchorV>>) {
|
2020-11-09 10:05:18 -08:00
|
|
|
if let Some(shielded_data) = shielded_data {
|
2021-03-31 14:34:25 -07:00
|
|
|
for nullifier in shielded_data.nullifiers() {
|
2020-11-09 10:05:18 -08:00
|
|
|
self.sapling_nullifiers.insert(*nullifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:34:25 -07:00
|
|
|
fn revert_chain_state_with(&mut self, shielded_data: &Option<sapling::ShieldedData<AnchorV>>) {
|
2020-11-09 10:05:18 -08:00
|
|
|
if let Some(shielded_data) = shielded_data {
|
2021-03-31 14:34:25 -07:00
|
|
|
for nullifier in shielded_data.nullifiers() {
|
2020-11-09 10:05:18 -08:00
|
|
|
assert!(
|
|
|
|
self.sapling_nullifiers.remove(nullifier),
|
|
|
|
"nullifier must be present if block was"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Chain {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.partial_cmp(other) == Some(Ordering::Equal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Chain {}
|
|
|
|
|
|
|
|
impl PartialOrd for Chain {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Chain {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|