Merge PR #4896: Refactor auth tests

This commit is contained in:
colin axner 2019-08-15 08:19:16 -07:00 committed by Alexander Bezobchuk
parent 74cb4869b9
commit 7ffa95d675
10 changed files with 264 additions and 309 deletions

View File

@ -40,7 +40,6 @@ var (
SetGasMeter = ante.SetGasMeter
GetSignBytes = ante.GetSignBytes
NewAccountKeeper = keeper.NewAccountKeeper
NewDummySupplyKeeper = keeper.NewDummySupplyKeeper
NewQuerier = keeper.NewQuerier
NewBaseAccount = types.NewBaseAccount
ProtoBaseAccount = types.ProtoBaseAccount

View File

@ -1,4 +1,4 @@
package ante
package ante_test
import (
"fmt"
@ -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/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"
)
@ -49,9 +49,8 @@ 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 := keeper.SetupTestInput()
ctx := input.Ctx
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
app, ctx := createTestApp(true)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
@ -89,32 +88,32 @@ func TestAnteHandlerSigErrors(t *testing.T) {
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress)
// save the first account, but second is still unrecognized
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(fee.Amount)
input.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc1)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress)
}
// Test logic around account number checking with one signer and many signers.
func TestAnteHandlerAccountNumbers(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
priv2, _, addr2 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
// msg and signatures
var tx sdk.Tx
@ -155,22 +154,22 @@ 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 := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(0)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(0)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// 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.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
// msg and signatures
var tx sdk.Tx
@ -211,9 +210,9 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
// Test logic around sequence checking with one signer and many signers.
func TestAnteHandlerSequences(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
@ -221,18 +220,18 @@ func TestAnteHandlerSequences(t *testing.T) {
priv3, _, addr3 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
acc3 := input.AccountKeeper.NewAccountWithAddress(ctx, addr3)
app.AccountKeeper.SetAccount(ctx, acc2)
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
acc3.SetCoins(types.NewTestCoins())
require.NoError(t, acc3.SetAccountNumber(2))
input.AccountKeeper.SetAccount(ctx, acc3)
app.AccountKeeper.SetAccount(ctx, acc3)
// msg and signatures
var tx sdk.Tx
@ -288,16 +287,15 @@ func TestAnteHandlerSequences(t *testing.T) {
// Test logic around fee deduction.
func TestAnteHandlerFees(t *testing.T) {
// setup
input := keeper.SetupTestInput()
ctx := input.Ctx
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
app, ctx := createTestApp(true)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
input.AccountKeeper.SetAccount(ctx, acc1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
// msg and signatures
var tx sdk.Tx
@ -311,34 +309,34 @@ func TestAnteHandlerFees(t *testing.T) {
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)
acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
input.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc1)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)
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)))
require.True(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty())
require.True(sdk.IntEq(t, app.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewInt(149)))
acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
input.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc1)
checkValidTx(t, anteHandler, ctx, tx, false)
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)))
require.True(sdk.IntEq(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewInt(150)))
require.True(sdk.IntEq(t, app.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.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc1)
// msg and signatures
var tx sdk.Tx
@ -368,9 +366,9 @@ func TestAnteHandlerMemoGas(t *testing.T) {
func TestAnteHandlerMultiSigner(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
@ -378,18 +376,18 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
priv3, _, addr3 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
acc3 := input.AccountKeeper.NewAccountWithAddress(ctx, addr3)
app.AccountKeeper.SetAccount(ctx, acc2)
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
acc3.SetCoins(types.NewTestCoins())
require.NoError(t, acc3.SetAccountNumber(2))
input.AccountKeeper.SetAccount(ctx, acc3)
app.AccountKeeper.SetAccount(ctx, acc3)
// set up msgs and fee
var tx sdk.Tx
@ -418,23 +416,23 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
func TestAnteHandlerBadSignBytes(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
priv2, _, addr2 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
var tx sdk.Tx
msg := types.NewTestMsg(addr1)
@ -495,23 +493,23 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
func TestAnteHandlerSetPubKey(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
_, _, addr2 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
var tx sdk.Tx
@ -523,7 +521,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee)
checkValidTx(t, anteHandler, ctx, tx, false)
acc1 = input.AccountKeeper.GetAccount(ctx, addr1)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.Equal(t, acc1.GetPubKey(), priv1.PubKey())
// test public key not found
@ -534,26 +532,25 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
sigs[0].PubKey = nil
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey)
acc2 = input.AccountKeeper.GetAccount(ctx, addr2)
acc2 = app.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.AccountKeeper.GetAccount(ctx, addr2)
acc2 = app.AccountKeeper.GetAccount(ctx, addr2)
require.Nil(t, acc2.GetPubKey())
}
func TestProcessPubKey(t *testing.T) {
input := keeper.SetupTestInput()
ctx := input.Ctx
app, ctx := createTestApp(true)
// keys
_, _, addr1 := types.KeyTestPubAddr()
priv2, _, addr2 := types.KeyTestPubAddr()
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetPubKey(priv2.PubKey())
@ -575,7 +572,7 @@ func TestProcessPubKey(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ProcessPubKey(tt.args.acc, tt.args.sig, tt.args.simulate)
_, err := ante.ProcessPubKey(tt.args.acc, tt.args.sig, tt.args.simulate)
require.Equal(t, tt.wantErr, !err.IsOK())
})
}
@ -612,7 +609,7 @@ func TestConsumeSignatureVerificationGas(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := DefaultSigVerificationGasConsumer(tt.args.meter, tt.args.sig, tt.args.pubkey, tt.args.params)
res := ante.DefaultSigVerificationGasConsumer(tt.args.meter, tt.args.sig, tt.args.pubkey, tt.args.params)
if tt.shouldErr {
require.False(t, res.IsOK())
@ -691,9 +688,9 @@ func TestCountSubkeys(t *testing.T) {
func TestAnteHandlerSigLimitExceeded(t *testing.T) {
// setup
input := keeper.SetupTestInput()
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, DefaultSigVerificationGasConsumer)
ctx := input.Ctx.WithBlockHeight(1)
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)
// keys and addresses
priv1, _, addr1 := types.KeyTestPubAddr()
@ -706,13 +703,13 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) {
priv8, _, addr8 := types.KeyTestPubAddr()
// set the accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
input.AccountKeeper.SetAccount(ctx, acc1)
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
var tx sdk.Tx
msg := types.NewTestMsg(addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8)
@ -728,8 +725,8 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) {
func TestEnsureSufficientMempoolFees(t *testing.T) {
// setup
input := keeper.SetupTestInput()
ctx := input.Ctx.WithMinGasPrices(
_, ctx := createTestApp(true)
ctx = ctx.WithMinGasPrices(
sdk.DecCoins{
sdk.NewDecCoinFromDec("photino", sdk.NewDecWithPrec(50000000000000, sdk.Precision)), // 0.0001photino
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(10000000000000, sdk.Precision)), // 0.000001stake
@ -769,7 +766,7 @@ func TestEnsureSufficientMempoolFees(t *testing.T) {
}
for i, tc := range testCases {
res := EnsureSufficientMempoolFees(ctx, tc.input)
res := ante.EnsureSufficientMempoolFees(ctx, tc.input)
require.Equal(
t, tc.expectedOK, res.IsOK(),
"unexpected result; tc #%d, input: %v, log: %v", i, tc.input, res.Log,
@ -780,9 +777,10 @@ func TestEnsureSufficientMempoolFees(t *testing.T) {
// Test custom SignatureVerificationGasConsumer
func TestCustomSignatureVerificationGasConsumer(t *testing.T) {
// setup
input := keeper.SetupTestInput()
app, ctx := createTestApp(true)
ctx = ctx.WithBlockHeight(1)
// setup an ante handler that only accepts PubKeyEd25519
anteHandler := NewAnteHandler(input.AccountKeeper, input.SupplyKeeper, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result {
anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.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")
@ -791,13 +789,12 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) {
return sdk.ErrInvalidPubKey(fmt.Sprintf("unrecognized public key type: %T", pubkey)).Result()
}
})
ctx := input.Ctx.WithBlockHeight(1)
// verify that an secp256k1 account gets rejected
priv1, _, addr1 := types.KeyTestPubAddr()
acc1 := input.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
_ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
input.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc1)
var tx sdk.Tx
msg := types.NewTestMsg(addr1)
@ -811,10 +808,10 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) {
priv2 := ed25519.GenPrivKey()
pub2 := priv2.PubKey()
addr2 := sdk.AccAddress(pub2.Address())
acc2 := input.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))))
require.NoError(t, acc2.SetAccountNumber(1))
input.AccountKeeper.SetAccount(ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc2)
msg = types.NewTestMsg(addr2)
privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{1}, []uint64{0}
fee = types.NewTestStdFee()

View File

@ -0,0 +1,18 @@
package ante_test
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
// returns context and app with params set on account keeper
func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) {
app := simapp.Setup(isCheckTx)
ctx := app.BaseApp.NewContext(isCheckTx, abci.Header{})
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
return app, ctx
}

View File

@ -0,0 +1,18 @@
package keeper_test
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
// returns context and app with params set on account keeper
func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) {
app := simapp.Setup(isCheckTx)
ctx := app.BaseApp.NewContext(isCheckTx, abci.Header{})
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
return app, ctx
}

View File

@ -1,4 +1,4 @@
package keeper
package keeper_test
import (
"testing"
@ -7,25 +7,26 @@ import (
)
func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
input := SetupTestInput()
app, ctx := createTestApp(false)
// assumes b.N < 2**24
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.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)
input.AccountKeeper.SetAccount(input.Ctx, acc)
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(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.AccountKeeper.GetAccount(input.Ctx, sdk.AccAddress(arr))
app.AccountKeeper.GetAccount(ctx, sdk.AccAddress(arr))
}
}
func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) {
input := SetupTestInput()
app, ctx := createTestApp(false)
coins := sdk.Coins{
sdk.NewCoin("LTC", sdk.NewInt(1000)),
sdk.NewCoin("BTC", sdk.NewInt(1000)),
@ -39,20 +40,20 @@ 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.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc.SetCoins(coins)
input.AccountKeeper.SetAccount(input.Ctx, acc)
app.AccountKeeper.SetAccount(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.AccountKeeper.GetAccount(input.Ctx, sdk.AccAddress(arr))
app.AccountKeeper.GetAccount(ctx, sdk.AccAddress(arr))
}
}
func BenchmarkAccountMapperSetAccount(b *testing.B) {
input := SetupTestInput()
app, ctx := createTestApp(false)
b.ResetTimer()
@ -60,13 +61,14 @@ 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.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)
input.AccountKeeper.SetAccount(input.Ctx, acc)
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
}
}
func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) {
input := SetupTestInput()
app, ctx := createTestApp(false)
coins := sdk.Coins{
sdk.NewCoin("LTC", sdk.NewInt(1000)),
sdk.NewCoin("BTC", sdk.NewInt(1000)),
@ -82,8 +84,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.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc.SetCoins(coins)
input.AccountKeeper.SetAccount(input.Ctx, acc)
app.AccountKeeper.SetAccount(ctx, acc)
}
}

