Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.1.0 new span msg #1213

Merged
merged 25 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7a5ae0f
New propose span msg
avalkov Dec 6, 2024
2c517e3
helper: temporarily set antevortaHeight for testing
Raneet10 Dec 9, 2024
34563f9
bor: fix nil author clause
Raneet10 Dec 9, 2024
e1d5a3f
bor: fix TestProposeSpanOne and TestGetNextSpanSeed
Raneet10 Dec 9, 2024
87ef859
bor: avoid nil ptr deref
Raneet10 Dec 9, 2024
7c76ee2
Add support for new msg propose span in cli and rest
avalkov Dec 9, 2024
4630b53
Enable all hardforks at zero height
avalkov Dec 9, 2024
695f3b8
helper: temporarily add non-zero hf heights for testing
Raneet10 Dec 9, 2024
fe2f618
bor: fix handler
Raneet10 Dec 9, 2024
0f90239
When proposing span query height from the node
avalkov Dec 9, 2024
eced431
Enable all forks on zero height
avalkov Dec 9, 2024
21cd612
Add temporary commands to test malicious cases
avalkov Dec 9, 2024
c22c680
Add temporary commands to test malicious cases
avalkov Dec 9, 2024
652b4da
Let the bridge propose only the first two spans
avalkov Dec 9, 2024
61d3f00
Dont enable antevorta from zero height
avalkov Dec 9, 2024
fe80a4f
Restrict new msg pre-hardfork
avalkov Dec 10, 2024
221e037
set non-zero height for antevorta
avalkov Dec 10, 2024
9323f1e
Add debug logging
avalkov Dec 10, 2024
35b725d
Lower enable fork height
avalkov Dec 10, 2024
76ac6d3
Clean up
avalkov Dec 10, 2024
3024891
Cleanup
avalkov Dec 10, 2024
14251ca
Fix lint
avalkov Dec 10, 2024
15a8f62
Fix comments
avalkov Dec 10, 2024
bafc9e6
Merge pull request #1212 from maticnetwork/v1.1.0-new-span-msg-zero-h…
avalkov Dec 10, 2024
5c353b1
fix: nil logger
marcello33 Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions bor/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

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

"github.com/maticnetwork/heimdall/bor/types"
hmClient "github.com/maticnetwork/heimdall/client"
"github.com/maticnetwork/heimdall/helper"
Expand Down Expand Up @@ -271,7 +269,7 @@ func GetNextSpanSeed(cdc *codec.Codec) *cobra.Command {
return cmd
}

// PostSendProposeSpanTx send propose span transaction
// GetPreparedProposeSpan generates a propose span transaction
func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "propose-span",
Expand Down Expand Up @@ -313,6 +311,11 @@ func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
return err
}

nodeStatus, err := helper.GetNodeStatus(cliCtx)
if err != nil {
return err
}

//
// Query data
//
Expand Down Expand Up @@ -345,23 +348,42 @@ func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
return errors.New("next span seed not found")
}

