-
Notifications
You must be signed in to change notification settings - Fork 19
/
parties.go
140 lines (117 loc) · 3.77 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"context"
"fmt"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/pkg/errors"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
"github.com/georgysavva/scany/pgxscan"
)
var (
ErrPartyNotFound = errors.New("party not found")
ErrInvalidPartyID = errors.New("invalid hex id")
)
var partiesOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
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() {
defer metrics.StartSQLQuery("Parties", "Initialise")()
_, err := ps.Connection.Exec(context.Background(),
`INSERT INTO parties(id, tx_hash) VALUES ($1, $2) ON CONFLICT (id) DO NOTHING`,
entities.PartyID("network"), entities.TxHash("01"))
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) 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))
if pgxscan.NotFound(err) {
return a, fmt.Errorf("'%v': %w", id, ErrPartyNotFound)
}
if errors.Is(err, entities.ErrInvalidID) {
return a, fmt.Errorf("'%v': %w", id, ErrInvalidPartyID)
}
return a, err
}
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
err error
)
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
}