View File

@ -1,4 +1,4 @@
package keeper
package keeper_test
import (
"testing"
@ -10,82 +10,71 @@ import (
)
func TestAccountMapperGetSet(t *testing.T) {
input := SetupTestInput()
app, ctx := createTestApp(true)
addr := sdk.AccAddress([]byte("some-address"))
// no account before its created
acc := input.AccountKeeper.GetAccount(input.Ctx, addr)
acc := app.AccountKeeper.GetAccount(ctx, addr)
require.Nil(t, acc)
// create account and check default values
acc = input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr)
acc = app.AccountKeeper.NewAccountWithAddress(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.AccountKeeper.GetAccount(input.Ctx, addr))
require.Nil(t, app.AccountKeeper.GetAccount(ctx, addr))
// set some values on the account and save it
newSequence := uint64(20)
acc.SetSequence(newSequence)
input.AccountKeeper.SetAccount(input.Ctx, acc)
app.AccountKeeper.SetAccount(ctx, acc)
// check the new values
acc = input.AccountKeeper.GetAccount(input.Ctx, addr)
acc = app.AccountKeeper.GetAccount(ctx, addr)
require.NotNil(t, acc)
require.Equal(t, newSequence, acc.GetSequence())
}
func TestAccountMapperRemoveAccount(t *testing.T) {
input := SetupTestInput()
app, ctx := createTestApp(true)
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
// create accounts
acc1 := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr1)
acc2 := input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr2)
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
accSeq1 := uint64(20)
accSeq2 := uint64(40)
acc1.SetSequence(accSeq1)
acc2.SetSequence(accSeq2)
input.AccountKeeper.SetAccount(input.Ctx, acc1)
input.AccountKeeper.SetAccount(input.Ctx, acc2)
app.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc2)
acc1 = input.AccountKeeper.GetAccount(input.Ctx, addr1)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.NotNil(t, acc1)
require.Equal(t, accSeq1, acc1.GetSequence())
// remove one account
input.AccountKeeper.RemoveAccount(input.Ctx, acc1)
acc1 = input.AccountKeeper.GetAccount(input.Ctx, addr1)
app.AccountKeeper.RemoveAccount(ctx, acc1)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.Nil(t, acc1)
acc2 = input.AccountKeeper.GetAccount(input.Ctx, addr2)
acc2 = app.AccountKeeper.GetAccount(ctx, addr2)
require.NotNil(t, acc2)
require.Equal(t, accSeq2, acc2.GetSequence())
}
func TestSetParams(t *testing.T) {
input := SetupTestInput()
func TestGetSetParams(t *testing.T) {
app, ctx := createTestApp(true)
params := types.DefaultParams()
input.AccountKeeper.SetParams(input.Ctx, params)
app.AccountKeeper.SetParams(ctx, params)
newParams := types.Params{}
input.AccountKeeper.paramSubspace.Get(input.Ctx, types.KeyTxSigLimit, &newParams.TxSigLimit)
require.Equal(t, newParams.TxSigLimit, types.DefaultTxSigLimit)
}
func TestGetParams(t *testing.T) {
input := SetupTestInput()
params := types.DefaultParams()
input.AccountKeeper.SetParams(input.Ctx, params)
newParams := input.AccountKeeper.GetParams(input.Ctx)
require.Equal(t, params, newParams)
actualParams := app.AccountKeeper.GetParams(ctx)
require.Equal(t, params, actualParams)
}

View File

@ -1,4 +1,4 @@
package keeper
package keeper_test
import (
"fmt"
@ -9,20 +9,23 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
keep "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
func TestQueryAccount(t *testing.T) {
input := SetupTestInput()
app, ctx := createTestApp(true)
cdc := app.Codec()
req := abci.RequestQuery{
Path: "",
Data: []byte{},
}
querier := NewQuerier(input.AccountKeeper)
path := []string{types.QueryAccount}
querier := keep.NewQuerier(app.AccountKeeper)
bz, err := querier(input.Ctx, []string{"other"}, req)
bz, err := querier(ctx, []string{"other"}, req)
require.Error(t, err)
require.Nil(t, bz)
@ -30,31 +33,31 @@ func TestQueryAccount(t *testing.T) {
Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount),
Data: []byte{},
}
res, err := queryAccount(input.Ctx, req, input.AccountKeeper)
res, err := querier(ctx, path, req)
require.Error(t, err)
require.Nil(t, res)
req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte("")))
res, err = queryAccount(input.Ctx, req, input.AccountKeeper)
req.Data = cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte("")))
res, err = querier(ctx, path, req)
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.AccountKeeper)
req.Data = cdc.MustMarshalJSON(types.NewQueryAccountParams(addr))
res, err = querier(ctx, path, req)
require.Error(t, err)
require.Nil(t, res)
input.AccountKeeper.SetAccount(input.Ctx, input.AccountKeeper.NewAccountWithAddress(input.Ctx, addr))
res, err = queryAccount(input.Ctx, req, input.AccountKeeper)
app.AccountKeeper.SetAccount(ctx, app.AccountKeeper.NewAccountWithAddress(ctx, addr))
res, err = querier(ctx, path, req)
require.NoError(t, err)
require.NotNil(t, res)
res, err = querier(input.Ctx, []string{types.QueryAccount}, req)
res, err = querier(ctx, path, req)
require.NoError(t, err)
require.NotNil(t, res)
var account exported.Account
err2 := input.cdc.UnmarshalJSON(res, &account)
err2 := cdc.UnmarshalJSON(res, &account)
require.Nil(t, err2)
}

