Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Track confirmation block hash and return via `Confirm::get_relevant_txids` by tnull · Pull Request #1796 · lightningdevkit/rust-lightning · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
//! servicing [`ChannelMonitor`] updates from the client.

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use crate::chain;
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
Expand DownExpand Up@@ -544,7 +544,7 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids = Vec::new();
let monitor_states = self.monitors.read().unwrap();
for monitor_state in monitor_states.values() {
Expand Down
77 changes: 45 additions & 32 deletions lightning/src/chain/channelmonitor.rs

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lightning/src/chain/mod.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,7 +171,8 @@ pub trait Confirm {
/// if they become available at the same time.
fn best_block_updated(&self, header: &BlockHeader, height: u32);

/// Returns transactions that should be monitored for reorganization out of the chain.
/// Returns transactions that should be monitored for reorganization out of the chain along
/// with the hash of the block as part of which had been previously confirmed.
Comment thread
tnull marked this conversation as resolved.
///
/// Will include any transactions passed to [`transactions_confirmed`] that have insufficient
/// confirmations to be safe from a chain reorganization. Will not include any transactions
Expand All@@ -180,11 +181,13 @@ pub trait Confirm {
/// May be called to determine the subset of transactions that must still be monitored for
/// reorganization. Will be idempotent between calls but may change as a result of calls to the
/// other interface methods. Thus, this is useful to determine which transactions may need to be
/// given to [`transaction_unconfirmed`].
/// given to [`transaction_unconfirmed`]. If any of the returned transactions are confirmed in
/// a block other than the one with the given hash, they need to be unconfirmed and reconfirmed
/// via [`transaction_unconfirmed`] and [`transactions_confirmed`], respectively.
///
/// [`transactions_confirmed`]: Self::transactions_confirmed
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
fn get_relevant_txids(&self) -> Vec<Txid>;
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)>;
}

/// An enum representing the status of a channel monitor update persistence.
Expand Down
61 changes: 45 additions & 16 deletions lightning/src/chain/onchaintx.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::Script;

use bitcoin::hash_types::Txid;
use bitcoin::hash_types::{Txid, BlockHash};

use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
use bitcoin::secp256k1;
Expand DownExpand Up@@ -58,6 +58,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
struct OnchainEventEntry {
txid: Txid,
height: u32,
block_hash: Option<BlockHash>, // Added as optional, will be filled in for any entry generated on 0.0.113 or after
event: OnchainEvent,
}

Expand DownExpand Up@@ -92,6 +93,7 @@ impl Writeable for OnchainEventEntry {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.txid, required),
(1, self.block_hash, option),
(2, self.height, required),
(4, self.event, required),
});
Expand All@@ -103,14 +105,16 @@ impl MaybeReadable for OnchainEventEntry {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
let mut txid = Txid::all_zeros();
let mut height = 0;
let mut block_hash = None;
let mut event = None;
read_tlv_fields!(reader, {
(0, txid, required),
(1, block_hash, option),
(2, height, required),
(4, event, ignorable),
});
if let Some(ev) = event {
Ok(Some(Self { txid, height, event: ev }))
Ok(Some(Self { txid, height, block_hash, event: ev }))
} else {
Ok(None)
}
Expand DownExpand Up@@ -543,17 +547,22 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
/// if we receive a preimage after force-close.
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
/// Together with `update_claims_view_from_matched_txn` this used to be named
/// `block_connected`, but it is now also used for claiming an HTLC output if we receive a
/// preimage after force-close.
///
/// `conf_height` represents the height at which the request was generated. This
/// does not need to equal the current blockchain tip height, which should be provided via
/// `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {} and {} claim requests", cur_height, txn_matched.len(), conf_height, requests.len());
log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
let mut preprocessed_requests = Vec::with_capacity(requests.len());
let mut aggregated_request = None;

Expand DownExpand Up@@ -633,7 +642,25 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.pending_claim_requests.insert(txid, req);
}
}
}

/// Upon channelmonitor.block_connected(..) or upon provision of a preimage on the forward link
/// for this channel, provide new relevant on-chain transactions and/or new claim requests.
/// Together with `update_claims_view_from_requests` this used to be named `block_connected`,
/// but it is now also used for claiming an HTLC output if we receive a preimage after force-close.
///
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
) where
B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
L::Target: Logger,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
Expand DownExpand Up@@ -661,6 +688,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -701,6 +729,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
let entry = OnchainEventEntry {
txid: tx.txid(),
height: conf_height,
block_hash: Some(conf_hash),
event: OnchainEvent::ContentiousOutpoint { package },
};
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
Expand DownExpand Up@@ -860,12 +889,12 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
self.claimable_outpoints.get(outpoint).is_some()
}

pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
pub(crate) fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let mut txids: Vec<(Txid, Option<BlockHash>)> = self.onchain_events_awaiting_threshold_conf
.iter()
.map(|entry| entry.txid)
.map(|entry| (entry.txid, entry.block_hash))
.collect();
txids.sort_unstable();
txids.sort_unstable_by_key(|(txid, _)| *txid);
txids.dedup();
txids
}
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4520,6 +4520,11 @@ impl<Signer: Sign> Channel<Signer> {
self.channel_transaction_parameters.funding_outpoint
}

/// Returns the block hash in which our funding transaction was confirmed.
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
self.funding_tx_confirmed_in
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5853,12 +5853,12 @@ where
});
}

fn get_relevant_txids(&self) -> Vec<Txid> {
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
let channel_state = self.channel_state.lock().unwrap();
let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len());
for chan in channel_state.by_id.values() {
if let Some(funding_txo) = chan.get_funding_txo() {
res.push(funding_txo.txid);
if let (Some(funding_txo), block_hash) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
res.push((funding_txo.txid, block_hash));
}
}
res
Expand Down
27 changes: 23 additions & 4 deletions lightning/src/ln/reorg_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,6 +271,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
*nodes[0].connect_style.borrow_mut() = connect_style;

let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1);
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());

let channel_state = nodes[0].node.channel_state.lock().unwrap();
Expand All@@ -281,15 +282,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand DownExpand Up@@ -350,15 +360,24 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
if reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
assert_eq!(relevant_txids.len(), 1);
let block_hash_opt = relevant_txids[0].1;
let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
assert_eq!(block_hash_opt, Some(expected_hash));
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
disconnect_blocks(&nodes[0], 1);
} else {
disconnect_all_blocks(&nodes[0]);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
assert_eq!(relevant_txids.len(), 0);

handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
check_added_monitors!(nodes[1], 1);
{
Expand Down