Skip to content

Commit

Permalink
fix stale-report checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv committed Dec 18, 2024
1 parent ce8cbe5 commit 5c19d49
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commit/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
53 changes: 53 additions & 0 deletions commit/report_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commit

import (
rand2 "math/rand"
"testing"

"github.com/smartcontractkit/libocr/commontypes"
Expand Down Expand Up @@ -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)
})
}
}
1 change: 1 addition & 0 deletions execute/plugin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"

Check failure on line 9 in execute/plugin_functions_test.go

View workflow job for this annotation

GitHub Actions / build-lint-test

"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" imported and not used (typecheck)
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
Expand Down

0 comments on commit 5c19d49

Please sign in to comment.