Skip to content

Commit

Permalink
chore: implement manage subspace msg for custom tx fee checker
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Jun 14, 2023
1 parent 2272470 commit 3e15154
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 18 deletions.
20 changes: 20 additions & 0 deletions x/subspaces/ante/tx_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,41 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
antetypes "github.com/desmos-labs/desmos/v5/x/subspaces/ante/types"

"github.com/desmos-labs/desmos/v5/x/subspaces/types"
)

// hasManageSubspaceMsg checks if tx has manage subspace msg or not
func hasManageSubspaceMsg(tx sdk.Tx) bool {
for _, msg := range tx.GetMsgs() {
if _, ok := msg.(types.ManageSubspaceMsg); ok {
return true
}
}
return false
}

// CheckTxFeeWithSubspaceMinPrices returns the tx checker that including the subspace allowed tokens into minimum prices list
func CheckTxFeeWithSubspaceMinPrices(txFeeChecker ante.TxFeeChecker, sk antetypes.SubspacesKeeper) ante.TxFeeChecker {
return func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
// Perform default tx fee checker if tx is not subspace tx
subspaceID, isSubspaceTx := GetTxSubspaceID(tx)
if !isSubspaceTx {
return txFeeChecker(ctx, tx)
}

// Perform default tx fee checker if tx includes manage subspace msg
if hasManageSubspaceMsg(tx) {
return txFeeChecker(ctx, tx)
}

// Perform default tx fee checker if subspace not found
subspace, found := sk.GetSubspace(ctx, subspaceID)
if !found {
return txFeeChecker(ctx, tx)
}

// Perform custom tx fee checker with subspace additional fee tokens
newMinPrices := MergeMinPrices(ctx.MinGasPrices(), sdk.NewDecCoinsFromCoins(subspace.AdditionalFeeTokens...))
newCtx := ctx.WithMinGasPrices(newMinPrices)
return txFeeChecker(newCtx, tx)
Expand Down
81 changes: 64 additions & 17 deletions x/subspaces/ante/tx_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
subspacesante "github.com/desmos-labs/desmos/v5/x/subspaces/ante"
"github.com/desmos-labs/desmos/v5/x/subspaces/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

relationshipstypes "github.com/desmos-labs/desmos/v5/x/relationships/types"
subspacesante "github.com/desmos-labs/desmos/v5/x/subspaces/ante"
"github.com/desmos-labs/desmos/v5/x/subspaces/types"
)

func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
signer := sdk.MustAccAddressFromBech32("cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5")
subspaceMsg := types.NewMsgAddUserToUserGroup(1, 1, "cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53", "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5")
msg := relationshipstypes.NewMsgCreateRelationship("cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53", "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", 1)
manageSubspaceMsg := types.NewMsgAddUserToUserGroup(1, 1, "cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53", "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5")
nonSubspaceMsg := testdata.NewTestMsg(signer)

testCases := []struct {
Expand Down Expand Up @@ -62,7 +65,7 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
shouldErr: true,
},
{
name: "non-existing subspace tx returns no error",
name: "subspace not found returns no error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Expand All @@ -71,15 +74,15 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
fees: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(subspaceMsg)
txBuilder.SetMsgs(msg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(100)
return txBuilder.GetTx()
},
shouldErr: false,
},
{
name: "non-existing subspace tx with subspace fees returns error",
name: "subspace not found returns error - with subspace fee token",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Expand All @@ -88,15 +91,37 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
fees: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(subspaceMsg)
txBuilder.SetMsgs(msg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(100)
return txBuilder.GetTx()
},
shouldErr: true,
},
{
name: "manage subspace tx with subspace fees returns error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Return(
types.Subspace{
AdditionalFeeTokens: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(1))),
},
true,
)
},
fees: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(manageSubspaceMsg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(100)
return txBuilder.GetTx()
},
shouldErr: true,
},
{
name: "existing subspace tx returns no error",
name: "manage subspace tx returns no error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Expand All @@ -110,15 +135,15 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
fees: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(subspaceMsg)
txBuilder.SetMsgs(manageSubspaceMsg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(100)
return txBuilder.GetTx()
},
shouldErr: false,
},
{
name: "existing subspace tx with subspace fees returns no error",
name: "non manage subspace tx with insufficient subspace fees returns error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Expand All @@ -132,15 +157,37 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
fees: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(subspaceMsg)
txBuilder.SetMsgs(msg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(1)
txBuilder.SetGasLimit(1000)
return txBuilder.GetTx()
},
shouldErr: true,
},
{
name: "non manage subspace tx returns no error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Return(
types.Subspace{
AdditionalFeeTokens: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(1))),
},
true,
)
},
fees: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(msg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(100)
return txBuilder.GetTx()
},
shouldErr: false,
},
{
name: "existing subspace tx with insufficient subspace fees returns error",
name: "non manage subspace tx with subspace fees returns no error",
setup: func() {
suite.sk.EXPECT().
GetSubspace(gomock.Any(), uint64(1)).
Expand All @@ -151,15 +198,15 @@ func (suite *AnteTestSuite) TestCheckTxFeeWithSubspaceMinPrices() {
true,
)
},
fees: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(10))),
fees: sdk.NewCoins(sdk.NewCoin("minttoken", sdk.NewInt(100))),
buildTx: func(fees sdk.Coins) sdk.Tx {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(subspaceMsg)
txBuilder.SetMsgs(msg)
txBuilder.SetFeeAmount(fees)
txBuilder.SetGasLimit(1000)
txBuilder.SetGasLimit(1)
return txBuilder.GetTx()
},
shouldErr: true,
shouldErr: false,
},
}

