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

CL33-09: Make seq num range resilient to mixed up beginning and end. #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 execute/report/roots.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ConstructMerkleTree(
lggr logger.Logger,
) (*merklemulti.Tree[[32]byte], error) {
// Ensure we have the expected number of messages
numMsgs := int(report.SequenceNumberRange.End() - report.SequenceNumberRange.Start() + 1)
numMsgs := report.SequenceNumberRange.Length()
if numMsgs != len(report.Messages) {
return nil, fmt.Errorf(
"malformed report %s, unexpected number of messages: expected %d, got %d",
Expand Down
4 changes: 2 additions & 2 deletions pkg/reader/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (r *ccipChainReader) ExecutedMessageRanges(
query.LimitAndSort{
SortBy: []query.SortBy{query.NewSortBySequence(query.Asc)},
Limit: query.Limit{
Count: uint64(seqNumRange.End() - seqNumRange.Start() + 1),
Count: uint64(seqNumRange.Length()),
},
},
&dataTyp,
Expand Down Expand Up @@ -332,7 +332,7 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
query.NewSortBySequence(query.Asc),
},
Limit: query.Limit{
Count: uint64(seqNumRange.End() - seqNumRange.Start() + 1),
Count: uint64(seqNumRange.Length()),
},
},
&SendRequestedEvent{},
Expand Down
8 changes: 7 additions & 1 deletion pkg/types/ccipocr3/generic_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s SeqNum) String() string {
}

func NewSeqNumRange(start, end SeqNum) SeqNumRange {
if end < start {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we even allow this to begin with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth about this question. This could generate an error but would require updating >150 spots where we use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they all tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure about that. Shouldn't we error when someone mixes start with the end? We could hide some underlying bug when "healing" that state during runtime

start, end = end, start
}
return SeqNumRange{start, end}
}

Expand Down Expand Up @@ -65,7 +68,7 @@ func (s *SeqNumRange) SetEnd(v SeqNum) {
func (s *SeqNumRange) Limit(n uint64) SeqNumRange {
limitedRange := NewSeqNumRange(s.Start(), s.End())

numElems := s.End() - s.Start() + 1
numElems := s.Length()
if numElems <= 0 {
return limitedRange
}
Expand Down Expand Up @@ -96,6 +99,9 @@ func (s SeqNumRange) String() string {
}

func (s SeqNumRange) Length() int {
if s.End() < s.Start() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is probably not needed considering you've already covered that on the constructor level

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's misleading to shuffle with the state in the read method. If you need a defensive check then probably calling absolute value on the computed length should work, no?

s[0], s[1] = s[1], s[0]
}
return int(s.End() - s.Start() + 1)
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/types/ccipocr3/generic_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func TestSeqNumRange(t *testing.T) {
assert.Equal(t, "[1 -> 2]", NewSeqNumRange(1, 2).String())
assert.Equal(t, "[0 -> 0]", SeqNumRange{}.String())
})

t.Run("end before start", func(t *testing.T) {
assert.Equal(t, NewSeqNumRange(10, 20), NewSeqNumRange(20, 10))
})
}

func TestSeqNumRange_Overlap(t *testing.T) {
Expand Down Expand Up @@ -118,10 +122,10 @@ func TestSeqNumRangeLimit(t *testing.T) {
want: NewSeqNumRange(0, 0),
},
{
name: "wrong range",
name: "wrong range is repaired",
rng: NewSeqNumRange(20, 15),
n: 3,
want: NewSeqNumRange(20, 15),
want: NewSeqNumRange(15, 17),
},
}

Expand Down
Loading