Skip to content

Commit b117daa

Browse files
committed
discovery+graph: convert errors from codes to variables
In preparation for moving funding transaction validiation from the Builder to the Gossiper in later commit, we first convert these graph Error Codes to normal error variables. This will help make the later commit a pure code move.
1 parent 3c0350e commit b117daa

File tree

5 files changed

+49
-59
lines changed

5 files changed

+49
-59
lines changed

discovery/gossiper.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,10 +2678,9 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
26782678

26792679
return anns, true
26802680

2681-
case graph.IsError(
2682-
err, graph.ErrNoFundingTransaction,
2683-
graph.ErrInvalidFundingOutput,
2684-
):
2681+
case errors.Is(err, graph.ErrNoFundingTransaction),
2682+
errors.Is(err, graph.ErrInvalidFundingOutput):
2683+
26852684
key := newRejectCacheKey(
26862685
scid.ToUint64(),
26872686
sourceToPub(nMsg.source),
@@ -2695,7 +2694,7 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
26952694
d.banman.incrementBanScore(nMsg.peer.PubKey())
26962695
}
26972696

2698-
case graph.IsError(err, graph.ErrChannelSpent):
2697+
case errors.Is(err, graph.ErrChannelSpent):
26992698
key := newRejectCacheKey(
27002699
scid.ToUint64(),
27012700
sourceToPub(nMsg.source),

discovery/gossiper_test.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/lightningnetwork/lnd/batch"
2525
"github.com/lightningnetwork/lnd/chainntnfs"
2626
"github.com/lightningnetwork/lnd/channeldb"
27-
"github.com/lightningnetwork/lnd/fn/v2"
2827
"github.com/lightningnetwork/lnd/graph"
2928
graphdb "github.com/lightningnetwork/lnd/graph/db"
3029
"github.com/lightningnetwork/lnd/graph/db/models"
@@ -76,13 +75,13 @@ var (
7675
type mockGraphSource struct {
7776
bestHeight uint32
7877

79-
mu sync.Mutex
80-
nodes []models.LightningNode
81-
infos map[uint64]models.ChannelEdgeInfo
82-
edges map[uint64][]models.ChannelEdgePolicy
83-
zombies map[uint64][][33]byte
84-
chansToReject map[uint64]struct{}
85-
addEdgeErrCode fn.Option[graph.ErrorCode]
78+
mu sync.Mutex
79+
nodes []models.LightningNode
80+
infos map[uint64]models.ChannelEdgeInfo
81+
edges map[uint64][]models.ChannelEdgePolicy
82+
zombies map[uint64][][33]byte
83+
chansToReject map[uint64]struct{}
84+
addEdgeErr error
8685
}
8786

8887
func newMockRouter(height uint32) *mockGraphSource {
@@ -113,10 +112,8 @@ func (r *mockGraphSource) AddEdge(info *models.ChannelEdgeInfo,
113112
r.mu.Lock()
114113
defer r.mu.Unlock()
115114

116-
if r.addEdgeErrCode.IsSome() {
117-
return graph.NewErrf(
118-
r.addEdgeErrCode.UnsafeFromSome(), "received error",
119-
)
115+
if r.addEdgeErr != nil {
116+
return r.addEdgeErr
120117
}
121118

122119
if _, ok := r.infos[info.ChannelID]; ok {
@@ -131,12 +128,12 @@ func (r *mockGraphSource) AddEdge(info *models.ChannelEdgeInfo,
131128
return nil
132129
}
133130

134-
func (r *mockGraphSource) resetAddEdgeErrCode() {
135-
r.addEdgeErrCode = fn.None[graph.ErrorCode]()
131+
func (r *mockGraphSource) resetAddEdgeErr() {
132+
r.addEdgeErr = nil
136133
}
137134

138-
func (r *mockGraphSource) setAddEdgeErrCode(code graph.ErrorCode) {
139-
r.addEdgeErrCode = fn.Some[graph.ErrorCode](code)
135+
func (r *mockGraphSource) setAddEdgeErr(err error) {
136+
r.addEdgeErr = err
140137
}
141138

142139
func (r *mockGraphSource) queueValidationFail(chanID uint64) {
@@ -4185,7 +4182,7 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
41854182
remoteKeyPriv2.PubKey(), nil, nil, atomic.Bool{},
41864183
}
41874184

4188-
ctx.router.setAddEdgeErrCode(graph.ErrInvalidFundingOutput)
4185+
ctx.router.setAddEdgeErr(graph.ErrInvalidFundingOutput)
41894186

41904187
// Loop 100 times to get nodePeer banned.
41914188
for i := 0; i < 100; i++ {
@@ -4199,11 +4196,7 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
41994196
case err = <-ctx.gossiper.ProcessRemoteAnnouncement(
42004197
ca, nodePeer1,
42014198
):
4202-
require.True(
4203-
t, graph.IsError(
4204-
err, graph.ErrInvalidFundingOutput,
4205-
),
4206-
)
4199+
require.ErrorIs(t, err, graph.ErrInvalidFundingOutput)
42074200

42084201
case <-time.After(2 * time.Second):
42094202
t.Fatalf("remote announcement not processed")
@@ -4221,11 +4214,11 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
42214214

42224215
// Set the error to ErrChannelSpent so that we can test that the
42234216
// gossiper ignores closed channels.
4224-
ctx.router.setAddEdgeErrCode(graph.ErrChannelSpent)
4217+
ctx.router.setAddEdgeErr(graph.ErrChannelSpent)
42254218

42264219
select {
42274220
case err = <-ctx.gossiper.ProcessRemoteAnnouncement(ca, nodePeer2):
4228-
require.True(t, graph.IsError(err, graph.ErrChannelSpent))
4221+
require.ErrorIs(t, err, graph.ErrChannelSpent)
42294222

42304223
case <-time.After(2 * time.Second):
42314224
t.Fatalf("remote announcement not processed")
@@ -4248,7 +4241,7 @@ func TestChanAnnBanningNonChanPeer(t *testing.T) {
42484241

42494242
// Reset the AddEdge error and pass the same announcement again. An
42504243
// error should be returned even though AddEdge won't fail.
4251-
ctx.router.resetAddEdgeErrCode()
4244+
ctx.router.resetAddEdgeErr()
42524245

42534246
select {
42544247
case err = <-ctx.gossiper.ProcessRemoteAnnouncement(ca, nodePeer2):
@@ -4269,7 +4262,7 @@ func TestChanAnnBanningChanPeer(t *testing.T) {
42694262

42704263
nodePeer := &mockPeer{remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{}}
42714264

4272-
ctx.router.setAddEdgeErrCode(graph.ErrInvalidFundingOutput)
4265+
ctx.router.setAddEdgeErr(graph.ErrInvalidFundingOutput)
42734266

42744267
// Loop 100 times to get nodePeer banned.
42754268
for i := 0; i < 100; i++ {
@@ -4283,11 +4276,7 @@ func TestChanAnnBanningChanPeer(t *testing.T) {
42834276
case err = <-ctx.gossiper.ProcessRemoteAnnouncement(
42844277
ca, nodePeer,
42854278
):
4286-
require.True(
4287-
t, graph.IsError(
4288-
err, graph.ErrInvalidFundingOutput,
4289-
),
4290-
)
4279+
require.ErrorIs(t, err, graph.ErrInvalidFundingOutput)
42914280

42924281
case <-time.After(2 * time.Second):
42934282
t.Fatalf("remote announcement not processed")

graph/builder.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,7 @@ func (b *Builder) addEdge(edge *models.ChannelEdgeInfo,
13141314
default:
13151315
}
13161316

1317-
return NewErrf(ErrNoFundingTransaction, "unable to "+
1318-
"locate funding tx: %v", err)
1317+
return fmt.Errorf("%w: %w", ErrNoFundingTransaction, err)
13191318
}
13201319

13211320
// Recreate witness output to be sure that declared in channel edge
@@ -1348,8 +1347,7 @@ func (b *Builder) addEdge(edge *models.ChannelEdgeInfo,
13481347
return err
13491348
}
13501349

1351-
return NewErrf(ErrInvalidFundingOutput, "output failed "+
1352-
"validation: %w", err)
1350+
return fmt.Errorf("%w: %w", ErrInvalidFundingOutput, err)
13531351
}
13541352

13551353
// Now that we have the funding outpoint of the channel, ensure
@@ -1366,8 +1364,8 @@ func (b *Builder) addEdge(edge *models.ChannelEdgeInfo,
13661364
}
13671365
}
13681366

1369-
return NewErrf(ErrChannelSpent, "unable to fetch utxo for "+
1370-
"chan_id=%v, chan_point=%v: %v", edge.ChannelID,
1367+
return fmt.Errorf("%w: unable to fetch utxo for chan_id=%v, "+
1368+
"chan_point=%v: %w", ErrChannelSpent, scid.ToUint64(),
13711369
fundingPoint, err)
13721370
}
13731371

graph/builder_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,14 +1275,12 @@ func newChannelEdgeInfo(t *testing.T, ctx *testCtx, fundingHeight uint32,
12751275
}
12761276

12771277
func assertChanChainRejection(t *testing.T, ctx *testCtx,
1278-
edge *models.ChannelEdgeInfo, failCode ErrorCode) {
1278+
edge *models.ChannelEdgeInfo, expectedErr error) {
12791279

12801280
t.Helper()
12811281

12821282
err := ctx.builder.AddEdge(edge)
1283-
if !IsError(err, failCode) {
1284-
t.Fatalf("validation should have failed: %v", err)
1285-
}
1283+
require.ErrorIs(t, err, expectedErr)
12861284

12871285
// This channel should now be present in the zombie channel index.
12881286
_, _, _, isZombie, err := ctx.graph.HasChannelEdge(

graph/errors.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ package graph
22

33
import "github.com/go-errors/errors"
44

5+
var (
6+
// ErrNoFundingTransaction is returned when we are unable to find the
7+
// funding transaction described by the short channel ID on chain.
8+
ErrNoFundingTransaction = errors.New(
9+
"unable to find the funding transaction",
10+
)
11+
12+
// ErrInvalidFundingOutput is returned if the channel funding output
13+
// fails validation.
14+
ErrInvalidFundingOutput = errors.New(
15+
"channel funding output validation failed",
16+
)
17+
18+
// ErrChannelSpent is returned when we go to validate a channel, but
19+
// the purported funding output has actually already been spent on
20+
// chain.
21+
ErrChannelSpent = errors.New("channel output has been spent")
22+
)
23+
524
// ErrorCode is used to represent the various errors that can occur within this
625
// package.
726
type ErrorCode uint8
@@ -15,19 +34,6 @@ const (
1534
// this update can't bring us something new, or because a node
1635
// announcement was given for node not found in any channel.
1736
ErrIgnored
18-
19-
// ErrChannelSpent is returned when we go to validate a channel, but
20-
// the purported funding output has actually already been spent on
21-
// chain.
22-
ErrChannelSpent
23-
24-
// ErrNoFundingTransaction is returned when we are unable to find the
25-
// funding transaction described by the short channel ID on chain.
26-
ErrNoFundingTransaction
27-
28-
// ErrInvalidFundingOutput is returned if the channel funding output
29-
// fails validation.
30-
ErrInvalidFundingOutput
3137
)
3238

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

0 commit comments

Comments
 (0)