-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
report_codec.go
100 lines (85 loc) · 3.2 KB
/
report_codec.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
package reportcodec
import (
"errors"
"fmt"
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
pkgerrors "github.com/pkg/errors"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
v1 "github.com/smartcontractkit/chainlink-common/pkg/types/mercury/v1"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/utils"
reporttypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/v1/types"
)
// NOTE:
// This report codec is based on the original median evmreportcodec
// here:
// https://github.com/smartcontractkit/offchain-reporting/blob/master/lib/offchainreporting2/reportingplugin/median/evmreportcodec/reportcodec.go
var ReportTypes = reporttypes.GetSchema()
var maxReportLength = 32 * len(ReportTypes) // each arg is 256 bit EVM word
var _ v1.ReportCodec = &ReportCodec{}
type ReportCodec struct {
logger logger.Logger
feedID utils.FeedID
}
func NewReportCodec(feedID [32]byte, lggr logger.Logger) *ReportCodec {
return &ReportCodec{lggr, feedID}
}
func (r *ReportCodec) BuildReport(rf v1.ReportFields) (ocrtypes.Report, error) {
var merr error
if rf.BenchmarkPrice == nil {
merr = errors.Join(merr, errors.New("benchmarkPrice may not be nil"))
}
if rf.Bid == nil {
merr = errors.Join(merr, errors.New("bid may not be nil"))
}
if rf.Ask == nil {
merr = errors.Join(merr, errors.New("ask may not be nil"))
}
if len(rf.CurrentBlockHash) != 32 {
merr = errors.Join(merr, fmt.Errorf("invalid length for currentBlockHash, expected: 32, got: %d", len(rf.CurrentBlockHash)))
}
if merr != nil {
return nil, merr
}
var currentBlockHash common.Hash
copy(currentBlockHash[:], rf.CurrentBlockHash)
reportBytes, err := ReportTypes.Pack(r.feedID, rf.Timestamp, rf.BenchmarkPrice, rf.Bid, rf.Ask, uint64(rf.CurrentBlockNum), currentBlockHash, uint64(rf.ValidFromBlockNum), rf.CurrentBlockTimestamp)
return ocrtypes.Report(reportBytes), pkgerrors.Wrap(err, "failed to pack report blob")
}
// Maximum length in bytes of Report returned by BuildReport. Used for
// defending against spam attacks.
func (r *ReportCodec) MaxReportLength(n int) (int, error) {
return maxReportLength, nil
}
func (r *ReportCodec) CurrentBlockNumFromReport(report ocrtypes.Report) (int64, error) {
decoded, err := r.Decode(report)
if err != nil {
return 0, err
}
if decoded.CurrentBlockNum > math.MaxInt64 {
return 0, fmt.Errorf("CurrentBlockNum=%d overflows max int64", decoded.CurrentBlockNum)
}
return int64(decoded.CurrentBlockNum), nil
}
func (r *ReportCodec) ValidFromBlockNumFromReport(report ocrtypes.Report) (int64, error) {
decoded, err := r.Decode(report)
if err != nil {
return 0, err
}
if decoded.ValidFromBlockNum > math.MaxInt64 {
return 0, fmt.Errorf("ValidFromBlockNum=%d overflows max int64", decoded.ValidFromBlockNum)
}
return int64(decoded.ValidFromBlockNum), nil
}
func (r *ReportCodec) Decode(report ocrtypes.Report) (*reporttypes.Report, error) {
return reporttypes.Decode(report)
}
func (r *ReportCodec) BenchmarkPriceFromReport(report ocrtypes.Report) (*big.Int, error) {
decoded, err := r.Decode(report)
if err != nil {
return nil, err
}
return decoded.BenchmarkPrice, nil
}