Skip to content

Commit e9434de

Browse files
committed
log_trace information about commitment txn as they're being built
1 parent a3c9e6b commit e9434de

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/ln/channel.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ impl Channel {
774774
let mut local_htlc_total_msat = 0;
775775
let mut value_to_self_msat_offset = 0;
776776

777+
log_trace!(self, "Building commitment transaction number {} for {}, generated by {} with fee {}...", commitment_number, if local { "us" } else { "remote" }, if generated_by_local { "us" } else { "remote" }, feerate_per_kw);
778+
777779
macro_rules! get_htlc_in_commitment {
778780
($htlc: expr, $offered: expr) => {
779781
HTLCOutputInCommitment {
@@ -791,21 +793,25 @@ impl Channel {
791793
if $outbound == local { // "offered HTLC output"
792794
let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
793795
if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
796+
log_trace!(self, " ...including {} HTLC {} with value {}", if $outbound { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
794797
txouts.push((TxOut {
795798
script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
796799
value: $htlc.amount_msat / 1000
797800
}, Some((htlc_in_tx, $source))));
798801
} else {
802+
log_trace!(self, " ...not including {} HTLC {} with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
799803
included_dust_htlcs.push((htlc_in_tx, $source));
800804
}
801805
} else {
802806
let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
803807
if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
808+
log_trace!(self, " ...including {} HTLC {} with value {}", if $outbound { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
804809
txouts.push((TxOut { // "received HTLC output"
805810
script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
806811
value: $htlc.amount_msat / 1000
807812
}, Some((htlc_in_tx, $source))));
808813
} else {
814+
log_trace!(self, " ...not including {} HTLC {} with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
809815
included_dust_htlcs.push((htlc_in_tx, $source));
810816
}
811817
}
@@ -825,6 +831,13 @@ impl Channel {
825831
add_htlc_output!(htlc, false, None);
826832
remote_htlc_total_msat += htlc.amount_msat;
827833
} else {
834+
log_trace!(self, " ...not including inbound HTLC {} with value {} due to state ({})", log_bytes!(htlc.payment_hash.0), htlc.amount_msat, match htlc.state {
835+
InboundHTLCState::RemoteAnnounced(_) => "RemoteAnnounced",
836+
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) => "AwaitingRemoteRevokeToAnnounce",
837+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => "AwaitingAnnouncedRemoteRevoke",
838+
InboundHTLCState::Committed => "Committed",
839+
InboundHTLCState::LocalRemoved(_) => "LocalRemoved",
840+
});
828841
match &htlc.state {
829842
&InboundHTLCState::LocalRemoved(ref reason) => {
830843
if generated_by_local {
@@ -851,6 +864,13 @@ impl Channel {
851864
add_htlc_output!(htlc, true, Some(&htlc.source));
852865
local_htlc_total_msat += htlc.amount_msat;
853866
} else {
867+
log_trace!(self, " ...not including outbound HTLC {} with value {} due to state ({})", log_bytes!(htlc.payment_hash.0), htlc.amount_msat, match htlc.state {
868+
OutboundHTLCState::LocalAnnounced(_) => "LocalAnnounced",
869+
OutboundHTLCState::Committed => "Committed",
870+
OutboundHTLCState::RemoteRemoved => "RemoteRemoved",
871+
OutboundHTLCState::AwaitingRemoteRevokeToRemove => "AwaitingRemoteRevokeToRemove",
872+
OutboundHTLCState::AwaitingRemovedRemoteRevoke => "AwaitingRemovedRemoteRevoke",
873+
});
854874
match htlc.state {
855875
OutboundHTLCState::AwaitingRemoteRevokeToRemove|OutboundHTLCState::AwaitingRemovedRemoteRevoke => {
856876
if htlc.fail_reason.is_none() {
@@ -1695,6 +1715,7 @@ impl Channel {
16951715
};
16961716
let local_commitment_txid = local_commitment_tx.0.txid();
16971717
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
1718+
log_trace!(self, "Checking commitment tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.signature[..]), log_bytes!(self.their_funding_pubkey.unwrap().serialize()), encode::serialize_hex(&local_commitment_tx.0), encode::serialize_hex(&funding_script));
16981719
secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
16991720

17001721
//If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
@@ -1720,6 +1741,7 @@ impl Channel {
17201741
if let Some(_) = htlc.transaction_output_index {
17211742
let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
17221743
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
1744+
log_trace!(self, "Checking HTLC tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.htlc_signatures[idx][..]), log_bytes!(local_keys.b_htlc_key.serialize()), encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript));
17231745
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
17241746
secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
17251747
let htlc_sig = if htlc.offered {
@@ -3285,6 +3307,7 @@ impl Channel {
32853307
let remote_commitment_txid = remote_commitment_tx.0.txid();
32863308
let remote_sighash = Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
32873309
let our_sig = self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key);
3310+
log_trace!(self, "Signing commitment tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&remote_commitment_tx.0), encode::serialize_hex(&funding_script), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize()), log_bytes!(our_sig[..]));
32883311

32893312
let mut htlc_sigs = Vec::with_capacity(remote_commitment_tx.1);
32903313
for &(ref htlc, _) in remote_commitment_tx.2.iter() {
@@ -3294,6 +3317,7 @@ impl Channel {
32943317
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
32953318
let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
32963319
htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
3320+
log_trace!(self, "Signing HTLC tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key).serialize()), log_bytes!(htlc_sigs.last().unwrap()[..]));
32973321
}
32983322
}
32993323

0 commit comments

Comments
 (0)