Skip to content

Commit fdf1248

Browse files
committed
Isolated channelmonitor weight unit tests and added anchor loops
1 parent 95c91ac commit fdf1248

File tree

1 file changed

+78
-63
lines changed

1 file changed

+78
-63
lines changed

lightning/src/chain/channelmonitor.rs

+78-63
Original file line numberDiff line numberDiff line change
@@ -3564,7 +3564,7 @@ mod tests {
35643564
let secp_ctx = Secp256k1::new();
35653565
let privkey = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
35663566
let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
3567-
let mut sum_actual_sigs = 0;
3567+
let mut sum_actual_sigs;
35683568

35693569
macro_rules! sign_input {
35703570
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr, $opt_anchors: expr) => {
@@ -3602,83 +3602,98 @@ mod tests {
36023602
let txid = Txid::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
36033603

36043604
// Justice tx with 1 to_holder, 2 revoked offered HTLCs, 1 revoked received HTLCs
3605-
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3606-
for i in 0..4 {
3607-
claim_tx.input.push(TxIn {
3608-
previous_output: BitcoinOutPoint {
3609-
txid,
3610-
vout: i,
3611-
},
3612-
script_sig: Script::new(),
3613-
sequence: 0xfffffffd,
3614-
witness: Vec::new(),
3605+
for &opt_anchors in [false, true].iter() {
3606+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3607+
sum_actual_sigs = 0;
3608+
for i in 0..4 {
3609+
claim_tx.input.push(TxIn {
3610+
previous_output: BitcoinOutPoint {
3611+
txid,
3612+
vout: i,
3613+
},
3614+
script_sig: Script::new(),
3615+
sequence: 0xfffffffd,
3616+
witness: Vec::new(),
3617+
});
3618+
}
3619+
claim_tx.output.push(TxOut {
3620+
script_pubkey: script_pubkey.clone(),
3621+
value: 0,
36153622
});
3616-
}
3617-
claim_tx.output.push(TxOut {
3618-
script_pubkey: script_pubkey.clone(),
3619-
value: 0,
3620-
});
3621-
let base_weight = claim_tx.get_weight();
3622-
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC];
3623-
let mut inputs_total_weight = 2; // count segwit flags
3624-
{
3625-
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3626-
for (idx, inp) in inputs_weight.iter().enumerate() {
3627-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
3628-
inputs_total_weight += inp;
3623+
let base_weight = claim_tx.get_weight();
3624+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC];
3625+
let mut inputs_total_weight = 2; // count segwit flags
3626+
{
3627+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3628+
for (idx, inp) in inputs_weight.iter().enumerate() {
3629+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
3630+
inputs_total_weight += inp;
3631+
}
36293632
}
3633+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
36303634
}
3631-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
36323635

36333636
// Claim tx with 1 offered HTLCs, 3 received HTLCs
3634-
claim_tx.input.clear();
3635-
sum_actual_sigs = 0;
3636-
for i in 0..4 {
3637+
for &opt_anchors in [false, true].iter() {
3638+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3639+
sum_actual_sigs = 0;
3640+
for i in 0..4 {
3641+
claim_tx.input.push(TxIn {
3642+
previous_output: BitcoinOutPoint {
3643+
txid,
3644+
vout: i,
3645+
},
3646+
script_sig: Script::new(),
3647+
sequence: 0xfffffffd,
3648+
witness: Vec::new(),
3649+
});
3650+
}
3651+
claim_tx.output.push(TxOut {
3652+
script_pubkey: script_pubkey.clone(),
3653+
value: 0,
3654+
});
3655+
let base_weight = claim_tx.get_weight();
3656+
let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3657+
let mut inputs_total_weight = 2; // count segwit flags
3658+
{
3659+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3660+
for (idx, inp) in inputs_weight.iter().enumerate() {
3661+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
3662+
inputs_total_weight += inp;
3663+
}
3664+
}
3665+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3666+
}
3667+
3668+
// Justice tx with 1 revoked HTLC-Success tx output
3669+
for &opt_anchors in [false, true].iter() {
3670+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3671+
sum_actual_sigs = 0;
36373672
claim_tx.input.push(TxIn {
36383673
previous_output: BitcoinOutPoint {
36393674
txid,
3640-
vout: i,
3675+
vout: 0,
36413676
},
36423677
script_sig: Script::new(),
36433678
sequence: 0xfffffffd,
36443679
witness: Vec::new(),
36453680
});
3646-
}
3647-
let base_weight = claim_tx.get_weight();
3648-
let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3649-
let mut inputs_total_weight = 2; // count segwit flags
3650-
{
3651-
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3652-
for (idx, inp) in inputs_weight.iter().enumerate() {
3653-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
3654-
inputs_total_weight += inp;
3655-
}
3656-
}
3657-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3658-
3659-
// Justice tx with 1 revoked HTLC-Success tx output
3660-
claim_tx.input.clear();
3661-
sum_actual_sigs = 0;
3662-
claim_tx.input.push(TxIn {
3663-
previous_output: BitcoinOutPoint {
3664-
txid,
3665-
vout: 0,
3666-
},
3667-
script_sig: Script::new(),
3668-
sequence: 0xfffffffd,
3669-
witness: Vec::new(),
3670-
});
3671-
let base_weight = claim_tx.get_weight();
3672-
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT];
3673-
let mut inputs_total_weight = 2; // count segwit flags
3674-
{
3675-
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3676-
for (idx, inp) in inputs_weight.iter().enumerate() {
3677-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
3678-
inputs_total_weight += inp;
3681+
claim_tx.output.push(TxOut {
3682+
script_pubkey: script_pubkey.clone(),
3683+
value: 0,
3684+
});
3685+
let base_weight = claim_tx.get_weight();
3686+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT];
3687+
let mut inputs_total_weight = 2; // count segwit flags
3688+
{
3689+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3690+
for (idx, inp) in inputs_weight.iter().enumerate() {
3691+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
3692+
inputs_total_weight += inp;
3693+
}
36793694
}
3695+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
36803696
}
3681-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
36823697
}
36833698

36843699
// Further testing is done in the ChannelManager integration tests.

0 commit comments

Comments
 (0)