@@ -84,17 +84,7 @@ fn test_taproot_hashlock_recovery_end_to_end() {
8484 maker. wallet ( ) . write ( ) . unwrap ( ) . sync_and_save ( ) . unwrap ( ) ;
8585 }
8686
87- // Get balances before swap
88- let maker_balance_before = {
89- let wallet = taproot_makers[ 0 ] . wallet ( ) . read ( ) . unwrap ( ) ;
90- let balances = wallet. get_balances ( ) . unwrap ( ) ;
91- info ! (
92- "Maker balance before swap: Regular: {}, Spendable: {}" ,
93- balances. regular, balances. spendable
94- ) ;
95- balances. spendable
96- } ;
97-
87+ let actual_maker_spendable_balances = verify_maker_pre_swap_balance_taproot ( & taproot_makers) ;
9888 info ! ( "🔄 Initiating taproot coinswap (will fail mid-swap)..." ) ;
9989
10090 // Swap params - small amount for faster testing
@@ -150,70 +140,93 @@ fn test_taproot_hashlock_recovery_end_to_end() {
150140 generate_blocks ( bitcoind, 2 ) ;
151141 taproot_taker. get_wallet_mut ( ) . sync_and_save ( ) . unwrap ( ) ;
152142
153- info ! ( "📊 Taker balance after recovery:" ) ;
143+ info ! ( "📊 Taproot Taker balance after recovery:" ) ;
154144 let taker_balances_after = taproot_taker. get_wallet ( ) . get_balances ( ) . unwrap ( ) ;
155145 info ! (
156146 " Regular: {}, Contract: {}, Spendable: {}" ,
157147 taker_balances_after. regular, taker_balances_after. contract, taker_balances_after. spendable
158148 ) ;
159149
160- // Verify taker recovered their funds via hashlock
161- let max_taker_fees = Amount :: from_sat ( 100000 ) ;
162- assert ! (
163- taker_balances_after. spendable > taproot_taker_original_balance - max_taker_fees,
164- "Taker should have recovered via hashlock. Original: {}, After: {}, Lost: {}" ,
165- taproot_taker_original_balance,
166- taker_balances_after. spendable,
167- taproot_taker_original_balance - taker_balances_after. spendable
168- ) ;
169-
170150 // Now wait for maker to extract preimage and recover via hashlock
171151 info ! ( "⏳ Waiting for maker to extract preimage and recover via hashlock..." ) ;
172152 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 60 ) ) ;
173-
174153 // Mine more blocks to give maker time to see the hashlock sweep
175154 generate_blocks ( bitcoind, 2 ) ;
176155
177- // Wait a bit more for maker's recovery
178- std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 10 ) ) ;
179-
180- // Verify maker recovered their incoming contract via hashlock
181- let maker_balance_after = {
182- let mut wallet = taproot_makers[ 0 ] . wallet ( ) . write ( ) . unwrap ( ) ;
183- wallet. sync_and_save ( ) . unwrap ( ) ;
184- let balances = wallet. get_balances ( ) . unwrap ( ) ;
185- info ! (
186- "📊 Maker balance after hashlock recovery: Regular: {}, Spendable: {}" ,
187- balances. regular, balances. spendable
188- ) ;
189- balances. spendable
190- } ;
156+ // Verify swap results
157+ let taker_wallet = taproot_taker. get_wallet ( ) ;
158+ let taker_balances = taker_wallet. get_balances ( ) . unwrap ( ) ;
191159
192- // Maker should have recovered their outgoing contract via hashlock after extracting preimage
193- // They swept incoming (~500k sats) and should have it confirmed
194- let max_maker_fees = Amount :: from_sat ( 100000 ) ; // 0.001 BTC max fees
160+ // Use spendable balance (regular + swap) since swept coins from V2 swaps
161+ // are tracked as SweptCoinV2 and appear in swap balance
162+ // Here in hashlock recovery the spendable balance is almost similar to key-path spend
163+ // as the parties are completing their swap by claiming their incoming contract
164+ // via script-path spend.
165+ let taker_total_after = taker_balances. spendable ;
195166 assert ! (
196- maker_balance_after >= maker_balance_before - max_maker_fees,
197- "Maker should have recovered via hashlock. Before: {}, After: {}, Lost: {}" ,
198- maker_balance_before,
199- maker_balance_after,
200- maker_balance_before - maker_balance_after
167+ taker_total_after. to_sat( ) == 14944003 ,
168+ "Taproot Taker Balance check after hashlock recovery. Original: {}, After: {}" ,
169+ taproot_taker_original_balance,
170+ taker_total_after
201171 ) ;
202172
203- info ! ( "✅ Hashlock recovery test passed!" ) ;
204- info ! (
205- " Taker original balance: {}, Recovered: {}, Fees paid: {}" ,
173+ // But the taker should still have a reasonable amount left (not all spent on fees)
174+ let balance_diff = taproot_taker_original_balance - taker_total_after;
175+ assert ! (
176+ balance_diff. to_sat( ) == 55997 , // Hashlock recovery fee
177+ "Taproot Taker should have paid some fees. Original: {}, After: {},fees paid: {}" ,
206178 taproot_taker_original_balance,
207- taker_balances_after . spendable ,
208- taproot_taker_original_balance - taker_balances_after . spendable
179+ taker_total_after ,
180+ balance_diff
209181 ) ;
210182 info ! (
211- " Maker balance before : {}, After: {} (change : {})" ,
212- maker_balance_before ,
213- maker_balance_after ,
214- maker_balance_after . to_sat ( ) as i64 - maker_balance_before . to_sat ( ) as i64
183+ "Taproot Taker balance verification passed. Original spendable : {}, After spendable : {} (fees paid : {})" ,
184+ taproot_taker_original_balance ,
185+ taker_total_after ,
186+ balance_diff
215187 ) ;
216188
189+ // Verify makers earned fees
190+ for ( i, ( maker, original_spendable) ) in taproot_makers
191+ . iter ( )
192+ . zip ( actual_maker_spendable_balances)
193+ . enumerate ( )
194+ {
195+ let wallet = maker. wallet ( ) . read ( ) . unwrap ( ) ;
196+ let balances = wallet. get_balances ( ) . unwrap ( ) ;
197+
198+ info ! (
199+ "Taproot Maker {} final balances - Regular: {}, Swap: {}, Contract: {}, Fidelity: {}, Spendable: {}" ,
200+ i, balances. regular, balances. swap, balances. contract, balances. fidelity, balances. spendable
201+ ) ;
202+
203+ // Use spendable (regular + swap) for comparison
204+ // Here in hashlock recovery the spendable balance is almost similar to key-path spend
205+ // as the parties are completing their swap by claiming their incoming contract
206+ // via script-path spend.
207+ assert_in_range ! (
208+ balances. spendable. to_sat( ) ,
209+ [ 14999510 , 15020999 , 15032506 ] ,
210+ "Taproot Maker after hashlock recovery balance check."
211+ ) ;
212+
213+ let balance_diff = balances
214+ . spendable
215+ . to_sat ( )
216+ . saturating_sub ( original_spendable. to_sat ( ) ) ;
217+ // maker gained fee
218+ assert_in_range ! (
219+ balance_diff,
220+ [ 0 , 21489 , 32996 ] ,
221+ "Taproot Maker fee gained by recovering via hashlock"
222+ ) ;
223+
224+ info ! (
225+ "Taproot Maker {} balance verification passed. Original spendable: {}, Current spendable: {}, fee gained: {}" ,
226+ i, original_spendable, balances. spendable, balance_diff
227+ ) ;
228+ }
229+ info ! ( "✅ Hashlock recovery test passed!" ) ;
217230 // Shutdown maker
218231 taproot_makers
219232 . iter ( )
0 commit comments