From 50fa8360cbff1aad2f5f2aede5ca009283ceb9e7 Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Mon, 28 Aug 2023 12:24:08 +0800 Subject: [PATCH] Use block number as cursor Using the RPC cursor param for polling doesn't seem to be supported. --- forcerelay-ckb-sdk-server/src/lib.rs | 19 +++++++++++++++---- src/search.rs | 24 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/forcerelay-ckb-sdk-server/src/lib.rs b/forcerelay-ckb-sdk-server/src/lib.rs index 579a7b5..c8373c7 100644 --- a/forcerelay-ckb-sdk-server/src/lib.rs +++ b/forcerelay-ckb-sdk-server/src/lib.rs @@ -32,7 +32,12 @@ use serde_with::{serde_as, TryFromIntoRef}; #[rpc] #[async_trait] pub trait Rpc { - async fn search_packet_cells(&self, config: Config, limit: u32) -> Result>; + async fn search_packet_cells( + &self, + config: Config, + limit: u32, + mut first_block_to_search: u64, + ) -> Result<(Vec, u64)>; async fn get_latest_channel_cell(&self, config: Config) -> Result; async fn get_axon_metadata_cell_dep(&self, config: Config) -> Result; @@ -115,10 +120,16 @@ impl RpcImpl { #[async_trait] impl Rpc for RpcImpl { - async fn search_packet_cells(&self, config: Config, limit: u32) -> Result> { - PacketCell::search(&self.client, &config, limit) + async fn search_packet_cells( + &self, + config: Config, + limit: u32, + mut first_block_to_search: u64, + ) -> Result<(Vec, u64)> { + let cells = PacketCell::search(&self.client, &config, limit, &mut first_block_to_search) .await - .map_err(internal_error) + .map_err(internal_error)?; + Ok((cells, first_block_to_search)) } async fn get_latest_channel_cell(&self, config: Config) -> Result { IbcChannelCell::get_latest(&self.client, &config) diff --git a/src/search.rs b/src/search.rs index 4beebee..7c2a419 100644 --- a/src/search.rs +++ b/src/search.rs @@ -6,7 +6,7 @@ use ckb_ics_axon::{ message::{Envelope, MsgType}, PacketArgs, }; -use ckb_jsonrpc_types::{CellOutput, JsonBytes, OutPoint, Script, TransactionView}; +use ckb_jsonrpc_types::{CellOutput, OutPoint, Script, TransactionView}; use ckb_sdk::rpc::ckb_indexer; use ckb_types::{ packed, @@ -32,8 +32,13 @@ pub struct PacketCell { impl PacketCell { /// Search for and parse live packet cells. - pub async fn search(client: &CkbRpcClient, config: &Config, limit: u32) -> Result> { - search_packet_cells(client, config, limit, &mut None).await + pub async fn search( + client: &CkbRpcClient, + config: &Config, + limit: u32, + first_block_to_search: &mut u64, + ) -> Result> { + search_packet_cells(client, config, limit, first_block_to_search).await } pub fn subscribe( @@ -41,7 +46,7 @@ impl PacketCell { config: Config, ) -> impl Stream>> { async_stream::try_stream! { - let mut cursor = None; + let mut cursor = 0; loop { let cells = search_packet_cells(&client, &config, 64, &mut cursor).await?; if !cells.is_empty() { @@ -87,7 +92,7 @@ async fn search_packet_cells( client: &CkbRpcClient, config: &Config, limit: u32, - cursor: &mut Option, + first_block_to_search: &mut u64, ) -> Result> { ensure!(limit > 0); let tip = client @@ -95,12 +100,15 @@ async fn search_packet_cells( .await? .map_or(0, |t| t.block_number.into()); let last_block_to_search = tip.saturating_sub(config.confirmations.into()); + if *first_block_to_search > last_block_to_search { + return Ok(vec![]); + } let cells = client .get_cells( ckb_indexer::SearchKey { filter: Some(ckb_indexer::SearchKeyFilter { block_range: Some([ - 0.into(), + (*first_block_to_search).into(), // +1 because this is exclusive. last_block_to_search.saturating_add(1).into(), ]), @@ -114,7 +122,7 @@ async fn search_packet_cells( }, ckb_indexer::Order::Asc, limit.into(), - cursor.clone(), + None, ) .await?; @@ -134,7 +142,7 @@ async fn search_packet_cells( }; result.push(p); } - *cursor = Some(cells.last_cursor); + *first_block_to_search = last_block_to_search.saturating_add(1); Ok(result) }