2020-04-01 18:49:21 +00:00
|
|
|
package keeper_test
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-30 03:14:58 +00:00
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
2019-09-26 15:54:23 +00:00
|
|
|
"testing"
|
2020-04-01 18:49:21 +00:00
|
|
|
"time"
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2021-06-29 10:54:29 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-09-26 15:54:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-07-23 14:24:36 +00:00
|
|
|
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-07-23 14:24:36 +00:00
|
|
|
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
2021-06-22 10:14:40 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/app"
|
2021-06-29 10:54:29 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
|
|
|
"github.com/tharsis/ethermint/encoding"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/tharsis/ethermint/server/config"
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/tharsis/ethermint/tests"
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2021-06-29 17:02:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-09-26 15:54:23 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-06-29 10:54:29 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-08-23 21:41:54 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
)
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
var (
|
|
|
|
//go:embed ERC20Contract.json
|
|
|
|
compiledContractJSON []byte
|
|
|
|
ContractBin []byte
|
|
|
|
ContractABI abi.ABI
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var tmp struct {
|
|
|
|
Abi string
|
|
|
|
Bin string
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(compiledContractJSON, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ContractBin = common.FromHex(tmp.Bin)
|
|
|
|
err = json.Unmarshal([]byte(tmp.Abi), &ContractABI)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
var testTokens = sdk.NewIntWithDecimal(1000, 18)
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
queryClient types.QueryClient
|
|
|
|
address ethcmn.Address
|
2021-06-22 10:14:40 +00:00
|
|
|
consAddress sdk.ConsAddress
|
2021-06-29 10:54:29 +00:00
|
|
|
|
|
|
|
// for generate test tx
|
|
|
|
clientCtx client.Context
|
|
|
|
ethSigner ethtypes.Signer
|
2021-07-23 14:24:36 +00:00
|
|
|
|
|
|
|
appCodec codec.Codec
|
|
|
|
signer keyring.Signer
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
/// DoSetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`.
|
|
|
|
func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
|
2020-04-01 18:49:21 +00:00
|
|
|
checkTx := false
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
// account key
|
|
|
|
priv, err := ethsecp256k1.GenerateKey()
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.address = ethcmn.BytesToAddress(priv.PubKey().Address().Bytes())
|
|
|
|
suite.signer = tests.NewSigner(priv)
|
|
|
|
|
|
|
|
// consensus key
|
|
|
|
priv, err = ethsecp256k1.GenerateKey()
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.app = app.Setup(checkTx)
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
|
|
|
|
Height: 1,
|
2021-08-17 14:11:26 +00:00
|
|
|
ChainID: "ethermint_9000-1",
|
2021-07-23 14:24:36 +00:00
|
|
|
Time: time.Now().UTC(),
|
|
|
|
ProposerAddress: suite.consAddress.Bytes(),
|
|
|
|
})
|
2021-06-02 08:06:12 +00:00
|
|
|
suite.app.EvmKeeper.WithContext(suite.ctx)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
acc := ðermint.EthAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
|
2021-06-29 17:02:21 +00:00
|
|
|
CodeHash: common.BytesToHash(ethcrypto.Keccak256(nil)).String(),
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
2021-06-22 10:14:40 +00:00
|
|
|
|
|
|
|
valAddr := sdk.ValAddress(suite.address.Bytes())
|
|
|
|
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
|
2021-07-23 14:24:36 +00:00
|
|
|
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-22 10:14:40 +00:00
|
|
|
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
|
2021-06-29 10:54:29 +00:00
|
|
|
|
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
|
|
|
|
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.appCodec = encodingConfig.Marshaler
|
|
|
|
|
|
|
|
// mint some tokens to coinbase address
|
|
|
|
_, bankKeeper := suite.initKeepersWithmAccPerms()
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
initCoin := sdk.NewCoins(sdk.NewCoin(suite.EvmDenom(), testTokens))
|
2021-07-23 14:24:36 +00:00
|
|
|
err = simapp.FundAccount(bankKeeper, suite.ctx, acc.GetAddress(), initCoin)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
|
|
|
suite.DoSetupTest(suite.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) EvmDenom() string {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
rsp, _ := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
|
|
|
|
return rsp.Params.EvmDenom
|
2021-07-23 14:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit and begin new block
|
|
|
|
func (suite *KeeperTestSuite) Commit() {
|
|
|
|
suite.app.Commit()
|
|
|
|
header := suite.ctx.BlockHeader()
|
|
|
|
header.Height += 1
|
|
|
|
suite.app.BeginBlock(abci.RequestBeginBlock{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
|
|
|
|
// update ctx
|
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(false, header)
|
|
|
|
suite.app.EvmKeeper.WithContext(suite.ctx)
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initKeepersWithmAccPerms construct a bank keeper that can mint tokens out of thin air
|
|
|
|
func (suite *KeeperTestSuite) initKeepersWithmAccPerms() (authkeeper.AccountKeeper, bankkeeper.BaseKeeper) {
|
|
|
|
maccPerms := app.GetMaccPerms()
|
|
|
|
|
|
|
|
maccPerms[authtypes.Burner] = []string{authtypes.Burner}
|
|
|
|
maccPerms[authtypes.Minter] = []string{authtypes.Minter}
|
|
|
|
authKeeper := authkeeper.NewAccountKeeper(
|
|
|
|
suite.appCodec, suite.app.GetKey(types.StoreKey), suite.app.GetSubspace(types.ModuleName),
|
|
|
|
authtypes.ProtoBaseAccount, maccPerms,
|
|
|
|
)
|
|
|
|
keeper := bankkeeper.NewBaseKeeper(
|
|
|
|
suite.appCodec, suite.app.GetKey(types.StoreKey), authKeeper,
|
|
|
|
suite.app.GetSubspace(types.ModuleName), map[string]bool{},
|
|
|
|
)
|
|
|
|
|
|
|
|
return authKeeper, keeper
|
2019-09-26 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
// DeployTestContract deploy a test erc20 contract and returns the contract address
|
|
|
|
func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner common.Address, supply *big.Int) common.Address {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
chainID := suite.app.EvmKeeper.ChainID()
|
|
|
|
|
|
|
|
ctorArgs, err := ContractABI.Pack("", owner, supply)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data := append(ContractBin, ctorArgs...)
|
|
|
|
args, err := json.Marshal(&types.CallArgs{
|
|
|
|
From: &suite.address,
|
|
|
|
Data: (*hexutil.Bytes)(&data),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
|
|
|
|
Args: args,
|
|
|
|
GasCap: uint64(config.DefaultGasCap),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
|
|
|
erc20DeployTx := types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
data, // input
|
|
|
|
nil, // accesses
|
|
|
|
)
|
|
|
|
erc20DeployTx.From = suite.address.Hex()
|
|
|
|
err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, erc20DeployTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rsp.VmError)
|
|
|
|
return crypto.CreateAddress(suite.address, nonce)
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|