Skip to content

Commit dc553c3

Browse files
committed
graph/db: move Topology client management to ChannelGraph
1 parent 71c7e9d commit dc553c3

File tree

9 files changed

+208
-158
lines changed

9 files changed

+208
-158
lines changed

autopilot/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/btcsuite/btcd/btcec/v2"
88
"github.com/btcsuite/btcd/wire"
9-
"github.com/lightningnetwork/lnd/graph"
9+
graphdb "github.com/lightningnetwork/lnd/graph/db"
1010
"github.com/lightningnetwork/lnd/lnwallet"
1111
"github.com/lightningnetwork/lnd/lnwire"
1212
)
@@ -36,7 +36,7 @@ type ManagerCfg struct {
3636

3737
// SubscribeTopology is used to get a subscription for topology changes
3838
// on the network.
39-
SubscribeTopology func() (*graph.TopologyClient, error)
39+
SubscribeTopology func() (*graphdb.TopologyClient, error)
4040
}
4141

4242
// Manager is struct that manages an autopilot agent, making it possible to

graph/builder.go

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ type Builder struct {
109109
started atomic.Bool
110110
stopped atomic.Bool
111111

112-
ntfnClientCounter atomic.Uint64
113-
bestHeight atomic.Uint32
112+
bestHeight atomic.Uint32
114113

115114
cfg *Config
116115

@@ -123,22 +122,6 @@ type Builder struct {
123122
// of our currently known best chain are sent over.
124123
staleBlocks <-chan *chainview.FilteredBlock
125124

126-
// topologyUpdate is a channel that carries new topology updates
127-
// messages from outside the Builder to be processed by the
128-
// networkHandler.
129-
topologyUpdate chan any
130-
131-
// topologyClients maps a client's unique notification ID to a
132-
// topologyClient client that contains its notification dispatch
133-
// channel.
134-
topologyClients *lnutils.SyncMap[uint64, *topologyClient]
135-
136-
// ntfnClientUpdates is a channel that's used to send new updates to
137-
// topology notification clients to the Builder. Updates either
138-
// add a new notification client, or cancel notifications for an
139-
// existing client.
140-
ntfnClientUpdates chan *topologyClientUpdate
141-
142125
// channelEdgeMtx is a mutex we use to make sure we process only one
143126
// ChannelEdgePolicy at a time for a given channelID, to ensure
144127
// consistency between the various database accesses.
@@ -163,14 +146,11 @@ var _ ChannelGraphSource = (*Builder)(nil)
163146
// NewBuilder constructs a new Builder.
164147
func NewBuilder(cfg *Config) (*Builder, error) {
165148
return &Builder{
166-
cfg: cfg,
167-
topologyUpdate: make(chan any),
168-
topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{},
169-
ntfnClientUpdates: make(chan *topologyClientUpdate),
170-
channelEdgeMtx: multimutex.NewMutex[uint64](),
171-
statTicker: ticker.New(defaultStatInterval),
172-
stats: new(builderStats),
173-
quit: make(chan struct{}),
149+
cfg: cfg,
150+
channelEdgeMtx: multimutex.NewMutex[uint64](),
151+
statTicker: ticker.New(defaultStatInterval),
152+
stats: new(builderStats),
153+
quit: make(chan struct{}),
174154
}, nil
175155
}
176156

@@ -656,28 +636,6 @@ func (b *Builder) pruneZombieChans() error {
656636
return nil
657637
}
658638

659-
// handleTopologyUpdate is responsible for sending any topology changes
660-
// notifications to registered clients.
661-
//
662-
// NOTE: must be run inside goroutine.
663-
func (b *Builder) handleTopologyUpdate(update any) {
664-
defer b.wg.Done()
665-
666-
topChange := &TopologyChange{}
667-
err := addToTopologyChange(b.cfg.Graph, topChange, update)
668-
if err != nil {
669-
log.Errorf("unable to update topology change notification: %v",
670-
err)
671-
return
672-
}
673-
674-
if topChange.isEmpty() {
675-
return
676-
}
677-
678-
b.notifyTopologyChange(topChange)
679-
}
680-
681639
// networkHandler is the primary goroutine for the Builder. The roles of
682640
// this goroutine include answering queries related to the state of the
683641
// network, pruning the graph on new block notification, applying network
@@ -701,16 +659,6 @@ func (b *Builder) networkHandler() {
701659
}
702660

703661
select {
704-
// A new fully validated topology update has just arrived.
705-
// We'll notify any registered clients.
706-
case update := <-b.topologyUpdate:
707-
b.wg.Add(1)
708-
go b.handleTopologyUpdate(update)
709-
710-
// TODO(roasbeef): remove all unconnected vertexes
711-
// after N blocks pass with no corresponding
712-
// announcements.
713-
714662
case chainUpdate, ok := <-b.staleBlocks:
715663
// If the channel has been closed, then this indicates
716664
// the daemon is shutting down, so we exit ourselves.
@@ -783,31 +731,6 @@ func (b *Builder) networkHandler() {
783731
" processed.", chainUpdate.Height)
784732
}
785733

786-
// A new notification client update has arrived. We're either
787-
// gaining a new client, or cancelling notifications for an
788-
// existing client.
789-
case ntfnUpdate := <-b.ntfnClientUpdates:
790-
clientID := ntfnUpdate.clientID
791-
792-
if ntfnUpdate.cancel {
793-
client, ok := b.topologyClients.LoadAndDelete(
794-
clientID,
795-
)
796-
if ok {
797-
close(client.exit)
798-
client.wg.Wait()
799-
800-
close(client.ntfnChan)
801-
}
802-
803-
continue
804-
}
805-
806-
b.topologyClients.Store(clientID, &topologyClient{
807-
ntfnChan: ntfnUpdate.ntfnChan,
808-
exit: make(chan struct{}),
809-
})
810-
811734
// The graph prune ticker has ticked, so we'll examine the
812735
// state of the known graph to filter out any zombie channels
813736
// for pruning.
@@ -934,16 +857,6 @@ func (b *Builder) updateGraphWithClosedChannels(
934857
log.Infof("Block %v (height=%v) closed %v channels", chainUpdate.Hash,
935858
blockHeight, len(chansClosed))
936859

937-
if len(chansClosed) == 0 {
938-
return err
939-
}
940-
941-
// Notify all currently registered clients of the newly closed channels.
942-
closeSummaries := createCloseSummaries(blockHeight, chansClosed...)
943-
b.notifyTopologyChange(&TopologyChange{
944-
ClosedChannels: closeSummaries,
945-
})
946-
947860
return nil
948861
}
949862

@@ -1073,12 +986,6 @@ func (b *Builder) AddNode(node *models.LightningNode,
1073986
return err
1074987
}
1075988

1076-
select {
1077-
case b.topologyUpdate <- node:
1078-
case <-b.quit:
1079-
return ErrGraphBuilderShuttingDown
1080-
}
1081-
1082989
return nil
1083990
}
1084991

@@ -1129,12 +1036,6 @@ func (b *Builder) AddEdge(edge *models.ChannelEdgeInfo,
11291036
return err
11301037
}
11311038

1132-
select {
1133-
case b.topologyUpdate <- edge:
1134-
case <-b.quit:
1135-
return ErrGraphBuilderShuttingDown
1136-
}
1137-
11381039
return nil
11391040
}
11401041

@@ -1242,12 +1143,6 @@ func (b *Builder) UpdateEdge(update *models.ChannelEdgePolicy,
12421143
return err
12431144
}
12441145

1245-
select {
1246-
case b.topologyUpdate <- update:
1247-
case <-b.quit:
1248-
return ErrGraphBuilderShuttingDown
1249-
}
1250-
12511146
return nil
12521147
}
12531148

0 commit comments

Comments
 (0)