From 78b3ff19251654f7e3c498f93de418476aa283f7 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 17 Jan 2025 14:34:16 -0500 Subject: [PATCH] fix `get_hashes` --- binaries/cuprated/src/rpc/handlers/bin.rs | 18 ++++++------- .../cuprated/src/rpc/service/blockchain.rs | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/binaries/cuprated/src/rpc/handlers/bin.rs b/binaries/cuprated/src/rpc/handlers/bin.rs index 239ac8d..bb0d015 100644 --- a/binaries/cuprated/src/rpc/handlers/bin.rs +++ b/binaries/cuprated/src/rpc/handlers/bin.rs @@ -177,18 +177,16 @@ async fn get_hashes( request.block_ids[len - 1] }; - let mut bytes = request.block_ids; - let hashes: Vec<[u8; 32]> = (&bytes).into(); + let GetHashesRequest { + 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)) = - blockchain::find_first_unknown(&mut state.blockchain_read, hashes).await? - else { - return Err(anyhow!("Failed")); - }; - - let m_blocks_ids = bytes.split_off(index); + let (m_block_ids, current_height) = + blockchain::next_chain_entry(&mut state.blockchain_read, hashes, start_height).await?; Ok(GetHashesResponse { base: helper::access_response_base(false), diff --git a/binaries/cuprated/src/rpc/service/blockchain.rs b/binaries/cuprated/src/rpc/service/blockchain.rs index 0912ee7..4f6402e 100644 --- a/binaries/cuprated/src/rpc/service/blockchain.rs +++ b/binaries/cuprated/src/rpc/service/blockchain.rs @@ -115,6 +115,32 @@ pub async fn find_block( 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>), 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`]. pub async fn filter_unknown_hashes( blockchain_read: &mut BlockchainReadHandle,