Skip to content

Commit 249ed0e

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

File tree

1 file changed

+101
-68
lines changed

1 file changed

+101
-68
lines changed

lightning/src/chain/channelmonitor.rs

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

36333642
// 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-
claim_tx.input.push(TxIn {
3638-
previous_output: BitcoinOutPoint {
3639-
txid,
3640-
vout: i,
3641-
},
3642-
script_sig: Script::new(),
3643-
sequence: 0xfffffffd,
3644-
witness: Vec::new(),
3645-
});
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;
3643+
macro_rules! claim_tx_with_htlcs {
3644+
($opt_anchors: expr) => {
3645+
{
3646+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3647+
sum_actual_sigs = 0;
3648+
for i in 0..4 {
3649+
claim_tx.input.push(TxIn {
3650+
previous_output: BitcoinOutPoint {
3651+
txid,
3652+
vout: i,
3653+
},
3654+
script_sig: Script::new(),
3655+
sequence: 0xfffffffd,
3656+
witness: Vec::new(),
3657+
});
3658+
}
3659+
claim_tx.output.push(TxOut {
3660+
script_pubkey: script_pubkey.clone(),
3661+
value: 0,
3662+
});
3663+
let base_weight = claim_tx.get_weight();
3664+
let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3665+
let mut inputs_total_weight = 2; // count segwit flags
3666+
{
3667+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3668+
for (idx, inp) in inputs_weight.iter().enumerate() {
3669+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, $opt_anchors);
3670+
inputs_total_weight += inp;
3671+
}
3672+
}
3673+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3674+
}
36553675
}
36563676
}
3657-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3677+
claim_tx_with_htlcs!(false);
3678+
claim_tx_with_htlcs!(true);
36583679

36593680
// 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+
macro_rules! justice_tx_with_revoked_htlc_success {
3682+
($opt_anchors: expr) => {
3683+
{
3684+
let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
3685+
sum_actual_sigs = 0;
3686+
claim_tx.input.push(TxIn {
3687+
previous_output: BitcoinOutPoint {
3688+
txid,
3689+
vout: 0,
3690+
},
3691+
script_sig: Script::new(),
3692+
sequence: 0xfffffffd,
3693+
witness: Vec::new(),
3694+
});
3695+
claim_tx.output.push(TxOut {
3696+
script_pubkey: script_pubkey.clone(),
3697+
value: 0,
3698+
});
3699+
let base_weight = claim_tx.get_weight();
3700+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT];
3701+
let mut inputs_total_weight = 2; // count segwit flags
3702+
{
3703+
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3704+
for (idx, inp) in inputs_weight.iter().enumerate() {
3705+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, $opt_anchors);
3706+
inputs_total_weight += inp;
3707+
}
3708+
}
3709+
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
3710+
}
36793711
}
36803712
}
3681-
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
3713+
justice_tx_with_revoked_htlc_success!(false);
3714+
justice_tx_with_revoked_htlc_success!(true);
36823715
}
36833716

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

0 commit comments

Comments
 (0)