code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/snapshot_data.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 21 "code.vegaprotocol.io/vega/datanode/entities" 22 "code.vegaprotocol.io/vega/datanode/metrics" 23 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 24 25 "github.com/georgysavva/scany/pgxscan" 26 ) 27 28 type CoreSnapshotData struct { 29 *ConnectionSource 30 } 31 32 var snapshotOrdering = TableOrdering{ 33 // ASC here actually means DESC for some reason 34 ColumnOrdering{Name: "block_height", Sorting: ASC}, 35 } 36 37 func NewCoreSnapshotData(connectionSource *ConnectionSource) *CoreSnapshotData { 38 return &CoreSnapshotData{ConnectionSource: connectionSource} 39 } 40 41 func (s *CoreSnapshotData) Add(ctx context.Context, csd entities.CoreSnapshotData) error { 42 defer metrics.StartSQLQuery("CoreSnapshotData", "Add")() 43 44 _, err := s.Exec(ctx, 45 `INSERT INTO core_snapshots( 46 block_height, 47 block_hash, 48 vega_core_version, 49 vega_time, 50 tx_hash) 51 VALUES ($1, $2, $3, $4, $5)`, 52 csd.BlockHeight, csd.BlockHash, csd.VegaCoreVersion, csd.VegaTime, csd.TxHash) 53 return err 54 } 55 56 func (s *CoreSnapshotData) List(ctx context.Context, pagination entities.CursorPagination) ([]entities.CoreSnapshotData, entities.PageInfo, error) { 57 args := []interface{}{} 58 query := ` 59 SELECT block_height, 60 block_hash, 61 vega_core_version, 62 vega_time, 63 tx_hash 64 FROM core_snapshots 65 ` 66 var err error 67 68 pageInfo := entities.PageInfo{} 69 query, args, err = PaginateQuery[entities.CoreSnapshotDataCursor](query, args, snapshotOrdering, pagination) 70 if err != nil { 71 return nil, pageInfo, err 72 } 73 74 defer metrics.StartSQLQuery("CoreSnapshotData", "List")() 75 snaps := make([]entities.CoreSnapshotData, 0) 76 if err := pgxscan.Select(ctx, s.ConnectionSource, &snaps, query, args...); err != nil { 77 return snaps, pageInfo, err 78 } 79 80 snaps, pageInfo = entities.PageEntities[*v2.CoreSnapshotEdge](snaps, pagination) 81 return snaps, pageInfo, nil 82 }