Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Logical Bug in isStaleReport Function #377

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commit/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,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)
})
}
}
Loading