multi: refactor select methods within channeldb to use ChannelParty

Also in this commit is a small adjustment to the call-sites to get
the boundaries stitched back together.
This commit is contained in:
Keagan McClelland committed 2024-07-31 14:50:26 -07:00
1 parent 3a15085014
commit 33934449ac
7 files changed
+58 -21

No files matched your search

+16 -6
View File
@@ -25,6 +25,7 @@ import (
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
"github.com/lightningnetwork/lnd/tlv" "github.com/lightningnetwork/lnd/tlv"
@@ -1690,11 +1691,11 @@ func (c *OpenChannel) isBorked(chanBucket kvdb.RBucket) (bool, error) {
// republish this tx at startup to ensure propagation, and we should still // republish this tx at startup to ensure propagation, and we should still
// handle the case where a different tx actually hits the chain. // handle the case where a different tx actually hits the chain.
func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx, func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
locallyInitiated bool) error { closer lntypes.ChannelParty) error {
return c.markBroadcasted( return c.markBroadcasted(
ChanStatusCommitBroadcasted, forceCloseTxKey, closeTx, ChanStatusCommitBroadcasted, forceCloseTxKey, closeTx,
locallyInitiated, closer,
) )
} }
@@ -1706,11 +1707,11 @@ func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
// ensure propagation, and we should still handle the case where a different tx // ensure propagation, and we should still handle the case where a different tx
// actually hits the chain. // actually hits the chain.
func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx, func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
locallyInitiated bool) error { closer lntypes.ChannelParty) error {
return c.markBroadcasted( return c.markBroadcasted(
ChanStatusCoopBroadcasted, coopCloseTxKey, closeTx, ChanStatusCoopBroadcasted, coopCloseTxKey, closeTx,
locallyInitiated, closer,
) )
} }
@@ -1719,7 +1720,7 @@ func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
// which should specify either a coop or force close. It adds a status which // which should specify either a coop or force close. It adds a status which
// indicates the party that initiated the channel close. // indicates the party that initiated the channel close.
func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte, func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
closeTx *wire.MsgTx, locallyInitiated bool) error { closeTx *wire.MsgTx, closer lntypes.ChannelParty) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
@@ -1741,7 +1742,7 @@ func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
// Add the initiator status to the status provided. These statuses are // Add the initiator status to the status provided. These statuses are
// set in addition to the broadcast status so that we do not need to // set in addition to the broadcast status so that we do not need to
// migrate the original logic which does not store initiator. // migrate the original logic which does not store initiator.
if locallyInitiated { if closer.IsLocal() {
status |= ChanStatusLocalCloseInitiator status |= ChanStatusLocalCloseInitiator
} else { } else {
status |= ChanStatusRemoteCloseInitiator status |= ChanStatusRemoteCloseInitiator
@@ -4486,6 +4487,15 @@ func NewShutdownInfo(deliveryScript lnwire.DeliveryAddress,
} }
} }
// Closer identifies the ChannelParty that initiated the coop-closure process.
func (s ShutdownInfo) Closer() lntypes.ChannelParty {
if s.LocalInitiator.Val {
return lntypes.Local
}
return lntypes.Remote
}
// encode serialises the ShutdownInfo to the given io.Writer. // encode serialises the ShutdownInfo to the given io.Writer.
func (s *ShutdownInfo) encode(w io.Writer) error { func (s *ShutdownInfo) encode(w io.Writer) error {
records := []tlv.Record{ records := []tlv.Record{
+13 -6
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnmock" "github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lntest/channels" "github.com/lightningnetwork/lnd/lntest/channels"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
"github.com/lightningnetwork/lnd/tlv" "github.com/lightningnetwork/lnd/tlv"
@@ -1084,13 +1085,17 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
}, },
) )
if err := channel.MarkCommitmentBroadcasted(closeTx, true); err != nil { if err := channel.MarkCommitmentBroadcasted(
closeTx, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark commitment broadcast: %v", err) t.Fatalf("unable to mark commitment broadcast: %v", err)
} }
// Now try to marking a coop close with a nil tx. This should // Now try to marking a coop close with a nil tx. This should
// succeed, but it shouldn't exit when queried. // succeed, but it shouldn't exit when queried.
if err = channel.MarkCoopBroadcasted(nil, true); err != nil { if err = channel.MarkCoopBroadcasted(
nil, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark nil coop broadcast: %v", err) t.Fatalf("unable to mark nil coop broadcast: %v", err)
} }
_, err := channel.BroadcastedCooperative() _, err := channel.BroadcastedCooperative()
@@ -1102,7 +1107,9 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// it as coop closed. Later we will test that distinct // it as coop closed. Later we will test that distinct
// transactions are returned for both coop and force closes. // transactions are returned for both coop and force closes.
closeTx.TxIn[0].PreviousOutPoint.Index ^= 1 closeTx.TxIn[0].PreviousOutPoint.Index ^= 1
if err := channel.MarkCoopBroadcasted(closeTx, true); err != nil { if err := channel.MarkCoopBroadcasted(
closeTx, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark coop broadcast: %v", err) t.Fatalf("unable to mark coop broadcast: %v", err)
} }
} }
@@ -1324,7 +1331,7 @@ func TestCloseInitiator(t *testing.T) {
// by the local party. // by the local party.
updateChannel: func(c *OpenChannel) error { updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted( return c.MarkCoopBroadcasted(
&wire.MsgTx{}, true, &wire.MsgTx{}, lntypes.Local,
) )
}, },
expectedStatuses: []ChannelStatus{ expectedStatuses: []ChannelStatus{
@@ -1338,7 +1345,7 @@ func TestCloseInitiator(t *testing.T) {
// by the remote party. // by the remote party.
updateChannel: func(c *OpenChannel) error { updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted( return c.MarkCoopBroadcasted(
&wire.MsgTx{}, false, &wire.MsgTx{}, lntypes.Remote,
) )
}, },
expectedStatuses: []ChannelStatus{ expectedStatuses: []ChannelStatus{
@@ -1352,7 +1359,7 @@ func TestCloseInitiator(t *testing.T) {
// local initiator. // local initiator.
updateChannel: func(c *OpenChannel) error { updateChannel: func(c *OpenChannel) error {
return c.MarkCommitmentBroadcasted( return c.MarkCommitmentBroadcasted(
&wire.MsgTx{}, true, &wire.MsgTx{}, lntypes.Local,
) )
}, },
expectedStatuses: []ChannelStatus{ expectedStatuses: []ChannelStatus{
+7 -2
View File
@@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@@ -606,7 +607,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(pendingWaitingChan), channelIDOption(pendingWaitingChan),
) )
err = pendingClosing.MarkCoopBroadcasted(nil, true) err = pendingClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@@ -626,7 +629,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(openWaitingChan), channelIDOption(openWaitingChan),
openChannelOption(), openChannelOption(),
) )
err = openClosing.MarkCoopBroadcasted(nil, true) err = openClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
+5 -2
View File
@@ -11,6 +11,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lntest/mock" "github.com/lightningnetwork/lnd/lntest/mock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -61,12 +62,14 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
for i := 0; i < numChans/2; i++ { for i := 0; i < numChans/2; i++ {
closeTx := channels[i].FundingTxn.Copy() closeTx := channels[i].FundingTxn.Copy()
closeTx.TxIn[0].PreviousOutPoint = channels[i].FundingOutpoint closeTx.TxIn[0].PreviousOutPoint = channels[i].FundingOutpoint
err := channels[i].MarkCommitmentBroadcasted(closeTx, true) err := channels[i].MarkCommitmentBroadcasted(
closeTx, lntypes.Local,
)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = channels[i].MarkCoopBroadcasted(closeTx, true) err = channels[i].MarkCoopBroadcasted(closeTx, lntypes.Local)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+2 -2
View File
@@ -129,7 +129,7 @@ type ChannelArbitratorConfig struct {
// MarkCommitmentBroadcasted should mark the channel as the commitment // MarkCommitmentBroadcasted should mark the channel as the commitment
// being broadcast, and we are waiting for the commitment to confirm. // being broadcast, and we are waiting for the commitment to confirm.
MarkCommitmentBroadcasted func(*wire.MsgTx, bool) error MarkCommitmentBroadcasted func(*wire.MsgTx, lntypes.ChannelParty) error
// MarkChannelClosed marks the channel closed in the database, with the // MarkChannelClosed marks the channel closed in the database, with the
// passed close summary. After this method successfully returns we can // passed close summary. After this method successfully returns we can
@@ -1084,7 +1084,7 @@ func (c *ChannelArbitrator) stateStep(
// database, such that we can re-publish later in case it // database, such that we can re-publish later in case it
// didn't propagate. We initiated the force close, so we // didn't propagate. We initiated the force close, so we
// mark broadcast with local initiator set to true. // mark broadcast with local initiator set to true.
err = c.cfg.MarkCommitmentBroadcasted(closeTx, true) err = c.cfg.MarkCommitmentBroadcasted(closeTx, lntypes.Local)
if err != nil { if err != nil {
log.Errorf("ChannelArbitrator(%v): unable to "+ log.Errorf("ChannelArbitrator(%v): unable to "+
"mark commitment broadcasted: %v", "mark commitment broadcasted: %v",
+3 -1
View File
@@ -416,7 +416,9 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
resolvedChan <- struct{}{} resolvedChan <- struct{}{}
return nil return nil
}, },
MarkCommitmentBroadcasted: func(_ *wire.MsgTx, _ bool) error { MarkCommitmentBroadcasted: func(_ *wire.MsgTx,
_ lntypes.ChannelParty) error {
return nil return nil
}, },
MarkChannelClosed: func(*channeldb.ChannelCloseSummary, MarkChannelClosed: func(*channeldb.ChannelCloseSummary,
+12 -2
View File
@@ -8460,7 +8460,12 @@ func (lc *LightningChannel) MarkCommitmentBroadcasted(tx *wire.MsgTx,
lc.Lock() lc.Lock()
defer lc.Unlock() defer lc.Unlock()
return lc.channelState.MarkCommitmentBroadcasted(tx, locallyInitiated) party := lntypes.Remote
if locallyInitiated {
party = lntypes.Local
}
return lc.channelState.MarkCommitmentBroadcasted(tx, party)
} }
// MarkCoopBroadcasted marks the channel as a cooperative close transaction has // MarkCoopBroadcasted marks the channel as a cooperative close transaction has
@@ -8473,7 +8478,12 @@ func (lc *LightningChannel) MarkCoopBroadcasted(tx *wire.MsgTx,
lc.Lock() lc.Lock()
defer lc.Unlock() defer lc.Unlock()
return lc.channelState.MarkCoopBroadcasted(tx, localInitiated) party := lntypes.Remote
if localInitiated {
party = lntypes.Local
}
return lc.channelState.MarkCoopBroadcasted(tx, party)
} }
// MarkShutdownSent persists the given ShutdownInfo. The existence of the // MarkShutdownSent persists the given ShutdownInfo. The existence of the