Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 8 bit-flags into script type args to control features #11

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "2464c8f"
rev = "837a307"
default-features = false
features = ["no-std"]
22 changes: 14 additions & 8 deletions contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
// - 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 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,
fork_client_id,
flags,
) = {
let (
mut input_info,
expected_tip_client_id,
expected_client_ids,
previous_tip_height,
fork_client_id,
flags,
) = load_inputs(inputs)?;
input_info.tip_client_id = expected_tip_client_id;
(
Expand All @@ -42,6 +45,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
expected_client_ids,
previous_tip_height,
fork_client_id,
flags,
)
};
// Checks the output info cell and the output client cells;
Expand Down Expand Up @@ -76,12 +80,12 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
}
};

expected_input_client.verify_new_client(&output_client, update)?;
expected_input_client.verify_new_client(&output_client, update, flags)?;

Ok(())
}

fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8, u8)> {
let mut client_ids_with_indexes = Vec::new();
let mut input_info_opt = None;
for i in inputs {
Expand Down Expand Up @@ -135,16 +139,17 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
}
};

let clients_count: u8 = {
let (clients_count, flags) = {
let script = hl::load_script()?;
let script_args = script.args();
let script_args_slice = script_args.as_reader().raw_data();
SpvTypeArgsReader::from_slice(script_args_slice)
.map_err(|_| SysError::Encoding)?
.clients_count()
.into()
let args =
SpvTypeArgsReader::from_slice(script_args_slice).map_err(|_| SysError::Encoding)?;
let clients_count: u8 = args.clients_count().into();
let flags: u8 = args.flags().into();
(clients_count, flags)
};
debug!("clients count: {clients_count}");
debug!("clients count: {clients_count}, flags: {flags:08b}");

let mut client_ids = client_ids_with_indexes
.into_iter()
Expand Down Expand Up @@ -179,6 +184,7 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
expected_client_ids,
tip_height,
fork_client_id,
flags,
))
}

Expand Down
25 changes: 13 additions & 12 deletions contracts/ckb-bitcoin-spv-type-lock/src/operations/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub(crate) fn update_client(
// - expected output info cell base on the input info cell,
// - the tip client id.
// - the expected client id, which will be the next tip client id.
let (expected_info, tip_client_id, expected_client_id) = {
let (mut input_info, tip_client_id, expected_client_id) = load_inputs(inputs)?;
let (expected_info, tip_client_id, expected_client_id, flags) = {
let (mut input_info, tip_client_id, expected_client_id, flags) = load_inputs(inputs)?;
input_info.tip_client_id = expected_client_id;
(input_info, tip_client_id, expected_client_id)
(input_info, tip_client_id, expected_client_id, flags)
};
// Checks the output info cell, then returns the client cell and the index of the info cell.
let (output_client, output_info_index) = load_outputs(outputs, &expected_info)?;
Expand All @@ -52,12 +52,12 @@ pub(crate) fn update_client(
}
};

expected_input_client.verify_new_client(&output_client, update)?;
expected_input_client.verify_new_client(&output_client, update, flags)?;

Ok(())
}

fn load_inputs(inputs: (usize, usize)) -> Result<(SpvInfo, u8, u8)> {
fn load_inputs(inputs: (usize, usize)) -> Result<(SpvInfo, u8, u8, u8)> {
debug!("load cell data of inputs[{}]", inputs.0);
let input_data_0 = hl::load_cell_data(inputs.0, Source::Input)?;
debug!("load cell data of inputs[{}]", inputs.1);
Expand Down Expand Up @@ -90,24 +90,25 @@ fn load_inputs(inputs: (usize, usize)) -> Result<(SpvInfo, u8, u8)> {
let input_client_id: u8 = packed_input_client.id().into();
debug!("input client id = {input_client_id}");

let clients_count: u8 = {
let (clients_count, flags) = {
let script = hl::load_script()?;
let script_args = script.args();
let script_args_slice = script_args.as_reader().raw_data();
SpvTypeArgsReader::from_slice(script_args_slice)
.map_err(|_| SysError::Encoding)?
.clients_count()
.into()
let args =
SpvTypeArgsReader::from_slice(script_args_slice).map_err(|_| SysError::Encoding)?;
let clients_count: u8 = args.clients_count().into();
let flags: u8 = args.flags().into();
(clients_count, flags)
};
debug!("clients count: {clients_count}");
debug!("clients count: {clients_count}, flags: {flags:08b}");

let expected_client_id = utilities::next_client_id(input_info.tip_client_id, clients_count);
debug!("expected client id = {expected_client_id}");
if input_client_id != expected_client_id {
return Err(InternalError::UpdateInputClientIdIsMismatch.into());
}

Ok((input_info, tip_client_id, expected_client_id))
Ok((input_info, tip_client_id, expected_client_id, flags))
}

fn load_outputs(
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 = "2464c8f"
rev = "837a307"

[dev-dependencies.ckb-bitcoin-spv-verifier]
version = "0.1.0"
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
rev = "2464c8f"
rev = "837a307"