@@ -794,6 +794,9 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
794
794
#[ cfg( not( test) ) ]
795
795
onchain_tx_handler : OnchainTxHandler < ChanSigner > ,
796
796
797
+ // Used to detect programming bug due to unsafe monitor update sequence { ChannelForceClosed, LatestLocalCommitmentTXInfo }
798
+ lockdown_from_offchain : bool ,
799
+
797
800
// We simply modify last_block_hash in Channel's block_connected so that serialization is
798
801
// consistent but hopefully the users' copy handles block_connected in a consistent way.
799
802
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
@@ -1053,6 +1056,8 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1053
1056
}
1054
1057
self . onchain_tx_handler . write ( writer) ?;
1055
1058
1059
+ self . lockdown_from_offchain . write ( writer) ?;
1060
+
1056
1061
Ok ( ( ) )
1057
1062
}
1058
1063
@@ -1136,6 +1141,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1136
1141
1137
1142
onchain_tx_handler : OnchainTxHandler :: new ( destination_script. clone ( ) , keys, funding_redeemscript, their_to_self_delay, logger. clone ( ) ) ,
1138
1143
1144
+ lockdown_from_offchain : false ,
1145
+
1139
1146
last_block_hash : Default :: default ( ) ,
1140
1147
secp_ctx : Secp256k1 :: new ( ) ,
1141
1148
logger,
@@ -1297,8 +1304,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1297
1304
pub ( super ) fn update_monitor_ooo ( & mut self , mut updates : ChannelMonitorUpdate ) -> Result < ( ) , MonitorUpdateError > {
1298
1305
for update in updates. updates . drain ( ..) {
1299
1306
match update {
1300
- ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } =>
1301
- self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?,
1307
+ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } => {
1308
+ if self . lockdown_from_offchain { panic ! ( ) ; }
1309
+ self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?
1310
+ } ,
1302
1311
ChannelMonitorUpdateStep :: LatestRemoteCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } =>
1303
1312
self . provide_latest_remote_commitment_tx_info ( & unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point) ,
1304
1313
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } =>
@@ -1326,8 +1335,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1326
1335
}
1327
1336
for update in updates. updates . drain ( ..) {
1328
1337
match update {
1329
- ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } =>
1330
- self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?,
1338
+ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } => {
1339
+ if self . lockdown_from_offchain { panic ! ( ) ; }
1340
+ self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?
1341
+ } ,
1331
1342
ChannelMonitorUpdateStep :: LatestRemoteCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } =>
1332
1343
self . provide_latest_remote_commitment_tx_info ( & unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point) ,
1333
1344
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } =>
@@ -1337,6 +1348,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1337
1348
ChannelMonitorUpdateStep :: RescueRemoteCommitmentTXInfo { their_current_per_commitment_point } =>
1338
1349
self . provide_rescue_remote_commitment_tx_info ( their_current_per_commitment_point) ,
1339
1350
ChannelMonitorUpdateStep :: ChannelForceClosed { should_broadcast } => {
1351
+ self . lockdown_from_offchain = true ;
1340
1352
if should_broadcast {
1341
1353
self . broadcast_latest_local_commitment_txn ( broadcaster) ;
1342
1354
} else {
@@ -2453,6 +2465,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
2453
2465
}
2454
2466
let onchain_tx_handler = ReadableArgs :: read ( reader, logger. clone ( ) ) ?;
2455
2467
2468
+ let lockdown_from_offchain = Readable :: read ( reader) ?;
2469
+
2456
2470
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
2457
2471
latest_update_id,
2458
2472
commitment_transaction_number_obscure_factor,
@@ -2491,6 +2505,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
2491
2505
2492
2506
onchain_tx_handler,
2493
2507
2508
+ lockdown_from_offchain,
2509
+
2494
2510
last_block_hash,
2495
2511
secp_ctx : Secp256k1 :: new ( ) ,
2496
2512
logger,
0 commit comments