Skip to content

Commit

Permalink
refactor(state): Make implementation of block consensus rules clearer (
Browse files Browse the repository at this point in the history
…#5915)

* Replace duplicate code with read::hash_by_height()

* Rename an unclear function to difficulty_threshold_and_time_are_valid()
  • Loading branch information
teor2345 authored Jan 6, 2023
1 parent 70f9e55 commit cda0a0b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
6 changes: 5 additions & 1 deletion zebra-state/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,11 @@ impl Service<ReadRequest> for ReadStateService {
span.in_scope(move || {
let hash = state.non_finalized_state_receiver.with_watch_data(
|non_finalized_state| {
read::hash(non_finalized_state.best_chain(), &state.db, height)
read::hash_by_height(
non_finalized_state.best_chain(),
&state.db,
height,
)
},
);

Expand Down
4 changes: 2 additions & 2 deletions zebra-state/src/service/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
});
let difficulty_adjustment =
AdjustedDifficulty::new_from_block(&prepared.block, network, relevant_data);
check::difficulty_threshold_is_valid(
check::difficulty_threshold_and_time_are_valid(
prepared.block.header.difficulty_threshold,
difficulty_adjustment,
)?;
Expand Down Expand Up @@ -233,7 +233,7 @@ fn height_one_more_than_parent_height(
///
/// These checks are performed together, because the time field is used to
/// calculate the expected difficulty adjustment.
fn difficulty_threshold_is_valid(
fn difficulty_threshold_and_time_are_valid(
difficulty_threshold: CompactDifficulty,
difficulty_adjustment: AdjustedDifficulty,
) -> Result<(), ValidateContextError> {
Expand Down
5 changes: 3 additions & 2 deletions zebra-state/src/service/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use crate::service;

pub mod address;
pub mod block;

#[cfg(feature = "getblocktemplate-rpcs")]
pub mod difficulty;

pub mod find;
pub mod tree;

Expand All @@ -29,13 +31,12 @@ pub use address::{
tx_id::transparent_tx_ids,
utxo::{address_utxos, AddressUtxos, ADDRESS_HEIGHTS_FULL_RANGE},
};

pub use block::{
any_utxo, block, block_header, transaction, transaction_hashes_for_block, unspent_utxo, utxo,
};

#[cfg(feature = "getblocktemplate-rpcs")]
pub use {block::hash, difficulty::get_block_template_chain_info};
pub use difficulty::get_block_template_chain_info;

pub use find::{
best_tip, block_locator, chain_contains_hash, depth, find_chain_hashes, find_chain_headers,
Expand Down
22 changes: 0 additions & 22 deletions zebra-state/src/service/read/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,3 @@ pub fn any_utxo(
.any_utxo(&outpoint)
.or_else(|| db.utxo(&outpoint).map(|utxo| utxo.utxo))
}

#[cfg(feature = "getblocktemplate-rpcs")]
/// Returns the [`Hash`] given [`block::Height`](zebra_chain::block::Height), if it exists in
/// the non-finalized `chain` or finalized `db`.
pub fn hash<C>(chain: Option<C>, db: &ZebraDb, height: Height) -> Option<zebra_chain::block::Hash>
where
C: AsRef<Chain>,
{
// # Correctness
//
// The StateService commits blocks to the finalized state before updating
// the latest chain, and it can commit additional blocks after we've cloned
// this `chain` variable.
//
// Since blocks are the same in the finalized and non-finalized state, we
// check the most efficient alternative first. (`chain` is always in memory,
// but `db` stores blocks on disk, with a memory cache.)
chain
.as_ref()
.and_then(|chain| chain.as_ref().hash_by_height(height))
.or_else(|| db.hash(height))
}

0 comments on commit cda0a0b

Please sign in to comment.