diff --git a/oracle/pkg/l1Listener/l1Listener.go b/oracle/pkg/l1Listener/l1Listener.go index e590afb2c..8de627281 100644 --- a/oracle/pkg/l1Listener/l1Listener.go +++ b/oracle/pkg/l1Listener/l1Listener.go @@ -22,6 +22,7 @@ type L1Recorder interface { type WinnerRegister interface { RegisterWinner(ctx context.Context, blockNum int64, winner []byte, window int64) error + LastWinnerBlock() (int64, error) } type EthClient interface { @@ -131,8 +132,7 @@ func (l *L1Listener) watchL1Block(ctx context.Context) error { ticker := time.NewTicker(checkInterval) defer ticker.Stop() - // TODO: change it to the store to not miss blocks, if oracle is down - currentBlockNo, err := l.l1Client.BlockNumber(ctx) + currentBlockNo, err := l.winnerRegister.LastWinnerBlock() if err != nil { l.logger.Error("failed to get block number", "error", err) return err @@ -187,7 +187,7 @@ func (l *L1Listener) watchL1Block(ctx context.Context) error { ) } - currentBlockNo = blockNum + currentBlockNo = int64(blockNum) } } } diff --git a/oracle/pkg/l1Listener/l1Listener_test.go b/oracle/pkg/l1Listener/l1Listener_test.go index c85695965..3913ad781 100644 --- a/oracle/pkg/l1Listener/l1Listener_test.go +++ b/oracle/pkg/l1Listener/l1Listener_test.go @@ -8,6 +8,7 @@ import ( "io" "log/slog" "math/big" + "os" "sort" "strings" "sync" @@ -47,7 +48,7 @@ func TestL1Listener(t *testing.T) { } l := l1Listener.NewL1Listener( - slog.New(slog.NewTextHandler(io.Discard, nil)), + slog.New(slog.NewTextHandler(os.Stdout, nil)), ethClient, reg, eventManager, @@ -67,7 +68,7 @@ func TestL1Listener(t *testing.T) { }) select { - case <-time.After(5 * time.Second): + case <-time.After(10 * time.Second): t.Fatal("timeout waiting for winner", i) case update := <-rec.updates: if update.blockNum.Int64() != int64(i) { @@ -157,6 +158,10 @@ func (t *testRegister) RegisterWinner(_ context.Context, blockNum int64, winner return nil } +func (t *testRegister) LastWinnerBlock() (int64, error) { + return 0, nil +} + type testEthClient struct { mu sync.Mutex headers map[uint64]*types.Header diff --git a/oracle/pkg/store/store.go b/oracle/pkg/store/store.go index e8b3e4a8b..ef14aab8a 100644 --- a/oracle/pkg/store/store.go +++ b/oracle/pkg/store/store.go @@ -132,6 +132,21 @@ func (s *Store) GetWinner( return winner, nil } +func (s *Store) LastWinnerBlock() (int64, error) { + var lastBlock sql.NullInt64 + 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, err + } + if !lastBlock.Valid { + return 0, nil + } + return lastBlock.Int64, nil +} + func (s *Store) AddEncryptedCommitment( ctx context.Context, commitmentIdx []byte, diff --git a/oracle/pkg/store/store_test.go b/oracle/pkg/store/store_test.go index 1eeec12b5..e17473c7b 100644 --- a/oracle/pkg/store/store_test.go +++ b/oracle/pkg/store/store_test.go @@ -206,6 +206,20 @@ func TestStore(t *testing.T) { } }) + t.Run("LastWinner", func(t *testing.T) { + st, err := store.NewStore(db) + if err != nil { + t.Fatalf("Failed to create store: %s", err) + } + blockNumber, err := st.LastWinnerBlock() + if err != nil { + t.Fatalf("Failed to get last winner block: %s", err) + } + if blockNumber != winners[1].BlockNumber { + t.Fatalf("Expected last winner block %d, got %d", winners[1].BlockNumber, blockNumber) + } + }) + t.Run("AddEncryptedCommitment", func(t *testing.T) { st, err := store.NewStore(db) if err != nil {