@@ -434,6 +434,7 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
434
434
#[ cfg( not( any( test, feature = "_test_utils" ) ) ) ]
435
435
channel_state : Mutex < ChannelHolder < Signer > > ,
436
436
our_network_key : SecretKey ,
437
+ our_network_pubkey : PublicKey ,
437
438
438
439
/// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
439
440
/// value increases strictly since we don't assume access to a time source.
@@ -822,7 +823,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
822
823
823
824
latest_block_height : AtomicUsize :: new ( params. latest_height ) ,
824
825
last_block_hash : RwLock :: new ( params. latest_hash ) ,
825
- secp_ctx,
826
826
827
827
channel_state : Mutex :: new ( ChannelHolder {
828
828
by_id : HashMap :: new ( ) ,
@@ -832,6 +832,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
832
832
pending_msg_events : Vec :: new ( ) ,
833
833
} ) ,
834
834
our_network_key : keys_manager. get_node_secret ( ) ,
835
+ our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( ) ) ,
836
+ secp_ctx,
835
837
836
838
last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
837
839
@@ -2315,7 +2317,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2315
2317
2316
2318
/// Gets the node_id held by this ChannelManager
2317
2319
pub fn get_our_node_id ( & self ) -> PublicKey {
2318
- PublicKey :: from_secret_key ( & self . secp_ctx , & self . our_network_key )
2320
+ self . our_network_pubkey . clone ( )
2319
2321
}
2320
2322
2321
2323
/// Restores a single, given channel to normal operation after a
@@ -4318,7 +4320,6 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4318
4320
4319
4321
latest_block_height : AtomicUsize :: new ( latest_block_height as usize ) ,
4320
4322
last_block_hash : RwLock :: new ( last_block_hash) ,
4321
- secp_ctx,
4322
4323
4323
4324
channel_state : Mutex :: new ( ChannelHolder {
4324
4325
by_id,
@@ -4328,6 +4329,8 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4328
4329
pending_msg_events : Vec :: new ( ) ,
4329
4330
} ) ,
4330
4331
our_network_key : args. keys_manager . get_node_secret ( ) ,
4332
+ our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
4333
+ secp_ctx,
4331
4334
4332
4335
last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
4333
4336
@@ -4404,3 +4407,154 @@ mod tests {
4404
4407
}
4405
4408
}
4406
4409
}
4410
+
4411
+ #[ cfg( all( any( test, feature = "_test_utils" ) , feature = "unstable" ) ) ]
4412
+ pub mod bench {
4413
+ use chain:: Listen ;
4414
+ use chain:: chainmonitor:: ChainMonitor ;
4415
+ use chain:: channelmonitor:: Persist ;
4416
+ use chain:: keysinterface:: { KeysManager , InMemorySigner } ;
4417
+ use chain:: transaction:: OutPoint ;
4418
+ use ln:: channelmanager:: { ChainParameters , ChannelManager , PaymentHash , PaymentPreimage } ;
4419
+ use ln:: features:: InitFeatures ;
4420
+ use ln:: functional_test_utils:: * ;
4421
+ use ln:: msgs:: ChannelMessageHandler ;
4422
+ use routing:: network_graph:: NetworkGraph ;
4423
+ use routing:: router:: get_route;
4424
+ use util:: test_utils;
4425
+ use util:: config:: UserConfig ;
4426
+ use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
4427
+
4428
+ use bitcoin:: hashes:: Hash ;
4429
+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
4430
+ use bitcoin:: { Block , BlockHeader , Transaction , TxOut } ;
4431
+
4432
+ use std:: sync:: Mutex ;
4433
+
4434
+ use test:: Bencher ;
4435
+
4436
+ struct NodeHolder < ' a , P : Persist < InMemorySigner > > {
4437
+ node : & ' a ChannelManager < InMemorySigner ,
4438
+ & ' a ChainMonitor < InMemorySigner , & ' a test_utils:: TestChainSource ,
4439
+ & ' a test_utils:: TestBroadcaster , & ' a test_utils:: TestFeeEstimator ,
4440
+ & ' a test_utils:: TestLogger , & ' a P > ,
4441
+ & ' a test_utils:: TestBroadcaster , & ' a KeysManager ,
4442
+ & ' a test_utils:: TestFeeEstimator , & ' a test_utils:: TestLogger >
4443
+ }
4444
+
4445
+ #[ cfg( test) ]
4446
+ #[ bench]
4447
+ fn bench_sends ( bench : & mut Bencher ) {
4448
+ bench_two_sends ( bench, test_utils:: TestPersister :: new ( ) , test_utils:: TestPersister :: new ( ) ) ;
4449
+ }
4450
+
4451
+ pub fn bench_two_sends < P : Persist < InMemorySigner > > ( bench : & mut Bencher , persister_a : P , persister_b : P ) {
4452
+ // Do a simple benchmark of sending a payment back and forth between two nodes.
4453
+ // Note that this is unrealistic as each payment send will require at least two fsync
4454
+ // calls per node.
4455
+ let network = bitcoin:: Network :: Testnet ;
4456
+ let genesis_hash = bitcoin:: blockdata:: constants:: genesis_block ( network) . header . block_hash ( ) ;
4457
+
4458
+ let tx_broadcaster = test_utils:: TestBroadcaster { txn_broadcasted : Mutex :: new ( Vec :: new ( ) ) } ;
4459
+ let fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
4460
+
4461
+ let mut config: UserConfig = Default :: default ( ) ;
4462
+ config. own_channel_config . minimum_depth = 1 ;
4463
+
4464
+ let logger_a = test_utils:: TestLogger :: with_id ( "node a" . to_owned ( ) ) ;
4465
+ let chain_monitor_a = ChainMonitor :: new ( None , & tx_broadcaster, & logger_a, & fee_estimator, & persister_a) ;
4466
+ let seed_a = [ 1u8 ; 32 ] ;
4467
+ let keys_manager_a = KeysManager :: new ( & seed_a, 42 , 42 ) ;
4468
+ let node_a = ChannelManager :: new ( & fee_estimator, & chain_monitor_a, & tx_broadcaster, & logger_a, & keys_manager_a, config. clone ( ) , ChainParameters {
4469
+ network,
4470
+ latest_hash : genesis_hash,
4471
+ latest_height : 0 ,
4472
+ } ) ;
4473
+ let node_a_holder = NodeHolder { node : & node_a } ;
4474
+
4475
+ let logger_b = test_utils:: TestLogger :: with_id ( "node a" . to_owned ( ) ) ;
4476
+ let chain_monitor_b = ChainMonitor :: new ( None , & tx_broadcaster, & logger_a, & fee_estimator, & persister_b) ;
4477
+ let seed_b = [ 2u8 ; 32 ] ;
4478
+ let keys_manager_b = KeysManager :: new ( & seed_b, 42 , 42 ) ;
4479
+ let node_b = ChannelManager :: new ( & fee_estimator, & chain_monitor_b, & tx_broadcaster, & logger_b, & keys_manager_b, config. clone ( ) , ChainParameters {
4480
+ network,
4481
+ latest_hash : genesis_hash,
4482
+ latest_height : 0 ,
4483
+ } ) ;
4484
+ let node_b_holder = NodeHolder { node : & node_b } ;
4485
+
4486
+ node_a. create_channel ( node_b. get_our_node_id ( ) , 8_000_000 , 100_000_000 , 42 , None ) . unwrap ( ) ;
4487
+ node_b. handle_open_channel ( & node_a. get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( node_a_holder, MessageSendEvent :: SendOpenChannel , node_b. get_our_node_id( ) ) ) ;
4488
+ node_a. handle_accept_channel ( & node_b. get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( node_b_holder, MessageSendEvent :: SendAcceptChannel , node_a. get_our_node_id( ) ) ) ;
4489
+
4490
+ let tx;
4491
+ if let Event :: FundingGenerationReady { temporary_channel_id, output_script, .. } = get_event ! ( node_a_holder, Event :: FundingGenerationReady ) {
4492
+ tx = Transaction { version : 2 , lock_time : 0 , input : Vec :: new ( ) , output : vec ! [ TxOut {
4493
+ value: 8_000_000 , script_pubkey: output_script,
4494
+ } ] } ;
4495
+ let funding_outpoint = OutPoint { txid : tx. txid ( ) , index : 0 } ;
4496
+ node_a. funding_transaction_generated ( & temporary_channel_id, funding_outpoint) ;
4497
+ } else { panic ! ( ) ; }
4498
+
4499
+ node_b. handle_funding_created ( & node_a. get_our_node_id ( ) , & get_event_msg ! ( node_a_holder, MessageSendEvent :: SendFundingCreated , node_b. get_our_node_id( ) ) ) ;
4500
+ node_a. handle_funding_signed ( & node_b. get_our_node_id ( ) , & get_event_msg ! ( node_b_holder, MessageSendEvent :: SendFundingSigned , node_a. get_our_node_id( ) ) ) ;
4501
+
4502
+ get_event ! ( node_a_holder, Event :: FundingBroadcastSafe ) ;
4503
+
4504
+ let block = Block {
4505
+ header : BlockHeader { version : 0x20000000 , prev_blockhash : genesis_hash, merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ,
4506
+ txdata : vec ! [ tx] ,
4507
+ } ;
4508
+ Listen :: block_connected ( & node_a, & block, 1 ) ;
4509
+ Listen :: block_connected ( & node_b, & block, 1 ) ;
4510
+
4511
+ node_a. handle_funding_locked ( & node_b. get_our_node_id ( ) , & get_event_msg ! ( node_b_holder, MessageSendEvent :: SendFundingLocked , node_a. get_our_node_id( ) ) ) ;
4512
+ node_b. handle_funding_locked ( & node_a. get_our_node_id ( ) , & get_event_msg ! ( node_a_holder, MessageSendEvent :: SendFundingLocked , node_b. get_our_node_id( ) ) ) ;
4513
+
4514
+ let dummy_graph = NetworkGraph :: new ( genesis_hash) ;
4515
+
4516
+ macro_rules! send_payment {
4517
+ ( $node_a: expr, $node_b: expr) => {
4518
+ let usable_channels = $node_a. list_usable_channels( ) ;
4519
+ let route = get_route( & $node_a. get_our_node_id( ) , & dummy_graph, & $node_b. get_our_node_id( ) , None , Some ( & usable_channels. iter( ) . map( |r| r) . collect:: <Vec <_>>( ) ) , & [ ] , 10_000 , TEST_FINAL_CLTV , & logger_a) . unwrap( ) ;
4520
+
4521
+ let payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
4522
+ let payment_hash = PaymentHash ( Sha256 :: hash( & payment_preimage. 0 [ ..] ) . into_inner( ) ) ;
4523
+
4524
+ $node_a. send_payment( & route, payment_hash, & None ) . unwrap( ) ;
4525
+ let payment_event = SendEvent :: from_event( $node_a. get_and_clear_pending_msg_events( ) . pop( ) . unwrap( ) ) ;
4526
+ $node_b. handle_update_add_htlc( & $node_a. get_our_node_id( ) , & payment_event. msgs[ 0 ] ) ;
4527
+ $node_b. handle_commitment_signed( & $node_a. get_our_node_id( ) , & payment_event. commitment_msg) ;
4528
+ let ( raa, cs) = get_revoke_commit_msgs!( NodeHolder { node: & $node_b } , $node_a. get_our_node_id( ) ) ;
4529
+ $node_a. handle_revoke_and_ack( & $node_b. get_our_node_id( ) , & raa) ;
4530
+ $node_a. handle_commitment_signed( & $node_b. get_our_node_id( ) , & cs) ;
4531
+ $node_b. handle_revoke_and_ack( & $node_a. get_our_node_id( ) , & get_event_msg!( NodeHolder { node: & $node_a } , MessageSendEvent :: SendRevokeAndACK , $node_b. get_our_node_id( ) ) ) ;
4532
+
4533
+ expect_pending_htlcs_forwardable!( NodeHolder { node: & $node_b } ) ;
4534
+ expect_payment_received!( NodeHolder { node: & $node_b } , payment_hash, 10_000 ) ;
4535
+ assert!( $node_b. claim_funds( payment_preimage, & None , 10_000 ) ) ;
4536
+
4537
+ match $node_b. get_and_clear_pending_msg_events( ) . pop( ) . unwrap( ) {
4538
+ MessageSendEvent :: UpdateHTLCs { node_id, updates } => {
4539
+ assert_eq!( node_id, $node_a. get_our_node_id( ) ) ;
4540
+ $node_a. handle_update_fulfill_htlc( & $node_b. get_our_node_id( ) , & updates. update_fulfill_htlcs[ 0 ] ) ;
4541
+ $node_a. handle_commitment_signed( & $node_b. get_our_node_id( ) , & updates. commitment_signed) ;
4542
+ } ,
4543
+ _ => panic!( "Failed to generate claim event" ) ,
4544
+ }
4545
+
4546
+ let ( raa, cs) = get_revoke_commit_msgs!( NodeHolder { node: & $node_a } , $node_b. get_our_node_id( ) ) ;
4547
+ $node_b. handle_revoke_and_ack( & $node_a. get_our_node_id( ) , & raa) ;
4548
+ $node_b. handle_commitment_signed( & $node_a. get_our_node_id( ) , & cs) ;
4549
+ $node_a. handle_revoke_and_ack( & $node_b. get_our_node_id( ) , & get_event_msg!( NodeHolder { node: & $node_b } , MessageSendEvent :: SendRevokeAndACK , $node_a. get_our_node_id( ) ) ) ;
4550
+
4551
+ expect_payment_sent!( NodeHolder { node: & $node_a } , payment_preimage) ;
4552
+ }
4553
+ }
4554
+
4555
+ bench. iter ( || {
4556
+ send_payment ! ( node_a, node_b) ;
4557
+ send_payment ! ( node_b, node_a) ;
4558
+ } ) ;
4559
+ }
4560
+ }
0 commit comments