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 cecf756
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 18 deletions.
12 changes: 12 additions & 0 deletions x/reactions/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ func (msg MsgAddRegisteredReaction) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

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

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

// NewMsgEditRegisteredReaction returns a new MsgEditRegisteredReaction instance
Expand Down Expand Up @@ -252,6 +255,9 @@ func (msg MsgEditRegisteredReaction) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

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

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

// NewMsgRemoveRegisteredReaction returns a new MsgRemoveRegisteredReaction instance
Expand Down Expand Up @@ -302,6 +308,9 @@ func (msg MsgRemoveRegisteredReaction) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{addr}
}

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

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

// NewMsgSetReactionsParams returns a new MsgSetReactionsParams instance
Expand Down Expand Up @@ -354,3 +363,6 @@ func (msg MsgSetReactionsParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}

// IsManageSubspaceMsg implements subspacestypes.ManageSubspaceMsg
func (msg MsgSetReactionsParams) IsManageSubspaceMsg() {}
15 changes: 15 additions & 0 deletions x/reports/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"

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

var (
Expand All @@ -14,6 +16,10 @@ var (
_ sdk.Msg = &MsgSupportStandardReason{}
_ sdk.Msg = &MsgAddReason{}
_ sdk.Msg = &MsgRemoveReason{}

_ subspacestypes.ManageSubspaceMsg = &MsgSupportStandardReason{}
_ subspacestypes.ManageSubspaceMsg = &MsgAddReason{}
_ subspacestypes.ManageSubspaceMsg = &MsgRemoveReason{}
)

// NewMsgCreateReport returns a new MsgCreateReport instance
Expand Down Expand Up @@ -183,6 +189,9 @@ func (msg MsgSupportStandardReason) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

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

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

// NewMsgAddReason returns a new MsgAddReason instance
Expand Down Expand Up @@ -232,6 +241,9 @@ func (msg MsgAddReason) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

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

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

// NewMsgRemoveReason returns a new MsgRemoveReason instance
Expand Down Expand Up @@ -280,6 +292,9 @@ func (msg MsgRemoveReason) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

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

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

func NewMsgUpdateParams(params Params, authority string) *MsgUpdateParams {
Expand Down
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
Loading

0 comments on commit cecf756

Please sign in to comment.