code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/ethereum_key_rotations.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  var ekrOrdering = TableOrdering{
    29  	ColumnOrdering{Name: "vega_time", Sorting: ASC},
    30  	ColumnOrdering{Name: "node_id", Sorting: ASC},
    31  	ColumnOrdering{Name: "old_address", Sorting: ASC},
    32  	ColumnOrdering{Name: "new_address", Sorting: ASC},
    33  }
    34  
    35  type EthereumKeyRotations struct {
    36  	*ConnectionSource
    37  }
    38  
    39  func NewEthereumKeyRotations(connectionSource *ConnectionSource) *EthereumKeyRotations {
    40  	return &EthereumKeyRotations{
    41  		ConnectionSource: connectionSource,
    42  	}
    43  }
    44  
    45  func (store *EthereumKeyRotations) Add(ctx context.Context, kr entities.EthereumKeyRotation) error {
    46  	defer metrics.StartSQLQuery("EthereumKeyRotations", "Add")()
    47  	_, err := store.Exec(ctx, `
    48  		INSERT INTO ethereum_key_rotations(node_id, old_address, new_address, block_height, tx_hash, vega_time, seq_num)
    49  		VALUES ($1, $2, $3, $4, $5, $6, $7)
    50  	`, kr.NodeID, kr.OldAddress, kr.NewAddress, kr.BlockHeight, kr.TxHash, kr.VegaTime, kr.SeqNum)
    51  
    52  	return err
    53  }
    54  
    55  func (store *EthereumKeyRotations) List(ctx context.Context,
    56  	nodeID entities.NodeID,
    57  	pagination entities.CursorPagination,
    58  ) ([]entities.EthereumKeyRotation, entities.PageInfo, error) {
    59  	defer metrics.StartSQLQuery("EthereumKeyRotations", "List")()
    60  
    61  	args := []interface{}{}
    62  	whereClause := ""
    63  
    64  	if nodeID.String() != "" {
    65  		whereClause = `WHERE node_id = $1`
    66  		args = append(args, nodeID)
    67  	}
    68  
    69  	query := `SELECT * FROM ethereum_key_rotations ` + whereClause
    70  
    71  	query, args, err := PaginateQuery[entities.EthereumKeyRotationCursor](query, args, ekrOrdering, pagination)
    72  	if err != nil {
    73  		return nil, entities.PageInfo{}, err
    74  	}
    75  
    76  	ethereumKeyRotations := []entities.EthereumKeyRotation{}
    77  
    78  	if err = pgxscan.Select(ctx, store.ConnectionSource, &ethereumKeyRotations, query, args...); err != nil {
    79  		return nil, entities.PageInfo{}, err
    80  	}
    81  
    82  	paged, pageInfo := entities.PageEntities[*v2.EthereumKeyRotationEdge](ethereumKeyRotations, pagination)
    83  	return paged, pageInfo, nil
    84  }
    85  
    86  func (store *EthereumKeyRotations) GetByTxHash(
    87  	ctx context.Context,
    88  	txHash entities.TxHash,
    89  ) ([]entities.EthereumKeyRotation, error) {
    90  	defer metrics.StartSQLQuery("EthereumKeyRotations", "GetByTxHash")()
    91  
    92  	var ethereumKeyRotations []entities.EthereumKeyRotation
    93  	query := `SELECT * FROM ethereum_key_rotations WHERE tx_hash = $1`
    94  
    95  	if err := pgxscan.Select(ctx, store.ConnectionSource, &ethereumKeyRotations, query, txHash); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return ethereumKeyRotations, nil
   100  }