Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value pools to FinalizedState #2599

Merged
merged 24 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zebra-chain/src/value_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ where
/// value pool.
///
/// See `update_with_block` for details.
pub(crate) fn update_with_chain_value_pool_change(
pub fn update_with_chain_value_pool_change(
self,
chain_value_pool_change: ValueBalance<NegativeAllowed>,
) -> Result<ValueBalance<C>, ValueBalanceError> {
Expand Down
3 changes: 2 additions & 1 deletion zebra-consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ where
metrics::gauge!("zcash.chain.verified.block.height", height.0 as _);
metrics::counter!("zcash.chain.verified.block.total", 1);

// Finally, submit the block for contextual verification.
let new_outputs = Arc::try_unwrap(known_utxos)
.expect("all verification tasks using known_utxos are complete");

// Finally, submit the block for contextual verification.
let prepared_block = zs::PreparedBlock {
block,
hash,
Expand Down
2 changes: 1 addition & 1 deletion zebra-state/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use zebra_chain::transparent::MIN_TRANSPARENT_COINBASE_MATURITY;
pub const MAX_BLOCK_REORG_HEIGHT: u32 = MIN_TRANSPARENT_COINBASE_MATURITY - 1;

/// The database format version, incremented each time the database format changes.
pub const DATABASE_FORMAT_VERSION: u32 = 9;
pub const DATABASE_FORMAT_VERSION: u32 = 10;

/// The maximum number of blocks to check for NU5 transactions,
/// before we assume we are on a pre-NU5 legacy chain.
Expand Down
27 changes: 25 additions & 2 deletions zebra-state/src/service/finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ mod disk_format;
#[cfg(test)]
mod tests;

use std::{collections::HashMap, convert::TryInto, path::Path, sync::Arc};
use std::{
borrow::Borrow,
collections::HashMap,
convert::TryInto,
path::Path,
sync::Arc
};

use zebra_chain::{
amount::NonNegative,
block::{self, Block},
history_tree::{HistoryTree, NonEmptyHistoryTree},
orchard,
parameters::{Network, GENESIS_PREVIOUS_BLOCK_HASH},
sapling, sprout,
transaction::{self, Transaction},
transparent,
value_balance::ValueBalance,
};

use crate::{BoxError, Config, FinalizedBlock, HashOrHeight};
Expand Down Expand Up @@ -64,6 +72,7 @@ impl FinalizedState {
db_options.clone(),
),
rocksdb::ColumnFamilyDescriptor::new("history_tree", db_options.clone()),
rocksdb::ColumnFamilyDescriptor::new("tip_chain_value_pool", db_options.clone()),
];
let db_result = rocksdb::DB::open_cf_descriptors(&db_options, &path, column_families);

Expand Down Expand Up @@ -240,6 +249,8 @@ impl FinalizedState {
self.db.cf_handle("orchard_note_commitment_tree").unwrap();
let history_tree_cf = self.db.cf_handle("history_tree").unwrap();

let tip_chain_value_pool = self.db.cf_handle("tip_chain_value_pool").unwrap();

// Assert that callers (including unit tests) get the chain order correct
if self.is_empty(hash_by_height) {
assert_eq!(
Expand Down Expand Up @@ -310,7 +321,7 @@ impl FinalizedState {
}

// Index all new transparent outputs
for (outpoint, utxo) in new_outputs.into_iter() {
for (outpoint, utxo) in new_outputs.borrow().into_iter() {
batch.zs_insert(utxo_by_outpoint, outpoint, utxo);
}

Expand Down Expand Up @@ -390,6 +401,10 @@ impl FinalizedState {
batch.zs_insert(history_tree_cf, height, history_tree);
}

let current_pool = self.current_value_pool();
let new_pool = current_pool.update_with_block(block.borrow(), &new_outputs)?;
batch.zs_insert(tip_chain_value_pool, (), new_pool);

Ok(batch)
};

Expand Down Expand Up @@ -572,6 +587,14 @@ impl FinalizedState {
pub fn path(&self) -> &Path {
self.db.path()
}

/// Returns the stored `ValueBalance` for the best chain at the finalized tip height.
pub fn current_value_pool(&self) -> ValueBalance<NonNegative> {
let value_pool_cf = self.db.cf_handle("tip_chain_value_pool").unwrap();
self.db
.zs_get(value_pool_cf, &())
.unwrap_or_else(ValueBalance::zero)
}
}

// Drop isn't guaranteed to run, such as when we panic, or if someone stored
Expand Down