-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
event_provider.go
229 lines (194 loc) · 6.82 KB
/
event_provider.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
package transmit
import (
"context"
"encoding/hex"
"fmt"
"sync"
"github.com/smartcontractkit/chainlink-automation/pkg/v3/types"
"github.com/ethereum/go-ethereum/common"
ocr2keepers "github.com/smartcontractkit/chainlink-common/pkg/types/automation"
"github.com/smartcontractkit/chainlink-common/pkg/services"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
iregistry21 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/i_keeper_registry_master_wrapper_2_1"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/core"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
var _ types.TransmitEventProvider = &EventProvider{}
type logParser func(registry *iregistry21.IKeeperRegistryMaster, log logpoller.Log) (transmitEventLog, error)
type EventProvider struct {
sync services.StateMachine
mu sync.RWMutex
runState int
runError error
logger logger.Logger
logPoller logpoller.LogPoller
registry *iregistry21.IKeeperRegistryMaster
client evmclient.Client
registryAddress common.Address
lookbackBlocks int64
parseLog logParser
cache transmitEventCache
}
func EventProviderFilterName(addr common.Address) string {
return logpoller.FilterName("KeepersRegistry TransmitEventProvider", addr)
}
func NewTransmitEventProvider(
logger logger.Logger,
logPoller logpoller.LogPoller,
registryAddress common.Address,
client evmclient.Client,
lookbackBlocks int64,
) (*EventProvider, error) {
var err error
contract, err := iregistry21.NewIKeeperRegistryMaster(registryAddress, client)
if err != nil {
return nil, err
}
err = logPoller.RegisterFilter(logpoller.Filter{
Name: EventProviderFilterName(contract.Address()),
EventSigs: []common.Hash{
// These are the events that are emitted when a node transmits a report
iregistry21.IKeeperRegistryMasterUpkeepPerformed{}.Topic(), // Happy path: report performed the upkeep
iregistry21.IKeeperRegistryMasterReorgedUpkeepReport{}.Topic(), // Report checkBlockNumber was reorged
iregistry21.IKeeperRegistryMasterInsufficientFundsUpkeepReport{}.Topic(), // Upkeep didn't have sufficient funds when report reached chain, perform was aborted early
// Report was too old when it reached the chain. For conditionals upkeep was already performed on a higher block than checkBlockNum
// for logs upkeep was already performed for the particular log
iregistry21.IKeeperRegistryMasterStaleUpkeepReport{}.Topic(),
},
Addresses: []common.Address{registryAddress},
})
if err != nil {
return nil, err
}
return &EventProvider{
logger: logger,
logPoller: logPoller,
registryAddress: registryAddress,
lookbackBlocks: lookbackBlocks,
registry: contract,
client: client,
parseLog: defaultLogParser,
cache: newTransmitEventCache(lookbackBlocks),
}, nil
}
func (c *EventProvider) Name() string {
return c.logger.Name()
}
func (c *EventProvider) Start(_ context.Context) error {
return c.sync.StartOnce("AutomationTransmitEventProvider", func() error {
c.mu.Lock()
defer c.mu.Unlock()
c.runState = 1
return nil
})
}
func (c *EventProvider) Close() error {
return c.sync.StopOnce("AutomationRegistry", func() error {
c.mu.Lock()
defer c.mu.Unlock()
c.runState = 0
c.runError = nil
return nil
})
}
func (c *EventProvider) Ready() error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.runState == 1 {
return nil
}
return c.sync.Ready()
}
func (c *EventProvider) HealthReport() map[string]error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.runState > 1 {
c.sync.SvcErrBuffer.Append(fmt.Errorf("failed run state: %w", c.runError))
}
return map[string]error{c.Name(): c.sync.Healthy()}
}
func (c *EventProvider) GetLatestEvents(ctx context.Context) ([]ocr2keepers.TransmitEvent, error) {
end, err := c.logPoller.LatestBlock(pg.WithParentCtx(ctx))
if err != nil {
return nil, fmt.Errorf("%w: failed to get latest block from log poller", err)
}
// always check the last lookback number of blocks and rebroadcast
// this allows the plugin to make decisions based on event confirmations
logs, err := c.logPoller.LogsWithSigs(
end.BlockNumber-c.lookbackBlocks,
end.BlockNumber,
[]common.Hash{
iregistry21.IKeeperRegistryMasterUpkeepPerformed{}.Topic(),
iregistry21.IKeeperRegistryMasterStaleUpkeepReport{}.Topic(),
iregistry21.IKeeperRegistryMasterReorgedUpkeepReport{}.Topic(),
iregistry21.IKeeperRegistryMasterInsufficientFundsUpkeepReport{}.Topic(),
},
c.registryAddress,
pg.WithParentCtx(ctx),
)
if err != nil {
return nil, fmt.Errorf("%w: failed to collect logs from log poller", err)
}
return c.processLogs(end.BlockNumber, logs...)
}
// processLogs will parse the unseen logs and return the corresponding transmit events.
func (c *EventProvider) processLogs(latestBlock int64, logs ...logpoller.Log) ([]ocr2keepers.TransmitEvent, error) {
var vals []ocr2keepers.TransmitEvent
for _, log := range logs {
k := c.logKey(log)
transmitEvent, ok := c.cache.get(ocr2keepers.BlockNumber(log.BlockNumber), k)
if !ok {
l, err := c.parseLog(c.registry, log)
if err != nil {
c.logger.Debugw("failed to parse log", "err", err)
continue
}
id := l.Id()
upkeepId := &ocr2keepers.UpkeepIdentifier{}
ok := upkeepId.FromBigInt(id)
if !ok {
return nil, core.ErrInvalidUpkeepID
}
triggerW, err := core.UnpackTrigger(id, l.Trigger())
if err != nil {
return nil, fmt.Errorf("%w: failed to unpack trigger", err)
}
trigger := ocr2keepers.NewTrigger(
ocr2keepers.BlockNumber(triggerW.BlockNum),
triggerW.BlockHash,
)
switch core.GetUpkeepType(*upkeepId) {
case types.LogTrigger:
trigger.LogTriggerExtension = &ocr2keepers.LogTriggerExtension{}
trigger.LogTriggerExtension.TxHash = triggerW.TxHash
trigger.LogTriggerExtension.Index = triggerW.LogIndex
trigger.LogTriggerExtension.BlockHash = triggerW.LogBlockHash
default:
}
workID := core.UpkeepWorkID(*upkeepId, trigger)
transmitEvent = ocr2keepers.TransmitEvent{
Type: l.TransmitEventType(),
TransmitBlock: ocr2keepers.BlockNumber(l.BlockNumber),
TransactionHash: l.TxHash,
WorkID: workID,
UpkeepID: *upkeepId,
CheckBlock: trigger.BlockNumber,
}
c.cache.add(k, transmitEvent)
}
transmitEvent.Confirmations = latestBlock - int64(transmitEvent.TransmitBlock)
vals = append(vals, transmitEvent)
}
return vals, nil
}
func (c *EventProvider) logKey(log logpoller.Log) string {
logExt := ocr2keepers.LogTriggerExtension{
TxHash: log.TxHash,
Index: uint32(log.LogIndex),
BlockHash: log.BlockHash,
}
logId := logExt.LogIdentifier()
return hex.EncodeToString(logId)
}