code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/notary.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package sqlstore 17 18 import ( 19 "context" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/metrics" 24 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 25 26 "github.com/georgysavva/scany/pgxscan" 27 ) 28 29 type Notary struct { 30 *ConnectionSource 31 } 32 33 var notaryOrdering = TableOrdering{ 34 ColumnOrdering{Name: "resource_id", Sorting: ASC}, 35 ColumnOrdering{Name: "sig", Sorting: ASC}, 36 } 37 38 func NewNotary(connectionSource *ConnectionSource) *Notary { 39 return &Notary{ 40 ConnectionSource: connectionSource, 41 } 42 } 43 44 func (n *Notary) Add(ctx context.Context, ns *entities.NodeSignature) error { 45 defer metrics.StartSQLQuery("Notary", "Add")() 46 query := `INSERT INTO node_signatures (resource_id, sig, kind, tx_hash, vega_time) 47 VALUES ($1, $2, $3, $4, $5) 48 ON CONFLICT (resource_id, sig) DO NOTHING` 49 50 if _, err := n.Exec(ctx, query, 51 ns.ResourceID, 52 ns.Sig, 53 ns.Kind, 54 ns.TxHash, 55 ns.VegaTime, 56 ); err != nil { 57 err = fmt.Errorf("could not insert node-signature into database: %w", err) 58 return err 59 } 60 61 return nil 62 } 63 64 func (n *Notary) GetByResourceID(ctx context.Context, id string, pagination entities.CursorPagination) ([]entities.NodeSignature, entities.PageInfo, error) { 65 defer metrics.StartSQLQuery("Notary", "GetByResourceID")() 66 var ( 67 pageInfo entities.PageInfo 68 args []interface{} 69 err error 70 ns []entities.NodeSignature 71 ) 72 73 resourceID := entities.NodeSignatureID(id) 74 // make sure the resourceID is valid HexID 75 err = resourceID.Error() 76 if err != nil { 77 return nil, pageInfo, err 78 } 79 80 query := fmt.Sprintf(`SELECT resource_id, sig, kind, tx_hash, vega_time FROM node_signatures where resource_id=%s`, 81 nextBindVar(&args, entities.NodeSignatureID(id))) 82 query, args, err = PaginateQuery[entities.NodeSignatureCursor](query, args, notaryOrdering, pagination) 83 if err != nil { 84 return ns, pageInfo, err 85 } 86 87 if err = pgxscan.Select(ctx, n.ConnectionSource, &ns, query, args...); err != nil { 88 return nil, pageInfo, fmt.Errorf("could not get node signatures for resource: %w", err) 89 } 90 91 ns, pageInfo = entities.PageEntities[*v2.NodeSignatureEdge](ns, pagination) 92 return ns, pageInfo, nil 93 } 94 95 func (n *Notary) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.NodeSignature, error) { 96 defer metrics.StartSQLQuery("Notary", "GetByTxHash")() 97 98 var ns []entities.NodeSignature 99 query := "SELECT resource_id, sig, kind, tx_hash, vega_time FROM node_signatures WHERE tx_hash=$1" 100 101 if err := pgxscan.Select(ctx, n.ConnectionSource, &ns, query, txHash); err != nil { 102 return nil, fmt.Errorf("could not get node signatures for tx hash: %w", err) 103 } 104 105 return ns, nil 106 }