Skip to content

Commit a0ebc29

Browse files
committed
test: add more test
1 parent 0b09023 commit a0ebc29

File tree

15 files changed

+1828
-159
lines changed

15 files changed

+1828
-159
lines changed

examples/omnilock_examples.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
5151
&SignContexts::new_omnilock(
5252
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
5353
omni_cfg,
54+
ckb_sdk::unlock::OmniUnlockMode::Normal,
5455
),
5556
)?;
5657

examples/omnilock_multisig_example.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
6262
&SignContexts::new_omnilock(
6363
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
6464
omni_cfg.clone(),
65+
ckb_sdk::unlock::OmniUnlockMode::Normal,
6566
),
6667
)?;
6768
let private_key = h256!("0x4fd809631a6aa6e3bb378dd65eae5d71df895a82c91a615a1e8264741515c79c");
@@ -70,6 +71,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
7071
&SignContexts::new_omnilock(
7172
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
7273
omni_cfg,
74+
ckb_sdk::unlock::OmniUnlockMode::Normal,
7375
),
7476
)?;
7577

src/core/advanced_builders.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ impl TransactionBuilder {
232232
if witness_data.is_empty() {
233233
WitnessArgs::default()
234234
} else {
235-
WitnessArgs::from_slice(witness_data.as_ref()).unwrap()
235+
WitnessArgs::from_slice(witness_data.as_ref())
236+
.expect("WitnessArgs expected but failed to decode ")
236237
}
237238
}
238239

src/tests/transaction/omnilock.rs

Lines changed: 1664 additions & 48 deletions
Large diffs are not rendered by default.

src/tests/tx_builder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ mod cheque;
33
mod cycle;
44
mod dao;
55
mod omni_lock;
6-
mod omni_lock_util;
6+
pub mod omni_lock_util;
77
mod transfer;
88
mod udt;

src/traits/default_impls.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,6 @@ impl SecpCkbRawKeySigner {
648648
let hash160 = eos_auth(&pubkey.into(), vtype);
649649
self.keys.insert(hash160.into(), key);
650650
}
651-
652-
pub fn new_with_owner_lock(keys: Vec<secp256k1::SecretKey>, hash: H160) -> Self {
653-
let mut signer = SecpCkbRawKeySigner::default();
654-
for key in keys {
655-
signer.keys.insert(hash.clone(), key);
656-
}
657-
signer
658-
}
659651
}
660652

