Skip to content

Commit

Permalink
Merge pull request #69 from b-harvest/patch/govshuttle_msg_validatebasic
Browse files Browse the repository at this point in the history
bug: Add legacy validate basic logic to govshuttle msgs
  • Loading branch information
dudong2 authored Jul 19, 2024
2 parents c2369a0 + e2dc217 commit ca7a7f8
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 26 deletions.
17 changes: 17 additions & 0 deletions x/govshuttle/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -14,6 +15,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/version"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/Canto-Network/Canto/v7/x/govshuttle/types"
)
Expand Down Expand Up @@ -78,6 +80,15 @@ Where metadata.json contains (example):
return errorsmod.Wrap(err, "Failure to parse JSON object")
}

// validate basic logic
cd, vals, sigs := len(propMetaData.GetCalldatas()), len(propMetaData.GetValues()), len(propMetaData.GetSignatures())
if cd != vals {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal array arguments must be same length")
}
if vals != sigs {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal array arguments must be same length")
}

authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = ac.StringToBytes(authority); err != nil {
Expand Down Expand Up @@ -143,6 +154,12 @@ Where metadata.json contains (example):
return errorsmod.Wrap(err, "Failure to parse JSON object")
}

// validate basic logic
s := strings.ToLower(propMetaData.GetDenom())
if s != "canto" && s != "note" {
return errorsmod.Wrapf(govtypes.ErrInvalidProposalContent, "%s is not a valid denom string", propMetaData.GetDenom())
}

authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = ac.StringToBytes(authority); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions x/govshuttle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strings"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -27,6 +28,15 @@ func (k Keeper) LendingMarketProposal(ctx context.Context, req *types.MsgLending
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}

