Skip to content

Commit 1d4f9c8

Browse files
authored
Merge pull request #936 from TheBlueMatt/2021-05-spendable-event-locktime-delay
Delay DelayedPaymentOutput spendable events until the CSV delay
2 parents 0e10141 + e60ccbb commit 1d4f9c8

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mod tests {
180180
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager};
181181
use lightning::chain::transaction::OutPoint;
182182
use lightning::get_event_msg;
183-
use lightning::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, SimpleArcChannelManager};
183+
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, BestBlock, ChainParameters, ChannelManager, SimpleArcChannelManager};
184184
use lightning::ln::features::InitFeatures;
185185
use lightning::ln::msgs::ChannelMessageHandler;
186186
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};
@@ -303,8 +303,8 @@ mod tests {
303303
}}
304304
}
305305

306-
fn confirm_transaction(node: &mut Node, tx: &Transaction) {
307-
for i in 1..=ANTI_REORG_DELAY {
306+
fn confirm_transaction_depth(node: &mut Node, tx: &Transaction, depth: u32) {
307+
for i in 1..=depth {
308308
let prev_blockhash = node.best_block.block_hash();
309309
let height = node.best_block.height() + 1;
310310
let header = BlockHeader { version: 0x20000000, prev_blockhash, merkle_root: Default::default(), time: height, bits: 42, nonce: 42 };
@@ -315,14 +315,17 @@ mod tests {
315315
node.node.transactions_confirmed(&header, &txdata, height);
316316
node.chain_monitor.transactions_confirmed(&header, &txdata, height);
317317
},
318-
ANTI_REORG_DELAY => {
318+
x if x == depth => {
319319
node.node.best_block_updated(&header, height);
320320
node.chain_monitor.best_block_updated(&header, height);
321321
},
322322
_ => {},
323323
}
324324
}
325325
}
326+
fn confirm_transaction(node: &mut Node, tx: &Transaction) {
327+
confirm_transaction_depth(node, tx, ANTI_REORG_DELAY);
328+
}
326329

327330
#[test]
328331
fn test_background_processor() {
@@ -454,7 +457,7 @@ mod tests {
454457
// Force close the channel and check that the SpendableOutputs event was handled.
455458
nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
456459
let commitment_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap();
457-
confirm_transaction(&mut nodes[0], &commitment_tx);
460+
confirm_transaction_depth(&mut nodes[0], &commitment_tx, BREAKDOWN_TIMEOUT as u32);
458461
let event = receiver
459462
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
460463
.expect("SpendableOutputs not handled within deadline");

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,15 @@ struct OnchainEventEntry {
362362

363363
impl OnchainEventEntry {
364364
fn confirmation_threshold(&self) -> u32 {
365-
self.height + ANTI_REORG_DELAY - 1
365+
let mut conf_threshold = self.height + ANTI_REORG_DELAY - 1;
366+
if let OnchainEvent::MaturingOutput {
367+
descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor)
368+
} = self.event {
369+
// A CSV'd transaction is confirmable in block (input height) + CSV delay, which means
370+
// it's broadcastable when we see the previous block.
371+
conf_threshold = cmp::max(conf_threshold, self.height + descriptor.to_self_delay as u32 - 1);
372+
}
373+
conf_threshold
366374
}
367375

368376
fn has_reached_confirmation_threshold(&self, height: u32) -> bool {

lightning/src/ln/functional_tests.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,11 +4875,13 @@ fn test_claim_sizeable_push_msat() {
48754875
assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
48764876

48774877
mine_transaction(&nodes[1], &node_txn[0]);
4878-
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4878+
connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
48794879

48804880
let spend_txn = check_spendable_outputs!(nodes[1], 1, node_cfgs[1].keys_manager, 100000);
48814881
assert_eq!(spend_txn.len(), 1);
4882+
assert_eq!(spend_txn[0].input.len(), 1);
48824883
check_spends!(spend_txn[0], node_txn[0]);
4884+
assert_eq!(spend_txn[0].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
48834885
}
48844886

48854887
#[test]
@@ -5507,12 +5509,14 @@ fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
55075509
};
55085510

55095511
mine_transaction(&nodes[1], &node_tx);
5510-
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5512+
connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
55115513

55125514
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
55135515
let spend_txn = check_spendable_outputs!(nodes[1], 1, node_cfgs[1].keys_manager, 100000);
55145516
assert_eq!(spend_txn.len(), 1);
5517+
assert_eq!(spend_txn[0].input.len(), 1);
55155518
check_spends!(spend_txn[0], node_tx);
5519+
assert_eq!(spend_txn[0].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
55165520
}
55175521

55185522
fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
@@ -5802,15 +5806,20 @@ fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
58025806
};
58035807

58045808
mine_transaction(&nodes[0], &htlc_timeout);
5805-
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5809+
connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
58065810
expect_payment_failed!(nodes[0], our_payment_hash, true);
58075811

58085812
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
58095813
let spend_txn = check_spendable_outputs!(nodes[0], 1, node_cfgs[0].keys_manager, 100000);
58105814
assert_eq!(spend_txn.len(), 3);
58115815
check_spends!(spend_txn[0], local_txn[0]);
5816+
assert_eq!(spend_txn[1].input.len(), 1);
58125817
check_spends!(spend_txn[1], htlc_timeout);
5818+
assert_eq!(spend_txn[1].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
5819+
assert_eq!(spend_txn[2].input.len(), 2);
58135820
check_spends!(spend_txn[2], local_txn[0], htlc_timeout);
5821+
assert!(spend_txn[2].input[0].sequence == BREAKDOWN_TIMEOUT as u32 ||
5822+
spend_txn[2].input[1].sequence == BREAKDOWN_TIMEOUT as u32);
58145823
}
58155824

58165825
#[test]
@@ -5877,16 +5886,21 @@ fn test_key_derivation_params() {
58775886
};
58785887

58795888
mine_transaction(&nodes[0], &htlc_timeout);
5880-
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5889+
connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
58815890
expect_payment_failed!(nodes[0], our_payment_hash, true);
58825891

58835892
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
58845893
let new_keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
58855894
let spend_txn = check_spendable_outputs!(nodes[0], 1, new_keys_manager, 100000);
58865895
assert_eq!(spend_txn.len(), 3);
58875896
check_spends!(spend_txn[0], local_txn_1[0]);
5897+
assert_eq!(spend_txn[1].input.len(), 1);
58885898
check_spends!(spend_txn[1], htlc_timeout);
5899+
assert_eq!(spend_txn[1].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
5900+
assert_eq!(spend_txn[2].input.len(), 2);
58895901
check_spends!(spend_txn[2], local_txn_1[0], htlc_timeout);
5902+
assert!(spend_txn[2].input[0].sequence == BREAKDOWN_TIMEOUT as u32 ||
5903+
spend_txn[2].input[1].sequence == BREAKDOWN_TIMEOUT as u32);
58905904
}
58915905

58925906
#[test]

lightning/src/util/events.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ pub enum Event {
126126
/// now + 5*time_forwardable).
127127
time_forwardable: Duration,
128128
},
129-
/// Used to indicate that an output was generated on-chain which you should know how to spend.
129+
/// Used to indicate that an output which you should know how to spend was confirmed on chain
130+
/// and is now spendable.
130131
/// Such an output will *not* ever be spent by rust-lightning, and are not at risk of your
131132
/// counterparty spending them due to some kind of timeout. Thus, you need to store them
132133
/// somewhere and spend them when you create on-chain transactions.

0 commit comments

Comments
 (0)