661653
impl Signer for SecpCkbRawKeySigner {
@@ -702,18 +694,22 @@ impl Drop for SecpCkbRawKeySigner {
702694
/// A signer use ed25519 raw key
703695
#[derive(Clone)]
704696
pub struct Ed25519Signer {
705-
key: ed25519_dalek::SigningKey,
697+
keys: HashMap<H160, ed25519_dalek::SigningKey>,
706698
}
707699

708700
impl Ed25519Signer {
709-
pub fn new(key: ed25519_dalek::SigningKey) -> Self {
710-
Self { key }
701+
pub fn new(keys: Vec<ed25519_dalek::SigningKey>) -> Self {
702+
let mut res = HashMap::with_capacity(keys.len());
703+
for key in keys {
704+
res.insert(blake160(key.verifying_key().as_bytes()), key);
705+
}
706+
Self { keys: res }
711707
}
712708
}
713709

714710
impl Signer for Ed25519Signer {
715711
fn match_id(&self, id: &[u8]) -> bool {
716-
id.len() == 20 && blake160(&self.key.verifying_key().as_bytes()[..]).as_bytes() == id
712+
id.len() == 20 && self.keys.contains_key(&H160::from_slice(id).unwrap())
717713
}
718714

719715
fn sign(
@@ -733,9 +729,10 @@ impl Signer for Ed25519Signer {
733729
)));
734730
}
735731

736-
let sig = self.key.clone().sign(message);
732+
let key = self.keys.get(&H160::from_slice(id).unwrap()).unwrap();
733+
let sig = key.clone().sign(message);
737734

738-
let verifying_key = self.key.verifying_key();
735+
let verifying_key = key.verifying_key();
739736
// 64 + 32
740737
let mut sig_plus_pubkey = sig.to_vec();
741738
sig_plus_pubkey.extend(verifying_key.as_bytes());

src/transaction/builder/mod.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use super::{handler::HandlerContexts, input::TransactionInput};
44
use crate::{
55
core::TransactionBuilder,
6-
traits::CellCollectorError,
6+
traits::{CellCollectorError, LiveCell},
77
transaction::TransactionBuilderConfiguration,
88
tx_builder::{BalanceTxCapacityError, TxBuilderError},
99
ScriptGroup, TransactionWithScriptGroups,
@@ -143,6 +143,7 @@ fn inner_build<
143143
input_iter: I,
144144
configuration: &TransactionBuilderConfiguration,
145145
contexts: &HandlerContexts,
146+
rc_cells: Vec<LiveCell>,
146147
) -> Result<TransactionWithScriptGroups, TxBuilderError> {
147148
let mut lock_groups: HashMap<Byte32, ScriptGroup> = HashMap::default();
148149
let mut type_groups: HashMap<Byte32, ScriptGroup> = HashMap::default();
@@ -159,10 +160,43 @@ fn inner_build<
159160
}
160161
}
161162

163+
let rc_len = rc_cells.len();
164+
165+
for (input_index, input) in rc_cells.into_iter().enumerate() {
166+
let input = TransactionInput::new(input, 0);
167+
tx.input(input.cell_input());
168+
tx.witness(packed::Bytes::default());
169+
170+
inputs.insert(
171+
input.live_cell.out_point.clone(),
172+
(
173+
input.live_cell.output.clone(),
174+
input.live_cell.output_data.clone(),
175+
),
176+
);
177+
178+
let previous_output = input.previous_output();
179+
let lock_script = previous_output.lock();
180+
lock_groups
181+
.entry(lock_script.calc_script_hash())
182+
.or_insert_with(|| ScriptGroup::from_lock_script(&lock_script))
183+
.input_indices
184+
.push(input_index);
185+
186+
if let Some(type_script) = previous_output.type_().to_opt() {
187+
type_groups
188+
.entry(type_script.calc_script_hash())
189+
.or_insert_with(|| ScriptGroup::from_type_script(&type_script))
190+
.input_indices
191+
.push(input_index);
192+
}
193+
}
194+
162195
// setup change output and data
163196
change_builder.init(&mut tx);
164197
// collect inputs
165-
for (input_index, input) in input_iter.enumerate() {
198+
for (mut input_index, input) in input_iter.enumerate() {
199+
input_index += rc_len;
166200
let input = input?;
167201
tx.input(input.cell_input());
168202
tx.witness(packed::Bytes::default());

src/transaction/builder/simple.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
core::TransactionBuilder,
3+
traits::LiveCell,
34
transaction::{
45
handler::HandlerContexts, input::InputIterator, TransactionBuilderConfiguration,
56
},
@@ -24,6 +25,7 @@ pub struct SimpleTransactionBuilder {
2425
input_iter: InputIterator,
2526
/// The inner transaction builder
2627
tx: TransactionBuilder,
28+
rc_cells: Vec<LiveCell>,
2729
}
2830

2931
impl SimpleTransactionBuilder {
@@ -37,9 +39,14 @@ impl SimpleTransactionBuilder {
3739
configuration,
3840
input_iter,
3941
tx: TransactionBuilder::default(),
42+
rc_cells: Vec::new(),
4043
}
4144
}
4245

46+
pub fn set_rc_cells(&mut self, rc_cells: Vec<LiveCell>) {
47+
self.rc_cells = rc_cells
48+
}
49+
4350
/// Update the change lock script.
4451
pub fn set_change_lock(&mut self, lock_script: Script) {
4552
self.change_lock = lock_script;
@@ -71,6 +78,7 @@ impl CkbTransactionBuilder for SimpleTransactionBuilder {
7178
configuration,
7279
input_iter,
7380
tx,
81+
rc_cells,
7482
} = self;
7583

7684
let change_builder = DefaultChangeBuilder {
@@ -79,6 +87,13 @@ impl CkbTransactionBuilder for SimpleTransactionBuilder {
7987
inputs: Vec::new(),
8088
};
8189

82-
inner_build(tx, change_builder, input_iter, &configuration, contexts)
90+
inner_build(
91+
tx,
92+
change_builder,
93+
input_iter,
94+
&configuration,
95+
contexts,
96+
rc_cells,
97+
)
8398
}
8499
}

src/transaction/builder/sudt.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
128128
};
129129

130130
if owner_mode {
131-
inner_build(tx, change_builder, input_iter, &configuration, contexts)
131+
inner_build(
132+
tx,
133+
change_builder,
134+
input_iter,
135+
&configuration,
136+
contexts,
137+
Default::default(),
138+
)
132139
} else {
133140
let sudt_type_script =
134141
build_sudt_type_script(configuration.network_info(), &sudt_owner_lock_script);
@@ -154,7 +161,14 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
154161
.to_le_bytes()
155162
.pack();
156163
tx.set_output_data(tx.outputs_data.len() - 1, change_output_data);
157-
return inner_build(tx, change_builder, input_iter, &configuration, contexts);
164+
return inner_build(
165+
tx,
166+
change_builder,
167+
input_iter,
168+
&configuration,
169+
contexts,
170+
Default::default(),
171+
);
158172
}
159173
}
160174

src/transaction/handler/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl HandlerContexts {
7777
}
7878
}
7979

80+
#[allow(unused_macros)]
8081
macro_rules! cell_dep {
8182
($hash: literal, $idx: expr, $dep_type: expr) => {{
8283
let out_point = ckb_types::packed::OutPoint::new_builder()
@@ -90,4 +91,5 @@ macro_rules! cell_dep {
9091
}};
9192
}
9293

94+
#[allow(unused_imports)]
9395
pub(crate) use cell_dep;

0 commit comments

Comments
 (0)