-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsource_txresults.go
More file actions
101 lines (89 loc) · 2.44 KB
/
source_txresults.go
File metadata and controls
101 lines (89 loc) · 2.44 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
package monitoring
import (
"context"
"fmt"
"sync"
relayMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
"github.com/smartcontractkit/chainlink-cosmos/pkg/monitoring/fcdclient"
)
// NewTxResultsSourceFactory builds sources of TxResults objects expected by the relay monitoring.
func NewTxResultsSourceFactory(
client fcdclient.Client,
) relayMonitoring.SourceFactory {
return &txResultsSourceFactory{client}
}
type txResultsSourceFactory struct {
client fcdclient.Client
}
func (t *txResultsSourceFactory) 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 &txResultsSource{
cosmosConfig,
cosmosFeedConfig,
t.client,
0,
sync.Mutex{},
}, nil
}
func (t *txResultsSourceFactory) GetType() string {
return "txresults"
}
type txResultsSource struct {
cosmosConfig CosmosConfig
cosmosFeedConfig CosmosFeedConfig
client fcdclient.Client
latestTxID uint64
latestTxIDMu sync.Mutex
}
func (t *txResultsSource) Fetch(ctx context.Context) (interface{}, error) {
// Query the FCD endpoint.
response, err := t.client.GetTxList(ctx, fcdclient.GetTxListParams{
Account: t.cosmosFeedConfig.ContractAddress,
Limit: 10,
})
if err != nil {
return nil, fmt.Errorf("unable to fetch transactions from cosmos FCD: %w", err)
}
// Filter recent transactions
// TODO (dru) keep latest processed tx in the state.
recentTxs := []fcdclient.Tx{}
func() {
t.latestTxIDMu.Lock()
defer t.latestTxIDMu.Unlock()
maxTxID := t.latestTxID
for _, tx := range response.Txs {
if tx.ID > t.latestTxID {
recentTxs = append(recentTxs, tx)
}
if tx.ID > maxTxID {
maxTxID = tx.ID
}
}
t.latestTxID = maxTxID
}()
// Count failed and succeeded recent transactions
output := relayMonitoring.TxResults{}
for _, tx := range recentTxs {
if isFailedTransaction(tx) {
output.NumFailed++
} else {
output.NumSucceeded++
}
}
return output, nil
}
// Helpers
func isFailedTransaction(tx fcdclient.Tx) bool {
// See https://docs.cosmos.network/master/building-modules/errors.html
return tx.Code != 0
}