@ -694,140 +694,6 @@ func testPreimagePush(t *testing.T) {
require . NoError ( t , <- errChan )
require . NoError ( t , <- errChan )
}
}
// TestExpiryBeforeReveal tests the case where the on-chain HTLC expires before
// we have revealed our preimage, demonstrating that we do not reveal our
// preimage once we've reached our expiry height.
func TestExpiryBeforeReveal ( t * testing . T ) {
t . Run ( "stable protocol" , func ( t * testing . T ) {
testExpiryBeforeReveal ( t )
} )
// Note that there's no point of testing this case with the new
// protocol where we use taproot htlc and attempt MuSig2 sweep. The
// reason is that the preimage is revealed to the server once the
// htlc is confirmed in order to facilitate the cooperative signing of
// the sweep transaction.
}
func testExpiryBeforeReveal ( t * testing . T ) {
defer test . Guard ( t ) ( )
lnd := test . NewMockLnd ( )
ctx := test . NewContext ( t , lnd )
server := newServerMock ( lnd )
testReq := * testRequest
// Set on-chain HTLC CLTV.
testReq . Expiry = ctx . Lnd . Height + testLoopOutMinOnChainCltvDelta
// Set our fee estimate to higher than our max miner fee will allow.
lnd . SetFeeEstimate ( testReq . SweepConfTarget , chainfee . SatPerKWeight (
testReq . MaxMinerFee * 2 ,
) )
// Setup the cfg using mock server and init a loop out request.
cfg := newSwapConfig (
& lnd . LndServices , newStoreMock ( t ) , server ,
)
initResult , err := newLoopOutSwap (
context . Background ( ) , cfg , ctx . Lnd . Height , & testReq ,
)
require . NoError ( t , err )
swap := initResult . swap
// Set up the required dependencies to execute the swap.
sweeper := & sweep . Sweeper { Lnd : & lnd . LndServices }
blockEpochChan := make ( chan interface { } )
statusChan := make ( chan SwapInfo )
expiryChan := make ( chan time . Time )
timerFactory := func ( _ time . Duration ) <- chan time . Time {
return expiryChan
}
errChan := make ( chan error )
go func ( ) {
err := swap . execute ( context . Background ( ) , & executeConfig {
statusChan : statusChan ,
blockEpochChan : blockEpochChan ,
timerFactory : timerFactory ,
sweeper : sweeper ,
verifySchnorrSig : mockVerifySchnorrSigFail ,
} , ctx . Lnd . Height )
if err != nil {
log . Error ( err )
}
errChan <- err
} ( )
// The swap should be found in its initial state.
cfg . store . ( * storeMock ) . assertLoopOutStored ( )
state := <- statusChan
require . Equal ( t , loopdb . StateInitiated , state . State )
// We'll then pay both the swap and prepay invoice, which should trigger
// the server to publish the on-chain HTLC.
signalSwapPaymentResult := ctx . AssertPaid ( swapInvoiceDesc )
signalPrepaymentResult := ctx . AssertPaid ( prepayInvoiceDesc )
signalSwapPaymentResult ( nil )
signalPrepaymentResult ( nil )
// Notify the confirmation notification for the HTLC.
ctx . AssertRegisterConf ( false , defaultConfirmations )
// Advance the block height to get the HTLC confirmed.
height := ctx . Lnd . Height + 1
blockEpochChan <- height
htlcTx := wire . NewMsgTx ( 2 )
htlcTx . AddTxOut ( & wire . TxOut {
Value : int64 ( swap . AmountRequested ) ,
PkScript : swap . htlc . PkScript ,
} )
ctx . NotifyConf ( htlcTx )
// The client should then register for a spend of the HTLC and attempt
// to sweep it using the custom confirmation target.
ctx . AssertRegisterSpendNtfn ( swap . htlc . PkScript )
// Assert that we made a query to track our payment, as required for
// preimage push tracking.
ctx . AssertTrackPayment ( )
// Tick the expiry channel. Because our max miner fee is too high, we
// won't attempt a sweep at this point.
expiryChan <- testTime
// Since we don't have a reliable mechanism to non-intrusively avoid
// races by setting the fee estimate too soon, let's sleep here a bit
// to ensure the first sweep fails.
time . Sleep ( 500 * time . Millisecond )
// Now we decrease our conf target to less than our max miner fee.
lnd . SetFeeEstimate ( testReq . SweepConfTarget , chainfee . SatPerKWeight (
testReq . MaxMinerFee / 2 ,
) )
// Advance the block height to the point where we would do timeout
// instead of pushing the preimage.
blockEpochChan <- testReq . Expiry + height
// Tick our expiry channel again to trigger another sweep attempt.
expiryChan <- testTime
// We should see our swap marked as failed.
cfg . store . ( * storeMock ) . assertLoopOutState (
loopdb . StateFailTimeout ,
)
status := <- statusChan
require . Equal (
t , status . State , loopdb . StateFailTimeout ,
)
require . Nil ( t , <- errChan )
}
// TestFailedOffChainCancelation tests sending of a cancelation message to
// TestFailedOffChainCancelation tests sending of a cancelation message to
// the server when a swap fails due to off-chain routing.
// the server when a swap fails due to off-chain routing.
func TestFailedOffChainCancelation ( t * testing . T ) {
func TestFailedOffChainCancelation ( t * testing . T ) {