Skip to content

Commit

Permalink
feat: decouple x/auth from simapp (#12360)
Browse files Browse the repository at this point in the history
## Description

Closes: #12302 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
likhita-809 authored Jun 29, 2022
1 parent b48ba98 commit ccb0081
Show file tree
Hide file tree
Showing 29 changed files with 560 additions and 487 deletions.
52 changes: 26 additions & 26 deletions x/auth/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ func (suite *AnteTestSuite) TestAnteHandlerSigErrors() {
{
"save the first account, but second is still unrecognized",
func() {
acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc1)
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feeAmount)
acc1 := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0)
suite.accountKeeper.SetAccount(suite.ctx, acc1)
err := suite.bankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feeAmount)
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr0, feeAmount)
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr0, feeAmount)
suite.Require().NoError(err)
},
false,
Expand Down Expand Up @@ -445,8 +445,8 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() {
// Same data for every test cases
priv0, _, addr0 := testdata.KeyTestPubAddr()

acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc1)
acc1 := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0)
suite.accountKeeper.SetAccount(suite.ctx, acc1)
msgs := []sdk.Msg{testdata.NewTestMsg(addr0)}
feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit()
Expand All @@ -471,7 +471,7 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() {
{
"signer does not have enough funds to pay the fee",
func() {
err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
suite.Require().NoError(err)
},
false,
Expand All @@ -483,12 +483,12 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() {
func() {
accNums = []uint64{acc1.GetAccountNumber()}

modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)
modAcc := suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)

suite.Require().True(suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).Empty())
require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(149)))
suite.Require().True(suite.bankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).Empty())
require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(149)))

err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 1)))
err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 1)))
suite.Require().NoError(err)
},
false,
Expand All @@ -498,10 +498,10 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() {
{
"signer doesn't have any more funds",
func() {
modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)
modAcc := suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)

require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150)))
require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(0)))
require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150)))
require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(0)))
},
false,
false,
Expand Down Expand Up @@ -820,7 +820,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() {
"make sure public key has been set (tx itself should fail because of replay protection)",
func() {
// Make sure public key has been set from previous test.
acc0 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[0].acc.GetAddress())
acc0 := suite.accountKeeper.GetAccount(suite.ctx, accounts[0].acc.GetAddress())
suite.Require().Equal(acc0.GetPubKey(), accounts[0].priv.PubKey())
},
false,
Expand All @@ -841,7 +841,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() {
"make sure public key is not set, when tx has no pubkey or signature",
func() {
// Make sure public key has not been set from previous test.
acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
acc1 := suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
suite.Require().Nil(acc1.GetPubKey())

privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{1}, []uint64{0}
Expand All @@ -863,7 +863,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() {
suite.Require().True(errors.Is(err, sdkerrors.ErrNoSignatures))

// Make sure public key has not been set.
acc1 = suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
acc1 = suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
suite.Require().Nil(acc1.GetPubKey())

// Set incorrect accSeq, to generate incorrect signature.
Expand All @@ -878,7 +878,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() {
func() {
// Make sure public key has been set, as SetPubKeyDecorator
// is called before all signature verification decorators.
acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
acc1 := suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress())
suite.Require().Equal(acc1.GetPubKey(), accounts[1].priv.PubKey())
},
false,
Expand Down Expand Up @@ -1011,9 +1011,9 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() {
// setup an ante handler that only accepts PubKeyEd25519
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: suite.app.AccountKeeper,
BankKeeper: suite.app.BankKeeper,
FeegrantKeeper: suite.app.FeeGrantKeeper,
AccountKeeper: suite.accountKeeper,
BankKeeper: suite.bankKeeper,
FeegrantKeeper: suite.feeGrantKeeper,
SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(),
SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error {
switch pubkey := sig.PubKey.(type) {
Expand Down Expand Up @@ -1116,14 +1116,14 @@ func (suite *AnteTestSuite) TestAnteHandlerReCheck() {
}
for _, tc := range testCases {
// set testcase parameters
suite.app.AccountKeeper.SetParams(suite.ctx, tc.params)
suite.accountKeeper.SetParams(suite.ctx, tc.params)

_, err := suite.anteHandler(suite.ctx, tx, false)

suite.Require().NotNil(err, "tx does not fail on recheck with updated params in test case: %s", tc.name)

// reset parameters to default values
suite.app.AccountKeeper.SetParams(suite.ctx, types.DefaultParams())
suite.accountKeeper.SetParams(suite.ctx, types.DefaultParams())
}

// require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck
Expand All @@ -1138,9 +1138,9 @@ func (suite *AnteTestSuite) TestAnteHandlerReCheck() {
suite.ctx = suite.ctx.WithMinGasPrices(sdk.DecCoins{})

// remove funds for account so antehandler fails on recheck
suite.app.AccountKeeper.SetAccount(suite.ctx, accounts[0].acc)
balances := suite.app.BankKeeper.GetAllBalances(suite.ctx, accounts[0].acc.GetAddress())
err = suite.app.BankKeeper.SendCoinsFromAccountToModule(suite.ctx, accounts[0].acc.GetAddress(), minttypes.ModuleName, balances)
suite.accountKeeper.SetAccount(suite.ctx, accounts[0].acc)
balances := suite.bankKeeper.GetAllBalances(suite.ctx, accounts[0].acc.GetAddress())
err = suite.bankKeeper.SendCoinsFromAccountToModule(suite.ctx, accounts[0].acc.GetAddress(), minttypes.ModuleName, balances)
suite.Require().NoError(err)

_, err = suite.anteHandler(suite.ctx, tx, false)
Expand Down
8 changes: 4 additions & 4 deletions x/auth/ante/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (suite *AnteTestSuite) TestValidateMemo() {
suite.Require().NoError(err)

// require that long memos get rejected
vmd := ante.NewValidateMemoDecorator(suite.app.AccountKeeper)
vmd := ante.NewValidateMemoDecorator(suite.accountKeeper)
antehandler := sdk.ChainAnteDecorators(vmd)
_, err = antehandler(suite.ctx, invalidTx, false)

Expand All @@ -99,7 +99,7 @@ func (suite *AnteTestSuite) TestConsumeGasForTxSize() {
feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit()

cgtsd := ante.NewConsumeGasForTxSizeDecorator(suite.app.AccountKeeper)
cgtsd := ante.NewConsumeGasForTxSizeDecorator(suite.accountKeeper)
antehandler := sdk.ChainAnteDecorators(cgtsd)

testCases := []struct {
Expand All @@ -125,15 +125,15 @@ func (suite *AnteTestSuite) TestConsumeGasForTxSize() {
txBytes, err := suite.clientCtx.TxConfig.TxJSONEncoder()(tx)
suite.Require().Nil(err, "Cannot marshal tx: %v", err)

params := suite.app.AccountKeeper.GetParams(suite.ctx)
params := suite.accountKeeper.GetParams(suite.ctx)
expectedGas := sdk.Gas(len(txBytes)) * params.TxSizeCostPerByte

// Set suite.ctx with TxBytes manually
suite.ctx = suite.ctx.WithTxBytes(txBytes)

// track how much gas is necessary to retrieve parameters
beforeGas := suite.ctx.GasMeter().GasConsumed()
suite.app.AccountKeeper.GetParams(suite.ctx)
suite.accountKeeper.GetParams(suite.ctx)
afterGas := suite.ctx.GasMeter().GasConsumed()
expectedGas += afterGas - beforeGas

Expand Down
16 changes: 8 additions & 8 deletions x/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ func (suite *AnteTestSuite) TestEnsureMempoolFees() {
suite.SetupTest(true) // setup
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, nil)
mfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, suite.feeGrantKeeper, nil)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins)
testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins)

// msg and signatures
msg := testdata.NewTestMsg(addr1)
Expand Down Expand Up @@ -85,22 +85,22 @@ func (suite *AnteTestSuite) TestDeductFees() {
suite.Require().NoError(err)

// Set account with insufficient funds
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr1)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr1)
suite.accountKeeper.SetAccount(suite.ctx, acc)
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))
err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins)
err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins)
suite.Require().NoError(err)

dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, nil, nil)
dfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, nil, nil)
antehandler := sdk.ChainAnteDecorators(dfd)

_, err = antehandler(suite.ctx, tx, false)

suite.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")

// Set account with sufficient funds
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
suite.accountKeeper.SetAccount(suite.ctx, acc)
err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
suite.Require().NoError(err)

_, err = antehandler(suite.ctx, tx, false)
Expand Down
22 changes: 10 additions & 12 deletions x/auth/ante/feegrant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import (

func (suite *AnteTestSuite) TestDeductFeesNoDelegation() {
suite.SetupTest(false)
// setup
app, ctx := suite.app, suite.ctx

protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes)
protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(suite.interfaceRegistry), tx.DefaultSignModes)

// this just tests our handler
dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, nil)
dfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, suite.feeGrantKeeper, nil)
feeAnteHandler := sdk.ChainAnteDecorators(dfd)

