Skip to content

Commit 89da00c

Browse files
committed
multi: add ToChannelAnnouncement helper on ChannelEdgeInfo
So that we have one place that converts from our `models` struct to the `lnwire.ChannelAnnouncement` struct. The commit also refactors netann.CreateChanAnnouncement to only take a ChannelEdgeInfo and get the proof from there instead of needing the proof to be passed in separately. Add ToChannelAnnouncement() method to ChannelEdgeInfo that converts the model struct to a lnwire.ChannelAnnouncement1 message. This: - Centralizes the conversion logic in one place instead of scattered across multiple call sites - Validates that AuthProof is present (can't create announcement without proof) - Currently only supports v1 channels, returning error for v2 Refactor netann.CreateChanAnnouncement to use this helper and remove the separate chanProof parameter since proof is now accessed from within ChannelEdgeInfo. This improves encapsulation and reduces parameter count.
1 parent 2691e13 commit 89da00c

File tree

5 files changed

+82
-80
lines changed

5 files changed

+82
-80
lines changed

discovery/chan_series.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash,
133133

134134
//nolint:ll
135135
chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
136-
channel.Info.AuthProof, channel.Info,
137-
channel.Policy1, channel.Policy2,
136+
channel.Info, channel.Policy1, channel.Policy2,
138137
)
139138
if err != nil {
140139
if !yield(nil, err) {
@@ -281,8 +280,7 @@ func (c *ChanSeries) FetchChanAnns(chain chainhash.Hash,
281280
}
282281

283282
chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
284-
channel.Info.AuthProof, channel.Info, channel.Policy1,
285-
channel.Policy2,
283+
channel.Info, channel.Policy1, channel.Policy2,
286284
)
287285
if err != nil {
288286
return nil, err

discovery/gossiper.go

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,10 +1996,14 @@ func (d *AuthenticatedGossiper) processRejectedEdge(_ context.Context,
19961996
return nil, nil
19971997
}
19981998

1999+
// Attach the proof to the channel info before creating the
2000+
// announcement.
2001+
chanInfo.AuthProof = proof
2002+
19992003
// We'll then create then validate the new fully assembled
20002004
// announcement.
20012005
chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
2002-
proof, chanInfo, e1, e2,
2006+
chanInfo, e1, e2,
20032007
)
20042008
if err != nil {
20052009
return nil, err
@@ -2392,44 +2396,13 @@ func (d *AuthenticatedGossiper) updateChannel(ctx context.Context,
23922396
// have a full channel announcement for this channel.
23932397
var chanAnn *lnwire.ChannelAnnouncement1
23942398
if info.AuthProof != nil {
2395-
chanID := lnwire.NewShortChanIDFromInt(info.ChannelID)
2396-
chanAnn = &lnwire.ChannelAnnouncement1{
2397-
ShortChannelID: chanID,
2398-
NodeID1: info.NodeKey1Bytes,
2399-
NodeID2: info.NodeKey2Bytes,
2400-
ChainHash: info.ChainHash,
2401-
BitcoinKey1: info.BitcoinKey1Bytes,
2402-
Features: lnwire.NewRawFeatureVector(),
2403-
BitcoinKey2: info.BitcoinKey2Bytes,
2404-
ExtraOpaqueData: info.ExtraOpaqueData,
2405-
}
2406-
chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
2407-
info.AuthProof.NodeSig1(),
2408-
)
2409-
if err != nil {
2410-
return nil, nil, err
2411-
}
2412-
chanAnn.NodeSig2, err = lnwire.NewSigFromECDSARawSignature(
2413-
info.AuthProof.NodeSig2(),
2414-
)
2415-
if err != nil {
2416-
return nil, nil, err
2417-
}
2418-
chanAnn.BitcoinSig1, err = lnwire.NewSigFromECDSARawSignature(
2419-
info.AuthProof.BitcoinSig1(),
2420-
)
2421-
if err != nil {
2422-
return nil, nil, err
2423-
}
2424-
chanAnn.BitcoinSig2, err = lnwire.NewSigFromECDSARawSignature(
2425-
info.AuthProof.BitcoinSig2(),
2426-
)
2399+
chanAnn, err = info.ToChannelAnnouncement()
24272400
if err != nil {
24282401
return nil, nil, err
24292402
}
24302403
}
24312404

2432-
return chanAnn, chanUpdate, err
2405+
return chanAnn, chanUpdate, nil
24332406
}
24342407

24352408
// SyncManager returns the gossiper's SyncManager instance.
@@ -3570,7 +3543,7 @@ func (d *AuthenticatedGossiper) handleAnnSig(ctx context.Context,
35703543
ann.ChannelID, peerID)
35713544

35723545
ca, _, _, err := netann.CreateChanAnnouncement(
3573-
chanInfo.AuthProof, chanInfo, e1, e2,
3546+
chanInfo, e1, e2,
35743547
)
35753548
if err != nil {
35763549
log.Errorf("unable to gen ann: %v",
@@ -3649,8 +3622,12 @@ func (d *AuthenticatedGossiper) handleAnnSig(ctx context.Context,
36493622
)
36503623
}
36513624

3625+
// Attach the proof to the channel info before creating the
3626+
// announcement.
3627+
chanInfo.AuthProof = dbProof
3628+
36523629
chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
3653-
dbProof, chanInfo, e1, e2,
3630+
chanInfo, e1, e2,
36543631
)
36553632
if err != nil {
36563633
log.Error(err)

graph/db/models/channel_edge_info.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,66 @@ func (c *ChannelEdgeInfo) OtherNodeKeyBytes(thisNodeKey []byte) (
220220
"this channel")
221221
}
222222
}
223+
224+
// ToChannelAnnouncement converts the ChannelEdgeInfo to a
225+
// lnwire.ChannelAnnouncement1 message. Returns an error if AuthProof is nil
226+
// or if the version is not v1.
227+
func (c *ChannelEdgeInfo) ToChannelAnnouncement() (
228+
*lnwire.ChannelAnnouncement1, error) {
229+
230+
// We currently only support v1 channel announcements.
231+
if c.Version != lnwire.GossipVersion1 {
232+
return nil, fmt.Errorf("unsupported channel version: %d",
233+
c.Version)
234+
}
235+
236+
// If there's no auth proof, we can't create a full channel
237+
// announcement.
238+
if c.AuthProof == nil {
239+
return nil, fmt.Errorf("cannot create channel announcement " +
240+
"without auth proof")
241+
}
242+
243+
chanID := lnwire.NewShortChanIDFromInt(c.ChannelID)
244+
chanAnn := &lnwire.ChannelAnnouncement1{
245+
ShortChannelID: chanID,
246+
NodeID1: c.NodeKey1Bytes,
247+
NodeID2: c.NodeKey2Bytes,
248+
ChainHash: c.ChainHash,
249+
BitcoinKey1: c.BitcoinKey1Bytes,
250+
BitcoinKey2: c.BitcoinKey2Bytes,
251+
Features: c.Features.RawFeatureVector,
252+
ExtraOpaqueData: c.ExtraOpaqueData,
253+
}
254+
255+
var err error
256+
chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
257+
c.AuthProof.NodeSig1(),
258+
)
259+
if err != nil {
260+
return nil, err
261+
}
262+
263+
chanAnn.NodeSig2, err = lnwire.NewSigFromECDSARawSignature(
264+
c.AuthProof.NodeSig2(),
265+
)
266+
if err != nil {
267+
return nil, err
268+
}
269+
270+
chanAnn.BitcoinSig1, err = lnwire.NewSigFromECDSARawSignature(
271+
c.AuthProof.BitcoinSig1(),
272+
)
273+
if err != nil {
274+
return nil, err
275+
}
276+
277+
chanAnn.BitcoinSig2, err = lnwire.NewSigFromECDSARawSignature(
278+
c.AuthProof.BitcoinSig2(),
279+
)
280+
if err != nil {
281+
return nil, err
282+
}
283+
284+
return chanAnn, nil
285+
}

