Skip to content

Commit

Permalink
Merge pull request #13 from yangby-cryptape/bugfix/partial-chain-work
Browse files Browse the repository at this point in the history
fix: use partial chain work to select better chain
  • Loading branch information
chaoticlonghair authored Mar 21, 2024
2 parents 159ac69 + 19fcaf7 commit 19bcf80
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/ckb-bitcoin-spv-type-lock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ ckb-hash = { version = "0.112.1", default-features = false, features = ["ckb-con
[dependencies.ckb-bitcoin-spv-verifier]
version = "0.1.0"
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
rev = "837a307"
rev = "a6fce4b"
default-features = false
features = ["no-std"]
28 changes: 17 additions & 11 deletions contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;

use ckb_bitcoin_spv_verifier::types::{
core::{SpvClient, SpvInfo},
core::{SpvClient, SpvInfo, U256},
packed::{self, SpvClientReader, SpvInfoReader, SpvTypeArgsReader, SpvUpdateReader},
prelude::*,
};
Expand All @@ -19,22 +19,22 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
// - expected output info cell base on the input info cell,
// - the new tip client id.
// - the expected client ids, which will be the new tip client id and the ids of all the cleared clients.
// - the height of the old tip client.
// - the previous chain work of the old tip client.
// - the id of the last client, whose blocks are all in main chain.
// - the flags in SPV script args
let (
expected_info,
expected_tip_client_id,
expected_client_ids,
previous_tip_height,
previous_chain_work,
fork_client_id,
flags,
) = {
let (
mut input_info,
expected_tip_client_id,
expected_client_ids,
previous_tip_height,
previous_chain_work,
fork_client_id,
flags,
) = load_inputs(inputs)?;
Expand All @@ -43,7 +43,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
input_info,
expected_tip_client_id,
expected_client_ids,
previous_tip_height,
previous_chain_work,
fork_client_id,
flags,
)
Expand All @@ -53,8 +53,11 @@ 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_tip_height: u32 = output_client.headers_mmr_root().max_height().unpack();
if previous_tip_height >= new_tip_height {
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());
}
}
Expand Down Expand Up @@ -85,7 +88,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
Ok(())
}

fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8, u8)> {
fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, U256, u8, u8)> {
let mut client_ids_with_indexes = Vec::new();
let mut input_info_opt = None;
for i in inputs {
Expand Down Expand Up @@ -129,11 +132,14 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8, u8)>
.map(|(index, _)| *index)
.ok_or(InternalError::ReorgInputTipClientNotFound)?;
debug!("tip client index = {tip_client_index}");
let tip_height: u32 = {
let tip_chain_work: U256 = {
let input_data = hl::load_cell_data(tip_client_index, Source::Input)?;
if let Ok(packed_input_client) = SpvClientReader::from_slice(&input_data) {
debug!("tip client = {packed_input_client} (index={tip_client_index})");
packed_input_client.headers_mmr_root().max_height().unpack()
packed_input_client
.headers_mmr_root()
.partial_chain_work()
.unpack()
} else {
return Err(InternalError::ReorgInputTipClientLoadFailed.into());
}
Expand Down Expand Up @@ -182,7 +188,7 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8, u8)>
input_info,
expected_client_id,
expected_client_ids,
tip_height,
tip_chain_work,
fork_client_id,
flags,
))
Expand Down
4 changes: 2 additions & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ walkdir = "2.4"
[dev-dependencies.ckb-bitcoin-spv-prover]
version = "0.1.0"
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
rev = "837a307"
rev = "a6fce4b"

[dev-dependencies.ckb-bitcoin-spv-verifier]
version = "0.1.0"
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
rev = "837a307"
rev = "a6fce4b"
2 changes: 1 addition & 1 deletion tests/data
Submodule data updated from 5e608d to 83704f

0 comments on commit 19bcf80

Please sign in to comment.