Skip to content

Commit

Permalink
bump libocr; add context
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Sep 19, 2024
1 parent c8ba79e commit 21f864a
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 232 deletions.
2 changes: 2 additions & 0 deletions commit/chainfee/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

mapset "github.com/deckarep/golang-set/v2"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"

"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon"
Expand All @@ -30,6 +31,7 @@ func (w *Processor) Observation(
}

func (w *Processor) Outcome(
ctx context.Context,
prevOutcome Outcome,
query Query,
aos []plugincommon.AttributedObservation[Observation],
Expand Down
61 changes: 32 additions & 29 deletions commit/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewPluginFactory(
}
}

func (p *PluginFactory) NewReportingPlugin(config ocr3types.ReportingPluginConfig,
func (p *PluginFactory) NewReportingPlugin(ctx context.Context, config ocr3types.ReportingPluginConfig,
) (ocr3types.ReportingPlugin[[]byte], ocr3types.ReportingPluginInfo, error) {
offchainConfig, err := pluginconfig.DecodeCommitOffchainConfig(config.OffchainConfig)
if err != nil {
Expand Down Expand Up @@ -134,34 +134,37 @@ func (p *PluginFactory) NewReportingPlugin(config ocr3types.ReportingPluginConfi
p.ocrConfig.Config.ChainSelector,
p.ocrConfig.Config.OfframpAddress,
)
return NewPlugin(
context.Background(),
config.OracleID,
oracleIDToP2PID,
pluginconfig.CommitPluginConfig{
DestChain: p.ocrConfig.Config.ChainSelector,
NewMsgScanBatchSize: merklemulti.MaxNumberTreeLeaves,
MaxReportTransmissionCheckAttempts: maxReportTransmissionCheckAttempts,
OffchainConfig: offchainConfig,
},
ccipReader,
onChainTokenPricesReader,
p.commitCodec,
p.msgHasher,
p.lggr,
p.homeChainReader,
config,
rmn.Config{}, // todo
), ocr3types.ReportingPluginInfo{
Name: "CCIPRoleCommit",
Limits: ocr3types.ReportingPluginLimits{
MaxQueryLength: maxQueryLength,
MaxObservationLength: 20_000, // 20kB
MaxOutcomeLength: 10_000, // 10kB
MaxReportLength: 10_000, // 10kB
MaxReportCount: 10,
},
}, nil
plugin := NewPlugin(
config.OracleID,
oracleIDToP2PID,
pluginconfig.CommitPluginConfig{
DestChain: p.ocrConfig.Config.ChainSelector,
NewMsgScanBatchSize: merklemulti.MaxNumberTreeLeaves,
MaxReportTransmissionCheckAttempts: maxReportTransmissionCheckAttempts,
OffchainConfig: offchainConfig,
},
ccipReader,
onChainTokenPricesReader,
p.commitCodec,
p.msgHasher,
p.lggr,
p.homeChainReader,
config,
rmn.Config{}, // todo
)
if err = plugin.Start(ctx); err != nil {
return nil, ocr3types.ReportingPluginInfo{}, fmt.Errorf("failed to start plugin: %w", err)
}
return plugin, ocr3types.ReportingPluginInfo{
Name: "CCIPRoleCommit",
Limits: ocr3types.ReportingPluginLimits{
MaxQueryLength: maxQueryLength,
MaxObservationLength: 20_000, // 20kB
MaxOutcomeLength: 10_000, // 10kB
MaxReportLength: 10_000, // 10kB
MaxReportCount: 10,
},
}, nil
}

func (p PluginFactory) Name() string {
Expand Down
8 changes: 6 additions & 2 deletions commit/merkleroot/observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/libocr/quorumhelper"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
Expand All @@ -26,9 +27,12 @@ import (
readerpkg "github.com/smartcontractkit/chainlink-ccip/pkg/reader"
)

func (w *Processor) ObservationQuorum(_ ocr3types.OutcomeContext, _ types.Query) (ocr3types.Quorum, error) {
func (w *Processor) ObservationQuorum(
_ context.Context, _ ocr3types.OutcomeContext, _ types.Query, aos []types.AttributedObservation,
) (bool, error) {
// Across all chains we require at least 2F+1 observations.
return ocr3types.QuorumTwoFPlusOne, nil
return quorumhelper.ObservationCountReachesObservationQuorum(
quorumhelper.QuorumTwoFPlusOne, w.reportingCfg.N, w.reportingCfg.F, aos), nil
}

func (w *Processor) Observation(
Expand Down
2 changes: 2 additions & 0 deletions commit/merkleroot/outcome.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package merkleroot

import (
"context"
"fmt"
"sort"
"time"
Expand All @@ -20,6 +21,7 @@ import (
// - builds a report
// - checks for the transmission of a previous report
func (w *Processor) Outcome(
ctx context.Context,
prevOutcome Outcome,
query Query,
aos []plugincommon.AttributedObservation[Observation],
Expand Down
24 changes: 17 additions & 7 deletions commit/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/libocr/quorumhelper"
libocrtypes "github.com/smartcontractkit/libocr/ragep2p/types"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
Expand Down Expand Up @@ -46,7 +47,6 @@ type Plugin struct {
}

func NewPlugin(
_ context.Context,
nodeID commontypes.OracleID,
oracleIDToP2pID map[commontypes.OracleID]libocrtypes.PeerID,
cfg pluginconfig.CommitPluginConfig,
Expand All @@ -65,9 +65,6 @@ func NewPlugin(
syncTimeout(cfg.SyncTimeout),
syncFrequency(cfg.SyncFrequency),
)
if err := readerSyncer.Start(context.Background()); err != nil {
lggr.Errorw("error starting background reader syncer", "err", err)
}

chainSupport := plugincommon.NewCCIPChainSupport(
lggr,
Expand Down Expand Up @@ -143,9 +140,12 @@ func (p *Plugin) Query(ctx context.Context, outCtx ocr3types.OutcomeContext) (ty
return q.Encode()
}

func (p *Plugin) ObservationQuorum(_ ocr3types.OutcomeContext, _ types.Query) (ocr3types.Quorum, error) {
func (p *Plugin) ObservationQuorum(
ctx context.Context, _ ocr3types.OutcomeContext, _ types.Query, aos []types.AttributedObservation,
) (bool, error) {
// Across all chains we require at least 2F+1 observations.
return ocr3types.QuorumTwoFPlusOne, nil
return quorumhelper.ObservationCountReachesObservationQuorum(
quorumhelper.QuorumFPlusOne, p.reportingCfg.N, p.reportingCfg.F, aos), nil
}

func (p *Plugin) Observation(
Expand Down Expand Up @@ -196,7 +196,7 @@ func (p *Plugin) ObserveFChain() map[cciptypes.ChainSelector]int {
// - builds a report
// - checks for the transmission of a previous report
func (p *Plugin) Outcome(
outCtx ocr3types.OutcomeContext, q types.Query, aos []types.AttributedObservation,
ctx context.Context, outCtx ocr3types.OutcomeContext, q types.Query, aos []types.AttributedObservation,
) (ocr3types.Outcome, error) {
prevOutcome := p.decodeOutcome(outCtx.PreviousOutcome)

Expand Down Expand Up @@ -238,6 +238,7 @@ func (p *Plugin) Outcome(
}

merkleRootOutcome, err := p.merkleRootProcessor.Outcome(
ctx,
prevOutcome.MerkleRootOutcome,
decodedQ.MerkleRootQuery,
merkleObservations,
Expand All @@ -247,6 +248,7 @@ func (p *Plugin) Outcome(
}

tokenPriceOutcome, err := p.tokenPriceProcessor.Outcome(
ctx,
prevOutcome.TokenPriceOutcome,
decodedQ.TokenPriceQuery,
tokensObservations,
Expand All @@ -256,6 +258,7 @@ func (p *Plugin) Outcome(
}

chainFeeOutcome, err := p.chainFeeProcessor.Outcome(
ctx,
prevOutcome.ChainFeeOutcome,
decodedQ.ChainFeeQuery,
feeObservations,
Expand All @@ -271,6 +274,13 @@ func (p *Plugin) Outcome(
}.Encode()
}

func (p *Plugin) Start(ctx context.Context) error {
if err := p.readerSyncer.Start(ctx); err != nil {
return fmt.Errorf("error starting background reader syncer: %w", err)
}
return nil
}

func (p *Plugin) Close() error {
timeout := 10 * time.Second
ctx, cf := context.WithTimeout(context.Background(), timeout)
Expand Down
2 changes: 1 addition & 1 deletion commit/plugin_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ func setupNode(
ccipReader.EXPECT().NextSeqNum(ctx, sourceChains).Return(offRampNextSeqNums, nil).Maybe()

p := NewPlugin(
ctx,
nodeID,
oracleIDToP2pID,
pluginCfg,
Expand All @@ -345,6 +344,7 @@ func setupNode(
reportingCfg,
rmn.Config{},
)
require.NoError(t, p.Start(ctx))

return nodeSetup{
node: p,
Expand Down
13 changes: 9 additions & 4 deletions commit/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

func (p *Plugin) Reports(seqNr uint64, outcomeBytes ocr3types.Outcome) ([]ocr3types.ReportWithInfo[[]byte], error) {
func (p *Plugin) Reports(
ctx context.Context, seqNr uint64, outcomeBytes ocr3types.Outcome,
) ([]ocr3types.ReportPlus[[]byte], error) {
outcome, err := DecodeOutcome(outcomeBytes)
if err != nil {
// TODO: metrics
Expand All @@ -22,7 +24,7 @@ func (p *Plugin) Reports(seqNr uint64, outcomeBytes ocr3types.Outcome) ([]ocr3ty

// Until we start adding tokens and gas to the report, we don't need to report anything
if outcome.MerkleRootOutcome.OutcomeType != merkleroot.ReportGenerated {
return []ocr3types.ReportWithInfo[[]byte]{}, nil
return []ocr3types.ReportPlus[[]byte]{}, nil
}

rep := cciptypes.CommitPluginReport{
Expand All @@ -34,12 +36,15 @@ func (p *Plugin) Reports(seqNr uint64, outcomeBytes ocr3types.Outcome) ([]ocr3ty
RMNSignatures: outcome.MerkleRootOutcome.RMNReportSignatures,
}

encodedReport, err := p.reportCodec.Encode(context.Background(), rep)
encodedReport, err := p.reportCodec.Encode(ctx, rep)
if err != nil {
return nil, fmt.Errorf("encode commit plugin report: %w", err)
}

return []ocr3types.ReportWithInfo[[]byte]{{Report: encodedReport, Info: nil}}, nil
return []ocr3types.ReportPlus[[]byte]{
{ReportWithInfo: ocr3types.ReportWithInfo[[]byte]{
Report: encodedReport, Info: nil}},
}, nil
}

func (p *Plugin) ShouldAcceptAttestedReport(
Expand Down
12 changes: 7 additions & 5 deletions commit/tokenprice/outcome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon"
"github.com/smartcontractkit/chainlink-ccip/internal/plugintypes"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/stretchr/testify/assert"
)

var ts = time.Now().UTC()
Expand Down Expand Up @@ -139,6 +140,7 @@ func TestSelectTokensForUpdate(t *testing.T) {

// Test Plugin Outcome method returns the correct token prices
func TestOutcome(t *testing.T) {
ctx := tests.Context(t)
lggr := logger.Test(t)
p := &processor{
lggr: lggr,
Expand All @@ -149,7 +151,7 @@ func TestOutcome(t *testing.T) {
bigF: 1,
}

outcome, err := p.Outcome(Outcome{}, Query{}, []plugincommon.AttributedObservation[Observation]{
outcome, err := p.Outcome(ctx, Outcome{}, Query{}, []plugincommon.AttributedObservation[Observation]{
{OracleID: 1, Observation: obs},
{OracleID: 2, Observation: obs},
{OracleID: 3, Observation: obs},
Expand Down
7 changes: 4 additions & 3 deletions commit/tokenprice/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

mapset "github.com/deckarep/golang-set/v2"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"

"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon"
"github.com/smartcontractkit/chainlink-ccip/internal/reader"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

type processor struct {
Expand Down Expand Up @@ -82,6 +82,7 @@ func (p *processor) ValidateObservation(
}

func (p *processor) Outcome(
ctx context.Context,
_ Outcome,
_ Query,
aos []plugincommon.AttributedObservation[Observation],
Expand Down
2 changes: 2 additions & 0 deletions commit/validate_observation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commit

import (
"context"
"fmt"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
Expand All @@ -11,6 +12,7 @@ import (

// ValidateObservation validates an observation to ensure it is well-formed
func (p *Plugin) ValidateObservation(
_ context.Context,
outCtx ocr3types.OutcomeContext,
q types.Query,
ao types.AttributedObservation,
Expand Down
Loading

0 comments on commit 21f864a

Please sign in to comment.