-
Notifications
You must be signed in to change notification settings - Fork 405
Allow retrieval of SpendableOutputDescriptors from relevant transactions #2609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1669,6 +1669,33 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> { | |
current_height, &broadcaster, &fee_estimator, &logger, | ||
); | ||
} | ||
|
||
/// Returns the descriptors for relevant outputs (i.e., those that we can spend) within the | ||
/// transaction if they exist and the transaction has at least [`ANTI_REORG_DELAY`] | ||
/// confirmations. | ||
/// | ||
/// Descriptors returned by this method are primarily exposed via [`Event::SpendableOutputs`] | ||
/// once they are no longer under reorg risk. This method serves as a way to retrieve these | ||
/// descriptors at a later time, either for historical purposes, or to replay any | ||
/// missed/unhandled descriptors. For the purpose of gathering historical records, if the | ||
/// channel close has fully resolved (i.e., [`ChannelMonitor::get_claimable_balances`] returns | ||
/// an empty set), you can retrieve all spendable outputs by providing all descendant spending | ||
/// transactions starting from the channel's funding or closing transaction that have at least | ||
/// [`ANTI_REORG_DELAY`] confirmations. | ||
/// | ||
/// `tx` is a transaction we'll scan the outputs of. Any transaction can be provided. If any | ||
/// outputs which can be spent by us are found, at least one descriptor is returned. | ||
/// | ||
/// `confirmation_height` must be the height of the block in which `tx` was included in. | ||
pub fn get_spendable_outputs(&self, tx: &Transaction, confirmation_height: u32) -> Vec<SpendableOutputDescriptor> { | ||
let inner = self.inner.lock().unwrap(); | ||
let current_height = inner.best_block.height; | ||
if current_height.saturating_sub(ANTI_REORG_DELAY) + 1 >= confirmation_height { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One API advance could be to pass a user-selected value rather than to rely on the hardcoded Here |
||
inner.get_spendable_outputs(tx) | ||
} else { | ||
Vec::new() | ||
} | ||
} | ||
} | ||
|
||
impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> { | ||
|
@@ -3441,7 +3468,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> { | |
} | ||
self.is_resolving_htlc_output(&tx, height, &block_hash, &logger); | ||
|
||
self.is_paying_spendable_output(&tx, height, &block_hash, &logger); | ||
self.check_tx_and_push_spendable_outputs(&tx, height, &block_hash, &logger); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a counterparty is paying to one of the spendable descriptor (e.g our I don’t think this is a new concern and this sounds already permissible by current |
||
} | ||
} | ||
|
||
|
@@ -3987,34 +4014,18 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> { | |
} | ||
} | ||
|
||
/// Check if any transaction broadcasted is paying fund back to some address we can assume to own | ||
fn is_paying_spendable_output<L: Deref>(&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &L) where L::Target: Logger { | ||
let mut spendable_output = None; | ||
for (i, outp) in tx.output.iter().enumerate() { // There is max one spendable output for any channel tx, including ones generated by us | ||
if i > ::core::u16::MAX as usize { | ||
// While it is possible that an output exists on chain which is greater than the | ||
// 2^16th output in a given transaction, this is only possible if the output is not | ||
// in a lightning transaction and was instead placed there by some third party who | ||
// wishes to give us money for no reason. | ||
// Namely, any lightning transactions which we pre-sign will never have anywhere | ||
// near 2^16 outputs both because such transactions must have ~2^16 outputs who's | ||
// scripts are not longer than one byte in length and because they are inherently | ||
// non-standard due to their size. | ||
// Thus, it is completely safe to ignore such outputs, and while it may result in | ||
// us ignoring non-lightning fund to us, that is only possible if someone fills | ||
// nearly a full block with garbage just to hit this case. | ||
continue; | ||
} | ||
fn get_spendable_outputs(&self, tx: &Transaction) -> Vec<SpendableOutputDescriptor> { | ||
let mut spendable_outputs = Vec::new(); | ||
for (i, outp) in tx.output.iter().enumerate() { | ||
if outp.script_pubkey == self.destination_script { | ||
spendable_output = Some(SpendableOutputDescriptor::StaticOutput { | ||
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput { | ||
outpoint: OutPoint { txid: tx.txid(), index: i as u16 }, | ||
output: outp.clone(), | ||
}); | ||
break; | ||
} | ||
if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script { | ||
if broadcasted_holder_revokable_script.0 == outp.script_pubkey { | ||
spendable_output = Some(SpendableOutputDescriptor::DelayedPaymentOutput(DelayedPaymentOutputDescriptor { | ||
spendable_outputs.push(SpendableOutputDescriptor::DelayedPaymentOutput(DelayedPaymentOutputDescriptor { | ||
outpoint: OutPoint { txid: tx.txid(), index: i as u16 }, | ||
per_commitment_point: broadcasted_holder_revokable_script.1, | ||
to_self_delay: self.on_holder_tx_csv, | ||
|
@@ -4023,27 +4034,32 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> { | |
channel_keys_id: self.channel_keys_id, | ||
channel_value_satoshis: self.channel_value_satoshis, | ||
})); | ||
break; | ||
} | ||
} | ||
if self.counterparty_payment_script == outp.script_pubkey { | ||
spendable_output = Some(SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor { | ||
spendable_outputs.push(SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor { | ||
outpoint: OutPoint { txid: tx.txid(), index: i as u16 }, | ||
output: outp.clone(), | ||
channel_keys_id: self.channel_keys_id, | ||
channel_value_satoshis: self.channel_value_satoshis, | ||
})); | ||
break; | ||
} | ||
if self.shutdown_script.as_ref() == Some(&outp.script_pubkey) { | ||
spendable_output = Some(SpendableOutputDescriptor::StaticOutput { | ||
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput { | ||
outpoint: OutPoint { txid: tx.txid(), index: i as u16 }, | ||
output: outp.clone(), | ||
}); | ||
break; | ||
} | ||
} | ||
if let Some(spendable_output) = spendable_output { | ||
spendable_outputs | ||
} | ||
|
||
/// Checks if the confirmed transaction is paying funds back to some address we can assume to | ||
/// own. | ||
fn check_tx_and_push_spendable_outputs<L: Deref>( | ||
&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &L, | ||
) where L::Target: Logger { | ||
for spendable_output in self.get_spendable_outputs(tx) { | ||
let entry = OnchainEventEntry { | ||
txid: tx.txid(), | ||
transaction: Some(tx.clone()), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to imply you only need to pass the one immediate descendant - the commitment tx, but actually we need to go two layers deep.