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/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"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
// handle the case where a different tx actually hits the chain.
func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
locallyInitiated bool) error {
closer lntypes.ChannelParty) error {
return c.markBroadcasted(
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
// actually hits the chain.
func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
locallyInitiated bool) error {
closer lntypes.ChannelParty) error {
return c.markBroadcasted(
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
// indicates the party that initiated the channel close.
func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
closeTx *wire.MsgTx, locallyInitiated bool) error {
closeTx *wire.MsgTx, closer lntypes.ChannelParty) error {
c.Lock()
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
// set in addition to the broadcast status so that we do not need to
// migrate the original logic which does not store initiator.
if locallyInitiated {
if closer.IsLocal() {
status |= ChanStatusLocalCloseInitiator
} else {
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.
func (s *ShutdownInfo) encode(w io.Writer) error {
records := []tlv.Record{
+13 -6
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lntest/channels"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"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)
}
// Now try to marking a coop close with a nil tx. This should
// 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)
}
_, err := channel.BroadcastedCooperative()
@@ -1102,7 +1107,9 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// it as coop closed. Later we will test that distinct
// transactions are returned for both coop and force closes.
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)
}
}
@@ -1324,7 +1331,7 @@ func TestCloseInitiator(t *testing.T) {
// by the local party.
updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted(
&wire.MsgTx{}, true,
&wire.MsgTx{}, lntypes.Local,
)
},
expectedStatuses: []ChannelStatus{
@@ -1338,7 +1345,7 @@ func TestCloseInitiator(t *testing.T) {
// by the remote party.
updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted(
&wire.MsgTx{}, false,
&wire.MsgTx{}, lntypes.Remote,
)
},
expectedStatuses: []ChannelStatus{
@@ -1352,7 +1359,7 @@ func TestCloseInitiator(t *testing.T) {
// local initiator.
updateChannel: func(c *OpenChannel) error {
return c.MarkCommitmentBroadcasted(
&wire.MsgTx{}, true,
&wire.MsgTx{}, lntypes.Local,
)
},
expectedStatuses: []ChannelStatus{
+7 -2
View File
@@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/stretchr/testify/require"
@@ -606,7 +607,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(pendingWaitingChan),
)
err = pendingClosing.MarkCoopBroadcasted(nil, true)
err = pendingClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -626,7 +629,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(openWaitingChan),
openChannelOption(),
)
err = openClosing.MarkCoopBroadcasted(nil, true)
err = openClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}