Skip to content

Commit 323b633

Browse files
committed
graph -> discovery: move ValidationBarrier to discovery
1 parent e0e4073 commit 323b633

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

discovery/gossiper.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ type AuthenticatedGossiper struct {
518518
chanUpdateRateLimiter map[uint64][2]*rate.Limiter
519519

520520
// vb is used to enforce job dependency ordering of gossip messages.
521-
vb *graph.ValidationBarrier
521+
vb *ValidationBarrier
522522

523523
sync.Mutex
524524
}
@@ -545,7 +545,7 @@ func New(cfg Config, selfKeyDesc *keychain.KeyDescriptor) *AuthenticatedGossiper
545545
banman: newBanman(),
546546
}
547547

548-
gossiper.vb = graph.NewValidationBarrier(1000, gossiper.quit)
548+
gossiper.vb = NewValidationBarrier(1000, gossiper.quit)
549549

550550
gossiper.syncMgr = newSyncManager(&SyncManagerCfg{
551551
ChainHash: cfg.ChainHash,
@@ -1543,7 +1543,7 @@ func (d *AuthenticatedGossiper) networkHandler() {
15431543
//
15441544
// NOTE: must be run as a goroutine.
15451545
func (d *AuthenticatedGossiper) handleNetworkMessages(nMsg *networkMsg,
1546-
deDuped *deDupedAnnouncements, jobID graph.JobID) {
1546+
deDuped *deDupedAnnouncements, jobID JobID) {
15471547

15481548
defer d.wg.Done()
15491549
defer d.vb.CompleteJob()
@@ -1559,12 +1559,7 @@ func (d *AuthenticatedGossiper) handleNetworkMessages(nMsg *networkMsg,
15591559
log.Debugf("Validating network message %s got err: %v",
15601560
nMsg.msg.MsgType(), err)
15611561

1562-
if !graph.IsError(
1563-
err,
1564-
graph.ErrVBarrierShuttingDown,
1565-
graph.ErrParentValidationFailed,
1566-
) {
1567-
1562+
if errors.Is(err, ErrVBarrierShuttingDown) {
15681563
log.Warnf("unexpected error during validation "+
15691564
"barrier shutdown: %v", err)
15701565
}
@@ -2423,7 +2418,6 @@ func (d *AuthenticatedGossiper) handleNodeAnnouncement(nMsg *networkMsg,
24232418
err,
24242419
graph.ErrOutdated,
24252420
graph.ErrIgnored,
2426-
graph.ErrVBarrierShuttingDown,
24272421
) {
24282422

24292423
log.Error(err)
@@ -3164,7 +3158,6 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
31643158
if graph.IsError(
31653159
err, graph.ErrOutdated,
31663160
graph.ErrIgnored,
3167-
graph.ErrVBarrierShuttingDown,
31683161
) {
31693162

31703163
log.Debugf("Update edge for short_chan_id(%v) got: %v",

graph/validation_barrier.go renamed to discovery/validation_barrier.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph
1+
package discovery
22

33
import (
44
"fmt"
@@ -11,6 +11,13 @@ import (
1111
"github.com/lightningnetwork/lnd/routing/route"
1212
)
1313

14+
var (
15+
// ErrVBarrierShuttingDown signals that the barrier has been requested
16+
// to shutdown, and that the caller should not treat the wait condition
17+
// as fulfilled.
18+
ErrVBarrierShuttingDown = errors.New("ValidationBarrier shutting down")
19+
)
20+
1421
// JobID identifies an active job in the validation barrier. It is large so
1522
// that we don't need to worry about overflows.
1623
type JobID uint64
@@ -88,7 +95,7 @@ func NewValidationBarrier(numActiveReqs int,
8895
}
8996

9097
// InitJobDependencies will wait for a new job slot to become open, and then
91-
// sets up any dependent signals/trigger for the new job
98+
// sets up any dependent signals/trigger for the new job.
9299
func (v *ValidationBarrier) InitJobDependencies(job interface{}) (JobID,
93100
error) {
94101

@@ -296,8 +303,7 @@ func (v *ValidationBarrier) WaitForParents(childJobID JobID,
296303
for {
297304
select {
298305
case <-v.quit:
299-
return NewErrf(ErrVBarrierShuttingDown,
300-
"validation barrier shutting down")
306+
return ErrVBarrierShuttingDown
301307

302308
case <-jobChan:
303309
// Every time this is sent on or if it's closed, a

graph/validation_barrier_test.go renamed to discovery/validation_barrier_test.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package graph_test
1+
package discovery
22

33
import (
44
"encoding/binary"
5+
"errors"
6+
"sync"
57
"testing"
68
"time"
79

8-
"github.com/lightningnetwork/lnd/graph"
910
"github.com/lightningnetwork/lnd/lnwire"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -22,18 +23,38 @@ func TestValidationBarrierSemaphore(t *testing.T) {
2223
)
2324

2425
quit := make(chan struct{})
25-
barrier := graph.NewValidationBarrier(numTasks, quit)
26+
barrier := NewValidationBarrier(numTasks, quit)
27+
28+
var scidMtx sync.RWMutex
29+
currentScid := lnwire.ShortChannelID{}
2630

2731
// Saturate the semaphore with jobs.
2832
for i := 0; i < numTasks; i++ {
29-
barrier.InitJobDependencies(nil)
33+
scidMtx.Lock()
34+
dummyUpdate := &lnwire.ChannelUpdate1{
35+
ShortChannelID: currentScid,
36+
}
37+
currentScid.TxIndex++
38+
scidMtx.Unlock()
39+
40+
_, err := barrier.InitJobDependencies(dummyUpdate)
41+
require.NoError(t, err)
3042
}
3143

3244
// Spawn additional tasks that will signal completion when added.
3345
jobAdded := make(chan struct{})
3446
for i := 0; i < numPendingTasks; i++ {
3547
go func() {
36-
barrier.InitJobDependencies(nil)
48+
scidMtx.Lock()
49+
dummyUpdate := &lnwire.ChannelUpdate1{
50+
ShortChannelID: currentScid,
51+
}
52+
currentScid.TxIndex++
53+
scidMtx.Unlock()
54+
55+
_, err := barrier.InitJobDependencies(dummyUpdate)
56+
require.NoError(t, err)
57+
3758
jobAdded <- struct{}{}
3859
}()
3960
}
@@ -70,12 +91,12 @@ func TestValidationBarrierQuit(t *testing.T) {
7091
)
7192

7293
quit := make(chan struct{})
73-
barrier := graph.NewValidationBarrier(2*numTasks, quit)
94+
barrier := NewValidationBarrier(2*numTasks, quit)
7495

7596
// Create a set of unique channel announcements that we will prep for
7697
// validation.
7798
anns := make([]*lnwire.ChannelAnnouncement1, 0, numTasks)
78-
parentJobIDs := make([]graph.JobID, 0, numTasks)
99+
parentJobIDs := make([]JobID, 0, numTasks)
79100
for i := 0; i < numTasks; i++ {
80101
anns = append(anns, &lnwire.ChannelAnnouncement1{
81102
ShortChannelID: lnwire.NewShortChanIDFromInt(uint64(i)),
@@ -91,7 +112,7 @@ func TestValidationBarrierQuit(t *testing.T) {
91112
// Create a set of channel updates, that must wait until their
92113
// associated channel announcement has been verified.
93114
chanUpds := make([]*lnwire.ChannelUpdate1, 0, numTasks)
94-
childJobIDs := make([]graph.JobID, 0, numTasks)
115+
childJobIDs := make([]JobID, 0, numTasks)
95116
for i := 0; i < numTasks; i++ {
96117
chanUpds = append(chanUpds, &lnwire.ChannelUpdate1{
97118
ShortChannelID: lnwire.NewShortChanIDFromInt(uint64(i)),
@@ -154,11 +175,12 @@ func TestValidationBarrierQuit(t *testing.T) {
154175
t.Fatalf("unexpected failure while waiting: %v", err)
155176

156177
// Last half should return the shutdown error.
157-
case i >= numTasks/2 && !graph.IsError(
158-
err, graph.ErrVBarrierShuttingDown,
178+
case i >= numTasks/2 && !errors.Is(
179+
err, ErrVBarrierShuttingDown,
159180
):
181+
160182
t.Fatalf("expected failure after quitting: want %v, "+
161-
"got %v", graph.ErrVBarrierShuttingDown, err)
183+
"got %v", ErrVBarrierShuttingDown, err)
162184
}
163185
}
164186
}
@@ -175,7 +197,7 @@ func TestValidationBarrierParentJobsClear(t *testing.T) {
175197
)
176198

177199
quit := make(chan struct{})
178-
barrier := graph.NewValidationBarrier(numTasks, quit)
200+
barrier := NewValidationBarrier(numTasks, quit)
179201

180202
sharedScid := lnwire.NewShortChanIDFromInt(0)
181203
sharedNodeID := nodeIDFromInt(0)
@@ -221,8 +243,8 @@ func TestValidationBarrierParentJobsClear(t *testing.T) {
221243
childID2, err := barrier.InitJobDependencies(node1)
222244
require.NoError(t, err)
223245

224-
run := func(vb *graph.ValidationBarrier, childJobID graph.JobID,
225-
job interface{}, resp chan error, start chan error) {
246+
run := func(vb *ValidationBarrier, childJobID JobID, job interface{},
247+
resp chan error, start chan error) {
226248

227249
close(start)
228250

graph/errors.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ const (
2828
// ErrInvalidFundingOutput is returned if the channel funding output
2929
// fails validation.
3030
ErrInvalidFundingOutput
31-
32-
// ErrVBarrierShuttingDown signals that the barrier has been requested
33-
// to shutdown, and that the caller should not treat the wait condition
34-
// as fulfilled.
35-
ErrVBarrierShuttingDown
36-
37-
// ErrParentValidationFailed signals that the validation of a
38-
// dependent's parent failed, so the dependent must not be processed.
39-
ErrParentValidationFailed
4031
)
4132

4233
// Error is a structure that represent the error inside the graph package,

0 commit comments

Comments
 (0)