@@ -837,11 +837,11 @@ impl ChannelMonitor {
837
837
mod tests {
838
838
use bitcoin:: util:: misc:: hex_bytes;
839
839
use bitcoin:: blockdata:: script:: Script ;
840
- use bitcoin:: util:: hash:: { Hash160 , Sha256dHash } ;
841
840
use bitcoin:: blockdata:: transaction:: Transaction ;
841
+ use crypto:: digest:: Digest ;
842
842
use ln:: channelmonitor:: ChannelMonitor ;
843
- use ln:: channelmonitor :: LocalSignedTx ;
844
- use ln :: chan_utils :: HTLCOutputInCommitment ;
843
+ use ln:: chan_utils :: { HTLCOutputInCommitment , TxCreationKeys } ;
844
+ use util :: sha2 :: Sha256 ;
845
845
use secp256k1:: key:: { SecretKey , PublicKey } ;
846
846
use secp256k1:: { Secp256k1 , Signature } ;
847
847
use rand:: { thread_rng, Rng } ;
@@ -1200,143 +1200,118 @@ mod tests {
1200
1200
}
1201
1201
}
1202
1202
1203
- macro_rules! gen_local_tx {
1204
- ( $hex : expr, $monitor : expr, $htlcs : expr, $rng : expr, $preimage : expr, $hash : expr) => {
1205
- {
1206
- let mut htlcs = Vec :: new( ) ;
1207
- for _i in 0 ..$htlcs {
1208
- $rng. fill_bytes( & mut $preimage) ;
1209
- $hash[ 0 ..20 ] . clone_from_slice( & Hash160 :: from_data( & $preimage) [ 0 ..20 ] ) ;
1210
- $monitor. provide_payment_preimage( & $hash, & $preimage) ;
1211
- htlcs. push( ( HTLCOutputInCommitment {
1212
- offered : true ,
1213
- amount_msat : 0 ,
1214
- cltv_expiry : 0 ,
1215
- payment_hash : $hash. clone( ) ,
1216
- transaction_output_index : 0 ,
1217
- } , Signature :: from_der( & Secp256k1 :: new( ) , $hex) . unwrap( ) ,
1218
- Signature :: from_der( & Secp256k1 :: new( ) , $hex) . unwrap( ) ) )
1219
- }
1203
+ #[ test]
1204
+ fn test_prune_preimages ( ) {
1205
+ let secp_ctx = Secp256k1 :: new ( ) ;
1206
+ let dummy_sig = Signature :: from_der ( & secp_ctx, & hex_bytes ( "3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd" ) . unwrap ( ) [ ..] ) . unwrap ( ) ;
1220
1207
1221
- Some ( LocalSignedTx {
1222
- txid: Sha256dHash :: from_data( & [ ] ) ,
1223
- tx: Transaction {
1224
- version: 0 ,
1225
- lock_time: 0 ,
1226
- input: Vec :: new( ) ,
1227
- output: Vec :: new( ) ,
1228
- } ,
1208
+ macro_rules! dummy_keys {
1209
+ ( ) => {
1210
+ TxCreationKeys {
1211
+ per_commitment_point: PublicKey :: new( ) ,
1229
1212
revocation_key: PublicKey :: new( ) ,
1230
1213
a_htlc_key: PublicKey :: new( ) ,
1231
1214
b_htlc_key: PublicKey :: new( ) ,
1232
- delayed_payment_key: PublicKey :: new( ) ,
1233
- feerate_per_kw: 0 ,
1234
- htlc_outputs: htlcs,
1235
- } )
1215
+ a_delayed_payment_key: PublicKey :: new( ) ,
1216
+ b_payment_key: PublicKey :: new( ) ,
1217
+ }
1236
1218
}
1237
1219
}
1238
- }
1220
+ let dummy_tx = Transaction { version : 0 , lock_time : 0 , input : Vec :: new ( ) , output : Vec :: new ( ) } ;
1239
1221
1240
- macro_rules! gen_remote_outpoints {
1241
- ( $monitor : expr, $tx : expr, $htlcs : expr, $rng : expr, $preimage : expr, $hash: expr, $number : expr) => {
1242
- {
1243
- let mut commitment_number = $number;
1244
- for i in 0 ..$tx {
1245
- let tx_zero = Transaction {
1246
- version : 0 ,
1247
- lock_time : i,
1248
- input : Vec :: new( ) ,
1249
- output: Vec :: new( ) ,
1250
- } ;
1222
+ let mut preimages = Vec :: new ( ) ;
1223
+ {
1224
+ let mut rng = thread_rng ( ) ;
1225
+ for _ in 0 ..20 {
1226
+ let mut preimage = [ 0 ; 32 ] ;
1227
+ rng. fill_bytes ( & mut preimage) ;
1228
+ let mut sha = Sha256 :: new ( ) ;
1229
+ sha. input ( & preimage) ;
1230
+ let mut hash = [ 0 ; 32 ] ;
1231
+ sha. result ( & mut hash) ;
1232
+ preimages. push ( ( preimage, hash) ) ;
1233
+ }
1234
+ }
1251
1235
1252
- let mut htlcs = Vec :: new ( ) ;
1253
- for _i in 0 ..$htlcs {
1254
- $rng . fill_bytes ( & mut $preimage ) ;
1255
- $hash [ 0 .. 20 ] . clone_from_slice ( & Hash160 :: from_data ( & $preimage ) [ 0 .. 20 ] ) ;
1256
- $monitor . provide_payment_preimage ( & $hash , & $ preimage) ;
1257
- htlcs . push( HTLCOutputInCommitment {
1258
- offered : true ,
1259
- amount_msat : 0 ,
1260
- cltv_expiry : 0 ,
1261
- payment_hash : $hash . clone( ) ,
1262
- transaction_output_index : 0 ,
1236
+ macro_rules! preimages_slice_to_htlc_outputs {
1237
+ ( $preimages_slice : expr ) => {
1238
+ {
1239
+ let mut res = Vec :: new ( ) ;
1240
+ for ( idx , preimage) in $preimages_slice . iter ( ) . enumerate ( ) {
1241
+ res . push( HTLCOutputInCommitment {
1242
+ offered: true ,
1243
+ amount_msat: 0 ,
1244
+ cltv_expiry: 0 ,
1245
+ payment_hash: preimage . 1 . clone( ) ,
1246
+ transaction_output_index: idx as u32 ,
1263
1247
} ) ;
1264
1248
}
1265
- commitment_number -= 1 ;
1266
- $monitor. provide_latest_remote_commitment_tx_info( & tx_zero, htlcs, commitment_number) ;
1249
+ res
1267
1250
}
1268
1251
}
1269
1252
}
1270
- }
1271
-
1272
- #[ test]
1273
- fn test_prune_preimages ( ) {
1274
- let mut secret = [ 0 ; 32 ] ;
1275
- secret[ 0 ..32 ] . clone_from_slice ( & hex_bytes ( "7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc" ) . unwrap ( ) ) ;
1276
- let secp_ctx = Secp256k1 :: new ( ) ;
1277
- let mut preimage: [ u8 ; 32 ] = [ 0 ; 32 ] ;
1278
- let mut hash: [ u8 ; 32 ] = [ 0 ; 32 ] ;
1279
- let mut rng = thread_rng ( ) ;
1280
-
1281
- {
1282
- // insert 30 random hash, 10 from local, 10 from remote, prune 30/50
1283
- let mut monitor = ChannelMonitor :: new ( & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) , & PublicKey :: new ( ) , & SecretKey :: from_slice ( & secp_ctx, & [ 43 ; 32 ] ) . unwrap ( ) , 0 , Script :: new ( ) ) ;
1284
-
1285
- for _i in 0 ..30 {
1286
- rng. fill_bytes ( & mut preimage) ;
1287
- hash[ 0 ..20 ] . clone_from_slice ( & Hash160 :: from_data ( & preimage) [ 0 ..20 ] ) ;
1288
- monitor. provide_payment_preimage ( & hash, & preimage) ;
1253
+ macro_rules! preimages_to_local_htlcs {
1254
+ ( $preimages_slice: expr) => {
1255
+ {
1256
+ let mut inp = preimages_slice_to_htlc_outputs!( $preimages_slice) ;
1257
+ let res: Vec <_> = inp. drain( ..) . map( |e| { ( e, dummy_sig. clone( ) , dummy_sig. clone( ) ) } ) . collect( ) ;
1258
+ res
1259
+ }
1289
1260
}
1290
- monitor. current_local_signed_commitment_tx = gen_local_tx ! ( & hex_bytes( "3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd" ) . unwrap( ) [ ..] , monitor, 10 , rng, preimage, hash) ;
1291
- gen_remote_outpoints ! ( monitor, 1 , 10 , rng, preimage, hash, 281474976710654 ) ;
1292
- monitor. provide_secret ( 281474976710655 , secret. clone ( ) , None ) . unwrap ( ) ;
1293
- assert_eq ! ( monitor. payment_preimages. len( ) , 20 ) ;
1294
1261
}
1295
1262
1296
- {
1297
- // insert 30 random hash, prune 30/30
1298
- let mut monitor = ChannelMonitor :: new ( & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) , & PublicKey :: new ( ) , & SecretKey :: from_slice ( & secp_ctx, & [ 43 ; 32 ] ) . unwrap ( ) , 0 , Script :: new ( ) ) ;
1299
-
1300
- for _i in 0 ..30 {
1301
- rng. fill_bytes ( & mut preimage) ;
1302
- hash[ 0 ..20 ] . clone_from_slice ( & Hash160 :: from_data ( & preimage) [ 0 ..20 ] ) ;
1303
- monitor. provide_payment_preimage ( & hash, & preimage) ;
1263
+ macro_rules! test_preimages_exist {
1264
+ ( $preimages_slice: expr, $monitor: expr) => {
1265
+ for preimage in $preimages_slice {
1266
+ assert!( $monitor. payment_preimages. contains_key( & preimage. 1 ) ) ;
1267
+ }
1304
1268
}
1305
- monitor. current_local_signed_commitment_tx = gen_local_tx ! ( & hex_bytes( "3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd" ) . unwrap( ) [ ..] , monitor, 0 , rng, preimage, hash) ;
1306
- gen_remote_outpoints ! ( monitor, 0 , 0 , rng, preimage, hash, 281474976710655 ) ;
1307
- monitor. provide_secret ( 281474976710655 , secret. clone ( ) , None ) . unwrap ( ) ;
1308
- assert_eq ! ( monitor. payment_preimages. len( ) , 0 ) ;
1309
1269
}
1310
1270
1311
- {
1312
- // insert 30 random hash, 25 on 5 remotes, prune 30/55
1313
- let mut monitor = ChannelMonitor :: new ( & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) , & PublicKey :: new ( ) , & SecretKey :: from_slice ( & secp_ctx, & [ 43 ; 32 ] ) . unwrap ( ) , 0 , Script :: new ( ) ) ;
1314
-
1315
- for _i in 0 ..30 {
1316
- rng. fill_bytes ( & mut preimage) ;
1317
- hash[ 0 ..20 ] . clone_from_slice ( & Hash160 :: from_data ( & preimage) [ 0 ..20 ] ) ;
1318
- monitor. provide_payment_preimage ( & hash, & preimage) ;
1319
- }
1320
- monitor. current_local_signed_commitment_tx = gen_local_tx ! ( & hex_bytes( "3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd" ) . unwrap( ) [ ..] , monitor, 0 , rng, preimage, hash) ;
1321
- gen_remote_outpoints ! ( monitor, 5 , 5 , rng, preimage, hash, 281474976710654 ) ;
1322
- monitor. provide_secret ( 281474976710655 , secret. clone ( ) , None ) . unwrap ( ) ;
1323
- assert_eq ! ( monitor. payment_preimages. len( ) , 25 ) ;
1271
+ // Prune with one old state and a local commitment tx holding a few overlaps with the
1272
+ // old state.
1273
+ let mut monitor = ChannelMonitor :: new ( & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) , & PublicKey :: new ( ) , & SecretKey :: from_slice ( & secp_ctx, & [ 43 ; 32 ] ) . unwrap ( ) , 0 , Script :: new ( ) ) ;
1274
+ monitor. set_their_to_self_delay ( 10 ) ;
1275
+
1276
+ monitor. provide_latest_local_commitment_tx_info ( dummy_tx. clone ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) ;
1277
+ monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 ) ;
1278
+ monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 ) ;
1279
+ monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 ) ;
1280
+ monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 ) ;
1281
+ for ( preimage, hash) in preimages. iter ( ) {
1282
+ monitor. provide_payment_preimage ( hash, preimage) ;
1324
1283
}
1325
1284
1326
- {
1327
- // insert 30 random hash, 25 from local, prune 30/55
1328
- let mut monitor = ChannelMonitor :: new ( & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) , & PublicKey :: new ( ) , & SecretKey :: from_slice ( & secp_ctx, & [ 43 ; 32 ] ) . unwrap ( ) , 0 , Script :: new ( ) ) ;
1329
-
1330
- for _i in 0 ..30 {
1331
- rng. fill_bytes ( & mut preimage) ;
1332
- hash[ 0 ..20 ] . clone_from_slice ( & Hash160 :: from_data ( & preimage) [ 0 ..20 ] ) ;
1333
- monitor. provide_payment_preimage ( & hash, & preimage) ;
1334
- }
1335
- monitor. current_local_signed_commitment_tx = gen_local_tx ! ( & hex_bytes( "3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd" ) . unwrap( ) [ ..] , monitor, 25 , rng, preimage, hash) ;
1336
- gen_remote_outpoints ! ( monitor, 0 , 0 , rng, preimage, hash, 281474976710655 ) ;
1337
- monitor. provide_secret ( 281474976710655 , secret. clone ( ) , None ) . unwrap ( ) ;
1338
- assert_eq ! ( monitor. payment_preimages. len( ) , 25 ) ;
1339
- }
1285
+ // Now provide a secret, pruning preimages 10-15
1286
+ let mut secret = [ 0 ; 32 ] ;
1287
+ secret[ 0 ..32 ] . clone_from_slice ( & hex_bytes ( "7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc" ) . unwrap ( ) ) ;
1288
+ monitor. provide_secret ( 281474976710655 , secret. clone ( ) , None ) . unwrap ( ) ;
1289
+ assert_eq ! ( monitor. payment_preimages. len( ) , 15 ) ;
1290
+ test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
1291
+ test_preimages_exist ! ( & preimages[ 15 ..20 ] , monitor) ;
1292
+
1293
+ // Now provide a further secret, pruning preimages 15-17
1294
+ secret[ 0 ..32 ] . clone_from_slice ( & hex_bytes ( "c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964" ) . unwrap ( ) ) ;
1295
+ monitor. provide_secret ( 281474976710654 , secret. clone ( ) , None ) . unwrap ( ) ;
1296
+ assert_eq ! ( monitor. payment_preimages. len( ) , 13 ) ;
1297
+ test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
1298
+ test_preimages_exist ! ( & preimages[ 17 ..20 ] , monitor) ;
1299
+
1300
+ // Now update local commitment tx info, pruning only element 18 as we still care about the
1301
+ // previous commitment tx's preimages too
1302
+ monitor. provide_latest_local_commitment_tx_info ( dummy_tx. clone ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) ;
1303
+ secret[ 0 ..32 ] . clone_from_slice ( & hex_bytes ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
1304
+ monitor. provide_secret ( 281474976710653 , secret. clone ( ) , None ) . unwrap ( ) ;
1305
+ assert_eq ! ( monitor. payment_preimages. len( ) , 12 ) ;
1306
+ test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
1307
+ test_preimages_exist ! ( & preimages[ 18 ..20 ] , monitor) ;
1308
+
1309
+ // But if we do it again, we'll prune 5-10
1310
+ monitor. provide_latest_local_commitment_tx_info ( dummy_tx. clone ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) ;
1311
+ secret[ 0 ..32 ] . clone_from_slice ( & hex_bytes ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
1312
+ monitor. provide_secret ( 281474976710652 , secret. clone ( ) , None ) . unwrap ( ) ;
1313
+ assert_eq ! ( monitor. payment_preimages. len( ) , 5 ) ;
1314
+ test_preimages_exist ! ( & preimages[ 0 ..5 ] , monitor) ;
1340
1315
}
1341
1316
1342
1317
// Further testing is done in the ChannelManager integration tests.
0 commit comments