Skip to content

Commit

Permalink
add context
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Mar 13, 2024
1 parent 537fc61 commit 052a04b
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 99 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ module github.com/smartcontractkit/chainlink-data-streams

go 1.21

replace github.com/smartcontractkit/libocr => github.com/jmank88/libocr v0.0.0-20240311135254-702bd5bd3727

require (
github.com/hashicorp/go-plugin v1.6.0
github.com/pkg/errors v0.9.1
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chain-selectors v1.0.5
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220201513-6cb818f8800d
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240313190927-9f7349158647
github.com/smartcontractkit/libocr v0.0.0-20240112202000-6359502d2ff1
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
google.golang.org/protobuf v1.32.0
)

Expand Down
19 changes: 8 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbg
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
github.com/jmank88/libocr v0.0.0-20240311135254-702bd5bd3727 h1:G9Djr4klIISnLqRgTOl75ay4PXb7BnxTl+JCCvk0Dm8=
github.com/jmank88/libocr v0.0.0-20240311135254-702bd5bd3727/go.mod h1:SJEZCHgMCAzzBvo9vMV2DQ9onfEcIJCYSViyP4JI6c4=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down Expand Up @@ -207,31 +209,26 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/smartcontractkit/chain-selectors v1.0.5 h1:NOefQsogPZS4aBbWPFrgAyoke0gppN2ojfa8SQkhu8c=
github.com/smartcontractkit/chain-selectors v1.0.5/go.mod h1:WBhLlODF5b95vvx2tdKK55vGACg1+qZpuBhOGu1UXVo=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240216181624-ccf0da042f11 h1:zazCJSjz60/V5B8+8LxLX7BrtN1AXGUv+B/E1Xrq2OA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240216181624-ccf0da042f11/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220164325-44ed6b4009cb h1:E0K6uq+9SjpktUeqTg8NVarUAfwRBL/reIuZb4msrtA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220164325-44ed6b4009cb/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220165243-0b144b02fd8d h1:OOezTy1Z4sITGuOx3oegof47O0AbloW/YLPjzVLXn1E=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220165243-0b144b02fd8d/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220201513-6cb818f8800d h1:XByRw7oF2mG+yBuoMpBqm5Ogu1vlunPZfhL74Ny3NFU=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240220201513-6cb818f8800d/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240313190927-9f7349158647 h1:1tvpNyjI9d590SH3KFoYTbhWOWt0E6xzDSR5BClgzpw=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240313190927-9f7349158647/go.mod h1:xZmkdvdylcXwU3jHNREG2KAwKzDFkUfHtDwXyc8ISOY=
github.com/smartcontractkit/go-plugin v0.0.0-20240208201424-b3b91517de16 h1:TFe+FvzxClblt6qRfqEhUfa4kFQx5UobuoFGO2W4mMo=
github.com/smartcontractkit/go-plugin v0.0.0-20240208201424-b3b91517de16/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f h1:hgJif132UCdjo8u43i7iPN1/MFnu49hv7lFGFftCHKU=
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f/go.mod h1:MvMXoufZAtqExNexqi4cjrNYE9MefKddKylxjS+//n0=
github.com/smartcontractkit/libocr v0.0.0-20240112202000-6359502d2ff1 h1:3y9WsXkZ5lxFrmfH7DQHs/q308lylKId5l/3VC0QAdM=
github.com/smartcontractkit/libocr v0.0.0-20240112202000-6359502d2ff1/go.mod h1:kC0qmVPUaVkFqGiZMNhmRmjdphuUmeyLEdlWFOQzFWI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
12 changes: 6 additions & 6 deletions llo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
chainselectors "github.com/smartcontractkit/chain-selectors"
"github.com/smartcontractkit/libocr/offchainreporting2/types"
ocr2types "github.com/smartcontractkit/libocr/offchainreporting2/types"
ocr3types "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
)

// TODO: Split out this file and write unit tests: https://smartcontract-it.atlassian.net/browse/MERC-3524
Expand Down Expand Up @@ -179,7 +179,7 @@ type PluginFactory struct {
Codecs map[llotypes.ReportFormat]ReportCodec
}