Expand Down
65 changes: 64 additions & 1 deletion x/subspaces/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type SubspaceMsg interface {
GetSubspaceID() uint64
}

// ManageSubspaceMsg represents a generic message that is related to a management purpose
type ManageSubspaceMsg interface {
SubspaceMsg

IsManageSubspaceMsg()
}

var (
_ sdk.Msg = &MsgCreateSubspace{}
_ sdk.Msg = &MsgEditSubspace{}
Expand All @@ -34,7 +41,21 @@ var (
_ sdk.Msg = &MsgAddUserToUserGroup{}
_ sdk.Msg = &MsgRemoveUserFromUserGroup{}
_ sdk.Msg = &MsgSetUserPermissions{}
_ sdk.Msg = &MsgGrantTreasuryAuthorization{}

_ ManageSubspaceMsg = &MsgEditSubspace{}
_ ManageSubspaceMsg = &MsgDeleteSubspace{}
_ ManageSubspaceMsg = &MsgCreateSection{}
_ ManageSubspaceMsg = &MsgEditSection{}
_ ManageSubspaceMsg = &MsgMoveSection{}
_ ManageSubspaceMsg = &MsgDeleteSection{}
_ ManageSubspaceMsg = &MsgCreateUserGroup{}
_ ManageSubspaceMsg = &MsgEditUserGroup{}
_ ManageSubspaceMsg = &MsgMoveUserGroup{}
_ ManageSubspaceMsg = &MsgSetUserGroupPermissions{}
_ ManageSubspaceMsg = &MsgDeleteUserGroup{}
_ ManageSubspaceMsg = &MsgAddUserToUserGroup{}
_ ManageSubspaceMsg = &MsgRemoveUserFromUserGroup{}
_ ManageSubspaceMsg = &MsgSetUserPermissions{}
)

// NewMsgCreateSubspace creates a new MsgCreateSubspace instance
Expand Down Expand Up @@ -143,6 +164,9 @@ func (msg MsgEditSubspace) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgEditSubspace) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgDeleteSubspace returns a new MsgDeleteSubspace instance
Expand Down Expand Up @@ -184,6 +208,9 @@ func (msg MsgDeleteSubspace) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgDeleteSubspace) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgCreateSection returns a new MsgCreateSection instance
Expand Down Expand Up @@ -238,6 +265,9 @@ func (msg MsgCreateSection) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgCreateSection) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgEditSection returns a new MsgEditSection instance
Expand Down Expand Up @@ -292,6 +322,9 @@ func (msg MsgEditSection) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgEditSection) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgMoveSection returns a new MsgMoveSection instance
Expand Down Expand Up @@ -344,6 +377,9 @@ func (msg MsgMoveSection) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgMoveSection) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgDeleteSection returns a new MsgDeleteSection instance
Expand Down Expand Up @@ -390,6 +426,9 @@ func (msg MsgDeleteSection) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgDeleteSection) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgCreateUserGroup creates a new MsgCreateUserGroup instance
Expand Down Expand Up @@ -459,6 +498,9 @@ func (msg MsgCreateUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgCreateUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgEditUserGroup returns a new NewMsgEditUserGroup instance
Expand Down Expand Up @@ -509,6 +551,9 @@ func (msg MsgEditUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgEditUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgMoveUserGroup returns a new NewMsgMoveUserGroup instance
Expand Down Expand Up @@ -561,6 +606,9 @@ func (msg MsgMoveUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgMoveUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgSetUserGroupPermissions returns a new MsgSetUserGroupPermissions instance
Expand Down Expand Up @@ -613,6 +661,9 @@ func (msg MsgSetUserGroupPermissions) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgSetUserGroupPermissions) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgDeleteUserGroup creates a new MsgDeleteUserGroup instance
Expand Down Expand Up @@ -659,6 +710,9 @@ func (msg MsgDeleteUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgDeleteUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgAddUserToUserGroup creates a new MsgAddUserToUserGroup instance
Expand Down Expand Up @@ -716,6 +770,9 @@ func (msg MsgAddUserToUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgAddUserToUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgRemoveUserFromUserGroup creates a new MsgRemoveUserFromUserGroup instance
Expand Down Expand Up @@ -773,6 +830,9 @@ func (msg MsgRemoveUserFromUserGroup) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgRemoveUserFromUserGroup) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

// NewMsgSetUserPermissions creates a new MsgSetUserPermissions instance
Expand Down Expand Up @@ -836,6 +896,9 @@ func (msg MsgSetUserPermissions) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgSetUserPermissions) IsManageSubspaceMsg() {}

// --------------------------------------------------------------------------------------------------------------------

var (
Expand Down
Loading

0 comments on commit 3e15154

Please sign in to comment.