Skip to content

Commit ea2be9c

Browse files
committed
graph/db: version FetchChannelEdgesByID/Outpoint
1 parent 535aaa0 commit ea2be9c

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

graph/db/graph.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,19 @@ func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
711711
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
712712
*models.ChannelEdgePolicy, error) {
713713

714-
return c.db.FetchChannelEdgesByOutpoint(op)
714+
return c.db.FetchChannelEdgesByOutpoint(
715+
lnwire.GossipVersion1, op,
716+
)
715717
}
716718

717719
// FetchChannelEdgesByID attempts to lookup directed edges by channel ID.
718720
func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) (
719721
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
720722
*models.ChannelEdgePolicy, error) {
721723

722-
return c.db.FetchChannelEdgesByID(chanID)
724+
return c.db.FetchChannelEdgesByID(
725+
lnwire.GossipVersion1, chanID,
726+
)
723727
}
724728

725729
// ChannelView returns the verifiable edge information for each active channel.
@@ -785,6 +789,23 @@ func (c *VersionedGraph) FetchNode(ctx context.Context,
785789
return c.db.FetchNode(ctx, c.v, nodePub)
786790
}
787791

792+
// FetchChannelEdgesByID attempts to lookup directed edges by channel ID.
793+
func (c *VersionedGraph) FetchChannelEdgesByID(chanID uint64) (
794+
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
795+
*models.ChannelEdgePolicy, error) {
796+
797+
return c.db.FetchChannelEdgesByID(c.v, chanID)
798+
}
799+
800+
// FetchChannelEdgesByOutpoint attempts to lookup directed edges by funding
801+
// outpoint.
802+
func (c *VersionedGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
803+
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
804+
*models.ChannelEdgePolicy, error) {
805+
806+
return c.db.FetchChannelEdgesByOutpoint(c.v, op)
807+
}
808+
788809
// AddrsForNode returns all known addresses for the target node public key.
789810
func (c *VersionedGraph) AddrsForNode(ctx context.Context,
790811
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {

graph/db/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ type Store interface { //nolint:interfacebloat
276276
// houses the general information for the channel itself is returned as
277277
// well as two structs that contain the routing policies for the channel
278278
// in either direction.
279-
FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
279+
FetchChannelEdgesByOutpoint(v lnwire.GossipVersion, op *wire.OutPoint) (
280280
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
281281
*models.ChannelEdgePolicy, error)
282282

@@ -291,7 +291,7 @@ type Store interface { //nolint:interfacebloat
291291
// zombie within the database. In this case, the ChannelEdgePolicy's
292292
// will be nil, and the ChannelEdgeInfo will only include the public
293293
// keys of each node.
294-
FetchChannelEdgesByID(chanID uint64) (
294+
FetchChannelEdgesByID(v lnwire.GossipVersion, chanID uint64) (
295295
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
296296
*models.ChannelEdgePolicy, error)
297297

graph/db/kv_store.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,8 +3809,8 @@ func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
38093809
// found, then ErrEdgeNotFound is returned. A struct which houses the general
38103810
// information for the channel itself is returned as well as two structs that
38113811
// contain the routing policies for the channel in either direction.
3812-
func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
3813-
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3812+
func (c *KVStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
3813+
op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
38143814
*models.ChannelEdgePolicy, error) {
38153815

38163816
var (
@@ -3819,6 +3819,10 @@ func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
38193819
policy2 *models.ChannelEdgePolicy
38203820
)
38213821

3822+
if v != lnwire.GossipVersion1 {
3823+
return nil, nil, nil, ErrVersionNotSupportedForKVDB
3824+
}
3825+
38223826
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
38233827
// First, grab the node bucket. This will be used to populate
38243828
// the Node pointers in each edge read from disk.
@@ -3895,10 +3899,14 @@ func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
38953899
// ErrZombieEdge an be returned if the edge is currently marked as a zombie
38963900
// within the database. In this case, the ChannelEdgePolicy's will be nil, and
38973901
// the ChannelEdgeInfo will only include the public keys of each node.
3898-
func (c *KVStore) FetchChannelEdgesByID(chanID uint64) (
3899-
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3902+
func (c *KVStore) FetchChannelEdgesByID(v lnwire.GossipVersion,
3903+
chanID uint64) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
39003904
*models.ChannelEdgePolicy, error) {
39013905

3906+
if v != lnwire.GossipVersion1 {
3907+
return nil, nil, nil, ErrVersionNotSupportedForKVDB
3908+
}
3909+
39023910
var (
39033911
edgeInfo *models.ChannelEdgeInfo
39043912
policy1 *models.ChannelEdgePolicy

graph/db/sql_store.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,8 +1998,8 @@ func (s *SQLStore) DeleteChannelEdges(v lnwire.GossipVersion,
19981998
// the ChannelEdgeInfo will only include the public keys of each node.
19991999
//
20002000
// NOTE: part of the Store interface.
2001-
func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
2002-
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
2001+
func (s *SQLStore) FetchChannelEdgesByID(v lnwire.GossipVersion,
2002+
chanID uint64) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
20032003
*models.ChannelEdgePolicy, error) {
20042004

20052005
var (
@@ -2008,11 +2008,17 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
20082008
policy1, policy2 *models.ChannelEdgePolicy
20092009
chanIDB = channelIDToBytes(chanID)
20102010
)
2011+
2012+
if !isKnownGossipVersion(v) {
2013+
return nil, nil, nil, fmt.Errorf("unsupported gossip version: %d",
2014+
v)
2015+
}
2016+
20112017
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
20122018
row, err := db.GetChannelBySCIDWithPolicies(
20132019
ctx, sqlc.GetChannelBySCIDWithPoliciesParams{
20142020
Scid: chanIDB,
2015-
Version: int16(lnwire.GossipVersion1),
2021+
Version: int16(v),
20162022
},
20172023
)
20182024
if errors.Is(err, sql.ErrNoRows) {
@@ -2021,7 +2027,7 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
20212027
zombie, err := db.GetZombieChannel(
20222028
ctx, sqlc.GetZombieChannelParams{
20232029
Scid: chanIDB,
2024-
Version: int16(lnwire.GossipVersion1),
2030+
Version: int16(v),
20252031
},
20262032
)
20272033
if errors.Is(err, sql.ErrNoRows) {
@@ -2111,20 +2117,26 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
21112117
// contain the routing policies for the channel in either direction.
21122118
//
21132119
// NOTE: part of the Store interface.
2114-
func (s *SQLStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
2115-
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
2120+
func (s *SQLStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
2121+
op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
21162122
*models.ChannelEdgePolicy, error) {
21172123

21182124
var (
21192125
ctx = context.TODO()
21202126
edge *models.ChannelEdgeInfo
21212127
policy1, policy2 *models.ChannelEdgePolicy
21222128
)
2129+
2130+
if !isKnownGossipVersion(v) {
2131+
return nil, nil, nil, fmt.Errorf("unsupported gossip version: %d",
2132+
v)
2133+
}
2134+
21232135
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
21242136
row, err := db.GetChannelByOutpointWithPolicies(
21252137
ctx, sqlc.GetChannelByOutpointWithPoliciesParams{
21262138
Outpoint: op.String(),
2127-
Version: int16(lnwire.GossipVersion1),
2139+
Version: int16(v),
21282140
},
21292141
)
21302142
if errors.Is(err, sql.ErrNoRows) {
@@ -4544,6 +4556,21 @@ func getAndBuildChanPolicies(ctx context.Context, cfg *sqldb.QueryConfig,
45444556
return nil, nil, nil
45454557
}
45464558

4559+
// TODO(elle): update to support v2 policies.
4560+
if dbPol1 != nil &&
4561+
lnwire.GossipVersion(dbPol1.Version) != lnwire.GossipVersion1 {
4562+
4563+
return nil, nil, fmt.Errorf("unsupported policy1 version: %d",
4564+
dbPol1.Version)
4565+
}
4566+
4567+
if dbPol2 != nil &&
4568+
lnwire.GossipVersion(dbPol2.Version) != lnwire.GossipVersion1 {
4569+
4570+
return nil, nil, fmt.Errorf("unsupported policy2 version: %d",
4571+
dbPol2.Version)
4572+
}
4573+
45474574
var policyIDs = make([]int64, 0, 2)
45484575
if dbPol1 != nil {
45494576
policyIDs = append(policyIDs, dbPol1.ID)

0 commit comments

Comments
 (0)