-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchain_reader.go
More file actions
79 lines (69 loc) · 2.41 KB
/
chain_reader.go
File metadata and controls
79 lines (69 loc) · 2.41 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
package monitoring
import (
"context"
"fmt"
"sync"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"go.uber.org/ratelimit"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
pkgClient "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/client"
)
// ChainReader is a subset of the pkg/cosmos/client.Reader interface enhanced with context support.
type ChainReader interface {
TxsEvents(ctx context.Context, events []string, paginationParams *query.PageRequest) (*txtypes.GetTxsEventResponse, error)
ContractState(ctx context.Context, contractAddress sdk.AccAddress, queryMsg []byte) ([]byte, error)
}
// NewChainReader produces a ChainReader that issues requests to the Cosmos RPC
// in sequence, even if it's called by multiple sources in parallel.
// That's because the Cosmos endpoint is aggresively rate limitting the monitor.
func NewChainReader(cosmosConfig CosmosConfig, coreLog logger.Logger) ChainReader {
return &chainReader{
cosmosConfig,
coreLog,
sync.Mutex{},
ratelimit.New(
cosmosConfig.TendermintReqsPerSec,
ratelimit.Per(1*time.Second),
ratelimit.WithoutSlack, // don't accumulate previously "unspent" requests for future bursts
),
}
}
type chainReader struct {
cosmosConfig CosmosConfig
coreLog logger.Logger
globalSequencer sync.Mutex
rateLimiter ratelimit.Limiter
}
func (c *chainReader) TxsEvents(ctx context.Context, events []string, paginationParams *query.PageRequest) (*txtypes.GetTxsEventResponse, error) {
c.globalSequencer.Lock()
defer c.globalSequencer.Unlock()
client, err := pkgClient.NewClient(
c.cosmosConfig.ChainID,
c.cosmosConfig.TendermintURL,
c.cosmosConfig.ReadTimeout,
c.coreLog,
)
if err != nil {
return nil, fmt.Errorf("failed to create a cosmos client: %w", err)
}
_ = c.rateLimiter.Take()
return client.TxsEvents(ctx, events, paginationParams)
}
func (c *chainReader) ContractState(ctx context.Context, contractAddress sdk.AccAddress, queryMsg []byte) ([]byte, error) {
c.globalSequencer.Lock()
defer c.globalSequencer.Unlock()
client, err := pkgClient.NewClient(
c.cosmosConfig.ChainID,
c.cosmosConfig.TendermintURL,
c.cosmosConfig.ReadTimeout,
c.coreLog,
)
if err != nil {
return nil, fmt.Errorf("failed to create a cosmos client: %w", err)
}
_ = c.rateLimiter.Take()
return client.ContractState(ctx, contractAddress, queryMsg)
}