Skip to content

Commit 7c3c361

Browse files
author
Antoine Riard
committed
Add RBF-bumping of preimage/timeout txn on remote HTLC outputs
Given they are only signed by us we can RBF at wish Fix tests broken by introduction of more txn broadcast (channel_monitor_network_test)
1 parent b60133c commit 7c3c361

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/ln/channelmonitor.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,10 @@ impl ChannelMonitor {
25872587
inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if script.len() == OFFERED_HTLC_SCRIPT_WEIGHT { &[InputDescriptors::RevokedOfferedHTLC] } else if script.len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { &[InputDescriptors::RevokedReceivedHTLC] } else { &[] });
25882588
amt += *amount;
25892589
},
2590-
&InputMaterial::RemoteHTLC { .. } => { return None; },
2590+
&InputMaterial::RemoteHTLC { ref preimage, ref amount, .. } => {
2591+
inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
2592+
amt += *amount;
2593+
},
25912594
&InputMaterial::LocalHTLC { .. } => { return None; }
25922595
}
25932596
}
@@ -2619,7 +2622,20 @@ impl ChannelMonitor {
26192622
bumped_tx.input[i].witness.push(script.clone().into_bytes());
26202623
log_trace!(self, "Going to broadcast bumped Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}", bumped_tx.txid(), if !is_htlc { "to_local" } else if script.len() == OFFERED_HTLC_SCRIPT_WEIGHT { "offered" } else if script.len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { "received" } else { "" }, vout, txid, new_feerate);
26212624
},
2622-
&InputMaterial::RemoteHTLC { .. } => {},
2625+
&InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount } => {
2626+
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
2627+
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[0], &script, *amount)[..]);
2628+
let sig = self.secp_ctx.sign(&sighash, &key);
2629+
bumped_tx.input[0].witness.push(sig.serialize_der().to_vec());
2630+
bumped_tx.input[0].witness[0].push(SigHashType::All as u8);
2631+
if let &Some(preimage) = preimage {
2632+
bumped_tx.input[0].witness.push(preimage.clone().0.to_vec());
2633+
} else {
2634+
bumped_tx.input[0].witness.push(vec![0]);
2635+
}
2636+
bumped_tx.input[0].witness.push(script.clone().into_bytes());
2637+
log_trace!(self, "Going to broadcast bumped Claim Transaction {} claiming remote htlc output {} from {} with new feerate {}", bumped_tx.txid(), vout, txid, new_feerate);
2638+
},
26232639
&InputMaterial::LocalHTLC { .. } => {
26242640
//TODO : Given that Local Commitment Transaction and HTLC-Timeout/HTLC-Success are counter-signed by peer, we can't
26252641
// RBF them. Need a Lightning specs change and package relay modification :

src/ln/functional_tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,8 +1718,10 @@ fn channel_monitor_network_test() {
17181718
// nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
17191719
// HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
17201720
nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
1721+
let node2_commitment_txid;
17211722
{
17221723
let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
1724+
node2_commitment_txid = node_txn[0].txid();
17231725

17241726
// Claim the payment on nodes[3], giving it knowledge of the preimage
17251727
claim_funds!(nodes[3], nodes[2], payment_preimage_1);
@@ -1753,6 +1755,16 @@ fn channel_monitor_network_test() {
17531755
nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
17541756
}
17551757

1758+
// Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer.
1759+
{
1760+
let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap();
1761+
node_txn.retain(|tx| {
1762+
if tx.input[0].previous_output.txid == node2_commitment_txid {
1763+
false
1764+
} else { true }
1765+
});
1766+
}
1767+
17561768
let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
17571769

17581770
// Claim the payment on nodes[4], giving it knowledge of the preimage

0 commit comments

Comments
 (0)