netann/channel_announcement.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,14 @@ const (
3434
// function is used to transform out database structs into the corresponding wire
3535
// structs for announcing new channels to other peers, or simply syncing up a
3636
// peer's initial routing table upon connect.
37-
func CreateChanAnnouncement(chanProof *models.ChannelAuthProof,
38-
chanInfo *models.ChannelEdgeInfo,
37+
func CreateChanAnnouncement(chanInfo *models.ChannelEdgeInfo,
3938
e1, e2 *models.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement1,
4039
*lnwire.ChannelUpdate1, *lnwire.ChannelUpdate1, error) {
4140

4241
// First, using the parameters of the channel, along with the channel
43-
// authentication chanProof, we'll create re-create the original
42+
// authentication proof, we'll create re-create the original
4443
// authenticated channel announcement.
45-
chanID := lnwire.NewShortChanIDFromInt(chanInfo.ChannelID)
46-
chanAnn := &lnwire.ChannelAnnouncement1{
47-
ShortChannelID: chanID,
48-
NodeID1: chanInfo.NodeKey1Bytes,
49-
NodeID2: chanInfo.NodeKey2Bytes,
50-
ChainHash: chanInfo.ChainHash,
51-
BitcoinKey1: chanInfo.BitcoinKey1Bytes,
52-
BitcoinKey2: chanInfo.BitcoinKey2Bytes,
53-
Features: chanInfo.Features.RawFeatureVector,
54-
ExtraOpaqueData: chanInfo.ExtraOpaqueData,
55-
}
56-
57-
var err error
58-
chanAnn.BitcoinSig1, err = lnwire.NewSigFromECDSARawSignature(
59-
chanProof.BitcoinSig1(),
60-
)
61-
if err != nil {
62-
return nil, nil, nil, err
63-
}
64-
chanAnn.BitcoinSig2, err = lnwire.NewSigFromECDSARawSignature(
65-
chanProof.BitcoinSig2(),
66-
)
67-
if err != nil {
68-
return nil, nil, nil, err
69-
}
70-
chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
71-
chanProof.NodeSig1(),
72-
)
73-
if err != nil {
74-
return nil, nil, nil, err
75-
}
76-
chanAnn.NodeSig2, err = lnwire.NewSigFromECDSARawSignature(
77-
chanProof.NodeSig2(),
78-
)
44+
chanAnn, err := chanInfo.ToChannelAnnouncement()
7945
if err != nil {
8046
return nil, nil, nil, err
8147
}

netann/channel_announcement_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ func TestCreateChanAnnouncement(t *testing.T) {
6262
models.WithFeatures(features),
6363
)
6464
require.NoError(t, err)
65-
chanAnn, _, _, err := CreateChanAnnouncement(
66-
chanProof, chanInfo, nil, nil,
67-
)
65+
chanAnn, _, _, err := CreateChanAnnouncement(chanInfo, nil, nil)
6866
require.NoError(t, err, "unable to create channel announcement")
6967

7068
assert.Equal(t, chanAnn, expChanAnn)

0 commit comments

Comments
 (0)