Skip to content

Commit

Permalink
fix: winner test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jul 8, 2024
1 parent 83b9089 commit 49555a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions oracle/pkg/l1Listener/l1Listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -187,7 +187,7 @@ func (l *L1Listener) watchL1Block(ctx context.Context) error {
)
}

currentBlockNo = blockNum
currentBlockNo = int64(blockNum)
}
}
}
9 changes: 7 additions & 2 deletions oracle/pkg/l1Listener/l1Listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log/slog"
"math/big"
"os"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions oracle/pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions oracle/pkg/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 49555a9

Please sign in to comment.