From 6d7d53f2e6c77a031af98f52ec884e570f5b846c Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 17 Jun 2022 14:59:22 +0200 Subject: [PATCH] fix(chain-storage): fetch_chain_headers should return empty vec if query is out of range --- .../src/grpc/base_node_grpc_server.rs | 4 ++++ .../src/chain_storage/blockchain_database.rs | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs index 8c7760ba97..97a0e75a98 100644 --- a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs +++ b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs @@ -1945,6 +1945,10 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer { } } + if headers.len() < BATCH_SIZE { + break; + } + current_height += BATCH_SIZE + 1; } }); diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index 991c07f207..4a6f3232e1 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -1372,9 +1372,19 @@ pub fn fetch_chain_headers( )); } - (start..=end_inclusive) - .map(|h| db.fetch_chain_header_by_height(h)) - .collect() + #[allow(clippy::cast_possible_truncation)] + let mut headers = Vec::with_capacity((end_inclusive - start) as usize); + for h in start..=end_inclusive { + match db.fetch_chain_header_by_height(h) { + Ok(header) => { + headers.push(header); + }, + Err(ChainStorageError::ValueNotFound { .. }) => break, + Err(e) => return Err(e), + } + } + + Ok(headers) } fn insert_headers(db: &mut T, headers: Vec) -> Result<(), ChainStorageError> {