// validate basic logic
cd, vals, sigs := len(req.Metadata.GetCalldatas()), len(req.Metadata.GetValues()), len(req.Metadata.GetSignatures())
if cd != vals {
return nil, errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal array arguments must be same length")
}
if vals != sigs {
return nil, errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal array arguments must be same length")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
_, err := k.AppendLendingMarketProposal(sdkCtx, req)
if err != nil {
Expand All @@ -41,6 +51,12 @@ func (k Keeper) TreasuryProposal(ctx context.Context, req *types.MsgTreasuryProp
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}

// validate basic logic
s := strings.ToLower(req.Metadata.GetDenom())
if s != "canto" && s != "note" {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidProposalContent, "%s is not a valid denom string", req.Metadata.GetDenom())
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
_, err := k.AppendLendingMarketProposal(sdkCtx, req.FromTreasuryToLendingMarket())
if err != nil {
Expand Down
94 changes: 68 additions & 26 deletions x/govshuttle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
suite.Require().True(shares.GT(sdkmath.LegacyNewDec(0)))

testCases := []struct {
name string
msg sdk.Msg
checkFunc func(uint64, sdk.Msg)
expectErr bool
name string
msg sdk.Msg
checkFunc func(uint64, sdk.Msg)
expectSubmitProposalErr bool
expectProposalFailed bool
}{
{
"fail - MsgLendingMarketProposal - authority check",
Expand All @@ -77,14 +78,33 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
Description: "lending market proposal test description",
Metadata: &govshuttletypes.LendingMarketMetadata{
Account: []string{"0x20F72265e2225837fd77C692e0781f720B93eF89", "0xf6Db2570A2417188a5788D6d5Fd9faAa5B1fE555"},
PropId: 1,
PropId: 0,
Values: []uint64{1234, 5678},
Calldatas: []string{hex.EncodeToString([]byte("calldata1")), hex.EncodeToString([]byte("calldata2"))},
Signatures: []string{"sig1", "sig2"},
},
},
func(uint64, sdk.Msg) {},
true,
false,
},
{
"fail - MsgLendingMarketProposal - validate basic logic",
&govshuttletypes.MsgLendingMarketProposal{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Title: "lending market proposal test",
Description: "lending market proposal test description",
Metadata: &govshuttletypes.LendingMarketMetadata{
Account: []string{"0x20F72265e2225837fd77C692e0781f720B93eF89", "0xf6Db2570A2417188a5788D6d5Fd9faAa5B1fE555"},
PropId: 0,
Values: []uint64{1234},
Calldatas: []string{hex.EncodeToString([]byte("calldata1")), hex.EncodeToString([]byte("calldata2"))},
Signatures: []string{"sig1", "sig2"},
},
},
func(uint64, sdk.Msg) {},
false,
true,
},
{
"ok - MsgLendingMarketProposal",
Expand All @@ -94,17 +114,13 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
Description: "lending market proposal test description",
Metadata: &govshuttletypes.LendingMarketMetadata{
Account: []string{"0x20F72265e2225837fd77C692e0781f720B93eF89", "0xf6Db2570A2417188a5788D6d5Fd9faAa5B1fE555"},
PropId: 1,
PropId: 0,
Values: []uint64{1234, 5678},
Calldatas: []string{hex.EncodeToString([]byte("calldata1")), hex.EncodeToString([]byte("calldata2"))},
Signatures: []string{"sig1", "sig2"},
},
},
func(proposalId uint64, msg sdk.Msg) {
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

func(propId uint64, msg sdk.Msg) {
proposalMsg, ok := msg.(*govshuttletypes.MsgLendingMarketProposal)
suite.Require().True(ok)

Expand All @@ -127,9 +143,9 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
}

suite.checkQueryPropResult(
proposalId,
propId,
ProposalResult{
Id: big.NewInt(int64(proposalMsg.Metadata.PropId)),
Id: big.NewInt(int64(propId)),
Title: proposalMsg.Title,
Desc: proposalMsg.Description,
Targets: targets,
Expand All @@ -140,6 +156,7 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
)
},
false,
false,
},
{
"fail - MsgTreasuryProposal - authority check",
Expand All @@ -148,14 +165,32 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
Title: "treasury proposal test",
Description: "treasury proposal test description",
Metadata: &govshuttletypes.TreasuryProposalMetadata{
PropID: 2,
PropID: 0,
Recipient: "0x20F72265e2225837fd77C692e0781f720B93eF89",
Amount: 1234,
Denom: "acanto",
},
},
func(uint64, sdk.Msg) {},
true,
false,
},
{
"fail - MsgTreasuryProposal - validate basic logic",
&govshuttletypes.MsgTreasuryProposal{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Title: "treasury proposal test",
Description: "treasury proposal test description",
Metadata: &govshuttletypes.TreasuryProposalMetadata{
PropID: 0,
Recipient: "0x20F72265e2225837fd77C692e0781f720B93eF89",
Amount: 1234,
Denom: "canto2",
},
},
func(uint64, sdk.Msg) {},
false,
true,
},
{
"ok - MsgTreasuryProposal",
Expand All @@ -164,17 +199,13 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
Title: "treasury proposal test",
Description: "treasury proposal test description",
Metadata: &govshuttletypes.TreasuryProposalMetadata{
PropID: 2,
PropID: 0,
Recipient: "0x20F72265e2225837fd77C692e0781f720B93eF89",
Amount: 1234,
Denom: "acanto",
Denom: "canto",
},
},
func(proposalId uint64, msg sdk.Msg) {
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

func(propId uint64, msg sdk.Msg) {
proposalMsg, ok := msg.(*govshuttletypes.MsgTreasuryProposal)
suite.Require().True(ok)

Expand All @@ -184,9 +215,9 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
calldatas := [][]byte{}

suite.checkQueryPropResult(
proposalId,
propId,
ProposalResult{
Id: big.NewInt(int64(proposalMsg.Metadata.PropID)),
Id: big.NewInt(int64(propId)),
Title: proposalMsg.Title,
Desc: proposalMsg.Description,
Targets: targets,
Expand All @@ -197,14 +228,15 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
)
},
false,
false,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
// submit proposal
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{tc.msg}, "", "test", "description", proposer, false)
if tc.expectErr {
if tc.expectSubmitProposalErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
Expand All @@ -219,8 +251,18 @@ func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
suite.Require().NoError(err)
suite.CommitAfter(*govParams.VotingPeriod)

// check proposal result
tc.checkFunc(proposal.Id, tc.msg)
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposal.Id)
suite.Require().NoError(err)
if tc.expectProposalFailed {
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_FAILED, proposal.Status)
} else {
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

// check proposal result
propId, err := suite.app.GovKeeper.ProposalID.Peek(suite.ctx)
suite.Require().NoError(err)
tc.checkFunc(propId, tc.msg)
}
}
})
}
Expand Down

0 comments on commit ca7a7f8

Please sign in to comment.