Skip to content

Commit

Permalink
"readability"fix(store): Clearer range check (#227)
Browse files Browse the repository at this point in the history
This PR cleans up a sanity check on the range request in the private
method of store to make it more readable.
  • Loading branch information
renaynay authored Nov 4, 2024
1 parent ee16dec commit a15424a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 2 additions & 4 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ func (s *Store[H]) GetRange(ctx context.Context, from, to uint64) ([]H, error) {
}

func (s *Store[H]) getRangeByHeight(ctx context.Context, from, to uint64) ([]H, error) {
// as the requested range is non-inclusive in the end[from;to), we need to compare
// `from` with `to-1`
if from > to-1 {
return nil, fmt.Errorf("header/store: invalid range(%d,%d)", from, to-1)
if from >= to {
return nil, fmt.Errorf("header/store: invalid range(%d,%d)", from, to)
}
h, err := s.GetByHeight(ctx, to-1)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func TestStore(t *testing.T) {
out, err = store.getRangeByHeight(ctx, 1, 13)
require.NoError(t, err)
assert.Len(t, out, 12)

out, err = store.getRangeByHeight(ctx, 10, 11)
require.NoError(t, err)
assert.Len(t, out, 1)
}

// TestStore_GetRangeByHeight_ExpectedRange
Expand Down

0 comments on commit a15424a

Please sign in to comment.