diff --git a/consensus/bor/heimdall/client.go b/consensus/bor/heimdall/client.go index 67ad49a0e3..627b005ea9 100644 --- a/consensus/bor/heimdall/client.go +++ b/consensus/bor/heimdall/client.go @@ -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" @@ -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/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/consensus/bor/heimdall/milestone/milestone.go b/consensus/bor/heimdall/milestone/milestone.go index a0dd04742a..65e7c8d6dd 100644 --- a/consensus/bor/heimdall/milestone/milestone.go +++ b/consensus/bor/heimdall/milestone/milestone.go @@ -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 { @@ -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 { 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 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()),