diff --git a/oracle/pkg/l1Listener/l1Listener.go b/oracle/pkg/l1Listener/l1Listener.go index 8de627281..c15b6b9c3 100644 --- a/oracle/pkg/l1Listener/l1Listener.go +++ b/oracle/pkg/l1Listener/l1Listener.go @@ -3,12 +3,14 @@ package l1Listener import ( "bytes" "context" + "errors" "log/slog" "math/big" "time" "github.com/ethereum/go-ethereum/core/types" blocktracker "github.com/primev/mev-commit/contracts-abi/clients/BlockTracker" + "github.com/primev/mev-commit/oracle/pkg/store" "github.com/primev/mev-commit/x/contracts/events" "github.com/prometheus/client_golang/prometheus" "golang.org/x/sync/errgroup" @@ -134,8 +136,18 @@ func (l *L1Listener) watchL1Block(ctx context.Context) error { currentBlockNo, err := l.winnerRegister.LastWinnerBlock() if err != nil { - l.logger.Error("failed to get block number", "error", err) - return err + // this is a fresh start, so start from the current block + if errors.Is(err, store.ErrNotFound) { + tip, err := l.l1Client.BlockNumber(ctx) + if err != nil { + l.logger.Error("failed to get current block number", "error", err) + return err + } + currentBlockNo = int64(tip) + } else { + l.logger.Error("failed to get last winner block", "error", err) + return err + } } for { select { diff --git a/oracle/pkg/store/store.go b/oracle/pkg/store/store.go index ef14aab8a..ad5b280c0 100644 --- a/oracle/pkg/store/store.go +++ b/oracle/pkg/store/store.go @@ -66,6 +66,8 @@ CREATE TABLE IF NOT EXISTS integers ( value BIGINT );` +var ErrNotFound = fmt.Errorf("not found") + type Store struct { db *sql.DB } @@ -137,7 +139,7 @@ func (s *Store) LastWinnerBlock() (int64, error) { err := s.db.QueryRow("SELECT block_number FROM winners ORDER BY block_number DESC LIMIT 1").Scan(&lastBlock) if err != nil { if err == sql.ErrNoRows { - return 0, nil + return 0, ErrNotFound } return 0, err }