Factor out a best_chain_tip_height() function (#5540)

This commit is contained in:
teor 2022-11-05 04:19:48 +10:00 committed by GitHub
parent 13cd8b9c2c
commit 75f83fc5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 24 deletions

View File

@ -360,7 +360,7 @@ where
+ Sync + Sync
+ 'static, + 'static,
State::Future: Send, State::Future: Send,
Tip: ChainTip + Send + Sync + 'static, Tip: ChainTip + Clone + Send + Sync + 'static,
{ {
fn get_info(&self) -> Result<GetInfo> { fn get_info(&self) -> Result<GetInfo> {
let response = GetInfo { let response = GetInfo {
@ -869,18 +869,16 @@ where
request: GetAddressTxIdsRequest, request: GetAddressTxIdsRequest,
) -> BoxFuture<Result<Vec<String>>> { ) -> BoxFuture<Result<Vec<String>>> {
let mut state = self.state.clone(); let mut state = self.state.clone();
let latest_chain_tip = self.latest_chain_tip.clone();
let start = Height(request.start); let start = Height(request.start);
let end = Height(request.end); 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 { async move {
let chain_height = best_chain_tip_height(&latest_chain_tip)?;
// height range checks // height range checks
check_height_range(start, end, chain_height?)?; check_height_range(start, end, chain_height)?;
let valid_addresses = AddressStrings { let valid_addresses = AddressStrings {
addresses: request.addresses, addresses: request.addresses,
@ -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. /// Response to a `getinfo` RPC request.
/// ///
/// See the notes for the [`Rpc::get_info` method]. /// See the notes for the [`Rpc::get_info` method].

View File

@ -18,6 +18,7 @@ use zebra_consensus::{BlockError, VerifyBlockError, VerifyChainError, VerifyChec
use zebra_node_services::mempool; use zebra_node_services::mempool;
use crate::methods::{ use crate::methods::{
best_chain_tip_height,
get_block_template_rpcs::types::{ get_block_template_rpcs::types::{
default_roots::DefaultRoots, get_block_template::GetBlockTemplate, hex_data::HexData, default_roots::DefaultRoots, get_block_template::GetBlockTemplate, hex_data::HexData,
submit_block, transaction::TransactionTemplate, submit_block, transaction::TransactionTemplate,
@ -197,7 +198,7 @@ where
+ Sync + Sync
+ 'static, + 'static,
<State as Service<zebra_state::ReadRequest>>::Future: Send, <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> ChainVerifier: Service<Arc<Block>, Response = block::Hash, Error = zebra_consensus::BoxError>
+ Clone + Clone
+ Send + Send
@ -206,27 +207,15 @@ where
<ChainVerifier as Service<Arc<Block>>>::Future: Send, <ChainVerifier as Service<Arc<Block>>>::Future: Send,
{ {
fn get_block_count(&self) -> Result<u32> { fn get_block_count(&self) -> Result<u32> {
self.latest_chain_tip best_chain_tip_height(&self.latest_chain_tip).map(|height| height.0)
.best_tip_height()
.map(|height| height.0)
.ok_or(Error {
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
})
} }
fn get_block_hash(&self, index: i32) -> BoxFuture<Result<GetBlockHash>> { fn get_block_hash(&self, index: i32) -> BoxFuture<Result<GetBlockHash>> {
let mut state = self.state.clone(); let mut state = self.state.clone();
let latest_chain_tip = self.latest_chain_tip.clone();
let maybe_tip_height = self.latest_chain_tip.best_tip_height();
async move { async move {
let tip_height = maybe_tip_height.ok_or(Error { let tip_height = best_chain_tip_height(&latest_chain_tip)?;
code: ErrorCode::ServerError(0),
message: "No blocks in state".to_string(),
data: None,
})?;
let height = get_height_from_int(index, tip_height)?; let height = get_height_from_int(index, tip_height)?;
@ -256,9 +245,12 @@ where
fn get_block_template(&self) -> BoxFuture<Result<GetBlockTemplate>> { fn get_block_template(&self) -> BoxFuture<Result<GetBlockTemplate>> {
let mempool = self.mempool.clone(); 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. // Since this is a very large RPC, we use separate functions for each group of fields.
async move { async move {
let _tip_height = best_chain_tip_height(&latest_chain_tip)?;
// TODO: put this in a separate get_mempool_transactions() function // TODO: put this in a separate get_mempool_transactions() function
let request = mempool::Request::FullTransactions; let request = mempool::Request::FullTransactions;
let response = mempool.oneshot(request).await.map_err(|error| Error { let response = mempool.oneshot(request).await.map_err(|error| Error {