Skip to content

Commit 159ac69

Browse files
authored
Merge pull request #11 from yangby-cryptape/feature/add-flags
feat: add 8 bit-flags into script type args to control features
2 parents 549076d + fe9849d commit 159ac69

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/ckb-bitcoin-spv-type-lock/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ ckb-hash = { version = "0.112.1", default-features = false, features = ["ckb-con
1515
[dependencies.ckb-bitcoin-spv-verifier]
1616
version = "0.1.0"
1717
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
18-
rev = "2464c8f"
18+
rev = "837a307"
1919
default-features = false
2020
features = ["no-std"]

contracts/ckb-bitcoin-spv-type-lock/src/operations/reorg.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
2121
// - the expected client ids, which will be the new tip client id and the ids of all the cleared clients.
2222
// - the height of the old tip client.
2323
// - the id of the last client, whose blocks are all in main chain.
24+
// - the flags in SPV script args
2425
let (
2526
expected_info,
2627
expected_tip_client_id,
2728
expected_client_ids,
2829
previous_tip_height,
2930
fork_client_id,
31+
flags,
3032
) = {
3133
let (
3234
mut input_info,
3335
expected_tip_client_id,
3436
expected_client_ids,
3537
previous_tip_height,
3638
fork_client_id,
39+
flags,
3740
) = load_inputs(inputs)?;
3841
input_info.tip_client_id = expected_tip_client_id;
3942
(
@@ -42,6 +45,7 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
4245
expected_client_ids,
4346
previous_tip_height,
4447
fork_client_id,
48+
flags,
4549
)
4650
};
4751
// Checks the output info cell and the output client cells;
@@ -76,12 +80,12 @@ pub(crate) fn reorg_clients(inputs: &[usize], outputs: &[usize], script_hash: &[
7680
}
7781
};
7882

79-
expected_input_client.verify_new_client(&output_client, update)?;
83+
expected_input_client.verify_new_client(&output_client, update, flags)?;
8084

8185
Ok(())
8286
}
8387

84-
fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
88+
fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8, u8)> {
8589
let mut client_ids_with_indexes = Vec::new();
8690
let mut input_info_opt = None;
8791
for i in inputs {
@@ -135,16 +139,17 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
135139
}
136140
};
137141

138-
let clients_count: u8 = {
142+
let (clients_count, flags) = {
139143
let script = hl::load_script()?;
140144
let script_args = script.args();
141145
let script_args_slice = script_args.as_reader().raw_data();
142-
SpvTypeArgsReader::from_slice(script_args_slice)
143-
.map_err(|_| SysError::Encoding)?
144-
.clients_count()
145-
.into()
146+
let args =
147+
SpvTypeArgsReader::from_slice(script_args_slice).map_err(|_| SysError::Encoding)?;
148+
let clients_count: u8 = args.clients_count().into();
149+
let flags: u8 = args.flags().into();
150+
(clients_count, flags)
146151
};
147-
debug!("clients count: {clients_count}");
152+
debug!("clients count: {clients_count}, flags: {flags:08b}");
148153

149154
let mut client_ids = client_ids_with_indexes
150155
.into_iter()
@@ -179,6 +184,7 @@ fn load_inputs(inputs: &[usize]) -> Result<(SpvInfo, u8, Vec<u8>, u32, u8)> {
179184
expected_client_ids,
180185
tip_height,
181186
fork_client_id,
187+
flags,
182188
))
183189
}
184190

contracts/ckb-bitcoin-spv-type-lock/src/operations/update.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ pub(crate) fn update_client(
2323
// - expected output info cell base on the input info cell,
2424
// - the tip client id.
2525
// - the expected client id, which will be the next tip client id.
26-
let (expected_info, tip_client_id, expected_client_id) = {
27-
let (mut input_info, tip_client_id, expected_client_id) = load_inputs(inputs)?;
26+
let (expected_info, tip_client_id, expected_client_id, flags) = {
27+
let (mut input_info, tip_client_id, expected_client_id, flags) = load_inputs(inputs)?;
2828
input_info.tip_client_id = expected_client_id;
29-
(input_info, tip_client_id, expected_client_id)
29+
(input_info, tip_client_id, expected_client_id, flags)
3030
};
3131
// Checks the output info cell, then returns the client cell and the index of the info cell.
3232
let (output_client, output_info_index) = load_outputs(outputs, &expected_info)?;
@@ -52,12 +52,12 @@ pub(crate) fn update_client(
5252
}
5353
};
5454

55-
expected_input_client.verify_new_client(&output_client, update)?;
55+
expected_input_client.verify_new_client(&output_client, update, flags)?;
5656

5757
Ok(())
5858
}
5959

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

93-
let clients_count: u8 = {
93+
let (clients_count, flags) = {
9494
let script = hl::load_script()?;
9595
let script_args = script.args();
9696
let script_args_slice = script_args.as_reader().raw_data();
97-
SpvTypeArgsReader::from_slice(script_args_slice)
98-
.map_err(|_| SysError::Encoding)?
99-
.clients_count()
100-
.into()
97+
let args =
98+
SpvTypeArgsReader::from_slice(script_args_slice).map_err(|_| SysError::Encoding)?;
99+
let clients_count: u8 = args.clients_count().into();
100+
let flags: u8 = args.flags().into();
101+
(clients_count, flags)
101102
};
102-
debug!("clients count: {clients_count}");
103+
debug!("clients count: {clients_count}, flags: {flags:08b}");
103104

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

110-
Ok((input_info, tip_client_id, expected_client_id))
111+
Ok((input_info, tip_client_id, expected_client_id, flags))
111112
}
112113

113114
fn load_outputs(

tests/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ walkdir = "2.4"
1717
[dev-dependencies.ckb-bitcoin-spv-prover]
1818
version = "0.1.0"
1919
git = "https://github.com/ckb-cell/ckb-bitcoin-spv"
20-
rev = "2464c8f"
20+
rev = "837a307"
2121

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

0 commit comments

Comments
 (0)