fix get_hashes

This commit is contained in:
hinto.janai 2025-01-17 14:34:16 -05:00
parent 0b3ff4e14c
commit 78b3ff1925
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
2 changed files with 34 additions and 10 deletions

View file

@ -177,18 +177,16 @@ async fn get_hashes(
request.block_ids[len - 1] request.block_ids[len - 1]
}; };
let mut bytes = request.block_ids; let GetHashesRequest {
let hashes: Vec<[u8; 32]> = (&bytes).into(); start_height,
block_ids,
..
} = request;
let (current_height, _) = helper::top_height(&mut state).await?; let hashes: Vec<[u8; 32]> = (&block_ids).into();
let Some((index, start_height)) = let (m_block_ids, current_height) =
blockchain::find_first_unknown(&mut state.blockchain_read, hashes).await? blockchain::next_chain_entry(&mut state.blockchain_read, hashes, start_height).await?;
else {
return Err(anyhow!("Failed"));
};
let m_blocks_ids = bytes.split_off(index);
Ok(GetHashesResponse { Ok(GetHashesResponse {
base: helper::access_response_base(false), base: helper::access_response_base(false),

View file

@ -115,6 +115,32 @@ pub async fn find_block(
Ok(option) Ok(option)
} }
/// [`BlockchainReadRequest::NextChainEntry`].
///
/// Returns only the:
/// - block IDs
/// - current chain height
pub async fn next_chain_entry(
blockchain_read: &mut BlockchainReadHandle,
block_hashes: Vec<[u8; 32]>,
len: usize,
) -> Result<(Vec<[u8; 32]>, Option<std::num::NonZero<usize>>), Error> {
let BlockchainResponse::NextChainEntry {
block_ids,
chain_height,
..
} = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::NextChainEntry(block_hashes, len))
.await?
else {
unreachable!();
};
Ok((block_ids, chain_height))
}
/// [`BlockchainReadRequest::FilterUnknownHashes`]. /// [`BlockchainReadRequest::FilterUnknownHashes`].
pub async fn filter_unknown_hashes( pub async fn filter_unknown_hashes(
blockchain_read: &mut BlockchainReadHandle, blockchain_read: &mut BlockchainReadHandle,