Skip to content

Commit

Permalink
Merge pull request #1399 from maticnetwork/raneet10/heimdallv2-change…
Browse files Browse the repository at this point in the history
…s-request-responses-fixes

Fix responses and parsing for milestone related requests.
  • Loading branch information
avalkov authored Jan 7, 2025
2 parents d372b13 + b0e57c5 commit 46b1b62
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 42 deletions.
8 changes: 4 additions & 4 deletions consensus/bor/heimdall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ const (
fetchMilestone = "/milestone/latest"
fetchMilestoneCount = "/milestone/count"

fetchLastNoAckMilestone = "/milestone/lastNoAck"
fetchNoAckMilestone = "/milestone/noAck/%s"
fetchLastNoAckMilestone = "/milestone/last-no-ack"
fetchNoAckMilestone = "/milestone/no-ack/%s"
fetchMilestoneID = "/milestone/ID/%s"

fetchSpanFormat = "bor/span/%d"
Expand Down Expand Up @@ -244,7 +244,7 @@ func (h *HeimdallClient) FetchLastNoAckMilestone(ctx context.Context) (string, e
return "", err
}

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

// FetchNoAckMilestone fetches the last no-ack-milestone from heimdall
Expand All @@ -261,7 +261,7 @@ func (h *HeimdallClient) FetchNoAckMilestone(ctx context.Context, milestoneID st
return err
}

if !response.Result.Result {
if !response.Result {
return fmt.Errorf("%w: milestoneID %q", ErrNotInRejectedList, milestoneID)
}

Expand Down
5 changes: 2 additions & 3 deletions consensus/bor/heimdall/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ func TestFetchMilestoneFromMockHeimdall(t *testing.T) {
handler := &HttpHandlerFake{}
handler.handleFetchMilestone = func(w http.ResponseWriter, _ *http.Request) {
err := json.NewEncoder(w).Encode(milestone.MilestoneResponse{
Height: "0",
Result: milestone.Milestone{
Proposer: common.Address{},
StartBlock: big.NewInt(0),
EndBlock: big.NewInt(512),
StartBlock: 0,
EndBlock: 512,
Hash: common.Hash{},
BorChainID: "15001",
Timestamp: 0,
Expand Down
78 changes: 57 additions & 21 deletions consensus/bor/heimdall/milestone/milestone.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
package milestone

import (
"math/big"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"

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

// milestone defines a response object type of bor milestone
type Milestone struct {
Proposer common.Address `json:"proposer"`
StartBlock *big.Int `json:"start_block"`
EndBlock *big.Int `json:"end_block"`
Hash common.Hash `json:"hash"`
BorChainID string `json:"bor_chain_id"`
Timestamp uint64 `json:"timestamp"`
Proposer common.Address `json:"proposer"`
StartBlock uint64 `json:"start_block"`
EndBlock uint64 `json:"end_block"`
Hash common.Hash `json:"hash"`
BorChainID string `json:"bor_chain_id"`
MilestoneID string `json:"milestone_id"`
Timestamp uint64 `json:"timestamp"`
}

func (m *Milestone) UnmarshalJSON(data []byte) error {
type Alias Milestone
temp := &struct {
StartBlock string `json:"start_block"`
EndBlock string `json:"end_block"`
Hash string `json:"hash"`
Timestamp string `json:"timestamp"`
*Alias
}{
Alias: (*Alias)(m),
}

if err := json.Unmarshal(data, temp); err != nil {
return err
}

startBlock, err := strconv.ParseUint(temp.StartBlock, 10, 64)
if err != nil {
return fmt.Errorf("invalid start_block: %w", err)
}
m.StartBlock = startBlock

endBlock, err := strconv.ParseUint(temp.EndBlock, 10, 64)
if err != nil {
return fmt.Errorf("invalid end_block: %w", err)
}
m.EndBlock = endBlock

decodedHash, err := base64.StdEncoding.DecodeString(temp.Hash)
if err != nil {
return fmt.Errorf("failed to decode hash: %w", err)
}
m.Hash = common.BytesToHash(decodedHash)

timestamp, err := strconv.ParseUint(temp.Timestamp, 10, 64)
if err != nil {
return fmt.Errorf("invalid timestamp: %w", err)
}
m.Timestamp = timestamp

return nil
}

type MilestoneResponse struct {
Height string `json:"height"`
Result Milestone `json:"result"`
Result Milestone `json:"milestone"`
}

type MilestoneCount struct {
Expand All @@ -30,22 +76,12 @@ type MilestoneCountResponse struct {
Result MilestoneCount `json:"result"`
}

type MilestoneLastNoAck struct {
Result string `json:"result"`
}

type MilestoneLastNoAckResponse struct {
Height string `json:"height"`
Result MilestoneLastNoAck `json:"result"`
}

type MilestoneNoAck struct {
Result bool `json:"result"`
Result string `json:"result"`
}

type MilestoneNoAckResponse struct {
Height string `json:"height"`
Result MilestoneNoAck `json:"result"`
Result bool `json:"result"`
}

type MilestoneID struct {
Expand Down
5 changes: 2 additions & 3 deletions consensus/bor/heimdallapp/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package heimdallapp
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/milestone"
Expand Down Expand Up @@ -85,8 +84,8 @@ func (h *HeimdallAppClient) FetchLastNoAckMilestone(_ context.Context) (string,
func toBorMilestone(hdMilestone *milestoneTypes.Milestone) *milestone.Milestone {
return &milestone.Milestone{
Proposer: common.HexToAddress(hdMilestone.Proposer),
StartBlock: big.NewInt(int64(hdMilestone.StartBlock)),
EndBlock: big.NewInt(int64(hdMilestone.EndBlock)),
StartBlock: hdMilestone.StartBlock,
EndBlock: hdMilestone.EndBlock,
Hash: common.BytesToHash(hdMilestone.Hash),
BorChainID: hdMilestone.BorChainId,
Timestamp: hdMilestone.Timestamp,
Expand Down
5 changes: 2 additions & 3 deletions consensus/bor/heimdallgrpc/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package heimdallgrpc
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/consensus/bor/heimdall"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/milestone"
Expand Down Expand Up @@ -37,8 +36,8 @@ func (h *HeimdallGRPCClient) FetchMilestone(ctx context.Context) (*milestone.Mil
log.Info("Fetched milestone")

milestone := &milestone.Milestone{
StartBlock: new(big.Int).SetUint64(res.Result.StartBlock),
EndBlock: new(big.Int).SetUint64(res.Result.EndBlock),
StartBlock: res.Result.StartBlock,
EndBlock: res.Result.EndBlock,
Hash: protoutils.ConvertH256ToHash(res.Result.RootHash),
Proposer: protoutils.ConvertH160toAddress(res.Result.Proposer),
BorChainID: res.Result.BorChainID,
Expand Down
8 changes: 4 additions & 4 deletions eth/handler_bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ func (h *ethHandler) fetchWhitelistMilestone(ctx context.Context, bor *bor.Bor,
return num, hash, err
}

num = milestone.EndBlock.Uint64()
num = milestone.EndBlock
hash = milestone.Hash

log.Debug("Got new milestone from heimdall", "start", milestone.StartBlock.Uint64(), "end", milestone.EndBlock.Uint64(), "hash", milestone.Hash.String())
log.Debug("Got new milestone from heimdall", "start", milestone.StartBlock, "end", milestone.EndBlock, "hash", milestone.Hash.String())

// Verify if the milestone fetched can be added to the local whitelist entry or not. If verified,
// the hash of the end block of the milestone is returned else appropriate error is returned.
_, err = verifier.verify(ctx, eth, h, milestone.StartBlock.Uint64(), milestone.EndBlock.Uint64(), milestone.Hash.String()[2:], false)
_, err = verifier.verify(ctx, eth, h, milestone.StartBlock, milestone.EndBlock, milestone.Hash.String()[2:], false)
if err != nil {
if errors.Is(err, errChainOutOfSync) {
log.Info("Whitelisting milestone deferred", "err", err)
} else {
log.Warn("Failed to whitelist milestone", "err", err)
}
h.downloader.UnlockSprint(milestone.EndBlock.Uint64())
h.downloader.UnlockSprint(milestone.EndBlock)
}

return num, hash, err
Expand Down
8 changes: 4 additions & 4 deletions eth/handler_bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func fetchMilestoneTest(t *testing.T, heimdall *mockHeimdall, bor *bor.Bor, hand

// Check if we have expected result
require.Equal(t, err, nil)
require.Equal(t, milestones[len(milestones)-1].EndBlock.Uint64(), num)
require.Equal(t, milestones[len(milestones)-1].EndBlock, num)
require.Equal(t, milestones[len(milestones)-1].Hash, hash)
}

Expand Down Expand Up @@ -176,14 +176,14 @@ func createMockCheckpoints(count int) []*checkpoint.Checkpoint {
func createMockMilestones(count int) []*milestone.Milestone {
var (
milestones []*milestone.Milestone = make([]*milestone.Milestone, count)
startBlock int64 = 257 // any number can be used
startBlock uint64 = 257 // any number can be used
)

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

0 comments on commit 46b1b62

Please sign in to comment.