From 1c145a47b0d232b044e29ec41da881ab5dd6b718 Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Thu, 10 Oct 2024 17:29:39 +0800 Subject: [PATCH 1/2] fix reorg: remove check partial_chain_work. --- checksums.txt | 2 +- .../src/operations/reorg.rs | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/checksums.txt b/checksums.txt index 3d25bfd..933b411 100644 --- a/checksums.txt +++ b/checksums.txt @@ -1,2 +1,2 @@ 6bbea4820329050e1fc65f9c15cab5948b824c73aa3430d7d92b793c53ca66b6 build/release/can-update-without-ownership-lock -eaa9e3aeb1205e611078a819e960c33f4934ea49029aab3445a241955e696ee1 build/release/ckb-bitcoin-spv-type-lock +cfa7d777f246ebd037af156db73c6c7eab3e8e4e1f6e15783a68d2a164875032 build/release/ckb-bitcoin-spv-type-lock diff --git a/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs b/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs index 8566e42..5a9c687 100644 --- a/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs +++ b/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs @@ -26,7 +26,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[ expected_info, expected_tip_client_id, expected_client_ids, - previous_chain_work, + _previous_chain_work, fork_client_id, flags, ) = { @@ -53,13 +53,17 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[ let (output_client, output_info_index) = load_outputs(outputs, &expected_info, expected_client_ids)?; { - let new_chain_work: U256 = output_client - .headers_mmr_root() - .partial_chain_work() - .unpack(); - if previous_chain_work >= new_chain_work { - return Err(InternalError::ReorgNotBetterChain.into()); - } + // If there is a limit on the number of headers to update + // it may cause the current work to be insufficient, but still on the main chain + // so here we no longer check the chain work + + // let new_chain_work: U256 = output_client + // .headers_mmr_root() + // .partial_chain_work() + // .unpack(); + // if previous_chain_work >= new_chain_work { + // return Err(InternalError::ReorgNotBetterChain.into()); + // } } // Finds the only one index of cell deps which use current script. // That cell should be the client which at the fork point. From 1a1a0aa597312176a3cbdebec443a1c9e72fe73e Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Fri, 11 Oct 2024 13:30:57 +0800 Subject: [PATCH 2/2] remove chain work check for testnet 3 only. --- checksums.txt | 2 +- .../src/operations/reorg.rs | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/checksums.txt b/checksums.txt index 933b411..da15da4 100644 --- a/checksums.txt +++ b/checksums.txt @@ -1,2 +1,2 @@ 6bbea4820329050e1fc65f9c15cab5948b824c73aa3430d7d92b793c53ca66b6 build/release/can-update-without-ownership-lock -cfa7d777f246ebd037af156db73c6c7eab3e8e4e1f6e15783a68d2a164875032 build/release/ckb-bitcoin-spv-type-lock +cd553a3858df32dbf051957ec97014200e632fed016084b28b25fcbbc49863d1 build/release/ckb-bitcoin-spv-type-lock diff --git a/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs b/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs index 5a9c687..fb1aa28 100644 --- a/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs +++ b/contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs @@ -1,7 +1,7 @@ use alloc::vec::Vec; use ckb_bitcoin_spv_verifier::types::{ - core::{SpvClient, SpvInfo, U256}, + core::{BitcoinChainType, SpvClient, SpvInfo, U256}, packed::{self, SpvClientReader, SpvInfoReader, SpvTypeArgsReader, SpvUpdateReader}, prelude::*, }; @@ -26,7 +26,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[ expected_info, expected_tip_client_id, expected_client_ids, - _previous_chain_work, + previous_chain_work, fork_client_id, flags, ) = { @@ -53,17 +53,21 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[ let (output_client, output_info_index) = load_outputs(outputs, &expected_info, expected_client_ids)?; { - // If there is a limit on the number of headers to update - // it may cause the current work to be insufficient, but still on the main chain - // so here we no longer check the chain work - - // let new_chain_work: U256 = output_client - // .headers_mmr_root() - // .partial_chain_work() - // .unpack(); - // if previous_chain_work >= new_chain_work { - // return Err(InternalError::ReorgNotBetterChain.into()); - // } + // Due to the block storm issue on testnet 3, a large number of blocks may be rolled back + // during a reorg, making it necessary to limit the update height. + // If there is a limit on the number of headers to update, + // the current chain work might not be sufficient but still remain on the main chain. + // Therefore, in this case, we no longer check the chain work. + // This handling is specific to testnet 3 to address the frequent block storm reorgs. + if BitcoinChainType::Testnet != flags.into() { + let new_chain_work: U256 = output_client + .headers_mmr_root() + .partial_chain_work() + .unpack(); + if previous_chain_work >= new_chain_work { + return Err(InternalError::ReorgNotBetterChain.into()); + } + } } // Finds the only one index of cell deps which use current script. // That cell should be the client which at the fork point.