code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/checkpoints.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 Checkpoints struct {
    30  	*ConnectionSource
    31  }
    32  
    33  var checkpointOrdering = TableOrdering{
    34  	ColumnOrdering{Name: "block_height", Sorting: ASC},
    35  }
    36  
    37  func NewCheckpoints(connectionSource *ConnectionSource) *Checkpoints {
    38  	p := &Checkpoints{
    39  		ConnectionSource: connectionSource,
    40  	}
    41  	return p
    42  }
    43  
    44  func (c *Checkpoints) Add(ctx context.Context, r entities.Checkpoint) error {
    45  	defer metrics.StartSQLQuery("Checkpoints", "Add")()
    46  	_, err := c.Exec(ctx,
    47  		`INSERT INTO checkpoints(
    48  			hash,
    49  			block_hash,
    50  			block_height,
    51  			tx_hash,
    52  			vega_time,
    53  			seq_num)
    54  		 VALUES ($1, $2, $3, $4, $5, $6)
    55  		 `,
    56  		r.Hash, r.BlockHash, r.BlockHeight, r.TxHash, r.VegaTime, r.SeqNum)
    57  	return err
    58  }
    59  
    60  func (c *Checkpoints) GetAll(ctx context.Context, pagination entities.CursorPagination) ([]entities.Checkpoint, entities.PageInfo, error) {
    61  	defer metrics.StartSQLQuery("Checkpoints", "GetAll")()
    62  	var nps []entities.Checkpoint
    63  	var pageInfo entities.PageInfo
    64  	var err error
    65  
    66  	query := `SELECT * FROM checkpoints`
    67  	var args []interface{}
    68  	query, args, err = PaginateQuery[entities.CheckpointCursor](query, args, checkpointOrdering, pagination)
    69  	if err != nil {
    70  		return nps, pageInfo, err
    71  	}
    72  
    73  	if err = pgxscan.Select(ctx, c.ConnectionSource, &nps, query, args...); err != nil {
    74  		return nil, pageInfo, fmt.Errorf("could not get checkpoint data: %w", err)
    75  	}
    76  
    77  	nps, pageInfo = entities.PageEntities[*v2.CheckpointEdge](nps, pagination)
    78  
    79  	return nps, pageInfo, nil
    80  }