This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
common.go
273 lines (247 loc) · 6.18 KB
/
common.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Package blockchain provides functionality to interact with
// different blockchain interfaces.
package blockchain
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/external-initiator/store"
"github.com/smartcontractkit/external-initiator/subscriber"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
promLastSourcePing = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "ei_last_source_ping",
Help: "The timestamp of the last source of life from the source",
}, []string{"endpoint", "jobid"})
)
var (
ErrConnectionType = errors.New("unknown connection type")
ErrSubscriberType = errors.New("unknown subscriber type")
)
// ExpectsMock variable is set when we run in a mock context
var ExpectsMock = false
var blockchains = []string{
ETH,
HMY,
XTZ,
Substrate,
ONT,
BSC,
NEAR,
IOTX,
CFX,
Keeper,
BIRITA,
Agoric,
Klaytn,
}
type Params struct {
Endpoint string `json:"endpoint"`
Addresses []string `json:"addresses"`
Topics []string `json:"topics"`
AccountIds []string `json:"accountIds"`
Address string `json:"address"`
UpkeepID string `json:"upkeepId"`
ServiceName string `json:"serviceName"`
From string `json:"from"`
}
// CreateJsonManager creates a new instance of a JSON blockchain manager with the provided
// connection type and store.Subscription config.
func CreateJsonManager(t subscriber.Type, sub store.Subscription) (subscriber.JsonManager, error) {
switch sub.Endpoint.Type {
case ETH:
return createEthManager(t, sub), nil
case HMY:
return createHmyManager(t, sub), nil
case BSC:
return createBscManager(t, sub), nil
case Substrate:
return createSubstrateManager(t, sub)
case NEAR:
return createNearManager(t, sub)
case CFX:
return createCfxManager(t, sub), nil
case Agoric:
return createAgoricManager(t, sub)
case Klaytn:
return createKlaytnManager(t, sub), nil
}
return nil, fmt.Errorf("unknown blockchain type %v for JSON manager", sub.Endpoint.Type)
}
// CreateClientManager creates a new instance of a subscriber.ISubscriber with the provided
// connection type and store.Subscription config.
func CreateClientManager(sub store.Subscription) (subscriber.ISubscriber, error) {
switch sub.Endpoint.Type {
case XTZ:
return createTezosSubscriber(sub), nil
case ONT:
return createOntSubscriber(sub), nil
case IOTX:
return createIoTeXSubscriber(sub)
case Keeper:
return createKeeperSubscriber(sub)
case BIRITA:
return createBSNIritaSubscriber(sub)
}
return nil, errors.New("unknown blockchain type for Client subscription")
}
func GetConnectionType(endpoint store.Endpoint) (subscriber.Type, error) {
switch endpoint.Type {
// Add blockchain implementations that encapsulate entire connection here
case XTZ, ONT, IOTX, Keeper, BIRITA:
return subscriber.Client, nil
default:
u, err := url.Parse(endpoint.Url)
if err != nil {
return subscriber.Unknown, err
}
if strings.HasPrefix(u.Scheme, "ws") {
return subscriber.WS, nil
} else if strings.HasPrefix(u.Scheme, "http") {
return subscriber.RPC, nil
}
return subscriber.Unknown, errors.New("unknown connection scheme")
}
}
func ValidBlockchain(name string) bool {
for _, blockchain := range blockchains {
if name == blockchain {
return true
}
}
return false
}
func GetValidations(t string, params Params) []int {
switch t {
case ETH, HMY, IOTX, Klaytn:
return []int{
len(params.Addresses) + len(params.Topics),
}
case XTZ:
return []int{
len(params.Addresses),
}
case Substrate:
return []int{
len(params.AccountIds),
}
case ONT:
return []int{
len(params.Addresses),
}
case BSC:
return []int{
len(params.Addresses),
}
case NEAR:
return []int{
len(params.AccountIds),
}
case CFX:
return []int{
len(params.Addresses) + len(params.Topics),
}
case Keeper:
return []int{
len(params.Address),
len(params.UpkeepID),
len(params.From),
}
case BIRITA:
return []int{
len(params.Addresses),
}
case Agoric:
return []int{
1,
}
}
return nil
}
func CreateSubscription(sub *store.Subscription, params Params) {
switch sub.Endpoint.Type {
case ETH, HMY, IOTX, Klaytn:
sub.Ethereum = store.EthSubscription{
Addresses: params.Addresses,
Topics: params.Topics,
}
case XTZ:
sub.Tezos = store.TezosSubscription{
Addresses: params.Addresses,
}
case Substrate:
sub.Substrate = store.SubstrateSubscription{
AccountIds: params.AccountIds,
}
case ONT:
sub.Ontology = store.OntSubscription{
Addresses: params.Addresses,
}
case BSC:
sub.BinanceSmartChain = store.BinanceSmartChainSubscription{
Addresses: params.Addresses,
}
case NEAR:
sub.NEAR = store.NEARSubscription{
AccountIds: params.AccountIds,
}
case CFX:
sub.Conflux = store.CfxSubscription{
Addresses: params.Addresses,
Topics: params.Topics,
}
case Keeper:
from := common.HexToAddress(params.From)
sub.Keeper = store.KeeperSubscription{
Address: params.Address,
UpkeepID: params.UpkeepID,
From: from,
}
case BIRITA:
sub.BSNIrita = store.BSNIritaSubscription{
Addresses: params.Addresses,
ServiceName: params.ServiceName,
}
case Agoric:
sub.Agoric = store.AgoricSubscription{}
}
}
// JsonrpcMessage declares JSON-RPC message type
type JsonrpcMessage struct {
Version string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Error *interface{} `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}
func convertStringArrayToKV(data []string) map[string]string {
result := make(map[string]string)
var key string
for i, val := range data {
if len(val) == 0 {
continue
}
if i%2 == 0 {
key = val
} else if len(key) != 0 {
result[key] = val
key = ""
}
}
return result
}
// matchesJobID checks if expected jobID matches the actual one, or are we in a mock context.
func matchesJobID(expected string, actual string) bool {
if actual == expected {
return true
} else if ExpectsMock && actual == "mock" {
return true
}
return false
}