@@ -11,6 +11,44 @@ use lightning::util::persist::KVStore;
1111
1212use std:: sync:: Arc ;
1313
14+ #[ test]
15+ fn persist_peer_from_inbound_channel ( ) {
16+ // Channel A open a channel to B,
17+ // B should persist A as a peer by adding there
18+ // node_id to peer_store
19+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
20+ println ! ( "== Node A ==" ) ;
21+ let esplora_url = format ! ( "http://{}" , electrsd. esplora_url. as_ref( ) . unwrap( ) ) ;
22+ let config_a = random_config ( ) ;
23+ let mut builder_a = NodeBuilder :: from_config ( config_a) ;
24+ builder_a. set_esplora_server ( esplora_url. clone ( ) ) ;
25+ let node_a = builder_a. build ( ) . unwrap ( ) ;
26+ node_a. start ( ) . unwrap ( ) ;
27+
28+ println ! ( "\n == Node B ==" ) ;
29+ let mut config_b = random_config ( ) ;
30+ config_b. trusted_peers_0conf = vec ! [ node_a. node_id( ) ] ;
31+ let mut builder_b = NodeBuilder :: from_config ( config_b. clone ( ) ) ;
32+ builder_b. set_esplora_server ( esplora_url. clone ( ) ) ;
33+ let node_b = builder_b. build ( ) . unwrap ( ) ;
34+ node_b. start ( ) . unwrap ( ) ;
35+ open_channel ( & node_a, & node_b, & bitcoind, & electrsd, true ) ;
36+ let node_b_id = node_b. node_id ( ) ;
37+ let node_a_id = node_a. node_id ( ) ;
38+ node_b. stop ( ) . unwrap ( ) ;
39+ assert_eq ! ( node_b. stop( ) , Err ( Error :: NotRunning ) ) ;
40+ node_b. start ( ) . unwrap ( ) ;
41+ node_b. sync_wallets ( ) . unwrap ( ) ;
42+ assert_eq ! ( node_b. is_running( ) , true ) ;
43+ assert_eq ! ( node_b. node_id( ) , node_b_id) ;
44+ assert_eq ! ( node_a. is_running( ) , true ) ;
45+ let node_b_peers = node_b. list_peers ( ) ;
46+ let node_b_peers = node_b_peers. get ( 0 ) . unwrap ( ) ;
47+ assert_eq ! ( node_b_peers. node_id, node_a_id) ;
48+ assert_eq ! ( node_b_peers. is_persisted, true ) ;
49+ assert_eq ! ( node_b_peers. is_connected, true ) ;
50+ }
51+
1452#[ test]
1553fn channel_full_cycle ( ) {
1654 let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
@@ -48,6 +86,90 @@ fn channel_full_cycle_0conf() {
4886 do_channel_full_cycle ( node_a, node_b, & bitcoind, & electrsd, true )
4987}
5088
89+ fn open_channel < K : KVStore + Sync + Send > (
90+ node_a : & Node < K > , node_b : & Node < K > , bitcoind : & BitcoinD , electrsd : & ElectrsD , allow_0conf : bool ,
91+ ) {
92+ let addr_a = node_a. new_onchain_address ( ) . unwrap ( ) ;
93+ let addr_b = node_b. new_onchain_address ( ) . unwrap ( ) ;
94+
95+ let premine_amount_sat = 100_000 ;
96+
97+ premine_and_distribute_funds (
98+ & bitcoind,
99+ & electrsd,
100+ vec ! [ addr_a, addr_b] ,
101+ Amount :: from_sat ( premine_amount_sat) ,
102+ ) ;
103+ node_a. sync_wallets ( ) . unwrap ( ) ;
104+ node_b. sync_wallets ( ) . unwrap ( ) ;
105+ assert_eq ! ( node_a. spendable_onchain_balance_sats( ) . unwrap( ) , premine_amount_sat) ;
106+ assert_eq ! ( node_b. spendable_onchain_balance_sats( ) . unwrap( ) , premine_amount_sat) ;
107+
108+ // Check we haven't got any events yet
109+ assert_eq ! ( node_a. next_event( ) , None ) ;
110+ assert_eq ! ( node_b. next_event( ) , None ) ;
111+
112+ println ! ( "\n A -- connect_open_channel -> B" ) ;
113+ let funding_amount_sat = 80_000 ;
114+ let push_msat = ( funding_amount_sat / 2 ) * 1000 ; // balance the channel
115+ node_a
116+ . connect_open_channel (
117+ node_b. node_id ( ) ,
118+ node_b. listening_address ( ) . unwrap ( ) . into ( ) ,
119+ funding_amount_sat,
120+ Some ( push_msat) ,
121+ None ,
122+ true ,
123+ )
124+ . unwrap ( ) ;
125+
126+ assert_eq ! ( node_a. list_peers( ) . first( ) . unwrap( ) . node_id, node_b. node_id( ) ) ;
127+ expect_event ! ( node_a, ChannelPending ) ;
128+
129+ let funding_txo = match node_b. wait_next_event ( ) {
130+ ref e @ Event :: ChannelPending { funding_txo, .. } => {
131+ println ! ( "{} got event {:?}" , std:: stringify!( node_b) , e) ;
132+ assert_eq ! ( node_b. next_event( ) . as_ref( ) , Some ( e) ) ;
133+ node_b. event_handled ( ) ;
134+ funding_txo
135+ }
136+ ref e => {
137+ panic ! ( "{} got unexpected event!: {:?}" , std:: stringify!( node_b) , e) ;
138+ }
139+ } ;
140+
141+ wait_for_tx ( & electrsd, funding_txo. txid ) ;
142+
143+ if !allow_0conf {
144+ println ! ( "\n .. generating blocks .." ) ;
145+ generate_blocks_and_wait ( & bitcoind, & electrsd, 6 ) ;
146+ }
147+
148+ node_a. sync_wallets ( ) . unwrap ( ) ;
149+ node_b. sync_wallets ( ) . unwrap ( ) ;
150+
151+ let onchain_fee_buffer_sat = 1500 ;
152+ let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
153+ let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
154+ assert ! ( node_a. spendable_onchain_balance_sats( ) . unwrap( ) < node_a_upper_bound_sat) ;
155+ assert ! ( node_a. spendable_onchain_balance_sats( ) . unwrap( ) > node_a_lower_bound_sat) ;
156+ assert_eq ! ( node_b. spendable_onchain_balance_sats( ) . unwrap( ) , premine_amount_sat) ;
157+
158+ expect_event ! ( node_a, ChannelReady ) ;
159+
160+ let ev = node_b. wait_next_event ( ) ;
161+ let _channel_id = match ev {
162+ ref e @ Event :: ChannelReady { ref channel_id, .. } => {
163+ println ! ( "{} got event {:?}" , std:: stringify!( node_b) , e) ;
164+ node_b. event_handled ( ) ;
165+ channel_id
166+ }
167+ ref e => {
168+ panic ! ( "{} got unexpected event!: {:?}" , std:: stringify!( node_b) , e) ;
169+ }
170+ } ;
171+ }
172+
51173fn do_channel_full_cycle < K : KVStore + Sync + Send > (
52174 node_a : Node < K > , node_b : Node < K > , bitcoind : & BitcoinD , electrsd : & ElectrsD , allow_0conf : bool ,
53175) {
0 commit comments