code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/protocol_upgrade_proposals.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  	"strings"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/metrics"
    25  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    26  
    27  	"github.com/georgysavva/scany/pgxscan"
    28  )
    29  
    30  type ProtocolUpgradeProposals struct {
    31  	*ConnectionSource
    32  }
    33  
    34  var pupOrdering = TableOrdering{
    35  	ColumnOrdering{Name: "upgrade_block_height", Sorting: ASC},
    36  	ColumnOrdering{Name: "vega_release_tag", Sorting: ASC},
    37  }
    38  
    39  func NewProtocolUpgradeProposals(connectionSource *ConnectionSource) *ProtocolUpgradeProposals {
    40  	p := &ProtocolUpgradeProposals{
    41  		ConnectionSource: connectionSource,
    42  	}
    43  	return p
    44  }
    45  
    46  func (ps *ProtocolUpgradeProposals) Add(ctx context.Context, p entities.ProtocolUpgradeProposal) error {
    47  	defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "Add")()
    48  	if p.Approvers == nil {
    49  		p.Approvers = []string{}
    50  	}
    51  
    52  	_, err := ps.Exec(ctx,
    53  		`INSERT INTO protocol_upgrade_proposals(
    54  			upgrade_block_height,
    55  			vega_release_tag,
    56  			approvers,
    57  			status,
    58  			vega_time,
    59  			tx_hash)
    60  		 VALUES ($1,  $2,  $3,  $4,  $5,  $6)
    61  		 ON CONFLICT (vega_time, upgrade_block_height, vega_release_tag) DO UPDATE SET
    62  			approvers = EXCLUDED.approvers,
    63  			status = EXCLUDED.status,
    64  			tx_hash = EXCLUDED.tx_hash;
    65  		`,
    66  		p.UpgradeBlockHeight, p.VegaReleaseTag, p.Approvers, p.Status, p.VegaTime, p.TxHash)
    67  	return err
    68  }
    69  
    70  func (ps *ProtocolUpgradeProposals) List(ctx context.Context,
    71  	status *entities.ProtocolUpgradeProposalStatus,
    72  	approvedBy *string,
    73  	pagination entities.CursorPagination,
    74  ) ([]entities.ProtocolUpgradeProposal, entities.PageInfo, error) {
    75  	args := []interface{}{}
    76  	query := `
    77          SELECT upgrade_block_height,
    78                 vega_release_tag,
    79                 approvers,
    80                 status,
    81                 vega_time,
    82                 tx_hash
    83          FROM protocol_upgrade_proposals_current
    84  	`
    85  	var predicates []string
    86  	var err error
    87  
    88  	if status != nil {
    89  		predicates = append(predicates, fmt.Sprintf("status=%s", nextBindVar(&args, *status)))
    90  	}
    91  
    92  	if approvedBy != nil {
    93  		predicates = append(predicates, fmt.Sprintf("%s=ANY(approvers)", nextBindVar(&args, *approvedBy)))
    94  	}
    95  
    96  	if len(predicates) > 0 {
    97  		query += fmt.Sprintf(" WHERE %s", strings.Join(predicates, " AND "))
    98  	}
    99  
   100  	pageInfo := entities.PageInfo{}
   101  	query, args, err = PaginateQuery[entities.ProtocolUpgradeProposalCursor](query, args, pupOrdering, pagination)
   102  	if err != nil {
   103  		return nil, pageInfo, err
   104  	}
   105  
   106  	defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "List")()
   107  	pups := make([]entities.ProtocolUpgradeProposal, 0)
   108  	if err := pgxscan.Select(ctx, ps.ConnectionSource, &pups, query, args...); err != nil {
   109  		return pups, pageInfo, err
   110  	}
   111  
   112  	pups, pageInfo = entities.PageEntities[*v2.ProtocolUpgradeProposalEdge](pups, pagination)
   113  	return pups, pageInfo, nil
   114  }
   115  
   116  func (ps *ProtocolUpgradeProposals) GetByTxHash(
   117  	ctx context.Context,
   118  	txHash entities.TxHash,
   119  ) ([]entities.ProtocolUpgradeProposal, error) {
   120  	defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "GetByTxHash")()
   121  
   122  	var pups []entities.ProtocolUpgradeProposal
   123  	query := `SELECT upgrade_block_height, vega_release_tag, approvers, status, vega_time, tx_hash
   124  		FROM protocol_upgrade_proposals WHERE tx_hash = $1`
   125  
   126  	if err := pgxscan.Select(ctx, ps.ConnectionSource, &pups, query, txHash); err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	return pups, nil
   131  }