@@ -285,6 +285,8 @@ pub struct Channel {
285
285
their_delayed_payment_basepoint : PublicKey ,
286
286
their_htlc_basepoint : PublicKey ,
287
287
their_cur_commitment_point : PublicKey ,
288
+
289
+ their_prev_commitment_point : Option < PublicKey > ,
288
290
their_node_id : PublicKey ,
289
291
290
292
their_shutdown_scriptpubkey : Option < Script > ,
@@ -413,6 +415,8 @@ impl Channel {
413
415
their_delayed_payment_basepoint : PublicKey :: new ( ) ,
414
416
their_htlc_basepoint : PublicKey :: new ( ) ,
415
417
their_cur_commitment_point : PublicKey :: new ( ) ,
418
+
419
+ their_prev_commitment_point : None ,
416
420
their_node_id : their_node_id,
417
421
418
422
their_shutdown_scriptpubkey : None ,
@@ -534,6 +538,8 @@ impl Channel {
534
538
their_delayed_payment_basepoint : msg. delayed_payment_basepoint ,
535
539
their_htlc_basepoint : msg. htlc_basepoint ,
536
540
their_cur_commitment_point : msg. first_per_commitment_point ,
541
+
542
+ their_prev_commitment_point : None ,
537
543
their_node_id : their_node_id,
538
544
539
545
their_shutdown_scriptpubkey : None ,
@@ -1183,6 +1189,8 @@ impl Channel {
1183
1189
self . channel_state = ChannelState :: FundingSent as u32 ;
1184
1190
let funding_txo = self . channel_monitor . get_funding_txo ( ) . unwrap ( ) ;
1185
1191
self . channel_id = funding_txo. 0 . into_be ( ) ^ Uint256 :: from_u64 ( funding_txo. 1 as u64 ) . unwrap ( ) ; //TODO: or le?
1192
+ self . cur_remote_commitment_transaction_number -= 1 ;
1193
+ self . cur_local_commitment_transaction_number -= 1 ;
1186
1194
1187
1195
Ok ( msgs:: FundingSigned {
1188
1196
channel_id : self . channel_id ,
@@ -1199,7 +1207,7 @@ impl Channel {
1199
1207
if self . channel_state != ChannelState :: FundingCreated as u32 {
1200
1208
return Err ( HandleError { err : "Received funding_signed in strange state!" , msg : None } ) ;
1201
1209
}
1202
- if self . channel_monitor . get_min_seen_secret ( ) != ( 1 << 48 ) || self . cur_remote_commitment_transaction_number != ( 1 << 48 ) - 1 || self . cur_local_commitment_transaction_number != ( 1 << 48 ) - 1 {
1210
+ if self . channel_monitor . get_min_seen_secret ( ) != ( 1 << 48 ) || self . cur_remote_commitment_transaction_number != ( 1 << 48 ) - 2 || self . cur_local_commitment_transaction_number != ( 1 << 48 ) - 1 {
1203
1211
panic ! ( "Should not have advanced channel commitment tx numbers prior to funding_created" ) ;
1204
1212
}
1205
1213
@@ -1213,6 +1221,7 @@ impl Channel {
1213
1221
secp_call ! ( self . secp_ctx. verify( & local_sighash, & msg. signature, & self . their_funding_pubkey) , "Invalid funding_signed signature from peer" ) ;
1214
1222
1215
1223
self . channel_state = ChannelState :: FundingSent as u32 ;
1224
+ self . cur_local_commitment_transaction_number -= 1 ;
1216
1225
1217
1226
Ok ( ( ) )
1218
1227
}
@@ -1227,13 +1236,7 @@ impl Channel {
1227
1236
return Err ( HandleError { err : "Peer sent a funding_locked at a strange time" , msg : None } ) ;
1228
1237
}
1229
1238
1230
- //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
1231
- //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
1232
- //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
1233
- //a protocol oversight, but I assume I'm just missing something.
1234
- if self . their_cur_commitment_point != msg. next_per_commitment_point {
1235
- return Err ( HandleError { err : "Non-duplicate next_per_commitment_point in funding_locked" , msg : None } ) ;
1236
- }
1239
+ self . their_prev_commitment_point = Some ( self . their_cur_commitment_point ) ;
1237
1240
self . their_cur_commitment_point = msg. next_per_commitment_point ;
1238
1241
Ok ( ( ) )
1239
1242
}
@@ -1407,9 +1410,7 @@ impl Channel {
1407
1410
}
1408
1411
1409
1412
let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number - 1 ) ) . unwrap ( ) ;
1410
- let per_commitment_secret = chan_utils:: build_commitment_secret ( self . local_keys . commitment_seed , self . cur_local_commitment_transaction_number ) ;
1411
-
1412
- //TODO: Store htlc keys in our channel_watcher
1413
+ let per_commitment_secret = chan_utils:: build_commitment_secret ( self . local_keys . commitment_seed , self . cur_local_commitment_transaction_number + 1 ) ;
1413
1414
1414
1415
// Update state now that we've passed all the can-fail calls...
1415
1416
@@ -1527,16 +1528,19 @@ impl Channel {
1527
1528
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1528
1529
return Err ( HandleError { err : "Got revoke/ACK message when channel was not in an operational state" , msg : None } ) ;
1529
1530
}
1530
- if PublicKey :: from_secret_key ( & self . secp_ctx , & secp_call ! ( SecretKey :: from_slice( & self . secp_ctx, & msg. per_commitment_secret) , "Peer provided an invalid per_commitment_secret" ) ) . unwrap ( ) != self . their_cur_commitment_point {
1531
- return Err ( HandleError { err : "Got a revoke commitment secret which didn't correspond to their current pubkey" , msg : None } ) ;
1531
+ if let Some ( their_prev_commitment_point) = self . their_prev_commitment_point {
1532
+ if PublicKey :: from_secret_key ( & self . secp_ctx , & secp_call ! ( SecretKey :: from_slice( & self . secp_ctx, & msg. per_commitment_secret) , "Peer provided an invalid per_commitment_secret" ) ) . unwrap ( ) != their_prev_commitment_point {
1533
+ return Err ( HandleError { err : "Got a revoke commitment secret which didn't correspond to their current pubkey" , msg : None } ) ;
1534
+ }
1532
1535
}
1533
- self . channel_monitor . provide_secret ( self . cur_remote_commitment_transaction_number , msg. per_commitment_secret ) ?;
1536
+ self . channel_monitor . provide_secret ( self . cur_remote_commitment_transaction_number + 1 , msg. per_commitment_secret ) ?;
1534
1537
1535
1538
// Update state now that we've passed all the can-fail calls...
1536
1539
// (note that we may still fail to generate the new commitment_signed message, but that's
1537
1540
// OK, we step the channel here and *then* if the new generation fails we can fail the
1538
1541
// channel based on that, but stepping stuff here should be safe either way.
1539
1542
self . channel_state &= !( ChannelState :: AwaitingRemoteRevoke as u32 ) ;
1543
+ self . their_prev_commitment_point = Some ( self . their_cur_commitment_point ) ;
1540
1544
self . their_cur_commitment_point = msg. next_per_commitment_point ;
1541
1545
self . cur_remote_commitment_transaction_number -= 1 ;
1542
1546
@@ -2058,6 +2062,7 @@ impl Channel {
2058
2062
self . channel_state = ChannelState :: FundingCreated as u32 ;
2059
2063
let funding_txo = self . channel_monitor . get_funding_txo ( ) . unwrap ( ) ;
2060
2064
self . channel_id = funding_txo. 0 . into_be ( ) ^ Uint256 :: from_u64 ( funding_txo. 1 as u64 ) . unwrap ( ) ; //TODO: or le?
2065
+ self . cur_remote_commitment_transaction_number -= 1 ;
2061
2066
2062
2067
Ok ( msgs:: FundingCreated {
2063
2068
temporary_channel_id : temporary_channel_id,
0 commit comments