diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 419c96bc3..12d9ef109 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -1128,7 +1128,7 @@ func TestAddEdgeProof(t *testing.T) { t.Parallel() ctx := context.Background() - graph := MakeTestGraph(t) + graph := MakeTestGraphNew(t) // Add an edge with no proof. node1 := createTestVertex(t) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index a6c67948c..bfd2b0125 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -91,6 +91,7 @@ type SQLQueries interface { Channel queries. */ CreateChannel(ctx context.Context, arg sqlc.CreateChannelParams) (int64, error) + AddV1ChannelProof(ctx context.Context, arg sqlc.AddV1ChannelProofParams) (sql.Result, error) GetChannelBySCID(ctx context.Context, arg sqlc.GetChannelBySCIDParams) (sqlc.Channel, error) GetChannelByOutpoint(ctx context.Context, outpoint string) (sqlc.GetChannelByOutpointRow, error) GetChannelsBySCIDRange(ctx context.Context, arg sqlc.GetChannelsBySCIDRangeParams) ([]sqlc.GetChannelsBySCIDRangeRow, error) @@ -2575,6 +2576,54 @@ func (s *SQLStore) DisconnectBlockAtHeight(height uint32) ( return removedChans, nil } +// AddEdgeProof sets the proof of an existing edge in the graph database. +// +// NOTE: part of the V1Store interface. +func (s *SQLStore) AddEdgeProof(scid lnwire.ShortChannelID, + proof *models.ChannelAuthProof) error { + + var ( + ctx = context.TODO() + scidBytes = channelIDToBytes(scid.ToUint64()) + ) + + err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error { + res, err := db.AddV1ChannelProof( + ctx, sqlc.AddV1ChannelProofParams{ + Scid: scidBytes[:], + Node1Signature: proof.NodeSig1Bytes, + Node2Signature: proof.NodeSig2Bytes, + Bitcoin1Signature: proof.BitcoinSig1Bytes, + Bitcoin2Signature: proof.BitcoinSig2Bytes, + }, + ) + if err != nil { + return fmt.Errorf("unable to add edge proof: %w", err) + } + + n, err := res.RowsAffected() + if err != nil { + return err + } + + if n == 0 { + return fmt.Errorf("no rows affected when adding edge "+ + "proof for SCID %v", scid) + } else if n > 1 { + return fmt.Errorf("multiple rows affected when adding "+ + "edge proof for SCID %v: %d rows affected", + scid, n) + } + + return nil + }, sqldb.NoOpReset) + if err != nil { + return fmt.Errorf("unable to add edge proof: %w", err) + } + + return nil +} + // forEachNodeDirectedChannel iterates through all channels of a given // node, executing the passed callback on the directed edge representing the // channel and its incoming policy. If the node is not found, no error is diff --git a/sqldb/sqlc/graph.sql.go b/sqldb/sqlc/graph.sql.go index c74b4c050..2f0380294 100644 --- a/sqldb/sqlc/graph.sql.go +++ b/sqldb/sqlc/graph.sql.go @@ -26,6 +26,34 @@ func (q *Queries) AddSourceNode(ctx context.Context, nodeID int64) error { return err } +const addV1ChannelProof = `-- name: AddV1ChannelProof :execresult +UPDATE channels +SET node_1_signature = $2, + node_2_signature = $3, + bitcoin_1_signature = $4, + bitcoin_2_signature = $5 +WHERE scid = $1 + AND version = 1 +` + +type AddV1ChannelProofParams struct { + Scid []byte + Node1Signature []byte + Node2Signature []byte + Bitcoin1Signature []byte + Bitcoin2Signature []byte +} + +func (q *Queries) AddV1ChannelProof(ctx context.Context, arg AddV1ChannelProofParams) (sql.Result, error) { + return q.db.ExecContext(ctx, addV1ChannelProof, + arg.Scid, + arg.Node1Signature, + arg.Node2Signature, + arg.Bitcoin1Signature, + arg.Bitcoin2Signature, + ) +} + const countZombieChannels = `-- name: CountZombieChannels :one SELECT COUNT(*) FROM zombie_channels diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index f0e1ad3e7..f166904da 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -12,6 +12,7 @@ import ( type Querier interface { AddSourceNode(ctx context.Context, nodeID int64) error + AddV1ChannelProof(ctx context.Context, arg AddV1ChannelProofParams) (sql.Result, error) ClearKVInvoiceHashIndex(ctx context.Context) error CountZombieChannels(ctx context.Context, version int16) (int64, error) CreateChannel(ctx context.Context, arg CreateChannelParams) (int64, error) diff --git a/sqldb/sqlc/queries/graph.sql b/sqldb/sqlc/queries/graph.sql index 67b5dae6b..6281ea0ab 100644 --- a/sqldb/sqlc/queries/graph.sql +++ b/sqldb/sqlc/queries/graph.sql @@ -208,6 +208,15 @@ INSERT INTO channels ( ) RETURNING id; +-- name: AddV1ChannelProof :execresult +UPDATE channels +SET node_1_signature = $2, + node_2_signature = $3, + bitcoin_1_signature = $4, + bitcoin_2_signature = $5 +WHERE scid = $1 + AND version = 1; + -- name: GetChannelsBySCIDRange :many SELECT sqlc.embed(c), n1.pub_key AS node1_pub_key,