Skip to content

Commit fbe4437

Browse files
committed
graph/db: make some ChannelEdgeInfo fields optional
Since not all will be required for V2 channels. Wrap BitcoinKey1Bytes and BitcoinKey2Bytes in fn.Option since these fields are only required for v1 channel announcements. V2 channels use taproot where the node keys serve as the bitcoin keys. NewV1Channel constructor wraps the bitcoin keys with fn.Some(). All access sites updated to unwrap the options, using UnwrapOr for non-critical paths and UnwrapOrErr where the keys must be present (e.g., KV serialization, ToChannelAnnouncement).
1 parent 89da00c commit fbe4437

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

graph/db/graph_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,14 @@ func assertEdgeInfoEqual(t *testing.T, e1 *models.ChannelEdgeInfo,
788788
if !bytes.Equal(e1.NodeKey2Bytes[:], e2.NodeKey2Bytes[:]) {
789789
t.Fatalf("nodekey2 doesn't match")
790790
}
791-
if !bytes.Equal(e1.BitcoinKey1Bytes[:], e2.BitcoinKey1Bytes[:]) {
791+
btcKey1E1 := e1.BitcoinKey1Bytes.UnwrapOr(route.Vertex{})
792+
btcKey1E2 := e2.BitcoinKey1Bytes.UnwrapOr(route.Vertex{})
793+
if !bytes.Equal(btcKey1E1[:], btcKey1E2[:]) {
792794
t.Fatalf("bitcoinkey1 doesn't match")
793795
}
794-
if !bytes.Equal(e1.BitcoinKey2Bytes[:], e2.BitcoinKey2Bytes[:]) {
796+
btcKey2E1 := e1.BitcoinKey2Bytes.UnwrapOr(route.Vertex{})
797+
btcKey2E2 := e2.BitcoinKey2Bytes.UnwrapOr(route.Vertex{})
798+
if !bytes.Equal(btcKey2E1[:], btcKey2E2[:]) {
795799
t.Fatalf("bitcoinkey2 doesn't match")
796800
}
797801

@@ -2001,9 +2005,10 @@ func TestGraphPruning(t *testing.T) {
20012005
t.Fatalf("unable to add node: %v", err)
20022006
}
20032007

2008+
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(route.Vertex{})
2009+
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(route.Vertex{})
20042010
pkScript, err := genMultiSigP2WSH(
2005-
edgeInfo.BitcoinKey1Bytes[:],
2006-
edgeInfo.BitcoinKey2Bytes[:],
2011+
btcKey1[:], btcKey2[:],
20072012
)
20082013
if err != nil {
20092014
t.Fatalf("unable to gen multi-sig p2wsh: %v", err)

graph/db/kv_store.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,9 +4113,14 @@ func (c *KVStore) ChannelView() ([]EdgePoint, error) {
41134113
return err
41144114
}
41154115

4116+
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(
4117+
route.Vertex{},
4118+
)
4119+
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(
4120+
route.Vertex{},
4121+
)
41164122
pkScript, err := genMultiSigP2WSH(
4117-
edgeInfo.BitcoinKey1Bytes[:],
4118-
edgeInfo.BitcoinKey2Bytes[:],
4123+
btcKey1[:], btcKey2[:],
41194124
)
41204125
if err != nil {
41214126
return err
@@ -4729,10 +4734,24 @@ func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
47294734
if _, err := b.Write(edgeInfo.NodeKey2Bytes[:]); err != nil {
47304735
return err
47314736
}
4732-
if _, err := b.Write(edgeInfo.BitcoinKey1Bytes[:]); err != nil {
4737+
4738+
btc1Key, err := edgeInfo.BitcoinKey1Bytes.UnwrapOrErr(
4739+
fmt.Errorf("edge missing bitcoin key 1"),
4740+
)
4741+
if err != nil {
4742+
return err
4743+
}
4744+
btc2Key, err := edgeInfo.BitcoinKey2Bytes.UnwrapOrErr(
4745+
fmt.Errorf("edge missing bitcoin key 2"),
4746+
)
4747+
if err != nil {
47334748
return err
47344749
}
4735-
if _, err := b.Write(edgeInfo.BitcoinKey2Bytes[:]); err != nil {
4750+
4751+
if _, err := b.Write(btc1Key[:]); err != nil {
4752+
return err
4753+
}
4754+
if _, err := b.Write(btc2Key[:]); err != nil {
47364755
return err
47374756
}
47384757

@@ -4770,7 +4789,7 @@ func putChanEdgeInfo(edgeIndex kvdb.RwBucket,
47704789
if err := WriteOutpoint(&b, &edgeInfo.ChannelPoint); err != nil {
47714790
return err
47724791
}
4773-
err := binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
4792+
err = binary.Write(&b, byteOrder, uint64(edgeInfo.Capacity))
47744793
if err != nil {
47754794
return err
47764795
}
@@ -4820,12 +4839,17 @@ func deserializeChanEdgeInfo(r io.Reader) (*models.ChannelEdgeInfo, error) {
48204839
if _, err := io.ReadFull(r, edgeInfo.NodeKey2Bytes[:]); err != nil {
48214840
return nil, err
48224841
}
4823-
if _, err := io.ReadFull(r, edgeInfo.BitcoinKey1Bytes[:]); err != nil {
4842+
4843+
var btcKey1, btcKey2 route.Vertex
4844+
if _, err := io.ReadFull(r, btcKey1[:]); err != nil {
48244845
return nil, err
48254846
}
4826-
if _, err := io.ReadFull(r, edgeInfo.BitcoinKey2Bytes[:]); err != nil {
4847+
edgeInfo.BitcoinKey1Bytes = fn.Some(btcKey1)
4848+
4849+
if _, err := io.ReadFull(r, btcKey2[:]); err != nil {
48274850
return nil, err
48284851
}
4852+
edgeInfo.BitcoinKey2Bytes = fn.Some(btcKey2)
48294853

48304854
featureBytes, err := wire.ReadVarBytes(r, 0, 900, "features")
48314855
if err != nil {

graph/db/models/channel_edge_info.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type ChannelEdgeInfo struct {
4141
nodeKey2 *btcec.PublicKey
4242

4343
// BitcoinKey1Bytes is the raw public key of the first node.
44-
BitcoinKey1Bytes route.Vertex
44+
BitcoinKey1Bytes fn.Option[route.Vertex]
4545

4646
// BitcoinKey2Bytes is the raw public key of the first node.
47-
BitcoinKey2Bytes route.Vertex
47+
BitcoinKey2Bytes fn.Option[route.Vertex]
4848

4949
// Features is the list of protocol features supported by this channel
5050
// edge.
@@ -142,8 +142,8 @@ func NewV1Channel(chanID uint64, chainHash chainhash.Hash, node1,
142142
Version: lnwire.GossipVersion1,
143143
NodeKey1Bytes: node1,
144144
NodeKey2Bytes: node2,
145-
BitcoinKey1Bytes: v1Fields.BitcoinKey1Bytes,
146-
BitcoinKey2Bytes: v1Fields.BitcoinKey2Bytes,
145+
BitcoinKey1Bytes: fn.Some(v1Fields.BitcoinKey1Bytes),
146+
BitcoinKey2Bytes: fn.Some(v1Fields.BitcoinKey2Bytes),
147147
ChannelID: chanID,
148148
ChainHash: chainHash,
149149
Features: lnwire.EmptyFeatureVector(),
@@ -240,19 +240,32 @@ func (c *ChannelEdgeInfo) ToChannelAnnouncement() (
240240
"without auth proof")
241241
}
242242

243+
btc1, err := c.BitcoinKey1Bytes.UnwrapOrErr(
244+
fmt.Errorf("bitcoin key 1 missing for v1 channel announcement"),
245+
)
246+
if err != nil {
247+
return nil, err
248+
}
249+
250+
btc2, err := c.BitcoinKey2Bytes.UnwrapOrErr(
251+
fmt.Errorf("bitcoin key 2 missing for v1 channel announcement"),
252+
)
253+
if err != nil {
254+
return nil, err
255+
}
256+
243257
chanID := lnwire.NewShortChanIDFromInt(c.ChannelID)
244258
chanAnn := &lnwire.ChannelAnnouncement1{
245259
ShortChannelID: chanID,
246260
NodeID1: c.NodeKey1Bytes,
247261
NodeID2: c.NodeKey2Bytes,
248262
ChainHash: c.ChainHash,
249-
BitcoinKey1: c.BitcoinKey1Bytes,
250-
BitcoinKey2: c.BitcoinKey2Bytes,
263+
BitcoinKey1: btc1,
264+
BitcoinKey2: btc2,
251265
Features: c.Features.RawFeatureVector,
252266
ExtraOpaqueData: c.ExtraOpaqueData,
253267
}
254268

255-
var err error
256269
chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
257270
c.AuthProof.NodeSig1(),
258271
)

graph/db/sql_store.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4164,15 +4164,19 @@ func insertChannel(ctx context.Context, db SQLQueries,
41644164
}
41654165

41664166
createParams := sqlc.CreateChannelParams{
4167-
Version: int16(v),
4168-
Scid: channelIDToBytes(edge.ChannelID),
4169-
NodeID1: node1DBID,
4170-
NodeID2: node2DBID,
4171-
Outpoint: edge.ChannelPoint.String(),
4172-
Capacity: capacity,
4173-
BitcoinKey1: edge.BitcoinKey1Bytes[:],
4174-
BitcoinKey2: edge.BitcoinKey2Bytes[:],
4175-
}
4167+
Version: int16(v),
4168+
Scid: channelIDToBytes(edge.ChannelID),
4169+
NodeID1: node1DBID,
4170+
NodeID2: node2DBID,
4171+
Outpoint: edge.ChannelPoint.String(),
4172+
Capacity: capacity,
4173+
}
4174+
edge.BitcoinKey1Bytes.WhenSome(func(vertex route.Vertex) {
4175+
createParams.BitcoinKey1 = vertex[:]
4176+
})
4177+
edge.BitcoinKey2Bytes.WhenSome(func(vertex route.Vertex) {
4178+
createParams.BitcoinKey2 = vertex[:]
4179+
})
41764180

41774181
if edge.AuthProof != nil {
41784182
proof := edge.AuthProof

0 commit comments

Comments
 (0)