Skip to content

Commit c8ff321

Browse files
committed
Return confirmation height via Confirm::get_relevant_txids
We previously included the block hash, but it's also useful to include the height under which we expect the respective transaction to be confirmed.
1 parent 70ea110 commit c8ff321

File tree

10 files changed

+45
-27
lines changed

10 files changed

+45
-27
lines changed

lightning-transaction-sync/src/esplora.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ where
316316
let relevant_txids = confirmables
317317
.iter()
318318
.flat_map(|c| c.get_relevant_txids())
319-
.collect::<HashSet<(Txid, Option<BlockHash>)>>();
319+
.collect::<HashSet<(Txid, u32, Option<BlockHash>)>>();
320320

321321
let mut unconfirmed_txs = Vec::new();
322322

323-
for (txid, block_hash_opt) in relevant_txids {
323+
for (txid, _conf_height, block_hash_opt) in relevant_txids {
324324
if let Some(block_hash) = block_hash_opt {
325325
let block_status = maybe_await!(self.client.get_block_status(&block_hash))?;
326326
if block_status.in_best_chain {

lightning-transaction-sync/tests/integration_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ impl Confirm for TestConfirmable {
143143
self.events.lock().unwrap().push(TestConfirmableEvent::BestBlockUpdated(block_hash, height));
144144
}
145145

146-
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
147-
self.confirmed_txs.lock().unwrap().iter().map(|(&txid, (hash, _))| (txid, Some(*hash))).collect::<Vec<_>>()
146+
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
147+
self.confirmed_txs.lock().unwrap().iter().map(|(&txid, (hash, height))| (txid, *height, Some(*hash))).collect::<Vec<_>>()
148148
}
149149
}
150150

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,15 @@ where
689689
});
690690
}
691691

692-
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
692+
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
693693
let mut txids = Vec::new();
694694
let monitor_states = self.monitors.read().unwrap();
695695
for monitor_state in monitor_states.values() {
696696
txids.append(&mut monitor_state.monitor.get_relevant_txids());
697697
}
698698

699-
txids.sort_unstable();
700-
txids.dedup();
699+
txids.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)));
700+
txids.dedup_by_key(|(txid, _, _)| *txid);
701701
txids
702702
}
703703
}

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,15 +1634,15 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
16341634
}
16351635

16361636
/// Returns the set of txids that should be monitored for re-organization out of the chain.
1637-
pub fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
1637+
pub fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
16381638
let inner = self.inner.lock().unwrap();
1639-
let mut txids: Vec<(Txid, Option<BlockHash>)> = inner.onchain_events_awaiting_threshold_conf
1639+
let mut txids: Vec<(Txid, u32, Option<BlockHash>)> = inner.onchain_events_awaiting_threshold_conf
16401640
.iter()
1641-
.map(|entry| (entry.txid, entry.block_hash))
1641+
.map(|entry| (entry.txid, entry.height, entry.block_hash))
16421642
.chain(inner.onchain_tx_handler.get_relevant_txids().into_iter())
16431643
.collect();
1644-
txids.sort_unstable();
1645-
txids.dedup();
1644+
txids.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)));
1645+
txids.dedup_by_key(|(txid, _, _)| *txid);
16461646
txids
16471647
}
16481648

@@ -4171,7 +4171,7 @@ where
41714171
self.0.best_block_updated(header, height, &*self.1, &*self.2, &*self.3);
41724172
}
41734173

4174-
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
4174+
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
41754175
self.0.get_relevant_txids()
41764176
}
41774177
}

lightning/src/chain/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub trait Confirm {
152152
/// blocks.
153153
fn best_block_updated(&self, header: &Header, height: u32);
154154
/// Returns transactions that must be monitored for reorganization out of the chain along
155-
/// with the hash of the block as part of which it had been previously confirmed.
155+
/// with the height and the hash of the block as part of which it had been previously confirmed.
156156
///
157157
/// Note that the returned `Option<BlockHash>` might be `None` for channels created with LDK
158158
/// 0.0.112 and prior, in which case you need to manually track previous confirmations.
@@ -167,12 +167,12 @@ pub trait Confirm {
167167
/// given to [`transaction_unconfirmed`].
168168
///
169169
/// If any of the returned transactions are confirmed in a block other than the one with the
170-
/// given hash, they need to be unconfirmed and reconfirmed via [`transaction_unconfirmed`] and
171-
/// [`transactions_confirmed`], respectively.
170+
/// given hash at the given height, they need to be unconfirmed and reconfirmed via
171+
/// [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
172172
///
173173
/// [`transactions_confirmed`]: Self::transactions_confirmed
174174
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
175-
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
175+
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)>;
176176
}
177177

