Skip to content

Commit 05ac59c

Browse files
authored
Merge pull request #830 from TheBlueMatt/2021-03-chanmon-deser-utils
Make chain::Filter slightly easier with some ChannelMonitor utilities.
2 parents 8b4ea56 + f52f777 commit 05ac59c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ where C::Target: chain::Filter,
193193
log_trace!(self.logger, "Got new Channel Monitor for channel {}", log_bytes!(funding_txo.0.to_channel_id()[..]));
194194

195195
if let Some(ref chain_source) = self.chain_source {
196-
chain_source.register_tx(&funding_txo.0.txid, &funding_txo.1);
197-
for (txid, outputs) in monitor.get_outputs_to_watch().iter() {
198-
for (idx, script_pubkey) in outputs.iter() {
199-
chain_source.register_output(&OutPoint { txid: *txid, index: *idx as u16 }, script_pubkey);
200-
}
201-
}
196+
monitor.load_outputs_to_watch(chain_source);
202197
}
203198
}
204199
entry.insert(monitor);

lightning/src/chain/channelmonitor.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use chain;
4545
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
4646
use chain::transaction::{OutPoint, TransactionData};
4747
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
48+
use chain::Filter;
4849
use util::logger::Logger;
4950
use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
5051
use util::byte_utils;
@@ -1165,10 +1166,23 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
11651166

11661167
/// Gets a list of txids, with their output scripts (in the order they appear in the
11671168
/// transaction), which we must learn about spends of via block_connected().
1168-
///
1169-
/// (C-not exported) because we have no HashMap bindings
1170-
pub fn get_outputs_to_watch(&self) -> HashMap<Txid, Vec<(u32, Script)>> {
1171-
self.inner.lock().unwrap().get_outputs_to_watch().clone()
1169+
pub fn get_outputs_to_watch(&self) -> Vec<(Txid, Vec<(u32, Script)>)> {
1170+
self.inner.lock().unwrap().get_outputs_to_watch()
1171+
.iter().map(|(txid, outputs)| (*txid, outputs.clone())).collect()
1172+
}
1173+
1174+
/// Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
1175+
/// calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
1176+
/// have been registered.
1177+
pub fn load_outputs_to_watch<F: Deref>(&self, filter: &F) where F::Target: chain::Filter {
1178+
let lock = self.inner.lock().unwrap();
1179+
filter.register_tx(&lock.get_funding_txo().0.txid, &lock.get_funding_txo().1);
1180+
for (txid, outputs) in lock.get_outputs_to_watch().iter() {
1181+
for (index, script_pubkey) in outputs.iter() {
1182+
assert!(*index <= u16::max_value() as u32);
1183+
filter.register_output(&OutPoint { txid: *txid, index: *index as u16 }, script_pubkey);
1184+
}
1185+
}
11721186
}
11731187

11741188
/// Get the list of HTLCs who's status has been updated on chain. This should be called by

0 commit comments

Comments
 (0)