Skip to content

Commit

Permalink
fix(chain-storage): fetch_chain_headers should return empty vec if qu…
Browse files Browse the repository at this point in the history
…ery is out of range
  • Loading branch information
sdbondi committed Jun 17, 2022
1 parent 534d3af commit 10af53d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
if headers.is_empty() {
break;
}
let num_headers = headers.len();

for header in headers {
let block_hash_hex = header.hash().to_hex();
Expand Down Expand Up @@ -1945,6 +1946,10 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
}
}

if num_headers < BATCH_SIZE as usize {
break;
}

current_height += BATCH_SIZE + 1;
}
});
Expand Down
16 changes: 13 additions & 3 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,9 +1372,19 @@ pub fn fetch_chain_headers<T: BlockchainBackend>(
));
}

(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<T: BlockchainBackend>(db: &mut T, headers: Vec<ChainHeader>) -> Result<(), ChainStorageError> {
Expand Down

0 comments on commit 10af53d

Please sign in to comment.