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 632b664 commit 6d7d53f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 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 @@ -1945,6 +1945,10 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
}
}

if headers.len() < BATCH_SIZE {
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 6d7d53f

Please sign in to comment.