2016-03-24 06:11:57 +01:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2018-12-10 04:37:47 +01:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2018-12-10 04:15:09 +01:00
|
|
|
"net"
|
2016-03-24 22:31:46 +01:00
|
|
|
"path/filepath"
|
2018-12-10 04:26:02 +01:00
|
|
|
"reflect"
|
2016-03-24 06:11:57 +01:00
|
|
|
"testing"
|
2018-11-20 15:09:46 +01:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2018-12-10 04:37:47 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-12-10 04:15:09 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-12-10 04:37:47 +01:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2024-07-31 01:25:40 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-11-20 15:09:46 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-12-10 04:37:47 +01:00
|
|
|
"github.com/lightningnetwork/lnd/shachain"
|
2020-12-11 02:08:56 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-24 06:11:57 +01:00
|
|
|
)
|
|
|
|
|
2016-03-24 22:31:46 +01:00
|
|
|
func TestOpenWithCreate(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-07-16 18:07:30 +02:00
|
|
|
// Checking for db file existence is not possible with postgres.
|
|
|
|
if kvdb.PostgresBackend {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2016-03-24 06:11:57 +01:00
|
|
|
// First, create a temporary directory to be used for the duration of
|
|
|
|
// this test.
|
2022-08-15 15:05:24 +02:00
|
|
|
tempDirName := t.TempDir()
|
2016-03-24 06:11:57 +01:00
|
|
|
|
2016-03-24 22:31:46 +01:00
|
|
|
// Next, open thereby creating channeldb for the first time.
|
|
|
|
dbPath := filepath.Join(tempDirName, "cdb")
|
2020-03-09 19:27:50 +01:00
|
|
|
backend, cleanup, err := kvdb.GetTestBackend(dbPath, "cdb")
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to get test db backend")
|
2022-08-27 09:04:55 +02:00
|
|
|
t.Cleanup(cleanup)
|
2020-03-09 19:27:50 +01:00
|
|
|
|
|
|
|
cdb, err := CreateWithBackend(backend)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to create channeldb")
|
2016-03-24 06:11:57 +01:00
|
|
|
if err := cdb.Close(); err != nil {
|
|
|
|
t.Fatalf("unable to close channeldb: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:11 +01:00
|
|
|
// The path should have been successfully created.
|
2016-03-24 22:31:46 +01:00
|
|
|
if !fileExists(dbPath) {
|
|
|
|
t.Fatalf("channeldb failed to create data directory")
|
2016-03-24 06:11:57 +01:00
|
|
|
}
|
2020-05-12 00:38:45 +02:00
|
|
|
|
|
|
|
// Now, reopen the same db in dry run migration mode. Since we have not
|
|
|
|
// applied any migrations, this should ignore the flag and not fail.
|
|
|
|
cdb, err = Open(dbPath, OptionDryRunMigration(true))
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to create channeldb")
|
2020-05-12 00:38:45 +02:00
|
|
|
if err := cdb.Close(); err != nil {
|
|
|
|
t.Fatalf("unable to close channeldb: %v", err)
|
|
|
|
}
|
2016-03-24 06:11:57 +01:00
|
|
|
}
|
2018-09-26 17:27:30 +02:00
|
|
|
|
|
|
|
// TestWipe tests that the database wipe operation completes successfully
|
|
|
|
// and that the buckets are deleted. It also checks that attempts to fetch
|
|
|
|
// information while the buckets are not set return the correct errors.
|
|
|
|
func TestWipe(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// First, create a temporary directory to be used for the duration of
|
|
|
|
// this test.
|
2022-08-15 15:05:24 +02:00
|
|
|
tempDirName := t.TempDir()
|
2018-09-26 17:27:30 +02:00
|
|
|
|
|
|
|
// Next, open thereby creating channeldb for the first time.
|
|
|
|
dbPath := filepath.Join(tempDirName, "cdb")
|
2020-03-09 19:27:50 +01:00
|
|
|
backend, cleanup, err := kvdb.GetTestBackend(dbPath, "cdb")
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to get test db backend")
|
2022-08-27 09:04:55 +02:00
|
|
|
t.Cleanup(cleanup)
|
2020-03-09 19:27:50 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
fullDB, err := CreateWithBackend(backend)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to create channeldb")
|
2021-09-21 19:18:17 +02:00
|
|
|
defer fullDB.Close()
|
2018-09-26 17:27:30 +02:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
if err := fullDB.Wipe(); err != nil {
|
2018-09-26 17:27:30 +02:00
|
|
|
t.Fatalf("unable to wipe channeldb: %v", err)
|
|
|
|
}
|
2021-09-21 19:18:17 +02:00
|
|
|
|
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2018-09-26 17:27:30 +02:00
|
|
|
// Check correct errors are returned
|
2020-12-11 02:08:56 +01:00
|
|
|
openChannels, err := cdb.FetchAllOpenChannels()
|
|
|
|
require.NoError(t, err, "fetching open channels")
|
|
|
|
require.Equal(t, 0, len(openChannels))
|
|
|
|
|
|
|
|
closedChannels, err := cdb.FetchClosedChannels(false)
|
|
|
|
require.NoError(t, err, "fetching closed channels")
|
|
|
|
require.Equal(t, 0, len(closedChannels))
|
2018-09-26 17:27:30 +02:00
|
|
|
}
|
2018-11-20 15:09:46 +01:00
|
|
|
|
|
|
|
// TestFetchClosedChannelForID tests that we are able to properly retrieve a
|
|
|
|
// ChannelCloseSummary from the DB given a ChannelID.
|
|
|
|
func TestFetchClosedChannelForID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const numChans = 101
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2018-11-20 15:09:46 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2018-11-20 15:09:46 +01:00
|
|
|
// Create the test channel state, that we will mutate the index of the
|
|
|
|
// funding point.
|
2020-02-05 14:39:31 +01:00
|
|
|
state := createTestChannelState(t, cdb)
|
2018-11-20 15:09:46 +01:00
|
|
|
|
|
|
|
// Now run through the number of channels, and modify the outpoint index
|
|
|
|
// to create new channel IDs.
|
|
|
|
for i := uint32(0); i < numChans; i++ {
|
|
|
|
// Save the open channel to disk.
|
|
|
|
state.FundingOutpoint.Index = i
|
2019-09-06 13:14:38 +02:00
|
|
|
|
2020-02-05 14:39:31 +01:00
|
|
|
// Write the channel to disk in a pending state.
|
|
|
|
createTestChannel(
|
|
|
|
t, cdb,
|
|
|
|
fundingPointOption(state.FundingOutpoint),
|
|
|
|
openChannelOption(),
|
|
|
|
)
|
2018-11-20 15:09:46 +01:00
|
|
|
|
|
|
|
// Close the channel. To make sure we retrieve the correct
|
|
|
|
// summary later, we make them differ in the SettledBalance.
|
|
|
|
closeSummary := &ChannelCloseSummary{
|
|
|
|
ChanPoint: state.FundingOutpoint,
|
|
|
|
RemotePub: state.IdentityPub,
|
|
|
|
SettledBalance: btcutil.Amount(500 + i),
|
|
|
|
}
|
|
|
|
if err := state.CloseChannel(closeSummary); err != nil {
|
|
|
|
t.Fatalf("unable to close channel: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now run though them all again and make sure we are able to retrieve
|
|
|
|
// summaries from the DB.
|
|
|
|
for i := uint32(0); i < numChans; i++ {
|
|
|
|
state.FundingOutpoint.Index = i
|
|
|
|
|
|
|
|
// We calculate the ChannelID and use it to fetch the summary.
|
2024-01-29 22:19:15 +01:00
|
|
|
cid := lnwire.NewChanIDFromOutPoint(state.FundingOutpoint)
|
2018-11-20 15:09:46 +01:00
|
|
|
fetchedSummary, err := cdb.FetchClosedChannelForID(cid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch close summary: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we retrieved the correct one by checking the
|
|
|
|
// SettledBalance.
|
|
|
|
if fetchedSummary.SettledBalance != btcutil.Amount(500+i) {
|
|
|
|
t.Fatalf("summaries don't match: expected %v got %v",
|
|
|
|
btcutil.Amount(500+i),
|
|
|
|
fetchedSummary.SettledBalance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a final test we make sure that we get ErrClosedChannelNotFound
|
|
|
|
// for a ChannelID we didn't add to the DB.
|
|
|
|
state.FundingOutpoint.Index++
|
2024-01-29 22:19:15 +01:00
|
|
|
cid := lnwire.NewChanIDFromOutPoint(state.FundingOutpoint)
|
2018-11-20 15:09:46 +01:00
|
|
|
_, err = cdb.FetchClosedChannelForID(cid)
|
|
|
|
if err != ErrClosedChannelNotFound {
|
|
|
|
t.Fatalf("expected ErrClosedChannelNotFound, instead got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 04:15:09 +01:00
|
|
|
|
|
|
|
// TestAddrsForNode tests the we're able to properly obtain all the addresses
|
|
|
|
// for a target node.
|
|
|
|
func TestAddrsForNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2018-12-10 04:15:09 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
graph := fullDB.ChannelGraph()
|
2018-12-10 04:15:09 +01:00
|
|
|
|
|
|
|
// We'll make a test vertex to insert into the database, as the source
|
|
|
|
// node, but this node will only have half the number of addresses it
|
|
|
|
// usually does.
|
2021-09-21 19:18:17 +02:00
|
|
|
testNode, err := createTestVertex(fullDB)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to create test node")
|
2018-12-10 04:15:09 +01:00
|
|
|
testNode.Addresses = []net.Addr{testAddr}
|
|
|
|
if err := graph.SetSourceNode(testNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll make a link node with the same pubkey, but with an
|
|
|
|
// additional address.
|
|
|
|
nodePub, err := testNode.PubKey()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to recv node pub")
|
2021-09-21 19:18:13 +02:00
|
|
|
linkNode := NewLinkNode(
|
2021-09-21 19:18:17 +02:00
|
|
|
fullDB.channelStateDB.linkNodeDB, wire.MainNet, nodePub,
|
|
|
|
anotherAddr,
|
2018-12-10 04:15:09 +01:00
|
|
|
)
|
|
|
|
if err := linkNode.Sync(); err != nil {
|
|
|
|
t.Fatalf("unable to sync link node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've created a link node, as well as a vertex for the
|
|
|
|
// node, we'll query for all its addresses.
|
2021-09-21 19:18:17 +02:00
|
|
|
nodeAddrs, err := fullDB.AddrsForNode(nodePub)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to obtain node addrs")
|
2018-12-10 04:15:09 +01:00
|
|
|
|
|
|
|
expectedAddrs := make(map[string]struct{})
|
|
|
|
expectedAddrs[testAddr.String()] = struct{}{}
|
|
|
|
expectedAddrs[anotherAddr.String()] = struct{}{}
|
|
|
|
|
|
|
|
// Finally, ensure that all the expected addresses are found.
|
|
|
|
if len(nodeAddrs) != len(expectedAddrs) {
|
|
|
|
t.Fatalf("expected %v addrs, got %v",
|
|
|
|
len(expectedAddrs), len(nodeAddrs))
|
|
|
|
}
|
|
|
|
for _, addr := range nodeAddrs {
|
|
|
|
if _, ok := expectedAddrs[addr.String()]; !ok {
|
|
|
|
t.Fatalf("unexpected addr: %v", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 04:26:02 +01:00
|
|
|
|
|
|
|
// TestFetchChannel tests that we're able to fetch an arbitrary channel from
|
|
|
|
// disk.
|
|
|
|
func TestFetchChannel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2018-12-10 04:26:02 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2020-02-05 14:39:31 +01:00
|
|
|
// Create an open channel.
|
|
|
|
channelState := createTestChannel(t, cdb, openChannelOption())
|
2018-12-10 04:26:02 +01:00
|
|
|
|
|
|
|
// Next, attempt to fetch the channel by its chan point.
|
2021-08-23 12:16:37 +02:00
|
|
|
dbChannel, err := cdb.FetchChannel(nil, channelState.FundingOutpoint)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to fetch channel")
|
2018-12-10 04:26:02 +01:00
|
|
|
|
|
|
|
// The decoded channel state should be identical to what we stored
|
|
|
|
// above.
|
2023-03-22 08:35:59 +01:00
|
|
|
require.Equal(t, channelState, dbChannel)
|
|
|
|
|
|
|
|
// Next, attempt to fetch the channel by its channel ID.
|
2024-01-29 22:19:15 +01:00
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(channelState.FundingOutpoint)
|
2023-03-22 08:35:59 +01:00
|
|
|
dbChannel, err = cdb.FetchChannelByID(nil, chanID)
|
|
|
|
require.NoError(t, err, "unable to fetch channel")
|
|
|
|
|
|
|
|
// The decoded channel state should be identical to what we stored
|
|
|
|
// above.
|
|
|
|
require.Equal(t, channelState, dbChannel)
|
2018-12-10 04:26:02 +01:00
|
|
|
|
2023-02-10 09:32:09 +01:00
|
|
|
// If we attempt to query for a non-existent channel, then we should
|
2018-12-10 04:26:02 +01:00
|
|
|
// get an error.
|
2020-02-05 14:39:31 +01:00
|
|
|
channelState2 := createTestChannelState(t, cdb)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to create channel state")
|
2023-02-10 09:32:09 +01:00
|
|
|
|
|
|
|
uniqueOutputIndex.Add(1)
|
|
|
|
channelState2.FundingOutpoint.Index = uniqueOutputIndex.Load()
|
2018-12-10 04:26:02 +01:00
|
|
|
|
2021-08-23 12:16:37 +02:00
|
|
|
_, err = cdb.FetchChannel(nil, channelState2.FundingOutpoint)
|
2023-03-22 08:35:59 +01:00
|
|
|
require.ErrorIs(t, err, ErrChannelNotFound)
|
|
|
|
|
2024-01-29 22:19:15 +01:00
|
|
|
chanID2 := lnwire.NewChanIDFromOutPoint(channelState2.FundingOutpoint)
|
2023-03-22 08:35:59 +01:00
|
|
|
_, err = cdb.FetchChannelByID(nil, chanID2)
|
|
|
|
require.ErrorIs(t, err, ErrChannelNotFound)
|
2018-12-10 04:26:02 +01:00
|
|
|
}
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
func genRandomChannelShell() (*ChannelShell, error) {
|
|
|
|
var testPriv [32]byte
|
|
|
|
if _, err := rand.Read(testPriv[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
_, pub := btcec.PrivKeyFromBytes(testPriv[:])
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
var chanPoint wire.OutPoint
|
|
|
|
if _, err := rand.Read(chanPoint.Hash[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chanPoint.Index = uint32(rand.Intn(math.MaxUint16))
|
|
|
|
|
|
|
|
chanStatus := ChanStatusDefault | ChanStatusRestored
|
|
|
|
|
|
|
|
var shaChainPriv [32]byte
|
|
|
|
if _, err := rand.Read(testPriv[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
revRoot, err := chainhash.NewHash(shaChainPriv[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
shaChainProducer := shachain.NewRevocationProducer(*revRoot)
|
|
|
|
|
2024-05-09 23:32:45 +02:00
|
|
|
commitParams := CommitmentParams{
|
|
|
|
CsvDelay: uint16(rand.Int63()),
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:37:47 +01:00
|
|
|
return &ChannelShell{
|
|
|
|
NodeAddrs: []net.Addr{&net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 18555,
|
|
|
|
}},
|
|
|
|
Chan: &OpenChannel{
|
|
|
|
chanStatus: chanStatus,
|
|
|
|
ChainHash: rev,
|
|
|
|
FundingOutpoint: chanPoint,
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(
|
|
|
|
uint64(rand.Int63()),
|
|
|
|
),
|
|
|
|
IdentityPub: pub,
|
|
|
|
LocalChanCfg: ChannelConfig{
|
2024-05-09 23:32:45 +02:00
|
|
|
CommitmentParams: commitParams,
|
2018-12-10 04:37:47 +01:00
|
|
|
PaymentBasePoint: keychain.KeyDescriptor{
|
|
|
|
KeyLocator: keychain.KeyLocator{
|
|
|
|
Family: keychain.KeyFamily(rand.Int63()),
|
|
|
|
Index: uint32(rand.Int63()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RemoteCurrentRevocation: pub,
|
|
|
|
IsPending: false,
|
|
|
|
RevocationStore: shachain.NewRevocationStore(),
|
|
|
|
RevocationProducer: shaChainProducer,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRestoreChannelShells tests that we're able to insert a partially channel
|
|
|
|
// populated to disk. This is useful for channel recovery purposes. We should
|
|
|
|
// find the new channel shell on disk, and also the db should be populated with
|
|
|
|
// an edge for that channel.
|
|
|
|
func TestRestoreChannelShells(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2018-12-10 04:37:47 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2018-12-10 04:37:47 +01:00
|
|
|
// First, we'll make our channel shell, it will only have the minimal
|
|
|
|
// amount of information required for us to initiate the data loss
|
|
|
|
// protection feature.
|
|
|
|
channelShell, err := genRandomChannelShell()
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to gen channel shell")
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
// With the channel shell constructed, we'll now insert it into the
|
|
|
|
// database with the restoration method.
|
|
|
|
if err := cdb.RestoreChannelShells(channelShell); err != nil {
|
|
|
|
t.Fatalf("unable to restore channel shell: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel has been inserted, we'll attempt to query for
|
|
|
|
// it to ensure we can properly locate it via various means.
|
|
|
|
//
|
|
|
|
// First, we'll attempt to query for all channels that we have with the
|
|
|
|
// node public key that was restored.
|
|
|
|
nodeChans, err := cdb.FetchOpenChannels(channelShell.Chan.IdentityPub)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable find channel")
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
// We should now find a single channel from the database.
|
|
|
|
if len(nodeChans) != 1 {
|
|
|
|
t.Fatalf("unable to find restored channel by node "+
|
|
|
|
"pubkey: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-19 04:00:37 +01:00
|
|
|
// Ensure that it isn't possible to modify the commitment state machine
|
|
|
|
// of this restored channel.
|
|
|
|
channel := nodeChans[0]
|
2022-05-10 12:33:44 +02:00
|
|
|
_, err = channel.UpdateCommitment(nil, nil)
|
2019-01-19 04:00:37 +01:00
|
|
|
if err != ErrNoRestoredChannelMutation {
|
|
|
|
t.Fatalf("able to mutate restored channel")
|
|
|
|
}
|
|
|
|
err = channel.AppendRemoteCommitChain(nil)
|
|
|
|
if err != ErrNoRestoredChannelMutation {
|
|
|
|
t.Fatalf("able to mutate restored channel")
|
|
|
|
}
|
2022-04-08 01:36:26 +02:00
|
|
|
err = channel.AdvanceCommitChainTail(
|
|
|
|
nil, nil, dummyLocalOutputIndex, dummyRemoteOutIndex,
|
|
|
|
)
|
2019-01-19 04:00:37 +01:00
|
|
|
if err != ErrNoRestoredChannelMutation {
|
|
|
|
t.Fatalf("able to mutate restored channel")
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:37:47 +01:00
|
|
|
// That single channel should have the proper channel point, and also
|
|
|
|
// the expected set of flags to indicate that it was a restored
|
|
|
|
// channel.
|
|
|
|
if nodeChans[0].FundingOutpoint != channelShell.Chan.FundingOutpoint {
|
|
|
|
t.Fatalf("wrong funding outpoint: expected %v, got %v",
|
|
|
|
nodeChans[0].FundingOutpoint,
|
|
|
|
channelShell.Chan.FundingOutpoint)
|
|
|
|
}
|
|
|
|
if !nodeChans[0].HasChanStatus(ChanStatusRestored) {
|
|
|
|
t.Fatalf("node has wrong status flags: %v",
|
|
|
|
nodeChans[0].chanStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should also be able to find the channel if we query for it
|
|
|
|
// directly.
|
2021-08-23 12:16:37 +02:00
|
|
|
_, err = cdb.FetchChannel(nil, channelShell.Chan.FundingOutpoint)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to fetch channel")
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
// We should also be able to find the link node that was inserted by
|
|
|
|
// its public key.
|
2021-09-21 19:18:17 +02:00
|
|
|
linkNode, err := fullDB.channelStateDB.linkNodeDB.FetchLinkNode(
|
2021-09-21 19:18:13 +02:00
|
|
|
channelShell.Chan.IdentityPub,
|
|
|
|
)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to fetch link node")
|
2018-12-10 04:37:47 +01:00
|
|
|
|
|
|
|
// The node should have the same address, as specified in the channel
|
|
|
|
// shell.
|
|
|
|
if reflect.DeepEqual(linkNode.Addresses, channelShell.NodeAddrs) {
|
2022-01-13 17:29:43 +01:00
|
|
|
t.Fatalf("addr mismatch: expected %v, got %v",
|
2018-12-10 04:37:47 +01:00
|
|
|
linkNode.Addresses, channelShell.NodeAddrs)
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 04:46:32 +01:00
|
|
|
|
|
|
|
// TestAbandonChannel tests that the AbandonChannel method is able to properly
|
|
|
|
// remove a channel from the database and add a close channel summary. If
|
|
|
|
// called after a channel has already been removed, the method shouldn't return
|
|
|
|
// an error.
|
|
|
|
func TestAbandonChannel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2019-11-20 04:46:32 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2019-11-20 04:46:32 +01:00
|
|
|
// If we attempt to abandon the state of a channel that doesn't exist
|
|
|
|
// in the open or closed channel bucket, then we should receive an
|
|
|
|
// error.
|
|
|
|
err = cdb.AbandonChannel(&wire.OutPoint{}, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("removing non-existent channel should have failed")
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:39:31 +01:00
|
|
|
// We'll now create a new channel in a pending state to abandon
|
|
|
|
// shortly.
|
|
|
|
chanState := createTestChannel(t, cdb)
|
2019-11-20 04:46:32 +01:00
|
|
|
|
|
|
|
// We should now be able to abandon the channel without any errors.
|
|
|
|
closeHeight := uint32(11)
|
|
|
|
err = cdb.AbandonChannel(&chanState.FundingOutpoint, closeHeight)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to abandon channel")
|
2019-11-20 04:46:32 +01:00
|
|
|
|
|
|
|
// At this point, the channel should no longer be found in the set of
|
|
|
|
// open channels.
|
2021-08-23 12:16:37 +02:00
|
|
|
_, err = cdb.FetchChannel(nil, chanState.FundingOutpoint)
|
2019-11-20 04:46:32 +01:00
|
|
|
if err != ErrChannelNotFound {
|
|
|
|
t.Fatalf("channel should not have been found: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// However we should be able to retrieve a close channel summary for
|
|
|
|
// the channel.
|
|
|
|
_, err = cdb.FetchClosedChannel(&chanState.FundingOutpoint)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to fetch closed channel")
|
2019-11-20 04:46:32 +01:00
|
|
|
|
|
|
|
// Finally, if we attempt to abandon the channel again, we should get a
|
|
|
|
// nil error as the channel has already been abandoned.
|
|
|
|
err = cdb.AbandonChannel(&chanState.FundingOutpoint, closeHeight)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to abandon channel")
|
2019-11-20 04:46:32 +01:00
|
|
|
}
|
2020-02-06 09:21:12 +01:00
|
|
|
|
|
|
|
// TestFetchChannels tests the filtering of open channels in fetchChannels.
|
|
|
|
// It tests the case where no filters are provided (which is equivalent to
|
|
|
|
// FetchAllOpenChannels) and every combination of pending and waiting close.
|
|
|
|
func TestFetchChannels(t *testing.T) {
|
|
|
|
// Create static channel IDs for each kind of channel retrieved by
|
|
|
|
// fetchChannels so that the expected channel IDs can be set in tests.
|
|
|
|
var (
|
|
|
|
// Pending is a channel that is pending open, and has not had
|
|
|
|
// a close initiated.
|
|
|
|
pendingChan = lnwire.NewShortChanIDFromInt(1)
|
|
|
|
|
|
|
|
// pendingWaitingClose is a channel that is pending open and
|
|
|
|
// has has its closing transaction broadcast.
|
|
|
|
pendingWaitingChan = lnwire.NewShortChanIDFromInt(2)
|
|
|
|
|
|
|
|
// openChan is a channel that has confirmed on chain.
|
|
|
|
openChan = lnwire.NewShortChanIDFromInt(3)
|
|
|
|
|
|
|
|
// openWaitingChan is a channel that has confirmed on chain,
|
|
|
|
// and it waiting for its close transaction to confirm.
|
|
|
|
openWaitingChan = lnwire.NewShortChanIDFromInt(4)
|
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
filters []fetchChannelsFilter
|
|
|
|
expectedChannels map[lnwire.ShortChannelID]bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "get all channels",
|
|
|
|
filters: []fetchChannelsFilter{},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingChan: true,
|
|
|
|
pendingWaitingChan: true,
|
|
|
|
openChan: true,
|
|
|
|
openWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending channels",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(true),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingChan: true,
|
|
|
|
pendingWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "open channels",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(false),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
openChan: true,
|
|
|
|
openWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "waiting close channels",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
waitingCloseFilter(true),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingWaitingChan: true,
|
|
|
|
openWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not waiting close channels",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
waitingCloseFilter(false),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingChan: true,
|
|
|
|
openChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending waiting",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(true),
|
|
|
|
waitingCloseFilter(true),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending, not waiting",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(true),
|
|
|
|
waitingCloseFilter(false),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
pendingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "open waiting",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(false),
|
|
|
|
waitingCloseFilter(true),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
openWaitingChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "open, not waiting",
|
|
|
|
filters: []fetchChannelsFilter{
|
|
|
|
pendingChannelFilter(false),
|
|
|
|
waitingCloseFilter(false),
|
|
|
|
},
|
|
|
|
expectedChannels: map[lnwire.ShortChannelID]bool{
|
|
|
|
openChan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2020-02-06 09:21:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test "+
|
|
|
|
"database: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2020-02-06 09:21:12 +01:00
|
|
|
// Create a pending channel that is not awaiting close.
|
|
|
|
createTestChannel(
|
|
|
|
t, cdb, channelIDOption(pendingChan),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a pending channel which has has been marked as
|
|
|
|
// broadcast, indicating that its closing transaction is
|
|
|
|
// waiting to confirm.
|
|
|
|
pendingClosing := createTestChannel(
|
|
|
|
t, cdb,
|
|
|
|
channelIDOption(pendingWaitingChan),
|
|
|
|
)
|
|
|
|
|
2024-07-31 01:25:40 +02:00
|
|
|
err = pendingClosing.MarkCoopBroadcasted(
|
|
|
|
nil, lntypes.Local,
|
|
|
|
)
|
2020-02-06 09:21:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a open channel that is not awaiting close.
|
|
|
|
createTestChannel(
|
|
|
|
t, cdb,
|
|
|
|
channelIDOption(openChan),
|
|
|
|
openChannelOption(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a open channel which has has been marked as
|
|
|
|
// broadcast, indicating that its closing transaction is
|
|
|
|
// waiting to confirm.
|
|
|
|
openClosing := createTestChannel(
|
|
|
|
t, cdb,
|
|
|
|
channelIDOption(openWaitingChan),
|
|
|
|
openChannelOption(),
|
|
|
|
)
|
2024-07-31 01:25:40 +02:00
|
|
|
err = openClosing.MarkCoopBroadcasted(
|
|
|
|
nil, lntypes.Local,
|
|
|
|
)
|
2020-02-06 09:21:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
channels, err := fetchChannels(cdb, test.filters...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(channels) != len(test.expectedChannels) {
|
|
|
|
t.Fatalf("expected: %v channels, "+
|
|
|
|
"got: %v", len(test.expectedChannels),
|
|
|
|
len(channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range channels {
|
|
|
|
_, ok := test.expectedChannels[ch.ShortChannelID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("fetch channels unexpected "+
|
|
|
|
"channel: %v", ch.ShortChannelID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 12:23:30 +01:00
|
|
|
|
|
|
|
// TestFetchHistoricalChannel tests lookup of historical channels.
|
|
|
|
func TestFetchHistoricalChannel(t *testing.T) {
|
2022-08-27 09:04:55 +02:00
|
|
|
fullDB, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable to make test database")
|
2020-02-21 12:23:30 +01:00
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
cdb := fullDB.ChannelStateDB()
|
|
|
|
|
2020-02-21 12:23:30 +01:00
|
|
|
// Create a an open channel in the database.
|
|
|
|
channel := createTestChannel(t, cdb, openChannelOption())
|
|
|
|
|
2022-01-14 00:05:48 +01:00
|
|
|
// First, try to lookup a channel when nothing is in the bucket. As the
|
|
|
|
// bucket is auto-created (on start up), we'll get a channel not found
|
|
|
|
// error.
|
2020-02-21 12:23:30 +01:00
|
|
|
_, err = cdb.FetchHistoricalChannel(&channel.FundingOutpoint)
|
2022-01-14 00:05:48 +01:00
|
|
|
if err != ErrChannelNotFound {
|
2020-02-21 12:23:30 +01:00
|
|
|
t.Fatalf("expected no bucket, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel so that it will be written to the historical
|
|
|
|
// bucket. The values provided in the channel close summary are the
|
|
|
|
// minimum required for this call to run without panicking.
|
|
|
|
if err := channel.CloseChannel(&ChannelCloseSummary{
|
|
|
|
ChanPoint: channel.FundingOutpoint,
|
|
|
|
RemotePub: channel.IdentityPub,
|
|
|
|
SettledBalance: btcutil.Amount(500),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("unexpected error closing channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
histChannel, err := cdb.FetchHistoricalChannel(&channel.FundingOutpoint)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unexpected error getting channel")
|
2020-02-21 12:23:30 +01:00
|
|
|
|
2021-08-30 12:58:43 +02:00
|
|
|
// FetchHistoricalChannel will attach the cdb to channel.Db, we set it
|
|
|
|
// here so that we can check that all other fields on the channel equal
|
|
|
|
// those on the historical channel.
|
|
|
|
channel.Db = cdb
|
2020-02-21 12:23:30 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(histChannel, channel) {
|
|
|
|
t.Fatalf("expected: %v, got: %v", channel, histChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an outpoint that will not be in the db and look it up.
|
|
|
|
badOutpoint := &wire.OutPoint{
|
|
|
|
Hash: channel.FundingOutpoint.Hash,
|
|
|
|
Index: channel.FundingOutpoint.Index + 1,
|
|
|
|
}
|
|
|
|
_, err = cdb.FetchHistoricalChannel(badOutpoint)
|
|
|
|
if err != ErrChannelNotFound {
|
|
|
|
t.Fatalf("expected chan not found, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|