diff --git a/chain/events/filter/index.go b/chain/events/filter/index.go index 2b316e9008d..4d7de5185f4 100644 --- a/chain/events/filter/index.go +++ b/chain/events/filter/index.go @@ -100,6 +100,7 @@ const ( upsertEventsSeen = `INSERT INTO events_seen(height, tipset_key_cid, reverted) VALUES(?, ?, ?) ON CONFLICT(height, tipset_key_cid) DO UPDATE SET reverted=false` isTipsetProcessed = `SELECT COUNT(*) > 0 FROM events_seen WHERE tipset_key_cid=?` getMaxHeightInIndex = `SELECT MAX(height) FROM events_seen` + isHeightProcessed = `SELECT COUNT(*) > 0 FROM events_seen WHERE height=?` createIndexEventEmitterAddr = `CREATE INDEX IF NOT EXISTS event_emitter_addr ON event (emitter_addr)` createIndexEventTipsetKeyCid = `CREATE INDEX IF NOT EXISTS event_tipset_key_cid ON event (tipset_key_cid);` @@ -136,6 +137,7 @@ type EventIndex struct { stmtIsTipsetProcessed *sql.Stmt stmtGetMaxHeightInIndex *sql.Stmt + stmtIsHeightProcessed *sql.Stmt mu sync.Mutex subIdCounter uint64 @@ -201,6 +203,11 @@ func (ei *EventIndex) initStatements() (err error) { return xerrors.Errorf("prepare getMaxHeightInIndex: %w", err) } + ei.stmtIsHeightProcessed, err = ei.db.Prepare(isHeightProcessed) + if err != nil { + return xerrors.Errorf("prepare isHeightProcessed: %w", err) + } + return nil } @@ -688,11 +695,10 @@ func (ei *EventIndex) GetMaxHeightInIndex(ctx context.Context) (uint64, error) { } func (ei *EventIndex) IsHeightProcessed(ctx context.Context, height uint64) (bool, error) { - mh, err := ei.GetMaxHeightInIndex(ctx) - if err != nil { - return false, err - } - return height <= mh, nil + row := ei.stmtIsHeightProcessed.QueryRowContext(ctx, height) + var exists bool + err := row.Scan(&exists) + return exists, err } func (ei *EventIndex) IsTipsetProcessed(ctx context.Context, tipsetKeyCid []byte) (bool, error) { diff --git a/chain/events/filter/index_test.go b/chain/events/filter/index_test.go index b9e5c49a05c..10b3eb57779 100644 --- a/chain/events/filter/index_test.go +++ b/chain/events/filter/index_test.go @@ -102,6 +102,10 @@ func TestEventIndexPrefillFilter(t *testing.T) { require.NoError(t, err) require.False(t, b) + b, err = ei.IsHeightProcessed(context.Background(), 13000) + require.NoError(t, err) + require.False(t, b) + tsKey := events14000.msgTs.Key() tsKeyCid, err := tsKey.Cid() require.NoError(t, err, "tipset key cid") diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index c85b04901b2..3cbffe84a2d 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -1286,6 +1286,14 @@ func (e *EthEventHandler) EthGetLogs(ctx context.Context, filterSpec *ethtypes.E return nil, err } + // should also have the minHeight in the filter indexed + b, err := e.EventFilterManager.EventIndex.IsHeightProcessed(ctx, uint64(pf.minHeight)) + if err != nil { + return nil, xerrors.Errorf("failed to check if event index has events for the minheight: %w", err) + } + if !b { + return nil, xerrors.Errorf("event index does not have event for epoch %d", pf.minHeight) + } } else { ts, err := e.Chain.GetTipSetByCid(ctx, pf.tipsetCid) if err != nil {