@@ -700,6 +700,11 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
700
700
// remote monitor out-of-order with regards to the block view.
701
701
holder_tx_signed : bool ,
702
702
703
+ // If a spend of the funding output is seen, we set this to true and reject any further
704
+ // updates. This prevents any further changes in the offchain state no matter the order
705
+ // of block connection between ChannelMonitors and the ChannelManager.
706
+ funding_spend_seen : bool ,
707
+
703
708
funding_spend_confirmed : Option < Txid > ,
704
709
/// The set of HTLCs which have been either claimed or failed on chain and have reached
705
710
/// the requisite confirmations on the claim/fail transaction (either ANTI_REORG_DELAY or the
@@ -765,6 +770,7 @@ impl<Signer: Sign> PartialEq for ChannelMonitorImpl<Signer> {
765
770
self . outputs_to_watch != other. outputs_to_watch ||
766
771
self . lockdown_from_offchain != other. lockdown_from_offchain ||
767
772
self . holder_tx_signed != other. holder_tx_signed ||
773
+ self . funding_spend_seen != other. funding_spend_seen ||
768
774
self . funding_spend_confirmed != other. funding_spend_confirmed ||
769
775
self . htlcs_resolved_on_chain != other. htlcs_resolved_on_chain
770
776
{
@@ -940,6 +946,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
940
946
( 1 , self . funding_spend_confirmed, option) ,
941
947
( 3 , self . htlcs_resolved_on_chain, vec_type) ,
942
948
( 5 , self . pending_monitor_events, vec_type) ,
949
+ ( 7 , self . funding_spend_seen, required) ,
943
950
} ) ;
944
951
945
952
Ok ( ( ) )
@@ -1038,6 +1045,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1038
1045
1039
1046
lockdown_from_offchain : false ,
1040
1047
holder_tx_signed : false ,
1048
+ funding_spend_seen : false ,
1041
1049
funding_spend_confirmed : None ,
1042
1050
htlcs_resolved_on_chain : Vec :: new ( ) ,
1043
1051
@@ -1847,6 +1855,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1847
1855
}
1848
1856
}
1849
1857
self . latest_update_id = updates. update_id ;
1858
+
1859
+ if ret. is_ok ( ) && self . funding_spend_seen {
1860
+ ret = Err ( MonitorUpdateError ( "Counterparty attempted to update commitment after funding was spent" ) ) ;
1861
+ }
1850
1862
ret
1851
1863
}
1852
1864
@@ -2275,6 +2287,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2275
2287
if prevout. txid == self . funding_info . 0 . txid && prevout. vout == self . funding_info . 0 . index as u32 {
2276
2288
let mut balance_spendable_csv = None ;
2277
2289
log_info ! ( logger, "Channel closed by funding output spend in txid {}." , log_bytes!( tx. txid( ) ) ) ;
2290
+ self . funding_spend_seen = true ;
2278
2291
if ( tx. input [ 0 ] . sequence >> 8 * 3 ) as u8 == 0x80 && ( tx. lock_time >> 8 * 3 ) as u8 == 0x20 {
2279
2292
let ( mut new_outpoints, new_outputs) = self . check_spend_counterparty_transaction ( & tx, height, & logger) ;
2280
2293
if !new_outputs. 1 . is_empty ( ) {
@@ -3124,10 +3137,12 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
3124
3137
3125
3138
let mut funding_spend_confirmed = None ;
3126
3139
let mut htlcs_resolved_on_chain = Some ( Vec :: new ( ) ) ;
3140
+ let mut funding_spend_seen = Some ( false ) ;
3127
3141
read_tlv_fields ! ( reader, {
3128
3142
( 1 , funding_spend_confirmed, option) ,
3129
3143
( 3 , htlcs_resolved_on_chain, vec_type) ,
3130
3144
( 5 , pending_monitor_events, vec_type) ,
3145
+ ( 7 , funding_spend_seen, option) ,
3131
3146
} ) ;
3132
3147
3133
3148
let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -3177,6 +3192,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
3177
3192
3178
3193
lockdown_from_offchain,
3179
3194
holder_tx_signed,
3195
+ funding_spend_seen : funding_spend_seen. unwrap ( ) ,
3180
3196
funding_spend_confirmed,
3181
3197
htlcs_resolved_on_chain : htlcs_resolved_on_chain. unwrap ( ) ,
3182
3198
@@ -3190,6 +3206,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
3190
3206
3191
3207
#[ cfg( test) ]
3192
3208
mod tests {
3209
+ use bitcoin:: blockdata:: block:: BlockHeader ;
3193
3210
use bitcoin:: blockdata:: script:: { Script , Builder } ;
3194
3211
use bitcoin:: blockdata:: opcodes;
3195
3212
use bitcoin:: blockdata:: transaction:: { Transaction , TxIn , TxOut , SigHashType } ;
@@ -3198,24 +3215,125 @@ mod tests {
3198
3215
use bitcoin:: hashes:: Hash ;
3199
3216
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
3200
3217
use bitcoin:: hashes:: hex:: FromHex ;
3201
- use bitcoin:: hash_types:: Txid ;
3218
+ use bitcoin:: hash_types:: { BlockHash , Txid } ;
3202
3219
use bitcoin:: network:: constants:: Network ;
3220
+ use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
3221
+ use bitcoin:: secp256k1:: Secp256k1 ;
3222
+
3203
3223
use hex;
3204
- use chain:: BestBlock ;
3224
+
3225
+ use super :: ChannelMonitorUpdateStep ;
3226
+ use :: { check_added_monitors, check_closed_broadcast, check_closed_event, check_spends, get_local_commitment_txn, get_monitor, get_route_and_payment_hash, unwrap_send_err} ;
3227
+ use chain:: { BestBlock , Confirm } ;
3205
3228
use chain:: channelmonitor:: ChannelMonitor ;
3206
3229
use chain:: package:: { WEIGHT_OFFERED_HTLC , WEIGHT_RECEIVED_HTLC , WEIGHT_REVOKED_OFFERED_HTLC , WEIGHT_REVOKED_RECEIVED_HTLC , WEIGHT_REVOKED_OUTPUT } ;
3207
3230
use chain:: transaction:: OutPoint ;
3231
+ use chain:: keysinterface:: InMemorySigner ;
3208
3232
use ln:: { PaymentPreimage , PaymentHash } ;
3209
3233
use ln:: chan_utils;
3210
3234
use ln:: chan_utils:: { HTLCOutputInCommitment , ChannelPublicKeys , ChannelTransactionParameters , HolderCommitmentTransaction , CounterpartyChannelTransactionParameters } ;
3235
+ use ln:: channelmanager:: PaymentSendFailure ;
3236
+ use ln:: features:: InitFeatures ;
3237
+ use ln:: functional_test_utils:: * ;
3211
3238
use ln:: script:: ShutdownScript ;
3239
+ use util:: errors:: APIError ;
3240
+ use util:: events:: { ClosureReason , MessageSendEventsProvider } ;
3212
3241
use util:: test_utils:: { TestLogger , TestBroadcaster , TestFeeEstimator } ;
3213
- use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
3214
- use bitcoin:: secp256k1:: Secp256k1 ;
3242
+ use util:: ser:: { ReadableArgs , Writeable } ;
3215
3243
use sync:: { Arc , Mutex } ;
3216
- use chain :: keysinterface :: InMemorySigner ;
3244
+ use io ;
3217
3245
use prelude:: * ;
3218
3246
3247
+ fn do_test_funding_spend_refuses_updates ( use_local_txn : bool ) {
3248
+ // Previously, monitor updates were allowed freely even after a funding-spend transaction
3249
+ // confirmed. This would allow a race condition where we could receive a payment (including
3250
+ // the counterparty revoking their broadcasted state!) and accept it without recourse as
3251
+ // long as the ChannelMonitor receives the block first, the full commitment update dance
3252
+ // occurs after the block is connected, and before the ChannelManager receives the block.
3253
+ // Obviously this is an incredibly contrived race given the counterparty would be risking
3254
+ // their full channel balance for it, but its worth fixing nonetheless as it makes the
3255
+ // potential ChannelMonitor states simpler to reason about.
3256
+ //
3257
+ // This test checks said behavior, as well as ensuring a ChannelMonitorUpdate with multiple
3258
+ // updates is handled correctly in such conditions.
3259
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3260
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3261
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3262
+ let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3263
+ let channel = create_announced_chan_between_nodes (
3264
+ & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
3265
+ create_announced_chan_between_nodes (
3266
+ & nodes, 1 , 2 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
3267
+
3268
+ // Rebalance somewhat
3269
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 10_000_000 ) ;
3270
+
3271
+ // First route two payments for testing at the end
3272
+ let payment_preimage_1 = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1_000_000 ) . 0 ;
3273
+ let payment_preimage_2 = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1_000_000 ) . 0 ;
3274
+
3275
+ let local_txn = get_local_commitment_txn ! ( nodes[ 1 ] , channel. 2 ) ;
3276
+ assert_eq ! ( local_txn. len( ) , 1 ) ;
3277
+ let remote_txn = get_local_commitment_txn ! ( nodes[ 0 ] , channel. 2 ) ;
3278
+ assert_eq ! ( remote_txn. len( ) , 3 ) ; // Commitment and two HTLC-Timeouts
3279
+ check_spends ! ( remote_txn[ 1 ] , remote_txn[ 0 ] ) ;
3280
+ check_spends ! ( remote_txn[ 2 ] , remote_txn[ 0 ] ) ;
3281
+ let broadcast_tx = if use_local_txn { & local_txn[ 0 ] } else { & remote_txn[ 0 ] } ;
3282
+
3283
+ // Connect a commitment transaction, but only to the ChainMonitor/ChannelMonitor. The
3284
+ // channel is now closed, but the ChannelManager doesn't know that yet.
3285
+ let new_header = BlockHeader {
3286
+ version : 2 , time : 0 , bits : 0 , nonce : 0 ,
3287
+ prev_blockhash : nodes[ 0 ] . best_block_info ( ) . 0 ,
3288
+ merkle_root : Default :: default ( ) } ;
3289
+ let conf_height = nodes[ 0 ] . best_block_info ( ) . 1 + 1 ;
3290
+ nodes[ 1 ] . chain_monitor . chain_monitor . transactions_confirmed ( & new_header,
3291
+ & [ ( 0 , broadcast_tx) ] , conf_height) ;
3292
+
3293
+ let ( _, pre_update_monitor) = <( BlockHash , ChannelMonitor < InMemorySigner > ) >:: read (
3294
+ & mut io:: Cursor :: new ( & get_monitor ! ( nodes[ 1 ] , channel. 2 ) . encode ( ) ) ,
3295
+ & nodes[ 1 ] . keys_manager . backing ) . unwrap ( ) ;
3296
+
3297
+ // If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
3298
+ // the update through to the ChannelMonitor which will refuse it (as the channel is closed).
3299
+ let ( route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 1 ] , nodes[ 0 ] , 100_000 ) ;
3300
+ unwrap_send_err ! ( nodes[ 1 ] . node. send_payment( & route, payment_hash, & Some ( payment_secret) ) ,
3301
+ true , APIError :: ChannelUnavailable { ref err } ,
3302
+ assert!( err. contains( "ChannelMonitor storage failure" ) ) ) ;
3303
+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ; // After the failure we generate a close-channel monitor update
3304
+ check_closed_broadcast ! ( nodes[ 1 ] , true ) ;
3305
+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: ProcessingError { err: "ChannelMonitor storage failure" . to_string( ) } ) ;
3306
+
3307
+ let monitor_updates = nodes[ 1 ] . chain_monitor . monitor_updates . lock ( ) . unwrap ( ) ;
3308
+ let mut replay_update = monitor_updates. get ( & channel. 2 ) . unwrap ( ) . iter ( ) . rev ( ) . skip ( 1 ) . next ( ) . unwrap ( ) . clone ( ) ;
3309
+ assert_eq ! ( replay_update. updates. len( ) , 1 ) ;
3310
+ if let ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } = replay_update. updates [ 0 ] {
3311
+ } else { panic ! ( ) ; }
3312
+ replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage : payment_preimage_1 } ) ;
3313
+ replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage : payment_preimage_2 } ) ;
3314
+
3315
+ let broadcaster = TestBroadcaster :: new ( Arc :: clone ( & nodes[ 1 ] . blocks ) ) ;
3316
+ assert ! (
3317
+ pre_update_monitor. update_monitor( & replay_update, &&broadcaster, &&chanmon_cfgs[ 1 ] . fee_estimator, & nodes[ 1 ] . logger)
3318
+ . is_err( ) ) ;
3319
+ // Even though we error'd on the first update, we should still have generated an HTLC claim
3320
+ // transaction
3321
+ let txn_broadcasted = broadcaster. txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3322
+ assert ! ( txn_broadcasted. len( ) >= 2 ) ;
3323
+ let htlc_txn = txn_broadcasted. iter ( ) . filter ( |tx| {
3324
+ assert_eq ! ( tx. input. len( ) , 1 ) ;
3325
+ tx. input [ 0 ] . previous_output . txid == broadcast_tx. txid ( )
3326
+ } ) . collect :: < Vec < _ > > ( ) ;
3327
+ assert_eq ! ( htlc_txn. len( ) , 2 ) ;
3328
+ check_spends ! ( htlc_txn[ 0 ] , broadcast_tx) ;
3329
+ check_spends ! ( htlc_txn[ 1 ] , broadcast_tx) ;
3330
+ }
3331
+ #[ test]
3332
+ fn test_funding_spend_refuses_updates ( ) {
3333
+ do_test_funding_spend_refuses_updates ( true ) ;
3334
+ do_test_funding_spend_refuses_updates ( false ) ;
3335
+ }
3336
+
3219
3337
#[ test]
3220
3338
fn test_prune_preimages ( ) {
3221
3339
let secp_ctx = Secp256k1 :: new ( ) ;
0 commit comments