@@ -2571,6 +2571,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
2571
2571
let current_height = self . current_best_block ( ) . height ;
2572
2572
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
2573
2573
2574
+ if inner. is_closed_without_updates ( )
2575
+ && is_all_funds_claimed
2576
+ && !inner. funding_spend_seen
2577
+ {
2578
+ // We closed the channel without ever advancing it and didn't have any funds in it.
2579
+ // We should immediately archive this monitor as there's nothing for us to ever do with
2580
+ // it.
2581
+ return ( true , false ) ;
2582
+ }
2583
+
2574
2584
if is_all_funds_claimed && !inner. funding_spend_seen {
2575
2585
debug_assert ! ( false , "We should see funding spend by the time a monitor clears out" ) ;
2576
2586
is_all_funds_claimed = false ;
@@ -3044,7 +3054,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3044
3054
transaction_fee_satoshis,
3045
3055
}
3046
3056
} )
3047
- . collect ( ) ;
3057
+ . collect :: < Vec < _ > > ( ) ;
3048
3058
let confirmed_balance_candidate_index = core:: iter:: once ( & us. funding )
3049
3059
. chain ( us. pending_funding . iter ( ) )
3050
3060
. enumerate ( )
@@ -3057,14 +3067,25 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3057
3067
} )
3058
3068
. map ( |( idx, _) | idx)
3059
3069
. expect ( "We must have one FundingScope that is confirmed" ) ;
3060
- res. push ( Balance :: ClaimableOnChannelClose {
3061
- balance_candidates,
3062
- confirmed_balance_candidate_index,
3063
- outbound_payment_htlc_rounded_msat,
3064
- outbound_forwarded_htlc_rounded_msat,
3065
- inbound_claiming_htlc_rounded_msat,
3066
- inbound_htlc_rounded_msat,
3067
- } ) ;
3070
+
3071
+ // Only push a primary balance if either the channel isn't closed or we've advanced the
3072
+ // channel state machine at least once (implying there are multiple previous commitment
3073
+ // transactions) or we actually have a balance.
3074
+ // Avoiding including a `Balance` if none of these are true allows us to prune monitors
3075
+ // for chanels that were opened inbound to us but where the funding transaction never
3076
+ // confirmed at all.
3077
+ if !us. is_closed_without_updates ( )
3078
+ || balance_candidates. iter ( ) . any ( |bal| bal. amount_satoshis != 0 )
3079
+ {
3080
+ res. push ( Balance :: ClaimableOnChannelClose {
3081
+ balance_candidates,
3082
+ confirmed_balance_candidate_index,
3083
+ outbound_payment_htlc_rounded_msat,
3084
+ outbound_forwarded_htlc_rounded_msat,
3085
+ inbound_claiming_htlc_rounded_msat,
3086
+ inbound_htlc_rounded_msat,
3087
+ } ) ;
3088
+ }
3068
3089
}
3069
3090
3070
3091
res
@@ -4352,6 +4373,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4352
4373
} else { ret }
4353
4374
}
4354
4375
4376
+ /// Returns true if the channel has been closed (i.e. no further updates are allowed) and no
4377
+ /// commitment state updates ever happened.
4378
+ fn is_closed_without_updates ( & self ) -> bool {
4379
+ let mut commitment_not_advanced =
4380
+ self . current_counterparty_commitment_number == INITIAL_COMMITMENT_NUMBER ;
4381
+ commitment_not_advanced &=
4382
+ self . current_holder_commitment_number == INITIAL_COMMITMENT_NUMBER ;
4383
+ ( self . holder_tx_signed || self . lockdown_from_offchain ) && commitment_not_advanced
4384
+ }
4385
+
4355
4386
fn no_further_updates_allowed ( & self ) -> bool {
4356
4387
self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed
4357
4388
}
0 commit comments