2019-03-07 02:19:57 +00:00
|
|
|
package loopdb
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"io/ioutil"
|
2019-03-07 02:19:57 +00:00
|
|
|
"os"
|
2019-05-15 11:59:05 +00:00
|
|
|
"path/filepath"
|
2019-03-06 20:13:50 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-25 10:06:16 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2020-06-23 09:42:12 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-05-15 11:59:05 +00:00
|
|
|
"github.com/coreos/bbolt"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/test"
|
2019-03-07 02:19:57 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-02-11 12:25:03 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2020-06-23 09:42:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
var (
|
|
|
|
senderKey = [33]byte{
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
receiverKey = [33]byte{
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
testPreimage = lntypes.Preimage([32]byte{
|
|
|
|
1, 1, 1, 1, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 4, 4, 4, 4,
|
|
|
|
1, 1, 1, 1, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 4, 4, 4, 4,
|
|
|
|
})
|
|
|
|
|
|
|
|
testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
// TestLoopOutStore tests all the basic functionality of the current bbolt
|
2019-03-07 02:19:57 +00:00
|
|
|
// swap store.
|
2019-03-12 15:09:57 +00:00
|
|
|
func TestLoopOutStore(t *testing.T) {
|
2019-03-06 20:13:50 +00:00
|
|
|
destAddr := test.GetDestAddr(t, 0)
|
|
|
|
initiationTime := time.Date(2018, 11, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// Next, we'll make a new pending swap that we'll insert into the
|
|
|
|
// database shortly.
|
2020-05-19 07:54:27 +00:00
|
|
|
unrestrictedSwap := LoopOutContract{
|
2019-03-06 20:13:50 +00:00
|
|
|
SwapContract: SwapContract{
|
2019-03-12 15:09:57 +00:00
|
|
|
AmountRequested: 100,
|
|
|
|
Preimage: testPreimage,
|
|
|
|
CltvExpiry: 144,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
|
|
|
|
ReceiverKey: receiverKey,
|
|
|
|
MaxMinerFee: 10,
|
|
|
|
MaxSwapFee: 20,
|
|
|
|
|
|
|
|
InitiationHeight: 99,
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Convert to/from unix to remove timezone, so that it
|
|
|
|
// doesn't interfere with DeepEqual.
|
|
|
|
InitiationTime: time.Unix(0, initiationTime.UnixNano()),
|
|
|
|
},
|
2019-11-14 09:35:31 +00:00
|
|
|
MaxPrepayRoutingFee: 40,
|
|
|
|
PrepayInvoice: "prepayinvoice",
|
|
|
|
DestAddr: destAddr,
|
|
|
|
SwapInvoice: "swapinvoice",
|
|
|
|
MaxSwapRoutingFee: 30,
|
|
|
|
SweepConfTarget: 2,
|
2020-08-04 18:28:06 +00:00
|
|
|
HtlcConfirmations: 2,
|
2019-11-14 09:35:31 +00:00
|
|
|
SwapPublicationDeadline: time.Unix(0, initiationTime.UnixNano()),
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
t.Run("no outgoing set", func(t *testing.T) {
|
|
|
|
testLoopOutStore(t, &unrestrictedSwap)
|
|
|
|
})
|
|
|
|
|
|
|
|
restrictedSwap := unrestrictedSwap
|
|
|
|
restrictedSwap.OutgoingChanSet = ChannelSet{1, 2}
|
|
|
|
|
|
|
|
t.Run("two channel outgoing set", func(t *testing.T) {
|
|
|
|
testLoopOutStore(t, &restrictedSwap)
|
|
|
|
})
|
2020-08-03 08:55:58 +00:00
|
|
|
|
|
|
|
labelledSwap := unrestrictedSwap
|
|
|
|
labelledSwap.Label = "test label"
|
|
|
|
t.Run("labelled swap", func(t *testing.T) {
|
|
|
|
testLoopOutStore(t, &labelledSwap)
|
|
|
|
})
|
2020-05-19 08:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// testLoopOutStore tests the basic functionality of the current bbolt
|
|
|
|
// swap store for specific swap parameters.
|
|
|
|
func testLoopOutStore(t *testing.T, pendingSwap *LoopOutContract) {
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-19 08:38:54 +00:00
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-19 08:38:54 +00:00
|
|
|
|
|
|
|
// First, verify that an empty database has no active swaps.
|
|
|
|
swaps, err := store.FetchLoopOutSwaps()
|
2023-02-02 11:58:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, swaps)
|
|
|
|
|
|
|
|
hash := pendingSwap.Preimage.Hash()
|
2020-05-19 08:38:54 +00:00
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// checkSwap is a test helper function that'll assert the state of a
|
|
|
|
// swap.
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap := func(expectedState SwapState) {
|
|
|
|
t.Helper()
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
swaps, err := store.FetchLoopOutSwaps()
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
require.Len(t, swaps, 1)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
swap, err := store.FetchLoopOutSwap(hash)
|
|
|
|
require.NoError(t, err)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
require.Equal(t, hash, swap.Hash)
|
|
|
|
require.Equal(t, hash, swaps[0].Hash)
|
|
|
|
|
|
|
|
swapContract := swap.Contract
|
|
|
|
|
|
|
|
require.Equal(t, swapContract, pendingSwap)
|
|
|
|
|
|
|
|
require.Equal(t, expectedState, swap.State().State)
|
2020-06-23 09:42:12 +00:00
|
|
|
|
|
|
|
if expectedState == StatePreimageRevealed {
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NotNil(t, swap.State().HtlcTxHash)
|
2020-06-23 09:42:12 +00:00
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// If we create a new swap, then it should show up as being initialized
|
|
|
|
// right after.
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.CreateLoopOut(hash, pendingSwap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap(StateInitiated)
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// Trying to make the same swap again should result in an error.
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.CreateLoopOut(hash, pendingSwap)
|
|
|
|
require.Error(t, err)
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap(StateInitiated)
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// Next, we'll update to the next state of the pre-image being
|
|
|
|
// revealed. The state should be reflected here again.
|
2019-03-07 04:32:24 +00:00
|
|
|
err = store.UpdateLoopOut(
|
2019-05-15 11:55:41 +00:00
|
|
|
hash, testTime,
|
|
|
|
SwapStateData{
|
2020-06-23 09:42:12 +00:00
|
|
|
State: StatePreimageRevealed,
|
|
|
|
HtlcTxHash: &chainhash.Hash{1, 6, 2},
|
2019-05-15 11:55:41 +00:00
|
|
|
},
|
2019-03-07 02:19:57 +00:00
|
|
|
)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap(StatePreimageRevealed)
|
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// Next, we'll update to the final state to ensure that the state is
|
|
|
|
// properly updated.
|
2019-03-07 04:32:24 +00:00
|
|
|
err = store.UpdateLoopOut(
|
2019-05-15 11:55:41 +00:00
|
|
|
hash, testTime,
|
|
|
|
SwapStateData{
|
|
|
|
State: StateFailInsufficientValue,
|
|
|
|
},
|
2019-03-07 02:19:57 +00:00
|
|
|
)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap(StateFailInsufficientValue)
|
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.Close()
|
|
|
|
require.NoError(t, err)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 02:19:57 +00:00
|
|
|
// If we re-open the same store, then the state of the current swap
|
|
|
|
// should be the same.
|
2019-03-25 10:06:16 +00:00
|
|
|
store, err = NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
checkSwap(StateFailInsufficientValue)
|
|
|
|
}
|
2019-03-12 15:09:57 +00:00
|
|
|
|
|
|
|
// TestLoopInStore tests all the basic functionality of the current bbolt
|
|
|
|
// swap store.
|
|
|
|
func TestLoopInStore(t *testing.T) {
|
|
|
|
initiationTime := time.Date(2018, 11, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
// Next, we'll make a new pending swap that we'll insert into the
|
|
|
|
// database shortly.
|
2020-02-11 12:25:03 +00:00
|
|
|
lastHop := route.Vertex{1, 2, 3}
|
2019-03-12 15:09:57 +00:00
|
|
|
|
|
|
|
pendingSwap := LoopInContract{
|
|
|
|
SwapContract: SwapContract{
|
|
|
|
AmountRequested: 100,
|
|
|
|
Preimage: testPreimage,
|
|
|
|
CltvExpiry: 144,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
ReceiverKey: receiverKey,
|
|
|
|
MaxMinerFee: 10,
|
|
|
|
MaxSwapFee: 20,
|
|
|
|
InitiationHeight: 99,
|
|
|
|
|
|
|
|
// Convert to/from unix to remove timezone, so that it
|
|
|
|
// doesn't interfere with DeepEqual.
|
|
|
|
InitiationTime: time.Unix(0, initiationTime.UnixNano()),
|
|
|
|
},
|
|
|
|
HtlcConfTarget: 2,
|
2020-02-11 12:25:03 +00:00
|
|
|
LastHop: &lastHop,
|
2019-03-28 12:29:21 +00:00
|
|
|
ExternalHtlc: true,
|
2019-03-12 15:09:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 08:55:58 +00:00
|
|
|
t.Run("loop in", func(t *testing.T) {
|
|
|
|
testLoopInStore(t, pendingSwap)
|
|
|
|
})
|
|
|
|
|
|
|
|
labelledSwap := pendingSwap
|
|
|
|
labelledSwap.Label = "test label"
|
|
|
|
t.Run("loop in with label", func(t *testing.T) {
|
|
|
|
testLoopInStore(t, labelledSwap)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoopInStore(t *testing.T, pendingSwap LoopInContract) {
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-03 08:55:58 +00:00
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-03 08:55:58 +00:00
|
|
|
|
|
|
|
// First, verify that an empty database has no active swaps.
|
|
|
|
swaps, err := store.FetchLoopInSwaps()
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, swaps)
|
|
|
|
|
|
|
|
hash := sha256.Sum256(testPreimage[:])
|
2020-08-03 08:55:58 +00:00
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
// checkSwap is a test helper function that'll assert the state of a
|
|
|
|
// swap.
|
|
|
|
checkSwap := func(expectedState SwapState) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
swaps, err := store.FetchLoopInSwaps()
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, swaps, 1)
|
2019-03-12 15:09:57 +00:00
|
|
|
|
|
|
|
swap := swaps[0].Contract
|
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
require.Equal(t, swap, &pendingSwap)
|
2019-03-12 15:09:57 +00:00
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
require.Equal(t, swaps[0].State().State, expectedState)
|
|
|
|
}
|
2020-08-03 08:55:58 +00:00
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
// If we create a new swap, then it should show up as being initialized
|
|
|
|
// right after.
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.CreateLoopIn(hash, &pendingSwap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
checkSwap(StateInitiated)
|
|
|
|
|
|
|
|
// Trying to make the same swap again should result in an error.
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.CreateLoopIn(hash, &pendingSwap)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
checkSwap(StateInitiated)
|
|
|
|
|
|
|
|
// Next, we'll update to the next state of the pre-image being
|
|
|
|
// revealed. The state should be reflected here again.
|
|
|
|
err = store.UpdateLoopIn(
|
2019-05-15 11:55:41 +00:00
|
|
|
hash, testTime,
|
|
|
|
SwapStateData{
|
|
|
|
State: StatePreimageRevealed,
|
|
|
|
},
|
2019-03-12 15:09:57 +00:00
|
|
|
)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
checkSwap(StatePreimageRevealed)
|
|
|
|
|
|
|
|
// Next, we'll update to the final state to ensure that the state is
|
|
|
|
// properly updated.
|
|
|
|
err = store.UpdateLoopIn(
|
2019-05-15 11:55:41 +00:00
|
|
|
hash, testTime,
|
|
|
|
SwapStateData{
|
|
|
|
State: StateFailInsufficientValue,
|
|
|
|
},
|
2019-03-12 15:09:57 +00:00
|
|
|
)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-12 15:09:57 +00:00
|
|
|
checkSwap(StateFailInsufficientValue)
|
|
|
|
|
2023-02-02 11:58:53 +00:00
|
|
|
err = store.Close()
|
|
|
|
require.NoError(t, err)
|
2019-03-12 15:09:57 +00:00
|
|
|
|
|
|
|
// If we re-open the same store, then the state of the current swap
|
|
|
|
// should be the same.
|
|
|
|
store, err = NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
2023-02-02 11:58:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
checkSwap(StateFailInsufficientValue)
|
|
|
|
}
|
2019-05-15 11:59:05 +00:00
|
|
|
|
|
|
|
// TestVersionNew tests that a new database is initialized with the current
|
|
|
|
// version.
|
|
|
|
func TestVersionNew(t *testing.T) {
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := getDBVersion(store.db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ver != latestDBVersion {
|
|
|
|
t.Fatal("db not at latest version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestVersionNew tests that an existing version zero database is migrated to
|
|
|
|
// the latest version.
|
|
|
|
func TestVersionMigrated(t *testing.T) {
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
createVersionZeroDb(t, tempDirName)
|
|
|
|
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := getDBVersion(store.db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ver != latestDBVersion {
|
|
|
|
t.Fatal("db not at latest version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// createVersionZeroDb creates a database with an empty meta bucket. In version
|
|
|
|
// zero, there was no version key specified yet.
|
|
|
|
func createVersionZeroDb(t *testing.T, dbPath string) {
|
|
|
|
path := filepath.Join(dbPath, dbFileName)
|
|
|
|
bdb, err := bbolt.Open(path, 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer bdb.Close()
|
|
|
|
|
|
|
|
err = bdb.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucket(metaBucketKey)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 07:54:27 +00:00
|
|
|
|
|
|
|
// TestLegacyOutgoingChannel asserts that a legacy channel restriction is
|
|
|
|
// properly mapped onto the newer channel set.
|
|
|
|
func TestLegacyOutgoingChannel(t *testing.T) {
|
|
|
|
var (
|
|
|
|
legacyDbVersion = Hex("00000003")
|
|
|
|
legacyOutgoingChannel = Hex("0000000000000005")
|
|
|
|
)
|
|
|
|
|
|
|
|
legacyDb := map[string]interface{}{
|
|
|
|
"loop-in": map[string]interface{}{},
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"dbp": legacyDbVersion,
|
|
|
|
},
|
|
|
|
"uncharge-swaps": map[string]interface{}{
|
|
|
|
Hex("2a595d79a55168970532805ae20c9b5fac98f04db79ba4c6ae9b9ac0f206359e"): map[string]interface{}{
|
|
|
|
"contract": Hex("1562d6fbec140000010101010202020203030303040404040101010102020202030303030404040400000000000000640d707265706179696e766f69636501010101010101010101010101010101010101010101010101010101010101010201010101010101010101010101010101010101010101010101010101010101010300000090000000000000000a0000000000000014000000000000002800000063223347454e556d6e4552745766516374344e65676f6d557171745a757a5947507742530b73776170696e766f69636500000002000000000000001e") + legacyOutgoingChannel + Hex("1562d6fbec140000"),
|
|
|
|
"updates": map[string]interface{}{
|
|
|
|
Hex("0000000000000001"): Hex("1508290a92d4c00001000000000000000000000000000000000000000000000000"),
|
|
|
|
Hex("0000000000000002"): Hex("1508290a92d4c00006000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore a legacy database.
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
tempPath := filepath.Join(tempDirName, dbFileName)
|
|
|
|
db, err := bbolt.Open(tempPath, 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return RestoreDB(tx, legacyDb)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
// Fetch the legacy swap.
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
swaps, err := store.FetchLoopOutSwaps()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the outgoing channel is read properly.
|
|
|
|
expectedChannelSet := ChannelSet{5}
|
2023-02-02 11:58:53 +00:00
|
|
|
|
|
|
|
require.Equal(t, expectedChannelSet, swaps[0].Contract.OutgoingChanSet)
|
2020-05-19 07:54:27 +00:00
|
|
|
}
|
2022-05-18 19:07:10 +00:00
|
|
|
|
|
|
|
// TestLiquidityParams checks that reading and writing to liquidty bucket are
|
|
|
|
// as expected.
|
|
|
|
func TestLiquidityParams(t *testing.T) {
|
|
|
|
tempDirName, err := ioutil.TempDir("", "clientstore")
|
|
|
|
require.NoError(t, err, "failed to db")
|
|
|
|
defer os.RemoveAll(tempDirName)
|
|
|
|
|
|
|
|
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
|
|
|
|
require.NoError(t, err, "failed to create store")
|
|
|
|
|
|
|
|
// Test when there's no params saved before, an empty bytes is
|
|
|
|
// returned.
|
|
|
|
params, err := store.FetchLiquidityParams()
|
|
|
|
require.NoError(t, err, "failed to fetch params")
|
|
|
|
require.Empty(t, params, "expect empty bytes")
|
|
|
|
|
|
|
|
params = []byte("test")
|
|
|
|
|
|
|
|
// Test we can save the params.
|
|
|
|
err = store.PutLiquidityParams(params)
|
|
|
|
require.NoError(t, err, "failed to put params")
|
|
|
|
|
|
|
|
// Now fetch the db again should return the above saved bytes.
|
|
|
|
paramsRead, err := store.FetchLiquidityParams()
|
|
|
|
require.NoError(t, err, "failed to fetch params")
|
|
|
|
require.Equal(t, params, paramsRead, "unexpected return value")
|
|
|
|
}
|