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
+ 'static,
State::Future: Send,
Tip: ChainTip + Send + Sync + 'static,
Tip: ChainTip + Clone + Send + Sync + 'static,
{
fn get_info(&self) -> Result<GetInfo> {
let response = GetInfo {
@ -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,
@ -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].

View File

@ -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,
@ -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
@ -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)?;
@ -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 {