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

rename(state): do additional renaming for clarification purposes #6967

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions zebra-consensus/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ pub enum VerifyCheckpointError {
#[error("checkpoint verifier was dropped")]
Dropped,
#[error(transparent)]
CommitFinalized(BoxError),
CommitCheckpointVerified(BoxError),
#[error(transparent)]
Tip(BoxError),
#[error(transparent)]
Expand Down Expand Up @@ -1084,19 +1084,19 @@ where
// we don't reject the entire checkpoint.
// Instead, we reset the verifier to the successfully committed state tip.
let state_service = self.state_service.clone();
let commit_finalized_block = tokio::spawn(async move {
let commit_checkpoint_verified = tokio::spawn(async move {
let hash = req_block
.rx
.await
.map_err(Into::into)
.map_err(VerifyCheckpointError::CommitFinalized)
.map_err(VerifyCheckpointError::CommitCheckpointVerified)
.expect("CheckpointVerifier does not leave dangling receivers")?;

// We use a `ServiceExt::oneshot`, so that every state service
// `poll_ready` has a corresponding `call`. See #1593.
match state_service
.oneshot(zs::Request::CommitCheckpointVerifiedBlock(req_block.block))
.map_err(VerifyCheckpointError::CommitFinalized)
.map_err(VerifyCheckpointError::CommitCheckpointVerified)
.await?
{
zs::Response::Committed(committed_hash) => {
Expand All @@ -1110,18 +1110,18 @@ where
let state_service = self.state_service.clone();
let reset_sender = self.reset_sender.clone();
async move {
let result = commit_finalized_block.await;
let result = commit_checkpoint_verified.await;
// Avoid a panic on shutdown
//
// When `zebrad` is terminated using Ctrl-C, the `commit_finalized_block` task
// When `zebrad` is terminated using Ctrl-C, the `commit_checkpoint_verified` task
// can return a `JoinError::Cancelled`. We expect task cancellation on shutdown,
// so we don't need to panic here. The persistent state is correct even when the
// task is cancelled, because block data is committed inside transactions, in
// height order.
let result = if zebra_chain::shutdown::is_shutting_down() {
Err(VerifyCheckpointError::ShuttingDown)
} else {
result.expect("commit_finalized_block should not panic")
result.expect("commit_checkpoint_verified should not panic")
};
if result.is_err() {
// If there was an error committing the block, then this verifier
Expand Down
167 changes: 89 additions & 78 deletions zebra-state/src/service.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions zebra-state/src/service/finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use zebra_chain::{block, parameters::Network};

use crate::{
request::ContextuallyVerifiedBlockWithTrees,
service::{check, QueuedFinalized},
service::{check, QueuedCheckpointVerified},
BoxError, CheckpointVerifiedBlock, CloneError, Config,
};

Expand Down Expand Up @@ -167,7 +167,7 @@ impl FinalizedState {
/// order.
pub fn commit_finalized(
&mut self,
ordered_block: QueuedFinalized,
ordered_block: QueuedCheckpointVerified,
) -> Result<CheckpointVerifiedBlock, BoxError> {
let (checkpoint_verified, rsp_tx) = ordered_block;
let result = self.commit_finalized_direct(
Expand Down
21 changes: 12 additions & 9 deletions zebra-state/src/service/queued_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::{BoxError, CheckpointVerifiedBlock, SemanticallyVerifiedBlock};
#[cfg(test)]
mod tests;

/// A queued finalized block, and its corresponding [`Result`] channel.
pub type QueuedFinalized = (
/// A finalized state queue block, and its corresponding [`Result`] channel.
pub type QueuedCheckpointVerified = (
CheckpointVerifiedBlock,
oneshot::Sender<Result<block::Hash, BoxError>>,
);

/// A queued non-finalized block, and its corresponding [`Result`] channel.
pub type QueuedNonFinalized = (
/// A non-finalized state queue block, and its corresponding [`Result`] channel.
pub type QueuedSemanticallyVerified = (
SemanticallyVerifiedBlock,
oneshot::Sender<Result<block::Hash, BoxError>>,
);
Expand All @@ -31,7 +31,7 @@ pub type QueuedNonFinalized = (
#[derive(Debug, Default)]
pub struct QueuedBlocks {
/// Blocks awaiting their parent blocks for contextual verification.
blocks: HashMap<block::Hash, QueuedNonFinalized>,
blocks: HashMap<block::Hash, QueuedSemanticallyVerified>,
/// Hashes from `queued_blocks`, indexed by parent hash.
by_parent: HashMap<block::Hash, HashSet<block::Hash>>,
/// Hashes from `queued_blocks`, indexed by block height.
Expand All @@ -47,7 +47,7 @@ impl QueuedBlocks {
///
/// - if a block with the same `block::Hash` has already been queued.
#[instrument(skip(self), fields(height = ?new.0.height, hash = %new.0.hash))]
pub fn queue(&mut self, new: QueuedNonFinalized) {
pub fn queue(&mut self, new: QueuedSemanticallyVerified) {
let new_hash = new.0.hash;
let new_height = new.0.height;
let parent_hash = new.0.block.header.previous_block_hash;
Expand Down Expand Up @@ -86,7 +86,10 @@ impl QueuedBlocks {
/// Dequeue and return all blocks that were waiting for the arrival of
/// `parent`.
#[instrument(skip(self), fields(%parent_hash))]
pub fn dequeue_children(&mut self, parent_hash: block::Hash) -> Vec<QueuedNonFinalized> {
pub fn dequeue_children(
&mut self,
parent_hash: block::Hash,
) -> Vec<QueuedSemanticallyVerified> {
let queued_children = self
.by_parent
.remove(&parent_hash)
Expand Down Expand Up @@ -176,7 +179,7 @@ impl QueuedBlocks {
}

/// Return the queued block if it has already been registered
pub fn get_mut(&mut self, hash: &block::Hash) -> Option<&mut QueuedNonFinalized> {
pub fn get_mut(&mut self, hash: &block::Hash) -> Option<&mut QueuedSemanticallyVerified> {
self.blocks.get_mut(hash)
}

Expand Down Expand Up @@ -208,7 +211,7 @@ impl QueuedBlocks {
/// Returns all key-value pairs of blocks as an iterator.
///
/// Doesn't update the metrics, because it is only used when the state is being dropped.
pub fn drain(&mut self) -> Drain<'_, block::Hash, QueuedNonFinalized> {
pub fn drain(&mut self) -> Drain<'_, block::Hash, QueuedSemanticallyVerified> {
self.known_utxos.clear();
self.known_utxos.shrink_to_fit();
self.by_parent.clear();
Expand Down
6 changes: 3 additions & 3 deletions zebra-state/src/service/queued_blocks/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ use zebra_test::prelude::*;

use crate::{
arbitrary::Prepare,
service::queued_blocks::{QueuedBlocks, QueuedNonFinalized},
service::queued_blocks::{QueuedBlocks, QueuedSemanticallyVerified},
tests::FakeChainHelper,
};

// Quick helper trait for making queued blocks with throw away channels
trait IntoQueued {
fn into_queued(self) -> QueuedNonFinalized;
fn into_queued(self) -> QueuedSemanticallyVerified;
}

impl IntoQueued for Arc<Block> {
fn into_queued(self) -> QueuedNonFinalized {
fn into_queued(self) -> QueuedSemanticallyVerified {
let (rsp_tx, _) = oneshot::channel();
(self.prepare(), rsp_tx)
}
Expand Down
8 changes: 4 additions & 4 deletions zebra-state/src/service/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ proptest! {
expected_finalized_value_pool += *block_value_pool;
}

let result_receiver = state_service.queue_and_commit_finalized(block.clone());
let result_receiver = state_service.queue_and_commit_to_finalized_state(block.clone());
let result = result_receiver.blocking_recv();

prop_assert!(result.is_ok(), "unexpected failed finalized block commit: {:?}", result);
Expand All @@ -450,7 +450,7 @@ proptest! {
let block_value_pool = &block.block.chain_value_pool_change(&transparent::utxos_from_ordered_utxos(utxos))?;
expected_non_finalized_value_pool += *block_value_pool;

let result_receiver = state_service.queue_and_commit_non_finalized(block.clone());
let result_receiver = state_service.queue_and_commit_to_non_finalized_state(block.clone());
let result = result_receiver.blocking_recv();

prop_assert!(result.is_ok(), "unexpected failed non-finalized block commit: {:?}", result);
Expand Down Expand Up @@ -509,7 +509,7 @@ proptest! {
TipAction::grow_with(expected_block.clone().into())
};

let result_receiver = state_service.queue_and_commit_finalized(block);
let result_receiver = state_service.queue_and_commit_to_finalized_state(block);
let result = result_receiver.blocking_recv();

prop_assert!(result.is_ok(), "unexpected failed finalized block commit: {:?}", result);
Expand All @@ -532,7 +532,7 @@ proptest! {
TipAction::grow_with(expected_block.clone().into())
};

let result_receiver = state_service.queue_and_commit_non_finalized(block);
let result_receiver = state_service.queue_and_commit_to_non_finalized_state(block);
let result = result_receiver.blocking_recv();

prop_assert!(result.is_ok(), "unexpected failed non-finalized block commit: {:?}", result);
Expand Down
6 changes: 3 additions & 3 deletions zebra-state/src/service/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
check,
finalized_state::{FinalizedState, ZebraDb},
non_finalized_state::NonFinalizedState,
queued_blocks::{QueuedFinalized, QueuedNonFinalized},
queued_blocks::{QueuedCheckpointVerified, QueuedSemanticallyVerified},
BoxError, ChainTipBlock, ChainTipSender, CloneError,
},
CommitSemanticallyVerifiedError, SemanticallyVerifiedBlock,
Expand Down Expand Up @@ -131,8 +131,8 @@ fn update_latest_chain_channels(
)
)]
pub fn write_blocks_from_channels(
mut finalized_block_write_receiver: UnboundedReceiver<QueuedFinalized>,
mut non_finalized_block_write_receiver: UnboundedReceiver<QueuedNonFinalized>,
mut finalized_block_write_receiver: UnboundedReceiver<QueuedCheckpointVerified>,
mut non_finalized_block_write_receiver: UnboundedReceiver<QueuedSemanticallyVerified>,
mut finalized_state: FinalizedState,
mut non_finalized_state: NonFinalizedState,
invalid_block_reset_sender: UnboundedSender<block::Hash>,
Expand Down