func (f *PluginFactory) NewReportingPlugin(cfg ocr3types.ReportingPluginConfig) (ocr3types.ReportingPlugin[llotypes.ReportInfo], ocr3types.ReportingPluginInfo, error) {
func (f *PluginFactory) NewReportingPlugin(ctx context.Context, cfg ocr3types.ReportingPluginConfig) (ocr3types.ReportingPlugin[llotypes.ReportInfo], ocr3types.ReportingPluginInfo, error) {
offchainCfg, err := DecodeOffchainConfig(cfg.OffchainConfig)
if err != nil {
return nil, ocr3types.ReportingPluginInfo{}, fmt.Errorf("NewReportingPlugin failed to decode offchain config; got: 0x%x (len: %d); %w", cfg.OffchainConfig, len(cfg.OffchainConfig), err)
Expand Down Expand Up @@ -374,7 +374,7 @@ func (p *LLOPlugin) Observation(ctx context.Context, outctx ocr3types.OutcomeCon
// *not* strictly) across the lifetime of a protocol instance and that
// outctx.previousOutcome contains the consensus outcome with sequence
// number (outctx.SeqNr-1).
func (p *LLOPlugin) ValidateObservation(outctx ocr3types.OutcomeContext, query types.Query, ao types.AttributedObservation) error {
func (p *LLOPlugin) ValidateObservation(ctx context.Context, outctx ocr3types.OutcomeContext, query types.Query, ao types.AttributedObservation) error {
if outctx.SeqNr <= 1 {
if len(ao.Observation) != 0 {
return fmt.Errorf("Observation is not empty")
Expand Down Expand Up @@ -515,7 +515,7 @@ func (out *Outcome) ReportableChannels() []llotypes.ChannelID {
//
// libocr guarantees that this will always be called with at least 2f+1
// AttributedObservations
func (p *LLOPlugin) Outcome(outctx ocr3types.OutcomeContext, query types.Query, aos []types.AttributedObservation) (ocr3types.Outcome, error) {
func (p *LLOPlugin) Outcome(ctx context.Context, outctx ocr3types.OutcomeContext, query types.Query, aos []types.AttributedObservation) (ocr3types.Outcome, error) {
if len(aos) < 2*p.F+1 {
return nil, fmt.Errorf("invariant violation: expected at least 2f+1 attributed observations, got %d (f: %d)", len(aos), p.F)
}
Expand Down Expand Up @@ -797,7 +797,7 @@ func (p *LLOPlugin) encodeReport(r Report, format llotypes.ReportFormat) (types.
// *not* strictly) across the lifetime of a protocol instance and that
// outctx.previousOutcome contains the consensus outcome with sequence
// number (outctx.SeqNr-1).
func (p *LLOPlugin) Reports(seqNr uint64, rawOutcome ocr3types.Outcome) ([]ocr3types.ReportWithInfo[llotypes.ReportInfo], error) {
func (p *LLOPlugin) Reports(ctx context.Context, seqNr uint64, rawOutcome ocr3types.Outcome) ([]ocr3types.ReportWithInfo[llotypes.ReportInfo], error) {
if seqNr <= 1 {
// no reports for initial round
return nil, nil
Expand Down Expand Up @@ -888,7 +888,7 @@ func (p *LLOPlugin) ShouldTransmitAcceptedReport(context.Context, uint64, ocr3ty
// This is an advanced feature. The "default" approach (what OCR1 & OCR2
// did) is to have an empty ValidateObservation function and return
// QuorumTwoFPlusOne from this function.
func (p *LLOPlugin) ObservationQuorum(outctx ocr3types.OutcomeContext, query types.Query) (ocr3types.Quorum, error) {
func (p *LLOPlugin) ObservationQuorum(ctx context.Context, outctx ocr3types.OutcomeContext, query types.Query) (ocr3types.Quorum, error) {
return ocr3types.QuorumTwoFPlusOne, nil
}

Expand Down
5 changes: 3 additions & 2 deletions mercury/onchain_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mercury

import (
"context"
"math/big"

pkgerrors "github.com/pkg/errors"
Expand All @@ -27,7 +28,7 @@ var _ mercury.OnchainConfigCodec = StandardOnchainConfigCodec{}
// returned by EncodeValueInt192.
type StandardOnchainConfigCodec struct{}

func (StandardOnchainConfigCodec) Decode(b []byte) (mercury.OnchainConfig, error) {
func (StandardOnchainConfigCodec) Decode(ctx context.Context, b []byte) (mercury.OnchainConfig, error) {
if len(b) != onchainConfigEncodedLength {
return mercury.OnchainConfig{}, pkgerrors.Errorf("unexpected length of OnchainConfig, expected %v, got %v", onchainConfigEncodedLength, len(b))
}
Expand Down Expand Up @@ -56,7 +57,7 @@ func (StandardOnchainConfigCodec) Decode(b []byte) (mercury.OnchainConfig, error
return mercury.OnchainConfig{Min: min, Max: max}, nil
}

func (StandardOnchainConfigCodec) Encode(c mercury.OnchainConfig) ([]byte, error) {
func (StandardOnchainConfigCodec) Encode(ctx context.Context, c mercury.OnchainConfig) ([]byte, error) {
verBytes, err := bigbigendian.SerializeSigned(32, onchainConfigVersionBig)
if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions mercury/onchain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import (
"testing"

"github.com/smartcontractkit/chainlink-common/pkg/types/mercury"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
)

func FuzzDecodeOnchainConfig(f *testing.F) {
valid, err := StandardOnchainConfigCodec{}.Encode(mercury.OnchainConfig{Min: big.NewInt(1), Max: big.NewInt(1000)})
valid, err := StandardOnchainConfigCodec{}.Encode(tests.Context(f), mercury.OnchainConfig{Min: big.NewInt(1), Max: big.NewInt(1000)})
if err != nil {
f.Fatalf("failed to construct valid OnchainConfig: %s", err)
}

f.Add([]byte{})
f.Add([]byte(valid))
f.Fuzz(func(t *testing.T, encoded []byte) {
decoded, err := StandardOnchainConfigCodec{}.Decode(encoded)
ctx := tests.Context(t)
decoded, err := StandardOnchainConfigCodec{}.Decode(ctx, encoded)
if err != nil {
return
}

encoded2, err := StandardOnchainConfigCodec{}.Encode(decoded)
encoded2, err := StandardOnchainConfigCodec{}.Encode(ctx, decoded)
if err != nil {
t.Fatalf("failed to re-encode decoded input: %s", err)
}
Expand Down
16 changes: 8 additions & 8 deletions mercury/v1/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ func NewFactory(ds DataSource, lggr logger.Logger, occ mercurytypes.OnchainConfi
return Factory{ds, lggr, occ, rc}
}

func (fac Factory) NewMercuryPlugin(configuration ocr3types.MercuryPluginConfig) (ocr3types.MercuryPlugin, ocr3types.MercuryPluginInfo, error) {
func (fac Factory) NewMercuryPlugin(ctx context.Context, configuration ocr3types.MercuryPluginConfig) (ocr3types.MercuryPlugin, ocr3types.MercuryPluginInfo, error) {
offchainConfig, err := mercury.DecodeOffchainConfig(configuration.OffchainConfig)
if err != nil {
return nil, ocr3types.MercuryPluginInfo{}, err
}

onchainConfig, err := fac.onchainConfigCodec.Decode(configuration.OnchainConfig)
onchainConfig, err := fac.onchainConfigCodec.Decode(ctx, configuration.OnchainConfig)
if err != nil {
return nil, ocr3types.MercuryPluginInfo{}, err
}

maxReportLength, err := fac.reportCodec.MaxReportLength(configuration.N)
maxReportLength, err := fac.reportCodec.MaxReportLength(ctx, configuration.N)
if err != nil {
return nil, ocr3types.MercuryPluginInfo{}, err
}
Expand Down Expand Up @@ -328,7 +328,7 @@ func parseAttributedObservations(lggr logger.Logger, aos []types.AttributedObser
return paos
}

func (rp *reportingPlugin) Report(repts types.ReportTimestamp, previousReport types.Report, aos []types.AttributedObservation) (shouldReport bool, report types.Report, err error) {
func (rp *reportingPlugin) Report(ctx context.Context, repts types.ReportTimestamp, previousReport types.Report, aos []types.AttributedObservation) (shouldReport bool, report types.Report, err error) {
paos := parseAttributedObservations(rp.logger, aos)

if len(paos) == 0 {
Expand All @@ -340,7 +340,7 @@ func (rp *reportingPlugin) Report(repts types.ReportTimestamp, previousReport ty
return false, nil, pkgerrors.Errorf("only received %v valid attributed observations, but need at least f+1 (%v)", len(paos), rp.f+1)
}

rf, err := rp.buildReportFields(previousReport, paos)
rf, err := rp.buildReportFields(ctx, previousReport, paos)
if err != nil {
rp.logger.Errorw("failed to build report fields", "paos", paos, "f", rp.f, "reportFields", rf, "repts", repts, "err", err)
return false, nil, err
Expand All @@ -359,7 +359,7 @@ func (rp *reportingPlugin) Report(repts types.ReportTimestamp, previousReport ty
"timestamp", repts,
)

report, err = rp.reportCodec.BuildReport(rf)
report, err = rp.reportCodec.BuildReport(ctx, rf)
if err != nil {
rp.logger.Debugw("failed to BuildReport", "paos", paos, "f", rp.f, "reportFields", rf, "repts", repts)
return false, nil, err
Expand All @@ -373,11 +373,11 @@ func (rp *reportingPlugin) Report(repts types.ReportTimestamp, previousReport ty
return true, report, nil
}

func (rp *reportingPlugin) buildReportFields(previousReport types.Report, paos []PAO) (rf v1.ReportFields, merr error) {
func (rp *reportingPlugin) buildReportFields(ctx context.Context, previousReport types.Report, paos []PAO) (rf v1.ReportFields, merr error) {
var err error
if previousReport != nil {
var maxFinalizedBlockNumber int64
maxFinalizedBlockNumber, err = rp.reportCodec.CurrentBlockNumFromReport(previousReport)
maxFinalizedBlockNumber, err = rp.reportCodec.CurrentBlockNumFromReport(ctx, previousReport)
if err != nil {
merr = errors.Join(merr, err)
} else {
Expand Down
Loading

0 comments on commit 052a04b

Please sign in to comment.