-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config_poller.go
232 lines (199 loc) · 8.45 KB
/
config_poller.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
package evm
import (
"context"
"database/sql"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/libocr/gethwrappers2/ocr2aggregator"
"github.com/smartcontractkit/libocr/gethwrappers2/ocrconfigurationstoreevmsimple"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/logger"
evmRelayTypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
)
var (
failedRPCContractCalls = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "ocr2_failed_rpc_contract_calls",
Help: "Running count of failed RPC contract calls by chain/contract",
},
[]string{"chainID", "contractAddress"},
)
)
type LogDecoder interface {
EventSig() common.Hash
Decode(rawLog []byte) (ocrtypes.ContractConfig, error)
}
type configPoller struct {
services.StateMachine
lggr logger.Logger
filterName string
destChainLogPoller logpoller.LogPoller
client client.Client
aggregatorContractAddr common.Address
aggregatorContract *ocr2aggregator.OCR2Aggregator
// Some chains "manage" state bloat by deleting older logs. The ConfigStore
// contract allows us work around such restrictions.
configStoreContractAddr *common.Address
configStoreContract *ocrconfigurationstoreevmsimple.OCRConfigurationStoreEVMSimple
// Depending on the exact contract used, the raw config log may be shaped
// in different ways
ld LogDecoder
}
func configPollerFilterName(addr common.Address) string {
return logpoller.FilterName("OCR2ConfigPoller", addr.String())
}
type CPConfig struct {
Client client.Client
DestinationChainPoller logpoller.LogPoller
AggregatorContractAddress common.Address
ConfigStoreAddress *common.Address
LogDecoder LogDecoder
}
func NewConfigPoller(ctx context.Context, lggr logger.Logger, cfg CPConfig) (evmRelayTypes.ConfigPoller, error) {
return newConfigPoller(ctx, lggr, cfg.Client, cfg.DestinationChainPoller, cfg.AggregatorContractAddress, cfg.ConfigStoreAddress, cfg.LogDecoder)
}
func newConfigPoller(ctx context.Context, lggr logger.Logger, client client.Client, destChainPoller logpoller.LogPoller, aggregatorContractAddr common.Address, configStoreAddr *common.Address, ld LogDecoder) (*configPoller, error) {
err := destChainPoller.RegisterFilter(ctx, logpoller.Filter{Name: configPollerFilterName(aggregatorContractAddr), EventSigs: []common.Hash{ld.EventSig()}, Addresses: []common.Address{aggregatorContractAddr}})
if err != nil {
return nil, err
}
aggregatorContract, err := ocr2aggregator.NewOCR2Aggregator(aggregatorContractAddr, client)
if err != nil {
return nil, err
}
cp := &configPoller{
lggr: lggr,
filterName: configPollerFilterName(aggregatorContractAddr),
destChainLogPoller: destChainPoller,
aggregatorContractAddr: aggregatorContractAddr,
client: client,
aggregatorContract: aggregatorContract,
ld: ld,
}
if configStoreAddr != nil {
cp.configStoreContractAddr = configStoreAddr
cp.configStoreContract, err = ocrconfigurationstoreevmsimple.NewOCRConfigurationStoreEVMSimple(*configStoreAddr, client)
if err != nil {
return nil, err
}
}
return cp, nil
}
func (cp *configPoller) Start() {}
func (cp *configPoller) Close() error {
return nil
}
// Notify noop method
func (cp *configPoller) Notify() <-chan struct{} {
return nil
}
// Replay abstracts the logpoller.LogPoller Replay() implementation
func (cp *configPoller) Replay(ctx context.Context, fromBlock int64) error {
return cp.destChainLogPoller.Replay(ctx, fromBlock)
}
// LatestConfigDetails returns the latest config details from the logs
func (cp *configPoller) LatestConfigDetails(ctx context.Context) (changedInBlock uint64, configDigest ocrtypes.ConfigDigest, err error) {
latest, err := cp.destChainLogPoller.LatestLogByEventSigWithConfs(ctx, cp.ld.EventSig(), cp.aggregatorContractAddr, 1)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
if cp.isConfigStoreAvailable() {
// Fallback to RPC call in case logs have been pruned and configStoreContract is available
return cp.callLatestConfigDetails(ctx)
}
// log not found means return zero config digest
return 0, ocrtypes.ConfigDigest{}, nil
}
return 0, ocrtypes.ConfigDigest{}, err
}
latestConfigSet, err := cp.ld.Decode(latest.Data)
if err != nil {
return 0, ocrtypes.ConfigDigest{}, err
}
return uint64(latest.BlockNumber), latestConfigSet.ConfigDigest, nil
}
// LatestConfig returns the latest config from the logs on a certain block
func (cp *configPoller) LatestConfig(ctx context.Context, changedInBlock uint64) (ocrtypes.ContractConfig, error) {
lgs, err := cp.destChainLogPoller.Logs(ctx, int64(changedInBlock), int64(changedInBlock), cp.ld.EventSig(), cp.aggregatorContractAddr)
if err != nil {
return ocrtypes.ContractConfig{}, err
}
if len(lgs) == 0 {
if cp.isConfigStoreAvailable() {
// Fallback to RPC call in case logs have been pruned
return cp.callReadConfigFromStore(ctx)
}
return ocrtypes.ContractConfig{}, fmt.Errorf("no logs found for config on contract %s (chain %s) at block %d", cp.aggregatorContractAddr.Hex(), cp.client.ConfiguredChainID().String(), changedInBlock)
}
latestConfigSet, err := cp.ld.Decode(lgs[len(lgs)-1].Data)
if err != nil {
return ocrtypes.ContractConfig{}, err
}
cp.lggr.Infow("LatestConfig", "latestConfig", latestConfigSet)
return latestConfigSet, nil
}
// LatestBlockHeight returns the latest block height from the logs
func (cp *configPoller) LatestBlockHeight(ctx context.Context) (blockHeight uint64, err error) {
latest, err := cp.destChainLogPoller.LatestBlock(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return 0, nil
}
return 0, err
}
return uint64(latest.BlockNumber), nil
}
func (cp *configPoller) isConfigStoreAvailable() bool {
return cp.configStoreContract != nil
}
// RPC call for latest config details
func (cp *configPoller) callLatestConfigDetails(ctx context.Context) (changedInBlock uint64, configDigest ocrtypes.ConfigDigest, err error) {
details, err := cp.aggregatorContract.LatestConfigDetails(&bind.CallOpts{
Context: ctx,
})
if err != nil {
failedRPCContractCalls.WithLabelValues(cp.client.ConfiguredChainID().String(), cp.aggregatorContractAddr.Hex()).Inc()
}
return uint64(details.BlockNumber), details.ConfigDigest, err
}
// RPC call to read config from config store contract
func (cp *configPoller) callReadConfigFromStore(ctx context.Context) (cfg ocrtypes.ContractConfig, err error) {
_, configDigest, err := cp.LatestConfigDetails(ctx)
if err != nil {
failedRPCContractCalls.WithLabelValues(cp.client.ConfiguredChainID().String(), cp.aggregatorContractAddr.Hex()).Inc()
return cfg, fmt.Errorf("failed to get latest config details: %w", err)
}
if configDigest == (ocrtypes.ConfigDigest{}) {
return cfg, fmt.Errorf("config details missing while trying to lookup config in store; no logs found for contract %s (chain %s)", cp.aggregatorContractAddr.Hex(), cp.client.ConfiguredChainID().String())
}
storedConfig, err := cp.configStoreContract.ReadConfig(&bind.CallOpts{
Context: ctx,
}, configDigest)
if err != nil {
failedRPCContractCalls.WithLabelValues(cp.client.ConfiguredChainID().String(), cp.configStoreContractAddr.Hex()).Inc()
return cfg, fmt.Errorf("failed to read config from config store contract: %w", err)
}
signers := make([]ocrtypes.OnchainPublicKey, len(storedConfig.Signers))
for i := range signers {
signers[i] = storedConfig.Signers[i].Bytes()
}
transmitters := make([]ocrtypes.Account, len(storedConfig.Transmitters))
for i := range transmitters {
transmitters[i] = ocrtypes.Account(storedConfig.Transmitters[i].Hex())
}
return ocrtypes.ContractConfig{
ConfigDigest: configDigest,
ConfigCount: uint64(storedConfig.ConfigCount),
Signers: signers,
Transmitters: transmitters,
F: storedConfig.F,
OnchainConfig: storedConfig.OnchainConfig,
OffchainConfigVersion: storedConfig.OffchainConfigVersion,
OffchainConfig: storedConfig.OffchainConfig,
}, err
}