Skip to content

Commit 9284938

Browse files
committed
graph/db: return channel DB info from insertChannel
In preparation for the kvdb->migration code, this commit updates `insertChannel` to return the ID of the newly inserted channel along with the IDs of the nodes that the channel links to.
1 parent 4bde8e2 commit 9284938

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

graph/db/sql_store.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ func (s *SQLStore) AddChannelEdge(ctx context.Context,
570570
alreadyExists = false
571571
},
572572
Do: func(tx SQLQueries) error {
573-
err := insertChannel(ctx, tx, edge)
573+
_, err := insertChannel(ctx, tx, edge)
574574

575575
// Silence ErrEdgeAlreadyExist so that the batch can
576576
// succeed, but propagate the error via local state.
@@ -3759,9 +3759,17 @@ func marshalExtraOpaqueData(data []byte) (map[uint64][]byte, error) {
37593759
return records, nil
37603760
}
37613761

3762+
// dbChanInfo holds the DB level IDs of a channel and the nodes involved in the
3763+
// channel.
3764+
type dbChanInfo struct {
3765+
channelID int64
3766+
node1ID int64
3767+
node2ID int64
3768+
}
3769+
37623770
// insertChannel inserts a new channel record into the database.
37633771
func insertChannel(ctx context.Context, db SQLQueries,
3764-
edge *models.ChannelEdgeInfo) error {
3772+
edge *models.ChannelEdgeInfo) (*dbChanInfo, error) {
37653773

37663774
chanIDB := channelIDToBytes(edge.ChannelID)
37673775

@@ -3776,21 +3784,21 @@ func insertChannel(ctx context.Context, db SQLQueries,
37763784
},
37773785
)
37783786
if err == nil {
3779-
return ErrEdgeAlreadyExist
3787+
return nil, ErrEdgeAlreadyExist
37803788
} else if !errors.Is(err, sql.ErrNoRows) {
3781-
return fmt.Errorf("unable to fetch channel: %w", err)
3789+
return nil, fmt.Errorf("unable to fetch channel: %w", err)
37823790
}
37833791

37843792
// Make sure that at least a "shell" entry for each node is present in
37853793
// the nodes table.
37863794
node1DBID, err := maybeCreateShellNode(ctx, db, edge.NodeKey1Bytes)
37873795
if err != nil {
3788-
return fmt.Errorf("unable to create shell node: %w", err)
3796+
return nil, fmt.Errorf("unable to create shell node: %w", err)
37893797
}
37903798

37913799
node2DBID, err := maybeCreateShellNode(ctx, db, edge.NodeKey2Bytes)
37923800
if err != nil {
3793-
return fmt.Errorf("unable to create shell node: %w", err)
3801+
return nil, fmt.Errorf("unable to create shell node: %w", err)
37943802
}
37953803

37963804
var capacity sql.NullInt64
@@ -3821,15 +3829,15 @@ func insertChannel(ctx context.Context, db SQLQueries,
38213829
// Insert the new channel record.
38223830
dbChanID, err := db.CreateChannel(ctx, createParams)
38233831
if err != nil {
3824-
return err
3832+
return nil, err
38253833
}
38263834

38273835
// Insert any channel features.
38283836
if len(edge.Features) != 0 {
38293837
chanFeatures := lnwire.NewRawFeatureVector()
38303838
err := chanFeatures.Decode(bytes.NewReader(edge.Features))
38313839
if err != nil {
3832-
return err
3840+
return nil, err
38333841
}
38343842

38353843
fv := lnwire.NewFeatureVector(chanFeatures, lnwire.Features)
@@ -3841,7 +3849,7 @@ func insertChannel(ctx context.Context, db SQLQueries,
38413849
},
38423850
)
38433851
if err != nil {
3844-
return fmt.Errorf("unable to insert "+
3852+
return nil, fmt.Errorf("unable to insert "+
38453853
"channel(%d) feature(%v): %w", dbChanID,
38463854
feature, err)
38473855
}
@@ -3851,8 +3859,8 @@ func insertChannel(ctx context.Context, db SQLQueries,
38513859
// Finally, insert any extra TLV fields in the channel announcement.
38523860
extra, err := marshalExtraOpaqueData(edge.ExtraOpaqueData)
38533861
if err != nil {
3854-
return fmt.Errorf("unable to marshal extra opaque data: %w",
3855-
err)
3862+
return nil, fmt.Errorf("unable to marshal extra opaque "+
3863+
"data: %w", err)
38563864
}
38573865

38583866
for tlvType, value := range extra {
@@ -3864,13 +3872,17 @@ func insertChannel(ctx context.Context, db SQLQueries,
38643872
},
38653873
)
38663874
if err != nil {
3867-
return fmt.Errorf("unable to upsert channel(%d) extra "+
3868-
"signed field(%v): %w", edge.ChannelID,
3869-
tlvType, err)
3875+
return nil, fmt.Errorf("unable to upsert "+
3876+
"channel(%d) extra signed field(%v): %w",
3877+
edge.ChannelID, tlvType, err)
38703878
}
38713879
}
38723880

3873-
return nil
3881+
return &dbChanInfo{
3882+
channelID: dbChanID,
3883+
node1ID: node1DBID,
3884+
node2ID: node2DBID,
3885+
}, nil
38743886
}
38753887

38763888
// maybeCreateShellNode checks if a shell node entry exists for the

0 commit comments

Comments
 (0)