178178
/// An enum representing the status of a channel monitor update persistence.

lightning/src/chain/onchaintx.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,13 +1076,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
10761076
self.claimable_outpoints.get(outpoint).is_some()
10771077
}
10781078

1079-
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
1080-
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
1079+
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
1080+
let mut txids: Vec<(Txid, u32, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
10811081
.iter()
1082-
.map(|entry| (entry.txid, entry.block_hash))
1082+
.map(|entry| (entry.txid, entry.height, entry.block_hash))
10831083
.collect();
1084-
txids.sort_unstable_by_key(|(txid, _)| *txid);
1085-
txids.dedup();
1084+
txids.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)));
1085+
txids.dedup_by_key(|(txid, _, _)| *txid);
10861086
txids
10871087
}
10881088

lightning/src/ln/channel.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,16 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
11111111
self.channel_transaction_parameters.funding_outpoint
11121112
}
11131113

1114+
/// Returns the height in which our funding transaction was confirmed.
1115+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
1116+
let conf_height = self.funding_tx_confirmation_height;
1117+
if conf_height > 0 {
1118+
Some(conf_height)
1119+
} else {
1120+
None
1121+
}
1122+
}
1123+
11141124
/// Returns the block hash in which our funding transaction was confirmed.
11151125
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
11161126
self.funding_tx_confirmed_in

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8336,14 +8336,17 @@ where
83368336
});
83378337
}
83388338

8339-
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
8339+
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
83408340
let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
83418341
for (_cp_id, peer_state_mutex) in self.per_peer_state.read().unwrap().iter() {
83428342
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
83438343
let peer_state = &mut *peer_state_lock;
83448344
for chan in peer_state.channel_by_id.values().filter_map(|phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }) {
8345-
if let (Some(funding_txo), Some(block_hash)) = (chan.context.get_funding_txo(), chan.context.get_funding_tx_confirmed_in()) {
8346-
res.push((funding_txo.txid, Some(block_hash)));
8345+
let txid_opt = chan.context.get_funding_txo();
8346+
let height_opt = chan.context.get_funding_tx_confirmation_height();
8347+
let hash_opt = chan.context.get_funding_tx_confirmed_in();
8348+
if let (Some(funding_txo), Some(conf_height), Some(block_hash)) = (txid_opt, height_opt, hash_opt) {
8349+
res.push((funding_txo.txid, conf_height, Some(block_hash)));
83478350
}
83488351
}
83498352
}

lightning/src/ln/reorg_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
270270
if use_funding_unconfirmed {
271271
let relevant_txids = nodes[0].node.get_relevant_txids();
272272
assert_eq!(relevant_txids.len(), 1);
273-
let block_hash_opt = relevant_txids[0].1;
273+
let block_hash_opt = relevant_txids[0].2;
274274
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
275+
assert_eq!(relevant_txids[0].1, chan_conf_height);
275276
assert_eq!(block_hash_opt, Some(expected_hash));
276277
let txid = relevant_txids[0].0;
277278
assert_eq!(txid, chan.3.txid());
@@ -315,8 +316,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
315316
if use_funding_unconfirmed {
316317
let relevant_txids = nodes[0].node.get_relevant_txids();
317318
assert_eq!(relevant_txids.len(), 1);
318-
let block_hash_opt = relevant_txids[0].1;
319+
let block_hash_opt = relevant_txids[0].2;
319320
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
321+
assert_eq!(chan_conf_height, relevant_txids[0].1);
320322
assert_eq!(block_hash_opt, Some(expected_hash));
321323
let txid = relevant_txids[0].0;
322324
assert_eq!(txid, chan.3.txid());

pending_changelog/electrum.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## API Updates
2+
3+
- The `Confirm::get_relevant_txids()` call now also returns the height under which LDK expects the respective transaction to be confirmed.

0 commit comments

Comments
 (0)