From d4d2c85ac91bd9cc9ee4fce99ff3cbae6a8144af Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 15:30:16 -0800 Subject: [PATCH 1/9] change block signing to use timestamp as key --- prover/db/db.go | 4 ++-- prover/db/db_test.go | 2 +- prover/prover.go | 32 ++++++++++++++++---------------- prover/server/api.go | 22 ++++++++++++++++++---- prover/server/api_test.go | 2 +- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index 1a4707cad..72f71d7e7 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -6,6 +6,6 @@ var ( BlockKeyPrefix = "blockid-" ) -func BuildBlockKey(blockID string) []byte { - return []byte(fmt.Sprintf("%v%v", BlockKeyPrefix, blockID)) +func BuildBlockKey(blockTimestamp uint64) []byte { + return []byte(fmt.Sprintf("%v%v", BlockKeyPrefix, blockTimestamp)) } diff --git a/prover/db/db_test.go b/prover/db/db_test.go index ced512152..6b30ac822 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -7,5 +7,5 @@ import ( ) func Test_BuildBlockKey(t *testing.T) { - assert.Equal(t, BuildBlockKey("1"), []byte("blockid-1")) + assert.Equal(t, BuildBlockKey(1), []byte("blockid-1")) } diff --git a/prover/prover.go b/prover/prover.go index 94e94a32e..110c783fe 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -1263,16 +1263,6 @@ func (p *Prover) signBlock(ctx context.Context, blockID *big.Int) error { log.Info("guardian prover signing block", "blockID", blockID.Uint64()) - exists, err := p.db.Has(db.BuildBlockKey(blockID.String())) - if err != nil { - return err - } - - if exists { - log.Info("guardian prover already signed block", "blockID", blockID.Uint64()) - return nil - } - latest, err := p.rpc.L2.BlockByNumber(ctx, nil) if err != nil { return err @@ -1291,22 +1281,32 @@ func (p *Prover) signBlock(ctx context.Context, blockID *big.Int) error { } } - log.Info("guardian prover block signing caught up", - "latestBlock", latest.Number().Uint64(), - "eventBlockID", blockID.Uint64(), - ) - block, err := p.rpc.L2.BlockByNumber(ctx, blockID) if err != nil { return err } + exists, err := p.db.Has(db.BuildBlockKey(block.Time())) + if err != nil { + return err + } + + if exists { + log.Info("guardian prover already signed block", "blockID", blockID.Uint64()) + return nil + } + + log.Info("guardian prover block signing caught up", + "latestBlock", latest.Number().Uint64(), + "eventBlockID", blockID.Uint64(), + ) + signed, err := crypto.Sign(block.Hash().Bytes(), p.proverPrivateKey) if err != nil { return err } - if err := p.db.Put(db.BuildBlockKey(blockID.String()), signed); err != nil { + if err := p.db.Put(db.BuildBlockKey(block.Time()), signed); err != nil { return err } diff --git a/prover/server/api.go b/prover/server/api.go index e6b158100..6d8ef74a1 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -243,18 +243,32 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { } } - var signedBlocks []SignedBlock + var signedBlocks []SignedBlock = []SignedBlock{} // start iterator at 0 - start := big.NewInt(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() { - start = new(big.Int).Sub(latestBlock.Number(), numBlocksToReturn) + 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() } - iter := srv.db.NewIterator([]byte(db.BlockKeyPrefix), start.Bytes()) + // 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()) defer iter.Release() diff --git a/prover/server/api_test.go b/prover/server/api_test.go index 2af0b576c..261c45101 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -65,7 +65,7 @@ func (s *ProverServerTestSuite) TestGetSignedBlocks() { signed, err := crypto.Sign(latest.Hash().Bytes(), s.s.proverPrivateKey) s.Nil(err) - s.Nil(s.s.db.Put(db.BuildBlockKey(latest.Number().String()), signed)) + s.Nil(s.s.db.Put(db.BuildBlockKey(latest.Time()), signed)) res := s.sendReq("/signedBlocks") s.Equal(http.StatusOK, res.StatusCode) From 8328cec1c3c5f2cfe4b66c300b20b561b2fd875e Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 15:37:48 -0800 Subject: [PATCH 2/9] based rollup branch --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6e5b3c88a..579539709 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,7 +45,7 @@ jobs: with: repository: taikoxyz/taiko-mono path: ${{ env.TAIKO_MONO_DIR }} - ref: based_contestable_zkrollup_improved + ref: based_contestable_zkrollup - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 From 8e03ade95e52f67d495f072b1cf81a19d63dfd69 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 16:11:32 -0800 Subject: [PATCH 3/9] add proper tests --- prover/db/db.go | 12 ++++++++++-- prover/db/db_test.go | 2 +- prover/server/api.go | 31 +++++++------------------------ prover/server/api_test.go | 30 +++++++++++++++++++----------- prover/server/server.go | 4 ---- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index 72f71d7e7..41cc5e5c2 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -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{}) } diff --git a/prover/db/db_test.go b/prover/db/db_test.go index 6b30ac822..900ae520f 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -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)) } diff --git a/prover/server/api.go b/prover/server/api.go index 6d8ef74a1..b5697315d 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -1,6 +1,8 @@ package server import ( + "errors" + "fmt" "math/big" "net/http" "strconv" @@ -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]) diff --git a/prover/server/api_test.go b/prover/server/api_test.go index 261c45101..ba2d3b902 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -1,9 +1,10 @@ package server import ( - "context" "encoding/json" + "fmt" "io" + "math/big" "net/http" "strings" "time" @@ -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) @@ -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)) } diff --git a/prover/server/server.go b/prover/server/server.go index 14552c138..f3540c742 100644 --- a/prover/server/server.go +++ b/prover/server/server.go @@ -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/ From b38fdc34438a879145e78f43698fb98175729bdf Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 16:24:10 -0800 Subject: [PATCH 4/9] block hash was using latest, we should store it as part of the value --- prover/db/db.go | 9 ++++++- prover/db/db_test.go | 8 ++++++ prover/prover.go | 4 ++- prover/server/api.go | 54 +++++++++++++++++++++++++++++---------- prover/server/api_test.go | 4 ++- prover/server/server.go | 4 +++ 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index 41cc5e5c2..a363c46ff 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -10,10 +10,17 @@ var ( ) func BuildBlockKey(blockTimestamp uint64) []byte { - strconv.Itoa(int(blockTimestamp)) return bytes.Join( [][]byte{ []byte(BlockKeyPrefix), []byte(strconv.Itoa(int(blockTimestamp))), }, []byte{}) } + +func BuildBlockValue(hash []byte, signature []byte) []byte { + return bytes.Join( + [][]byte{ + hash, + signature, + }, []byte("-")) +} diff --git a/prover/db/db_test.go b/prover/db/db_test.go index 900ae520f..2e12c833f 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -1,6 +1,7 @@ package db import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -9,3 +10,10 @@ import ( func Test_BuildBlockKey(t *testing.T) { assert.Equal(t, []byte("block-1"), BuildBlockKey(1)) } + +func Test_BuildBlockValue(t *testing.T) { + v := BuildBlockValue([]byte("hash"), []byte("sig")) + spl := strings.Split(string(v), "-") + assert.Equal(t, "hash", spl[0]) + assert.Equal(t, "sig", spl[1]) +} diff --git a/prover/prover.go b/prover/prover.go index 110c783fe..cd17f4c86 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -1306,7 +1306,9 @@ func (p *Prover) signBlock(ctx context.Context, blockID *big.Int) error { return err } - if err := p.db.Put(db.BuildBlockKey(block.Time()), signed); err != nil { + val := db.BuildBlockValue(block.Hash().Bytes(), signed) + + if err := p.db.Put(db.BuildBlockKey(block.Time()), val); err != nil { return err } diff --git a/prover/server/api.go b/prover/server/api.go index b5697315d..062fb347c 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -1,7 +1,7 @@ package server import ( - "errors" + "bytes" "fmt" "math/big" "net/http" @@ -237,25 +237,49 @@ type SignedBlock struct { // @Success 200 {object} []SignedBlock // @Router /signedBlocks [get] func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { - latestBlock, err := srv.rpc.L2.BlockByNumber(c.Request().Context(), nil) - if err != nil { + var signedBlocks []SignedBlock = []SignedBlock{} + + // start iterator at at provided timestamp or 0 if not defined + start := "0" + if c.QueryParam("start") != "" { + start = c.QueryParam("start") + } + + if start == "0" { + latestBlock, err := srv.rpc.L2.BlockByNumber(c.Request().Context(), nil) if err != nil { - log.Error("Failed to get latest L2 block", "error", err) - return echo.NewHTTPError(http.StatusInternalServerError, err) + if err != nil { + log.Error("Failed to get latest L2 block", "error", err) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } } - } - if c.QueryParam("start") == "" { - return echo.NewHTTPError(http.StatusInternalServerError, errors.New("start queryParam is required")) + // 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() > defaultNumBlocksToReturn.Uint64() { + blockNum := new(big.Int).Sub(latestBlock.Number(), defaultNumBlocksToReturn) + 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 = strconv.Itoa(int(block.Time())) + } } - var signedBlocks []SignedBlock = []SignedBlock{} - iter := srv.db.NewIterator([]byte(db.BlockKeyPrefix), []byte(c.QueryParam("start"))) + // 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), []byte(start)) defer iter.Release() for iter.Next() { - fmt.Println(string(iter.Key())) k := strings.Split(string(iter.Key()), "-") blockID, err := strconv.Atoi(k[1]) @@ -264,13 +288,17 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } + v := bytes.Split(iter.Value(), []byte("-")) + signedBlocks = append(signedBlocks, SignedBlock{ BlockID: uint64(blockID), - BlockHash: latestBlock.Hash().Hex(), - Signature: common.Bytes2Hex(iter.Value()), + BlockHash: common.Bytes2Hex(v[0]), + Signature: common.Bytes2Hex(v[1]), Prover: srv.proverAddress, }) } + fmt.Println("done") + return c.JSON(http.StatusOK, signedBlocks) } diff --git a/prover/server/api_test.go b/prover/server/api_test.go index ba2d3b902..7d821c75b 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -71,7 +71,9 @@ func (s *ProverServerTestSuite) TestGetSignedBlocks() { s.Nil(err) key := db.BuildBlockKey(bigInt.Uint64()) - s.Nil(s.s.db.Put(key, signed)) + val := db.BuildBlockValue(common.BigToHash(bigInt).Bytes(), signed) + + s.Nil(s.s.db.Put(key, val)) has, err := s.s.db.Has(key) s.Nil(err) s.True(has) diff --git a/prover/server/server.go b/prover/server/server.go index f3540c742..2b07cbcb0 100644 --- a/prover/server/server.go +++ b/prover/server/server.go @@ -17,6 +17,10 @@ import ( capacity "github.com/taikoxyz/taiko-client/prover/capacity_manager" ) +var ( + defaultNumBlocksToReturn = new(big.Int).SetUint64(100) +) + // @title Taiko Prover API // @version 1.0 // @termsOfService http://swagger.io/terms/ From 53134181d4e9b798bb9e7782e16247f1ad0dac7b Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 16:26:16 -0800 Subject: [PATCH 5/9] comments --- prover/db/db.go | 2 ++ prover/server/api.go | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index a363c46ff..9a4d159dc 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -9,6 +9,7 @@ var ( BlockKeyPrefix = "blockid-" ) +// BuildBlockKey will build a block key for a signed block func BuildBlockKey(blockTimestamp uint64) []byte { return bytes.Join( [][]byte{ @@ -17,6 +18,7 @@ func BuildBlockKey(blockTimestamp uint64) []byte { }, []byte{}) } +// BuildBlockValue will build a block value for a signed block func BuildBlockValue(hash []byte, signature []byte) []byte { return bytes.Join( [][]byte{ diff --git a/prover/server/api.go b/prover/server/api.go index 062fb347c..dde1ca69d 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -245,6 +245,8 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { start = c.QueryParam("start") } + // if no start timestamp was provided, we can get the latest block, and return + // defaultNumBlocksToReturn blocks signed before latest, if our guardian prover has signed them. if start == "0" { latestBlock, err := srv.rpc.L2.BlockByNumber(c.Request().Context(), nil) if err != nil { @@ -271,9 +273,8 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { } } - // 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. + // start should be set to a block timestamp latestBlock-numBlocksToReturn blocks ago if + // a start timestamp was not provided. iter := srv.db.NewIterator([]byte(db.BlockKeyPrefix), []byte(start)) From bc940093e1b77107dcb14dbe9d7c9c31f3ae4e95 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 16:36:12 -0800 Subject: [PATCH 6/9] key --- prover/db/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prover/db/db.go b/prover/db/db.go index 9a4d159dc..f48f4598c 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -6,7 +6,7 @@ import ( ) var ( - BlockKeyPrefix = "blockid-" + BlockKeyPrefix = "block-" ) // BuildBlockKey will build a block key for a signed block From 6f23be583393096ef94f326d60c28d2f58d85b32 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 17:45:45 -0800 Subject: [PATCH 7/9] store block ID in value --- prover/db/db.go | 3 ++- prover/db/db_test.go | 3 ++- prover/prover.go | 2 +- prover/server/api.go | 11 +---------- prover/server/api_test.go | 2 +- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index f48f4598c..5204d0374 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -19,10 +19,11 @@ func BuildBlockKey(blockTimestamp uint64) []byte { } // BuildBlockValue will build a block value for a signed block -func BuildBlockValue(hash []byte, signature []byte) []byte { +func BuildBlockValue(hash []byte, signature []byte, blockID uint64) []byte { return bytes.Join( [][]byte{ hash, signature, + []byte(strconv.Itoa(int(blockID))), }, []byte("-")) } diff --git a/prover/db/db_test.go b/prover/db/db_test.go index 2e12c833f..17f2ed210 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -12,8 +12,9 @@ func Test_BuildBlockKey(t *testing.T) { } func Test_BuildBlockValue(t *testing.T) { - v := BuildBlockValue([]byte("hash"), []byte("sig")) + v := BuildBlockValue([]byte("hash"), []byte("sig"), 1) spl := strings.Split(string(v), "-") assert.Equal(t, "hash", spl[0]) assert.Equal(t, "sig", spl[1]) + assert.Equal(t, "1", spl[2]) } diff --git a/prover/prover.go b/prover/prover.go index cd17f4c86..60f473b2a 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -1306,7 +1306,7 @@ func (p *Prover) signBlock(ctx context.Context, blockID *big.Int) error { return err } - val := db.BuildBlockValue(block.Hash().Bytes(), signed) + val := db.BuildBlockValue(block.Hash().Bytes(), signed, blockID.Uint64()) if err := p.db.Put(db.BuildBlockKey(block.Time()), val); err != nil { return err diff --git a/prover/server/api.go b/prover/server/api.go index dde1ca69d..b3ee810f3 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -6,7 +6,6 @@ import ( "math/big" "net/http" "strconv" - "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -281,18 +280,10 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { defer iter.Release() for iter.Next() { - k := strings.Split(string(iter.Key()), "-") - - blockID, err := strconv.Atoi(k[1]) - - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, err) - } - v := bytes.Split(iter.Value(), []byte("-")) signedBlocks = append(signedBlocks, SignedBlock{ - BlockID: uint64(blockID), + BlockID: new(big.Int).SetBytes(v[2]).Uint64(), BlockHash: common.Bytes2Hex(v[0]), Signature: common.Bytes2Hex(v[1]), Prover: srv.proverAddress, diff --git a/prover/server/api_test.go b/prover/server/api_test.go index 7d821c75b..827321261 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -71,7 +71,7 @@ func (s *ProverServerTestSuite) TestGetSignedBlocks() { s.Nil(err) key := db.BuildBlockKey(bigInt.Uint64()) - val := db.BuildBlockValue(common.BigToHash(bigInt).Bytes(), signed) + val := db.BuildBlockValue(common.BigToHash(bigInt).Bytes(), signed, uint64(i)) s.Nil(s.s.db.Put(key, val)) has, err := s.s.db.Has(key) From 2b3750c377ae2ab094ec36c8b58d723f781e9f16 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 18:10:46 -0800 Subject: [PATCH 8/9] use a function and struct --- prover/db/db.go | 23 +++++++++++++++++++++-- prover/db/db_test.go | 27 +++++++++++++++++++++------ prover/prover.go | 2 +- prover/server/api.go | 9 ++++----- prover/server/api_test.go | 2 +- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/prover/db/db.go b/prover/db/db.go index 5204d0374..a172a0560 100644 --- a/prover/db/db.go +++ b/prover/db/db.go @@ -2,13 +2,22 @@ package db import ( "bytes" + "math/big" "strconv" + + "github.com/ethereum/go-ethereum/common" ) var ( BlockKeyPrefix = "block-" ) +type SignedBlockData struct { + BlockID *big.Int + BlockHash common.Hash + Signature string +} + // BuildBlockKey will build a block key for a signed block func BuildBlockKey(blockTimestamp uint64) []byte { return bytes.Join( @@ -19,11 +28,21 @@ func BuildBlockKey(blockTimestamp uint64) []byte { } // BuildBlockValue will build a block value for a signed block -func BuildBlockValue(hash []byte, signature []byte, blockID uint64) []byte { +func BuildBlockValue(hash []byte, signature []byte, blockID *big.Int) []byte { return bytes.Join( [][]byte{ hash, signature, - []byte(strconv.Itoa(int(blockID))), + blockID.Bytes(), }, []byte("-")) } + +func SignedBlockDataFromValue(val []byte) SignedBlockData { + v := bytes.Split(val, []byte("-")) + + return SignedBlockData{ + BlockID: new(big.Int).SetBytes(v[2]), + BlockHash: common.BytesToHash(v[0]), + Signature: common.Bytes2Hex(v[1]), + } +} diff --git a/prover/db/db_test.go b/prover/db/db_test.go index 17f2ed210..15b6ccbf4 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -1,9 +1,11 @@ package db import ( - "strings" + "bytes" + "math/big" "testing" + "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" ) @@ -12,9 +14,22 @@ func Test_BuildBlockKey(t *testing.T) { } func Test_BuildBlockValue(t *testing.T) { - v := BuildBlockValue([]byte("hash"), []byte("sig"), 1) - spl := strings.Split(string(v), "-") - assert.Equal(t, "hash", spl[0]) - assert.Equal(t, "sig", spl[1]) - assert.Equal(t, "1", spl[2]) + v := BuildBlockValue([]byte("hash"), []byte("sig"), big.NewInt(1)) + spl := bytes.Split(v, []byte("-")) + assert.Equal(t, "hash", string(spl[0])) + assert.Equal(t, "sig", string(spl[1])) + assert.Equal(t, uint64(1), new(big.Int).SetBytes(spl[2]).Uint64()) +} + +func Test_SignedBlockDataFromValue(t *testing.T) { + hash := common.HexToHash("0x1ada5c5ba58cfca1fbcd4531f4132f8cfef736c2cf40209a1315c489717dfc49") + // nolint: lll + sig := common.Hex2Bytes("789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301") + + v := BuildBlockValue(hash.Bytes(), sig, big.NewInt(1)) + data := SignedBlockDataFromValue(v) + + assert.Equal(t, common.Bytes2Hex(sig), data.Signature) + assert.Equal(t, hash, data.BlockHash) + assert.Equal(t, data.BlockID, big.NewInt(1)) } diff --git a/prover/prover.go b/prover/prover.go index 60f473b2a..6e41424cc 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -1306,7 +1306,7 @@ func (p *Prover) signBlock(ctx context.Context, blockID *big.Int) error { return err } - val := db.BuildBlockValue(block.Hash().Bytes(), signed, blockID.Uint64()) + val := db.BuildBlockValue(block.Hash().Bytes(), signed, blockID) if err := p.db.Put(db.BuildBlockKey(block.Time()), val); err != nil { return err diff --git a/prover/server/api.go b/prover/server/api.go index b3ee810f3..6a3b380e6 100644 --- a/prover/server/api.go +++ b/prover/server/api.go @@ -1,7 +1,6 @@ package server import ( - "bytes" "fmt" "math/big" "net/http" @@ -280,12 +279,12 @@ func (srv *ProverServer) GetSignedBlocks(c echo.Context) error { defer iter.Release() for iter.Next() { - v := bytes.Split(iter.Value(), []byte("-")) + signedblockData := db.SignedBlockDataFromValue(iter.Value()) signedBlocks = append(signedBlocks, SignedBlock{ - BlockID: new(big.Int).SetBytes(v[2]).Uint64(), - BlockHash: common.Bytes2Hex(v[0]), - Signature: common.Bytes2Hex(v[1]), + BlockID: signedblockData.BlockID.Uint64(), + BlockHash: signedblockData.BlockHash.Hex(), + Signature: signedblockData.Signature, Prover: srv.proverAddress, }) } diff --git a/prover/server/api_test.go b/prover/server/api_test.go index 827321261..9fc6e31d1 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -71,7 +71,7 @@ func (s *ProverServerTestSuite) TestGetSignedBlocks() { s.Nil(err) key := db.BuildBlockKey(bigInt.Uint64()) - val := db.BuildBlockValue(common.BigToHash(bigInt).Bytes(), signed, uint64(i)) + val := db.BuildBlockValue(common.BigToHash(bigInt).Bytes(), signed, big.NewInt(1)) s.Nil(s.s.db.Put(key, val)) has, err := s.s.db.Has(key) From 8c5286e8074607415fbf91f9bb30d7b248f716da Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Wed, 6 Dec 2023 18:11:59 -0800 Subject: [PATCH 9/9] rm 0x --- prover/db/db_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prover/db/db_test.go b/prover/db/db_test.go index 15b6ccbf4..f7cb80509 100644 --- a/prover/db/db_test.go +++ b/prover/db/db_test.go @@ -22,7 +22,7 @@ func Test_BuildBlockValue(t *testing.T) { } func Test_SignedBlockDataFromValue(t *testing.T) { - hash := common.HexToHash("0x1ada5c5ba58cfca1fbcd4531f4132f8cfef736c2cf40209a1315c489717dfc49") + hash := common.HexToHash("1ada5c5ba58cfca1fbcd4531f4132f8cfef736c2cf40209a1315c489717dfc49") // nolint: lll sig := common.Hex2Bytes("789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301")