Skip to content

Commit f124afa

Browse files
committed
Isolated channelmonitor weight unit tests and macro-ed for anchors
1 parent 95c91ac commit f124afa

File tree

1 file changed

+79
-63
lines changed

1 file changed

+79
-63
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 79 additions & 63 deletions
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,99 @@ 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(),
3615-
});
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
3605+
for opt_anchors in &[false, true]
36243606
{
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;
3607+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3608+
sum_actual_sigs = 0;
3609+
for i in 0..4 {
3610+
claim_tx.input.push(TxIn {
3611+
previous_output: BitcoinOutPoint {
3612+
txid,
3613+
vout: i,
3614+
},
3615+
script_sig: Script::new(),
3616+
sequence: 0xfffffffd,
3617+
witness: Vec::new(),
3618+
});
36293619
}
3620+
claim_tx.output.push(TxOut {
3621+
script_pubkey: script_pubkey.clone(),
3622+
value: 0,
3623+
});
3624+
let base_weight = claim_tx.get_weight();
3625+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC];
3626+
let mut inputs_total_weight = 2; // count segwit flags
3627+
{
3628+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3629+
for (idx, inp) in inputs_weight.iter().enumerate() {
3630+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, *opt_anchors);
3631+
inputs_total_weight += inp;
3632+
}
3633+
}
3634+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
36303635
}
3631-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
36323636

36333637
// 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 {
3638+
for opt_anchors in &[false, true] {
3639+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3640+
sum_actual_sigs = 0;
3641+
for i in 0..4 {
3642+
claim_tx.input.push(TxIn {
3643+
previous_output: BitcoinOutPoint {
3644+
txid,
3645+
vout: i,
3646+
},
3647+
script_sig: Script::new(),
3648+
sequence: 0xfffffffd,
3649+
witness: Vec::new(),
3650+
});
3651+
}
3652+
claim_tx.output.push(TxOut {
3653+
script_pubkey: script_pubkey.clone(),
3654+
value: 0,
3655+
});
3656+
let base_weight = claim_tx.get_weight();
3657+
let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3658+
let mut inputs_total_weight = 2; // count segwit flags
3659+
{
3660+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3661+
for (idx, inp) in inputs_weight.iter().enumerate() {
3662+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, *opt_anchors);
3663+
inputs_total_weight += inp;
3664+
}
3665+
}
3666+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3667+
}
3668+
3669+
// Justice tx with 1 revoked HTLC-Success tx output
3670+
for opt_anchors in &[false, true] {
3671+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3672+
sum_actual_sigs = 0;
36373673
claim_tx.input.push(TxIn {
36383674
previous_output: BitcoinOutPoint {
36393675
txid,
3640-
vout: i,
3676+
vout: 0,
36413677
},
36423678
script_sig: Script::new(),
36433679
sequence: 0xfffffffd,
36443680
witness: Vec::new(),
36453681
});
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;
3682+
claim_tx.output.push(TxOut {
3683+
script_pubkey: script_pubkey.clone(),
3684+
value: 0,
3685+
});
3686+
let base_weight = claim_tx.get_weight();
3687+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT];
3688+
let mut inputs_total_weight = 2; // count segwit flags
3689+
{
3690+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3691+
for (idx, inp) in inputs_weight.iter().enumerate() {
3692+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, *opt_anchors);
3693+
inputs_total_weight += inp;
3694+
}
36793695
}
3696+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
36803697
}
3681-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
36823698
}
36833699

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

0 commit comments

Comments
 (0)