-
Notifications
You must be signed in to change notification settings - Fork 19
/
oracle_spec.go
188 lines (160 loc) · 5.5 KB
/
oracle_spec.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
// 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 OracleSpec struct {
*ConnectionSource
}
var oracleSpecOrdering = TableOrdering{
ColumnOrdering{Name: "id", Sorting: ASC},
ColumnOrdering{Name: "vega_time", Sorting: ASC},
}
const (
sqlOracleSpecColumns = `id, created_at, updated_at, signers, filters, status, tx_hash, vega_time`
)
func NewOracleSpec(connectionSource *ConnectionSource) *OracleSpec {
return &OracleSpec{
ConnectionSource: connectionSource,
}
}
func (os *OracleSpec) Upsert(ctx context.Context, spec *entities.OracleSpec) error {
query := fmt.Sprintf(`insert into oracle_specs(%s)
values ($1, $2, $3, $4, $5, $6, $7, $8)
on conflict (id, vega_time) do update
set
created_at=EXCLUDED.created_at,
updated_at=EXCLUDED.updated_at,
signers=EXCLUDED.signers,
filters=EXCLUDED.filters,
status=EXCLUDED.status,
tx_hash=EXCLUDED.tx_hash`, sqlOracleSpecColumns)
defer metrics.StartSQLQuery("OracleSpec", "Upsert")()
dataSourceSpec := spec.ExternalDataSourceSpec.Spec
signers := []entities.Signer{}
filters := []entities.Filter{}
if dataSourceSpec.Data != nil {
if dataSourceSpec.Data.External != nil {
filters = dataSourceSpec.Data.External.Filters
signers = dataSourceSpec.Data.External.Signers
}
}
if _, err := os.Connection.Exec(ctx, query, dataSourceSpec.ID, dataSourceSpec.CreatedAt, dataSourceSpec.UpdatedAt, signers,
filters, dataSourceSpec.Status, dataSourceSpec.TxHash, dataSourceSpec.VegaTime); err != nil {
return err
}
return nil
}
func (os *OracleSpec) GetSpecByID(ctx context.Context, specID string) (*entities.OracleSpec, error) {
defer metrics.StartSQLQuery("OracleSpec", "GetByID")()
var spec entities.DataSourceSpecRaw
query := fmt.Sprintf(`%s
where id = $1
order by id, vega_time desc`, getOracleSpecsQuery())
err := pgxscan.Get(ctx, os.Connection, &spec, query, entities.SpecID(specID))
if err != nil {
return nil, os.wrapE(err)
}
oracleSpec := dataSourceSpecRawToOracleSpec(spec)
return &oracleSpec, err
}
func (os *OracleSpec) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.OracleSpec, error) {
defer metrics.StartSQLQuery("OracleSpec", "GetByTxHash")()
var specsRaw []entities.DataSourceSpecRaw
query := "SELECT * FROM oracle_specs WHERE tx_hash = $1"
err := pgxscan.Select(ctx, os.Connection, &specsRaw, query, txHash)
if err != nil {
return nil, os.wrapE(err)
}
specs := []entities.OracleSpec{}
for _, spec := range specsRaw {
specs = append(specs, dataSourceSpecRawToOracleSpec(spec))
}
return specs, err
}
func (os *OracleSpec) GetSpecsWithCursorPagination(ctx context.Context, specID string, pagination entities.CursorPagination) (
[]entities.OracleSpec, entities.PageInfo, error,
) {
if specID != "" {
return os.getSingleSpecWithPageInfo(ctx, specID)
}
return os.getSpecsWithPageInfo(ctx, pagination)
}
func (os *OracleSpec) getSingleSpecWithPageInfo(ctx context.Context, specID string) ([]entities.OracleSpec, entities.PageInfo, error) {
spec, err := os.GetSpecByID(ctx, specID)
if err != nil {
return nil, entities.PageInfo{}, err
}
return []entities.OracleSpec{*spec},
entities.PageInfo{
HasNextPage: false,
HasPreviousPage: false,
StartCursor: spec.Cursor().Encode(),
EndCursor: spec.Cursor().Encode(),
}, nil
}
func (os *OracleSpec) getSpecsWithPageInfo(ctx context.Context, pagination entities.CursorPagination) (
[]entities.OracleSpec, entities.PageInfo, error,
) {
var (
dataSpecs []entities.DataSourceSpecRaw
specs = []entities.OracleSpec{}
pageInfo entities.PageInfo
err error
args []interface{}
)
query := getOracleSpecsQuery()
query, args, err = PaginateQuery[entities.DataSourceSpecCursor](query, args, oracleSpecOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
if err = pgxscan.Select(ctx, os.Connection, &dataSpecs, query, args...); err != nil {
return nil, pageInfo, fmt.Errorf("querying oracle specs: %w", err)
}
if len(dataSpecs) > 0 {
for i := range dataSpecs {
specs = append(specs, dataSourceSpecRawToOracleSpec(dataSpecs[i]))
}
}
specs, pageInfo = entities.PageEntities[*v2.OracleSpecEdge](specs, pagination)
return specs, pageInfo, nil
}
func dataSourceSpecRawToOracleSpec(spec entities.DataSourceSpecRaw) entities.OracleSpec {
return entities.OracleSpec{
ExternalDataSourceSpec: &entities.ExternalDataSourceSpec{
Spec: &entities.DataSourceSpec{
ID: spec.ID,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
Data: &entities.DataSourceDefinition{
External: &entities.DataSourceDefinitionExternal{
Signers: spec.Signers,
Filters: spec.Filters,
},
},
Status: spec.Status,
TxHash: spec.TxHash,
VegaTime: spec.VegaTime,
},
},
}
}
func getOracleSpecsQuery() string {
return fmt.Sprintf(`select distinct on (id) %s
from oracle_specs`, sqlOracleSpecColumns)
}