Skip to content

Commit b4e158d

Browse files
authored
fix: take ICA into account for dyncomm ante (#450)
1 parent 7bd5634 commit b4e158d

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

app/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func NewTerraApp(
216216
TXCounterStoreKey: app.GetKey(wasm.StoreKey),
217217
DyncommKeeper: app.DyncommKeeper,
218218
StakingKeeper: app.StakingKeeper,
219+
Cdc: app.appCodec,
219220
},
220221
)
221222
if err != nil {

custom/auth/ante/ante.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ante
33
import (
44
dyncommante "github.com/classic-terra/core/v2/x/dyncomm/ante"
55
dyncommkeeper "github.com/classic-terra/core/v2/x/dyncomm/keeper"
6+
"github.com/cosmos/cosmos-sdk/codec"
67
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
78
ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante"
89
ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper"
@@ -36,6 +37,7 @@ type HandlerOptions struct {
3637
TXCounterStoreKey storetypes.StoreKey
3738
DyncommKeeper dyncommkeeper.Keeper
3839
StakingKeeper stakingkeeper.Keeper
40+
Cdc codec.BinaryCodec
3941
}
4042

4143
// NewAnteHandler returns an AnteHandler that checks and increments sequence
@@ -84,7 +86,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
8486
NewMinInitialDepositDecorator(options.GovKeeper, options.TreasuryKeeper),
8587
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
8688
NewFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TreasuryKeeper),
87-
dyncommante.NewDyncommDecorator(options.DyncommKeeper, options.StakingKeeper),
89+
dyncommante.NewDyncommDecorator(options.Cdc, options.DyncommKeeper, options.StakingKeeper),
8890

8991
// Do not add any other decorators below this point unless explicitly explain.
9092
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators

x/dyncomm/ante/ante.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ import (
44
"fmt"
55

66
dyncommkeeper "github.com/classic-terra/core/v2/x/dyncomm/keeper"
7+
"github.com/cosmos/cosmos-sdk/codec"
78
sdk "github.com/cosmos/cosmos-sdk/types"
89
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
910
authz "github.com/cosmos/cosmos-sdk/x/authz"
1011
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
1112
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
13+
icatypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/types"
14+
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
1215
)
1316

1417
// DyncommDecorator checks for EditValidator and rejects
1518
// edits that do not conform with dyncomm
1619
type DyncommDecorator struct {
1720
dyncommKeeper dyncommkeeper.Keeper
1821
stakingKeeper stakingkeeper.Keeper
22+
cdc codec.BinaryCodec
1923
}
2024

21-
func NewDyncommDecorator(dk dyncommkeeper.Keeper, sk stakingkeeper.Keeper) DyncommDecorator {
25+
func NewDyncommDecorator(cdc codec.BinaryCodec, dk dyncommkeeper.Keeper, sk stakingkeeper.Keeper) DyncommDecorator {
2226
return DyncommDecorator{
2327
dyncommKeeper: dk,
2428
stakingKeeper: sk,
29+
cdc: cdc,
2530
}
2631
}
2732

@@ -50,6 +55,19 @@ func (dd DyncommDecorator) FilterMsgsAndProcessMsgs(ctx sdk.Context, msgs ...sdk
5055
if msgerr == nil {
5156
err = dd.FilterMsgsAndProcessMsgs(ctx, messages...)
5257
}
58+
case *channeltypes.MsgRecvPacket:
59+
var data icatypes.InterchainAccountPacketData
60+
err = icatypes.ModuleCdc.UnmarshalJSON(msg.Packet.GetData(), &data)
61+
if err != nil {
62+
continue
63+
}
64+
if data.Type != icatypes.EXECUTE_TX {
65+
continue
66+
}
67+
messages, msgerr := icatypes.DeserializeCosmosTx(dd.cdc, data.Data)
68+
if msgerr == nil {
69+
err = dd.FilterMsgsAndProcessMsgs(ctx, messages...)
70+
}
5371
default:
5472
continue
5573
}

x/dyncomm/ante/ante_test.go

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"cosmossdk.io/math"
88
"github.com/classic-terra/core/v2/app"
99
core "github.com/classic-terra/core/v2/types"
10+
"github.com/gogo/protobuf/proto"
1011
"github.com/stretchr/testify/suite"
1112

1213
"github.com/cosmos/cosmos-sdk/client"
@@ -23,6 +24,9 @@ import (
2324
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
2425
authz "github.com/cosmos/cosmos-sdk/x/authz"
2526
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
27+
icatypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/types"
28+
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
29+
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
2630

2731
abci "github.com/tendermint/tendermint/abci/types"
2832
)
@@ -174,7 +178,7 @@ func (suite *AnteTestSuite) TestAnte_EnsureDynCommissionIsMinComm() {
174178
suite.CreateValidator(50_000_000_000)
175179
suite.App.DyncommKeeper.UpdateAllBondedValidatorRates(suite.Ctx)
176180

177-
mfd := dyncommante.NewDyncommDecorator(suite.App.DyncommKeeper, suite.App.StakingKeeper)
181+
mfd := dyncommante.NewDyncommDecorator(suite.App.AppCodec(), suite.App.DyncommKeeper, suite.App.StakingKeeper)
178182
antehandler := sdk.ChainAnteDecorators(mfd)
179183

180184
dyncomm := suite.App.DyncommKeeper.CalculateDynCommission(suite.Ctx, val1)
@@ -217,7 +221,7 @@ func (suite *AnteTestSuite) TestAnte_EnsureDynCommissionIsMinCommAuthz() {
217221
suite.CreateValidator(50_000_000_000)
218222
suite.App.DyncommKeeper.UpdateAllBondedValidatorRates(suite.Ctx)
219223

220-
mfd := dyncommante.NewDyncommDecorator(suite.App.DyncommKeeper, suite.App.StakingKeeper)
224+
mfd := dyncommante.NewDyncommDecorator(suite.App.AppCodec(), suite.App.DyncommKeeper, suite.App.StakingKeeper)
221225
antehandler := sdk.ChainAnteDecorators(mfd)
222226

223227
dyncomm := suite.App.DyncommKeeper.CalculateDynCommission(suite.Ctx, val1)
@@ -254,6 +258,81 @@ func (suite *AnteTestSuite) TestAnte_EnsureDynCommissionIsMinCommAuthz() {
254258
suite.Require().NoError(err)
255259
}
256260

261+
func (suite *AnteTestSuite) TestAnte_EnsureDynCommissionIsMinCommICA() {
262+
suite.SetupTest() // setup
263+
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
264+
suite.txBuilder.SetGasLimit(400_000)
265+
suite.Ctx = suite.Ctx.WithIsCheckTx(false)
266+
267+
_, _, val1, _ := suite.CreateValidator(50_000_000_000)
268+
priv2, _, _ := testdata.KeyTestPubAddr()
269+
suite.CreateValidator(50_000_000_000)
270+
suite.App.DyncommKeeper.UpdateAllBondedValidatorRates(suite.Ctx)
271+
272+
mfd := dyncommante.NewDyncommDecorator(suite.App.AppCodec(), suite.App.DyncommKeeper, suite.App.StakingKeeper)
273+
antehandler := sdk.ChainAnteDecorators(mfd)
274+
275+
dyncomm := suite.App.DyncommKeeper.CalculateDynCommission(suite.Ctx, val1)
276+
invalidtarget := dyncomm.Mul(sdk.NewDecWithPrec(9, 1))
277+
validtarget := dyncomm.Mul(sdk.NewDecWithPrec(11, 1))
278+
279+
// prepare invalid tx -> expect it fails
280+
editmsg := stakingtypes.NewMsgEditValidator(
281+
val1.GetOperator(),
282+
val1.Description, &invalidtarget, &val1.MinSelfDelegation,
283+
)
284+
data, err := icatypes.SerializeCosmosTx(suite.App.AppCodec(), []proto.Message{editmsg})
285+
suite.Require().NoError(err)
286+
icaPacketData := icatypes.InterchainAccountPacketData{
287+
Type: icatypes.EXECUTE_TX,
288+
Data: data,
289+
}
290+
packetData := icaPacketData.GetBytes()
291+
packet := channeltypes.NewPacket(
292+
packetData, 1, "source-port", "source-channel",
293+
"dest-port", "dest-channel",
294+
clienttypes.NewHeight(1, 1), 0,
295+
)
296+
recvmsg := channeltypes.NewMsgRecvPacket(
297+
packet, []byte{}, clienttypes.NewHeight(1, 1), "signer",
298+
)
299+
300+
err = suite.txBuilder.SetMsgs(recvmsg)
301+
suite.Require().NoError(err)
302+
tx, err := suite.CreateTestTx([]cryptotypes.PrivKey{priv2}, []uint64{0}, []uint64{0}, suite.Ctx.ChainID())
303+
suite.Require().NoError(err)
304+
_, err = antehandler(suite.Ctx, tx, false)
305+
suite.Require().Error(err)
306+
307+
// prepare valid tx -> expect it passes
308+
editmsg = stakingtypes.NewMsgEditValidator(
309+
val1.GetOperator(),
310+
val1.Description, &validtarget, &val1.MinSelfDelegation,
311+
)
312+
data, err = icatypes.SerializeCosmosTx(suite.App.AppCodec(), []proto.Message{editmsg})
313+
suite.Require().NoError(err)
314+
icaPacketData = icatypes.InterchainAccountPacketData{
315+
Type: icatypes.EXECUTE_TX,
316+
Data: data,
317+
}
318+
packetData = icaPacketData.GetBytes()
319+
packet = channeltypes.NewPacket(
320+
packetData, 1, "source-port", "source-channel",
321+
"dest-port", "dest-channel",
322+
clienttypes.NewHeight(1, 1), 0,
323+
)
324+
recvmsg = channeltypes.NewMsgRecvPacket(
325+
packet, []byte{}, clienttypes.NewHeight(1, 1), "signer",
326+
)
327+
328+
err = suite.txBuilder.SetMsgs(recvmsg)
329+
suite.Require().NoError(err)
330+
tx, err = suite.CreateTestTx([]cryptotypes.PrivKey{priv2}, []uint64{0}, []uint64{0}, suite.Ctx.ChainID())
331+
suite.Require().NoError(err)
332+
_, err = antehandler(suite.Ctx, tx, false)
333+
suite.Require().NoError(err)
334+
}
335+
257336
// go test -v -run ^TestAnteTestSuite/TestAnte_EditValidatorAccountSequence$ github.com/classic-terra/core/v2/x/dyncomm/ante
258337
// check that account keeper sequence no longer increases when editing validator unsuccessfully
259338
func (suite *AnteTestSuite) TestAnte_EditValidatorAccountSequence() {

0 commit comments

Comments
 (0)