-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
encoder.go
126 lines (105 loc) · 3.46 KB
/
encoder.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
package encoding
import (
"fmt"
"math/big"
ocr2keepers "github.com/smartcontractkit/ocr2keepers/pkg/v3/types"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/automation_utils_2_1"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evm21/core"
)
var (
ErrEmptyResults = fmt.Errorf("empty results; cannot encode")
)
type reportEncoder struct {
packer Packer
}
var _ ocr2keepers.Encoder = (*reportEncoder)(nil)
func NewReportEncoder(p Packer) ocr2keepers.Encoder {
return &reportEncoder{
packer: p,
}
}
func (e reportEncoder) Encode(results ...ocr2keepers.CheckResult) ([]byte, error) {
if len(results) == 0 {
return nil, ErrEmptyResults
}
report := automation_utils_2_1.KeeperRegistryBase21Report{
FastGasWei: big.NewInt(0),
LinkNative: big.NewInt(0),
UpkeepIds: make([]*big.Int, len(results)),
GasLimits: make([]*big.Int, len(results)),
Triggers: make([][]byte, len(results)),
PerformDatas: make([][]byte, len(results)),
}
encoded := 0
highestCheckBlock := big.NewInt(0)
for i, result := range results {
checkBlock := big.NewInt(int64(result.Trigger.BlockNumber))
if checkBlock.Cmp(highestCheckBlock) == 1 {
highestCheckBlock = checkBlock
if result.FastGasWei != nil {
report.FastGasWei = result.FastGasWei
}
if result.LinkNative != nil {
report.LinkNative = result.LinkNative
}
}
id := result.UpkeepID.BigInt()
report.UpkeepIds[i] = id
report.GasLimits[i] = big.NewInt(0).SetUint64(result.GasAllocated)
triggerW := triggerWrapper{
BlockNum: uint32(result.Trigger.BlockNumber),
BlockHash: result.Trigger.BlockHash,
}
switch core.GetUpkeepType(result.UpkeepID) {
case ocr2keepers.LogTrigger:
triggerW.TxHash = result.Trigger.LogTriggerExtension.TxHash
triggerW.LogIndex = result.Trigger.LogTriggerExtension.Index
triggerW.LogBlockHash = result.Trigger.LogTriggerExtension.BlockHash
default:
// no special handling here for conditional triggers
}
trigger, err := core.PackTrigger(id, triggerW)
if err != nil {
return nil, fmt.Errorf("%w: failed to pack trigger", err)
}
report.Triggers[i] = trigger
report.PerformDatas[i] = result.PerformData
encoded++
}
return e.packer.PackReport(report)
}
// Extract the plugin will call this function to accept/transmit reports
func (e reportEncoder) Extract(raw []byte) ([]ocr2keepers.ReportedUpkeep, error) {
report, err := e.packer.UnpackReport(raw)
if err != nil {
return nil, fmt.Errorf("%w: failed to unpack report", err)
}
reportedUpkeeps := make([]ocr2keepers.ReportedUpkeep, len(report.UpkeepIds))
for i, upkeepId := range report.UpkeepIds {
triggerW, err := core.UnpackTrigger(upkeepId, report.Triggers[i])
if err != nil {
return nil, fmt.Errorf("%w: failed to unpack trigger", err)
}
id := &ocr2keepers.UpkeepIdentifier{}
id.FromBigInt(upkeepId)
trigger := ocr2keepers.NewTrigger(
ocr2keepers.BlockNumber(triggerW.BlockNum),
triggerW.BlockHash,
)
switch core.GetUpkeepType(*id) {
case ocr2keepers.LogTrigger:
trigger.LogTriggerExtension = &ocr2keepers.LogTriggerExtension{}
trigger.LogTriggerExtension.TxHash = triggerW.TxHash
trigger.LogTriggerExtension.Index = triggerW.LogIndex
trigger.LogTriggerExtension.BlockHash = triggerW.LogBlockHash
default:
}
workID := core.UpkeepWorkID(*id, trigger)
reportedUpkeeps[i] = ocr2keepers.ReportedUpkeep{
WorkID: workID,
UpkeepID: *id,
Trigger: trigger,
}
}
return reportedUpkeeps, nil
}