/
upkeep_executer.go
288 lines (256 loc) · 9.28 KB
/
upkeep_executer.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package keeper
import (
"context"
"fmt"
"math/big"
"sync"
"time"
"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/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils/mailbox"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
)
const (
executionQueueSize = 10
maxUpkeepPerformGas = 5_000_000 // Max perform gas for upkeep is 5M on all chains for v1.x
)
// UpkeepExecuter fulfills Service and HeadTrackable interfaces
var (
_ job.ServiceCtx = (*UpkeepExecuter)(nil)
_ httypes.HeadTrackable = (*UpkeepExecuter)(nil)
)
var (
promCheckUpkeepExecutionTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "keeper_check_upkeep_execution_time",
Help: "Time taken to fully execute the check upkeep logic",
},
[]string{"upkeepID"},
)
)
type UpkeepExecuterConfig interface {
MaxGracePeriod() int64
TurnLookBack() int64
Registry() config.Registry
}
// UpkeepExecuter implements the logic to communicate with KeeperRegistry
type UpkeepExecuter struct {
services.StateMachine
chStop services.StopChan
ethClient evmclient.Client
config UpkeepExecuterConfig
executionQueue chan struct{}
headBroadcaster httypes.HeadBroadcasterRegistry
gasEstimator gas.EvmFeeEstimator
job job.Job
mailbox *mailbox.Mailbox[*evmtypes.Head]
orm ORM
pr pipeline.Runner
logger logger.Logger
wgDone sync.WaitGroup
effectiveKeeperAddress common.Address
}
// NewUpkeepExecuter is the constructor of UpkeepExecuter
func NewUpkeepExecuter(
job job.Job,
orm ORM,
pr pipeline.Runner,
ethClient evmclient.Client,
headBroadcaster httypes.HeadBroadcaster,
gasEstimator gas.EvmFeeEstimator,
logger logger.Logger,
config UpkeepExecuterConfig,
effectiveKeeperAddress common.Address,
) *UpkeepExecuter {
return &UpkeepExecuter{
chStop: make(services.StopChan),
ethClient: ethClient,
executionQueue: make(chan struct{}, executionQueueSize),
headBroadcaster: headBroadcaster,
gasEstimator: gasEstimator,
job: job,
mailbox: mailbox.NewSingle[*evmtypes.Head](),
config: config,
orm: orm,
pr: pr,
effectiveKeeperAddress: effectiveKeeperAddress,
logger: logger.Named("UpkeepExecuter"),
}
}
// Start starts the upkeep executer logic
func (ex *UpkeepExecuter) Start(context.Context) error {
return ex.StartOnce("UpkeepExecuter", func() error {
ex.wgDone.Add(2)
go ex.run()
latestHead, unsubscribeHeads := ex.headBroadcaster.Subscribe(ex)
if latestHead != nil {
ex.mailbox.Deliver(latestHead)
}
go func() {
defer unsubscribeHeads()
defer ex.wgDone.Done()
<-ex.chStop
}()
return nil
})
}
// Close stops and closes upkeep executer
func (ex *UpkeepExecuter) Close() error {
return ex.StopOnce("UpkeepExecuter", func() error {
close(ex.chStop)
ex.wgDone.Wait()
return nil
})
}
// OnNewLongestChain handles the given head of a new longest chain
func (ex *UpkeepExecuter) OnNewLongestChain(_ context.Context, head *evmtypes.Head) {
ex.mailbox.Deliver(head)
}
func (ex *UpkeepExecuter) run() {
defer ex.wgDone.Done()
for {
select {
case <-ex.chStop:
return
case <-ex.mailbox.Notify():
ex.processActiveUpkeeps()
}
}
}
func (ex *UpkeepExecuter) processActiveUpkeeps() {
// Keepers could miss their turn in the turn taking algo if they are too overloaded
// with work because processActiveUpkeeps() blocks
head, exists := ex.mailbox.Retrieve()
if !exists {
ex.logger.Info("no head to retrieve. It might have been skipped")
return
}
ex.logger.Debugw("checking active upkeeps", "blockheight", head.Number)
registry, err := ex.orm.RegistryByContractAddress(ex.job.KeeperSpec.ContractAddress)
if err != nil {
ex.logger.Error(errors.Wrap(err, "unable to load registry"))
return
}
var activeUpkeeps []UpkeepRegistration
turnBinary, err2 := ex.turnBlockHashBinary(registry, head, ex.config.TurnLookBack())
if err2 != nil {
ex.logger.Error(errors.Wrap(err2, "unable to get turn block number hash"))
return
}
activeUpkeeps, err2 = ex.orm.EligibleUpkeepsForRegistry(
ex.job.KeeperSpec.ContractAddress,
head.Number,
ex.config.MaxGracePeriod(),
turnBinary)
if err2 != nil {
ex.logger.Error(errors.Wrap(err2, "unable to load active registrations"))
return
}
if head.Number%10 == 0 {
// Log this once every 10 blocks
fetchedUpkeepIDs := make([]string, len(activeUpkeeps))
for i, activeUpkeep := range activeUpkeeps {
fetchedUpkeepIDs[i] = NewUpkeepIdentifier(activeUpkeep.UpkeepID).String()
}
ex.logger.Debugw("Fetched list of active upkeeps", "blockNum", head.Number, "active upkeeps list", fetchedUpkeepIDs)
}
wg := sync.WaitGroup{}
wg.Add(len(activeUpkeeps))
done := func() {
<-ex.executionQueue
wg.Done()
}
for _, reg := range activeUpkeeps {
ex.executionQueue <- struct{}{}
go ex.execute(reg, head, done)
}
wg.Wait()
ex.logger.Debugw("Finished checking upkeeps", "blockNum", head.Number)
}
// execute triggers the pipeline run
func (ex *UpkeepExecuter) execute(upkeep UpkeepRegistration, head *evmtypes.Head, done func()) {
defer done()
start := time.Now()
svcLogger := ex.logger.With("jobID", ex.job.ID, "blockNum", head.Number, "upkeepID", upkeep.UpkeepID)
svcLogger.Debugw("checking upkeep", "lastRunBlockHeight", upkeep.LastRunBlockHeight, "lastKeeperIndex", upkeep.LastKeeperIndex)
ctxService, cancel := ex.chStop.CtxCancel(context.WithTimeout(context.Background(), time.Minute))
defer cancel()
evmChainID := ""
if ex.job.KeeperSpec.EVMChainID != nil {
evmChainID = ex.job.KeeperSpec.EVMChainID.String()
}
var gasPrice, gasTipCap, gasFeeCap *assets.Wei
// effectiveKeeperAddress is always fromAddress when forwarding is not enabled.
// when forwarding is enabled, effectiveKeeperAddress is on-chain forwarder.
vars := pipeline.NewVarsFrom(buildJobSpec(ex.job, ex.effectiveKeeperAddress, upkeep, ex.config.Registry(), gasPrice, gasTipCap, gasFeeCap, evmChainID))
// DotDagSource in database is empty because all the Keeper pipeline runs make use of the same observation source
ex.job.PipelineSpec.DotDagSource = pipeline.KeepersObservationSource
run := pipeline.NewRun(*ex.job.PipelineSpec, vars)
if _, err := ex.pr.Run(ctxService, run, svcLogger, true, nil); err != nil {
svcLogger.Error(errors.Wrap(err, "failed executing run"))
return
}
// Only after task runs where a tx was broadcast
if run.State == pipeline.RunStatusCompleted {
rowsAffected, err := ex.orm.SetLastRunInfoForUpkeepOnJob(ex.job.ID, upkeep.UpkeepID, head.Number, upkeep.Registry.FromAddress, pg.WithParentCtx(ctxService))
if err != nil {
svcLogger.Error(errors.Wrap(err, "failed to set last run height for upkeep"))
}
svcLogger.Debugw("execute pipeline status completed", "fromAddr", upkeep.Registry.FromAddress, "rowsAffected", rowsAffected)
elapsed := time.Since(start)
promCheckUpkeepExecutionTime.
WithLabelValues(upkeep.PrettyID()).
Set(float64(elapsed))
}
}
func (ex *UpkeepExecuter) turnBlockHashBinary(registry Registry, head *evmtypes.Head, lookback int64) (string, error) {
turnBlock := head.Number - (head.Number % int64(registry.BlockCountPerTurn)) - lookback
block, err := ex.ethClient.HeadByNumber(context.Background(), big.NewInt(turnBlock))
if err != nil {
return "", err
}
hashAtHeight := block.Hash
binaryString := fmt.Sprintf("%b", hashAtHeight.Big())
return binaryString, nil
}
func buildJobSpec(
jb job.Job,
effectiveKeeperAddress common.Address,
upkeep UpkeepRegistration,
ormConfig RegistryGasChecker,
gasPrice *assets.Wei,
gasTipCap *assets.Wei,
gasFeeCap *assets.Wei,
chainID string,
) map[string]interface{} {
return map[string]interface{}{
"jobSpec": map[string]interface{}{
"jobID": jb.ID,
"fromAddress": upkeep.Registry.FromAddress.String(),
"effectiveKeeperAddress": effectiveKeeperAddress.String(),
"contractAddress": upkeep.Registry.ContractAddress.String(),
"upkeepID": upkeep.UpkeepID.String(),
"prettyID": upkeep.PrettyID(),
"pipelineSpec": &pipeline.Spec{
ForwardingAllowed: jb.ForwardingAllowed,
},
"performUpkeepGasLimit": maxUpkeepPerformGas + ormConfig.PerformGasOverhead(),
"maxPerformDataSize": ormConfig.MaxPerformDataSize(),
"gasPrice": gasPrice.ToInt(),
"gasTipCap": gasTipCap.ToInt(),
"gasFeeCap": gasFeeCap.ToInt(),
"evmChainID": chainID,
},
}
}