From 50a4ba1b1466509de3106c9d9995bc094500c54c Mon Sep 17 00:00:00 2001 From: Boyu Yang Date: Tue, 2 Apr 2024 21:41:34 +0800 Subject: [PATCH] fix: overflow when do addition --- src/cli/serve.rs | 3 ++- src/components/storage/prelude.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cli/serve.rs b/src/cli/serve.rs index 49e4a78..d7a0895 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -64,6 +64,7 @@ pub struct Args { /// Don't update all headers in one CKB transaction, /// to avoid size limit or cycles limit. + /// `0` means no limit. #[arg(long, default_value = "10")] pub(crate) spv_headers_update_limit: u32, @@ -158,7 +159,7 @@ impl Args { let spv_tip_height = input.curr.client.headers_mmr_root.max_height; let (spv_client, spv_update) = - storage.generate_spv_client_and_spv_update(spv_tip_height, u32::MAX)?; + storage.generate_spv_client_and_spv_update(spv_tip_height, 0)?; let tx_hash = self.reorg_spv_cells(&spv_service, input, spv_client, spv_update)?; diff --git a/src/components/storage/prelude.rs b/src/components/storage/prelude.rs index 302c581..42579ce 100644 --- a/src/components/storage/prelude.rs +++ b/src/components/storage/prelude.rs @@ -161,9 +161,10 @@ pub(crate) trait BitcoinSpvStorage: InternalBitcoinSpvStorage { limit: u32, ) -> Result<(SpvClient, packed::SpvUpdate)> { let mut tip_height = self.get_tip_bitcoin_height()?; - if tip_height > prev_height + limit { - tip_height = prev_height + limit; + if limit > 0 && tip_height > prev_height.saturating_add(limit) { + tip_height = prev_height.saturating_add(limit); } + log::trace!("new tip height will be {tip_height}, prev {prev_height}, limit {limit}",); let tip_header = self.get_bitcoin_header(tip_height)?; let (headers_mmr_root, headers_mmr_proof) = { let (base_height, mmr) = self.chain_root_mmr(tip_height)?;