Skip to content

Commit

Permalink
ocr2: add context
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Jul 30, 2024
1 parent ae747ca commit 6ce1cf0
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evmreportcodec

import (
"context"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -33,7 +34,7 @@ var _ median.ReportCodec = ReportCodec{}

type ReportCodec struct{}

func (ReportCodec) BuildReport(paos []median.ParsedAttributedObservation) (types.Report, error) {
func (ReportCodec) BuildReport(ctx context.Context, paos []median.ParsedAttributedObservation) (types.Report, error) {
if len(paos) == 0 {
return nil, fmt.Errorf("cannot build report from empty attributed observations")
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func (ReportCodec) BuildReport(paos []median.ParsedAttributedObservation) (types
return types.Report(reportBytes), err
}

func (ReportCodec) MedianFromReport(report types.Report) (*big.Int, error) {
func (ReportCodec) MedianFromReport(ctx context.Context, report types.Report) (*big.Int, error) {
reportElems := map[string]interface{}{}
if err := reportTypes.UnpackIntoMap(reportElems, report); err != nil {
return nil, fmt.Errorf("error during unpack: %w", err)
Expand Down Expand Up @@ -98,7 +99,7 @@ func (ReportCodec) MedianFromReport(report types.Report) (*big.Int, error) {
return median, nil
}

func (ReportCodec) MaxReportLength(n int) (int, error) {
func (ReportCodec) MaxReportLength(ctx context.Context, n int) (int, error) {
return 32 /* timestamp */ + 32 /* rawObservers */ + (2*32 + n*32) /*observations*/ + 32 /* juelsPerFeeCoin */, nil
}

Expand Down
24 changes: 12 additions & 12 deletions offchainreporting2/reportingplugin/median/median.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type OnchainConfig struct {
}

type OnchainConfigCodec interface {
Encode(OnchainConfig) ([]byte, error)
Decode([]byte) (OnchainConfig, error)
Encode(context.Context, OnchainConfig) ([]byte, error)
Decode(context.Context, []byte) (OnchainConfig, error)
}

var _ OnchainConfigCodec = StandardOnchainConfigCodec{}
Expand All @@ -41,7 +41,7 @@ var _ OnchainConfigCodec = StandardOnchainConfigCodec{}
// returned by EncodeValue.
type StandardOnchainConfigCodec struct{}

func (StandardOnchainConfigCodec) Decode(b []byte) (OnchainConfig, error) {
func (StandardOnchainConfigCodec) Decode(ctx context.Context, b []byte) (OnchainConfig, error) {
if len(b) != onchainConfigEncodedLength {
return OnchainConfig{}, fmt.Errorf("unexpected length of OnchainConfig, expected %v, got %v", onchainConfigEncodedLength, len(b))
}
Expand All @@ -66,7 +66,7 @@ func (StandardOnchainConfigCodec) Decode(b []byte) (OnchainConfig, error) {
return OnchainConfig{min, max}, nil
}

func (StandardOnchainConfigCodec) Encode(c OnchainConfig) ([]byte, error) {
func (StandardOnchainConfigCodec) Encode(ctx context.Context, c OnchainConfig) ([]byte, error) {
minBytes, err := EncodeValue(c.Min)
if err != nil {
return nil, err
Expand Down Expand Up @@ -210,17 +210,17 @@ type ReportCodec interface {
// ParsedAttributedObservation per observer, and that all observers are
// valid. However, observation values, timestamps, etc... should all be
// treated as untrusted.
BuildReport([]ParsedAttributedObservation) (types.Report, error)
BuildReport(context.Context, []ParsedAttributedObservation) (types.Report, error)

// Gets the "median" (the n//2-th ranked element to be more precise where n
// is the length of the list) observation from the report. The input to this
// function should be an output of BuildReport in the benign case.
// Nevertheless, make sure to treat the input to this function as untrusted.
MedianFromReport(types.Report) (*big.Int, error)
MedianFromReport(context.Context, types.Report) (*big.Int, error)

// Returns the maximum length of a report based on n, the number of oracles.
// The output of BuildReport must respect this maximum length.
MaxReportLength(n int) (int, error)
MaxReportLength(ctx context.Context, n int) (int, error)
}

var _ types.ReportingPluginFactory = NumericalMedianFactory{}
Expand Down Expand Up @@ -253,14 +253,14 @@ type NumericalMedianFactory struct {
ReportCodec ReportCodec
}

func (fac NumericalMedianFactory) NewReportingPlugin(configuration types.ReportingPluginConfig) (types.ReportingPlugin, types.ReportingPluginInfo, error) {
func (fac NumericalMedianFactory) NewReportingPlugin(ctx context.Context, configuration types.ReportingPluginConfig) (types.ReportingPlugin, types.ReportingPluginInfo, error) {

offchainConfig, err := DecodeOffchainConfig(configuration.OffchainConfig)
if err != nil {
return nil, types.ReportingPluginInfo{}, err
}

onchainConfig, err := fac.OnchainConfigCodec.Decode(configuration.OnchainConfig)
onchainConfig, err := fac.OnchainConfigCodec.Decode(ctx, configuration.OnchainConfig)
if err != nil {
return nil, types.ReportingPluginInfo{}, err
}
Expand All @@ -270,7 +270,7 @@ func (fac NumericalMedianFactory) NewReportingPlugin(configuration types.Reporti
"reportingPlugin": "NumericalMedian",
})

maxReportLength, err := fac.ReportCodec.MaxReportLength(configuration.N)
maxReportLength, err := fac.ReportCodec.MaxReportLength(ctx, configuration.N)
if err != nil {
return nil, types.ReportingPluginInfo{}, err
}
Expand Down Expand Up @@ -482,7 +482,7 @@ func (nm *numericalMedian) Report(ctx context.Context, repts types.ReportTimesta
if !should {
return false, nil, nil
}
report, err := nm.reportCodec.BuildReport(paos)
report, err := nm.reportCodec.BuildReport(ctx, paos)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -659,7 +659,7 @@ func (nm *numericalMedian) ShouldAcceptFinalizedReport(ctx context.Context, rept
return false, nil
}

reportMedian, err := nm.reportCodec.MedianFromReport(report)
reportMedian, err := nm.reportCodec.MedianFromReport(ctx, report)
if err != nil {
return false, fmt.Errorf("error during MedianFromReport: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type TitleRequestPluginFactory struct {
Contract *ocr2titlerequest.OCR2TitleRequest
}

func (fac *TitleRequestPluginFactory) NewReportingPlugin(config types.ReportingPluginConfig) (types.ReportingPlugin, types.ReportingPluginInfo, error) {
func (fac *TitleRequestPluginFactory) NewReportingPlugin(ctx context.Context, config types.ReportingPluginConfig) (types.ReportingPlugin, types.ReportingPluginInfo, error) {
return &TitleRequestPlugin{
config.F,
fac.Client,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evmutil

import (
"context"
"fmt"
"strings"

Expand All @@ -15,7 +16,7 @@ type EVMOffchainConfigDigester struct {
ContractAddress common.Address
}

func (d EVMOffchainConfigDigester) ConfigDigest(cc types.ContractConfig) (types.ConfigDigest, error) {
func (d EVMOffchainConfigDigester) ConfigDigest(ctx context.Context, cc types.ContractConfig) (types.ConfigDigest, error) {
signers := []common.Address{}
for i, signer := range cc.Signers {
if len(signer) != 20 {
Expand Down Expand Up @@ -46,6 +47,6 @@ func (d EVMOffchainConfigDigester) ConfigDigest(cc types.ContractConfig) (types.
), nil
}

func (d EVMOffchainConfigDigester) ConfigDigestPrefix() (types.ConfigDigestPrefix, error) {
func (d EVMOffchainConfigDigester) ConfigDigestPrefix(context.Context) (types.ConfigDigestPrefix, error) {
return types.ConfigDigestPrefixEVMSimple, nil
}
11 changes: 6 additions & 5 deletions offchainreporting2plus/internal/managed/config_digest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package managed

import (
"context"
"fmt"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
Expand All @@ -13,13 +14,13 @@ type prefixCheckConfigDigester struct {

// ConfigDigest method that checks that the computed ConfigDigest's prefix is
// consistent with OffchainConfigDigester.ConfigDigestPrefix
func (d prefixCheckConfigDigester) ConfigDigest(cc types.ContractConfig) (types.ConfigDigest, error) {
prefix, err := d.offchainConfigDigester.ConfigDigestPrefix()
func (d prefixCheckConfigDigester) ConfigDigest(ctx context.Context, cc types.ContractConfig) (types.ConfigDigest, error) {
prefix, err := d.offchainConfigDigester.ConfigDigestPrefix(ctx)
if err != nil {
return types.ConfigDigest{}, err
}

cd, err := d.offchainConfigDigester.ConfigDigest(cc)
cd, err := d.offchainConfigDigester.ConfigDigest(ctx, cc)
if err != nil {
return types.ConfigDigest{}, err
}
Expand All @@ -33,8 +34,8 @@ func (d prefixCheckConfigDigester) ConfigDigest(cc types.ContractConfig) (types.

// Check that the ContractConfig's ConfigDigest matches the one computed
// offchain
func (d prefixCheckConfigDigester) CheckContractConfig(cc types.ContractConfig) error {
goodConfigDigest, err := d.ConfigDigest(cc)
func (d prefixCheckConfigDigester) CheckContractConfig(ctx context.Context, cc types.ContractConfig) error {
goodConfigDigest, err := d.ConfigDigest(ctx, cc)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func RunManagedMercuryOracle(
func(ctx context.Context, contractConfig types.ContractConfig, logger loghelper.LoggerWithContext) {
skipResourceExhaustionChecks := localConfig.DevelopmentMode == types.EnableDangerousDevelopmentMode

fromAccount, err := contractTransmitter.FromAccount()
fromAccount, err := contractTransmitter.FromAccount(ctx)
if err != nil {
logger.Error("ManagedMercuryOracle: error getting FromAccount", commontypes.LogFields{
"error": err,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func RunManagedOCR2Oracle(
func(ctx context.Context, contractConfig types.ContractConfig, logger loghelper.LoggerWithContext) {
skipResourceExhaustionChecks := localConfig.DevelopmentMode == types.EnableDangerousDevelopmentMode

fromAccount, err := contractTransmitter.FromAccount()
fromAccount, err := contractTransmitter.FromAccount(ctx)
if err != nil {
logger.Error("ManagedOCR2Oracle: error getting FromAccount", commontypes.LogFields{
"error": err,
Expand Down Expand Up @@ -108,7 +108,7 @@ func RunManagedOCR2Oracle(
"oid": oid,
})

reportingPlugin, reportingPluginInfo, err := reportingPluginFactory.NewReportingPlugin(types.ReportingPluginConfig{
reportingPlugin, reportingPluginInfo, err := reportingPluginFactory.NewReportingPlugin(ctx, types.ReportingPluginConfig{
sharedConfig.ConfigDigest,
oid,
sharedConfig.N(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (rwcc *runWithContractConfigState) configChanged(contractConfig types.Contr
})

// note that there is an analogous check in TrackConfig, so this should never trigger.
if err := rwcc.configDigester.CheckContractConfig(contractConfig); err != nil {
if err := rwcc.configDigester.CheckContractConfig(rwcc.ctx, contractConfig); err != nil {
rwcc.logger.Error("runWithContractConfig: detected corruption while attempting to change configuration", commontypes.LogFields{
"err": err,
"contractConfig": contractConfig,
Expand Down
2 changes: 1 addition & 1 deletion offchainreporting2plus/internal/managed/track_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (state *trackConfigState) checkLatestConfigDetails() (

// Ignore configs where the configDigest doesn't match, they might have
// been corrupted somehow.
if err := state.configDigester.CheckContractConfig(contractConfig); err != nil {
if err := state.configDigester.CheckContractConfig(state.ctx, contractConfig); err != nil {
state.logger.Error("TrackConfig: received corrupted config change", commontypes.LogFields{
"error": err,
"contractConfig": contractConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (t *MercuryOCR3ContractTransmitter) Transmit(
}

func (t *MercuryOCR3ContractTransmitter) FromAccount() (types.Account, error) {
return t.ocr2ContractTransmitter.FromAccount()
return t.ocr2ContractTransmitter.FromAccount(context.Background())
}

func ocr3MaxOutcomeLength(maxReportLength int) int {
Expand Down
5 changes: 3 additions & 2 deletions offchainreporting2plus/types/config_digest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"context"
"encoding"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -102,8 +103,8 @@ func (c ConfigDigest) MarshalText() (text []byte, err error) {
type OffchainConfigDigester interface {
// Compute ConfigDigest for the given ContractConfig. The first two bytes of the
// ConfigDigest must be the big-endian encoding of ConfigDigestPrefix!
ConfigDigest(ContractConfig) (ConfigDigest, error)
ConfigDigest(context.Context, ContractConfig) (ConfigDigest, error)

// This should return the same constant value on every invocation
ConfigDigestPrefix() (ConfigDigestPrefix, error)
ConfigDigestPrefix(context.Context) (ConfigDigestPrefix, error)
}
4 changes: 2 additions & 2 deletions offchainreporting2plus/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type ReportingPluginFactory interface {
// Creates a new reporting plugin instance. The instance may have
// associated goroutines or hold system resources, which should be
// released when its Close() function is called.
NewReportingPlugin(ReportingPluginConfig) (ReportingPlugin, ReportingPluginInfo, error)
NewReportingPlugin(context.Context, ReportingPluginConfig) (ReportingPlugin, ReportingPluginInfo, error)
}

type ReportingPluginConfig struct {
Expand Down Expand Up @@ -330,7 +330,7 @@ type ContractTransmitter interface {
)

// Account from which the transmitter invokes the contract
FromAccount() (Account, error)
FromAccount(context.Context) (Account, error)
}

// ContractConfigTracker tracks configuration changes of the OCR contract
Expand Down

0 comments on commit 6ce1cf0

Please sign in to comment.