-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsubscription.go
425 lines (370 loc) · 12.8 KB
/
subscription.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package services
import (
"encoding/json"
"errors"
"fmt"
"math/big"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/smartcontractkit/chainlink/logger"
"github.com/smartcontractkit/chainlink/store"
"github.com/smartcontractkit/chainlink/store/models"
"github.com/smartcontractkit/chainlink/store/presenters"
"github.com/smartcontractkit/chainlink/utils"
"go.uber.org/multierr"
)
// Descriptive indices of a RunLog's Topic array
const (
RunLogTopicSignature = iota
RunLogTopicInternalID
RunLogTopicJobID
RunLogTopicAmount
)
// RunLogTopic is the signature for the RunRequest(...) event
// which Chainlink RunLog initiators watch for.
// See https://github.com/smartcontractkit/chainlink/blob/master/solidity/contracts/Oracle.sol
// If updating this, be sure to update the truffle suite's "expected event signature" test.
var RunLogTopic = common.HexToHash("0x3fab86a1207bdcfe3976d0d9df25f263d45ae8d381a60960559771a2b223974d")
// Unsubscriber is the interface for all subscriptions, allowing one to unsubscribe.
type Unsubscriber interface {
Unsubscribe()
}
// JobSubscription listens to event logs being pushed from the Ethereum Node to a job.
type JobSubscription struct {
Job models.JobSpec
unsubscribers []Unsubscriber
}
// StartJobSubscription is the constructor of JobSubscription that to starts
// listening to and keeps track of event logs corresponding to a job.
func StartJobSubscription(job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (JobSubscription, error) {
var merr error
var initSubs []Unsubscriber
for _, initr := range job.InitiatorsFor(models.InitiatorEthLog) {
sub, err := StartEthLogSubscription(initr, job, head, store)
merr = multierr.Append(merr, err)
if err == nil {
initSubs = append(initSubs, sub)
}
}
for _, initr := range job.InitiatorsFor(models.InitiatorRunLog) {
sub, err := StartRunLogSubscription(initr, job, head, store)
merr = multierr.Append(merr, err)
if err == nil {
initSubs = append(initSubs, sub)
}
}
if len(initSubs) == 0 {
return JobSubscription{}, multierr.Append(merr, errors.New("Job must have a valid log initiator"))
}
js := JobSubscription{Job: job, unsubscribers: initSubs}
return js, merr
}
// Unsubscribe stops the subscription and cleans up associated resources.
func (js JobSubscription) Unsubscribe() {
for _, sub := range js.unsubscribers {
sub.Unsubscribe()
}
}
// NewInitiatorFilterQuery returns a new InitiatorSubscriber with initialized filter.
func NewInitiatorFilterQuery(
initr models.Initiator,
head *models.IndexableBlockNumber,
topics [][]common.Hash,
) ethereum.FilterQuery {
listenFromNumber := head.NextInt()
q := utils.ToFilterQueryFor(listenFromNumber, []common.Address{initr.Address})
q.Topics = topics
return q
}
// InitiatorSubscription encapsulates all functionality needed to wrap an ethereum subscription
// for use with a Chainlink Initiator. Initiator specific functionality is delegated
// to the callback.
type InitiatorSubscription struct {
*ManagedSubscription
Job models.JobSpec
Initiator models.Initiator
store *store.Store
callback func(InitiatorSubscriptionLogEvent)
}
// NewInitiatorSubscription creates a new InitiatorSubscription that feeds received
// logs to the callback func parameter.
func NewInitiatorSubscription(
initr models.Initiator,
job models.JobSpec,
store *store.Store,
filter ethereum.FilterQuery,
callback func(InitiatorSubscriptionLogEvent),
) (InitiatorSubscription, error) {
if !initr.IsLogInitiated() {
return InitiatorSubscription{}, errors.New("Can only create an initiator subscription for log initiators")
}
sub := InitiatorSubscription{
Job: job,
Initiator: initr,
store: store,
callback: callback,
}
managedSub, err := NewManagedSubscription(store, filter, sub.dispatchLog)
if err != nil {
return sub, err
}
sub.ManagedSubscription = managedSub
loggerLogListening(initr, filter.FromBlock)
return sub, nil
}
func (sub InitiatorSubscription) dispatchLog(log types.Log) {
sub.callback(InitiatorSubscriptionLogEvent{
Job: sub.Job,
Initiator: sub.Initiator,
Log: log,
store: sub.store,
})
}
// TopicFiltersForRunLog generates the two variations of RunLog IDs that could
// possibly be entered. There is the ID, hex encoded and the ID zero padded.
func TopicFiltersForRunLog(jobID string) [][]common.Hash {
hexJobID := common.BytesToHash([]byte(jobID))
jobIDZeroPadded := common.BytesToHash(common.RightPadBytes(hexutil.MustDecode("0x"+jobID), utils.EVMWordByteLen))
// RunLogTopic AND (0xHEXJOBID OR 0xJOBID0padded)
return [][]common.Hash{{RunLogTopic}, nil, {hexJobID, jobIDZeroPadded}}
}
// StartRunLogSubscription starts an InitiatorSubscription tailored for use with RunLogs.
func StartRunLogSubscription(initr models.Initiator, job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
filter := NewInitiatorFilterQuery(initr, head, TopicFiltersForRunLog(job.ID))
return NewInitiatorSubscription(initr, job, store, filter, receiveRunLog)
}
// StartEthLogSubscription starts an InitiatorSubscription tailored for use with EthLogs.
func StartEthLogSubscription(initr models.Initiator, job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
filter := NewInitiatorFilterQuery(initr, head, nil)
return NewInitiatorSubscription(initr, job, store, filter, receiveEthLog)
}
func loggerLogListening(initr models.Initiator, blockNumber *big.Int) {
msg := fmt.Sprintf(
"Listening for %v from block %v for address %v for job %v",
initr.Type,
presenters.FriendlyBigInt(blockNumber),
presenters.LogListeningAddress(initr.Address),
initr.JobID)
logger.Infow(msg)
}
// Parse the log and run the job specific to this initiator log event.
func receiveRunLog(le InitiatorSubscriptionLogEvent) {
if !le.ValidateRunLog() {
return
}
le.ToDebug()
data, err := le.RunLogJSON()
if err != nil {
logger.Errorw(err.Error(), le.ForLogger()...)
return
}
runJob(le, data, le.Initiator)
}
// Parse the log and run the job specific to this initiator log event.
func receiveEthLog(le InitiatorSubscriptionLogEvent) {
le.ToDebug()
data, err := le.EthLogJSON()
if err != nil {
logger.Errorw(err.Error(), le.ForLogger()...)
return
}
runJob(le, data, le.Initiator)
}
func runJob(le InitiatorSubscriptionLogEvent, data models.JSON, initr models.Initiator) {
payment, err := le.ContractPayment()
if err != nil {
logger.Errorw(err.Error(), le.ForLogger()...)
return
}
input := models.RunResult{
Data: data,
Amount: payment,
}
if _, err := BeginRunAtBlock(le.Job, initr, input, le.store, le.ToIndexableBlockNumber()); err != nil {
logger.Errorw(err.Error(), le.ForLogger()...)
}
}
// ManagedSubscription encapsulates the connecting, backfilling, and clean up of an
// ethereum node subscription.
type ManagedSubscription struct {
store *store.Store
logs chan types.Log
errors chan error
ethSubscription models.EthSubscription
callback func(types.Log)
}
// NewManagedSubscription subscribes to the ethereum node with the passed filter
// and delegates incoming logs to callback.
func NewManagedSubscription(
store *store.Store,
filter ethereum.FilterQuery,
callback func(types.Log),
) (*ManagedSubscription, error) {
logs := make(chan types.Log)
es, err := store.TxManager.SubscribeToLogs(logs, filter)
if err != nil {
return nil, err
}
sub := &ManagedSubscription{
store: store,
callback: callback,
logs: logs,
ethSubscription: es,
errors: make(chan error),
}
go sub.listenToSubscriptionErrors()
go sub.listenToLogs(filter)
return sub, nil
}
// Unsubscribe closes channels and cleans up resources.
func (sub ManagedSubscription) Unsubscribe() {
if sub.ethSubscription != nil {
sub.ethSubscription.Unsubscribe()
}
close(sub.logs)
close(sub.errors)
}
func (sub ManagedSubscription) listenToSubscriptionErrors() {
for err := range sub.errors {
logger.Errorw(fmt.Sprintf("Error in log subscription: %s", err.Error()), "err", err)
}
}
func (sub ManagedSubscription) listenToLogs(q ethereum.FilterQuery) {
backfilledSet := sub.backfillLogs(q)
for log := range sub.logs {
if _, present := backfilledSet[log.BlockHash.String()]; !present {
sub.callback(log)
}
}
}
func (sub ManagedSubscription) backfillLogs(q ethereum.FilterQuery) map[string]bool {
backfilledSet := map[string]bool{}
if q.FromBlock.Cmp(big.NewInt(0)) <= 0 {
return backfilledSet
}
logs, err := sub.store.TxManager.GetLogs(q)
if err != nil {
logger.Errorw("Unable to backfill logs", "err", err)
return backfilledSet
}
for _, log := range logs {
backfilledSet[log.BlockHash.String()] = true
sub.callback(log)
}
return backfilledSet
}
// InitiatorSubscriptionLogEvent encapsulates all information as a result of a received log from an
// InitiatorSubscription.
type InitiatorSubscriptionLogEvent struct {
Log types.Log
Job models.JobSpec
Initiator models.Initiator
store *store.Store
}
// ForLogger formats the InitiatorSubscriptionLogEvent for easy common formatting in logs (trace statements, not ethereum events).
func (le InitiatorSubscriptionLogEvent) ForLogger(kvs ...interface{}) []interface{} {
output := []interface{}{
"job", le.Job,
"log", le.Log,
"initiator", le.Initiator,
}
return append(kvs, output...)
}
// ToDebug prints this event via logger.Debug.
func (le InitiatorSubscriptionLogEvent) ToDebug() {
friendlyAddress := presenters.LogListeningAddress(le.Initiator.Address)
msg := fmt.Sprintf("Received log from block #%v for address %v for job %v", le.Log.BlockNumber, friendlyAddress, le.Job.ID)
logger.Debugw(msg, le.ForLogger()...)
}
// ToIndexableBlockNumber returns an IndexableBlockNumber for the given InitiatorSubscriptionLogEvent Block
func (le InitiatorSubscriptionLogEvent) ToIndexableBlockNumber() *models.IndexableBlockNumber {
num := new(big.Int)
num.SetUint64(le.Log.BlockNumber)
return models.NewIndexableBlockNumber(num, le.Log.BlockHash)
}
// ValidateRunLog returns whether or not the contained log is a RunLog,
// a specific Chainlink event trigger from smart contracts.
func (le InitiatorSubscriptionLogEvent) ValidateRunLog() bool {
el := le.Log
if !isRunLog(el) {
logger.Errorw("Skipping; Unable to retrieve runlog parameters from log", le.ForLogger()...)
return false
}
jid, err := jobIDFromHexEncodedTopic(el)
if err != nil {
logger.Errorw("Failed to retrieve Job ID from log", le.ForLogger("err", err.Error())...)
return false
} else if jid != le.Job.ID && jobIDFromImproperEncodedTopic(el) != le.Job.ID {
logger.Errorw(fmt.Sprintf("Run Log didn't have matching job ID: %v != %v", jid, le.Job.ID), le.ForLogger()...)
return false
}
return true
}
// RunLogJSON extracts data from the log's topics and data specific to the format defined
// by RunLogs.
func (le InitiatorSubscriptionLogEvent) RunLogJSON() (models.JSON, error) {
el := le.Log
js, err := decodeABIToJSON(el.Data)
if err != nil {
return js, err
}
fullfillmentJSON, err := fulfillmentToJSON(le)
if err != nil {
return js, err
}
return js.Merge(fullfillmentJSON)
}
func fulfillmentToJSON(le InitiatorSubscriptionLogEvent) (models.JSON, error) {
el := le.Log
var js models.JSON
js, err := js.Add("address", el.Address.String())
if err != nil {
return js, err
}
js, err = js.Add("dataPrefix", el.Topics[RunLogTopicInternalID].String())
if err != nil {
return js, err
}
return js.Add("functionSelector", OracleFulfillmentFunctionID)
}
// EthLogJSON reformats the log as JSON.
func (le InitiatorSubscriptionLogEvent) EthLogJSON() (models.JSON, error) {
el := le.Log
var out models.JSON
b, err := json.Marshal(el)
if err != nil {
return out, err
}
return out, json.Unmarshal(b, &out)
}
// ContractPayment returns the amount attached to a contract to pay the Oracle upon fulfillment.
func (le InitiatorSubscriptionLogEvent) ContractPayment() (*big.Int, error) {
if !isRunLog(le.Log) {
return nil, nil
}
encodedAmount := le.Log.Topics[RunLogTopicAmount].Hex()
payment, ok := new(big.Int).SetString(encodedAmount, 0)
if !ok {
return payment, fmt.Errorf("unable to decoded amount from RunLog: %s", encodedAmount)
}
return payment, nil
}
func decodeABIToJSON(data hexutil.Bytes) (models.JSON, error) {
versionSize := 32
varLocationSize := 32
varLengthSize := 32
prefix := versionSize + varLocationSize + varLengthSize
hex := []byte(string([]byte(data)[prefix:]))
return models.ParseCBOR(hex)
}
func isRunLog(log types.Log) bool {
return len(log.Topics) == 4 && log.Topics[0] == RunLogTopic
}
func jobIDFromHexEncodedTopic(log types.Log) (string, error) {
return utils.HexToString(log.Topics[RunLogTopicJobID].Hex())
}
func jobIDFromImproperEncodedTopic(log types.Log) string {
return log.Topics[RunLogTopicJobID].String()[2:34]
}