Skip to content

Commit 2ead861

Browse files
authored
fix(eth): minor improvements to event range checking (#12867)
1 parent 94da734 commit 2ead861

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

chain/index/events.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ import (
2222
"github.com/filecoin-project/lotus/chain/types"
2323
)
2424

25+
const maxLookBackForWait = 120 // one hour of tipsets
26+
2527
var (
2628
ErrMaxResultsReached = xerrors.New("filter matches too many events, try a more restricted filter")
27-
ErrRangeInFuture = xerrors.New("range end is in the future")
2829
)
2930

30-
const maxLookBackForWait = 120 // one hour of tipsets
31+
type ErrRangeInFuture struct {
32+
HighestEpoch int
33+
}
34+
35+
func (e *ErrRangeInFuture) Error() string {
36+
return fmt.Sprintf("range end is in the future, highest epoch: %d", e.HighestEpoch)
37+
}
38+
39+
func (e *ErrRangeInFuture) Is(target error) bool {
40+
_, ok := target.(*ErrRangeInFuture)
41+
return ok
42+
}
3143

3244
type executedMessage struct {
3345
msg types.ChainMsg
@@ -287,7 +299,7 @@ func (si *SqliteIndexer) checkFilterTipsetsIndexed(ctx context.Context, f *Event
287299
func (si *SqliteIndexer) checkRangeIndexedStatus(ctx context.Context, minHeight abi.ChainEpoch, maxHeight abi.ChainEpoch) error {
288300
head := si.cs.GetHeaviestTipSet()
289301
if minHeight > head.Height() || maxHeight > head.Height() {
290-
return ErrRangeInFuture
302+
return &ErrRangeInFuture{HighestEpoch: int(head.Height())}
291303
}
292304

293305
// Find the first non-null round in the range
@@ -305,7 +317,7 @@ func (si *SqliteIndexer) checkRangeIndexedStatus(ctx context.Context, minHeight
305317
if err != nil {
306318
return xerrors.Errorf("failed to find last non-null round: %w", err)
307319
}
308-
// If all rounds are null, consider the range valid
320+
// We should have already rulled out all rounds being null in the startCid check
309321
if endCid == nil {
310322
return xerrors.Errorf("unexpected error finding last non-null round: all rounds are null but start round is not (%d to %d)", minHeight, maxHeight)
311323
}
@@ -344,8 +356,7 @@ func (si *SqliteIndexer) findFirstNonNullRound(ctx context.Context, minHeight ab
344356
// else null round, keep searching
345357
continue
346358
}
347-
minHeight = height // Update the minHeight to the found height
348-
return cid, minHeight, nil
359+
return cid, height, nil
349360
}
350361
// All rounds are null
351362
return nil, 0, nil
@@ -356,14 +367,13 @@ func (si *SqliteIndexer) findLastNonNullRound(ctx context.Context, maxHeight abi
356367
for height := maxHeight; height >= minHeight; height-- {
357368
cid, err := si.getTipsetKeyCidByHeight(ctx, height)
358369
if err == nil {
359-
maxHeight = height // Update the maxHeight to the found height
360-
return cid, maxHeight, nil
370+
return cid, height, nil
361371
}
362372
if !errors.Is(err, ErrNotFound) {
363373
return nil, 0, xerrors.Errorf("failed to get tipset key cid for height %d: %w", height, err)
364374
}
365375
}
366-
376+
// All rounds are null
367377
return nil, 0, nil
368378
}
369379

chain/index/events_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestGetEventsForFilterNoEvents(t *testing.T) {
8989
MaxHeight: 200,
9090
}
9191
ces, err = si.GetEventsForFilter(ctx, f)
92-
require.ErrorIs(t, err, ErrRangeInFuture)
92+
require.ErrorIs(t, err, &ErrRangeInFuture{})
9393
require.Equal(t, 0, len(ces))
9494

9595
// search for a range (start too) that is in the future
@@ -98,7 +98,7 @@ func TestGetEventsForFilterNoEvents(t *testing.T) {
9898
MaxHeight: 200,
9999
}
100100
ces, err = si.GetEventsForFilter(ctx, f)
101-
require.ErrorIs(t, err, ErrRangeInFuture)
101+
require.ErrorIs(t, err, &ErrRangeInFuture{})
102102
require.Equal(t, 0, len(ces))
103103
}
104104

0 commit comments

Comments
 (0)