var seed common.Hash
if err := jsoniter.Unmarshal(res, &seed); err != nil {
var seedResponse types.QuerySpanSeedResponse
if err := jsoniter.Unmarshal(res, &seedResponse); err != nil {
return err
}

msg := types.NewMsgProposeSpan(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seed,
)

result, err := jsoniter.Marshal(&msg)
if err != nil {
return err
var result []byte

if nodeStatus.SyncInfo.LatestBlockHeight < helper.GetAntevortaHeight() {
msg := types.NewMsgProposeSpan(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seedResponse.Seed,
)

result, err = jsoniter.Marshal(&msg)
if err != nil {
return err
}
} else {
msg := types.NewMsgProposeSpanV2(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seedResponse.Seed,
seedResponse.SeedAuthor,
)

result, err = jsoniter.Marshal(&msg)
if err != nil {
return err
}
}

fmt.Println(string(result))
Expand Down
40 changes: 28 additions & 12 deletions bor/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

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

"github.com/maticnetwork/heimdall/bor/types"
hmClient "github.com/maticnetwork/heimdall/client"
"github.com/maticnetwork/heimdall/helper"
Expand Down Expand Up @@ -84,6 +82,11 @@ func PostSendProposeSpanTx(cdc *codec.Codec) *cobra.Command {
return err
}

nodeStatus, err := helper.GetNodeStatus(cliCtx)
if err != nil {
return err
}

//
// Query data
//
Expand Down Expand Up @@ -116,19 +119,32 @@ func PostSendProposeSpanTx(cdc *codec.Codec) *cobra.Command {
return errors.New("next span seed not found")
}

var seed common.Hash
if err := jsoniter.ConfigFastest.Unmarshal(res, &seed); err != nil {
var seedResponse types.QuerySpanSeedResponse
if err := jsoniter.ConfigFastest.Unmarshal(res, &seedResponse); err != nil {
return err
}

msg := types.NewMsgProposeSpan(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seed,
)
var msg sdk.Msg
if nodeStatus.SyncInfo.LatestBlockHeight < helper.GetAntevortaHeight() {
msg = types.NewMsgProposeSpan(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seedResponse.Seed,
)
} else {
msg = types.NewMsgProposeSpanV2(
spanID,
proposer,
startBlock,
startBlock+spanDuration-1,
borChainID,
seedResponse.Seed,
seedResponse.SeedAuthor,
)
}

return helper.BroadcastMsgsWithCLI(cliCtx, []sdk.Msg{msg})
},
Expand Down
45 changes: 32 additions & 13 deletions bor/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"github.com/gorilla/mux"
jsoniter "github.com/json-iterator/go"

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

"github.com/maticnetwork/heimdall/bor/types"
restClient "github.com/maticnetwork/heimdall/client/rest"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
"github.com/maticnetwork/heimdall/types/rest"
)
Expand Down Expand Up @@ -163,20 +162,40 @@ func postProposeSpanHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

var seed common.Hash
if err = jsoniter.ConfigFastest.Unmarshal(res, &seed); err != nil {
var seedResponse types.QuerySpanSeedResponse
if err = jsoniter.ConfigFastest.Unmarshal(res, &seedResponse); err != nil {
return
}

// draft a propose span message
msg := types.NewMsgProposeSpan(
req.ID,
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.StartBlock,
req.StartBlock+spanDuration-1,
req.BorChainID,
seed,
)
nodeStatus, err := helper.GetNodeStatus(cliCtx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

var msg sdk.Msg
if nodeStatus.SyncInfo.LatestBlockHeight < helper.GetAntevortaHeight() {
// draft a propose span message
msg = types.NewMsgProposeSpan(
req.ID,
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.StartBlock,
req.StartBlock+spanDuration-1,
req.BorChainID,
seedResponse.Seed,
)
} else {
// draft a propose span v2 message
msg = types.NewMsgProposeSpanV2(
req.ID,
hmTypes.HexToHeimdallAddress(req.BaseReq.From),
req.StartBlock,
req.StartBlock+spanDuration-1,
req.BorChainID,
seedResponse.Seed,
seedResponse.SeedAuthor,
)
}

// send response
restClient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
Expand Down
63 changes: 46 additions & 17 deletions bor/handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bor

import (
"errors"
"strconv"

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

"github.com/maticnetwork/heimdall/bor/types"
"github.com/maticnetwork/heimdall/common"
"github.com/maticnetwork/heimdall/helper"
)

// NewHandler returns a handler for "bor" type messages.
Expand All @@ -15,7 +17,8 @@ func NewHandler(k Keeper) sdk.Handler {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case types.MsgProposeSpan:
case types.MsgProposeSpan,
types.MsgProposeSpanV2:
return HandleMsgProposeSpan(ctx, msg, k)
default:
return sdk.ErrTxDecode("Invalid message in bor module").Result()
Expand All @@ -24,21 +27,47 @@ func NewHandler(k Keeper) sdk.Handler {
}

// HandleMsgProposeSpan handles proposeSpan msg
func HandleMsgProposeSpan(ctx sdk.Context, msg types.MsgProposeSpan, k Keeper) sdk.Result {
func HandleMsgProposeSpan(ctx sdk.Context, msg sdk.Msg, k Keeper) sdk.Result {
var proposeMsg types.MsgProposeSpanV2
switch msg := msg.(type) {
case types.MsgProposeSpan:
if ctx.BlockHeight() >= helper.GetAntevortaHeight() {
err := errors.New("msg span is not allowed after Antevorta hardfork height")
k.Logger(ctx).Error(err.Error())
return sdk.ErrTxDecode(err.Error()).Result()
}
proposeMsg = types.MsgProposeSpanV2{
ID: msg.ID,
Proposer: msg.Proposer,
StartBlock: msg.StartBlock,
EndBlock: msg.EndBlock,
ChainID: msg.ChainID,
Seed: msg.Seed,
}
case types.MsgProposeSpanV2:
if ctx.BlockHeight() < helper.GetAntevortaHeight() {
err := errors.New("msg span v2 is not allowed before Antevorta hardfork height")
k.Logger(ctx).Error(err.Error())
return sdk.ErrTxDecode(err.Error()).Result()
}
proposeMsg = msg
}

k.Logger(ctx).Debug("✅ Validating proposed span msg",
"spanId", msg.ID,
"startBlock", msg.StartBlock,
"endBlock", msg.EndBlock,
"seed", msg.Seed.String(),
"proposer", proposeMsg.Proposer.String(),
"spanId", proposeMsg.ID,
"startBlock", proposeMsg.StartBlock,
"endBlock", proposeMsg.EndBlock,
"seed", proposeMsg.Seed.String(),
)

// chainManager params
params := k.chainKeeper.GetParams(ctx)
chainParams := params.ChainParams

// check chain id
if chainParams.BorChainID != msg.ChainID {
k.Logger(ctx).Error("Invalid Bor chain id", "msgChainID", msg.ChainID)
if chainParams.BorChainID != proposeMsg.ChainID {
k.Logger(ctx).Error("Invalid Bor chain id", "msgChainID", proposeMsg.ChainID)
return common.ErrInvalidBorChainID(k.Codespace()).Result()
}

Expand All @@ -50,24 +79,24 @@ func HandleMsgProposeSpan(ctx sdk.Context, msg types.MsgProposeSpan, k Keeper) s
}

// Validate span continuity
if lastSpan.ID+1 != msg.ID || msg.StartBlock != lastSpan.EndBlock+1 || msg.EndBlock < msg.StartBlock {
if lastSpan.ID+1 != proposeMsg.ID || proposeMsg.StartBlock != lastSpan.EndBlock+1 || proposeMsg.EndBlock < proposeMsg.StartBlock {
k.Logger(ctx).Error("Blocks not in continuity",
"lastSpanId", lastSpan.ID,
"spanId", msg.ID,
"spanId", proposeMsg.ID,
"lastSpanStartBlock", lastSpan.StartBlock,
"lastSpanEndBlock", lastSpan.EndBlock,
"spanStartBlock", msg.StartBlock,
"spanEndBlock", msg.EndBlock,
"spanStartBlock", proposeMsg.StartBlock,
"spanEndBlock", proposeMsg.EndBlock,
)

return common.ErrSpanNotInContinuity(k.Codespace()).Result()
}

// Validate Span duration
spanDuration := k.GetParams(ctx).SpanDuration
if spanDuration != (msg.EndBlock - msg.StartBlock + 1) {
if spanDuration != (proposeMsg.EndBlock - proposeMsg.StartBlock + 1) {
k.Logger(ctx).Error("Span duration of proposed span is wrong",
"proposedSpanDuration", msg.EndBlock-msg.StartBlock+1,
"proposedSpanDuration", proposeMsg.EndBlock-proposeMsg.StartBlock+1,
"paramsSpanDuration", spanDuration,
)

Expand All @@ -79,9 +108,9 @@ func HandleMsgProposeSpan(ctx sdk.Context, msg types.MsgProposeSpan, k Keeper) s
sdk.NewEvent(
types.EventTypeProposeSpan,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySpanID, strconv.FormatUint(msg.ID, 10)),
sdk.NewAttribute(types.AttributeKeySpanStartBlock, strconv.FormatUint(msg.StartBlock, 10)),
sdk.NewAttribute(types.AttributeKeySpanEndBlock, strconv.FormatUint(msg.EndBlock, 10)),
sdk.NewAttribute(types.AttributeKeySpanID, strconv.FormatUint(proposeMsg.ID, 10)),
sdk.NewAttribute(types.AttributeKeySpanStartBlock, strconv.FormatUint(proposeMsg.StartBlock, 10)),
sdk.NewAttribute(types.AttributeKeySpanEndBlock, strconv.FormatUint(proposeMsg.EndBlock, 10)),
),
})

Expand Down
22 changes: 15 additions & 7 deletions bor/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,13 @@ func (k *Keeper) GetLastEthBlock(ctx sdk.Context) *big.Int {
return lastEthBlock
}

func (k *Keeper) GetNextSpanSeed(ctx sdk.Context, id uint64) (common.Hash, error) {
func (k *Keeper) GetNextSpanSeed(ctx sdk.Context, id uint64) (common.Hash, common.Address, error) {
var (
blockHeader *ethTypes.Header
borBlock uint64
seedSpan *hmTypes.Span
err error
author *common.Address
)

if ctx.BlockHeader().Height < helper.GetJorvikHeight() {
Expand All @@ -367,8 +369,9 @@ func (k *Keeper) GetNextSpanSeed(ctx sdk.Context, id uint64) (common.Hash, error
blockHeader, err = k.contractCaller.GetMainChainBlock(newEthBlock)
if err != nil {
k.Logger(ctx).Error("Error fetching block header from mainchain while calculating next span seed", "error", err)
return common.Hash{}, err
return common.Hash{}, common.Address{}, err
}
author = &common.Address{}
} else {
var seedSpanID uint64
if id < 2 {
Expand All @@ -379,24 +382,29 @@ func (k *Keeper) GetNextSpanSeed(ctx sdk.Context, id uint64) (common.Hash, error
seedSpan, err = k.GetSpan(ctx, seedSpanID)
if err != nil {
k.Logger(ctx).Error("Error fetching span while calculating next span seed", "error", err)
return common.Hash{}, err
return common.Hash{}, common.Address{}, err
}

borBlock, author, err := k.getBorBlockForSpanSeed(ctx, seedSpan, id)
borBlock, author, err = k.getBorBlockForSpanSeed(ctx, seedSpan, id)
if err != nil {
return common.Hash{}, err
return common.Hash{}, common.Address{}, err
}

blockHeader, err = k.contractCaller.GetMaticChainBlock(big.NewInt(int64(borBlock)))
if err != nil {
k.Logger(ctx).Error("Error fetching block header from bor chain while calculating next span seed", "error", err, "block", borBlock)
return common.Hash{}, err
return common.Hash{}, common.Address{}, err
}

if author == nil {
k.Logger(ctx).Error("seed author is nil")
return blockHeader.Hash(), common.Address{}, fmt.Errorf("seed author is nil")
}

k.Logger(ctx).Debug("fetched block for seed", "block", borBlock, "author", author, "span id", id)
}

return blockHeader.Hash(), nil
return blockHeader.Hash(), *author, nil
}

// StoreSeedProducer stores producer of the block used for seed for the given span id
Expand Down
Loading
Loading