Skip to content

Commit

Permalink
Merge PR #5580: ADR 004 Implementation II (Unit Tests & Misc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Jan 30, 2020
1 parent d8eb4b7 commit 4290946
Show file tree
Hide file tree
Showing 81 changed files with 1,692 additions and 1,204 deletions.
17 changes: 3 additions & 14 deletions simapp/genesis_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestSimGenesisAccountValidate(t *testing.T) {
vestingStart := time.Now().UTC()

coins := sdk.NewCoins(sdk.NewInt64Coin("test", 1000))
baseAcc := authtypes.NewBaseAccount(addr, nil, pubkey, 0, 0)
require.NoError(t, baseAcc.SetCoins(coins))
baseAcc := authtypes.NewBaseAccount(addr, pubkey, 0, 0)

testCases := []struct {
name string
Expand All @@ -38,14 +37,14 @@ func TestSimGenesisAccountValidate(t *testing.T) {
{
"invalid basic account with mismatching address/pubkey",
simapp.SimGenesisAccount{
BaseAccount: authtypes.NewBaseAccount(addr, nil, secp256k1.GenPrivKey().PubKey(), 0, 0),
BaseAccount: authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0),
},
true,
},
{
"valid basic account with module name",
simapp.SimGenesisAccount{
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(crypto.AddressHash([]byte("testmod"))), nil, nil, 0, 0),
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(crypto.AddressHash([]byte("testmod"))), nil, 0, 0),
ModuleName: "testmod",
},
false,
Expand Down Expand Up @@ -78,16 +77,6 @@ func TestSimGenesisAccountValidate(t *testing.T) {
},
true,
},
{
"valid basic account with invalid original vesting coins",
simapp.SimGenesisAccount{
BaseAccount: baseAcc,
OriginalVesting: coins.Add(coins...),
StartTime: vestingStart.Unix(),
EndTime: vestingStart.Add(1 * time.Hour).Unix(),
},
true,
},
}

for _, tc := range testCases {
Expand Down
3 changes: 1 addition & 2 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simapp

import (
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -48,7 +47,7 @@ func Setup(isCheckTx bool) *SimApp {
// genesis accounts.
func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount) *SimApp {
db := dbm.NewMemDB()
app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0)
app := NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 0)

// initialize the chain with the passed in genesis accounts
genesisState := NewDefaultGenesisState()
Expand Down
4 changes: 1 addition & 3 deletions store/prefix/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (

// copied from iavl/store_test.go
var (
cacheSize = 100
numRecent int64 = 5
storeEvery int64 = 3
cacheSize = 100
)

func bz(s string) []byte { return []byte(s) }
Expand Down
61 changes: 32 additions & 29 deletions x/auth/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ func TestSimulateGasCost(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, acc1.GetAddress(), types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, acc2.GetAddress(), types.NewTestCoins())
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
acc3.SetCoins(types.NewTestCoins())
require.NoError(t, acc3.SetAccountNumber(2))
app.AccountKeeper.SetAccount(ctx, acc3)
app.BankKeeper.SetBalances(ctx, acc3.GetAddress(), types.NewTestCoins())

// set up msgs and fee
var tx sdk.Tx
Expand Down Expand Up @@ -128,8 +128,8 @@ func TestAnteHandlerSigErrors(t *testing.T) {

// save the first account, but second is still unrecognized
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(fee.Amount)
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, fee.Amount)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnknownAddress)
}

Expand All @@ -146,13 +146,13 @@ func TestAnteHandlerAccountNumbers(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())

// msg and signatures
var tx sdk.Tx
Expand Down Expand Up @@ -203,12 +203,12 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {

// set the accounts, we don't need the acc numbers as it is in the genesis block
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())

// msg and signatures
var tx sdk.Tx
Expand Down Expand Up @@ -260,17 +260,17 @@ func TestAnteHandlerSequences(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
acc3.SetCoins(types.NewTestCoins())
require.NoError(t, acc3.SetAccountNumber(2))
app.AccountKeeper.SetAccount(ctx, acc3)
app.BankKeeper.SetBalances(ctx, addr3, types.NewTestCoins())

// msg and signatures
var tx sdk.Tx
Expand Down Expand Up @@ -347,19 +347,21 @@ func TestAnteHandlerFees(t *testing.T) {
tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds)

acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds)

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)))
modAcc := app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName)

require.True(t, app.BankKeeper.GetAllBalances(ctx, modAcc.GetAddress()).Empty())
require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, addr1).AmountOf("atom"), sdk.NewInt(149)))

acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
checkValidTx(t, anteHandler, ctx, tx, false)

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)))
require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150)))
require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, addr1).AmountOf("atom"), sdk.NewInt(0)))
}

// Test logic around memo gas consumption.
Expand Down Expand Up @@ -416,17 +418,17 @@ func TestAnteHandlerMultiSigner(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
acc3.SetCoins(types.NewTestCoins())
require.NoError(t, acc3.SetAccountNumber(2))
app.AccountKeeper.SetAccount(ctx, acc3)
app.BankKeeper.SetBalances(ctx, addr3, types.NewTestCoins())

// set up msgs and fee
var tx sdk.Tx
Expand Down Expand Up @@ -465,13 +467,13 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())

