-
Notifications
You must be signed in to change notification settings - Fork 19
/
oracle_data.go
208 lines (173 loc) · 6.92 KB
/
oracle_data.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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"
"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 OracleData struct {
*ConnectionSource
}
const (
sqlOracleDataColumns = `signers, data, matched_spec_ids, broadcast_at, tx_hash, vega_time, seq_num`
)
var oracleDataOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "signers", Sorting: ASC},
}
func NewOracleData(connectionSource *ConnectionSource) *OracleData {
return &OracleData{
ConnectionSource: connectionSource,
}
}
func (od *OracleData) Add(ctx context.Context, oracleData *entities.OracleData) error {
defer metrics.StartSQLQuery("OracleData", "Add")()
query := fmt.Sprintf("insert into oracle_data(%s) values ($1, $2, $3, $4, $5, $6, $7)", sqlOracleDataColumns)
if _, err := od.Connection.Exec(ctx, query, oracleData.ExternalData.Data.Signers, oracleData.ExternalData.Data.Data, oracleData.ExternalData.Data.MatchedSpecIds,
oracleData.ExternalData.Data.BroadcastAt, oracleData.ExternalData.Data.TxHash, oracleData.ExternalData.Data.VegaTime, oracleData.ExternalData.Data.SeqNum); err != nil {
err = fmt.Errorf("could not insert oracle data into database: %w", err)
return err
}
return nil
}
func (od *OracleData) GetOracleDataBySpecID(ctx context.Context, id string, pagination entities.Pagination) ([]entities.OracleData, entities.PageInfo, error) {
switch p := pagination.(type) {
case entities.OffsetPagination:
return getOracleDataBySpecIDOffsetPagination(ctx, od.Connection, id, p)
case entities.CursorPagination:
return getOracleDataBySpecIDCursorPagination(ctx, od.Connection, id, p)
default:
return nil, entities.PageInfo{}, fmt.Errorf("unrecognised pagination: %v", p)
}
}
func parseScannedDataToOracleData(scanned []entities.Data) []entities.OracleData {
oracleData := []entities.OracleData{}
if len(scanned) > 0 {
for _, s := range scanned {
oracleData = append(oracleData, entities.OracleData{
ExternalData: &entities.ExternalData{
Data: &entities.Data{
Signers: s.Signers,
Data: s.Data,
MatchedSpecIds: s.MatchedSpecIds,
BroadcastAt: s.BroadcastAt,
TxHash: s.TxHash,
VegaTime: s.VegaTime,
SeqNum: s.SeqNum,
},
},
})
}
}
return oracleData
}
func getOracleDataBySpecIDOffsetPagination(ctx context.Context, conn Connection, id string, pagination entities.OffsetPagination) (
[]entities.OracleData, entities.PageInfo, error,
) {
specID := entities.SpecID(id)
var bindVars []interface{}
var pageInfo entities.PageInfo
query := fmt.Sprintf(`select %s
from oracle_data where %s = ANY(matched_spec_ids)`, sqlOracleDataColumns, nextBindVar(&bindVars, specID))
query, bindVars = orderAndPaginateQuery(query, nil, pagination, bindVars...)
data := []entities.Data{}
var oracleData []entities.OracleData
defer metrics.StartSQLQuery("OracleData", "GetBySpecID")()
err := pgxscan.Select(ctx, conn, &data, query, bindVars...)
oracleData = parseScannedDataToOracleData(data)
return oracleData, pageInfo, err
}
func getOracleDataBySpecIDCursorPagination(ctx context.Context, conn Connection, id string, pagination entities.CursorPagination) (
[]entities.OracleData, entities.PageInfo, error,
) {
var (
oracleData []entities.OracleData
data = []entities.Data{}
pageInfo entities.PageInfo
bindVars []interface{}
err error
)
specID := entities.SpecID(id)
query := fmt.Sprintf(`select %s
from oracle_data where %s = ANY(matched_spec_ids)`, sqlOracleDataColumns, nextBindVar(&bindVars, specID))
query, bindVars, err = PaginateQuery[entities.OracleDataCursor](query, bindVars, oracleDataOrdering, pagination)
if err != nil {
return oracleData, pageInfo, err
}
defer metrics.StartSQLQuery("OracleData", "ListOracleData")()
// NOTE: If any error during the scan occurred, we return empty oracle data object.
if err = pgxscan.Select(ctx, conn, &data, query, bindVars...); err != nil {
return oracleData, pageInfo, err
}
oracleData = parseScannedDataToOracleData(data)
oracleData, pageInfo = entities.PageEntities[*v2.OracleDataEdge](oracleData, pagination)
return oracleData, pageInfo, nil
}
func (od *OracleData) ListOracleData(ctx context.Context, pagination entities.Pagination) ([]entities.OracleData, entities.PageInfo, error) {
switch p := pagination.(type) {
case entities.OffsetPagination:
return listOracleDataOffsetPagination(ctx, od.Connection, p)
case entities.CursorPagination:
return listOracleDataCursorPagination(ctx, od.Connection, p)
default:
return nil, entities.PageInfo{}, fmt.Errorf("unrecognised pagination: %v", p)
}
}
func listOracleDataOffsetPagination(ctx context.Context, conn Connection, pagination entities.OffsetPagination) (
[]entities.OracleData, entities.PageInfo, error,
) {
var (
data []entities.Data
oracleData []entities.OracleData
pageInfo entities.PageInfo
)
query := fmt.Sprintf(`%s
order by vega_time desc, matched_spec_id`, selectOracleData())
var bindVars []interface{}
query, bindVars = orderAndPaginateQuery(query, nil, pagination, bindVars...)
defer metrics.StartSQLQuery("OracleData", "ListOracleData")()
err := pgxscan.Select(ctx, conn, &data, query, bindVars...)
oracleData = parseScannedDataToOracleData(data)
return oracleData, pageInfo, err
}
func listOracleDataCursorPagination(ctx context.Context, conn Connection, pagination entities.CursorPagination) (
[]entities.OracleData, entities.PageInfo, error,
) {
var (
data []entities.Data
oracleData []entities.OracleData
pageInfo entities.PageInfo
bindVars []interface{}
err error
)
query := selectOracleData()
query, bindVars, err = PaginateQuery[entities.OracleDataCursor](query, bindVars, oracleDataOrdering, pagination)
if err != nil {
return oracleData, pageInfo, err
}
defer metrics.StartSQLQuery("OracleData", "ListOracleData")()
// NOTE: If any error during the scan occurred, we return empty oracle data object.
if err = pgxscan.Select(ctx, conn, &data, query, bindVars...); err != nil {
return oracleData, pageInfo, err
}
oracleData = parseScannedDataToOracleData(data)
oracleData, pageInfo = entities.PageEntities[*v2.OracleDataEdge](oracleData, pagination)
return oracleData, pageInfo, nil
}
func selectOracleData() string {
return fmt.Sprintf(`select %s
from oracle_data_current`, sqlOracleDataColumns)
}