From cc6fb6e6cb86e69a9da00a75c404df2c545a06dd Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Mon, 22 Jul 2019 12:35:52 +0200 Subject: [PATCH 1/8] restructure x/auth --- go.mod | 1 - x/auth/alias.go | 105 +++++++++++------- x/auth/{ => ante}/ante.go | 54 ++++----- x/auth/{ => ante}/ante_test.go | 29 ++--- x/auth/{ => keeper}/keeper.go | 2 +- x/auth/{ => keeper}/keeper_bench_test.go | 10 +- x/auth/{ => keeper}/keeper_test.go | 21 ++-- x/auth/{ => keeper}/querier.go | 2 +- x/auth/{ => keeper}/querier_test.go | 6 +- x/auth/{ => keeper}/test_common.go | 5 +- x/mock/app.go | 9 +- x/slashing/internal/types/expected_keepers.go | 14 +-- x/supply/genesis.go | 4 +- x/supply/internal/keeper/test_common.go | 3 +- 14 files changed, 146 insertions(+), 119 deletions(-) rename x/auth/{ => ante}/ante.go (86%) rename x/auth/{ => ante}/ante_test.go (98%) rename x/auth/{ => keeper}/keeper.go (99%) rename x/auth/{ => keeper}/keeper_bench_test.go (95%) rename x/auth/{ => keeper}/keeper_test.go (83%) rename x/auth/{ => keeper}/querier.go (98%) rename x/auth/{ => keeper}/querier_test.go (89%) rename x/auth/{ => keeper}/test_common.go (98%) diff --git a/go.mod b/go.mod index f20d9f8e5b81..6809ae310151 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/fortytw2/leaktest v1.3.0 // indirect github.com/gogo/protobuf v1.2.1 github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 - github.com/golang/protobuf v1.3.0 github.com/gorilla/mux v1.7.0 github.com/gorilla/websocket v1.4.0 // indirect github.com/mattn/go-isatty v0.0.6 diff --git a/x/auth/alias.go b/x/auth/alias.go index 2f129ea302a9..e4ba5e68fc72 100644 --- a/x/auth/alias.go +++ b/x/auth/alias.go @@ -1,11 +1,15 @@ // nolint // autogenerated code using github.com/rigelrozanski/multitool // aliases generated for the following subdirectories: +// ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/ante +// ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/keeper // ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/types package auth import ( + "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/exported" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -25,33 +29,46 @@ const ( var ( // functions aliases - NewBaseAccount = types.NewBaseAccount - ProtoBaseAccount = types.ProtoBaseAccount - NewBaseAccountWithAddress = types.NewBaseAccountWithAddress - NewBaseVestingAccount = types.NewBaseVestingAccount - NewContinuousVestingAccountRaw = types.NewContinuousVestingAccountRaw - NewContinuousVestingAccount = types.NewContinuousVestingAccount - NewDelayedVestingAccountRaw = types.NewDelayedVestingAccountRaw - NewDelayedVestingAccount = types.NewDelayedVestingAccount - RegisterCodec = types.RegisterCodec - NewGenesisState = types.NewGenesisState - DefaultGenesisState = types.DefaultGenesisState - ValidateGenesis = types.ValidateGenesis - AddressStoreKey = types.AddressStoreKey - NewParams = types.NewParams - ParamKeyTable = types.ParamKeyTable - DefaultParams = types.DefaultParams - NewQueryAccountParams = types.NewQueryAccountParams - NewStdTx = types.NewStdTx - CountSubKeys = types.CountSubKeys - NewStdFee = types.NewStdFee - StdSignBytes = types.StdSignBytes - DefaultTxDecoder = types.DefaultTxDecoder - DefaultTxEncoder = types.DefaultTxEncoder - NewTxBuilder = types.NewTxBuilder - NewTxBuilderFromCLI = types.NewTxBuilderFromCLI - MakeSignature = types.MakeSignature - NewAccountRetriever = types.NewAccountRetriever + NewAnteHandler = ante.NewAnteHandler + GetSignerAcc = ante.GetSignerAcc + ValidateSigCount = ante.ValidateSigCount + ValidateMemo = ante.ValidateMemo + ProcessPubKey = ante.ProcessPubKey + DefaultSigVerificationGasConsumer = ante.DefaultSigVerificationGasConsumer + DeductFees = ante.DeductFees + EnsureSufficientMempoolFees = ante.EnsureSufficientMempoolFees + SetGasMeter = ante.SetGasMeter + GetSignBytes = ante.GetSignBytes + NewAccountKeeper = keeper.NewAccountKeeper + NewDummySupplyKeeper = keeper.NewDummySupplyKeeper + NewQuerier = keeper.NewQuerier + NewBaseAccount = types.NewBaseAccount + ProtoBaseAccount = types.ProtoBaseAccount + NewBaseAccountWithAddress = types.NewBaseAccountWithAddress + NewBaseVestingAccount = types.NewBaseVestingAccount + NewContinuousVestingAccountRaw = types.NewContinuousVestingAccountRaw + NewContinuousVestingAccount = types.NewContinuousVestingAccount + NewDelayedVestingAccountRaw = types.NewDelayedVestingAccountRaw + NewDelayedVestingAccount = types.NewDelayedVestingAccount + NewAccountRetriever = types.NewAccountRetriever + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + AddressStoreKey = types.AddressStoreKey + NewParams = types.NewParams + ParamKeyTable = types.ParamKeyTable + DefaultParams = types.DefaultParams + NewQueryAccountParams = types.NewQueryAccountParams + NewStdTx = types.NewStdTx + CountSubKeys = types.CountSubKeys + NewStdFee = types.NewStdFee + StdSignBytes = types.StdSignBytes + DefaultTxDecoder = types.DefaultTxDecoder + DefaultTxEncoder = types.DefaultTxEncoder + NewTxBuilder = types.NewTxBuilder + NewTxBuilderFromCLI = types.NewTxBuilderFromCLI + MakeSignature = types.MakeSignature // variable aliases ModuleCdc = types.ModuleCdc @@ -65,19 +82,23 @@ var ( ) type ( - Account = exported.Account - VestingAccount = exported.VestingAccount - BaseAccount = types.BaseAccount - BaseVestingAccount = types.BaseVestingAccount - ContinuousVestingAccount = types.ContinuousVestingAccount - DelayedVestingAccount = types.DelayedVestingAccount - GenesisState = types.GenesisState - Params = types.Params - QueryAccountParams = types.QueryAccountParams - StdSignMsg = types.StdSignMsg - StdTx = types.StdTx - StdFee = types.StdFee - StdSignDoc = types.StdSignDoc - StdSignature = types.StdSignature - TxBuilder = types.TxBuilder + SignatureVerificationGasConsumer = ante.SignatureVerificationGasConsumer + Account = exported.Account + VestingAccount = exported.VestingAccount + AccountKeeper = keeper.AccountKeeper + BaseAccount = types.BaseAccount + BaseVestingAccount = types.BaseVestingAccount + ContinuousVestingAccount = types.ContinuousVestingAccount + DelayedVestingAccount = types.DelayedVestingAccount + NodeQuerier = types.NodeQuerier + AccountRetriever = types.AccountRetriever + GenesisState = types.GenesisState + Params = types.Params + QueryAccountParams = types.QueryAccountParams + StdSignMsg = types.StdSignMsg + StdTx = types.StdTx + StdFee = types.StdFee + StdSignDoc = types.StdSignDoc + StdSignature = types.StdSignature + TxBuilder = types.TxBuilder ) diff --git a/x/auth/ante.go b/x/auth/ante/ante.go similarity index 86% rename from x/auth/ante.go rename to x/auth/ante/ante.go index 9387b07ecd39..01d1f0da72de 100644 --- a/x/auth/ante.go +++ b/x/auth/ante/ante.go @@ -1,4 +1,4 @@ -package auth +package ante import ( "bytes" @@ -13,6 +13,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/exported" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -30,12 +32,12 @@ func init() { // SignatureVerificationGasConsumer is the type of function that is used to both consume gas when verifying signatures // and also to accept or reject different types of PubKey's. This is where apps can define their own PubKey -type SignatureVerificationGasConsumer = func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params) sdk.Result +type SignatureVerificationGasConsumer = func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result // NewAnteHandler returns an AnteHandler that checks and increments sequence // numbers, checks signatures & account numbers, and deducts fees from the first // signer. -func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasConsumer SignatureVerificationGasConsumer) sdk.AnteHandler { +func NewAnteHandler(ak keeper.AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasConsumer SignatureVerificationGasConsumer) sdk.AnteHandler { return func( ctx sdk.Context, tx sdk.Tx, simulate bool, ) (newCtx sdk.Context, res sdk.Result, abort bool) { @@ -45,12 +47,12 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon } // all transactions must be of type auth.StdTx - stdTx, ok := tx.(StdTx) + stdTx, ok := tx.(types.StdTx) if !ok { // Set a gas meter with limit 0 as to prevent an infinite gas meter attack // during runTx. newCtx = SetGasMeter(simulate, ctx, 0) - return newCtx, sdk.ErrInternal("tx must be StdTx").Result(), true + return newCtx, sdk.ErrInternal("tx must be types.StdTx").Result(), true } params := ak.GetParams(ctx) @@ -107,7 +109,7 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon // stdSigs contains the sequence number, account number, and signatures. // When simulating, this would just be a 0-length slice. signerAddrs := stdTx.GetSigners() - signerAccs := make([]Account, len(signerAddrs)) + signerAccs := make([]exported.Account, len(signerAddrs)) isGenesis := ctx.BlockHeight() == 0 // fetch first signer, who's going to pay the fees @@ -157,7 +159,7 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon // GetSignerAcc returns an account for a given address that is expected to sign // a transaction. -func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (Account, sdk.Result) { +func GetSignerAcc(ctx sdk.Context, ak keeper.AccountKeeper, addr sdk.AccAddress) (exported.Account, sdk.Result) { if acc := ak.GetAccount(ctx, addr); acc != nil { return acc, sdk.Result{} } @@ -166,12 +168,12 @@ func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (Accou // ValidateSigCount validates that the transaction has a valid cumulative total // amount of signatures. -func ValidateSigCount(stdTx StdTx, params Params) sdk.Result { +func ValidateSigCount(stdTx types.StdTx, params types.Params) sdk.Result { stdSigs := stdTx.GetSignatures() sigCount := 0 for i := 0; i < len(stdSigs); i++ { - sigCount += CountSubKeys(stdSigs[i].PubKey) + sigCount += types.CountSubKeys(stdSigs[i].PubKey) if uint64(sigCount) > params.TxSigLimit { return sdk.ErrTooManySignatures( fmt.Sprintf("signatures: %d, limit: %d", sigCount, params.TxSigLimit), @@ -183,7 +185,7 @@ func ValidateSigCount(stdTx StdTx, params Params) sdk.Result { } // ValidateMemo validates the memo size. -func ValidateMemo(stdTx StdTx, params Params) sdk.Result { +func ValidateMemo(stdTx types.StdTx, params types.Params) sdk.Result { memoLength := len(stdTx.GetMemo()) if uint64(memoLength) > params.MaxMemoCharacters { return sdk.ErrMemoTooLarge( @@ -200,9 +202,9 @@ func ValidateMemo(stdTx StdTx, params Params) sdk.Result { // verify the signature and increment the sequence. If the account doesn't have // a pubkey, set it. func processSig( - ctx sdk.Context, acc Account, sig StdSignature, signBytes []byte, simulate bool, params Params, + ctx sdk.Context, acc exported.Account, sig types.StdSignature, signBytes []byte, simulate bool, params types.Params, sigGasConsumer SignatureVerificationGasConsumer, -) (updatedAcc Account, res sdk.Result) { +) (updatedAcc exported.Account, res sdk.Result) { pubKey, res := ProcessPubKey(acc, sig, simulate) if !res.IsOK() { @@ -217,7 +219,7 @@ func processSig( if simulate { // Simulated txs should not contain a signature and are not required to // contain a pubkey, so we must account for tx size of including a - // StdSignature (Amino encoding) and simulate gas consumption + // types.StdSignature (Amino encoding) and simulate gas consumption // (assuming a SECP256k1 simulation key). consumeSimSigGas(ctx.GasMeter(), pubKey, sig, params) } @@ -237,13 +239,13 @@ func processSig( return acc, res } -func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig StdSignature, params Params) { - simSig := StdSignature{PubKey: pubkey} +func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig types.StdSignature, params types.Params) { + simSig := types.StdSignature{PubKey: pubkey} if len(sig.Signature) == 0 { simSig.Signature = simSecp256k1Sig[:] } - sigBz := ModuleCdc.MustMarshalBinaryLengthPrefixed(simSig) + sigBz := types.ModuleCdc.MustMarshalBinaryLengthPrefixed(simSig) cost := sdk.Gas(len(sigBz) + 6) // If the pubkey is a multi-signature pubkey, then we estimate for the maximum @@ -256,10 +258,10 @@ func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig StdSignat } // ProcessPubKey verifies that the given account address matches that of the -// StdSignature. In addition, it will set the public key of the account if it +// types.StdSignature. In addition, it will set the public key of the account if it // has not been set. -func ProcessPubKey(acc Account, sig StdSignature, simulate bool) (crypto.PubKey, sdk.Result) { - // If pubkey is not known for account, set it from the StdSignature. +func ProcessPubKey(acc exported.Account, sig types.StdSignature, simulate bool) (crypto.PubKey, sdk.Result) { + // If pubkey is not known for account, set it from the types.StdSignature. pubKey := acc.GetPubKey() if simulate { // In simulate mode the transaction comes with no signatures, thus if the @@ -292,7 +294,7 @@ func ProcessPubKey(acc Account, sig StdSignature, simulate bool) (crypto.PubKey, // for signature verification based upon the public key type. The cost is fetched from the given params and is matched // by the concrete type. func DefaultSigVerificationGasConsumer( - meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params, + meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params, ) sdk.Result { switch pubkey := pubkey.(type) { case ed25519.PubKeyEd25519: @@ -317,7 +319,7 @@ func DefaultSigVerificationGasConsumer( func consumeMultisignatureVerificationGas(meter sdk.GasMeter, sig multisig.Multisignature, pubkey multisig.PubKeyMultisigThreshold, - params Params) { + params types.Params) { size := sig.BitArray.Size() sigIndex := 0 @@ -331,9 +333,9 @@ func consumeMultisignatureVerificationGas(meter sdk.GasMeter, // DeductFees deducts fees from the given account. // -// NOTE: We could use the CoinKeeper (in addition to the AccountKeeper, because +// NOTE: We could use the CoinKeeper (in addition to the exported.AccountKeeper, because // the CoinKeeper doesn't give us accounts), but it seems easier to do this. -func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc Account, fees sdk.Coins) sdk.Result { +func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc exported.Account, fees sdk.Coins) sdk.Result { blockTime := ctx.BlockHeader().Time coins := acc.GetCoins() @@ -372,7 +374,7 @@ func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc Account, f // // Contract: This should only be called during CheckTx as it cannot be part of // consensus. -func EnsureSufficientMempoolFees(ctx sdk.Context, stdFee StdFee) sdk.Result { +func EnsureSufficientMempoolFees(ctx sdk.Context, stdFee types.StdFee) sdk.Result { minGasPrices := ctx.MinGasPrices() if !minGasPrices.IsZero() { requiredFees := make(sdk.Coins, len(minGasPrices)) @@ -410,13 +412,13 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64) sdk.Context { // GetSignBytes returns a slice of bytes to sign over for a given transaction // and an account. -func GetSignBytes(chainID string, stdTx StdTx, acc Account, genesis bool) []byte { +func GetSignBytes(chainID string, stdTx types.StdTx, acc exported.Account, genesis bool) []byte { var accNum uint64 if !genesis { accNum = acc.GetAccountNumber() } - return StdSignBytes( + return types.StdSignBytes( chainID, accNum, acc.GetSequence(), stdTx.Fee, stdTx.Msgs, stdTx.Memo, ) } diff --git a/x/auth/ante_test.go b/x/auth/ante/ante_test.go similarity index 98% rename from x/auth/ante_test.go rename to x/auth/ante/ante_test.go index 4faae8ea92ac..51791734b78e 100644 --- a/x/auth/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1,4 +1,4 @@ -package auth +package ante import ( "fmt" @@ -14,6 +14,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" ) // run the tx through the anteHandler and ensure its valid @@ -47,7 +48,7 @@ func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, // Test various error cases in the AnteHandler control flow. func TestAnteHandlerSigErrors(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() ctx := input.ctx anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) @@ -96,7 +97,7 @@ func TestAnteHandlerSigErrors(t *testing.T) { // Test logic around account number checking with one signer and many signers. func TestAnteHandlerAccountNumbers(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -153,7 +154,7 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { // Test logic around account number checking with many signers when BlockHeight is 0. func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(0) @@ -209,7 +210,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { // Test logic around sequence checking with one signer and many signers. func TestAnteHandlerSequences(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -286,7 +287,7 @@ func TestAnteHandlerSequences(t *testing.T) { // Test logic around fee deduction. func TestAnteHandlerFees(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() ctx := input.ctx anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) @@ -326,7 +327,7 @@ func TestAnteHandlerFees(t *testing.T) { // Test logic around memo gas consumption. func TestAnteHandlerMemoGas(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -366,7 +367,7 @@ func TestAnteHandlerMemoGas(t *testing.T) { func TestAnteHandlerMultiSigner(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -416,7 +417,7 @@ func TestAnteHandlerMultiSigner(t *testing.T) { func TestAnteHandlerBadSignBytes(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -493,7 +494,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { func TestAnteHandlerSetPubKey(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -544,7 +545,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) { } func TestProcessPubKey(t *testing.T) { - input := setupTestInput() + input := keeper.SetupTestInput() ctx := input.ctx // keys @@ -689,7 +690,7 @@ func TestCountSubkeys(t *testing.T) { func TestAnteHandlerSigLimitExceeded(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) ctx := input.ctx.WithBlockHeight(1) @@ -726,7 +727,7 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) { func TestEnsureSufficientMempoolFees(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() ctx := input.ctx.WithMinGasPrices( sdk.DecCoins{ sdk.NewDecCoinFromDec("photino", sdk.NewDecWithPrec(50000000000000, sdk.Precision)), // 0.0001photino @@ -778,7 +779,7 @@ func TestEnsureSufficientMempoolFees(t *testing.T) { // Test custom SignatureVerificationGasConsumer func TestCustomSignatureVerificationGasConsumer(t *testing.T) { // setup - input := setupTestInput() + input := keeper.SetupTestInput() // setup an ante handler that only accepts PubKeyEd25519 anteHandler := NewAnteHandler(input.ak, input.sk, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params) sdk.Result { switch pubkey := pubkey.(type) { diff --git a/x/auth/keeper.go b/x/auth/keeper/keeper.go similarity index 99% rename from x/auth/keeper.go rename to x/auth/keeper/keeper.go index f70d4430552d..455c9d56d276 100644 --- a/x/auth/keeper.go +++ b/x/auth/keeper/keeper.go @@ -1,4 +1,4 @@ -package auth +package keeper import ( "fmt" diff --git a/x/auth/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go similarity index 95% rename from x/auth/keeper_bench_test.go rename to x/auth/keeper/keeper_bench_test.go index 8a3ae6da59ff..f03fe4f880fe 100644 --- a/x/auth/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -1,4 +1,4 @@ -package auth +package keeper import ( "testing" @@ -7,7 +7,7 @@ import ( ) func BenchmarkAccountMapperGetAccountFound(b *testing.B) { - input := setupTestInput() + input := SetupTestInput() // assumes b.N < 2**24 for i := 0; i < b.N; i++ { @@ -25,7 +25,7 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) { } func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) { - input := setupTestInput() + input := SetupTestInput() coins := sdk.Coins{ sdk.NewCoin("LTC", sdk.NewInt(1000)), sdk.NewCoin("BTC", sdk.NewInt(1000)), @@ -52,7 +52,7 @@ func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) { } func BenchmarkAccountMapperSetAccount(b *testing.B) { - input := setupTestInput() + input := SetupTestInput() b.ResetTimer() @@ -66,7 +66,7 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { } func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) { - input := setupTestInput() + input := SetupTestInput() coins := sdk.Coins{ sdk.NewCoin("LTC", sdk.NewInt(1000)), sdk.NewCoin("BTC", sdk.NewInt(1000)), diff --git a/x/auth/keeper_test.go b/x/auth/keeper/keeper_test.go similarity index 83% rename from x/auth/keeper_test.go rename to x/auth/keeper/keeper_test.go index 4285132cb69e..28f99f286d84 100644 --- a/x/auth/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -1,4 +1,4 @@ -package auth +package keeper import ( "testing" @@ -6,10 +6,11 @@ import ( "github.com/stretchr/testify/require" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) func TestAccountMapperGetSet(t *testing.T) { - input := setupTestInput() + input := SetupTestInput() addr := sdk.AccAddress([]byte("some-address")) // no account before its created @@ -38,7 +39,7 @@ func TestAccountMapperGetSet(t *testing.T) { } func TestAccountMapperRemoveAccount(t *testing.T) { - input := setupTestInput() + input := SetupTestInput() addr1 := sdk.AccAddress([]byte("addr1")) addr2 := sdk.AccAddress([]byte("addr2")) @@ -69,19 +70,19 @@ func TestAccountMapperRemoveAccount(t *testing.T) { } func TestSetParams(t *testing.T) { - input := setupTestInput() - params := DefaultParams() + input := SetupTestInput() + params := types.DefaultParams() input.ak.SetParams(input.ctx, params) - newParams := Params{} - input.ak.paramSubspace.Get(input.ctx, KeyTxSigLimit, &newParams.TxSigLimit) - require.Equal(t, newParams.TxSigLimit, DefaultTxSigLimit) + newParams := types.Params{} + input.ak.paramSubspace.Get(input.ctx, types.KeyTxSigLimit, &newParams.TxSigLimit) + require.Equal(t, newParams.TxSigLimit, types.DefaultTxSigLimit) } func TestGetParams(t *testing.T) { - input := setupTestInput() - params := DefaultParams() + input := SetupTestInput() + params := types.DefaultParams() input.ak.SetParams(input.ctx, params) diff --git a/x/auth/querier.go b/x/auth/keeper/querier.go similarity index 98% rename from x/auth/querier.go rename to x/auth/keeper/querier.go index ea2fd5c46cd4..eb13140f29cf 100644 --- a/x/auth/querier.go +++ b/x/auth/keeper/querier.go @@ -1,4 +1,4 @@ -package auth +package keeper import ( "fmt" diff --git a/x/auth/querier_test.go b/x/auth/keeper/querier_test.go similarity index 89% rename from x/auth/querier_test.go rename to x/auth/keeper/querier_test.go index a73b12a402b5..914ea31ab8a3 100644 --- a/x/auth/querier_test.go +++ b/x/auth/keeper/querier_test.go @@ -1,4 +1,4 @@ -package auth +package keeper import ( "fmt" @@ -12,9 +12,9 @@ import ( ) func Test_queryAccount(t *testing.T) { - input := setupTestInput() + input := SetupTestInput() req := abci.RequestQuery{ - Path: fmt.Sprintf("custom/%s/%s", QuerierRoute, QueryAccount), + Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), Data: []byte{}, } diff --git a/x/auth/test_common.go b/x/auth/keeper/test_common.go similarity index 98% rename from x/auth/test_common.go rename to x/auth/keeper/test_common.go index 58ac87cf2189..dc185af8b9d7 100644 --- a/x/auth/test_common.go +++ b/x/auth/keeper/test_common.go @@ -1,5 +1,6 @@ // nolint -package auth +// DONTCOVER +package keeper import ( abci "github.com/tendermint/tendermint/abci/types" @@ -49,7 +50,7 @@ func (ma moduleAccount) GetPermissions() []string { return ma.permissions } -func setupTestInput() testInput { +func SetupTestInput() testInput { db := dbm.NewMemDB() cdc := codec.New() diff --git a/x/mock/app.go b/x/mock/app.go index 591b3110de40..cadde5841b47 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -18,6 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/params" ) @@ -38,7 +39,7 @@ type App struct { AccountKeeper auth.AccountKeeper ParamsKeeper params.Keeper - GenesisAccounts []auth.Account + GenesisAccounts []authexported.Account TotalCoinsSupply sdk.Coins } @@ -167,7 +168,7 @@ func (b AddrKeysSlice) Swap(i, j int) { // CreateGenAccounts generates genesis accounts loaded with coins, and returns // their addresses, pubkeys, and privkeys. -func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, +func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []authexported.Account, addrs []sdk.AccAddress, pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) { addrKeysSlice := AddrKeysSlice{} @@ -196,7 +197,7 @@ func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, } // SetGenesis sets the mock app genesis accounts. -func SetGenesis(app *App, accs []auth.Account) { +func SetGenesis(app *App, accs []authexported.Account) { // Pass the accounts in via the application (lazy) instead of through // RequestInitChain. app.GenesisAccounts = accs @@ -283,7 +284,7 @@ func GeneratePrivKeyAddressPairsFromRand(rand *rand.Rand, n int) (keys []crypto. // provided addresses and coin denominations. // nolint: errcheck func RandomSetGenesis(r *rand.Rand, app *App, addrs []sdk.AccAddress, denoms []string) { - accts := make([]auth.Account, len(addrs)) + accts := make([]authexported.Account, len(addrs)) randCoinIntervals := []BigInterval{ {sdk.NewIntWithDecimal(1, 0), sdk.NewIntWithDecimal(1, 1)}, {sdk.NewIntWithDecimal(1, 2), sdk.NewIntWithDecimal(1, 3)}, diff --git a/x/slashing/internal/types/expected_keepers.go b/x/slashing/internal/types/expected_keepers.go index c73ab095ef4c..eaf4f9979c4b 100644 --- a/x/slashing/internal/types/expected_keepers.go +++ b/x/slashing/internal/types/expected_keepers.go @@ -4,14 +4,14 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/params" - "github.com/cosmos/cosmos-sdk/x/staking/exported" + stakingexported "github.com/cosmos/cosmos-sdk/x/staking/exported" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" ) // AccountKeeper expected account keeper type AccountKeeper interface { - IterateAccounts(ctx sdk.Context, process func(auth.Account) (stop bool)) + IterateAccounts(ctx sdk.Context, process func(authexported.Account) (stop bool)) } // ParamSubspace defines the expected Subspace interfacace @@ -26,10 +26,10 @@ type ParamSubspace interface { type StakingKeeper interface { // iterate through validators by operator address, execute func for each validator IterateValidators(sdk.Context, - func(index int64, validator exported.ValidatorI) (stop bool)) + func(index int64, validator stakingexported.ValidatorI) (stop bool)) - Validator(sdk.Context, sdk.ValAddress) exported.ValidatorI // get a particular validator by operator address - ValidatorByConsAddr(sdk.Context, sdk.ConsAddress) exported.ValidatorI // get a particular validator by consensus address + Validator(sdk.Context, sdk.ValAddress) stakingexported.ValidatorI // get a particular validator by operator address + ValidatorByConsAddr(sdk.Context, sdk.ConsAddress) stakingexported.ValidatorI // get a particular validator by consensus address // slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction Slash(sdk.Context, sdk.ConsAddress, int64, int64, sdk.Dec) @@ -38,7 +38,7 @@ type StakingKeeper interface { // Delegation allows for getting a particular delegation for a given validator // and delegator outside the scope of the staking module. - Delegation(sdk.Context, sdk.AccAddress, sdk.ValAddress) exported.DelegationI + Delegation(sdk.Context, sdk.AccAddress, sdk.ValAddress) stakingexported.DelegationI // MaxValidators returns the maximum amount of bonded validators MaxValidators(sdk.Context) uint16 diff --git a/x/supply/genesis.go b/x/supply/genesis.go index 33c67800cc27..ddef251166ef 100644 --- a/x/supply/genesis.go +++ b/x/supply/genesis.go @@ -2,7 +2,7 @@ package supply import ( sdk "github.com/cosmos/cosmos-sdk/types" - autypes "github.com/cosmos/cosmos-sdk/x/auth" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" ) @@ -14,7 +14,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data Ge if data.Supply.Total.Empty() { var totalSupply sdk.Coins ak.IterateAccounts(ctx, - func(acc autypes.Account) (stop bool) { + func(acc authexported.Account) (stop bool) { totalSupply = totalSupply.Add(acc.GetCoins()) return false }, diff --git a/x/supply/internal/keeper/test_common.go b/x/supply/internal/keeper/test_common.go index 5370d0c16042..aa480a606526 100644 --- a/x/supply/internal/keeper/test_common.go +++ b/x/supply/internal/keeper/test_common.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" "github.com/cosmos/cosmos-sdk/x/auth" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" @@ -93,7 +94,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initPower int64, nAccs int64) } // nolint: unparam deadcode unused -func createTestAccs(ctx sdk.Context, numAccs int, initialCoins sdk.Coins, ak *auth.AccountKeeper) (accs []auth.Account) { +func createTestAccs(ctx sdk.Context, numAccs int, initialCoins sdk.Coins, ak *auth.AccountKeeper) (accs []authexported.Account) { for i := 0; i < numAccs; i++ { privKey := secp256k1.GenPrivKey() pubKey := privKey.PubKey() From 5bcd82a61e9619182bc8f2b77e1011fdfc46838a Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Tue, 23 Jul 2019 12:03:18 +0200 Subject: [PATCH 2/8] cleanup --- x/auth/keeper/account.go | 84 +++++++++++++++++++++++ x/auth/keeper/keeper.go | 101 +--------------------------- x/auth/keeper/params.go | 17 +++++ x/auth/keeper/querier.go | 2 +- x/auth/keeper/querier_test.go | 30 +++++++-- x/staking/types/expected_keepers.go | 4 +- 6 files changed, 130 insertions(+), 108 deletions(-) create mode 100644 x/auth/keeper/account.go create mode 100644 x/auth/keeper/params.go diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go new file mode 100644 index 000000000000..ffb516c92aa9 --- /dev/null +++ b/x/auth/keeper/account.go @@ -0,0 +1,84 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/exported" + "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +// NewAccountWithAddress implements sdk.AccountKeeper. +func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) exported.Account { + acc := ak.proto() + err := acc.SetAddress(addr) + if err != nil { + panic(err) + } + err = acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)) + if err != nil { + panic(err) + } + return acc +} + +// NewAccount creates a new account +func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc exported.Account) exported.Account { + if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil { + panic(err) + } + return acc +} + +// GetAccount implements sdk.AccountKeeper. +func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) exported.Account { + store := ctx.KVStore(ak.key) + bz := store.Get(types.AddressStoreKey(addr)) + if bz == nil { + return nil + } + acc := ak.decodeAccount(bz) + return acc +} + +// GetAllAccounts returns all accounts in the accountKeeper. +func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []exported.Account) { + ak.IterateAccounts(ctx, + func(acc exported.Account) (stop bool) { + accounts = append(accounts, acc) + return false + }) + return accounts +} + +// SetAccount implements sdk.AccountKeeper. +func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc exported.Account) { + addr := acc.GetAddress() + store := ctx.KVStore(ak.key) + bz, err := ak.cdc.MarshalBinaryBare(acc) + if err != nil { + panic(err) + } + store.Set(types.AddressStoreKey(addr), bz) +} + +// RemoveAccount removes an account for the account mapper store. +// NOTE: this will cause supply invariant violation if called +func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc exported.Account) { + addr := acc.GetAddress() + store := ctx.KVStore(ak.key) + store.Delete(types.AddressStoreKey(addr)) +} + +// IterateAccounts iterates over all the stored accounts and performs a callback function +func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account exported.Account) (stop bool)) { + store := ctx.KVStore(ak.key) + iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix) + + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + account := ak.decodeAccount(iterator.Value()) + + if cb(account) { + break + } + } +} \ No newline at end of file diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 455c9d56d276..d60a396d342b 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -48,89 +48,6 @@ func (ak AccountKeeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -// NewAccountWithAddress implements sdk.AccountKeeper. -func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) exported.Account { - acc := ak.proto() - err := acc.SetAddress(addr) - if err != nil { - // Handle w/ #870 - panic(err) - } - err = acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)) - if err != nil { - // Handle w/ #870 - panic(err) - } - return acc -} - -// NewAccount creates a new account -func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc exported.Account) exported.Account { - if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil { - panic(err) - } - return acc -} - -// GetAccount implements sdk.AccountKeeper. -func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) exported.Account { - store := ctx.KVStore(ak.key) - bz := store.Get(types.AddressStoreKey(addr)) - if bz == nil { - return nil - } - acc := ak.decodeAccount(bz) - return acc -} - -// GetAllAccounts returns all accounts in the accountKeeper. -func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) []exported.Account { - accounts := []exported.Account{} - appendAccount := func(acc exported.Account) (stop bool) { - accounts = append(accounts, acc) - return false - } - ak.IterateAccounts(ctx, appendAccount) - return accounts -} - -// SetAccount implements sdk.AccountKeeper. -func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc exported.Account) { - addr := acc.GetAddress() - store := ctx.KVStore(ak.key) - bz, err := ak.cdc.MarshalBinaryBare(acc) - if err != nil { - panic(err) - } - store.Set(types.AddressStoreKey(addr), bz) -} - -// RemoveAccount removes an account for the account mapper store. -// NOTE: this will cause supply invariant violation if called -func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc exported.Account) { - addr := acc.GetAddress() - store := ctx.KVStore(ak.key) - store.Delete(types.AddressStoreKey(addr)) -} - -// IterateAccounts implements sdk.AccountKeeper. -func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, process func(exported.Account) (stop bool)) { - store := ctx.KVStore(ak.key) - iter := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix) - defer iter.Close() - for { - if !iter.Valid() { - return - } - val := iter.Value() - acc := ak.decodeAccount(val) - if process(acc) { - return - } - iter.Next() - } -} - // GetPubKey Returns the PubKey of the account at address func (ak AccountKeeper) GetPubKey(ctx sdk.Context, addr sdk.AccAddress) (crypto.PubKey, sdk.Error) { acc := ak.GetAccount(ctx, addr) @@ -149,12 +66,14 @@ func (ak AccountKeeper) GetSequence(ctx sdk.Context, addr sdk.AccAddress) (uint6 return acc.GetSequence(), nil } -// GetNextAccountNumber Returns and increments the global account number counter +// GetNextAccountNumber returns and increments the global account number counter. +// If the global account number is not set, it initializes it with value 0. func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 { var accNumber uint64 store := ctx.KVStore(ak.key) bz := store.Get(types.GlobalAccountNumberKey) if bz == nil { + // initialize the account numbers accNumber = 0 } else { err := ak.cdc.UnmarshalBinaryLengthPrefixed(bz, &accNumber) @@ -169,20 +88,6 @@ func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 { return accNumber } -// ----------------------------------------------------------------------------- -// Params - -// SetParams sets the auth module's parameters. -func (ak AccountKeeper) SetParams(ctx sdk.Context, params types.Params) { - ak.paramSubspace.SetParamSet(ctx, ¶ms) -} - -// GetParams gets the auth module's parameters. -func (ak AccountKeeper) GetParams(ctx sdk.Context) (params types.Params) { - ak.paramSubspace.GetParamSet(ctx, ¶ms) - return -} - // ----------------------------------------------------------------------------- // Misc. diff --git a/x/auth/keeper/params.go b/x/auth/keeper/params.go new file mode 100644 index 000000000000..48c600dd3e08 --- /dev/null +++ b/x/auth/keeper/params.go @@ -0,0 +1,17 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +// SetParams sets the auth module's parameters. +func (ak AccountKeeper) SetParams(ctx sdk.Context, params types.Params) { + ak.paramSubspace.SetParamSet(ctx, ¶ms) +} + +// GetParams gets the auth module's parameters. +func (ak AccountKeeper) GetParams(ctx sdk.Context) (params types.Params) { + ak.paramSubspace.GetParamSet(ctx, ¶ms) + return +} \ No newline at end of file diff --git a/x/auth/keeper/querier.go b/x/auth/keeper/querier.go index eb13140f29cf..b40dc0135d8f 100644 --- a/x/auth/keeper/querier.go +++ b/x/auth/keeper/querier.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -// creates a querier for auth REST endpoints +// NewQuerier creates a querier for auth REST endpoints func NewQuerier(keeper AccountKeeper) sdk.Querier { return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) { switch path[0] { diff --git a/x/auth/keeper/querier_test.go b/x/auth/keeper/querier_test.go index 914ea31ab8a3..5310a9921f32 100644 --- a/x/auth/keeper/querier_test.go +++ b/x/auth/keeper/querier_test.go @@ -8,37 +8,53 @@ import ( abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/types" ) -func Test_queryAccount(t *testing.T) { +func TestQueryAccount(t *testing.T) { input := SetupTestInput() + req := abci.RequestQuery{ - Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), + Path: "", Data: []byte{}, } + querier := NewQuerier(input.ak) + + bz, err := querier(input.ctx, []string{"other"}, req) + require.Error(t, err) + require.Nil(t, bz) + + req = abci.RequestQuery{ + Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), + Data: []byte{}, + } res, err := queryAccount(input.ctx, req, input.ak) - require.NotNil(t, err) + require.Error(t, err) require.Nil(t, res) req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte(""))) res, err = queryAccount(input.ctx, req, input.ak) - require.NotNil(t, err) + require.Error(t, err) require.Nil(t, res) _, _, addr := types.KeyTestPubAddr() req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams(addr)) res, err = queryAccount(input.ctx, req, input.ak) - require.NotNil(t, err) + require.Error(t, err) require.Nil(t, res) input.ak.SetAccount(input.ctx, input.ak.NewAccountWithAddress(input.ctx, addr)) res, err = queryAccount(input.ctx, req, input.ak) - require.Nil(t, err) + require.NoError(t, err) + require.NotNil(t, res) + + res, err = querier(input.ctx, []string{types.QueryAccount}, req) + require.NoError(t, err) require.NotNil(t, res) - var account Account + var account exported.Account err2 := input.cdc.UnmarshalJSON(res, &account) require.Nil(t, err2) } diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index 23da1ac5b5e1..e172d5a86835 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -2,7 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" stakingexported "github.com/cosmos/cosmos-sdk/x/staking/exported" "github.com/cosmos/cosmos-sdk/x/supply" supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported" @@ -16,7 +16,7 @@ type DistributionKeeper interface { // AccountKeeper defines the expected account keeper (noalias) type AccountKeeper interface { - IterateAccounts(ctx sdk.Context, process func(auth.Account) (stop bool)) + IterateAccounts(ctx sdk.Context, process func(authexported.Account) (stop bool)) } // SupplyKeeper defines the expected supply Keeper (noalias) From 03564f315e3cf9b96786c4d5c7ecd0c6eb253e81 Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Tue, 23 Jul 2019 12:07:19 +0200 Subject: [PATCH 3/8] make format --- client/cmd_test.go | 1 + go.mod | 1 - go.sum | 2 -- x/auth/ante/ante_test.go | 2 +- x/auth/keeper/account.go | 2 +- x/auth/keeper/params.go | 2 +- x/slashing/internal/types/expected_keepers.go | 2 +- 7 files changed, 5 insertions(+), 7 deletions(-) diff --git a/client/cmd_test.go b/client/cmd_test.go index 3ca286f64035..ea71399a60bc 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -1,3 +1,4 @@ +// nolint: misspell package client_test import ( diff --git a/go.mod b/go.mod index d9545349c8c2..6809ae310151 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 github.com/cosmos/ledger-cosmos-go v0.10.3 - github.com/cosmos/tools/cmd/clog v0.0.0-20190722180430-ea942c183cba // indirect github.com/fortytw2/leaktest v1.3.0 // indirect github.com/gogo/protobuf v1.2.1 github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 diff --git a/go.sum b/go.sum index 0b0d7dd839e9..72d7f35eedff 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,6 @@ github.com/cosmos/ledger-cosmos-go v0.10.3 h1:Qhi5yTR5Pg1CaTpd00pxlGwNl4sFRdtK1J github.com/cosmos/ledger-cosmos-go v0.10.3/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI= github.com/cosmos/ledger-go v0.9.2/go.mod h1:oZJ2hHAZROdlHiwTg4t7kP+GKIIkBT+o6c9QWFanOyI= -github.com/cosmos/tools/cmd/clog v0.0.0-20190722180430-ea942c183cba h1:YhVnGzBkE2TvfBW5fAYBdNVK/3bwTPYVbMaOIGRHFRY= -github.com/cosmos/tools/cmd/clog v0.0.0-20190722180430-ea942c183cba/go.mod h1:TdPuAVaU2rc6K24ejr/AnGznt9Fd2qjtMoRrTO4uFrI= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 51791734b78e..14019bc1e58c 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -13,8 +13,8 @@ import ( "github.com/tendermint/tendermint/crypto/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) // run the tx through the anteHandler and ensure its valid diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go index ffb516c92aa9..4eb1bfa9d5bb 100644 --- a/x/auth/keeper/account.go +++ b/x/auth/keeper/account.go @@ -81,4 +81,4 @@ func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account exporte break } } -} \ No newline at end of file +} diff --git a/x/auth/keeper/params.go b/x/auth/keeper/params.go index 48c600dd3e08..0236c7ae1627 100644 --- a/x/auth/keeper/params.go +++ b/x/auth/keeper/params.go @@ -14,4 +14,4 @@ func (ak AccountKeeper) SetParams(ctx sdk.Context, params types.Params) { func (ak AccountKeeper) GetParams(ctx sdk.Context) (params types.Params) { ak.paramSubspace.GetParamSet(ctx, ¶ms) return -} \ No newline at end of file +} diff --git a/x/slashing/internal/types/expected_keepers.go b/x/slashing/internal/types/expected_keepers.go index eaf4f9979c4b..776817416a3c 100644 --- a/x/slashing/internal/types/expected_keepers.go +++ b/x/slashing/internal/types/expected_keepers.go @@ -4,9 +4,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/params" stakingexported "github.com/cosmos/cosmos-sdk/x/staking/exported" - authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" ) // AccountKeeper expected account keeper From 53b8c44833b017844aea12764fe6a537b4b57416 Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Wed, 24 Jul 2019 12:52:49 +0200 Subject: [PATCH 4/8] fix tests --- x/auth/ante/ante_test.go | 211 +++++++++++++++-------------- x/auth/keeper/keeper_bench_test.go | 20 +-- x/auth/keeper/keeper_test.go | 34 ++--- x/auth/keeper/querier_test.go | 16 +-- x/auth/keeper/test_common.go | 8 +- 5 files changed, 145 insertions(+), 144 deletions(-) diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 14019bc1e58c..70cf1e976760 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -13,6 +13,7 @@ import ( "github.com/tendermint/tendermint/crypto/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -49,8 +50,8 @@ func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, func TestAnteHandlerSigErrors(t *testing.T) { // setup input := keeper.SetupTestInput() - ctx := input.ctx - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() @@ -88,9 +89,9 @@ func TestAnteHandlerSigErrors(t *testing.T) { checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress) // save the first account, but second is still unrecognized - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(fee.Amount) - input.ak.SetAccount(ctx, acc1) + input.Ak.SetAccount(ctx, acc1) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress) } @@ -98,22 +99,22 @@ func TestAnteHandlerSigErrors(t *testing.T) { func TestAnteHandlerAccountNumbers(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) // msg and signatures var tx sdk.Tx @@ -155,21 +156,21 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(0) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(0) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts, we don't need the acc numbers as it is in the genesis block - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) // msg and signatures var tx sdk.Tx @@ -211,8 +212,8 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { func TestAnteHandlerSequences(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() @@ -220,18 +221,18 @@ func TestAnteHandlerSequences(t *testing.T) { priv3, _, addr3 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) - acc3 := input.ak.NewAccountWithAddress(ctx, addr3) + input.Ak.SetAccount(ctx, acc2) + acc3 := input.Ak.NewAccountWithAddress(ctx, addr3) acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) - input.ak.SetAccount(ctx, acc3) + input.Ak.SetAccount(ctx, acc3) // msg and signatures var tx sdk.Tx @@ -288,15 +289,15 @@ func TestAnteHandlerSequences(t *testing.T) { func TestAnteHandlerFees(t *testing.T) { // setup input := keeper.SetupTestInput() - ctx := input.ctx - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) - input.ak.SetAccount(ctx, acc1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + input.Ak.SetAccount(ctx, acc1) // msg and signatures var tx sdk.Tx @@ -310,57 +311,57 @@ func TestAnteHandlerFees(t *testing.T) { checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds) acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) - input.ak.SetAccount(ctx, acc1) + input.Ak.SetAccount(ctx, acc1) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds) - require.True(t, input.sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) - require.True(sdk.IntEq(t, input.ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149))) + require.True(t, input.Sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) + require.True(sdk.IntEq(t, input.Ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149))) acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) - input.ak.SetAccount(ctx, acc1) + input.Ak.SetAccount(ctx, acc1) checkValidTx(t, anteHandler, ctx, tx, false) - require.True(sdk.IntEq(t, input.sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150))) - require.True(sdk.IntEq(t, input.ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(0))) + require.True(sdk.IntEq(t, input.Sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150))) + require.True(sdk.IntEq(t, input.Ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(0))) } // Test logic around memo gas consumption. func TestAnteHandlerMemoGas(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) + input.Ak.SetAccount(ctx, acc1) // msg and signatures var tx sdk.Tx msg := types.NewTestMsg(addr1) privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} - fee := NewStdFee(0, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) + fee := types.NewStdFee(0, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) // tx does not have enough gas tx = types.NewTestTx(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas) // tx with memo doesn't have enough gas - fee = NewStdFee(801, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) + fee = types.NewStdFee(801, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd") checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas) // memo too large - fee = NewStdFee(9000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) + fee = types.NewStdFee(9000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("01234567890", 500)) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeMemoTooLarge) // tx with memo has enough gas - fee = NewStdFee(9000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) + fee = types.NewStdFee(9000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("0123456789", 10)) checkValidTx(t, anteHandler, ctx, tx, false) } @@ -368,8 +369,8 @@ func TestAnteHandlerMemoGas(t *testing.T) { func TestAnteHandlerMultiSigner(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() @@ -377,18 +378,18 @@ func TestAnteHandlerMultiSigner(t *testing.T) { priv3, _, addr3 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) - acc3 := input.ak.NewAccountWithAddress(ctx, addr3) + input.Ak.SetAccount(ctx, acc2) + acc3 := input.Ak.NewAccountWithAddress(ctx, addr3) acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) - input.ak.SetAccount(ctx, acc3) + input.Ak.SetAccount(ctx, acc3) // set up msgs and fee var tx sdk.Tx @@ -418,22 +419,22 @@ func TestAnteHandlerMultiSigner(t *testing.T) { func TestAnteHandlerBadSignBytes(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -457,7 +458,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { chainID string accnum uint64 seq uint64 - fee StdFee + fee types.StdFee msgs []sdk.Msg code sdk.CodeType }{ @@ -473,7 +474,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { for _, cs := range cases { tx := types.NewTestTxWithSignBytes( msgs, privs, accnums, seqs, fee, - StdSignBytes(cs.chainID, cs.accnum, cs.seq, cs.fee, cs.msgs, ""), + types.StdSignBytes(cs.chainID, cs.accnum, cs.seq, cs.fee, cs.msgs, ""), "", ) checkInvalidTx(t, anteHandler, ctx, tx, false, cs.code) @@ -495,22 +496,22 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { func TestAnteHandlerSetPubKey(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() _, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) var tx sdk.Tx @@ -522,7 +523,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) { tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) - acc1 = input.ak.GetAccount(ctx, addr1) + acc1 = input.Ak.GetAccount(ctx, addr1) require.Equal(t, acc1.GetPubKey(), priv1.PubKey()) // test public key not found @@ -533,32 +534,32 @@ func TestAnteHandlerSetPubKey(t *testing.T) { sigs[0].PubKey = nil checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey) - acc2 = input.ak.GetAccount(ctx, addr2) + acc2 = input.Ak.GetAccount(ctx, addr2) require.Nil(t, acc2.GetPubKey()) // test invalid signature and public key tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey) - acc2 = input.ak.GetAccount(ctx, addr2) + acc2 = input.Ak.GetAccount(ctx, addr2) require.Nil(t, acc2.GetPubKey()) } func TestProcessPubKey(t *testing.T) { input := keeper.SetupTestInput() - ctx := input.ctx + ctx := input.Ctx // keys _, _, addr1 := types.KeyTestPubAddr() priv2, _, addr2 := types.KeyTestPubAddr() - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetPubKey(priv2.PubKey()) type args struct { - acc Account - sig StdSignature + acc exported.Account + sig types.StdSignature simulate bool } tests := []struct { @@ -566,11 +567,11 @@ func TestProcessPubKey(t *testing.T) { args args wantErr bool }{ - {"no sigs, simulate off", args{acc1, StdSignature{}, false}, true}, - {"no sigs, simulate on", args{acc1, StdSignature{}, true}, false}, - {"no sigs, account with pub, simulate on", args{acc2, StdSignature{}, true}, false}, - {"pubkey doesn't match addr, simulate off", args{acc1, StdSignature{PubKey: priv2.PubKey()}, false}, true}, - {"pubkey doesn't match addr, simulate on", args{acc1, StdSignature{PubKey: priv2.PubKey()}, true}, false}, + {"no sigs, simulate off", args{acc1, types.StdSignature{}, false}, true}, + {"no sigs, simulate on", args{acc1, types.StdSignature{}, true}, false}, + {"no sigs, account with pub, simulate on", args{acc2, types.StdSignature{}, true}, false}, + {"pubkey doesn't match addr, simulate off", args{acc1, types.StdSignature{PubKey: priv2.PubKey()}, false}, true}, + {"pubkey doesn't match addr, simulate on", args{acc1, types.StdSignature{PubKey: priv2.PubKey()}, true}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -581,7 +582,7 @@ func TestProcessPubKey(t *testing.T) { } func TestConsumeSignatureVerificationGas(t *testing.T) { - params := DefaultParams() + params := types.DefaultParams() msg := []byte{1, 2, 3, 4} pkSet1, sigSet1 := generatePubKeysAndSignatures(5, msg, false) @@ -596,7 +597,7 @@ func TestConsumeSignatureVerificationGas(t *testing.T) { meter sdk.GasMeter sig []byte pubkey crypto.PubKey - params Params + params types.Params } tests := []struct { name string @@ -604,8 +605,8 @@ func TestConsumeSignatureVerificationGas(t *testing.T) { gasConsumed uint64 shouldErr bool }{ - {"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(), nil, ed25519.GenPrivKey().PubKey(), params}, DefaultSigVerifyCostED25519, true}, - {"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(), nil, secp256k1.GenPrivKey().PubKey(), params}, DefaultSigVerifyCostSecp256k1, false}, + {"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(), nil, ed25519.GenPrivKey().PubKey(), params}, types.DefaultSigVerifyCostED25519, true}, + {"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(), nil, secp256k1.GenPrivKey().PubKey(), params}, types.DefaultSigVerifyCostSecp256k1, false}, {"Multisig", args{sdk.NewInfiniteGasMeter(), multisignature1.Marshal(), multisigKey1, params}, expectedCost1, false}, {"unknown key", args{sdk.NewInfiniteGasMeter(), nil, nil, params}, 0, true}, } @@ -645,9 +646,9 @@ func expectedGasCostByKeys(pubkeys []crypto.PubKey) uint64 { pubkeyType := strings.ToLower(fmt.Sprintf("%T", pubkey)) switch { case strings.Contains(pubkeyType, "ed25519"): - cost += DefaultParams().SigVerifyCostED25519 + cost += types.DefaultParams().SigVerifyCostED25519 case strings.Contains(pubkeyType, "secp256k1"): - cost += DefaultParams().SigVerifyCostSecp256k1 + cost += types.DefaultParams().SigVerifyCostSecp256k1 default: panic("unexpected key type") } @@ -683,7 +684,7 @@ func TestCountSubkeys(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(T *testing.T) { - require.Equal(t, tt.want, CountSubKeys(tt.args.pub)) + require.Equal(t, tt.want, types.CountSubKeys(tt.args.pub)) }) } } @@ -691,8 +692,8 @@ func TestCountSubkeys(t *testing.T) { func TestAnteHandlerSigLimitExceeded(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.ak, input.sk, DefaultSigVerificationGasConsumer) - ctx := input.ctx.WithBlockHeight(1) + anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() @@ -705,13 +706,13 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) { priv8, _, addr8 := types.KeyTestPubAddr() // set the accounts - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) - input.ak.SetAccount(ctx, acc1) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + input.Ak.SetAccount(ctx, acc1) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) var tx sdk.Tx msg := types.NewTestMsg(addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8) @@ -728,7 +729,7 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) { func TestEnsureSufficientMempoolFees(t *testing.T) { // setup input := keeper.SetupTestInput() - ctx := input.ctx.WithMinGasPrices( + ctx := input.Ctx.WithMinGasPrices( sdk.DecCoins{ sdk.NewDecCoinFromDec("photino", sdk.NewDecWithPrec(50000000000000, sdk.Precision)), // 0.0001photino sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(10000000000000, sdk.Precision)), // 0.000001stake @@ -736,16 +737,16 @@ func TestEnsureSufficientMempoolFees(t *testing.T) { ) testCases := []struct { - input StdFee + input types.StdFee expectedOK bool }{ - {NewStdFee(200000, sdk.Coins{}), false}, - {NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("photino", 5))), false}, - {NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("stake", 1))), false}, - {NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("stake", 2))), true}, - {NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("photino", 10))), true}, + {types.NewStdFee(200000, sdk.Coins{}), false}, + {types.NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("photino", 5))), false}, + {types.NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("stake", 1))), false}, + {types.NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("stake", 2))), true}, + {types.NewStdFee(200000, sdk.NewCoins(sdk.NewInt64Coin("photino", 10))), true}, { - NewStdFee( + types.NewStdFee( 200000, sdk.NewCoins( sdk.NewInt64Coin("photino", 10), @@ -755,7 +756,7 @@ func TestEnsureSufficientMempoolFees(t *testing.T) { true, }, { - NewStdFee( + types.NewStdFee( 200000, sdk.NewCoins( sdk.NewInt64Coin("atom", 5), @@ -781,7 +782,7 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { // setup input := keeper.SetupTestInput() // setup an ante handler that only accepts PubKeyEd25519 - anteHandler := NewAnteHandler(input.ak, input.sk, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params) sdk.Result { + anteHandler := NewAnteHandler(input.Ak, input.Sk, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result { switch pubkey := pubkey.(type) { case ed25519.PubKeyEd25519: meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") @@ -790,13 +791,13 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { return sdk.ErrInvalidPubKey(fmt.Sprintf("unrecognized public key type: %T", pubkey)).Result() } }) - ctx := input.ctx.WithBlockHeight(1) + ctx := input.Ctx.WithBlockHeight(1) // verify that an secp256k1 account gets rejected priv1, _, addr1 := types.KeyTestPubAddr() - acc1 := input.ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) _ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) - input.ak.SetAccount(ctx, acc1) + input.Ak.SetAccount(ctx, acc1) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -810,10 +811,10 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { priv2 := ed25519.GenPrivKey() pub2 := priv2.PubKey() addr2 := sdk.AccAddress(pub2.Address()) - acc2 := input.ak.NewAccountWithAddress(ctx, addr2) + acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))) require.NoError(t, acc2.SetAccountNumber(1)) - input.ak.SetAccount(ctx, acc2) + input.Ak.SetAccount(ctx, acc2) msg = types.NewTestMsg(addr2) privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{1}, []uint64{0} fee = types.NewTestStdFee() diff --git a/x/auth/keeper/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go index f03fe4f880fe..932f1e83a4c4 100644 --- a/x/auth/keeper/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -13,14 +13,14 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.ak.NewAccountWithAddress(input.ctx, addr) - input.ak.SetAccount(input.ctx, acc) + acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) + input.Ak.SetAccount(input.Ctx, acc) } b.ResetTimer() for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - input.ak.GetAccount(input.ctx, sdk.AccAddress(arr)) + input.Ak.GetAccount(input.Ctx, sdk.AccAddress(arr)) } } @@ -39,15 +39,15 @@ func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.ak.NewAccountWithAddress(input.ctx, addr) + acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) acc.SetCoins(coins) - input.ak.SetAccount(input.ctx, acc) + input.Ak.SetAccount(input.Ctx, acc) } b.ResetTimer() for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - input.ak.GetAccount(input.ctx, sdk.AccAddress(arr)) + input.Ak.GetAccount(input.Ctx, sdk.AccAddress(arr)) } } @@ -60,8 +60,8 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.ak.NewAccountWithAddress(input.ctx, addr) - input.ak.SetAccount(input.ctx, acc) + acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) + input.Ak.SetAccount(input.Ctx, acc) } } @@ -82,8 +82,8 @@ func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.ak.NewAccountWithAddress(input.ctx, addr) + acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) acc.SetCoins(coins) - input.ak.SetAccount(input.ctx, acc) + input.Ak.SetAccount(input.Ctx, acc) } } diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 28f99f286d84..438eddf41323 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -14,26 +14,26 @@ func TestAccountMapperGetSet(t *testing.T) { addr := sdk.AccAddress([]byte("some-address")) // no account before its created - acc := input.ak.GetAccount(input.ctx, addr) + acc := input.Ak.GetAccount(input.Ctx, addr) require.Nil(t, acc) // create account and check default values - acc = input.ak.NewAccountWithAddress(input.ctx, addr) + acc = input.Ak.NewAccountWithAddress(input.Ctx, addr) require.NotNil(t, acc) require.Equal(t, addr, acc.GetAddress()) require.EqualValues(t, nil, acc.GetPubKey()) require.EqualValues(t, 0, acc.GetSequence()) // NewAccount doesn't call Set, so it's still nil - require.Nil(t, input.ak.GetAccount(input.ctx, addr)) + require.Nil(t, input.Ak.GetAccount(input.Ctx, addr)) // set some values on the account and save it newSequence := uint64(20) acc.SetSequence(newSequence) - input.ak.SetAccount(input.ctx, acc) + input.Ak.SetAccount(input.Ctx, acc) // check the new values - acc = input.ak.GetAccount(input.ctx, addr) + acc = input.Ak.GetAccount(input.Ctx, addr) require.NotNil(t, acc) require.Equal(t, newSequence, acc.GetSequence()) } @@ -44,27 +44,27 @@ func TestAccountMapperRemoveAccount(t *testing.T) { addr2 := sdk.AccAddress([]byte("addr2")) // create accounts - acc1 := input.ak.NewAccountWithAddress(input.ctx, addr1) - acc2 := input.ak.NewAccountWithAddress(input.ctx, addr2) + acc1 := input.Ak.NewAccountWithAddress(input.Ctx, addr1) + acc2 := input.Ak.NewAccountWithAddress(input.Ctx, addr2) accSeq1 := uint64(20) accSeq2 := uint64(40) acc1.SetSequence(accSeq1) acc2.SetSequence(accSeq2) - input.ak.SetAccount(input.ctx, acc1) - input.ak.SetAccount(input.ctx, acc2) + input.Ak.SetAccount(input.Ctx, acc1) + input.Ak.SetAccount(input.Ctx, acc2) - acc1 = input.ak.GetAccount(input.ctx, addr1) + acc1 = input.Ak.GetAccount(input.Ctx, addr1) require.NotNil(t, acc1) require.Equal(t, accSeq1, acc1.GetSequence()) // remove one account - input.ak.RemoveAccount(input.ctx, acc1) - acc1 = input.ak.GetAccount(input.ctx, addr1) + input.Ak.RemoveAccount(input.Ctx, acc1) + acc1 = input.Ak.GetAccount(input.Ctx, addr1) require.Nil(t, acc1) - acc2 = input.ak.GetAccount(input.ctx, addr2) + acc2 = input.Ak.GetAccount(input.Ctx, addr2) require.NotNil(t, acc2) require.Equal(t, accSeq2, acc2.GetSequence()) } @@ -73,10 +73,10 @@ func TestSetParams(t *testing.T) { input := SetupTestInput() params := types.DefaultParams() - input.ak.SetParams(input.ctx, params) + input.Ak.SetParams(input.Ctx, params) newParams := types.Params{} - input.ak.paramSubspace.Get(input.ctx, types.KeyTxSigLimit, &newParams.TxSigLimit) + input.Ak.paramSubspace.Get(input.Ctx, types.KeyTxSigLimit, &newParams.TxSigLimit) require.Equal(t, newParams.TxSigLimit, types.DefaultTxSigLimit) } @@ -84,8 +84,8 @@ func TestGetParams(t *testing.T) { input := SetupTestInput() params := types.DefaultParams() - input.ak.SetParams(input.ctx, params) + input.Ak.SetParams(input.Ctx, params) - newParams := input.ak.GetParams(input.ctx) + newParams := input.Ak.GetParams(input.Ctx) require.Equal(t, params, newParams) } diff --git a/x/auth/keeper/querier_test.go b/x/auth/keeper/querier_test.go index 5310a9921f32..621f2184361d 100644 --- a/x/auth/keeper/querier_test.go +++ b/x/auth/keeper/querier_test.go @@ -20,9 +20,9 @@ func TestQueryAccount(t *testing.T) { Data: []byte{}, } - querier := NewQuerier(input.ak) + querier := NewQuerier(input.Ak) - bz, err := querier(input.ctx, []string{"other"}, req) + bz, err := querier(input.Ctx, []string{"other"}, req) require.Error(t, err) require.Nil(t, bz) @@ -30,27 +30,27 @@ func TestQueryAccount(t *testing.T) { Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), Data: []byte{}, } - res, err := queryAccount(input.ctx, req, input.ak) + res, err := queryAccount(input.Ctx, req, input.Ak) require.Error(t, err) require.Nil(t, res) req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte(""))) - res, err = queryAccount(input.ctx, req, input.ak) + res, err = queryAccount(input.Ctx, req, input.Ak) require.Error(t, err) require.Nil(t, res) _, _, addr := types.KeyTestPubAddr() req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams(addr)) - res, err = queryAccount(input.ctx, req, input.ak) + res, err = queryAccount(input.Ctx, req, input.Ak) require.Error(t, err) require.Nil(t, res) - input.ak.SetAccount(input.ctx, input.ak.NewAccountWithAddress(input.ctx, addr)) - res, err = queryAccount(input.ctx, req, input.ak) + input.Ak.SetAccount(input.Ctx, input.Ak.NewAccountWithAddress(input.Ctx, addr)) + res, err = queryAccount(input.Ctx, req, input.Ak) require.NoError(t, err) require.NotNil(t, res) - res, err = querier(input.ctx, []string{types.QueryAccount}, req) + res, err = querier(input.Ctx, []string{types.QueryAccount}, req) require.NoError(t, err) require.NotNil(t, res) diff --git a/x/auth/keeper/test_common.go b/x/auth/keeper/test_common.go index dc185af8b9d7..912239b27262 100644 --- a/x/auth/keeper/test_common.go +++ b/x/auth/keeper/test_common.go @@ -18,9 +18,9 @@ import ( type testInput struct { cdc *codec.Codec - ctx sdk.Context - ak AccountKeeper - sk types.SupplyKeeper + Ctx sdk.Context + Ak AccountKeeper + Sk types.SupplyKeeper } // moduleAccount defines an account for modules that holds coins on a pool @@ -77,7 +77,7 @@ func SetupTestInput() testInput { ak.SetParams(ctx, types.DefaultParams()) - return testInput{cdc: cdc, ctx: ctx, ak: ak, sk: sk} + return testInput{cdc: cdc, Ctx: ctx, Ak: ak, Sk: sk} } // DummySupplyKeeper defines a supply keeper used only for testing to avoid From cd27d10b44ad4e508777ca8224caa1cd1b34b946 Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Wed, 24 Jul 2019 13:02:41 +0200 Subject: [PATCH 5/8] minor changes --- x/auth/ante/ante.go | 7 +++---- x/auth/keeper/account.go | 8 ++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 01d1f0da72de..635c22d20fea 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -152,7 +152,6 @@ func NewAnteHandler(ak keeper.AccountKeeper, supplyKeeper types.SupplyKeeper, si ak.SetAccount(newCtx, signerAccs[i]) } - // TODO: tx tags (?) return newCtx, sdk.Result{GasWanted: stdTx.Fee.Gas}, false // continue... } } @@ -219,7 +218,7 @@ func processSig( if simulate { // Simulated txs should not contain a signature and are not required to // contain a pubkey, so we must account for tx size of including a - // types.StdSignature (Amino encoding) and simulate gas consumption + // StdSignature (Amino encoding) and simulate gas consumption // (assuming a SECP256k1 simulation key). consumeSimSigGas(ctx.GasMeter(), pubKey, sig, params) } @@ -258,7 +257,7 @@ func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig types.Std } // ProcessPubKey verifies that the given account address matches that of the -// types.StdSignature. In addition, it will set the public key of the account if it +// StdSignature. In addition, it will set the public key of the account if it // has not been set. func ProcessPubKey(acc exported.Account, sig types.StdSignature, simulate bool) (crypto.PubKey, sdk.Result) { // If pubkey is not known for account, set it from the types.StdSignature. @@ -333,7 +332,7 @@ func consumeMultisignatureVerificationGas(meter sdk.GasMeter, // DeductFees deducts fees from the given account. // -// NOTE: We could use the CoinKeeper (in addition to the exported.AccountKeeper, because +// NOTE: We could use the CoinKeeper (in addition to the AccountKeeper, because // the CoinKeeper doesn't give us accounts), but it seems easier to do this. func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc exported.Account, fees sdk.Coins) sdk.Result { blockTime := ctx.BlockHeader().Time diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go index 4eb1bfa9d5bb..8417a4a34d2b 100644 --- a/x/auth/keeper/account.go +++ b/x/auth/keeper/account.go @@ -13,14 +13,10 @@ func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddre if err != nil { panic(err) } - err = acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)) - if err != nil { - panic(err) - } - return acc + return ak.NewAccount(ctx, acc) } -// NewAccount creates a new account +// NewAccount sets the next account number to a given account interface func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc exported.Account) exported.Account { if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil { panic(err) From 5c4a51c7c6e8981a0f1ee7cb80d4ecbb67561c8a Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Thu, 25 Jul 2019 15:29:06 +0200 Subject: [PATCH 6/8] clog --- .pending/improvements/modules/_4760-update-auth | 1 + 1 file changed, 1 insertion(+) create mode 100644 .pending/improvements/modules/_4760-update-auth diff --git a/.pending/improvements/modules/_4760-update-auth b/.pending/improvements/modules/_4760-update-auth new file mode 100644 index 000000000000..f200d78a63d6 --- /dev/null +++ b/.pending/improvements/modules/_4760-update-auth @@ -0,0 +1 @@ +#4760 update `x/auth` to match module spec From 46281d1d499cd0ab688fd6ea24c613b8b60c45f9 Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Fri, 26 Jul 2019 11:23:07 +0200 Subject: [PATCH 7/8] changes from code review --- x/auth/ante/ante.go | 2 +- x/auth/ante/ante_test.go | 128 ++++++++++++++--------------- x/auth/keeper/keeper_bench_test.go | 20 ++--- x/auth/keeper/keeper_test.go | 34 ++++---- x/auth/keeper/querier_test.go | 12 +-- x/auth/keeper/test_common.go | 11 +-- 6 files changed, 104 insertions(+), 103 deletions(-) diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 635c22d20fea..457dba893f7b 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -52,7 +52,7 @@ func NewAnteHandler(ak keeper.AccountKeeper, supplyKeeper types.SupplyKeeper, si // Set a gas meter with limit 0 as to prevent an infinite gas meter attack // during runTx. newCtx = SetGasMeter(simulate, ctx, 0) - return newCtx, sdk.ErrInternal("tx must be types.StdTx").Result(), true + return newCtx, sdk.ErrInternal("tx must be StdTx").Result(), true } params := ak.GetParams(ctx) diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 70cf1e976760..218204185770 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -51,7 +51,7 @@ func TestAnteHandlerSigErrors(t *testing.T) { // setup input := keeper.SetupTestInput() ctx := input.Ctx - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() @@ -89,9 +89,9 @@ func TestAnteHandlerSigErrors(t *testing.T) { checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress) // save the first account, but second is still unrecognized - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(fee.Amount) - input.Ak.SetAccount(ctx, acc1) + input.AccountKeeper.SetAccount(ctx, acc1) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress) } @@ -99,7 +99,7 @@ func TestAnteHandlerSigErrors(t *testing.T) { func TestAnteHandlerAccountNumbers(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -107,14 +107,14 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) // msg and signatures var tx sdk.Tx @@ -156,7 +156,7 @@ func TestAnteHandlerAccountNumbers(t *testing.T) { func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(0) // keys and addresses @@ -164,13 +164,13 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts, we don't need the acc numbers as it is in the genesis block - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) // msg and signatures var tx sdk.Tx @@ -212,7 +212,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { func TestAnteHandlerSequences(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -221,18 +221,18 @@ func TestAnteHandlerSequences(t *testing.T) { priv3, _, addr3 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) - acc3 := input.Ak.NewAccountWithAddress(ctx, addr3) + input.AccountKeeper.SetAccount(ctx, acc2) + acc3 := input.AccountKeeper.NewAccountWithAddress(ctx, addr3) acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) - input.Ak.SetAccount(ctx, acc3) + input.AccountKeeper.SetAccount(ctx, acc3) // msg and signatures var tx sdk.Tx @@ -290,14 +290,14 @@ func TestAnteHandlerFees(t *testing.T) { // setup input := keeper.SetupTestInput() ctx := input.Ctx - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) - input.Ak.SetAccount(ctx, acc1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) + input.AccountKeeper.SetAccount(ctx, acc1) // msg and signatures var tx sdk.Tx @@ -311,34 +311,34 @@ func TestAnteHandlerFees(t *testing.T) { checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds) acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) - input.Ak.SetAccount(ctx, acc1) + input.AccountKeeper.SetAccount(ctx, acc1) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds) - require.True(t, input.Sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) - require.True(sdk.IntEq(t, input.Ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149))) + require.True(t, input.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) + require.True(sdk.IntEq(t, input.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149))) acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) - input.Ak.SetAccount(ctx, acc1) + input.AccountKeeper.SetAccount(ctx, acc1) checkValidTx(t, anteHandler, ctx, tx, false) - require.True(sdk.IntEq(t, input.Sk.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150))) - require.True(sdk.IntEq(t, input.Ak.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(0))) + require.True(sdk.IntEq(t, input.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150))) + require.True(sdk.IntEq(t, input.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(0))) } // Test logic around memo gas consumption. func TestAnteHandlerMemoGas(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses priv1, _, addr1 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) + input.AccountKeeper.SetAccount(ctx, acc1) // msg and signatures var tx sdk.Tx @@ -369,7 +369,7 @@ func TestAnteHandlerMemoGas(t *testing.T) { func TestAnteHandlerMultiSigner(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -378,18 +378,18 @@ func TestAnteHandlerMultiSigner(t *testing.T) { priv3, _, addr3 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) - acc3 := input.Ak.NewAccountWithAddress(ctx, addr3) + input.AccountKeeper.SetAccount(ctx, acc2) + acc3 := input.AccountKeeper.NewAccountWithAddress(ctx, addr3) acc3.SetCoins(types.NewTestCoins()) require.NoError(t, acc3.SetAccountNumber(2)) - input.Ak.SetAccount(ctx, acc3) + input.AccountKeeper.SetAccount(ctx, acc3) // set up msgs and fee var tx sdk.Tx @@ -419,7 +419,7 @@ func TestAnteHandlerMultiSigner(t *testing.T) { func TestAnteHandlerBadSignBytes(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -427,14 +427,14 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { priv2, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -496,7 +496,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { func TestAnteHandlerSetPubKey(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -504,14 +504,14 @@ func TestAnteHandlerSetPubKey(t *testing.T) { _, _, addr2 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) require.NoError(t, acc1.SetAccountNumber(0)) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) var tx sdk.Tx @@ -523,7 +523,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) { tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) - acc1 = input.Ak.GetAccount(ctx, addr1) + acc1 = input.AccountKeeper.GetAccount(ctx, addr1) require.Equal(t, acc1.GetPubKey(), priv1.PubKey()) // test public key not found @@ -534,14 +534,14 @@ func TestAnteHandlerSetPubKey(t *testing.T) { sigs[0].PubKey = nil checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey) - acc2 = input.Ak.GetAccount(ctx, addr2) + acc2 = input.AccountKeeper.GetAccount(ctx, addr2) require.Nil(t, acc2.GetPubKey()) // test invalid signature and public key tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey) - acc2 = input.Ak.GetAccount(ctx, addr2) + acc2 = input.AccountKeeper.GetAccount(ctx, addr2) require.Nil(t, acc2.GetPubKey()) } @@ -552,8 +552,8 @@ func TestProcessPubKey(t *testing.T) { // keys _, _, addr1 := types.KeyTestPubAddr() priv2, _, addr2 := types.KeyTestPubAddr() - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetPubKey(priv2.PubKey()) @@ -692,7 +692,7 @@ func TestCountSubkeys(t *testing.T) { func TestAnteHandlerSigLimitExceeded(t *testing.T) { // setup input := keeper.SetupTestInput() - anteHandler := NewAnteHandler(input.Ak, input.Sk, DefaultSigVerificationGasConsumer) + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer) ctx := input.Ctx.WithBlockHeight(1) // keys and addresses @@ -706,13 +706,13 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) { priv8, _, addr8 := types.KeyTestPubAddr() // set the accounts - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) acc1.SetCoins(types.NewTestCoins()) - input.Ak.SetAccount(ctx, acc1) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + input.AccountKeeper.SetAccount(ctx, acc1) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) acc2.SetCoins(types.NewTestCoins()) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) var tx sdk.Tx msg := types.NewTestMsg(addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8) @@ -782,7 +782,7 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { // setup input := keeper.SetupTestInput() // setup an ante handler that only accepts PubKeyEd25519 - anteHandler := NewAnteHandler(input.Ak, input.Sk, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result { + anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result { switch pubkey := pubkey.(type) { case ed25519.PubKeyEd25519: meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") @@ -795,9 +795,9 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { // verify that an secp256k1 account gets rejected priv1, _, addr1 := types.KeyTestPubAddr() - acc1 := input.Ak.NewAccountWithAddress(ctx, addr1) + acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1) _ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) - input.Ak.SetAccount(ctx, acc1) + input.AccountKeeper.SetAccount(ctx, acc1) var tx sdk.Tx msg := types.NewTestMsg(addr1) @@ -811,10 +811,10 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) { priv2 := ed25519.GenPrivKey() pub2 := priv2.PubKey() addr2 := sdk.AccAddress(pub2.Address()) - acc2 := input.Ak.NewAccountWithAddress(ctx, addr2) + acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2) require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))) require.NoError(t, acc2.SetAccountNumber(1)) - input.Ak.SetAccount(ctx, acc2) + input.AccountKeeper.SetAccount(ctx, acc2) msg = types.NewTestMsg(addr2) privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{1}, []uint64{0} fee = types.NewTestStdFee() diff --git a/x/auth/keeper/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go index 932f1e83a4c4..fc9da6db17b1 100644 --- a/x/auth/keeper/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -13,14 +13,14 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) - input.Ak.SetAccount(input.Ctx, acc) + acc := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr) + input.AccountKeeper.SetAccount(input.Ctx, acc) } b.ResetTimer() for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - input.Ak.GetAccount(input.Ctx, sdk.AccAddress(arr)) + input.AccountKeeper.GetAccount(input.Ctx, sdk.AccAddress(arr)) } } @@ -39,15 +39,15 @@ func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) + acc := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr) acc.SetCoins(coins) - input.Ak.SetAccount(input.Ctx, acc) + input.AccountKeeper.SetAccount(input.Ctx, acc) } b.ResetTimer() for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - input.Ak.GetAccount(input.Ctx, sdk.AccAddress(arr)) + input.AccountKeeper.GetAccount(input.Ctx, sdk.AccAddress(arr)) } } @@ -60,8 +60,8 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) - input.Ak.SetAccount(input.Ctx, acc) + acc := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr) + input.AccountKeeper.SetAccount(input.Ctx, acc) } } @@ -82,8 +82,8 @@ func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := input.Ak.NewAccountWithAddress(input.Ctx, addr) + acc := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr) acc.SetCoins(coins) - input.Ak.SetAccount(input.Ctx, acc) + input.AccountKeeper.SetAccount(input.Ctx, acc) } } diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 438eddf41323..9e94a8be1149 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -14,26 +14,26 @@ func TestAccountMapperGetSet(t *testing.T) { addr := sdk.AccAddress([]byte("some-address")) // no account before its created - acc := input.Ak.GetAccount(input.Ctx, addr) + acc := input.AccountKeeper.GetAccount(input.Ctx, addr) require.Nil(t, acc) // create account and check default values - acc = input.Ak.NewAccountWithAddress(input.Ctx, addr) + acc = input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr) require.NotNil(t, acc) require.Equal(t, addr, acc.GetAddress()) require.EqualValues(t, nil, acc.GetPubKey()) require.EqualValues(t, 0, acc.GetSequence()) // NewAccount doesn't call Set, so it's still nil - require.Nil(t, input.Ak.GetAccount(input.Ctx, addr)) + require.Nil(t, input.AccountKeeper.GetAccount(input.Ctx, addr)) // set some values on the account and save it newSequence := uint64(20) acc.SetSequence(newSequence) - input.Ak.SetAccount(input.Ctx, acc) + input.AccountKeeper.SetAccount(input.Ctx, acc) // check the new values - acc = input.Ak.GetAccount(input.Ctx, addr) + acc = input.AccountKeeper.GetAccount(input.Ctx, addr) require.NotNil(t, acc) require.Equal(t, newSequence, acc.GetSequence()) } @@ -44,27 +44,27 @@ func TestAccountMapperRemoveAccount(t *testing.T) { addr2 := sdk.AccAddress([]byte("addr2")) // create accounts - acc1 := input.Ak.NewAccountWithAddress(input.Ctx, addr1) - acc2 := input.Ak.NewAccountWithAddress(input.Ctx, addr2) + acc1 := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr1) + acc2 := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr2) accSeq1 := uint64(20) accSeq2 := uint64(40) acc1.SetSequence(accSeq1) acc2.SetSequence(accSeq2) - input.Ak.SetAccount(input.Ctx, acc1) - input.Ak.SetAccount(input.Ctx, acc2) + input.AccountKeeper.SetAccount(input.Ctx, acc1) + input.AccountKeeper.SetAccount(input.Ctx, acc2) - acc1 = input.Ak.GetAccount(input.Ctx, addr1) + acc1 = input.AccountKeeper.GetAccount(input.Ctx, addr1) require.NotNil(t, acc1) require.Equal(t, accSeq1, acc1.GetSequence()) // remove one account - input.Ak.RemoveAccount(input.Ctx, acc1) - acc1 = input.Ak.GetAccount(input.Ctx, addr1) + input.AccountKeeper.RemoveAccount(input.Ctx, acc1) + acc1 = input.AccountKeeper.GetAccount(input.Ctx, addr1) require.Nil(t, acc1) - acc2 = input.Ak.GetAccount(input.Ctx, addr2) + acc2 = input.AccountKeeper.GetAccount(input.Ctx, addr2) require.NotNil(t, acc2) require.Equal(t, accSeq2, acc2.GetSequence()) } @@ -73,10 +73,10 @@ func TestSetParams(t *testing.T) { input := SetupTestInput() params := types.DefaultParams() - input.Ak.SetParams(input.Ctx, params) + input.AccountKeeper.SetParams(input.Ctx, params) newParams := types.Params{} - input.Ak.paramSubspace.Get(input.Ctx, types.KeyTxSigLimit, &newParams.TxSigLimit) + input.AccountKeeper.paramSubspace.Get(input.Ctx, types.KeyTxSigLimit, &newParams.TxSigLimit) require.Equal(t, newParams.TxSigLimit, types.DefaultTxSigLimit) } @@ -84,8 +84,8 @@ func TestGetParams(t *testing.T) { input := SetupTestInput() params := types.DefaultParams() - input.Ak.SetParams(input.Ctx, params) + input.AccountKeeper.SetParams(input.Ctx, params) - newParams := input.Ak.GetParams(input.Ctx) + newParams := input.AccountKeeper.GetParams(input.Ctx) require.Equal(t, params, newParams) } diff --git a/x/auth/keeper/querier_test.go b/x/auth/keeper/querier_test.go index 621f2184361d..0e5e7ba478a8 100644 --- a/x/auth/keeper/querier_test.go +++ b/x/auth/keeper/querier_test.go @@ -20,7 +20,7 @@ func TestQueryAccount(t *testing.T) { Data: []byte{}, } - querier := NewQuerier(input.Ak) + querier := NewQuerier(input.AccountKeeper) bz, err := querier(input.Ctx, []string{"other"}, req) require.Error(t, err) @@ -30,23 +30,23 @@ func TestQueryAccount(t *testing.T) { Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), Data: []byte{}, } - res, err := queryAccount(input.Ctx, req, input.Ak) + res, err := queryAccount(input.Ctx, req, input.AccountKeeper) require.Error(t, err) require.Nil(t, res) req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte(""))) - res, err = queryAccount(input.Ctx, req, input.Ak) + res, err = queryAccount(input.Ctx, req, input.AccountKeeper) require.Error(t, err) require.Nil(t, res) _, _, addr := types.KeyTestPubAddr() req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams(addr)) - res, err = queryAccount(input.Ctx, req, input.Ak) + res, err = queryAccount(input.Ctx, req, input.AccountKeeper) require.Error(t, err) require.Nil(t, res) - input.Ak.SetAccount(input.Ctx, input.Ak.NewAccountWithAddress(input.Ctx, addr)) - res, err = queryAccount(input.Ctx, req, input.Ak) + input.AccountKeeper.SetAccount(input.Ctx, input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)) + res, err = queryAccount(input.Ctx, req, input.AccountKeeper) require.NoError(t, err) require.NotNil(t, res) diff --git a/x/auth/keeper/test_common.go b/x/auth/keeper/test_common.go index 912239b27262..b78833a992c0 100644 --- a/x/auth/keeper/test_common.go +++ b/x/auth/keeper/test_common.go @@ -1,7 +1,8 @@ // nolint -// DONTCOVER package keeper +// DONTCOVER + import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto" @@ -17,10 +18,10 @@ import ( ) type testInput struct { - cdc *codec.Codec - Ctx sdk.Context - Ak AccountKeeper - Sk types.SupplyKeeper + cdc *codec.Codec + Ctx sdk.Context + AccountKeeper AccountKeeper + SupplyKeeper types.SupplyKeeper } // moduleAccount defines an account for modules that holds coins on a pool From 86fca84c51ce29c34123f3168819947e7b304b9f Mon Sep 17 00:00:00 2001 From: Federico Kunze <federico.kunze94@gmail.com> Date: Fri, 26 Jul 2019 11:23:56 +0200 Subject: [PATCH 8/8] fix --- x/auth/keeper/test_common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/auth/keeper/test_common.go b/x/auth/keeper/test_common.go index b78833a992c0..ee2a7fb4956b 100644 --- a/x/auth/keeper/test_common.go +++ b/x/auth/keeper/test_common.go @@ -78,7 +78,7 @@ func SetupTestInput() testInput { ak.SetParams(ctx, types.DefaultParams()) - return testInput{cdc: cdc, Ctx: ctx, Ak: ak, Sk: sk} + return testInput{cdc: cdc, Ctx: ctx, AccountKeeper: ak, SupplyKeeper: sk} } // DummySupplyKeeper defines a supply keeper used only for testing to avoid @@ -138,7 +138,7 @@ func (sk DummySupplyKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) macc := &moduleAccount{ BaseAccount: &baseAcc, name: moduleName, - permissions: []string{"basic"}, + permissions: nil, } maccI := (sk.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)