211 lines
6.8 KiB
Go
211 lines
6.8 KiB
Go
package integration
|
|
|
|
/**
|
|
This file is full of test helper functions, taken from simapp
|
|
**/
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
wasmd "github.com/CosmWasm/wasmd/app"
|
|
"github.com/CosmWasm/wasmd/x/wasm"
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
"github.com/stretchr/testify/require"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"github.com/tendermint/tendermint/crypto"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
dbm "github.com/tendermint/tm-db"
|
|
)
|
|
|
|
// Setup initializes a new wasmd.WasmApp. A Nop logger is set in WasmApp.
|
|
func Setup(isCheckTx bool, homeDir string) *wasmd.WasmApp {
|
|
db := dbm.NewMemDB()
|
|
app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, homeDir, 5, wasm.EnableAllProposals, wasmd.EmptyAppOptions{})
|
|
// app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 0)
|
|
if !isCheckTx {
|
|
// init chain must be called to stop deliverState from being nil
|
|
genesisState := wasmd.NewDefaultGenesisState()
|
|
stateBytes, err := json.Marshal(genesisState)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Initialize the chain
|
|
app.InitChain(
|
|
abci.RequestInitChain{
|
|
Validators: []abci.ValidatorUpdate{},
|
|
AppStateBytes: stateBytes,
|
|
},
|
|
)
|
|
}
|
|
|
|
return app
|
|
}
|
|
|
|
type GenesisState map[string]json.RawMessage
|
|
|
|
// NewDefaultGenesisState generates the default state for the application.
|
|
func NewDefaultGenesisState() GenesisState {
|
|
encCfg := wasmd.MakeEncodingConfig()
|
|
return wasmd.ModuleBasics.DefaultGenesis(encCfg.Marshaler)
|
|
}
|
|
|
|
func SetupWithGenesisValSet(t *testing.T, homeDir string, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *wasmd.WasmApp {
|
|
db := dbm.NewMemDB()
|
|
app := wasmd.NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, homeDir, 5, wasm.EnableAllProposals, wasmd.EmptyAppOptions{})
|
|
genesisState := NewDefaultGenesisState()
|
|
|
|
// set genesis accounts
|
|
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
|
|
appCodec := wasmd.NewTestSupport(t, app).AppCodec()
|
|
genesisState[authtypes.ModuleName] = appCodec.MustMarshalJSON(authGenesis)
|
|
|
|
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
|
|
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
|
|
|
|
bondAmt := sdk.NewInt(1000000)
|
|
|
|
for _, val := range valSet.Validators {
|
|
// Currently validator requires tmcrypto.ed25519 keys, which don't support
|
|
// our Marshaling interfaces, so we need to pack them into our version of ed25519.
|
|
// There is ongoing work to add secp256k1 keys (https://github.com/cosmos/cosmos-sdk/pull/7604).
|
|
pk, err := ed25519.FromTmEd25519(val.PubKey)
|
|
require.NoError(t, err)
|
|
pkAny, err := codectypes.PackAny(pk)
|
|
require.NoError(t, err)
|
|
validator := stakingtypes.Validator{
|
|
OperatorAddress: sdk.ValAddress(val.Address).String(),
|
|
ConsensusPubkey: pkAny,
|
|
Jailed: false,
|
|
Status: stakingtypes.Bonded,
|
|
Tokens: bondAmt,
|
|
DelegatorShares: sdk.OneDec(),
|
|
Description: stakingtypes.Description{},
|
|
UnbondingHeight: int64(0),
|
|
UnbondingTime: time.Unix(0, 0).UTC(),
|
|
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
|
|
MinSelfDelegation: sdk.ZeroInt(),
|
|
}
|
|
validators = append(validators, validator)
|
|
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
|
|
|
|
}
|
|
|
|
// set validators and delegations
|
|
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
|
|
genesisState[stakingtypes.ModuleName] = appCodec.MustMarshalJSON(stakingGenesis)
|
|
|
|
totalSupply := sdk.NewCoins()
|
|
for _, b := range balances {
|
|
// add genesis acc tokens and delegated tokens to total supply
|
|
totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...)
|
|
}
|
|
|
|
// update total supply
|
|
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
|
|
genesisState[banktypes.ModuleName] = appCodec.MustMarshalJSON(bankGenesis)
|
|
|
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
|
require.NoError(t, err)
|
|
|
|
// init chain will set the validator set and initialize the genesis accounts
|
|
app.InitChain(
|
|
abci.RequestInitChain{
|
|
Validators: []abci.ValidatorUpdate{},
|
|
ConsensusParams: DefaultConsensusParams,
|
|
AppStateBytes: stateBytes,
|
|
},
|
|
)
|
|
|
|
// commit genesis changes
|
|
app.Commit()
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
|
|
Height: app.LastBlockHeight() + 1,
|
|
AppHash: app.LastCommitID().Hash,
|
|
ValidatorsHash: valSet.Hash(),
|
|
NextValidatorsHash: valSet.Hash(),
|
|
}})
|
|
|
|
return app
|
|
}
|
|
|
|
var DefaultConsensusParams = &abci.ConsensusParams{
|
|
Block: &abci.BlockParams{
|
|
MaxBytes: 200000,
|
|
MaxGas: 2000000,
|
|
},
|
|
Evidence: &tmproto.EvidenceParams{
|
|
MaxAgeNumBlocks: 302400,
|
|
MaxAgeDuration: 1814400,
|
|
},
|
|
Validator: &tmproto.ValidatorParams{
|
|
PubKeyTypes: []string{
|
|
tmtypes.ABCIPubKeyTypeEd25519,
|
|
},
|
|
},
|
|
}
|
|
|
|
// SignCheckDeliver checks a generated signed transaction and simulates a
|
|
// block commitment with the given transaction. A test assertion is made using
|
|
// the parameter 'expPass' against the result. A corresponding result is
|
|
// returned.
|
|
func SignCheckDeliver(
|
|
t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg,
|
|
chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...crypto.PrivKey,
|
|
) (sdk.GasInfo, *sdk.Result, error) {
|
|
|
|
tx, err := helpers.GenTx(
|
|
txCfg,
|
|
msgs,
|
|
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
|
|
helpers.DefaultGenTxGas,
|
|
chainID,
|
|
accNums,
|
|
accSeqs,
|
|
priv...,
|
|
)
|
|
require.NoError(t, err)
|
|
txBytes, err := txCfg.TxEncoder()(tx)
|
|
require.Nil(t, err)
|
|
|
|
// Must simulate now as CheckTx doesn't run Msgs anymore
|
|
_, res, err := app.Simulate(txBytes)
|
|
|
|
if expSimPass {
|
|
require.NoError(t, err)
|
|
require.NotNil(t, res)
|
|
} else {
|
|
require.Error(t, err)
|
|
require.Nil(t, res)
|
|
}
|
|
|
|
// Simulate a sending a transaction and committing a block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
|
gInfo, res, err := app.Deliver(txCfg.TxEncoder(), tx)
|
|
|
|
if expPass {
|
|
require.NoError(t, err)
|
|
require.NotNil(t, res)
|
|
} else {
|
|
require.Error(t, err)
|
|
require.Nil(t, res)
|
|
}
|
|
|
|
app.EndBlock(abci.RequestEndBlock{})
|
|
app.Commit()
|
|
|
|
return gInfo, res, err
|
|
}
|