From 5c19d4955792fd1539d04fc540c0edd877dc4ec9 Mon Sep 17 00:00:00 2001 From: dimkouv Date: Wed, 18 Dec 2024 11:45:03 +0200 Subject: [PATCH 1/2] fix stale-report checks --- commit/report.go | 2 +- commit/report_test.go | 53 ++++++++++++++++++++++++++++++++ execute/plugin_functions_test.go | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/commit/report.go b/commit/report.go index 99a64b3c3..75bf0d3d9 100644 --- a/commit/report.go +++ b/commit/report.go @@ -230,7 +230,7 @@ func (p *Plugin) decodeReport(ctx context.Context, report []byte) (cciptypes.Com } func (p *Plugin) isStaleReport(seqNr, latestSeqNr uint64, decodedReport cciptypes.CommitPluginReport) bool { - if seqNr < latestSeqNr && len(decodedReport.MerkleRoots) == 0 { + if seqNr <= latestSeqNr && len(decodedReport.MerkleRoots) == 0 { p.lggr.Infow("skipping stale report", "seqNr", seqNr, "latestSeqNr", latestSeqNr) return true } diff --git a/commit/report_test.go b/commit/report_test.go index 59fc253c8..32c90f66c 100644 --- a/commit/report_test.go +++ b/commit/report_test.go @@ -1,6 +1,7 @@ package commit import ( + rand2 "math/rand" "testing" "github.com/smartcontractkit/libocr/commontypes" @@ -199,3 +200,55 @@ func TestPluginReports_InvalidOutcome(t *testing.T) { _, err := p.Reports(tests.Context(t), 0, []byte("invalid json")) require.Error(t, err) } + +func Test_Plugin_isStaleReport(t *testing.T) { + testCases := []struct { + name string + onChainSeqNum uint64 + reportSeqNum uint64 + lenMerkleRoots int + shouldBeStale bool + }{ + { + name: "report is not stale when merkle roots exist no matter the seq nums", + onChainSeqNum: rand2.Uint64(), + reportSeqNum: rand2.Uint64(), + lenMerkleRoots: 1, + shouldBeStale: false, + }, + { + name: "report is stale when onChainSeqNum is equal to report seq num", + onChainSeqNum: 33, + reportSeqNum: 33, + lenMerkleRoots: 0, + shouldBeStale: true, + }, + { + name: "report is stale when onChainSeqNum is greater than report seq num", + onChainSeqNum: 34, + reportSeqNum: 33, + lenMerkleRoots: 0, + shouldBeStale: true, + }, + { + name: "report is not stale when onChainSeqNum is less than report seq num", + onChainSeqNum: 32, + reportSeqNum: 33, + lenMerkleRoots: 0, + shouldBeStale: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + p := Plugin{ + lggr: logger.Test(t), + } + report := ccipocr3.CommitPluginReport{ + MerkleRoots: make([]ccipocr3.MerkleRootChain, tc.lenMerkleRoots), + } + stale := p.isStaleReport(tc.reportSeqNum, tc.onChainSeqNum, report) + require.Equal(t, tc.shouldBeStale, stale) + }) + } +} diff --git a/execute/plugin_functions_test.go b/execute/plugin_functions_test.go index 64987354d..87fc87056 100644 --- a/execute/plugin_functions_test.go +++ b/execute/plugin_functions_test.go @@ -6,6 +6,7 @@ import ( "time" mapset "github.com/deckarep/golang-set/v2" + "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/assert" From 3cf73aeaa7621e3312c7a27a96b85fb576827bf7 Mon Sep 17 00:00:00 2001 From: dimkouv Date: Wed, 18 Dec 2024 11:52:26 +0200 Subject: [PATCH 2/2] fix imports --- execute/plugin_functions_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/execute/plugin_functions_test.go b/execute/plugin_functions_test.go index 87fc87056..64987354d 100644 --- a/execute/plugin_functions_test.go +++ b/execute/plugin_functions_test.go @@ -6,7 +6,6 @@ import ( "time" mapset "github.com/deckarep/golang-set/v2" - "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/assert"