From fe62e112f199a4e903cbc0ce03bfdaa0af65b553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E1=BA=B7c?= Date: Tue, 29 Aug 2023 19:42:27 +0700 Subject: [PATCH 1/2] fix update limit per addr --- x/tx-boundary/ante/antetest/ante_test.go | 84 +++++++++++++++++++++++- x/tx-boundary/keeper/keeper.go | 8 ++- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/x/tx-boundary/ante/antetest/ante_test.go b/x/tx-boundary/ante/antetest/ante_test.go index 0b135a2dd..c5e10bc99 100644 --- a/x/tx-boundary/ante/antetest/ante_test.go +++ b/x/tx-boundary/ante/antetest/ante_test.go @@ -19,13 +19,14 @@ func TestAnteTestSuite(t *testing.T) { suite.Run(t, new(AnteTestSuite)) } -func (s *AnteTestSuite) TestStakingAnte() { +func (s *AnteTestSuite) TestStakingAnteBasic() { _, _, addr1 := testdata.KeyTestPubAddr() delegateMsg := stakingtypes.NewMsgDelegate(s.delegator, s.validators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))) msgDelegateAny, err := cdctypes.NewAnyWithValue(delegateMsg) + require.NoError(s.T(), err) + addr2 := s.delegator - require.NoError(s.T(), err) for _, tc := range []struct { desc string txMsg sdk.Msg @@ -118,3 +119,82 @@ func (s *AnteTestSuite) TestStakingAnte() { } } } + +func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { + _, _, addr1 := testdata.KeyTestPubAddr() + delegateMsg := stakingtypes.NewMsgDelegate(s.delegator, s.validators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))) + + msgDelegateAny, err := cdctypes.NewAnyWithValue(delegateMsg) + require.NoError(s.T(), err) + + addr2 := s.delegator + + for _, tc := range []struct { + desc string + txMsg sdk.Msg + malleate func() error + expErr bool + expErrStr string + }{ + { + desc: "Case delegate success update limit", + txMsg: stakingtypes.NewMsgDelegate(s.delegator, s.validators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))), + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + expErr: false, + }, + { + desc: "Case redelegate success update limit", + txMsg: stakingtypes.NewMsgBeginRedelegate(s.delegator, s.validators[0].GetOperator(), s.newvalidators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))), + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + expErr: false, + }, + { + desc: "Case authz success update limit", + txMsg: &authz.MsgExec{Grantee: addr1.String(), Msgs: []*cdctypes.Any{msgDelegateAny}}, + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + expErr: false, + }, + } { + tc := tc + s.SetupTest() + tc.malleate() + s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder() + priv1, _, _ := testdata.KeyTestPubAddr() + privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + + mfd := txboundaryAnte.NewStakingPermissionDecorator(s.app.AppCodec(), s.app.TxBoundaryKeepper) + antehandler := sdk.ChainAnteDecorators(mfd) + s.Require().NoError(s.txBuilder.SetMsgs(tc.txMsg)) + + tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID()) + s.Require().NoError(err) + s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 5) + _, err = antehandler(s.ctx, tx, false) + if !tc.expErr { + s.Require().NoError(err) + } else { + s.Require().Error(err) + } + } +} diff --git a/x/tx-boundary/keeper/keeper.go b/x/tx-boundary/keeper/keeper.go index f02942f94..989ae7f83 100644 --- a/x/tx-boundary/keeper/keeper.go +++ b/x/tx-boundary/keeper/keeper.go @@ -149,10 +149,11 @@ func (k Keeper) UpdateLimitPerAddr(ctx sdk.Context, addr sdk.AccAddress) { return } boundary := k.GetDelegateBoundary(ctx) - if limitPerAddr.LatestUpdateBlock+int64(boundary.BlocksPerGeneration) >= ctx.BlockHeight() { + if limitPerAddr.LatestUpdateBlock+int64(boundary.BlocksPerGeneration) <= ctx.BlockHeight() { // Calculate the generated tx number from the duration between latest update block and current block height var generatedTx uint64 - duration := uint64(limitPerAddr.LatestUpdateBlock) + boundary.BlocksPerGeneration - uint64(ctx.BlockHeight()) + + duration := uint64(ctx.BlockHeight()) - uint64(limitPerAddr.LatestUpdateBlock) if duration/boundary.BlocksPerGeneration > 5 { generatedTx = 5 } else { @@ -173,6 +174,9 @@ func (k Keeper) UpdateLimitPerAddr(ctx sdk.Context, addr sdk.AccAddress) { } // Update LatestUpdateBlock limitPerAddr.LatestUpdateBlock = ctx.BlockHeight() + + // Set to store + k.SetLimitPerAddr(ctx, addr, limitPerAddr) return } } From e16f6dffd52f443f4c9692ce01ca4b80e61915db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E1=BA=B7c?= Date: Tue, 29 Aug 2023 19:48:06 +0700 Subject: [PATCH 2/2] update ante test cases --- x/tx-boundary/ante/antetest/ante_test.go | 64 ++++++++++++++++++++---- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/x/tx-boundary/ante/antetest/ante_test.go b/x/tx-boundary/ante/antetest/ante_test.go index c5e10bc99..1e52f0a60 100644 --- a/x/tx-boundary/ante/antetest/ante_test.go +++ b/x/tx-boundary/ante/antetest/ante_test.go @@ -130,11 +130,12 @@ func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { addr2 := s.delegator for _, tc := range []struct { - desc string - txMsg sdk.Msg - malleate func() error - expErr bool - expErrStr string + desc string + txMsg sdk.Msg + malleate func() error + blocksAdded int64 + expErr bool + expErrStr string }{ { desc: "Case delegate success update limit", @@ -147,7 +148,8 @@ func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { }) return nil }, - expErr: false, + blocksAdded: 5, + expErr: false, }, { desc: "Case redelegate success update limit", @@ -160,7 +162,8 @@ func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { }) return nil }, - expErr: false, + blocksAdded: 5, + expErr: false, }, { desc: "Case authz success update limit", @@ -173,7 +176,50 @@ func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { }) return nil }, - expErr: false, + blocksAdded: 5, + expErr: false, + }, + { + desc: "Case delegate fail update limit", + txMsg: stakingtypes.NewMsgDelegate(s.delegator, s.validators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))), + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + blocksAdded: 4, + expErr: true, + }, + { + desc: "Case redelegate fail update limit", + txMsg: stakingtypes.NewMsgBeginRedelegate(s.delegator, s.validators[0].GetOperator(), s.newvalidators[0].GetOperator(), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000))), + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + blocksAdded: 4, + expErr: true, + }, + { + desc: "Case authz success update limit", + txMsg: &authz.MsgExec{Grantee: addr1.String(), Msgs: []*cdctypes.Any{msgDelegateAny}}, + malleate: func() error { + s.app.TxBoundaryKeepper.SetLimitPerAddr(s.ctx, addr2, types.LimitPerAddr{ + DelegateCount: 5, + ReledegateCount: 5, + LatestUpdateBlock: s.ctx.BlockHeight(), + }) + return nil + }, + blocksAdded: 4, + expErr: true, }, } { tc := tc @@ -189,7 +235,7 @@ func (s *AnteTestSuite) TestStakingAnteUpdateLimit() { tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID()) s.Require().NoError(err) - s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 5) + s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + tc.blocksAdded) _, err = antehandler(s.ctx, tx, false) if !tc.expErr { s.Require().NoError(err)