laconicd-deprecated/app/test_utils.go

135 lines
3.6 KiB
Go
Raw Normal View History

2018-11-28 22:19:22 +00:00
package app
import (
"fmt"
"math/big"
"time"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
2018-11-28 22:19:22 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/params"
2018-11-28 22:19:22 +00:00
"github.com/cosmos/ethermint/crypto"
emint "github.com/cosmos/ethermint/types"
2018-11-28 22:19:22 +00:00
evmtypes "github.com/cosmos/ethermint/x/evm/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
2018-11-28 22:19:22 +00:00
abci "github.com/tendermint/tendermint/abci/types"
tmcrypto "github.com/tendermint/tendermint/crypto"
dbm "github.com/tendermint/tendermint/libs/db"
2018-11-28 22:19:22 +00:00
"github.com/tendermint/tendermint/libs/log"
)
type testSetup struct {
ctx sdk.Context
cdc *codec.Codec
accKeeper auth.AccountKeeper
supplyKeeper types.SupplyKeeper
anteHandler sdk.AnteHandler
2018-11-28 22:19:22 +00:00
}
func newTestSetup() testSetup {
db := dbm.NewMemDB()
authCapKey := sdk.NewKVStoreKey("authCapKey")
keySupply := sdk.NewKVStoreKey("keySupply")
keyParams := sdk.NewKVStoreKey("params")
tkeyParams := sdk.NewTransientStoreKey("transient_params")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(authCapKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeIAVL, db)
// nolint:errcheck
ms.LoadLatestVersion()
cdc := MakeCodec()
cdc.RegisterConcrete(&sdk.TestMsg{}, "test/TestMsg", nil)
// Set params keeper and subspaces
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace)
authSubspace := paramsKeeper.Subspace(auth.DefaultParamspace)
// Add keepers
accKeeper := auth.NewAccountKeeper(cdc, authCapKey, authSubspace, auth.ProtoBaseAccount)
supplyKeeper := auth.NewDummySupplyKeeper(accKeeper)
anteHandler := NewAnteHandler(accKeeper, supplyKeeper)
2018-11-28 22:19:22 +00:00
ctx := sdk.NewContext(
ms,
abci.Header{ChainID: "3", Time: time.Now().UTC()},
true,
2018-11-28 22:19:22 +00:00
log.NewNopLogger(),
)
return testSetup{
ctx: ctx,
cdc: cdc,
accKeeper: accKeeper,
supplyKeeper: supplyKeeper,
anteHandler: anteHandler,
2018-11-28 22:19:22 +00:00
}
}
func newTestMsg(addrs ...sdk.AccAddress) *sdk.TestMsg {
return sdk.NewTestMsg(addrs...)
}
func newTestCoins() sdk.Coins {
return sdk.Coins{sdk.NewInt64Coin(emint.DenomDefault, 500000000)}
2018-11-28 22:19:22 +00:00
}
func newTestStdFee() auth.StdFee {
return auth.NewStdFee(220000, sdk.NewCoins(sdk.NewInt64Coin(emint.DenomDefault, 150)))
2018-11-28 22:19:22 +00:00
}
// GenerateAddress generates an Ethereum address.
func newTestAddrKey() (sdk.AccAddress, tmcrypto.PrivKey) {
privkey, _ := crypto.GenerateKey()
addr := ethcrypto.PubkeyToAddress(privkey.PublicKey)
return sdk.AccAddress(addr.Bytes()), privkey
2018-11-28 22:19:22 +00:00
}
func newTestSDKTx(
ctx sdk.Context, msgs []sdk.Msg, privs []tmcrypto.PrivKey,
accNums []uint64, seqs []uint64, fee auth.StdFee,
2018-11-28 22:19:22 +00:00
) sdk.Tx {
sigs := make([]auth.StdSignature, len(privs))
for i, priv := range privs {
signBytes := auth.StdSignBytes(ctx.ChainID(), accNums[i], seqs[i], fee, msgs, "")
2018-11-28 22:19:22 +00:00
sig, err := priv.Sign(signBytes)
if err != nil {
panic(err)
}
sigs[i] = auth.StdSignature{
PubKey: priv.PubKey(),
Signature: sig,
2018-11-28 22:19:22 +00:00
}
}
return auth.NewStdTx(msgs, fee, sigs, "")
}
func newTestEthTx(ctx sdk.Context, msg *evmtypes.EthereumTxMsg, priv tmcrypto.PrivKey) sdk.Tx {
2018-11-28 22:19:22 +00:00
chainID, ok := new(big.Int).SetString(ctx.ChainID(), 10)
if !ok {
panic(fmt.Sprintf("invalid chainID: %s", ctx.ChainID()))
}
privkey, ok := priv.(crypto.PrivKeySecp256k1)
if !ok {
panic(fmt.Sprintf("invalid private key type: %T", priv))
2018-11-28 22:19:22 +00:00
}
msg.Sign(chainID, privkey.ToECDSA())
return msg
2018-11-28 22:19:22 +00:00
}