@@ -20,20 +20,24 @@ fn channel_full_cycle() {
20
20
node_b. start ( ) . unwrap ( ) ;
21
21
let addr_b = node_b. new_funding_address ( ) . unwrap ( ) ;
22
22
23
+ let premine_amount_sat = 100_000 ;
24
+
23
25
premine_and_distribute_funds (
24
26
& bitcoind,
25
27
& electrsd,
26
28
vec ! [ addr_a, addr_b] ,
27
- Amount :: from_sat ( 100000 ) ,
29
+ Amount :: from_sat ( premine_amount_sat ) ,
28
30
) ;
29
31
node_a. sync_wallets ( ) . unwrap ( ) ;
30
32
node_b. sync_wallets ( ) . unwrap ( ) ;
31
- assert_eq ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
32
- assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
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
35
34
36
println ! ( "\n A -- connect_open_channel -> B" ) ;
37
+ let funding_amount_sat = 80_000 ;
38
+ let push_msat = ( funding_amount_sat / 2 ) * 1000 ; // balance the channel
35
39
let node_b_addr = format ! ( "{}@{}" , node_b. node_id( ) , node_b. listening_address( ) . unwrap( ) ) ;
36
- node_a. connect_open_channel ( & node_b_addr, 50000 , true ) . unwrap ( ) ;
40
+ node_a. connect_open_channel ( & node_b_addr, funding_amount_sat , Some ( push_msat ) , true ) . unwrap ( ) ;
37
41
38
42
expect_event ! ( node_a, ChannelPending ) ;
39
43
@@ -55,10 +59,13 @@ fn channel_full_cycle() {
55
59
node_a. sync_wallets ( ) . unwrap ( ) ;
56
60
node_b. sync_wallets ( ) . unwrap ( ) ;
57
61
62
+ let onchain_fee_buffer_sat = 1500 ;
58
63
let node_a_balance = node_a. on_chain_balance ( ) . unwrap ( ) ;
59
- assert ! ( node_a_balance. get_spendable( ) < 50000 ) ;
60
- assert ! ( node_a_balance. get_spendable( ) > 40000 ) ;
61
- assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
64
+ let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
65
+ let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
66
+ assert ! ( node_a_balance. get_spendable( ) < node_a_upper_bound_sat) ;
67
+ assert ! ( node_a_balance. get_spendable( ) > node_a_lower_bound_sat) ;
68
+ assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , premine_amount_sat) ;
62
69
63
70
expect_event ! ( node_a, ChannelReady ) ;
64
71
@@ -74,8 +81,8 @@ fn channel_full_cycle() {
74
81
} ;
75
82
76
83
println ! ( "\n B receive_payment" ) ;
77
- let invoice_amount = 1000000 ;
78
- let invoice = node_b. receive_payment ( invoice_amount , & "asdf" , 9217 ) . unwrap ( ) ;
84
+ let invoice_amount_1_msat = 1000000 ;
85
+ let invoice = node_b. receive_payment ( invoice_amount_1_msat , & "asdf" , 9217 ) . unwrap ( ) ;
79
86
80
87
println ! ( "\n A send_payment" ) ;
81
88
let payment_hash = node_a. send_payment ( invoice. clone ( ) ) . unwrap ( ) ;
@@ -100,27 +107,27 @@ fn channel_full_cycle() {
100
107
expect_event ! ( node_b, PaymentReceived ) ;
101
108
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
102
109
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Outbound ) ;
103
- assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( invoice_amount ) ) ;
110
+ assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( invoice_amount_1_msat ) ) ;
104
111
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
105
112
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Inbound ) ;
106
- assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( invoice_amount ) ) ;
113
+ assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( invoice_amount_1_msat ) ) ;
107
114
108
115
// Assert we fail duplicate outbound payments.
109
116
assert_eq ! ( Err ( Error :: NonUniquePaymentHash ) , node_a. send_payment( invoice) ) ;
110
117
111
118
// Test under-/overpayment
112
- let invoice_amount = 1000000 ;
113
- let invoice = node_b. receive_payment ( invoice_amount , & "asdf" , 9217 ) . unwrap ( ) ;
119
+ let invoice_amount_2_msat = 1000_000 ;
120
+ let invoice = node_b. receive_payment ( invoice_amount_2_msat , & "asdf" , 9217 ) . unwrap ( ) ;
114
121
115
- let underpaid_amount = invoice_amount - 1 ;
122
+ let underpaid_amount = invoice_amount_2_msat - 1 ;
116
123
assert_eq ! (
117
124
Err ( Error :: InvalidAmount ) ,
118
125
node_a. send_payment_using_amount( invoice, underpaid_amount)
119
126
) ;
120
127
121
- let invoice = node_b. receive_payment ( invoice_amount , & "asdf" , 9217 ) . unwrap ( ) ;
122
- let overpaid_amount = invoice_amount + 100 ;
123
- let payment_hash = node_a. send_payment_using_amount ( invoice, overpaid_amount ) . unwrap ( ) ;
128
+ let invoice = node_b. receive_payment ( invoice_amount_2_msat , & "asdf" , 9217 ) . unwrap ( ) ;
129
+ let overpaid_amount_msat = invoice_amount_2_msat + 100 ;
130
+ let payment_hash = node_a. send_payment_using_amount ( invoice, overpaid_amount_msat ) . unwrap ( ) ;
124
131
expect_event ! ( node_a, PaymentSuccessful ) ;
125
132
let received_amount = match node_b. next_event ( ) {
126
133
ref e @ Event :: PaymentReceived { amount_msat, .. } => {
@@ -132,20 +139,20 @@ fn channel_full_cycle() {
132
139
panic ! ( "{} got unexpected event!: {:?}" , std:: stringify!( node_b) , e) ;
133
140
}
134
141
} ;
135
- assert_eq ! ( received_amount, overpaid_amount ) ;
142
+ assert_eq ! ( received_amount, overpaid_amount_msat ) ;
136
143
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
137
144
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Outbound ) ;
138
- assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( overpaid_amount ) ) ;
145
+ assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( overpaid_amount_msat ) ) ;
139
146
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
140
147
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Inbound ) ;
141
- assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( overpaid_amount ) ) ;
148
+ assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( overpaid_amount_msat ) ) ;
142
149
143
150
// Test "zero-amount" invoice payment
144
151
let variable_amount_invoice = node_b. receive_variable_amount_payment ( & "asdf" , 9217 ) . unwrap ( ) ;
145
- let determined_amount = 1234567 ;
152
+ let determined_amount_msat = 1234_567 ;
146
153
assert_eq ! ( Err ( Error :: InvalidInvoice ) , node_a. send_payment( variable_amount_invoice. clone( ) ) ) ;
147
154
let payment_hash =
148
- node_a. send_payment_using_amount ( variable_amount_invoice, determined_amount ) . unwrap ( ) ;
155
+ node_a. send_payment_using_amount ( variable_amount_invoice, determined_amount_msat ) . unwrap ( ) ;
149
156
150
157
expect_event ! ( node_a, PaymentSuccessful ) ;
151
158
let received_amount = match node_b. next_event ( ) {
@@ -158,13 +165,13 @@ fn channel_full_cycle() {
158
165
panic ! ( "{} got unexpected event!: {:?}" , std:: stringify!( node_b) , e) ;
159
166
}
160
167
} ;
161
- assert_eq ! ( received_amount, determined_amount ) ;
168
+ assert_eq ! ( received_amount, determined_amount_msat ) ;
162
169
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
163
170
assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Outbound ) ;
164
- assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( determined_amount ) ) ;
171
+ assert_eq ! ( node_a. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( determined_amount_msat ) ) ;
165
172
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
166
173
assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . direction, PaymentDirection :: Inbound ) ;
167
- assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( determined_amount ) ) ;
174
+ assert_eq ! ( node_b. payment( & payment_hash) . unwrap( ) . amount_msat, Some ( determined_amount_msat ) ) ;
168
175
169
176
node_b. close_channel ( & channel_id, & node_a. node_id ( ) ) . unwrap ( ) ;
170
177
expect_event ! ( node_a, ChannelClosed ) ;
@@ -176,8 +183,18 @@ fn channel_full_cycle() {
176
183
node_a. sync_wallets ( ) . unwrap ( ) ;
177
184
node_b. sync_wallets ( ) . unwrap ( ) ;
178
185
179
- assert ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) > 90000 ) ;
180
- assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 103234 ) ;
186
+ let sum_of_all_payments_sat =
187
+ ( push_msat + invoice_amount_1_msat + overpaid_amount_msat + determined_amount_msat) / 1000 ;
188
+ let node_a_upper_bound_sat =
189
+ ( premine_amount_sat - funding_amount_sat) + ( funding_amount_sat - sum_of_all_payments_sat) ;
190
+ let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
191
+ assert ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) > node_a_lower_bound_sat) ;
192
+ assert ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) < node_a_upper_bound_sat) ;
193
+ let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
194
+ assert_eq ! (
195
+ node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) ,
196
+ expected_final_amount_node_b_sat
197
+ ) ;
181
198
182
199
node_a. stop ( ) . unwrap ( ) ;
183
200
println ! ( "\n A stopped" ) ;
@@ -201,22 +218,24 @@ fn channel_open_fails_when_funds_insufficient() {
201
218
node_b. start ( ) . unwrap ( ) ;
202
219
let addr_b = node_b. new_funding_address ( ) . unwrap ( ) ;
203
220
221
+ let premine_amount_sat = 100_000 ;
222
+
204
223
premine_and_distribute_funds (
205
224
& bitcoind,
206
225
& electrsd,
207
226
vec ! [ addr_a, addr_b] ,
208
- Amount :: from_sat ( 100000 ) ,
227
+ Amount :: from_sat ( premine_amount_sat ) ,
209
228
) ;
210
229
node_a. sync_wallets ( ) . unwrap ( ) ;
211
230
node_b. sync_wallets ( ) . unwrap ( ) ;
212
- assert_eq ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
213
- assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , 100000 ) ;
231
+ assert_eq ! ( node_a. on_chain_balance( ) . unwrap( ) . get_spendable( ) , premine_amount_sat ) ;
232
+ assert_eq ! ( node_b. on_chain_balance( ) . unwrap( ) . get_spendable( ) , premine_amount_sat ) ;
214
233
215
234
println ! ( "\n A -- connect_open_channel -> B" ) ;
216
235
let node_b_addr = format ! ( "{}@{}" , node_b. node_id( ) , node_b. listening_address( ) . unwrap( ) ) ;
217
236
assert_eq ! (
218
237
Err ( Error :: InsufficientFunds ) ,
219
- node_a. connect_open_channel( & node_b_addr, 120000 , true )
238
+ node_a. connect_open_channel( & node_b_addr, 120000 , None , true )
220
239
) ;
221
240
}
222
241
0 commit comments