-
Notifications
You must be signed in to change notification settings - Fork 22
/
parties.go
192 lines (159 loc) · 5.59 KB
/
parties.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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"
"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"
)
var (
partiesOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "id", Sorting: ASC},
}
partiesProfilesOrdering = TableOrdering{
ColumnOrdering{Name: "id", Sorting: ASC},
}
)
type Parties struct {
*ConnectionSource
}
func NewParties(connectionSource *ConnectionSource) *Parties {
ps := &Parties{
ConnectionSource: connectionSource,
}
return ps
}
// Initialise adds the built-in 'network' party which is never explicitly sent on the event
// bus, but nonetheless is necessary.
func (ps *Parties) Initialise(ctx context.Context) {
defer metrics.StartSQLQuery("Parties", "Initialise")()
_, err := ps.Connection.Exec(ctx,
`INSERT INTO parties(id, tx_hash, alias) VALUES ($1, $2, $3) ON CONFLICT (id) DO NOTHING`,
entities.PartyID("network"), entities.TxHash("01"), "network")
if err != nil {
panic(fmt.Errorf("unable to add built-in network party: %w", err))
}
}
func (ps *Parties) Add(ctx context.Context, p entities.Party) error {
defer metrics.StartSQLQuery("Parties", "Add")()
_, err := ps.Connection.Exec(ctx,
`INSERT INTO parties(id, tx_hash, vega_time)
VALUES ($1, $2, $3)
ON CONFLICT (id) DO NOTHING`,
p.ID,
p.TxHash,
p.VegaTime,
)
return err
}
func (ps *Parties) UpdateProfile(ctx context.Context, p *entities.PartyProfile) error {
defer metrics.StartSQLQuery("Parties", "Add")()
_, err := ps.Connection.Exec(ctx,
`UPDATE parties SET alias = $1, metadata = $2 WHERE id = $3`,
p.Alias,
p.Metadata,
p.PartyID,
)
return err
}
func (ps *Parties) ListProfiles(ctx context.Context, ids []string, pagination entities.CursorPagination) ([]entities.PartyProfile, entities.PageInfo, error) {
defer metrics.StartSQLQuery("Parties", "ListProfiles")()
profiles := make([]entities.PartyProfile, 0)
args := make([]interface{}, 0)
whereClause := ""
if len(ids) > 0 {
partyIDs := make([][]byte, len(ids))
for i, id := range ids {
partyID := entities.PartyID(id)
partyIDBytes, err := partyID.Bytes()
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("invalid party ID found: %w", err)
}
partyIDs[i] = partyIDBytes
}
whereClause = fmt.Sprintf(" where id = ANY(%s)", nextBindVar(&args, partyIDs))
}
query := `SELECT id AS party_id, alias, metadata FROM parties` + whereClause
var pageInfo entities.PageInfo
query, args, err := PaginateQuery[entities.PartyProfile](query, args, partiesProfilesOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
if err := pgxscan.Select(ctx, ps.Connection, &profiles, query, args...); err != nil {
return nil, pageInfo, err
}
profiles, pageInfo = entities.PageEntities[*v2.PartyProfileEdge](profiles, pagination)
return profiles, pageInfo, nil
}
func (ps *Parties) GetByID(ctx context.Context, id string) (entities.Party, error) {
a := entities.Party{}
defer metrics.StartSQLQuery("Parties", "GetByID")()
err := pgxscan.Get(ctx, ps.Connection, &a,
`SELECT id, tx_hash, vega_time
FROM parties WHERE id=$1`,
entities.PartyID(id))
return a, ps.wrapE(err)
}
func (ps *Parties) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Party, error) {
defer metrics.StartSQLQuery("Parties", "GetByTxHash")()
var parties []entities.Party
err := pgxscan.Select(ctx, ps.Connection, &parties, `SELECT id, tx_hash, vega_time FROM parties WHERE tx_hash=$1`, txHash)
if err != nil {
return nil, ps.wrapE(err)
}
return parties, nil
}
func (ps *Parties) GetAll(ctx context.Context) ([]entities.Party, error) {
parties := []entities.Party{}
defer metrics.StartSQLQuery("Parties", "GetAll")()
err := pgxscan.Select(ctx, ps.Connection, &parties, `
SELECT id, tx_hash, vega_time
FROM parties`)
return parties, err
}
func (ps *Parties) GetAllPaged(ctx context.Context, partyID string, pagination entities.CursorPagination) ([]entities.Party, entities.PageInfo, error) {
if partyID != "" {
party, err := ps.GetByID(ctx, partyID)
if err != nil {
return nil, entities.PageInfo{}, err
}
return []entities.Party{party}, entities.PageInfo{
HasNextPage: false,
HasPreviousPage: false,
StartCursor: party.Cursor().Encode(),
EndCursor: party.Cursor().Encode(),
}, nil
}
parties := make([]entities.Party, 0)
args := make([]interface{}, 0)
query := `
SELECT id, tx_hash, vega_time
FROM parties
`
var pageInfo entities.PageInfo
query, args, err := PaginateQuery[entities.Party](query, args, partiesOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
if err := pgxscan.Select(ctx, ps.Connection, &parties, query, args...); err != nil {
return nil, pageInfo, err
}
parties, pageInfo = entities.PageEntities[*v2.PartyEdge](parties, pagination)
return parties, pageInfo, nil
}