Skip to content

Commit

Permalink
Merge commit from fork
Browse files Browse the repository at this point in the history
* add fix

* Revert "test: Fix use of hardcoded cosmos... address (backport #2122) (#2142)"

This reverts commit d7af672.

* add fix

* revert nit

* call validateBasic

* Revert "call validateBasic"

This reverts commit 463caea5ce267cb8e1f529ccd38dddbf7338a10f.
  • Loading branch information
sainoe authored and mpoke committed Sep 4, 2024
1 parent ac22ba3 commit 0a7309a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
28 changes: 21 additions & 7 deletions x/ccv/provider/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ func (msg MsgAssignConsumerKey) ValidateBasic() error {
if 128 < len(msg.ChainId) {
return errorsmod.Wrapf(ErrInvalidConsumerChainID, "chainId cannot exceed 128 length")
}
_, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
valAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
return ErrInvalidProviderAddress
}
// Check that the provider validator address and the signer address are the same
if sdk.AccAddress(valAddr.Bytes()).String() != msg.Signer {
return errorsmod.Wrapf(ErrInvalidProviderAddress, "provider validator address must be the same as the signer address")
}
if msg.ConsumerKey == "" {
return ErrInvalidConsumerConsensusPubKey
}
Expand Down Expand Up @@ -357,11 +361,14 @@ func (msg MsgOptIn) ValidateBasic() error {
if 128 < len(msg.ChainId) {
return errorsmod.Wrapf(ErrInvalidConsumerChainID, "chainId cannot exceed 128 length")
}
_, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
valAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
return ErrInvalidProviderAddress
}

// Check that the provider validator address and the signer address are the same
if sdk.AccAddress(valAddr.Bytes()).String() != msg.Signer {
return errorsmod.Wrapf(ErrInvalidProviderAddress, "provider validator address must be the same as the signer address")
}
if msg.ConsumerKey != "" {
if _, _, err := ParseConsumerKeyFromJson(msg.ConsumerKey); err != nil {
return ErrInvalidConsumerConsensusPubKey
Expand Down Expand Up @@ -415,10 +422,15 @@ func (msg MsgOptOut) ValidateBasic() error {
if 128 < len(msg.ChainId) {
return errorsmod.Wrapf(ErrInvalidConsumerChainID, "chainId cannot exceed 128 length")
}
_, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
valAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
return ErrInvalidProviderAddress
}
// Check that the provider validator address and the signer address are the same
if sdk.AccAddress(valAddr.Bytes()).String() != msg.Signer {
return errorsmod.Wrapf(ErrInvalidProviderAddress, "provider validator address must be the same as the signer address")
}

return nil
}

Expand All @@ -444,15 +456,17 @@ func (msg MsgSetConsumerCommissionRate) ValidateBasic() error {
if strings.TrimSpace(msg.ChainId) == "" {
return errorsmod.Wrapf(ErrInvalidConsumerChainID, "chainId cannot be blank")
}

if 128 < len(msg.ChainId) {
return errorsmod.Wrapf(ErrInvalidConsumerChainID, "chainId cannot exceed 128 length")
}
_, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
valAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
return ErrInvalidProviderAddress
}

// Check that the provider validator address and the signer address are the same
if sdk.AccAddress(valAddr.Bytes()).String() != msg.Signer {
return errorsmod.Wrapf(ErrInvalidProviderAddress, "provider validator address must be the same as the signer address")
}
if msg.Rate.IsNegative() || msg.Rate.GT(math.LegacyOneDec()) {
return errorsmod.Wrapf(ErrInvalidConsumerCommissionRate, "consumer commission rate should be in the range [0, 1]")
}
Expand Down
89 changes: 89 additions & 0 deletions x/ccv/provider/types/msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
cryptoutil "github.com/cosmos/interchain-security/v5/testutil/crypto"
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
"github.com/stretchr/testify/require"
)

func TestMsgAssignConsumerKeyValidateBasic(t *testing.T) {
cId1 := cryptoutil.NewCryptoIdentityFromIntSeed(35443543534)
cId2 := cryptoutil.NewCryptoIdentityFromIntSeed(65465464564)

valOpAddr1 := cId1.SDKValOpAddress()
acc1 := sdk.AccAddress(valOpAddr1.Bytes()).String()
acc2 := sdk.AccAddress(cId2.SDKValOpAddress().Bytes()).String()

longChainId := "abcdefghijklmnopqrstuvwxyz"
for i := 0; i < 3; i++ {
longChainId += longChainId
}

testCases := []struct {
name string
chainId string
providerAddr string
signer string
consumerKey string
expErr bool
}{
{
name: "chain Id empty",
expErr: true,
},
{
name: "chain Id too long",
chainId: longChainId,
expErr: true,
},
{
name: "invalid provider address",
chainId: "chainId",
expErr: true,
},
{
name: "invalid signer address: must be the same as the provider address",
chainId: "chainId",
providerAddr: valOpAddr1.String(),
signer: acc2,
expErr: true,
},
{
name: "invalid consumer pubkey",
chainId: "chainId",
providerAddr: valOpAddr1.String(),
signer: acc1,
expErr: true,
},
{
name: "valid assign consumer key msg",
chainId: "chainId",
providerAddr: valOpAddr1.String(),
consumerKey: "{\"@type\": \"/cosmos.crypto.ed25519.PubKey\", \"key\": \"e3BehnEIlGUAnJYn9V8gBXuMh4tXO8xxlxyXD1APGyk=\"}",
signer: acc1,
expErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

msg := types.MsgAssignConsumerKey{
ChainId: tc.chainId,
ConsumerKey: tc.consumerKey,
ProviderAddr: tc.providerAddr,
Signer: tc.signer,
}

err := msg.ValidateBasic()
if tc.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit 0a7309a

Please sign in to comment.