From fea9c171ee0423ae0d4e533b72290f251ead96df Mon Sep 17 00:00:00 2001 From: ziggie Date: Fri, 15 May 2026 13:07:27 -0300 Subject: [PATCH] chanstate: use channel types in consumers Move channelnotifier and invoice hop-hint code to the chanstate channel types. These consumers already depend on the chanstate store interfaces, so they no longer need to refer to the channeldb compatibility aliases for OpenChannel and ChannelCloseSummary. --- channelnotifier/channelnotifier.go | 15 ++++--- channelnotifier/channelnotifier_test.go | 12 +++--- lnrpc/invoicesrpc/addinvoice.go | 13 +++--- lnrpc/invoicesrpc/addinvoice_test.go | 54 +++++++++++++------------ 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/channelnotifier/channelnotifier.go b/channelnotifier/channelnotifier.go index 733242167..c9430dd72 100644 --- a/channelnotifier/channelnotifier.go +++ b/channelnotifier/channelnotifier.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/btcsuite/btcd/wire/v2" - "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/chanstate" "github.com/lightningnetwork/lnd/subscribe" ) @@ -31,14 +30,14 @@ type PendingOpenChannelEvent struct { // channel. This might not have been persisted to the channel DB yet // because we are still waiting for the final message from the remote // peer. - PendingChannel *channeldb.OpenChannel + PendingChannel *chanstate.OpenChannel } // OpenChannelEvent represents a new event where a channel goes from pending // open to open. type OpenChannelEvent struct { // Channel is the channel that has become open. - Channel *channeldb.OpenChannel + Channel *chanstate.OpenChannel } // ActiveLinkEvent represents a new event where the link becomes active in the @@ -70,13 +69,13 @@ type InactiveChannelEvent struct { // ClosedChannelEvent represents a new event where a channel becomes closed. type ClosedChannelEvent struct { // CloseSummary is the summary of the channel close that has occurred. - CloseSummary *channeldb.ChannelCloseSummary + CloseSummary *chanstate.ChannelCloseSummary } // ChannelUpdateEvent represents a new event where a channel's state is updated. type ChannelUpdateEvent struct { // Channel is the channel that has been updated. - Channel *channeldb.OpenChannel + Channel *chanstate.OpenChannel } // FullyResolvedChannelEvent represents a new event where a channel becomes @@ -148,7 +147,7 @@ func (c *ChannelNotifier) SubscribeChannelEvents() (*subscribe.Client, error) { // persisted to the DB because we still wait for the final message from the // remote peer. func (c *ChannelNotifier) NotifyPendingOpenChannelEvent(chanPoint wire.OutPoint, - pendingChan *channeldb.OpenChannel) { + pendingChan *chanstate.OpenChannel) { event := PendingOpenChannelEvent{ ChannelPoint: &chanPoint, @@ -200,7 +199,7 @@ func (c *ChannelNotifier) NotifyClosedChannelEvent(chanPoint wire.OutPoint) { // IsPending field will typically be true at this point; callers should set it // accordingly. func (c *ChannelNotifier) NotifyEarlyClosedChannelEvent( - summary *channeldb.ChannelCloseSummary) { + summary *chanstate.ChannelCloseSummary) { event := ClosedChannelEvent{CloseSummary: summary} if err := c.ntfnServer.SendUpdate(event); err != nil { @@ -271,7 +270,7 @@ func (c *ChannelNotifier) NotifyInactiveChannelEvent(chanPoint wire.OutPoint) { // NotifyChannelUpdateEvent notifies subscribers that a channel's state has been // updated. func (c *ChannelNotifier) NotifyChannelUpdateEvent( - channel *channeldb.OpenChannel) { + channel *chanstate.OpenChannel) { event := ChannelUpdateEvent{Channel: channel} if err := c.ntfnServer.SendUpdate(event); err != nil { diff --git a/channelnotifier/channelnotifier_test.go b/channelnotifier/channelnotifier_test.go index afeea30f8..165d0e34d 100644 --- a/channelnotifier/channelnotifier_test.go +++ b/channelnotifier/channelnotifier_test.go @@ -6,7 +6,7 @@ import ( "github.com/btcsuite/btcd/chainhash/v2" "github.com/btcsuite/btcd/wire/v2" - "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/chanstate" "github.com/stretchr/testify/require" ) @@ -27,7 +27,7 @@ func TestChannelUpdateEvent(t *testing.T) { defer sub.Cancel() // Create a mock channel state. - channel := &channeldb.OpenChannel{} + channel := &chanstate.OpenChannel{} // Notify the server of a channel update event. ntfnServer.NotifyChannelUpdateEvent(channel) @@ -69,9 +69,9 @@ func TestNotifyEarlyClosedChannelEvent(t *testing.T) { Hash: chainhash.Hash{0x01, 0x02, 0x03}, Index: 4, } - summary := &channeldb.ChannelCloseSummary{ + summary := &chanstate.ChannelCloseSummary{ ChanPoint: chanPoint, - CloseType: channeldb.CooperativeClose, + CloseType: chanstate.CooperativeClose, IsPending: true, } @@ -111,9 +111,9 @@ func TestNotifyEarlyClosedChannelEventSingleEvent(t *testing.T) { require.NoError(t, err) t.Cleanup(sub.Cancel) - summary := &channeldb.ChannelCloseSummary{ + summary := &chanstate.ChannelCloseSummary{ ChanPoint: wire.OutPoint{Index: 7}, - CloseType: channeldb.CooperativeClose, + CloseType: chanstate.CooperativeClose, IsPending: true, } ntfnServer.NotifyEarlyClosedChannelEvent(summary) diff --git a/lnrpc/invoicesrpc/addinvoice.go b/lnrpc/invoicesrpc/addinvoice.go index 761055265..42839d851 100644 --- a/lnrpc/invoicesrpc/addinvoice.go +++ b/lnrpc/invoicesrpc/addinvoice.go @@ -17,7 +17,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/v2" "github.com/btcsuite/btcd/chainhash/v2" "github.com/btcsuite/btcd/wire/v2" - "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/chanstate" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/invoices" @@ -732,7 +731,7 @@ type HopHintInfo struct { ScidAliasFeature bool } -func newHopHintInfo(c *channeldb.OpenChannel, isActive bool) *HopHintInfo { +func newHopHintInfo(c *chanstate.OpenChannel, isActive bool) *HopHintInfo { isPublic := c.ChannelFlags&lnwire.FFAnnounceChannel != 0 return &HopHintInfo{ @@ -783,7 +782,7 @@ type SelectHopHintsCfg struct { // FetchAllChannels retrieves all open channels currently stored // within the database. - FetchAllChannels func() ([]*channeldb.OpenChannel, error) + FetchAllChannels func() ([]*chanstate.OpenChannel, error) // IsChannelActive checks whether the channel identified by the provided // ChannelID is considered active. @@ -846,7 +845,7 @@ func sufficientHints(nHintsLeft int, currentAmount, // getPotentialHints returns a slice of open channels that should be considered // for the hopHint list in an invoice. The slice is sorted in descending order // based on the remote balance. -func getPotentialHints(cfg *SelectHopHintsCfg) ([]*channeldb.OpenChannel, +func getPotentialHints(cfg *SelectHopHintsCfg) ([]*chanstate.OpenChannel, error) { // TODO(positiveblue): get the channels slice already filtered by @@ -856,7 +855,7 @@ func getPotentialHints(cfg *SelectHopHintsCfg) ([]*channeldb.OpenChannel, return nil, err } - privateChannels := make([]*channeldb.OpenChannel, 0, len(openChannels)) + privateChannels := make([]*chanstate.OpenChannel, 0, len(openChannels)) for _, oc := range openChannels { isPublic := oc.ChannelFlags&lnwire.FFAnnounceChannel != 0 if !isPublic { @@ -878,7 +877,7 @@ func getPotentialHints(cfg *SelectHopHintsCfg) ([]*channeldb.OpenChannel, // shouldIncludeChannel returns true if the channel passes all the checks to // be a hopHint in a given invoice. func shouldIncludeChannel(cfg *SelectHopHintsCfg, - channel *channeldb.OpenChannel, + channel *chanstate.OpenChannel, alreadyIncluded map[uint64]bool) (zpay32.HopHint, lnwire.MilliSatoshi, bool) { @@ -924,7 +923,7 @@ func shouldIncludeChannel(cfg *SelectHopHintsCfg, // descending priority. func selectHopHints(cfg *SelectHopHintsCfg, nHintsLeft int, targetBandwidth lnwire.MilliSatoshi, - potentialHints []*channeldb.OpenChannel, + potentialHints []*chanstate.OpenChannel, alreadyIncluded map[uint64]bool) [][]zpay32.HopHint { currentBandwidth := lnwire.MilliSatoshi(0) diff --git a/lnrpc/invoicesrpc/addinvoice_test.go b/lnrpc/invoicesrpc/addinvoice_test.go index c4f402e48..ac8408f52 100644 --- a/lnrpc/invoicesrpc/addinvoice_test.go +++ b/lnrpc/invoicesrpc/addinvoice_test.go @@ -8,7 +8,7 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/chainhash/v2" "github.com/btcsuite/btcd/wire/v2" - "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/chanstate" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/invoices" "github.com/lightningnetwork/lnd/lnwire" @@ -78,11 +78,15 @@ func (h *hopHintsConfigMock) GetAlias( // FetchAllChannels retrieves all open channels currently stored // within the database. -func (h *hopHintsConfigMock) FetchAllChannels() ([]*channeldb.OpenChannel, +func (h *hopHintsConfigMock) FetchAllChannels() ([]*chanstate.OpenChannel, error) { args := h.Mock.Called() - return args.Get(0).([]*channeldb.OpenChannel), args.Error(1) + + channels, ok := args.Get(0).([]*chanstate.OpenChannel) + require.True(h.t, ok) + + return channels, args.Error(1) } // FetchChannelEdgesByID attempts to lookup the two directed edges for @@ -121,7 +125,7 @@ func getTestPubKey() *btcec.PublicKey { var shouldIncludeChannelTestCases = []struct { name string setupMock func(*hopHintsConfigMock) - channel *channeldb.OpenChannel + channel *chanstate.OpenChannel alreadyIncluded map[uint64]bool cfg *SelectHopHintsCfg hopHint zpay32.HopHint @@ -131,7 +135,7 @@ var shouldIncludeChannelTestCases = []struct { name: "already included channels should not be included " + "again", alreadyIncluded: map[uint64]bool{1: true}, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ ShortChannelID: lnwire.NewShortChanIDFromInt(1), }, include: false, @@ -146,7 +150,7 @@ var shouldIncludeChannelTestCases = []struct { "IsChannelActive", chanID, ).Once().Return(true) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, @@ -163,7 +167,7 @@ var shouldIncludeChannelTestCases = []struct { "IsChannelActive", chanID, ).Once().Return(false) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, @@ -185,7 +189,7 @@ var shouldIncludeChannelTestCases = []struct { "IsPublicNode", mock.Anything, ).Once().Return(false, nil) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, @@ -220,7 +224,7 @@ var shouldIncludeChannelTestCases = []struct { "FetchChannelEdgesByID", mock.Anything, ).Once().Return(nil, nil, nil, fmt.Errorf("no edge")) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, @@ -256,12 +260,12 @@ var shouldIncludeChannelTestCases = []struct { "GetAlias", mock.Anything, ).Once().Return(lnwire.ShortChannelID{}, nil) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, IdentityPub: getTestPubKey(), - ChanType: channeldb.ScidAliasFeatureBit, + ChanType: chanstate.ScidAliasFeatureBit, }, include: false, }, { @@ -294,12 +298,12 @@ var shouldIncludeChannelTestCases = []struct { "GetAlias", mock.Anything, ).Once().Return(alias, nil) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 0, }, IdentityPub: getTestPubKey(), - ChanType: channeldb.ScidAliasFeatureBit, + ChanType: chanstate.ScidAliasFeatureBit, }, include: false, }, { @@ -347,7 +351,7 @@ var shouldIncludeChannelTestCases = []struct { nil, ) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 1, }, @@ -394,7 +398,7 @@ var shouldIncludeChannelTestCases = []struct { }, nil, ) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 1, }, @@ -447,13 +451,13 @@ var shouldIncludeChannelTestCases = []struct { "GetAlias", mock.Anything, ).Once().Return(aliasSCID, nil) }, - channel: &channeldb.OpenChannel{ + channel: &chanstate.OpenChannel{ FundingOutpoint: wire.OutPoint{ Index: 1, }, IdentityPub: getTestPubKey(), ShortChannelID: lnwire.NewShortChanIDFromInt(12), - ChanType: channeldb.ScidAliasFeatureBit, + ChanType: chanstate.ScidAliasFeatureBit, }, hopHint: zpay32.HopHint{ NodeID: getTestPubKey(), @@ -571,7 +575,7 @@ var populateHopHintsTestCases = []struct { setupMock: func(h *hopHintsConfigMock) { fundingOutpoint := wire.OutPoint{Index: 9} chanID := lnwire.NewChanIDFromOutPoint(fundingOutpoint) - allChannels := []*channeldb.OpenChannel{ + allChannels := []*chanstate.OpenChannel{ { FundingOutpoint: fundingOutpoint, ShortChannelID: lnwire.NewShortChanIDFromInt(9), @@ -618,9 +622,9 @@ var populateHopHintsTestCases = []struct { fundingOutpoint := wire.OutPoint{Index: 9} chanID := lnwire.NewChanIDFromOutPoint(fundingOutpoint) remoteBalance := lnwire.MilliSatoshi(10_000_000) - allChannels := []*channeldb.OpenChannel{ + allChannels := []*chanstate.OpenChannel{ { - LocalCommitment: channeldb.ChannelCommitment{ + LocalCommitment: chanstate.ChannelCommitment{ RemoteBalance: remoteBalance, }, FundingOutpoint: fundingOutpoint, @@ -669,12 +673,12 @@ var populateHopHintsTestCases = []struct { fundingOutpoint := wire.OutPoint{Index: 9} chanID := lnwire.NewChanIDFromOutPoint(fundingOutpoint) remoteBalance := lnwire.MilliSatoshi(10_000_000) - allChannels := []*channeldb.OpenChannel{ + allChannels := []*chanstate.OpenChannel{ // Because the channels with higher remote balance have // enough bandwidth we should never use this one. {}, { - LocalCommitment: channeldb.ChannelCommitment{ + LocalCommitment: chanstate.ChannelCommitment{ RemoteBalance: remoteBalance, }, FundingOutpoint: fundingOutpoint, @@ -868,11 +872,11 @@ func setupMockTwoChannels(h *hopHintsConfigMock) (lnwire.ChannelID, chanID2 := lnwire.NewChanIDFromOutPoint(fundingOutpoint2) remoteBalance2 := lnwire.MilliSatoshi(1_000_000) - allChannels := []*channeldb.OpenChannel{ + allChannels := []*chanstate.OpenChannel{ // After sorting we will first process chanID1 and then // chanID2. { - LocalCommitment: channeldb.ChannelCommitment{ + LocalCommitment: chanstate.ChannelCommitment{ RemoteBalance: remoteBalance2, }, FundingOutpoint: fundingOutpoint2, @@ -880,7 +884,7 @@ func setupMockTwoChannels(h *hopHintsConfigMock) (lnwire.ChannelID, IdentityPub: getTestPubKey(), }, { - LocalCommitment: channeldb.ChannelCommitment{ + LocalCommitment: chanstate.ChannelCommitment{ RemoteBalance: remoteBalance1, }, FundingOutpoint: fundingOutpoint1,