Skip to content

Commit

Permalink
Merge pull request #1163 from marcello33/mardizzone/bb-31177
Browse files Browse the repository at this point in the history
Fix bb-31177
  • Loading branch information
marcello33 authored May 23, 2024
2 parents ae5a160 + 90fc571 commit 1eec11a
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 30 deletions.
4 changes: 2 additions & 2 deletions checkpoint/handler_milestone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ func (suite *HandlerTestSuite) SendMilestone(header hmTypes.Milestone) (res sdk.

// keeper := app.MilestoneKeeper

borChainId := "1234"
milestoneID := "00000"
borChainId := header.BorChainID
milestoneID := header.MilestoneID
// create milestone msg
msgMilestone := types.NewMsgMilestoneBlock(
header.Proposer,
Expand Down
2 changes: 1 addition & 1 deletion checkpoint/side_milestone_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func SideHandleMsgMilestone(ctx sdk.Context, k Keeper, msg types.MsgMilestone, c
return common.ErrorSideTx(k.Codespace(), common.CodeInvalidBlockInput)
}

//Validating the milestone
// Validating the milestone
validMilestone, err := types.ValidateMilestone(msg.StartBlock, msg.EndBlock, msg.Hash, msg.MilestoneID, contractCaller, milestoneLength, cmTypes.DefaultMaticchainMilestoneTxConfirmations)
if err != nil {
logger.Error("Error validating milestone",
Expand Down
50 changes: 50 additions & 0 deletions checkpoint/side_milestone_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,56 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgMilestone() {
require.Equal(t, uint32(common.CodeInvalidBlockInput), result.Code)
})

suite.Run("invalid milestone uuid", func() {
suite.contractCaller = mocks.IContractCaller{}

milestone.MilestoneID = "0-0a18-41a8-ab7e-59d8002f027b - 0x901a64406d97a3fa9b87b320cbeb86b3c62328f5"

// create milestone msg
msgMilestone := types.NewMsgMilestoneBlock(
milestone.Proposer,
milestone.StartBlock,
milestone.EndBlock-1,
milestone.Hash,
borChainId,
milestone.MilestoneID,
)

msgMilestone.BorChainID = msgMilestone.MilestoneID

suite.contractCaller.On("CheckIfBlocksExist", milestone.EndBlock+cmTypes.DefaultMaticchainMilestoneTxConfirmations).Return(true)
suite.contractCaller.On("GetVoteOnHash", milestone.StartBlock, milestone.EndBlock, milestoneLength, milestone.Hash.String(), milestone.MilestoneID).Return(true, nil)

result := suite.sideHandler(ctx, msgMilestone)
require.NotEqual(t, uint32(sdk.CodeOK), result.Code, "Side tx handler should fail")
require.Equal(t, uint32(common.CodeInvalidBlockInput), result.Code)
})

suite.Run("invalid milestone proposer", func() {
suite.contractCaller = mocks.IContractCaller{}

milestone.MilestoneID = "17ce48fe-0a18-41a8-ab7e-59d8002f027b - 0xz01a64406d97a3fa9b87b320cbeb86b3c62328f5"

// create milestone msg
msgMilestone := types.NewMsgMilestoneBlock(
milestone.Proposer,
milestone.StartBlock,
milestone.EndBlock-1,
milestone.Hash,
borChainId,
milestone.MilestoneID,
)

msgMilestone.BorChainID = msgMilestone.MilestoneID

suite.contractCaller.On("CheckIfBlocksExist", milestone.EndBlock+cmTypes.DefaultMaticchainMilestoneTxConfirmations).Return(true)
suite.contractCaller.On("GetVoteOnHash", milestone.StartBlock, milestone.EndBlock, milestoneLength, milestone.Hash.String(), milestone.MilestoneID).Return(true, nil)

result := suite.sideHandler(ctx, msgMilestone)
require.NotEqual(t, uint32(sdk.CodeOK), result.Code, "Side tx handler should fail")
require.Equal(t, uint32(common.CodeInvalidBlockInput), result.Code)
})

suite.Run("Not in continuity", func() {
suite.contractCaller = mocks.IContractCaller{}
err := keeper.AddMilestone(ctx, milestone)
Expand Down
4 changes: 3 additions & 1 deletion checkpoint/simulation/header_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/maticnetwork/heimdall/types"
)

const TestMilestoneID = "17ce48fe-0a18-41a8-ab7e-59d8002f027b - 0x901a64406d97a3fa9b87b320cbeb86b3c62328f5"

// GenRandMilestone return headers
func GenRandMilestone(start uint64, sprintLength uint64) (milestone types.Milestone, err error) {
end := start + sprintLength - 1
borChainID := "1234"
rootHash := types.HexToHeimdallHash("123")
proposer := types.HeimdallAddress{}
milestoneID := "00000"
milestoneID := TestMilestoneID
milestone = types.CreateMilestone(
start,
end,
Expand Down
25 changes: 22 additions & 3 deletions checkpoint/types/merkel_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package types
import (
"errors"
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"

"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
)

// ValidateMilestone - Validates if milestone rootHash matches or not
// ValidateMilestone validates the structure of the milestone
func ValidateMilestone(start uint64, end uint64, rootHash hmTypes.HeimdallHash, milestoneID string, contractCaller helper.IContractCaller, milestoneLength uint64, confirmations uint64) (bool, error) {
msgMilestoneLength := int64(end) - int64(start) + 1

//Check for the minimum length of the milestone
// Check for the minimum length of the milestone
if msgMilestoneLength < int64(milestoneLength) {
return false, errors.New(fmt.Sprint("Invalid milestone, difference in start and end block is less than milestone length", "Milestone Length:", milestoneLength))
}
Expand All @@ -22,11 +26,26 @@ func ValidateMilestone(start uint64, end uint64, rootHash hmTypes.HeimdallHash,
return false, errors.New(fmt.Sprint("End block number with confirmation is not available in the Bor chain", "EndBlock", end, "Confirmation", confirmations))
}

//Get the vote on hash of milestone from Bor
// Get the vote on hash of milestone from Bor
vote, err := contractCaller.GetVoteOnHash(start, end, milestoneLength, rootHash.String(), milestoneID)
if err != nil {
return false, err
}

// validate that milestoneID is composed by `UUID - HexAddressOfTheProposer`
splitMilestoneID := strings.Split(strings.TrimSpace(milestoneID), " - ")
if len(splitMilestoneID) != 2 {
return false, errors.New(fmt.Sprint("invalid milestoneID, it should be composed by `UUID - HexAddressOfTheProposer`", "milestoneID", milestoneID))
}

_, err = uuid.Parse(splitMilestoneID[0])
if err != nil {
return false, errors.New(fmt.Sprint("invalid milestoneID, the UUID is not correct", "milestoneID", milestoneID))
}

if !common.IsHexAddress(splitMilestoneID[1]) {
return false, errors.New(fmt.Sprint("invalid milestoneID, the proposer address is not correct", "milestoneID", milestoneID))
}

return vote, nil
}
14 changes: 1 addition & 13 deletions checkpoint/types/msg_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package types

import (
"bytes"
"math/big"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -84,17 +82,7 @@ func (msg MsgMilestone) ValidateBasic() sdk.Error {

// GetSideSignBytes returns side sign bytes
func (msg MsgMilestone) GetSideSignBytes() []byte {
// keccak256(abi.encoded(proposer, startBlock, endBlock, rootHash, accountRootHash, bor chain id))
borChainID, _ := strconv.ParseUint(msg.BorChainID, 10, 64)

return appendBytes32(
msg.Proposer.Bytes(),
new(big.Int).SetUint64(msg.StartBlock).Bytes(),
new(big.Int).SetUint64(msg.EndBlock).Bytes(),
msg.Hash.Bytes(),
new(big.Int).SetUint64(borChainID).Bytes(),
[]byte(msg.MilestoneID),
)
return nil
}

var _ sdk.Msg = &MsgMilestoneTimeout{}
Expand Down
4 changes: 2 additions & 2 deletions packaging/deb/heimdalld/DEBIAN/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Section: develop
Priority: Testing
Maintainer: Polygon <team@polygon.technology>
Build-Depends: debhelper-compat (= 13)
Standards-Version: v1.0.5
Standards-Version: v1.0.6
Homepage: https://polygon.technology
Rules-Requires-Root: no
Package: heimdalld
Version: 1.0.5
Version: 1.0.6
Architecture: amd64
Multi-Arch: foreign
Depends:
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: heimdalld
Version: 1.0.5
Version: 1.0.6
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.arm64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: heimdalld
Version: 1.0.5
Version: 1.0.6
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.profile.amd64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: heimdalld-profile
Version: 1.0.5
Version: 1.0.6
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.profile.arm64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: heimdalld-profile
Version: 1.0.5
Version: 1.0.6
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
4 changes: 2 additions & 2 deletions packaging/templates/package_scripts/control.validator
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Section: develop
Priority: Testing
Maintainer: Polygon <team@polygon.technology>
Build-Depends: debhelper-compat (= 13)
Standards-Version: v1.0.5
Standards-Version: v1.0.6
Homepage: https://polygon.technology
Rules-Requires-Root: no
Package: heimdalld-profile
Version: 1.0.5
Version: 1.0.6
Architecture: amd64
Multi-Arch: foreign
Depends: rabbitmq-server
Expand Down
4 changes: 2 additions & 2 deletions packaging/templates/package_scripts/control.validator.arm64
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Section: develop
Priority: Testing
Maintainer: Polygon <team@polygon.technology>
Build-Depends: debhelper-compat (= 13)
Standards-Version: v1.0.5
Standards-Version: v1.0.6
Homepage: https://polygon.technology
Rules-Requires-Root: no
Package: heimdalld-profile
Version: 1.0.5
Version: 1.0.6
Architecture: arm64
Multi-Arch: foreign
Depends: rabbitmq-server
Expand Down

0 comments on commit 1eec11a

Please sign in to comment.