View File

@ -1,152 +0,0 @@
// nolint
package keeper
// DONTCOVER
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
"github.com/cosmos/cosmos-sdk/x/supply/exported"
)
type testInput struct {
cdc *codec.Codec
Ctx sdk.Context
AccountKeeper AccountKeeper
SupplyKeeper types.SupplyKeeper
}
// moduleAccount defines an account for modules that holds coins on a pool
type moduleAccount struct {
*types.BaseAccount
name string `json:"name" yaml:"name"` // name of the module
permissions []string `json:"permissions" yaml"permissions"` // permissions of module account
}
// HasPermission returns whether or not the module account has permission.
func (ma moduleAccount) HasPermission(permission string) bool {
for _, perm := range ma.permissions {
if perm == permission {
return true
}
}
return false
}
// GetName returns the the name of the holder's module
func (ma moduleAccount) GetName() string {
return ma.name
}
// GetPermissions returns permissions granted to the module account
func (ma moduleAccount) GetPermissions() []string {
return ma.permissions
}
func SetupTestInput() testInput {
db := dbm.NewMemDB()
cdc := codec.New()
types.RegisterCodec(cdc)
cdc.RegisterInterface((*exported.ModuleAccountI)(nil), nil)
cdc.RegisterConcrete(&moduleAccount{}, "cosmos-sdk/ModuleAccount", nil)
codec.RegisterCrypto(cdc)
authCapKey := sdk.NewKVStoreKey("authCapKey")
keyParams := sdk.NewKVStoreKey("subspace")
tkeyParams := sdk.NewTransientStoreKey("transient_subspace")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(authCapKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.LoadLatestVersion()
ps := subspace.NewSubspace(cdc, keyParams, tkeyParams, types.DefaultParamspace)
ak := NewAccountKeeper(cdc, authCapKey, ps, types.ProtoBaseAccount)
sk := NewDummySupplyKeeper(ak)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "test-chain-id"}, false, log.NewNopLogger())
ak.SetParams(ctx, types.DefaultParams())
return testInput{cdc: cdc, Ctx: ctx, AccountKeeper: ak, SupplyKeeper: sk}
}
// DummySupplyKeeper defines a supply keeper used only for testing to avoid
// circle dependencies
type DummySupplyKeeper struct {
ak AccountKeeper
}
// NewDummySupplyKeeper creates a DummySupplyKeeper instance
func NewDummySupplyKeeper(ak AccountKeeper) DummySupplyKeeper {
return DummySupplyKeeper{ak}
}
// SendCoinsFromAccountToModule for the dummy supply keeper
func (sk DummySupplyKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, fromAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) sdk.Error {
fromAcc := sk.ak.GetAccount(ctx, fromAddr)
moduleAcc := sk.GetModuleAccount(ctx, recipientModule)
newFromCoins, hasNeg := fromAcc.GetCoins().SafeSub(amt)
if hasNeg {
return sdk.ErrInsufficientCoins(fromAcc.GetCoins().String())
}
newToCoins := moduleAcc.GetCoins().Add(amt)
if err := fromAcc.SetCoins(newFromCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
if err := moduleAcc.SetCoins(newToCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
sk.ak.SetAccount(ctx, fromAcc)
sk.ak.SetAccount(ctx, moduleAcc)
return nil
}
// GetModuleAccount for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
addr := sk.GetModuleAddress(moduleName)
acc := sk.ak.GetAccount(ctx, addr)
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if ok {
return macc
}
}
moduleAddress := sk.GetModuleAddress(moduleName)
baseAcc := types.NewBaseAccountWithAddress(moduleAddress)
// create a new module account
macc := &moduleAccount{
BaseAccount: &baseAcc,
name: moduleName,
permissions: nil,
}
maccI := (sk.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)
sk.ak.SetAccount(ctx, maccI)
return maccI
}
// GetModuleAddress for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(moduleName)))
}