var tx sdk.Tx
msg := types.NewTestMsg(addr1)
Expand Down Expand Up @@ -542,13 +544,13 @@ func TestAnteHandlerSetPubKey(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(types.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
app.BankKeeper.SetBalances(ctx, addr2, types.NewTestCoins())

var tx sdk.Tx

Expand Down Expand Up @@ -669,9 +671,9 @@ func TestAnteHandlerSigLimitExceeded(t *testing.T) {
// set the accounts
for i, addr := range addrs {
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc.SetCoins(types.NewTestCoins())
acc.SetAccountNumber(uint64(i))
app.AccountKeeper.SetAccount(ctx, acc)
app.BankKeeper.SetBalances(ctx, addr, types.NewTestCoins())
}

var tx sdk.Tx
Expand Down Expand Up @@ -705,8 +707,8 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) {
// verify that an secp256k1 account gets rejected
priv1, _, addr1 := types.KeyTestPubAddr()
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
_ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))

var tx sdk.Tx
msg := types.NewTestMsg(addr1)
Expand All @@ -721,7 +723,8 @@ func TestCustomSignatureVerificationGasConsumer(t *testing.T) {
pub2 := priv2.PubKey()
addr2 := sdk.AccAddress(pub2.Address())
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))))

require.NoError(t, app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("atom", 150))))
require.NoError(t, acc2.SetAccountNumber(1))
app.AccountKeeper.SetAccount(ctx, acc2)
msg = types.NewTestMsg(addr2)
Expand All @@ -745,9 +748,9 @@ func TestAnteHandlerReCheck(t *testing.T) {

// set the accounts
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc1.SetCoins(types.NewTestCoins())
require.NoError(t, acc1.SetAccountNumber(0))
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, types.NewTestCoins())

antehandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer)

Expand Down Expand Up @@ -806,8 +809,8 @@ func TestAnteHandlerReCheck(t *testing.T) {
ctx = ctx.WithMinGasPrices(sdk.DecCoins{})

// remove funds for account so antehandler fails on recheck
acc1.SetCoins(sdk.Coins{})
app.AccountKeeper.SetAccount(ctx, acc1)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins())

_, err = antehandler(ctx, tx, false)
require.NotNil(t, err, "antehandler on recheck did not fail once feePayer no longer has sufficient funds")
Expand Down
4 changes: 2 additions & 2 deletions x/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func TestDeductFees(t *testing.T) {

// Set account with insufficient funds
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))})
app.AccountKeeper.SetAccount(ctx, acc)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))))

dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.SupplyKeeper)
antehandler := sdk.ChainAnteDecorators(dfd)
Expand All @@ -89,8 +89,8 @@ func TestDeductFees(t *testing.T) {
require.NotNil(t, err, "Tx did not error when fee payer had insufficient funds")

// Set account with sufficient funds
acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(200))})
app.AccountKeeper.SetAccount(ctx, acc)
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))

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

Expand Down
2 changes: 1 addition & 1 deletion x/auth/exported/exported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestGenesisAccountsContains(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
acc := authtypes.NewBaseAccount(addr, nil, secp256k1.GenPrivKey().PubKey(), 0, 0)
acc := authtypes.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0)

genAccounts := exported.GenesisAccounts{}
require.False(t, genAccounts.Contains(acc.GetAddress()))
Expand Down
52 changes: 0 additions & 52 deletions x/auth/keeper/keeper_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,6 @@ func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
}
}

func BenchmarkAccountMapperGetAccountFoundWithCoins(b *testing.B) {
app, ctx := createTestApp(false)

coins := sdk.Coins{
sdk.NewCoin("ltc", sdk.NewInt(1000)),
sdk.NewCoin("btc", sdk.NewInt(1000)),
sdk.NewCoin("eth", sdk.NewInt(1000)),
sdk.NewCoin("xrp", sdk.NewInt(1000)),
sdk.NewCoin("bch", sdk.NewInt(1000)),
sdk.NewCoin("eos", sdk.NewInt(1000)),
}

// 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 := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc.SetCoins(coins)
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)}
app.AccountKeeper.GetAccount(ctx, sdk.AccAddress(arr))
}
}

func BenchmarkAccountMapperSetAccount(b *testing.B) {
app, ctx := createTestApp(false)

Expand All @@ -65,27 +37,3 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) {
app.AccountKeeper.SetAccount(ctx, acc)
}
}

func BenchmarkAccountMapperSetAccountWithCoins(b *testing.B) {
app, ctx := createTestApp(false)

coins := sdk.Coins{
sdk.NewCoin("ltc", sdk.NewInt(1000)),
sdk.NewCoin("btc", sdk.NewInt(1000)),
sdk.NewCoin("eth", sdk.NewInt(1000)),
sdk.NewCoin("xrp", sdk.NewInt(1000)),
sdk.NewCoin("bch", sdk.NewInt(1000)),
sdk.NewCoin("eos", sdk.NewInt(1000)),
}

b.ResetTimer()

// 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 := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc.SetCoins(coins)
app.AccountKeeper.SetAccount(ctx, acc)
}
}
4 changes: 2 additions & 2 deletions x/auth/legacy/v0_38/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func Migrate(authGenState v036auth.GenesisState, genAccountsGenState v036genacco
accounts[i] = genAccount
}

accounts = sanitizeGenesisAccounts(accounts)
accounts = SanitizeGenesisAccounts(accounts)

if err := validateGenAccounts(accounts); err != nil {
if err := ValidateGenAccounts(accounts); err != nil {
panic(err)
}

Expand Down
Loading

0 comments on commit 4290946

Please sign in to comment.