Skip to content

Commit 26008bb

Browse files
committed
Move events into ChannelMonitor from ManyChannelMonitor
This is the next step after "Move pending-HTLC-updated ChannelMonitor from ManyChannelMonitor", moving our events into ChannelMonitor as well and leaving only new-outputs-to-watch in the return value for ChannelMonitor::block_connected (which is fine as those are duplicatively tracked in the ChannelMonitor directly, so losing/replaying them is acceptable).
1 parent bfd4ac4 commit 26008bb

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInter
3737
use chain::transaction::OutPoint;
3838
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
3939
use util::logger::Logger;
40-
use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48};
40+
use util::ser::{ReadableArgs, Readable, MaybeReadable, Writer, Writeable, U48};
4141
use util::{byte_utils, events};
4242

4343
use std::collections::{HashMap, hash_map, HashSet};
@@ -222,7 +222,6 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: D
222222
monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
223223
chain_monitor: Arc<ChainWatchInterface>,
224224
broadcaster: T,
225-
pending_events: Mutex<Vec<events::Event>>,
226225
logger: Arc<Logger>,
227226
fee_estimator: F
228227
}
@@ -234,16 +233,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
234233
{
235234
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
236235
let block_hash = header.bitcoin_hash();
237-
let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
238236
{
239237
let mut monitors = self.monitors.lock().unwrap();
240238
for monitor in monitors.values_mut() {
241-
let (txn_outputs, spendable_outputs) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator);
242-
if spendable_outputs.len() > 0 {
243-
new_events.push(events::Event::SpendableOutputs {
244-
outputs: spendable_outputs,
245-
});
246-
}
239+
let txn_outputs = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator);
247240

248241
for (ref txid, ref outputs) in txn_outputs {
249242
for (idx, output) in outputs.iter().enumerate() {
@@ -252,8 +245,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
252245
}
253246
}
254247
}
255-
let mut pending_events = self.pending_events.lock().unwrap();
256-
pending_events.append(&mut new_events);
257248
}
258249

259250
fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
@@ -276,7 +267,6 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
276267
monitors: Mutex::new(HashMap::new()),
277268
chain_monitor,
278269
broadcaster,
279-
pending_events: Mutex::new(Vec::new()),
280270
logger,
281271
fee_estimator: feeest,
282272
};
@@ -362,10 +352,11 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
362352
F::Target: FeeEstimator
363353
{
364354
fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
365-
let mut pending_events = self.pending_events.lock().unwrap();
366-
let mut ret = Vec::new();
367-
mem::swap(&mut ret, &mut *pending_events);
368-
ret
355+
let mut pending_events = Vec::new();
356+
for chan in self.monitors.lock().unwrap().values_mut() {
357+
pending_events.append(&mut chan.get_and_clear_pending_events());
358+
}
359+
pending_events
369360
}
370361
}
371362

@@ -835,6 +826,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
835826
payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
836827

837828
pending_htlcs_updated: Vec<HTLCUpdate>,
829+
pending_events: Vec<events::Event>,
838830

839831
destination_script: Script,
840832
// Thanks to data loss protection, we may be able to claim our non-htlc funds
@@ -948,6 +940,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
948940
self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
949941
self.payment_preimages != other.payment_preimages ||
950942
self.pending_htlcs_updated != other.pending_htlcs_updated ||
943+
self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
951944
self.destination_script != other.destination_script ||
952945
self.to_remote_rescue != other.to_remote_rescue ||
953946
self.pending_claim_requests != other.pending_claim_requests ||
@@ -1135,6 +1128,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
11351128
data.write(writer)?;
11361129
}
11371130

1131+
writer.write_all(&byte_utils::be64_to_array(self.pending_events.len() as u64))?;
1132+
for event in self.pending_events.iter() {
1133+
event.write(writer)?;
1134+
}
1135+
11381136
self.last_block_hash.write(writer)?;
11391137
self.destination_script.write(writer)?;
11401138
if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
@@ -1267,6 +1265,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12671265

12681266
payment_preimages: HashMap::new(),
12691267
pending_htlcs_updated: Vec::new(),
1268+
pending_events: Vec::new(),
12701269

12711270
destination_script: destination_script.clone(),
12721271
to_remote_rescue: None,
@@ -1560,6 +1559,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15601559
ret
15611560
}
15621561

1562+
/// Gets the list of pending events which were generated by previous actions, clearing the list
1563+
/// in the process.
1564+
///
1565+
/// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
1566+
/// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
1567+
/// no internal locking in ChannelMonitors.
1568+
pub fn get_and_clear_pending_events(&mut self) -> Vec<events::Event> {
1569+
let mut ret = Vec::new();
1570+
mem::swap(&mut ret, &mut self.pending_events);
1571+
ret
1572+
}
1573+
15631574
/// Can only fail if idx is < get_min_seen_secret
15641575
pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
15651576
self.commitment_secrets.get_secret(idx)
@@ -2534,7 +2545,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
25342545
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
25352546
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
25362547
/// on-chain.
2537-
fn block_connected<B: Deref, F: Deref>(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: B, fee_estimator: F)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>)
2548+
fn block_connected<B: Deref, F: Deref>(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: B, fee_estimator: F)-> Vec<(Sha256dHash, Vec<TxOut>)>
25382549
where B::Target: BroadcasterInterface,
25392550
F::Target: FeeEstimator
25402551
{
@@ -2767,7 +2778,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
27672778
for &(ref txid, ref output_scripts) in watch_outputs.iter() {
27682779
self.outputs_to_watch.insert(txid.clone(), output_scripts.iter().map(|o| o.script_pubkey.clone()).collect());
27692780
}
2770-
(watch_outputs, spendable_outputs)
2781+
2782+
if spendable_outputs.len() > 0 {
2783+
self.pending_events.push(events::Event::SpendableOutputs {
2784+
outputs: spendable_outputs,
2785+
});
2786+
}
2787+
2788+
watch_outputs
27712789
}
27722790

27732791
fn block_disconnected<B: Deref, F: Deref>(&mut self, height: u32, block_hash: &Sha256dHash, broadcaster: B, fee_estimator: F)
@@ -3369,6 +3387,14 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
33693387
pending_htlcs_updated.push(Readable::read(reader)?);
33703388
}
33713389

3390+
let pending_events_len: u64 = Readable::read(reader)?;
3391+
let mut pending_events = Vec::with_capacity(cmp::min(pending_events_len as usize, MAX_ALLOC_SIZE / mem::size_of::<events::Event>()));
3392+
for _ in 0..pending_events_len {
3393+
if let Some(event) = MaybeReadable::read(reader)? {
3394+
pending_events.push(event);
3395+
}
3396+
}
3397+
33723398
let last_block_hash: Sha256dHash = Readable::read(reader)?;
33733399
let destination_script = Readable::read(reader)?;
33743400
let to_remote_rescue = match <u8 as Readable<R>>::read(reader)? {
@@ -3471,6 +3497,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
34713497

34723498
payment_preimages,
34733499
pending_htlcs_updated,
3500+
pending_events,
34743501

34753502
destination_script,
34763503
to_remote_rescue,

0 commit comments

Comments
 (0)