@@ -30,8 +30,8 @@ fn channel_full_cycle() {
30
30
) ;
31
31
node_a. sync_wallets ( ) . unwrap ( ) ;
32
32
node_b. sync_wallets ( ) . unwrap ( ) ;
33
- assert_eq ! ( node_a. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
34
- assert_eq ! ( node_b. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
33
+ assert_eq ! ( node_a. onchain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
34
+ assert_eq ! ( node_b. onchain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
35
35
36
36
println ! ( "\n A -- connect_open_channel -> B" ) ;
37
37
let funding_amount_sat = 80_000 ;
@@ -67,12 +67,12 @@ fn channel_full_cycle() {
67
67
node_b. sync_wallets ( ) . unwrap ( ) ;
68
68
69
69
let onchain_fee_buffer_sat = 1500 ;
70
- let node_a_balance = node_a. on_chain_balance ( ) . unwrap ( ) ;
70
+ let node_a_balance = node_a. onchain_balance ( ) . unwrap ( ) ;
71
71
let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
72
72
let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
73
73
assert ! ( node_a_balance. get_spendable( ) < node_a_upper_bound_sat) ;
74
74
assert ! ( node_a_balance. get_spendable( ) > node_a_lower_bound_sat) ;
75
- assert_eq ! ( node_b. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
75
+ assert_eq ! ( node_b. onchain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
76
76
77
77
expect_event ! ( node_a, ChannelReady ) ;
78
78
@@ -195,13 +195,10 @@ fn channel_full_cycle() {
195
195
let node_a_upper_bound_sat =
196
196
( premine_amount_sat - funding_amount_sat) + ( funding_amount_sat - sum_of_all_payments_sat) ;
197
197
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
198
- assert ! ( node_a. on_chain_balance ( ) . unwrap( ) . get_spendable( ) > node_a_lower_bound_sat) ;
199
- assert ! ( node_a. on_chain_balance ( ) . unwrap( ) . get_spendable( ) < node_a_upper_bound_sat) ;
198
+ assert ! ( node_a. onchain_balance ( ) . unwrap( ) . get_spendable( ) > node_a_lower_bound_sat) ;
199
+ assert ! ( node_a. onchain_balance ( ) . unwrap( ) . get_spendable( ) < node_a_upper_bound_sat) ;
200
200
let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
201
- assert_eq ! (
202
- node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) ,
203
- expected_final_amount_node_b_sat
204
- ) ;
201
+ assert_eq ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) , expected_final_amount_node_b_sat) ;
205
202
206
203
node_a. stop ( ) . unwrap ( ) ;
207
204
println ! ( "\n A stopped" ) ;
@@ -235,8 +232,8 @@ fn channel_open_fails_when_funds_insufficient() {
235
232
) ;
236
233
node_a. sync_wallets ( ) . unwrap ( ) ;
237
234
node_b. sync_wallets ( ) . unwrap ( ) ;
238
- assert_eq ! ( node_a. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
239
- assert_eq ! ( node_b. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
235
+ assert_eq ! ( node_a. onchain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
236
+ assert_eq ! ( node_b. onchain_balance ( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
240
237
241
238
println ! ( "\n A -- connect_open_channel -> B" ) ;
242
239
assert_eq ! (
@@ -276,13 +273,13 @@ fn start_stop_reinit() {
276
273
let expected_amount = Amount :: from_sat ( 100000 ) ;
277
274
278
275
premine_and_distribute_funds ( & bitcoind, & electrsd, vec ! [ funding_address] , expected_amount) ;
279
- assert_eq ! ( node. on_chain_balance ( ) . unwrap( ) . get_total( ) , 0 ) ;
276
+ assert_eq ! ( node. onchain_balance ( ) . unwrap( ) . get_total( ) , 0 ) ;
280
277
281
278
node. start ( ) . unwrap ( ) ;
282
279
assert_eq ! ( node. start( ) , Err ( Error :: AlreadyRunning ) ) ;
283
280
284
281
node. sync_wallets ( ) . unwrap ( ) ;
285
- assert_eq ! ( node. on_chain_balance ( ) . unwrap( ) . get_spendable( ) , expected_amount. to_sat( ) ) ;
282
+ assert_eq ! ( node. onchain_balance ( ) . unwrap( ) . get_spendable( ) , expected_amount. to_sat( ) ) ;
286
283
287
284
node. stop ( ) . unwrap ( ) ;
288
285
assert_eq ! ( node. stop( ) , Err ( Error :: NotRunning ) ) ;
@@ -300,15 +297,67 @@ fn start_stop_reinit() {
300
297
reinitialized_node. start ( ) . unwrap ( ) ;
301
298
302
299
assert_eq ! (
303
- reinitialized_node. on_chain_balance ( ) . unwrap( ) . get_spendable( ) ,
300
+ reinitialized_node. onchain_balance ( ) . unwrap( ) . get_spendable( ) ,
304
301
expected_amount. to_sat( )
305
302
) ;
306
303
307
304
reinitialized_node. sync_wallets ( ) . unwrap ( ) ;
308
305
assert_eq ! (
309
- reinitialized_node. on_chain_balance ( ) . unwrap( ) . get_spendable( ) ,
306
+ reinitialized_node. onchain_balance ( ) . unwrap( ) . get_spendable( ) ,
310
307
expected_amount. to_sat( )
311
308
) ;
312
309
313
310
reinitialized_node. stop ( ) . unwrap ( ) ;
314
311
}
312
+
313
+ #[ test]
314
+ fn onchain_spend_receive ( ) {
315
+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
316
+ let esplora_url = electrsd. esplora_url . as_ref ( ) . unwrap ( ) ;
317
+
318
+ let config_a = random_config ( esplora_url) ;
319
+ let node_a = Builder :: from_config ( config_a) . build ( ) ;
320
+ node_a. start ( ) . unwrap ( ) ;
321
+ let addr_a = node_a. new_funding_address ( ) . unwrap ( ) ;
322
+
323
+ let config_b = random_config ( esplora_url) ;
324
+ let node_b = Builder :: from_config ( config_b) . build ( ) ;
325
+ node_b. start ( ) . unwrap ( ) ;
326
+ let addr_b = node_b. new_funding_address ( ) . unwrap ( ) ;
327
+
328
+ premine_and_distribute_funds (
329
+ & bitcoind,
330
+ & electrsd,
331
+ vec ! [ addr_b. clone( ) ] ,
332
+ Amount :: from_sat ( 100000 ) ,
333
+ ) ;
334
+
335
+ node_a. sync_wallets ( ) . unwrap ( ) ;
336
+ node_b. sync_wallets ( ) . unwrap ( ) ;
337
+ assert_eq ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
338
+
339
+ assert_eq ! ( Err ( Error :: InsufficientFunds ) , node_a. send_to_onchain_address( & addr_b, 1000 ) ) ;
340
+
341
+ let txid = node_b. send_to_onchain_address ( & addr_a, 1000 ) . unwrap ( ) ;
342
+ generate_blocks_and_wait ( & bitcoind, & electrsd, 6 ) ;
343
+ wait_for_tx ( & electrsd, txid) ;
344
+
345
+ node_a. sync_wallets ( ) . unwrap ( ) ;
346
+ node_b. sync_wallets ( ) . unwrap ( ) ;
347
+
348
+ assert_eq ! ( node_a. onchain_balance( ) . unwrap( ) . get_spendable( ) , 1000 ) ;
349
+ assert ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) > 98000 ) ;
350
+ assert ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) < 100000 ) ;
351
+
352
+ let addr_b = node_b. new_funding_address ( ) . unwrap ( ) ;
353
+ let txid = node_a. send_all_to_onchain_address ( & addr_b) . unwrap ( ) ;
354
+ generate_blocks_and_wait ( & bitcoind, & electrsd, 6 ) ;
355
+ wait_for_tx ( & electrsd, txid) ;
356
+
357
+ node_a. sync_wallets ( ) . unwrap ( ) ;
358
+ node_b. sync_wallets ( ) . unwrap ( ) ;
359
+
360
+ assert_eq ! ( node_a. onchain_balance( ) . unwrap( ) . get_total( ) , 0 ) ;
361
+ assert ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) > 99000 ) ;
362
+ assert ! ( node_b. onchain_balance( ) . unwrap( ) . get_spendable( ) < 100000 ) ;
363
+ }
0 commit comments