View File

@ -73,7 +73,7 @@ func NewApp() *App {
auth.ProtoBaseAccount,
)
supplyKeeper := auth.NewDummySupplyKeeper(app.AccountKeeper)
supplyKeeper := NewDummySupplyKeeper(app.AccountKeeper)
// Initialize the app. The chainers and blockers can be overwritten before
// calling complete setup.

81
x/mock/types.go Normal file
View File

@ -0,0 +1,81 @@
package mock
import (
"github.com/tendermint/tendermint/crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/cosmos/cosmos-sdk/x/supply/exported"
)
// DummySupplyKeeper defines a supply keeper used only for testing to avoid
// circle dependencies
type DummySupplyKeeper struct {
ak auth.AccountKeeper
}
// NewDummySupplyKeeper creates a DummySupplyKeeper instance
func NewDummySupplyKeeper(ak auth.AccountKeeper) DummySupplyKeeper {
return DummySupplyKeeper{ak}
}
// SendCoinsFromAccountToModule for the dummy supply keeper
func (sk DummySupplyKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, fromAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) sdk.Error {
fromAcc := sk.ak.GetAccount(ctx, fromAddr)
moduleAcc := sk.GetModuleAccount(ctx, recipientModule)
newFromCoins, hasNeg := fromAcc.GetCoins().SafeSub(amt)
if hasNeg {
return sdk.ErrInsufficientCoins(fromAcc.GetCoins().String())
}
newToCoins := moduleAcc.GetCoins().Add(amt)
if err := fromAcc.SetCoins(newFromCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
if err := moduleAcc.SetCoins(newToCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
sk.ak.SetAccount(ctx, fromAcc)
sk.ak.SetAccount(ctx, moduleAcc)
return nil
}
// GetModuleAccount for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
addr := sk.GetModuleAddress(moduleName)
acc := sk.ak.GetAccount(ctx, addr)
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if ok {
return macc
}
}
moduleAddress := sk.GetModuleAddress(moduleName)
baseAcc := auth.NewBaseAccountWithAddress(moduleAddress)
// create a new module account
macc := &supply.ModuleAccount{
BaseAccount: &baseAcc,
Name: moduleName,
Permissions: nil,
}
maccI := (sk.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)
sk.ak.SetAccount(ctx, maccI)
return maccI
}
// GetModuleAddress for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(moduleName)))
}