multi: move all channelstate operations to ChannelStateDB

This commit is contained in:
Andras Banki-Horvath
2021-09-21 19:18:17 +02:00
committed by Oliver Gugger
parent ddea833d31
commit 11cf4216e4
35 changed files with 377 additions and 238 deletions

View File

@@ -183,7 +183,7 @@ var channelIDOption = func(chanID lnwire.ShortChannelID) testChannelOption {
// createTestChannel writes a test channel to the database. It takes a set of
// functional options which can be used to overwrite the default of creating
// a pending channel that was broadcast at height 100.
func createTestChannel(t *testing.T, cdb *DB,
func createTestChannel(t *testing.T, cdb *ChannelStateDB,
opts ...testChannelOption) *OpenChannel {
// Create a default set of parameters.
@@ -221,7 +221,7 @@ func createTestChannel(t *testing.T, cdb *DB,
return params.channel
}
func createTestChannelState(t *testing.T, cdb *DB) *OpenChannel {
func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
// Simulate 1000 channel updates.
producer, err := shachain.NewRevocationProducerFromBytes(key[:])
if err != nil {
@@ -359,12 +359,14 @@ func createTestChannelState(t *testing.T, cdb *DB) *OpenChannel {
func TestOpenChannelPutGetDelete(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create the test channel state, with additional htlcs on the local
// and remote commitment.
localHtlcs := []HTLC{
@@ -508,12 +510,14 @@ func TestOptionalShutdown(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create a channel with upfront scripts set as
// specified in the test.
state := createTestChannel(
@@ -565,12 +569,14 @@ func assertCommitmentEqual(t *testing.T, a, b *ChannelCommitment) {
func TestChannelStateTransition(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// First create a minimal channel, then perform a full sync in order to
// persist the data.
channel := createTestChannel(t, cdb)
@@ -842,7 +848,7 @@ func TestChannelStateTransition(t *testing.T) {
}
// At this point, we should have 2 forwarding packages added.
fwdPkgs := loadFwdPkgs(t, cdb, channel.Packager)
fwdPkgs := loadFwdPkgs(t, cdb.backend, channel.Packager)
require.Len(t, fwdPkgs, 2, "wrong number of forwarding packages")
// Now attempt to delete the channel from the database.
@@ -877,19 +883,21 @@ func TestChannelStateTransition(t *testing.T) {
}
// All forwarding packages of this channel has been deleted too.
fwdPkgs = loadFwdPkgs(t, cdb, channel.Packager)
fwdPkgs = loadFwdPkgs(t, cdb.backend, channel.Packager)
require.Empty(t, fwdPkgs, "no forwarding packages should exist")
}
func TestFetchPendingChannels(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create a pending channel that was broadcast at height 99.
const broadcastHeight = 99
createTestChannel(t, cdb, pendingHeightOption(broadcastHeight))
@@ -963,12 +971,14 @@ func TestFetchPendingChannels(t *testing.T) {
func TestFetchClosedChannels(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create an open channel in the database.
state := createTestChannel(t, cdb, openChannelOption())
@@ -1054,18 +1064,20 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// We'll start by creating two channels within our test database. One of
// them will have their funding transaction confirmed on-chain, while
// the other one will remain unconfirmed.
db, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
channels := make([]*OpenChannel, numChannels)
for i := 0; i < numChannels; i++ {
// Create a pending channel in the database at the broadcast
// height.
channels[i] = createTestChannel(
t, db, pendingHeightOption(broadcastHeight),
t, cdb, pendingHeightOption(broadcastHeight),
)
}
@@ -1116,7 +1128,7 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// Now, we'll fetch all the channels waiting to be closed from the
// database. We should expect to see both channels above, even if any of
// them haven't had their funding transaction confirm on-chain.
waitingCloseChannels, err := db.FetchWaitingCloseChannels()
waitingCloseChannels, err := cdb.FetchWaitingCloseChannels()
if err != nil {
t.Fatalf("unable to fetch all waiting close channels: %v", err)
}
@@ -1169,12 +1181,14 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
func TestRefreshShortChanID(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// First create a test channel.
state := createTestChannel(t, cdb)
@@ -1317,13 +1331,15 @@ func TestCloseInitiator(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create an open channel.
channel := createTestChannel(
t, cdb, openChannelOption(),
@@ -1362,13 +1378,15 @@ func TestCloseInitiator(t *testing.T) {
// TestCloseChannelStatus tests setting of a channel status on the historical
// channel on channel close.
func TestCloseChannelStatus(t *testing.T) {
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create an open channel.
channel := createTestChannel(
t, cdb, openChannelOption(),
@@ -1427,7 +1445,7 @@ func TestBalanceAtHeight(t *testing.T) {
putRevokedState := func(c *OpenChannel, height uint64, local,
remote lnwire.MilliSatoshi) error {
err := kvdb.Update(c.Db, func(tx kvdb.RwTx) error {
err := kvdb.Update(c.Db.backend, func(tx kvdb.RwTx) error {
chanBucket, err := fetchChanBucketRw(
tx, c.IdentityPub, &c.FundingOutpoint,
c.ChainHash,
@@ -1508,13 +1526,15 @@ func TestBalanceAtHeight(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cdb, cleanUp, err := MakeTestDB()
fullDB, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
}
defer cleanUp()
cdb := fullDB.ChannelStateDB()
// Create options to set the heights and balances of
// our local and remote commitments.
localCommitOpt := channelCommitmentOption(