Skip to content

Commit

Permalink
checkpoint: fix response
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikspatil024 committed Jan 20, 2025
1 parent 46b1b62 commit 6a2f587
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
14 changes: 3 additions & 11 deletions consensus/bor/heimdall/checkpoint/checkpoint.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package checkpoint

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
)

// Checkpoint defines a response object type of bor checkpoint
type Checkpoint struct {
Proposer common.Address `json:"proposer"`
StartBlock *big.Int `json:"start_block"`
EndBlock *big.Int `json:"end_block"`
StartBlock uint64 `json:"start_block"`
EndBlock uint64 `json:"end_block"`
RootHash common.Hash `json:"root_hash"`
BorChainID string `json:"bor_chain_id"`
Timestamp uint64 `json:"timestamp"`
}

type CheckpointResponse struct {
Height string `json:"height"`
Result Checkpoint `json:"result"`
}

type CheckpointCount struct {
Result int64 `json:"result"`
}

type CheckpointCountResponse struct {
Height string `json:"height"`
Result CheckpointCount `json:"result"`
Result int64 `json:"result"`
}
4 changes: 2 additions & 2 deletions consensus/bor/heimdall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (h *HeimdallClient) FetchCheckpoint(ctx context.Context, number int64) (*ch
return &response.Result, nil
}

