From 7e465cb653be0ce7b5f85d835b83f410a2bb6087 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 09:03:10 +0200 Subject: [PATCH 1/9] Fix milestone response --- consensus/bor/heimdall/milestone/milestone.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index a0dd04742a..13c41c7ee3 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -8,17 +8,17 @@ import ( // 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 *big.Int `json:"start_block"` + EndBlock *big.Int `json:"end_block"` + Hash common.Hash `json:"hash"` + BorChainID string `json:"bor_chain_id"` + MilestoneID string `json:"milestone_id"` + Timestamp uint64 `json:"timestamp"` } type MilestoneResponse struct { - Height string `json:"height"` - Result Milestone `json:"result"` + Result Milestone `json:"milestone"` } type MilestoneCount struct { From eba691c9258af8aa699bc63ad73c9bba76ffb949 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 09:41:42 +0200 Subject: [PATCH 2/9] Latest milestone response fixes --- consensus/bor/heimdall/milestone/milestone.go | 6 ++---- consensus/bor/heimdallapp/milestone.go | 5 ++--- consensus/bor/heimdallgrpc/milestone.go | 5 ++--- eth/handler_bor.go | 8 ++++---- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index 13c41c7ee3..1a81d53cd0 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -1,16 +1,14 @@ package milestone import ( - "math/big" - "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"` + 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"` diff --git a/consensus/bor/heimdallapp/milestone.go b/consensus/bor/heimdallapp/milestone.go index f8c5bded68..27eac343cb 100644 --- a/consensus/bor/heimdallapp/milestone.go +++ b/consensus/bor/heimdallapp/milestone.go @@ -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" @@ -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, diff --git a/consensus/bor/heimdallgrpc/milestone.go b/consensus/bor/heimdallgrpc/milestone.go index a156838548..83ae902a6a 100644 --- a/consensus/bor/heimdallgrpc/milestone.go +++ b/consensus/bor/heimdallgrpc/milestone.go @@ -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" @@ -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, diff --git a/eth/handler_bor.go b/eth/handler_bor.go index be06165b11..ce89eacad9 100644 --- a/eth/handler_bor.go +++ b/eth/handler_bor.go @@ -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 From 0ab6c16f15a2009d1a35f6d78af4dfeacad52e7b Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 10:32:29 +0200 Subject: [PATCH 3/9] Milestone custom unmarshal --- consensus/bor/heimdall/milestone/milestone.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index 1a81d53cd0..3004778c44 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -1,6 +1,10 @@ package milestone import ( + "encoding/json" + "fmt" + "strconv" + "github.com/ethereum/go-ethereum/common" ) @@ -15,6 +19,35 @@ type Milestone struct { 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"` + *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 + + return nil +} + type MilestoneResponse struct { Result Milestone `json:"milestone"` } From 402c030f3744e3b13ccd35bfeabf2180af99777c Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 12:47:59 +0200 Subject: [PATCH 4/9] Fix milestone parsing --- consensus/bor/heimdall/milestone/milestone.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index 3004778c44..cb52b7a5a2 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -1,6 +1,7 @@ package milestone import ( + "encoding/base64" "encoding/json" "fmt" "strconv" @@ -24,6 +25,7 @@ func (m *Milestone) UnmarshalJSON(data []byte) error { temp := &struct { StartBlock string `json:"start_block"` EndBlock string `json:"end_block"` + Hash string `json:"hash"` *Alias }{ Alias: (*Alias)(m), @@ -45,6 +47,12 @@ func (m *Milestone) UnmarshalJSON(data []byte) error { } 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) + return nil } From 14edef724342d210d0385d5b440765e933576b21 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 13:10:50 +0200 Subject: [PATCH 5/9] Fix milestone parsing --- consensus/bor/heimdall/milestone/milestone.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index cb52b7a5a2..3a4e70c9a5 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -26,6 +26,7 @@ func (m *Milestone) UnmarshalJSON(data []byte) error { StartBlock string `json:"start_block"` EndBlock string `json:"end_block"` Hash string `json:"hash"` + Timestamp string `json:"timestamp"` *Alias }{ Alias: (*Alias)(m), @@ -53,6 +54,12 @@ func (m *Milestone) UnmarshalJSON(data []byte) error { } 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 } From 7fbf825b5095900a5356bfb4fcfcef7c9f373dc3 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 13:30:36 +0200 Subject: [PATCH 6/9] Fix milestone no-ack parsing --- consensus/bor/heimdall/client.go | 6 +++--- consensus/bor/heimdall/milestone/milestone.go | 14 ++------------ 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/consensus/bor/heimdall/client.go b/consensus/bor/heimdall/client.go index 67ad49a0e3..9fccdd2e3a 100644 --- a/consensus/bor/heimdall/client.go +++ b/consensus/bor/heimdall/client.go @@ -85,7 +85,7 @@ const ( fetchMilestone = "/milestone/latest" fetchMilestoneCount = "/milestone/count" - fetchLastNoAckMilestone = "/milestone/lastNoAck" + fetchLastNoAckMilestone = "/milestone/last-no-ack" fetchNoAckMilestone = "/milestone/noAck/%s" fetchMilestoneID = "/milestone/ID/%s" @@ -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 @@ -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) } diff --git a/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index 3a4e70c9a5..65e7c8d6dd 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -76,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 { From 63050a02a86fb09e45fe49d5192a9d967df880bb Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 13:50:53 +0200 Subject: [PATCH 7/9] Fix milestone no-ack parsing --- consensus/bor/bor.go | 2 ++ consensus/bor/heimdall/client.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index b98344b844..a49f770b93 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -1252,6 +1252,8 @@ func (c *Bor) FetchAndCommitSpan( for _, val := range response.SelectedProducers { producers = append(producers, val.MinimalVal()) } + + log.Error("Fetched span", "span", minSpan, "validators", validators, "producers", producers, "span", fmt.Sprintf("response: %+v", response)) } // check if chain id matches with Heimdall span diff --git a/consensus/bor/heimdall/client.go b/consensus/bor/heimdall/client.go index 9fccdd2e3a..627b005ea9 100644 --- a/consensus/bor/heimdall/client.go +++ b/consensus/bor/heimdall/client.go @@ -86,7 +86,7 @@ const ( fetchMilestoneCount = "/milestone/count" fetchLastNoAckMilestone = "/milestone/last-no-ack" - fetchNoAckMilestone = "/milestone/noAck/%s" + fetchNoAckMilestone = "/milestone/no-ack/%s" fetchMilestoneID = "/milestone/ID/%s" fetchSpanFormat = "bor/span/%d" From 8ceff9aa385d716de8fa699525c63a42f0fbe9b7 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Fri, 3 Jan 2025 14:03:54 +0200 Subject: [PATCH 8/9] Remove log --- consensus/bor/bor.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index a49f770b93..b98344b844 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -1252,8 +1252,6 @@ func (c *Bor) FetchAndCommitSpan( for _, val := range response.SelectedProducers { producers = append(producers, val.MinimalVal()) } - - log.Error("Fetched span", "span", minSpan, "validators", validators, "producers", producers, "span", fmt.Sprintf("response: %+v", response)) } // check if chain id matches with Heimdall span From b0e57c5d4e081462a01b34aa6aaeafef18efd476 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Mon, 6 Jan 2025 13:51:33 +0200 Subject: [PATCH 9/9] Test fixes --- consensus/bor/heimdall/client_test.go | 5 ++--- eth/handler_bor_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/consensus/bor/heimdall/client_test.go b/consensus/bor/heimdall/client_test.go index 0fa7665048..e91c19fe8a 100644 --- a/consensus/bor/heimdall/client_test.go +++ b/consensus/bor/heimdall/client_test.go @@ -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, diff --git a/eth/handler_bor_test.go b/eth/handler_bor_test.go index 414ba57708..347c042ed8 100644 --- a/eth/handler_bor_test.go +++ b/eth/handler_bor_test.go @@ -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) } @@ -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()),