Skip to content

Commit

Permalink
cleanup(state): condenses match arms for redirected StateService requ…
Browse files Browse the repository at this point in the history
…ests and moves metrics counting to Request methods (#5137)

* Move AwaitUtxos next to the other shared writeable state requests

* Rename ReadResponse::Utxos to ReadResponse::AddressUtxos

```sh
fastmod Utxos AddressUtxos zebra*
```

* Rename an out_point variable to outpoint for consistency

* Rename transparent_utxos to address_utxos

```sh
fastmod transparent_utxos address_utxos zebra*
```

* Run AwaitUtxo without accessing shared mutable chain state

* Fix some incorrect comments

* Explain why some concurrent reads are ok

* Add a TODO

* Stop using self.mem in AwaitUtxo requests

* Update state service module documentation

* Move the QueuedBlock type into the queued_blocks module

* Explain how spent UTXOs are treated by the state

* Clarify how cached Chains impact state read requests

And move repeated comments to the module header.

* fastmod ChainUtxo BestChainUtxo zebra*

* Add an AnyChainUtxo request

* Make AwaitUtxo non-blocking

* Move the finalized block queue into the StateService

* Move the queued_blocks module to the state service

* Move QueuedFinalized into queued_blocks

* Move the queued_blocks tests into their own module

* Make the FinalizedState cloneable

* cleanup of repetitive code

* fixes merge by adding back req.count_metric and removing the metrics::counter in AwaitUtxo

Co-authored-by: teor <[email protected]>
  • Loading branch information
arya2 and teor2345 authored Sep 16, 2022
1 parent 64d9843 commit 2fa1879
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 279 deletions.
58 changes: 58 additions & 0 deletions zebra-state/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,33 @@ pub enum Request {
},
}

impl Request {
fn variant_name(&self) -> &'static str {
match self {
Request::CommitBlock(_) => "commit_block",
Request::CommitFinalizedBlock(_) => "commit_finalized_block",
Request::AwaitUtxo(_) => "await_utxo",
Request::Depth(_) => "depth",
Request::Tip => "tip",
Request::BlockLocator => "block_locator",
Request::Transaction(_) => "transaction",
Request::Block(_) => "block",
Request::FindBlockHashes { .. } => "find_block_hashes",
Request::FindBlockHeaders { .. } => "find_block_headers",
}
}

/// Counts metric for StateService call
pub fn count_metric(&self) {
metrics::counter!(
"state.requests",
1,
"service" => "state",
"type" => self.variant_name()
);
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A read-only query about the chain state, via the
/// [`ReadStateService`](crate::service::ReadStateService).
Expand Down Expand Up @@ -670,6 +697,37 @@ pub enum ReadRequest {
UtxosByAddresses(HashSet<transparent::Address>),
}

impl ReadRequest {
fn variant_name(&self) -> &'static str {
match self {
ReadRequest::Tip => "tip",
ReadRequest::Depth(_) => "depth",
ReadRequest::Block(_) => "block",
ReadRequest::Transaction(_) => "transaction",
ReadRequest::BestChainUtxo { .. } => "best_chain_utxo",
ReadRequest::AnyChainUtxo { .. } => "any_chain_utxo",
ReadRequest::BlockLocator => "block_locator",
ReadRequest::FindBlockHashes { .. } => "find_block_hashes",
ReadRequest::FindBlockHeaders { .. } => "find_block_headers",
ReadRequest::SaplingTree { .. } => "sapling_tree",
ReadRequest::OrchardTree { .. } => "orchard_tree",
ReadRequest::AddressBalance { .. } => "address_balance",
ReadRequest::TransactionIdsByAddresses { .. } => "transaction_ids_by_addesses",
ReadRequest::UtxosByAddresses(_) => "utxos_by_addesses",
}
}

/// Counts metric for ReadStateService call
pub fn count_metric(&self) {
metrics::counter!(
"state.requests",
1,
"service" => "read_state",
"type" => self.variant_name()
);
}
}

/// Conversion from read-write [`Request`]s to read-only [`ReadRequest`]s.
///
/// Used to dispatch read requests concurrently from the [`StateService`](crate::service::StateService).
Expand Down
Loading

0 comments on commit 2fa1879

Please sign in to comment.