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

fix(v17): inbound and outbound validation for BSC chain #2249

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v17.0.0

### Fixes

* [2249](https://github.com/zeta-chain/node/pull/2249) - fix inbound and outbound validation for BSC chain

## v16.0.0

### Breaking Changes
Expand Down
5 changes: 3 additions & 2 deletions typescript/crosschain/genesis_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { InTxTracker } from "./in_tx_tracker_pb.js";
import type { RateLimiterFlags } from "./rate_limiter_flags_pb.js";

/**
* GenesisState defines the metacore module's genesis state.
* GenesisState defines the crosschain modules genesis state.
*
* @generated from message zetachain.zetacore.crosschain.GenesisState
*/
Expand Down Expand Up @@ -80,7 +80,8 @@ export declare class GenesisState extends Message<GenesisState> {
}

/**
* GenesisState defines the metacore module's genesis state.
* Remove legacy types
* https://github.com/zeta-chain/node/issues/2139
*
* @generated from message zetachain.zetacore.crosschain.GenesisState_legacy
*/
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/types/cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Test_InitializeCCTX(t *testing.T) {
tss := sample.Tss()
msg := types.MsgVoteOnObservedInboundTx{
Creator: creator,
Sender: "invalid",
Sender: "",
SenderChainId: senderChain.ChainId,
Receiver: receiver.String(),
ReceiverChain: receiverChain.ChainId,
Expand All @@ -106,7 +106,7 @@ func Test_InitializeCCTX(t *testing.T) {
EventIndex: eventIndex,
}
_, err := types.NewCCTX(ctx, msg, tss.TssPubkey)
require.ErrorContains(t, err, "invalid address")
require.ErrorContains(t, err, "sender cannot be empty")
})
}

Expand Down
5 changes: 3 additions & 2 deletions x/crosschain/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 23 additions & 21 deletions x/crosschain/types/inbound_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,42 @@ package types
import (
"fmt"

"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/zeta-chain/zetacore/pkg/chains"
)

func (m InboundTxParams) Validate() error {
if m.Sender == "" {
return fmt.Errorf("sender cannot be empty")
}

if chains.GetChainFromChainID(m.SenderChainId) == nil {
return fmt.Errorf("invalid sender chain id %d", m.SenderChainId)
}
err := ValidateAddressForChain(m.Sender, m.SenderChainId)
if err != nil {
return err
}

if m.TxOrigin != "" {
errTxOrigin := ValidateAddressForChain(m.TxOrigin, m.SenderChainId)
if errTxOrigin != nil {
return errTxOrigin
}
}
if m.Amount.IsNil() {
return fmt.Errorf("amount cannot be nil")
}
err = ValidateHashForChain(m.InboundTxObservedHash, m.SenderChainId)
if err != nil {
return errors.Wrap(err, "invalid inbound tx observed hash")
}
if m.InboundTxBallotIndex != "" {
err = ValidateZetaIndex(m.InboundTxBallotIndex)
if err != nil {
return errors.Wrap(err, "invalid inbound tx ballot index")
}
}

// Disabled checks
// TODO: Improve the checks, move the validation call to a new place and reenable
// https://github.com/zeta-chain/node/issues/2234
// https://github.com/zeta-chain/node/issues/2235
//if err := ValidateAddressForChain(m.Sender, m.SenderChainId) err != nil {
// return err
//}
//if m.TxOrigin != "" {
// errTxOrigin := ValidateAddressForChain(m.TxOrigin, m.SenderChainId)
// if errTxOrigin != nil {
// return errTxOrigin
// }
//}
//if err := ValidateHashForChain(m.ObservedHash, m.SenderChainId); err != nil {
// return errors.Wrap(err, "invalid inbound tx observed hash")
//}
//if m.BallotIndex != "" {
// if err := ValidateCCTXIndex(m.BallotIndex); err != nil {
// return errors.Wrap(err, "invalid inbound tx ballot index")
// }
//}
return nil
}
18 changes: 2 additions & 16 deletions x/crosschain/types/inbound_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/testutil/sample"
)