// FetchMilestone fetches the checkpoint from heimdall
// FetchMilestone fetches the milestone from heimdall
func (h *HeimdallClient) FetchMilestone(ctx context.Context) (*milestone.Milestone, error) {
url, err := milestoneURL(h.urlString)
if err != nil {
Expand Down Expand Up @@ -210,7 +210,7 @@ func (h *HeimdallClient) FetchCheckpointCount(ctx context.Context) (int64, error
return 0, err
}

return response.Result.Result, nil
return response.Result, nil
}

// FetchMilestoneCount fetches the milestone count from heimdall
Expand Down
11 changes: 4 additions & 7 deletions consensus/bor/heimdall/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -118,11 +117,10 @@ func TestFetchCheckpointFromMockHeimdall(t *testing.T) {
handler := &HttpHandlerFake{}
handler.handleFetchCheckpoint = func(w http.ResponseWriter, _ *http.Request) {
err := json.NewEncoder(w).Encode(checkpoint.CheckpointResponse{
Height: "0",
Result: checkpoint.Checkpoint{
Proposer: common.Address{},
StartBlock: big.NewInt(0),
EndBlock: big.NewInt(512),
StartBlock: 0,
EndBlock: 512,
RootHash: common.Hash{},
BorChainID: "15001",
Timestamp: 0,
Expand Down Expand Up @@ -224,11 +222,10 @@ func TestFetchShutdown(t *testing.T) {
time.Sleep(100 * time.Millisecond)

err := json.NewEncoder(w).Encode(checkpoint.CheckpointResponse{
Height: "0",
Result: checkpoint.Checkpoint{
Proposer: common.Address{},
StartBlock: big.NewInt(0),
EndBlock: big.NewInt(512),
StartBlock: 0,
EndBlock: 512,
RootHash: common.Hash{},
BorChainID: "15001",
Timestamp: 0,
Expand Down
5 changes: 2 additions & 3 deletions consensus/bor/heimdallapp/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package heimdallapp

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
Expand Down Expand Up @@ -40,8 +39,8 @@ func (h *HeimdallAppClient) FetchCheckpoint(_ context.Context, number int64) (*c
func toBorCheckpoint(hdCheckpoint hmTypes.Checkpoint) *checkpoint.Checkpoint {
return &checkpoint.Checkpoint{
Proposer: common.HexToAddress(hdCheckpoint.Proposer),
StartBlock: big.NewInt(int64(hdCheckpoint.StartBlock)),
EndBlock: big.NewInt(int64(hdCheckpoint.EndBlock)),
StartBlock: hdCheckpoint.StartBlock,
EndBlock: hdCheckpoint.EndBlock,
RootHash: common.BytesToHash(hdCheckpoint.RootHash),
BorChainID: hdCheckpoint.BorChainId,
Timestamp: hdCheckpoint.Timestamp,
Expand Down
5 changes: 2 additions & 3 deletions consensus/bor/heimdallgrpc/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package heimdallgrpc

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -39,8 +38,8 @@ func (h *HeimdallGRPCClient) FetchCheckpoint(ctx context.Context, number int64)
log.Info("Fetched checkpoint", "number", number)

checkpoint := &checkpoint.Checkpoint{
StartBlock: new(big.Int).SetUint64(res.Result.StartBlock),
EndBlock: new(big.Int).SetUint64(res.Result.EndBlock),
StartBlock: res.Result.StartBlock,
EndBlock: res.Result.EndBlock,
RootHash: protoutils.ConvertH256ToHash(res.Result.RootHash),
Proposer: protoutils.ConvertH160toAddress(res.Result.Proposer),
BorChainID: res.Result.BorChainID,
Expand Down
6 changes: 3 additions & 3 deletions eth/handler_bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func (h *ethHandler) fetchWhitelistCheckpoint(ctx context.Context, bor *bor.Bor,
return blockNum, blockHash, err
}

log.Debug("Got new checkpoint from heimdall", "start", checkpoint.StartBlock.Uint64(), "end", checkpoint.EndBlock.Uint64(), "rootHash", checkpoint.RootHash.String())
log.Debug("Got new checkpoint from heimdall", "start", checkpoint.StartBlock, "end", checkpoint.EndBlock, "rootHash", checkpoint.RootHash.String())

// Verify if the checkpoint fetched can be added to the local whitelist entry or not
// If verified, it returns the hash of the end block of the checkpoint. If not,
// it will return appropriate error.
hash, err := verifier.verify(ctx, eth, h, checkpoint.StartBlock.Uint64(), checkpoint.EndBlock.Uint64(), checkpoint.RootHash.String()[2:], true)
hash, err := verifier.verify(ctx, eth, h, checkpoint.StartBlock, checkpoint.EndBlock, checkpoint.RootHash.String()[2:], true)
if err != nil {
if errors.Is(err, errChainOutOfSync) {
log.Info("Whitelisting checkpoint deferred", "err", err)
Expand All @@ -52,7 +52,7 @@ func (h *ethHandler) fetchWhitelistCheckpoint(ctx context.Context, bor *bor.Bor,
return blockNum, blockHash, err
}

blockNum = checkpoint.EndBlock.Uint64()
blockNum = checkpoint.EndBlock
blockHash = common.HexToHash(hash)

return blockNum, blockHash, nil
Expand Down
9 changes: 4 additions & 5 deletions eth/handler_bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eth

import (
"context"
"math/big"
"testing"
"time"

Expand Down Expand Up @@ -118,7 +117,7 @@ func fetchCheckpointTest(t *testing.T, heimdall *mockHeimdall, bor *bor.Bor, han

// Check if we have expected result
require.Equal(t, err, nil)
require.Equal(t, checkpoints[len(checkpoints)-1].EndBlock.Uint64(), blockNum)
require.Equal(t, checkpoints[len(checkpoints)-1].EndBlock, blockNum)
require.Equal(t, checkpoints[len(checkpoints)-1].RootHash, blockHash)
}

Expand Down Expand Up @@ -155,14 +154,14 @@ func fetchMilestoneTest(t *testing.T, heimdall *mockHeimdall, bor *bor.Bor, hand
func createMockCheckpoints(count int) []*checkpoint.Checkpoint {
var (
checkpoints []*checkpoint.Checkpoint = make([]*checkpoint.Checkpoint, count)
startBlock int64 = 257 // any number can be used
startBlock uint64 = 257 // any number can be used
)

for i := 0; i < count; i++ {
checkpoints[i] = &checkpoint.Checkpoint{
Proposer: common.Address{},
StartBlock: big.NewInt(startBlock),
EndBlock: big.NewInt(startBlock + 255),
StartBlock: startBlock,
EndBlock: startBlock + 255,
RootHash: common.Hash{},
BorChainID: "137",
Timestamp: uint64(time.Now().Unix()),
Expand Down

0 comments on commit 6a2f587

Please sign in to comment.