From 34032047d96c58aee99baf24fad84b14fb170bc8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 20 Jan 2020 18:55:30 +0100 Subject: [PATCH] Start writing full-app level integration test --- app/app.go | 30 ++++----- app/test_common.go | 4 +- x/wasm/internal/keeper/mask_test.go | 96 +++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 x/wasm/internal/keeper/mask_test.go diff --git a/app/app.go b/app/app.go index 8c659e4..3659ede 100644 --- a/app/app.go +++ b/app/app.go @@ -103,7 +103,7 @@ type WasmApp struct { tKeys map[string]*sdk.TransientStoreKey // keepers - accountKeeper auth.AccountKeeper + AccountKeeper auth.AccountKeeper bankKeeper bank.Keeper supplyKeeper supply.Keeper stakingKeeper staking.Keeper @@ -169,9 +169,9 @@ func NewWasmApp( evidenceSubspace := app.paramsKeeper.Subspace(evidence.DefaultParamspace) // add keepers - app.accountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount) - app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper, bankSubspace, bank.DefaultCodespace, app.ModuleAccountAddrs()) - app.supplyKeeper = supply.NewKeeper(app.cdc, keys[supply.StoreKey], app.accountKeeper, app.bankKeeper, maccPerms) + app.AccountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount) + app.bankKeeper = bank.NewBaseKeeper(app.AccountKeeper, bankSubspace, bank.DefaultCodespace, app.ModuleAccountAddrs()) + app.supplyKeeper = supply.NewKeeper(app.cdc, keys[supply.StoreKey], app.AccountKeeper, app.bankKeeper, maccPerms) stakingKeeper := staking.NewKeeper( app.cdc, keys[staking.StoreKey], app.supplyKeeper, stakingSubspace, staking.DefaultCodespace, ) @@ -197,7 +197,7 @@ func NewWasmApp( wasmConfig := wasmWrap.Wasm fmt.Printf("WasmConfig: %#v\n", wasmConfig) - app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, wasmRouter, wasmDir, wasmConfig) + app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.AccountKeeper, app.bankKeeper, wasmRouter, wasmDir, wasmConfig) // create evidence keeper with evidence router app.evidenceKeeper = evidence.NewKeeper( @@ -226,16 +226,16 @@ func NewWasmApp( // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. app.mm = module.NewManager( - genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx), - auth.NewAppModule(app.accountKeeper), - bank.NewAppModule(app.bankKeeper, app.accountKeeper), + genutil.NewAppModule(app.AccountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx), + auth.NewAppModule(app.AccountKeeper), + bank.NewAppModule(app.bankKeeper, app.AccountKeeper), crisis.NewAppModule(&app.crisisKeeper), - supply.NewAppModule(app.supplyKeeper, app.accountKeeper), + supply.NewAppModule(app.supplyKeeper, app.AccountKeeper), distr.NewAppModule(app.distrKeeper, app.supplyKeeper), gov.NewAppModule(app.govKeeper, app.supplyKeeper), mint.NewAppModule(app.mintKeeper), slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), - staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), + staking.NewAppModule(app.stakingKeeper, app.AccountKeeper, app.supplyKeeper), evidence.NewAppModule(*app.evidenceKeeper), wasm.NewAppModule(app.wasmKeeper), ) @@ -263,13 +263,13 @@ func NewWasmApp( // NOTE: This is not required for apps that don't use the simulator for fuzz testing // transactions. app.sm = module.NewSimulationManager( - auth.NewAppModule(app.accountKeeper), - bank.NewAppModule(app.bankKeeper, app.accountKeeper), - supply.NewAppModule(app.supplyKeeper, app.accountKeeper), + auth.NewAppModule(app.AccountKeeper), + bank.NewAppModule(app.bankKeeper, app.AccountKeeper), + supply.NewAppModule(app.supplyKeeper, app.AccountKeeper), gov.NewAppModule(app.govKeeper, app.supplyKeeper), mint.NewAppModule(app.mintKeeper), distr.NewAppModule(app.distrKeeper, app.supplyKeeper), - staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), + staking.NewAppModule(app.stakingKeeper, app.AccountKeeper, app.supplyKeeper), slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), ) @@ -282,7 +282,7 @@ func NewWasmApp( // initialize BaseApp app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) - app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer)) + app.SetAnteHandler(auth.NewAnteHandler(app.AccountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer)) app.SetEndBlocker(app.EndBlocker) if loadLatest { diff --git a/app/test_common.go b/app/test_common.go index a05e15e..5ad67d8 100644 --- a/app/test_common.go +++ b/app/test_common.go @@ -5,6 +5,7 @@ This file is full of test helper functions, taken from simapp **/ import ( + "fmt" "os" "testing" @@ -69,6 +70,7 @@ func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount) *WasmApp { if err != nil { panic(err) } + fmt.Println(string(stateBytes)) // Initialize the chain app.InitChain( @@ -98,7 +100,7 @@ func SignAndDeliver( msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, DefaultGenTxGas, - "", + SimAppChainID, accNums, seq, priv..., diff --git a/x/wasm/internal/keeper/mask_test.go b/x/wasm/internal/keeper/mask_test.go new file mode 100644 index 0000000..320e568 --- /dev/null +++ b/x/wasm/internal/keeper/mask_test.go @@ -0,0 +1,96 @@ +package keeper_test + +import ( + "encoding/binary" + "testing" + + "github.com/cosmwasm/wasmd/app" + "github.com/stretchr/testify/require" + + 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/bank" + + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto" + // "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" +) + +// CreateTestApp will create a new wasmd application and provide money to every address +// listed there +func CreateTestApp(t *testing.T, accounts []*auth.BaseAccount) *app.WasmApp { + genAccounts := make([]authExported.GenesisAccount, len(accounts)) + for i, acct := range accounts { + genAccounts[i] = acct + } + wasmd := app.SetupWithGenesisAccounts(genAccounts) + return wasmd +} + +func TestSendWithApp(t *testing.T) { + // TODO: how to change default coin? + coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 123456)) + accts, keys := genAccountsWithKey(t, coin, 2) + wasm := CreateTestApp(t, accts) + + // TODO: check account balance first + msg := bank.MsgSend{ + FromAddress: accts[0].Address, + ToAddress: accts[1].Address, + Amount: sdk.NewCoins(sdk.NewInt64Coin("stake", 20)), + } + _ = sign(t, wasm, 2, msg, &keys[0], true) +} + +func sign(t *testing.T, wasm *app.WasmApp, height int64, msg sdk.Msg, signer *signer, expectPass bool) sdk.Result { + header := abci.Header{ + Height: height, + ChainID: app.SimAppChainID, + } + signer.seq++ + res := app.SignAndDeliver(t, wasm, header, []sdk.Msg{msg}, []uint64{signer.acctNum}, []uint64{signer.seq}, expectPass, signer.priv) + return res +} + +type signer struct { + priv crypto.PrivKey + seq uint64 + acctNum uint64 +} + +func genAccountsWithKey(t *testing.T, coins sdk.Coins, n int) ([]*auth.BaseAccount, []signer) { + accts := make([]*auth.BaseAccount, n) + keys := make([]signer, n) + + for i := range accts { + priv, _, addr := maskKeyPubAddr() + baseAcct := auth.NewBaseAccountWithAddress(addr) + err := baseAcct.SetCoins(coins) + require.NoError(t, err) + accts[i] = &baseAcct + keys[i] = signer{ + priv: priv, + acctNum: baseAcct.GetAccountNumber(), + seq: baseAcct.GetSequence(), + } + } + + return accts, keys +} + +var maskKeyCounter uint64 = 0 + +// we need to make this deterministic (same every test run), as encoded address size and thus gas cost, +// depends on the actual bytes (due to ugly CanonicalAddress encoding) +func maskKeyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) { + maskKeyCounter++ + seed := make([]byte, 8) + binary.BigEndian.PutUint64(seed, maskKeyCounter) + + key := secp256k1.GenPrivKeySecp256k1(seed) + pub := key.PubKey() + addr := sdk.AccAddress(pub.Address()) + return key, pub, addr +}