11use crate :: builder:: NodeBuilder ;
22use crate :: io:: test_utils:: TestSyncStore ;
33use crate :: test:: utils:: * ;
4- use crate :: test:: utils:: { expect_channel_pending_event, expect_event, open_channel, random_config} ;
4+ use crate :: test:: utils:: {
5+ expect_channel_pending_event, expect_channel_ready_event, expect_event, open_channel,
6+ random_config,
7+ } ;
58use crate :: { Error , Event , Node , PaymentDirection , PaymentStatus } ;
69
710use bitcoin:: Amount ;
@@ -11,6 +14,53 @@ use lightning::util::persist::KVStore;
1114
1215use std:: sync:: Arc ;
1316
17+ #[ test]
18+ fn persist_peer_from_inbound_channel ( ) {
19+ // Channel A open a channel to B,
20+ // B should persist A as a peer by adding there
21+ // node_id to peer_store
22+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
23+ let funding_amount_sat = 80_000 ;
24+ let ( node_a, node_b) = setup_two_nodes ( & electrsd, false ) ;
25+ let addr_a = node_a. new_onchain_address ( ) . unwrap ( ) ;
26+ let addr_b = node_b. new_onchain_address ( ) . unwrap ( ) ;
27+
28+ let premine_amount_sat = 100_000 ;
29+
30+ premine_and_distribute_funds (
31+ & bitcoind,
32+ & electrsd,
33+ vec ! [ addr_a, addr_b] ,
34+ Amount :: from_sat ( premine_amount_sat) ,
35+ ) ;
36+ node_a. sync_wallets ( ) . unwrap ( ) ;
37+ node_b. sync_wallets ( ) . unwrap ( ) ;
38+ assert_eq ! ( node_a. spendable_onchain_balance_sats( ) . unwrap( ) , premine_amount_sat) ;
39+ assert_eq ! ( node_b. spendable_onchain_balance_sats( ) . unwrap( ) , premine_amount_sat) ;
40+ assert_eq ! ( node_a. next_event( ) , None ) ;
41+ assert_eq ! ( node_b. next_event( ) , None ) ;
42+ open_channel ( & node_a, & node_b, funding_amount_sat, true , & electrsd) ;
43+ generate_blocks_and_wait ( & bitcoind, & electrsd, 6 ) ;
44+ node_a. sync_wallets ( ) . unwrap ( ) ;
45+ node_b. sync_wallets ( ) . unwrap ( ) ;
46+ expect_channel_ready_event ! ( & node_a, Some ( node_b. node_id( ) ) ) ;
47+ expect_channel_ready_event ! ( & node_b, Some ( node_a. node_id( ) ) ) ;
48+ let node_b_id = node_b. node_id ( ) ;
49+ let node_a_id = node_a. node_id ( ) ;
50+ node_b. stop ( ) . unwrap ( ) ;
51+ assert_eq ! ( node_b. stop( ) , Err ( Error :: NotRunning ) ) ;
52+ node_b. start ( ) . unwrap ( ) ;
53+ node_b. sync_wallets ( ) . unwrap ( ) ;
54+ assert_eq ! ( node_b. is_running( ) , true ) ;
55+ assert_eq ! ( node_b. node_id( ) , node_b_id) ;
56+ assert_eq ! ( node_a. is_running( ) , true ) ;
57+ let node_b_peers = node_b. list_peers ( ) ;
58+ let node_b_peers = node_b_peers. get ( 0 ) . expect ( "Should have one peer" ) ;
59+ assert_eq ! ( node_b_peers. node_id, node_a_id) ;
60+ assert_eq ! ( node_b_peers. is_persisted, true ) ;
61+ assert_eq ! ( node_b_peers. is_connected, true ) ;
62+ }
63+
1464#[ test]
1565fn channel_full_cycle ( ) {
1666 let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
0 commit comments