@@ -29,7 +29,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
29
29
use crate :: chain;
30
30
use crate :: chain:: { ChannelMonitorUpdateStatus , Filter , WatchedOutput } ;
31
31
use crate :: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
32
- use crate :: chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , Balance , MonitorEvent , TransactionOutputs , WithChannelMonitor , LATENCY_GRACE_PERIOD_BLOCKS } ;
32
+ use crate :: chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , Balance , MonitorEvent , TransactionOutputs , WithChannelMonitor } ;
33
33
use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
34
34
use crate :: ln:: ChannelId ;
35
35
use crate :: sign:: ecdsa:: WriteableEcdsaChannelSigner ;
@@ -209,17 +209,6 @@ struct MonitorHolder<ChannelSigner: WriteableEcdsaChannelSigner> {
209
209
/// update_persisted_channel, the user returns a
210
210
/// [`ChannelMonitorUpdateStatus::InProgress`], and then calls channel_monitor_updated
211
211
/// immediately, racing our insertion of the pending update into the contained Vec.
212
- ///
213
- /// Beyond the synchronization of updates themselves, we cannot handle user events until after
214
- /// any chain updates have been stored on disk. Thus, we scan this list when returning updates
215
- /// to the ChannelManager, refusing to return any updates for a ChannelMonitor which is still
216
- /// being persisted fully to disk after a chain update.
217
- ///
218
- /// This avoids the possibility of handling, e.g. an on-chain claim, generating a claim monitor
219
- /// event, resulting in the relevant ChannelManager generating a PaymentSent event and dropping
220
- /// the pending payment entry, and then reloading before the monitor is persisted, resulting in
221
- /// the ChannelManager re-adding the same payment entry, before the same block is replayed,
222
- /// resulting in a duplicate PaymentSent event.
223
212
pending_monitor_updates : Mutex < Vec < MonitorUpdateId > > ,
224
213
/// The last block height at which no [`UpdateOrigin::ChainSync`] monitor updates were present
225
214
/// in `pending_monitor_updates`.
@@ -393,7 +382,7 @@ where C::Target: chain::Filter,
393
382
chain_sync_update_id
394
383
) ,
395
384
ChannelMonitorUpdateStatus :: InProgress => {
396
- log_debug ! ( logger, "Channel Monitor sync for channel {} in progress, holding events until completion! " , log_funding_info!( monitor) ) ;
385
+ log_debug ! ( logger, "Channel Monitor sync for channel {} in progress. " , log_funding_info!( monitor) ) ;
397
386
pending_monitor_updates. push ( update_id) ;
398
387
} ,
399
388
ChannelMonitorUpdateStatus :: UnrecoverableError => {
@@ -924,21 +913,12 @@ where C::Target: chain::Filter,
924
913
fn release_pending_monitor_events ( & self ) -> Vec < ( OutPoint , ChannelId , Vec < MonitorEvent > , Option < PublicKey > ) > {
925
914
let mut pending_monitor_events = self . pending_monitor_events . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
926
915
for monitor_state in self . monitors . read ( ) . unwrap ( ) . values ( ) {
927
- let logger = WithChannelMonitor :: from ( & self . logger , & monitor_state. monitor ) ;
928
- let is_pending_monitor_update = monitor_state. has_pending_chainsync_updates ( & monitor_state. pending_monitor_updates . lock ( ) . unwrap ( ) ) ;
929
- if !is_pending_monitor_update || monitor_state. last_chain_persist_height . load ( Ordering :: Acquire ) + LATENCY_GRACE_PERIOD_BLOCKS as usize <= self . highest_chain_height . load ( Ordering :: Acquire ) {
930
- if is_pending_monitor_update {
931
- log_error ! ( logger, "A ChannelMonitor sync took longer than {} blocks to complete." , LATENCY_GRACE_PERIOD_BLOCKS ) ;
932
- log_error ! ( logger, " To avoid funds-loss, we are allowing monitor updates to be released." ) ;
933
- log_error ! ( logger, " This may cause duplicate payment events to be generated." ) ;
934
- }
935
- let monitor_events = monitor_state. monitor . get_and_clear_pending_monitor_events ( ) ;
936
- if monitor_events. len ( ) > 0 {
937
- let monitor_outpoint = monitor_state. monitor . get_funding_txo ( ) . 0 ;
938
- let monitor_channel_id = monitor_state. monitor . channel_id ( ) ;
939
- let counterparty_node_id = monitor_state. monitor . get_counterparty_node_id ( ) ;
940
- pending_monitor_events. push ( ( monitor_outpoint, monitor_channel_id, monitor_events, counterparty_node_id) ) ;
941
- }
916
+ let monitor_events = monitor_state. monitor . get_and_clear_pending_monitor_events ( ) ;
917
+ if monitor_events. len ( ) > 0 {
918
+ let monitor_outpoint = monitor_state. monitor . get_funding_txo ( ) . 0 ;
919
+ let monitor_channel_id = monitor_state. monitor . channel_id ( ) ;
920
+ let counterparty_node_id = monitor_state. monitor . get_counterparty_node_id ( ) ;
921
+ pending_monitor_events. push ( ( monitor_outpoint, monitor_channel_id, monitor_events, counterparty_node_id) ) ;
942
922
}
943
923
}
944
924
pending_monitor_events
@@ -975,15 +955,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L
975
955
#[ cfg( test) ]
976
956
mod tests {
977
957
use crate :: check_added_monitors;
978
- use crate :: { expect_payment_claimed, expect_payment_path_successful, get_event_msg} ;
979
- use crate :: { get_htlc_update_msgs, get_local_commitment_txn, get_revoke_commit_msgs, get_route_and_payment_hash, unwrap_send_err} ;
980
- use crate :: chain:: { ChannelMonitorUpdateStatus , Confirm , Watch } ;
981
- use crate :: chain:: channelmonitor:: LATENCY_GRACE_PERIOD_BLOCKS ;
958
+ use crate :: { expect_payment_path_successful, get_event_msg} ;
959
+ use crate :: { get_htlc_update_msgs, get_revoke_commit_msgs} ;
960
+ use crate :: chain:: { ChannelMonitorUpdateStatus , Watch } ;
982
961
use crate :: events:: { Event , MessageSendEvent , MessageSendEventsProvider } ;
983
- use crate :: ln:: channelmanager:: { PaymentSendFailure , PaymentId , RecipientOnionFields } ;
984
962
use crate :: ln:: functional_test_utils:: * ;
985
963
use crate :: ln:: msgs:: ChannelMessageHandler ;
986
- use crate :: util:: errors:: APIError ;
987
964
988
965
#[ test]
989
966
fn test_async_ooo_offchain_updates ( ) {
@@ -1090,76 +1067,6 @@ mod tests {
1090
1067
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1091
1068
}
1092
1069
1093
- fn do_chainsync_pauses_events ( block_timeout : bool ) {
1094
- // When a chainsync monitor update occurs, any MonitorUpdates should be held before being
1095
- // passed upstream to a `ChannelManager` via `Watch::release_pending_monitor_events`. This
1096
- // tests that behavior, as well as some ways it might go wrong.
1097
- let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1098
- let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1099
- let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1100
- let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1101
- let channel = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1102
-
1103
- // Get a route for later and rebalance the channel somewhat
1104
- send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 10_000_000 ) ;
1105
- let ( route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 1 ] , 100_000 ) ;
1106
-
1107
- // First route a payment that we will claim on chain and give the recipient the preimage.
1108
- let ( payment_preimage, payment_hash, ..) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
1109
- nodes[ 1 ] . node . claim_funds ( payment_preimage) ;
1110
- expect_payment_claimed ! ( nodes[ 1 ] , payment_hash, 1_000_000 ) ;
1111
- nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1112
- check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1113
- let remote_txn = get_local_commitment_txn ! ( nodes[ 1 ] , channel. 2 ) ;
1114
- assert_eq ! ( remote_txn. len( ) , 2 ) ;
1115
-
1116
- // Temp-fail the block connection which will hold the channel-closed event
1117
- chanmon_cfgs[ 0 ] . persister . chain_sync_monitor_persistences . lock ( ) . unwrap ( ) . clear ( ) ;
1118
- chanmon_cfgs[ 0 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
1119
-
1120
- // Connect B's commitment transaction, but only to the ChainMonitor/ChannelMonitor. The
1121
- // channel is now closed, but the ChannelManager doesn't know that yet.
1122
- let new_header = create_dummy_header ( nodes[ 0 ] . best_block_info ( ) . 0 , 0 ) ;
1123
- nodes[ 0 ] . chain_monitor . chain_monitor . transactions_confirmed ( & new_header,
1124
- & [ ( 0 , & remote_txn[ 0 ] ) , ( 1 , & remote_txn[ 1 ] ) ] , nodes[ 0 ] . best_block_info ( ) . 1 + 1 ) ;
1125
- assert ! ( nodes[ 0 ] . chain_monitor. release_pending_monitor_events( ) . is_empty( ) ) ;
1126
- nodes[ 0 ] . chain_monitor . chain_monitor . best_block_updated ( & new_header, nodes[ 0 ] . best_block_info ( ) . 1 + 1 ) ;
1127
- assert ! ( nodes[ 0 ] . chain_monitor. release_pending_monitor_events( ) . is_empty( ) ) ;
1128
-
1129
- // If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
1130
- // the update through to the ChannelMonitor which will refuse it (as the channel is closed).
1131
- chanmon_cfgs[ 0 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: Completed ) ;
1132
- unwrap_send_err ! ( nodes[ 0 ] . node. send_payment_with_route( & route, second_payment_hash,
1133
- RecipientOnionFields :: secret_only( second_payment_secret) , PaymentId ( second_payment_hash. 0 )
1134
- ) , false , APIError :: MonitorUpdateInProgress , { } ) ;
1135
- check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1136
-
1137
- // However, as the ChainMonitor is still waiting for the original persistence to complete,
1138
- // it won't yet release the MonitorEvents.
1139
- assert ! ( nodes[ 0 ] . chain_monitor. release_pending_monitor_events( ) . is_empty( ) ) ;
1140
-
1141
- if block_timeout {
1142
- // After three blocks, pending MontiorEvents should be released either way.
1143
- let latest_header = create_dummy_header ( nodes[ 0 ] . best_block_info ( ) . 0 , 0 ) ;
1144
- nodes[ 0 ] . chain_monitor . chain_monitor . best_block_updated ( & latest_header, nodes[ 0 ] . best_block_info ( ) . 1 + LATENCY_GRACE_PERIOD_BLOCKS ) ;
1145
- } else {
1146
- let persistences = chanmon_cfgs[ 0 ] . persister . chain_sync_monitor_persistences . lock ( ) . unwrap ( ) . clone ( ) ;
1147
- for ( funding_outpoint, update_ids) in persistences {
1148
- for update_id in update_ids {
1149
- nodes[ 0 ] . chain_monitor . chain_monitor . channel_monitor_updated ( funding_outpoint, update_id) . unwrap ( ) ;
1150
- }
1151
- }
1152
- }
1153
-
1154
- expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , false ) ;
1155
- }
1156
-
1157
- #[ test]
1158
- fn chainsync_pauses_events ( ) {
1159
- do_chainsync_pauses_events ( false ) ;
1160
- do_chainsync_pauses_events ( true ) ;
1161
- }
1162
-
1163
1070
#[ test]
1164
1071
#[ cfg( feature = "std" ) ]
1165
1072
fn update_during_chainsync_poisons_channel ( ) {
@@ -1182,3 +1089,4 @@ mod tests {
1182
1089
} ) . is_err( ) ) ;
1183
1090
}
1184
1091
}
1092
+
0 commit comments