Skip to content

Commit 39bb23e

Browse files
committed
discovery: lock the channelMtx before making the funding script
As we move the funding transaction validation logic out of the builder and into the gossiper, we want to ensure that the behaviour stays consistent with what we have today. So we should aquire this lock before performing any expensive checks such as building the funding tx or valdating it.
1 parent 7853e36 commit 39bb23e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

discovery/gossiper.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,14 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
26342634
tapscriptRoot = nMsg.optionalMsgFields.tapscriptRoot
26352635
}
26362636

2637+
// Before we start validation or add the edge to the database, we obtain
2638+
// the mutex for this channel ID. We do this to ensure no other
2639+
// goroutine has read the database and is now making decisions based on
2640+
// this DB state, before it writes to the DB. It also ensures that we
2641+
// don't perform the expensive validation check on the same channel
2642+
// announcement at the same time.
2643+
d.channelMtx.Lock(scid.ToUint64())
2644+
26372645
// We only make use of the funding script later on during funding
26382646
// transaction validation if AssumeChannelValid is not true.
26392647
if !(d.cfg.AssumeChannelValid || d.cfg.IsAlias(scid)) {
@@ -2642,6 +2650,8 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
26422650
tapscriptRoot,
26432651
)
26442652
if err != nil {
2653+
defer d.channelMtx.Unlock(scid.ToUint64())
2654+
26452655
log.Errorf("Unable to make funding script %v", err)
26462656
nMsg.err <- err
26472657

@@ -2656,12 +2666,6 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
26562666
// We will add the edge to the channel router. If the nodes present in
26572667
// this channel are not present in the database, a partial node will be
26582668
// added to represent each node while we wait for a node announcement.
2659-
//
2660-
// Before we add the edge to the database, we obtain the mutex for this
2661-
// channel ID. We do this to ensure no other goroutine has read the
2662-
// database and is now making decisions based on this DB state, before
2663-
// it writes to the DB.
2664-
d.channelMtx.Lock(scid.ToUint64())
26652669
err = d.cfg.Graph.AddEdge(edge, ops...)
26662670
if err != nil {
26672671
log.Debugf("Graph rejected edge for short_chan_id(%v): %v",

0 commit comments

Comments
 (0)