Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
add proper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Dec 7, 2023
1 parent 8328cec commit 8e03ade
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
12 changes: 10 additions & 2 deletions prover/db/db.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package db

import "fmt"
import (
"bytes"
"strconv"
)

var (
BlockKeyPrefix = "blockid-"
)

func BuildBlockKey(blockTimestamp uint64) []byte {
return []byte(fmt.Sprintf("%v%v", BlockKeyPrefix, blockTimestamp))
strconv.Itoa(int(blockTimestamp))
return bytes.Join(
[][]byte{
[]byte(BlockKeyPrefix),
[]byte(strconv.Itoa(int(blockTimestamp))),
}, []byte{})
}
2 changes: 1 addition & 1 deletion prover/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func Test_BuildBlockKey(t *testing.T) {
assert.Equal(t, BuildBlockKey(1), []byte("blockid-1"))
assert.Equal(t, []byte("block-1"), BuildBlockKey(1))
}
31 changes: 7 additions & 24 deletions prover/server/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"errors"
"fmt"
"math/big"
"net/http"
"strconv"
Expand Down Expand Up @@ -243,36 +245,17 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error {
}
}

var signedBlocks []SignedBlock = []SignedBlock{}

// start iterator at 0
var start uint64 = 0

// if latestBlock is greater than the number of blocks to return, we only want to return
// the most recent N blocks signed by this guardian prover.
if latestBlock.NumberU64() > numBlocksToReturn.Uint64() {
blockNum := new(big.Int).Sub(latestBlock.Number(), numBlocksToReturn)
block, err := srv.rpc.L2.BlockByNumber(
c.Request().Context(),
blockNum,
)
if err != nil {
log.Error("Failed to get L2 block", "error", err, "blockNum", blockNum)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

start = block.Time()
if c.QueryParam("start") == "" {
return echo.NewHTTPError(http.StatusInternalServerError, errors.New("start queryParam is required"))
}
var signedBlocks []SignedBlock = []SignedBlock{}

// start should be set to a block timestamp latestBlock-numBlocksToReturn blocks ago.
// so when we iterate, we should only be seeing numBlocksToReturn amount of blocks being pulled
// from the database and returned.

iter := srv.db.NewIterator([]byte(db.BlockKeyPrefix), new(big.Int).SetUint64(start).Bytes())
iter := srv.db.NewIterator([]byte(db.BlockKeyPrefix), []byte(c.QueryParam("start")))

defer iter.Release()

for iter.Next() {
fmt.Println(string(iter.Key()))
k := strings.Split(string(iter.Key()), "-")

blockID, err := strconv.Atoi(k[1])
Expand Down
30 changes: 19 additions & 11 deletions prover/server/api_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package server

import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -59,14 +60,24 @@ func (s *ProverServerTestSuite) TestProposeBlockSuccess() {
}

func (s *ProverServerTestSuite) TestGetSignedBlocks() {
latest, err := s.s.rpc.L2.BlockByNumber(context.Background(), nil)
s.Nil(err)
var start uint64
// create 200, we only expect to get 100 back.
for i := 0; i < 200; i++ {
bigInt := big.NewInt(time.Now().UnixNano())
if i == 100 {
start = bigInt.Uint64()
}
signed, err := crypto.Sign(common.BigToHash(bigInt).Bytes(), s.s.proverPrivateKey)
s.Nil(err)
key := db.BuildBlockKey(bigInt.Uint64())

signed, err := crypto.Sign(latest.Hash().Bytes(), s.s.proverPrivateKey)
s.Nil(err)
s.Nil(s.s.db.Put(key, signed))
has, err := s.s.db.Has(key)
s.Nil(err)
s.True(has)
}

s.Nil(s.s.db.Put(db.BuildBlockKey(latest.Time()), signed))
res := s.sendReq("/signedBlocks")
res := s.sendReq(fmt.Sprintf("/signedBlocks?start=%v", start))
s.Equal(http.StatusOK, res.StatusCode)

signedBlocks := make([]SignedBlock, 0)
Expand All @@ -76,8 +87,5 @@ func (s *ProverServerTestSuite) TestGetSignedBlocks() {
s.Nil(err)
s.Nil(json.Unmarshal(b, &signedBlocks))

s.Equal(1, len(signedBlocks))
s.Equal(latest.Hash().Hex(), signedBlocks[0].BlockHash)
s.Equal(latest.Number().Uint64(), signedBlocks[0].BlockID)
s.Equal(common.Bytes2Hex(signed), signedBlocks[0].Signature)
s.Equal(100, len(signedBlocks))
}
4 changes: 0 additions & 4 deletions prover/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
capacity "github.com/taikoxyz/taiko-client/prover/capacity_manager"
)

var (
numBlocksToReturn = new(big.Int).SetUint64(200)
)

// @title Taiko Prover API
// @version 1.0
// @termsOfService http://swagger.io/terms/
Expand Down

0 comments on commit 8e03ade

Please sign in to comment.