// this tests the whole stack
Expand All @@ -46,21 +44,21 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() {
priv5, _, addr5 := testdata.KeyTestPubAddr()

// Set addr1 with insufficient funds
err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))})
err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))})
suite.Require().NoError(err)

// Set addr2 with more funds
err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))})
err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))})
suite.Require().NoError(err)

// grant fee allowance from `addr2` to `addr3` (plenty to pay)
err = app.FeeGrantKeeper.GrantAllowance(ctx, addr2, addr3, &feegrant.BasicAllowance{
err = suite.feeGrantKeeper.GrantAllowance(suite.ctx, addr2, addr3, &feegrant.BasicAllowance{
SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)),
})
suite.Require().NoError(err)

// grant low fee allowance (20atom), to check the tx requesting more than allowed.
err = app.FeeGrantKeeper.GrantAllowance(ctx, addr2, addr4, &feegrant.BasicAllowance{
err = suite.feeGrantKeeper.GrantAllowance(suite.ctx, addr2, addr4, &feegrant.BasicAllowance{
SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)),
})
suite.Require().NoError(err)
Expand Down Expand Up @@ -138,22 +136,22 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() {
fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee))
msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)}

acc := app.AccountKeeper.GetAccount(ctx, tc.signer)
acc := suite.accountKeeper.GetAccount(suite.ctx, tc.signer)
privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0}
if acc != nil {
accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()}
}

tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, simtestutil.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...)
tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, simtestutil.DefaultGenTxGas, suite.ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...)
suite.Require().NoError(err)
_, err = feeAnteHandler(ctx, tx, false) // tests only feegrant ante
_, err = feeAnteHandler(suite.ctx, tx, false) // tests only feegrant ante
if tc.valid {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}

_, err = anteHandlerStack(ctx, tx, false) // tests while stack
_, err = anteHandlerStack(suite.ctx, tx, false) // tests while stack
if tc.valid {
suite.Require().NoError(err)
} else {
Expand Down
Loading

0 comments on commit ccb0081

Please sign in to comment.