|
| 1 | +package loop |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/btcutil" |
| 9 | + "github.com/lightninglabs/lndclient" |
| 10 | + "github.com/lightninglabs/loop/loopdb" |
| 11 | + "github.com/lightninglabs/loop/test" |
| 12 | + "github.com/lightningnetwork/lnd/lntypes" |
| 13 | + "github.com/lightningnetwork/lnd/lnwire" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// TestCalculateLoopOutCost tests the CalculateLoopOutCost function. |
| 18 | +func TestCalculateLoopOutCost(t *testing.T) { |
| 19 | + // Set up test context objects. |
| 20 | + lnd := test.NewMockLnd() |
| 21 | + server := newServerMock(lnd) |
| 22 | + store := loopdb.NewStoreMock(t) |
| 23 | + |
| 24 | + cfg := &swapConfig{ |
| 25 | + lnd: &lnd.LndServices, |
| 26 | + store: store, |
| 27 | + server: server, |
| 28 | + } |
| 29 | + |
| 30 | + height := int32(600) |
| 31 | + req := *testRequest |
| 32 | + initResult, err := newLoopOutSwap( |
| 33 | + context.Background(), cfg, height, &req, |
| 34 | + ) |
| 35 | + require.NoError(t, err) |
| 36 | + swap, err := store.FetchLoopOutSwap( |
| 37 | + context.Background(), initResult.swap.hash, |
| 38 | + ) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + // Override the chain cost so it's negative. |
| 42 | + const expectedChainCost = btcutil.Amount(1000) |
| 43 | + |
| 44 | + // Now we have the swap and prepay invoices so let's calculate the |
| 45 | + // costs without providing the payments first, so we don't account for |
| 46 | + // any routing fees. |
| 47 | + paymentFees := make(map[lntypes.Hash]lnwire.MilliSatoshi) |
| 48 | + _, err = CalculateLoopOutCost(lnd.ChainParams, swap, paymentFees) |
| 49 | + |
| 50 | + // We expect that the call fails as the swap isn't finished yet. |
| 51 | + require.Error(t, err) |
| 52 | + |
| 53 | + // Override the swap state to make it look like the swap is finished |
| 54 | + // and make the chain cost negative too, so we can test that it'll be |
| 55 | + // corrected to be positive in the cost calculation. |
| 56 | + swap.Events = append( |
| 57 | + swap.Events, &loopdb.LoopEvent{ |
| 58 | + SwapStateData: loopdb.SwapStateData{ |
| 59 | + State: loopdb.StateSuccess, |
| 60 | + Cost: loopdb.SwapCost{ |
| 61 | + Onchain: -expectedChainCost, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ) |
| 66 | + costs, err := CalculateLoopOutCost(lnd.ChainParams, swap, paymentFees) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + expectedServerCost := server.swapInvoiceAmt + server.prepayInvoiceAmt - |
| 70 | + swap.Contract.AmountRequested |
| 71 | + require.Equal(t, expectedServerCost, costs.Server) |
| 72 | + require.Equal(t, btcutil.Amount(0), costs.Offchain) |
| 73 | + require.Equal(t, expectedChainCost, costs.Onchain) |
| 74 | + |
| 75 | + // Now add the two payments to the payments map and calculate the costs |
| 76 | + // again. We expect that the routng fees are now accounted for. |
| 77 | + paymentFees[server.swapHash] = lnwire.NewMSatFromSatoshis(44) |
| 78 | + paymentFees[server.prepayHash] = lnwire.NewMSatFromSatoshis(11) |
| 79 | + |
| 80 | + costs, err = CalculateLoopOutCost(lnd.ChainParams, swap, paymentFees) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + expectedOffchainCost := btcutil.Amount(44 + 11) |
| 84 | + require.Equal(t, expectedServerCost, costs.Server) |
| 85 | + require.Equal(t, expectedOffchainCost, costs.Offchain) |
| 86 | + require.Equal(t, expectedChainCost, costs.Onchain) |
| 87 | + |
| 88 | + // Now override the last update to make the swap timed out at the HTLC |
| 89 | + // sweep. We expect that the chain cost won't change, and only the |
| 90 | + // prepay will be accounted for. |
| 91 | + swap.Events[0] = &loopdb.LoopEvent{ |
| 92 | + SwapStateData: loopdb.SwapStateData{ |
| 93 | + State: loopdb.StateFailSweepTimeout, |
| 94 | + Cost: loopdb.SwapCost{ |
| 95 | + Onchain: 0, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + costs, err = CalculateLoopOutCost(lnd.ChainParams, swap, paymentFees) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + expectedServerCost = server.prepayInvoiceAmt |
| 104 | + expectedOffchainCost = btcutil.Amount(11) |
| 105 | + require.Equal(t, expectedServerCost, costs.Server) |
| 106 | + require.Equal(t, expectedOffchainCost, costs.Offchain) |
| 107 | + require.Equal(t, btcutil.Amount(0), costs.Onchain) |
| 108 | +} |
| 109 | + |
| 110 | +// TestCostMigration tests the cost migration for loop out swaps. |
| 111 | +func TestCostMigration(t *testing.T) { |
| 112 | + // Set up test context objects. |
| 113 | + lnd := test.NewMockLnd() |
| 114 | + server := newServerMock(lnd) |
| 115 | + store := loopdb.NewStoreMock(t) |
| 116 | + |
| 117 | + cfg := &swapConfig{ |
| 118 | + lnd: &lnd.LndServices, |
| 119 | + store: store, |
| 120 | + server: server, |
| 121 | + } |
| 122 | + |
| 123 | + height := int32(600) |
| 124 | + req := *testRequest |
| 125 | + initResult, err := newLoopOutSwap( |
| 126 | + context.Background(), cfg, height, &req, |
| 127 | + ) |
| 128 | + require.NoError(t, err) |
| 129 | + |
| 130 | + // Override the chain cost so it's negative. |
| 131 | + const expectedChainCost = btcutil.Amount(1000) |
| 132 | + |
| 133 | + // Override the swap state to make it look like the swap is finished |
| 134 | + // and make the chain cost negative too, so we can test that it'll be |
| 135 | + // corrected to be positive in the cost calculation. |
| 136 | + err = store.UpdateLoopOut( |
| 137 | + context.Background(), initResult.swap.hash, time.Now(), |
| 138 | + loopdb.SwapStateData{ |
| 139 | + State: loopdb.StateSuccess, |
| 140 | + Cost: loopdb.SwapCost{ |
| 141 | + Onchain: -expectedChainCost, |
| 142 | + }, |
| 143 | + }, |
| 144 | + ) |
| 145 | + require.NoError(t, err) |
| 146 | + |
| 147 | + // Add the two mocked payment to LND. Note that we only care about the |
| 148 | + // fees here, so we don't need to provide the full payment details. |
| 149 | + lnd.Payments = []lndclient.Payment{ |
| 150 | + { |
| 151 | + Hash: server.swapHash, |
| 152 | + Fee: lnwire.NewMSatFromSatoshis(44), |
| 153 | + }, |
| 154 | + { |
| 155 | + Hash: server.prepayHash, |
| 156 | + Fee: lnwire.NewMSatFromSatoshis(11), |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + // Now we can run the migration. |
| 161 | + err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, store) |
| 162 | + require.NoError(t, err) |
| 163 | + |
| 164 | + // Finally check that the swap cost has been updated correctly. |
| 165 | + swap, err := store.FetchLoopOutSwap( |
| 166 | + context.Background(), initResult.swap.hash, |
| 167 | + ) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + expectedServerCost := server.swapInvoiceAmt + server.prepayInvoiceAmt - |
| 171 | + swap.Contract.AmountRequested |
| 172 | + |
| 173 | + costs := swap.Events[0].Cost |
| 174 | + expectedOffchainCost := btcutil.Amount(44 + 11) |
| 175 | + require.Equal(t, expectedServerCost, costs.Server) |
| 176 | + require.Equal(t, expectedOffchainCost, costs.Offchain) |
| 177 | + require.Equal(t, expectedChainCost, costs.Onchain) |
| 178 | + |
| 179 | + // Now run the migration again to make sure it doesn't fail. This also |
| 180 | + // indicates that the migration did not run the second time as |
| 181 | + // otherwise the store mocks SetMigration function would fail. |
| 182 | + err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, store) |
| 183 | + require.NoError(t, err) |
| 184 | +} |
0 commit comments