Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Sep 10, 2024
1 parent 1799d79 commit 423a7c1
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
10 changes: 10 additions & 0 deletions x/ccv/provider/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ func (msg MsgCreateConsumer) ValidateBasic() error {
return errorsmod.Wrapf(ErrInvalidMsgCreateConsumer, "ChainId: %s", err.Error())
}

// With permissionless ICS, we can have multiple consumer chains with the exact same chain id.
// However, as we already have the Neutron and Stride Top N chains running, as a first step we would like to
// prevent permissionless chains from re-using the chain ids of Neutron and Stride. Note that this is just a
// preliminary measure that will be removed later on as part of:
// TODO (#2242): find a better way of ignoring past misbehaviors
if msg.ChainId == "neutron-1" || msg.ChainId == "stride-1" {
return errorsmod.Wrapf(ErrInvalidMsgCreateConsumer,
"cannot reuse chain ids of existing Neutron and Stride Top N consumer chains")
}

if err := ValidateConsumerMetadata(msg.Metadata); err != nil {
return errorsmod.Wrapf(ErrInvalidMsgCreateConsumer, "Metadata: %s", err.Error())
}
Expand Down
129 changes: 129 additions & 0 deletions x/ccv/provider/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,77 @@ func TestTruncateString(t *testing.T) {
}
}

func TestValidateConsumerMetadata(t *testing.T) {
generateLongString := func(length int) string {
result := make([]byte, length)
for i := range result {
result[i] = byte('a')
}
return string(result)
}

testCases := []struct {
name string
metadata types.ConsumerMetadata
valid bool
}{
{
name: "valid",
metadata: types.ConsumerMetadata{
Name: "name",
Description: "description",
Metadata: "metadata",
},
valid: true,
},
{
name: "valid with long strings",
metadata: types.ConsumerMetadata{
Name: generateLongString(types.MaxNameLength),
Description: generateLongString(types.MaxDescriptionLength),
Metadata: generateLongString(types.MaxMetadataLength),
},
valid: true,
},
{
name: "invalid name",
metadata: types.ConsumerMetadata{
Name: generateLongString(types.MaxNameLength + 1),
Description: "description",
Metadata: "metadata",
},
valid: false,
},
{
name: "invalid description",
metadata: types.ConsumerMetadata{
Name: "name",
Description: generateLongString(types.MaxDescriptionLength + 1),
Metadata: "metadata",
},
valid: false,
},
{
name: "invalid metadata",
metadata: types.ConsumerMetadata{
Name: "name",
Description: "description",
Metadata: generateLongString(types.MaxMetadataLength + 1),
},
valid: false,
},
}

for _, tc := range testCases {
err := types.ValidateConsumerMetadata(tc.metadata)
if tc.valid {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
}
}
}

func TestValidateInitializationParameters(t *testing.T) {
now := time.Now().UTC()
coolStr := "Cosmos Hub is the best place to launch a chain. Interchain Security is awesome."
Expand Down Expand Up @@ -367,6 +438,64 @@ func TestValidateByteSlice(t *testing.T) {
}
}

func TestMsgCreateConsumerValidateBasic(t *testing.T) {
testCases := []struct {
name string
chainId string
powerShapingParameters *types.PowerShapingParameters
expPass bool
}{
{
"empty chain id",
"",
nil, // no power-shaping parameters
false,
},
{
"empty chain id after trimming",
" ",
nil, // no power-shaping parameters
false,
},
{
"neutron chain id that cannot be reused",
"neutron-1",
nil, // no power-shaping parameters
false,
},
{
"stride chain id that cannot be reused",
"stride-1",
nil, // no power-shaping parameters
false,
},
{
"valid chain id",
"somechain-1",
nil, // no power-shaping parameters
true,
},
{
"valid chain id and invalid power-shaping parameters",
"somechain-1",
&types.PowerShapingParameters{Top_N: 51}, // TopN cannot be > 0 in MsgCreateConsumer
false,
},
}

for _, tc := range testCases {
validConsumerMetadata := types.ConsumerMetadata{Name: "name", Description: "description", Metadata: "metadata"}
msg, err := types.NewMsgCreateConsumer("submitter", tc.chainId, validConsumerMetadata, nil, tc.powerShapingParameters)
require.NoError(t, err)
err = msg.ValidateBasic()
if tc.expPass {
require.NoError(t, err, "valid case: %s should not return error. got %w", tc.name, err)
} else {
require.Error(t, err, "invalid case: '%s' must return error but got none", tc.name)
}
}
}

func TestMsgUpdateConsumerValidateBasic(t *testing.T) {
consAddr1 := "cosmosvalcons1qmq08eruchr5sf5s3rwz7djpr5a25f7xw4mceq"
consAddr2 := "cosmosvalcons1nx7n5uh0ztxsynn4sje6eyq2ud6rc6klc96w39"
Expand Down

0 comments on commit 423a7c1

Please sign in to comment.