Expand All @@ -18,24 +17,11 @@ func TestInboundTxParams_Validate(t *testing.T) {
inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.SenderChainId = 1000
require.ErrorContains(t, inTxParams.Validate(), "invalid sender chain id 1000")
inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.SenderChainId = chains.GoerliChain().ChainId
inTxParams.Sender = "0x123"
require.ErrorContains(t, inTxParams.Validate(), "invalid address 0x123")
inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.SenderChainId = chains.GoerliChain().ChainId
inTxParams.TxOrigin = "0x123"
require.ErrorContains(t, inTxParams.Validate(), "invalid address 0x123")

inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.Amount = sdkmath.Uint{}
require.ErrorContains(t, inTxParams.Validate(), "amount cannot be nil")
inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.InboundTxObservedHash = "12"
require.ErrorContains(t, inTxParams.Validate(), "hash must be a valid ethereum hash 12")
inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.InboundTxObservedHash = sample.Hash().String()
inTxParams.InboundTxBallotIndex = "12"
require.ErrorContains(t, inTxParams.Validate(), "invalid index length 2")

inTxParams = sample.InboundTxParamsValidChainID(r)
inTxParams.InboundTxObservedHash = sample.Hash().String()
inTxParams.InboundTxBallotIndex = sample.ZetaIndex(t)
Expand Down
38 changes: 21 additions & 17 deletions x/crosschain/types/outbound_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/zeta-chain/zetacore/pkg/chains"
)

Expand All @@ -24,24 +23,29 @@ func (m OutboundTxParams) Validate() error {
if chains.GetChainFromChainID(m.ReceiverChainId) == nil {
return fmt.Errorf("invalid receiver chain id %d", m.ReceiverChainId)
}
err := ValidateAddressForChain(m.Receiver, m.ReceiverChainId)
if err != nil {
return err
}

if m.Amount.IsNil() {
return fmt.Errorf("amount cannot be nil")
}
if m.OutboundTxBallotIndex != "" {
err = ValidateZetaIndex(m.OutboundTxBallotIndex)
if err != nil {
return errors.Wrap(err, "invalid outbound tx ballot index")
}
}
if m.OutboundTxHash != "" {
err = ValidateHashForChain(m.OutboundTxHash, m.ReceiverChainId)
if err != nil {
return errors.Wrap(err, "invalid outbound tx hash")
}
}

// Disabled checks
// TODO: Improve the checks, move the validation call to a new place and reenable
// https://github.com/zeta-chain/node/issues/2234
// https://github.com/zeta-chain/node/issues/2235
//if err := ValidateAddressForChain(m.Receiver, m.ReceiverChainId); err != nil {
// return err
//}
//if m.BallotIndex != "" {
//
// if err := ValidateCCTXIndex(m.BallotIndex); err != nil {
// return errors.Wrap(err, "invalid outbound tx ballot index")
// }
//}
//if m.Hash != "" {
// if err := ValidateHashForChain(m.Hash, m.ReceiverChainId); err != nil {
// return errors.Wrap(err, "invalid outbound tx hash")
// }
//}

return nil
}
21 changes: 15 additions & 6 deletions x/crosschain/types/outbound_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@ import (

func TestOutboundTxParams_Validate(t *testing.T) {
r := rand.New(rand.NewSource(42))

outTxParams := sample.OutboundTxParamsValidChainID(r)
outTxParams.Receiver = ""
require.ErrorContains(t, outTxParams.Validate(), "receiver cannot be empty")

outTxParams = sample.OutboundTxParamsValidChainID(r)
outTxParams.ReceiverChainId = 1000
require.ErrorContains(t, outTxParams.Validate(), "invalid receiver chain id 1000")
outTxParams = sample.OutboundTxParamsValidChainID(r)
outTxParams.Receiver = "0x123"
require.ErrorContains(t, outTxParams.Validate(), "invalid address 0x123")

outTxParams = sample.OutboundTxParamsValidChainID(r)
outTxParams.Amount = sdkmath.Uint{}
require.ErrorContains(t, outTxParams.Validate(), "amount cannot be nil")
outTxParams = sample.OutboundTxParamsValidChainID(r)
outTxParams.OutboundTxBallotIndex = "12"
require.ErrorContains(t, outTxParams.Validate(), "invalid index length 2")

outTxParams = sample.OutboundTxParamsValidChainID(r)
outTxParams.OutboundTxBallotIndex = sample.ZetaIndex(t)
outTxParams.OutboundTxHash = sample.Hash().String()
require.NoError(t, outTxParams.Validate())

// Disabled checks
// TODO: Improve the checks, move the validation call to a new place and reenable
// https://github.com/zeta-chain/node/issues/2234
// https://github.com/zeta-chain/node/issues/2235
//outTxParams = sample.OutboundParamsValidChainID(r)
//outTxParams.Receiver = "0x123"
//require.ErrorContains(t, outTxParams.Validate(), "invalid address 0x123")
//outTxParams = sample.OutboundParamsValidChainID(r)
//outTxParams.BallotIndex = "12"
//require.ErrorContains(t, outTxParams.Validate(), "invalid index length 2")
}

func TestOutboundTxParams_GetGasPrice(t *testing.T) {
Expand Down
Loading