@@ -65,6 +65,8 @@ use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, Maybe
65
65
use crate :: util:: logger:: { Level , Logger } ;
66
66
use crate :: util:: errors:: APIError ;
67
67
68
+ use alloc:: collections:: BTreeMap ;
69
+
68
70
use crate :: io;
69
71
use crate :: prelude:: * ;
70
72
use core:: { cmp, mem} ;
@@ -474,6 +476,11 @@ pub(crate) enum MonitorUpdateCompletionAction {
474
476
EmitEvent { event : events:: Event } ,
475
477
}
476
478
479
+ impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
480
+ ( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
481
+ ( 2 , EmitEvent ) => { ( 0 , event, ignorable) } ,
482
+ ) ;
483
+
477
484
/// State we hold per-peer.
478
485
pub ( super ) struct PeerState < Signer : ChannelSigner > {
479
486
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -487,6 +494,21 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
487
494
/// Messages to send to the peer - pushed to in the same lock that they are generated in (except
488
495
/// for broadcast messages, where ordering isn't as strict).
489
496
pub ( super ) pending_msg_events : Vec < MessageSendEvent > ,
497
+ /// Map from a specific channel to some action(s) that should be taken when all pending
498
+ /// [`ChannelMonitorUpdate`]s for the channel complete updating.
499
+ ///
500
+ /// Note that because we generally only have one entry here a HashMap is pretty overkill. A
501
+ /// BTreeMap currently stores more than ten elements per leaf node, so even up to a few
502
+ /// channels with a peer this will just be one allocation and will amount to a linear list of
503
+ /// channels to walk, avoiding the whole hashing rigmarole.
504
+ ///
505
+ /// Note that the channel may no longer exist. For example, if a channel was closed but we
506
+ /// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
507
+ /// for a missing channel. While a malicious peer could construct a second channel with the
508
+ /// same `temporary_channel_id` (or final `channel_id` in the case of 0conf channels or prior
509
+ /// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
510
+ /// duplicates do not occur, so such channels should fail without a monitor update completing.
511
+ monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
490
512
/// The peer is currently connected (i.e. we've seen a
491
513
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
492
514
/// [`ChannelMessageHandler::peer_disconnected`].
@@ -501,7 +523,7 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
501
523
if require_disconnected && self . is_connected {
502
524
return false
503
525
}
504
- self . channel_by_id . len ( ) == 0
526
+ self . channel_by_id . is_empty ( ) && self . monitor_update_blocked_actions . is_empty ( )
505
527
}
506
528
}
507
529
@@ -6319,6 +6341,7 @@ where
6319
6341
channel_by_id : HashMap :: new ( ) ,
6320
6342
latest_features : init_msg. features . clone ( ) ,
6321
6343
pending_msg_events : Vec :: new ( ) ,
6344
+ monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6322
6345
is_connected : true ,
6323
6346
} ) ) ;
6324
6347
} ,
@@ -6946,17 +6969,26 @@ where
6946
6969
htlc_purposes. push ( purpose) ;
6947
6970
}
6948
6971
6972
+ let mut monitor_update_blocked_actions_per_peer = None ;
6973
+ let mut peer_states = Vec :: new ( ) ;
6974
+ for ( _, peer_state_mutex) in per_peer_state. iter ( ) {
6975
+ peer_states. push ( peer_state_mutex. lock ( ) . unwrap ( ) ) ;
6976
+ }
6977
+
6949
6978
( serializable_peer_count) . write ( writer) ?;
6950
- for ( peer_pubkey, peer_state_mutex) in per_peer_state. iter ( ) {
6951
- let peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6952
- let peer_state = & * peer_state_lock;
6979
+ for ( ( peer_pubkey, _) , peer_state) in per_peer_state. iter ( ) . zip ( peer_states. iter ( ) ) {
6953
6980
// Peers which we have no channels to should be dropped once disconnected. As we
6954
6981
// disconnect all peers when shutting down and serializing the ChannelManager, we
6955
6982
// consider all peers as disconnected here. There's therefore no need write peers with
6956
6983
// no channels.
6957
6984
if !peer_state. ok_to_remove ( false ) {
6958
6985
peer_pubkey. write ( writer) ?;
6959
6986
peer_state. latest_features . write ( writer) ?;
6987
+ if !peer_state. monitor_update_blocked_actions . is_empty ( ) {
6988
+ monitor_update_blocked_actions_per_peer
6989
+ . get_or_insert_with ( Vec :: new)
6990
+ . push ( ( * peer_pubkey, & peer_state. monitor_update_blocked_actions ) ) ;
6991
+ }
6960
6992
}
6961
6993
}
6962
6994
@@ -7044,6 +7076,7 @@ where
7044
7076
( 3 , pending_outbound_payments, required) ,
7045
7077
( 4 , pending_claiming_payments, option) ,
7046
7078
( 5 , self . our_network_pubkey, required) ,
7079
+ ( 6 , monitor_update_blocked_actions_per_peer, option) ,
7047
7080
( 7 , self . fake_scid_rand_bytes, required) ,
7048
7081
( 9 , htlc_purposes, vec_type) ,
7049
7082
( 11 , self . probing_cookie_secret, required) ,
@@ -7356,6 +7389,7 @@ where
7356
7389
channel_by_id : peer_channels. remove ( & peer_pubkey) . unwrap_or ( HashMap :: new ( ) ) ,
7357
7390
latest_features : Readable :: read ( reader) ?,
7358
7391
pending_msg_events : Vec :: new ( ) ,
7392
+ monitor_update_blocked_actions : BTreeMap :: new ( ) ,
7359
7393
is_connected : false ,
7360
7394
} ;
7361
7395
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -7412,12 +7446,14 @@ where
7412
7446
let mut probing_cookie_secret: Option < [ u8 ; 32 ] > = None ;
7413
7447
let mut claimable_htlc_purposes = None ;
7414
7448
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
7449
+ let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
7415
7450
read_tlv_fields ! ( reader, {
7416
7451
( 1 , pending_outbound_payments_no_retry, option) ,
7417
7452
( 2 , pending_intercepted_htlcs, option) ,
7418
7453
( 3 , pending_outbound_payments, option) ,
7419
7454
( 4 , pending_claiming_payments, option) ,
7420
7455
( 5 , received_network_pubkey, option) ,
7456
+ ( 6 , monitor_update_blocked_actions_per_peer, option) ,
7421
7457
( 7 , fake_scid_rand_bytes, option) ,
7422
7458
( 9 , claimable_htlc_purposes, vec_type) ,
7423
7459
( 11 , probing_cookie_secret, option) ,
@@ -7683,6 +7719,15 @@ where
7683
7719
}
7684
7720
}
7685
7721
7722
+ for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
7723
+ if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
7724
+ peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
7725
+ } else {
7726
+ log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
7727
+ return Err ( DecodeError :: InvalidValue ) ;
7728
+ }
7729
+ }
7730
+
7686
7731
let channel_manager = ChannelManager {
7687
7732
genesis_hash,
7688
7733
fee_estimator : bounded_fee_estimator,
0 commit comments