-
Notifications
You must be signed in to change notification settings - Fork 19
/
protocol_upgrade_proposals.go
131 lines (111 loc) · 3.96 KB
/
protocol_upgrade_proposals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sqlstore
import (
"context"
"fmt"
"strings"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/georgysavva/scany/pgxscan"
)
type ProtocolUpgradeProposals struct {
*ConnectionSource
}
var pupOrdering = TableOrdering{
ColumnOrdering{Name: "upgrade_block_height", Sorting: ASC},
ColumnOrdering{Name: "vega_release_tag", Sorting: ASC},
}
func NewProtocolUpgradeProposals(connectionSource *ConnectionSource) *ProtocolUpgradeProposals {
p := &ProtocolUpgradeProposals{
ConnectionSource: connectionSource,
}
return p
}
func (ps *ProtocolUpgradeProposals) Add(ctx context.Context, p entities.ProtocolUpgradeProposal) error {
defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "Add")()
if p.Approvers == nil {
p.Approvers = []string{}
}
_, err := ps.Connection.Exec(ctx,
`INSERT INTO protocol_upgrade_proposals(
upgrade_block_height,
vega_release_tag,
approvers,
status,
vega_time,
tx_hash)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (vega_time, upgrade_block_height, vega_release_tag) DO UPDATE SET
approvers = EXCLUDED.approvers,
status = EXCLUDED.status,
tx_hash = EXCLUDED.tx_hash;
`,
p.UpgradeBlockHeight, p.VegaReleaseTag, p.Approvers, p.Status, p.VegaTime, p.TxHash)
return err
}
func (ps *ProtocolUpgradeProposals) List(ctx context.Context,
status *entities.ProtocolUpgradeProposalStatus,
approvedBy *string,
pagination entities.CursorPagination,
) ([]entities.ProtocolUpgradeProposal, entities.PageInfo, error) {
args := []interface{}{}
query := `
SELECT upgrade_block_height,
vega_release_tag,
approvers,
status,
vega_time,
tx_hash
FROM protocol_upgrade_proposals_current
`
var predicates []string
var err error
if status != nil {
predicates = append(predicates, fmt.Sprintf("status=%s", nextBindVar(&args, *status)))
}
if approvedBy != nil {
predicates = append(predicates, fmt.Sprintf("%s=ANY(approvers)", nextBindVar(&args, *approvedBy)))
}
if len(predicates) > 0 {
query += fmt.Sprintf(" WHERE %s", strings.Join(predicates, " AND "))
}
pageInfo := entities.PageInfo{}
query, args, err = PaginateQuery[entities.ProtocolUpgradeProposalCursor](query, args, pupOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "List")()
pups := make([]entities.ProtocolUpgradeProposal, 0)
if err := pgxscan.Select(ctx, ps.Connection, &pups, query, args...); err != nil {
return pups, pageInfo, err
}
pups, pageInfo = entities.PageEntities[*v2.ProtocolUpgradeProposalEdge](pups, pagination)
return pups, pageInfo, nil
}
func (ps *ProtocolUpgradeProposals) GetByTxHash(
ctx context.Context,
txHash entities.TxHash,
) ([]entities.ProtocolUpgradeProposal, error) {
defer metrics.StartSQLQuery("ProtocolUpgradeProposals", "GetByTxHash")()
var pups []entities.ProtocolUpgradeProposal
query := `SELECT upgrade_block_height, vega_release_tag, approvers, status, vega_time, tx_hash
FROM protocol_upgrade_proposals WHERE tx_hash = $1`
if err := pgxscan.Select(ctx, ps.Connection, &pups, query, txHash); err != nil {
return nil, err
}
return pups, nil
}