Skip to content

Commit

Permalink
Factor out a best_chain_tip_height() function (#5540)
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 authored Nov 4, 2022
1 parent 13cd8b9 commit 75f83fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
27 changes: 19 additions & 8 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ where
+ Sync
+ 'static,
State::Future: Send,
Tip: ChainTip + Send + Sync + 'static,
Tip: ChainTip + Clone + Send + Sync + 'static,
{
fn get_info(&self) -> Result<GetInfo> {
let response = GetInfo {
Expand Down Expand Up @@ -869,18 +869,16 @@ where
request: GetAddressTxIdsRequest,
) -> BoxFuture<Result<Vec<String>>> {
let mut state = self.state.clone();
let latest_chain_tip = self.latest_chain_tip.clone();

let start = Height(request.start);
let end = Height(request.end);

let chain_height = self.latest_chain_tip.best_tip_height().ok_or(Error {
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
});

async move {
let chain_height = best_chain_tip_height(&latest_chain_tip)?;

// height range checks
check_height_range(start, end, chain_height?)?;
check_height_range(start, end, chain_height)?;

let valid_addresses = AddressStrings {
addresses: request.addresses,
Expand Down Expand Up @@ -994,6 +992,19 @@ where
}
}

/// Returns the best chain tip height of `latest_chain_tip`,
/// or an RPC error if there are no blocks in the state.
pub fn best_chain_tip_height<Tip>(latest_chain_tip: &Tip) -> Result<Height>
where
Tip: ChainTip + Clone + Send + Sync + 'static,
{
latest_chain_tip.best_tip_height().ok_or(Error {
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
})
}

/// Response to a `getinfo` RPC request.
///
/// See the notes for the [`Rpc::get_info` method].
Expand Down
24 changes: 8 additions & 16 deletions zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use zebra_consensus::{BlockError, VerifyBlockError, VerifyChainError, VerifyChec
use zebra_node_services::mempool;

use crate::methods::{
best_chain_tip_height,
get_block_template_rpcs::types::{
default_roots::DefaultRoots, get_block_template::GetBlockTemplate, hex_data::HexData,
submit_block, transaction::TransactionTemplate,
Expand Down Expand Up @@ -197,7 +198,7 @@ where
+ Sync
+ 'static,
<State as Service<zebra_state::ReadRequest>>::Future: Send,
Tip: ChainTip + Send + Sync + 'static,
Tip: ChainTip + Clone + Send + Sync + 'static,
ChainVerifier: Service<Arc<Block>, Response = block::Hash, Error = zebra_consensus::BoxError>
+ Clone
+ Send
Expand All @@ -206,27 +207,15 @@ where
<ChainVerifier as Service<Arc<Block>>>::Future: Send,
{
fn get_block_count(&self) -> Result<u32> {
self.latest_chain_tip
.best_tip_height()
.map(|height| height.0)
.ok_or(Error {
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
})
best_chain_tip_height(&self.latest_chain_tip).map(|height| height.0)
}

fn get_block_hash(&self, index: i32) -> BoxFuture<Result<GetBlockHash>> {
let mut state = self.state.clone();

let maybe_tip_height = self.latest_chain_tip.best_tip_height();
let latest_chain_tip = self.latest_chain_tip.clone();

async move {
let tip_height = maybe_tip_height.ok_or(Error {
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
})?;
let tip_height = best_chain_tip_height(&latest_chain_tip)?;

let height = get_height_from_int(index, tip_height)?;

Expand Down Expand Up @@ -256,9 +245,12 @@ where

fn get_block_template(&self) -> BoxFuture<Result<GetBlockTemplate>> {
let mempool = self.mempool.clone();
let latest_chain_tip = self.latest_chain_tip.clone();

// Since this is a very large RPC, we use separate functions for each group of fields.
async move {
let _tip_height = best_chain_tip_height(&latest_chain_tip)?;

// TODO: put this in a separate get_mempool_transactions() function
let request = mempool::Request::FullTransactions;
let response = mempool.oneshot(request).await.map_err(|error| Error {
Expand Down

0 comments on commit 75f83fc

Please sign in to comment.