-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsource_envelope.go
More file actions
457 lines (431 loc) · 14.2 KB
/
source_envelope.go
File metadata and controls
457 lines (431 loc) · 14.2 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package monitoring
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/smartcontractkit/libocr/offchainreporting2/types"
relayMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/adapters/cosmwasm"
"github.com/smartcontractkit/chainlink-cosmos/pkg/monitoring/fcdclient"
)
// NewEnvelopeSourceFactory build a new object that reads observations and
// configurations from the Cosmos chain.
func NewEnvelopeSourceFactory(
rpcClient ChainReader,
fcdClient fcdclient.Client,
log relayMonitoring.Logger,
) relayMonitoring.SourceFactory {
return &envelopeSourceFactory{rpcClient, fcdClient, log}
}
type envelopeSourceFactory struct {
rpcClient ChainReader
fcdClient fcdclient.Client
log relayMonitoring.Logger
}
func (e *envelopeSourceFactory) NewSource(
chainConfig relayMonitoring.ChainConfig,
feedConfig relayMonitoring.FeedConfig,
) (relayMonitoring.Source, error) {
cosmosConfig, ok := chainConfig.(CosmosConfig)
if !ok {
return nil, fmt.Errorf("expected chainConfig to be of type CosmosConfig not %T", chainConfig)
}
cosmosFeedConfig, ok := feedConfig.(CosmosFeedConfig)
if !ok {
return nil, fmt.Errorf("expected feedConfig to be of type CosmosFeedConfig not %T", feedConfig)
}
return &envelopeSource{
e.rpcClient,
e.fcdClient,
e.log,
cosmosConfig,
cosmosFeedConfig,
sync.Mutex{},
types.ContractConfig{}, // initial value for cached ContractConfig
0, // initial value for the block height of the latest cached contract config
}, nil
}
func (e *envelopeSourceFactory) GetType() string {
return "envelope"
}
type envelopeSource struct {
rpcClient ChainReader
fcdClient fcdclient.Client
log relayMonitoring.Logger
cosmosConfig CosmosConfig
cosmosFeedConfig CosmosFeedConfig
cachedConfigMu sync.Mutex
cachedConfig types.ContractConfig
cachedConfigBlock uint64
}
func (e *envelopeSource) Fetch(ctx context.Context) (interface{}, error) {
envelope := relayMonitoring.Envelope{}
var envelopeErr error
envelopeMu := &sync.Mutex{}
wg := &sync.WaitGroup{}
wg.Add(4)
go func() {
defer wg.Done()
data, err := e.fetchLatestTransmission(ctx)
envelopeMu.Lock()
defer envelopeMu.Unlock()
if err != nil {
envelopeErr = errors.Join(envelopeErr, fmt.Errorf("failed to fetch transmission: %w", err))
return
}
envelope.ConfigDigest = data.configDigest
envelope.Epoch = data.epoch
envelope.Round = data.round
envelope.LatestAnswer = data.latestAnswer
envelope.LatestTimestamp = data.latestTimestamp
// Note: block number is read from the transmission transaction, not set_config!
envelope.BlockNumber = data.blockNumber
envelope.Transmitter = data.transmitter
envelope.JuelsPerFeeCoin = data.juelsPerFeeCoin
envelope.AggregatorRoundID = data.aggregatorRoundID
}()
go func() {
defer wg.Done()
contractConfig, err := e.fetchLatestConfig(ctx)
envelopeMu.Lock()
defer envelopeMu.Unlock()
if err != nil {
envelopeErr = errors.Join(envelopeErr, fmt.Errorf("failed to fetch config: %w", err))
return
}
envelope.ContractConfig = contractConfig
}()
go func() {
defer wg.Done()
balance, err := e.fetchLinkBalance(ctx)
envelopeMu.Lock()
defer envelopeMu.Unlock()
if err != nil {
envelopeErr = errors.Join(envelopeErr, fmt.Errorf("failed to fetch link balance: %w", err))
return
}
envelope.LinkBalance = balance
}()
go func() {
defer wg.Done()
amount, err := e.fetchLinkAvailableForPayment(ctx)
envelopeMu.Lock()
defer envelopeMu.Unlock()
if err != nil {
envelopeErr = errors.Join(envelopeErr, fmt.Errorf("failed to fetch link balance: %w", err))
return
}
envelope.LinkAvailableForPayment = amount
}()
wg.Wait()
return envelope, envelopeErr
}
type transmissionData struct {
configDigest types.ConfigDigest
epoch uint32
round uint8
latestAnswer *big.Int
latestTimestamp time.Time
blockNumber uint64
transmitter types.Account
aggregatorRoundID uint32
juelsPerFeeCoin *big.Int
}
func (e *envelopeSource) fetchLatestTransmission(ctx context.Context) (transmissionData, error) {
res, err := e.fcdClient.GetTxList(ctx, fcdclient.GetTxListParams{
Account: e.cosmosFeedConfig.ContractAddress,
Limit: 10, // there should be a new transmission in the last 10 blocks
})
if err != nil {
return transmissionData{}, fmt.Errorf("failed to fetch latest 'new_transmission' event: %w", err)
}
data := transmissionData{}
err = e.extractDataFromTxResponse("wasm-new_transmission", e.cosmosFeedConfig.ContractAddressBech32, res, map[string]func(string) error{
"config_digest": func(value string) error {
return cosmwasm.HexToConfigDigest(value, &data.configDigest)
},
"epoch": func(value string) error {
rawEpoch, parseErr := strconv.ParseUint(value, 10, 32)
data.epoch = uint32(rawEpoch)
return parseErr
},
"round": func(value string) error {
rawRound, parseErr := strconv.ParseUint(value, 10, 8)
data.round = uint8(rawRound)
return parseErr
},
"answer": func(value string) error {
var success bool
data.latestAnswer, success = new(big.Int).SetString(value, 10)
if !success {
return fmt.Errorf("failed to read latest answer from value '%s'", value)
}
return nil
},
"observations_timestamp": func(value string) error {
rawTimestamp, parseErr := strconv.ParseInt(value, 10, 64)
data.latestTimestamp = time.Unix(rawTimestamp, 0)
return parseErr
},
"transmitter": func(value string) error {
data.transmitter = types.Account(value)
return nil
},
"aggregator_round_id": func(value string) error {
raw, pasrseErr := strconv.ParseUint(value, 10, 32)
data.aggregatorRoundID = uint32(raw)
return pasrseErr
},
"juels_per_fee_coin": func(value string) error {
var success bool
data.juelsPerFeeCoin, success = new(big.Int).SetString(value, 10)
if !success {
return fmt.Errorf("failed to parse juel per fee coin from '%s'", value)
}
return nil
},
})
if err != nil {
return data, fmt.Errorf("failed to extract transmission from logs: %w", err)
}
data.blockNumber, err = strconv.ParseUint(res.Txs[0].Height, 10, 64)
if err != nil {
return data, fmt.Errorf("failed to parse block height from fcd data '%s': %w", res.Txs[0].Height, err)
}
return data, nil
}
func (e *envelopeSource) fetchLatestConfig(ctx context.Context) (types.ContractConfig, error) {
var cachedConfig types.ContractConfig
var cachedConfigBlock uint64
go func() {
e.cachedConfigMu.Lock()
defer e.cachedConfigMu.Unlock()
cachedConfig = e.cachedConfig
cachedConfigBlock = e.cachedConfigBlock
}()
latestConfigBlock, err := e.fetchLatestConfigBlock(ctx)
if err != nil {
return types.ContractConfig{}, err
}
if cachedConfigBlock != 0 && latestConfigBlock == cachedConfigBlock {
return cachedConfig, nil
}
latestConfig, err := e.fetchLatestConfigFromLogs(ctx, latestConfigBlock)
if err != nil {
return types.ContractConfig{}, err
}
// Cache the config and block height
e.cachedConfigMu.Lock()
defer e.cachedConfigMu.Unlock()
e.cachedConfig = latestConfig
e.cachedConfigBlock = latestConfigBlock
return latestConfig, nil
}
func (e *envelopeSource) fetchLatestConfigBlock(ctx context.Context) (uint64, error) {
resp, err := e.rpcClient.ContractState(
ctx,
e.cosmosFeedConfig.ContractAddress,
[]byte(`{"latest_config_details":{}}`),
)
var details cosmwasm.ConfigDetails
if err != nil {
return 0, fmt.Errorf("failed to fetch config details: %w", err)
}
if err = json.Unmarshal(resp, &details); err != nil {
return 0, fmt.Errorf("failed to unmarshal config details: %w", err)
}
return details.BlockNumber, nil
}
func (e *envelopeSource) fetchLatestConfigFromLogs(ctx context.Context, blockHeight uint64) (types.ContractConfig, error) {
res, err := e.fcdClient.GetBlockAtHeight(ctx, blockHeight)
if err != nil {
return types.ContractConfig{}, fmt.Errorf("failed to fetch block at height: %w", err)
}
output := types.ContractConfig{}
err = e.extractDataFromTxResponse("wasm-set_config", e.cosmosFeedConfig.ContractAddressBech32, res, map[string]func(string) error{
"latest_config_digest": func(value string) error {
// parse byte array encoded as hex string
return cosmwasm.HexToConfigDigest(value, &output.ConfigDigest)
},
"config_count": func(value string) error {
i, parseErr := strconv.ParseInt(value, 10, 64)
output.ConfigCount = uint64(i)
return parseErr
},
"signers": func(value string) error {
// this assumes the value will be a hex encoded string which each signer
// 32 bytes and each signer will be a separate parameter
var v []byte
convertErr := cosmwasm.HexToByteArray(value, &v)
output.Signers = append(output.Signers, v)
return convertErr
},
"transmitters": func(value string) error {
// this assumes the return value be a string for each transmitter and each transmitter will be separate
output.Transmitters = append(output.Transmitters, types.Account(value))
return nil
},
"f": func(value string) error {
i, parseErr := strconv.ParseInt(value, 10, 8)
output.F = uint8(i)
return parseErr
},
"onchain_config": func(value string) error {
// parse byte array encoded as base64
config, decodeErr := base64.StdEncoding.DecodeString(value)
output.OnchainConfig = config
return decodeErr
},
"offchain_config_version": func(value string) error {
i, parseErr := strconv.ParseInt(value, 10, 64)
output.OffchainConfigVersion = uint64(i)
return parseErr
},
"offchain_config": func(value string) error {
// parse byte array encoded as hex string
config, convertErr := base64.StdEncoding.DecodeString(value)
output.OffchainConfig = config
return convertErr
},
})
if err != nil {
return types.ContractConfig{}, fmt.Errorf("failed to extract config from logs: %w", err)
}
return output, nil
}
type linkBalanceResponse struct {
Balance string `json:"balance"`
}
func (e *envelopeSource) fetchLinkBalance(ctx context.Context) (*big.Int, error) {
query := fmt.Sprintf(`{"balance":{"address":"%s"}}`, e.cosmosFeedConfig.ContractAddressBech32)
res, err := e.rpcClient.ContractState(
ctx,
e.cosmosConfig.LinkTokenAddress,
[]byte(query),
)
if err != nil {
return nil, fmt.Errorf("failed to fetch balance: %w", err)
}
balanceRes := linkBalanceResponse{}
if err = json.Unmarshal(res, &balanceRes); err != nil {
return nil, fmt.Errorf("failed to unmarshal balance response: %w", err)
}
balance, success := new(big.Int).SetString(balanceRes.Balance, 10)
if !success {
return nil, fmt.Errorf("failed to parse link balance from '%s'", balanceRes.Balance)
}
return balance, nil
}
type linkAvailableForPaymentRes struct {
Amount string `json:"amount,omitempty"`
}
func (e *envelopeSource) fetchLinkAvailableForPayment(ctx context.Context) (*big.Int, error) {
res, err := e.rpcClient.ContractState(
ctx,
e.cosmosFeedConfig.ContractAddress,
[]byte(`{"link_available_for_payment":{}}`),
)
if err != nil {
return nil, fmt.Errorf("failed to read link_available_for_payment from the contract: %w", err)
}
linkAvailableForPayment := linkAvailableForPaymentRes{}
if err := json.Unmarshal(res, &linkAvailableForPayment); err != nil {
return nil, fmt.Errorf("failed to unmarshal link available data from the response '%s': %w", string(res), err)
}
amount, success := new(big.Int).SetString(linkAvailableForPayment.Amount, 10)
if !success {
return nil, fmt.Errorf("failed to parse amount of link available for payment from string '%s' into a big.Int", linkAvailableForPayment.Amount)
}
return amount, nil
}
// Helpers
func (e *envelopeSource) extractDataFromTxResponse(
eventType string,
contractAddressBech32 string,
res fcdclient.Response,
extractors map[string]func(string) error,
) error {
// Extract matching events
events := extractMatchingEvents(res, eventType, contractAddressBech32)
if len(events) == 0 {
return fmt.Errorf("no event found with type='%s' and contract_address='%s'", eventType, contractAddressBech32)
}
if len(events) != 1 {
e.log.Debugw("multiple matching events found, selecting the most recent one which is the first", "type", eventType, "contract_address", contractAddressBech32)
}
event := events[0]
if err := checkEventAttributes(event, extractors); err != nil {
return fmt.Errorf("received incorrect event with type='%s' and contract_address='%s': %w", eventType, contractAddressBech32, err)
}
// Apply extractors.
// Note! If multiple attributes with the same key are present, the corresponding
// extractor fn will be called for each of them.
for _, attribute := range event.Attributes {
key, value := attribute.Key, attribute.Value
extractor, found := extractors[key]
if !found {
continue
}
if err := extractor(value); err != nil {
return fmt.Errorf("failed to extract '%s' from raw value '%s': %w", key, value, err)
}
}
return nil
}
func extractMatchingEvents(res fcdclient.Response, eventType, contractAddressBech32 string) []fcdclient.Event {
out := []fcdclient.Event{}
// Sort txs such that the most recent tx is first
sort.Slice(res.Txs, func(i, j int) bool {
return res.Txs[i].ID > res.Txs[j].ID
})
for _, tx := range res.Txs {
if !strings.Contains(tx.RawLog, fmt.Sprintf(`"type":"%s"`, eventType)) {
continue
}
for _, event := range tx.Logs[0].Events {
if event.Typ != eventType {
continue
}
isMatchingContractAddress := false
for _, attribute := range event.Attributes {
if attribute.Key == "contract_address" && attribute.Value == contractAddressBech32 {
isMatchingContractAddress = true
break
}
}
if isMatchingContractAddress {
out = append(out, event)
}
}
}
return out
}
func checkEventAttributes(
event fcdclient.Event,
extractors map[string]func(string) error,
) error {
// The event should have at least one attribute with the Key in the extractors map.
isPresent := map[string]bool{}
for key := range extractors {
isPresent[key] = false
}
for _, attribute := range event.Attributes {
if _, found := extractors[attribute.Key]; found {
isPresent[attribute.Key] = true
}
}
for key, found := range isPresent {
if !found {
return fmt.Errorf("failed to extract key '%s' from event", key)
}
}
return nil
}