Skip to content

Commit

Permalink
fix: initial block in l1 listener oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jul 8, 2024
1 parent 5906630 commit 0c642e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 14 additions & 2 deletions oracle/pkg/l1Listener/l1Listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion oracle/pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ CREATE TABLE IF NOT EXISTS integers (
value BIGINT
);`

var ErrNotFound = fmt.Errorf("not found")

type Store struct {
db *sql.DB
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0c642e7

Please sign in to comment.