2021-11-17 13:32:05 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-05-31 16:28:46 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2021-11-17 13:32:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2022-05-31 16:28:46 +00:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2021-11-17 13:32:05 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/app"
|
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
"github.com/evmos/ethermint/encoding"
|
|
|
|
"github.com/evmos/ethermint/tests"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
"github.com/evmos/ethermint/x/feemarket/types"
|
2021-11-17 13:32:05 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
2022-05-31 16:28:46 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-11-17 13:32:05 +00:00
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
|
|
|
"github.com/tendermint/tendermint/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
queryClient types.QueryClient
|
|
|
|
address common.Address
|
|
|
|
consAddress sdk.ConsAddress
|
|
|
|
|
|
|
|
// for generate test tx
|
|
|
|
clientCtx client.Context
|
|
|
|
ethSigner ethtypes.Signer
|
|
|
|
|
|
|
|
appCodec codec.Codec
|
|
|
|
signer keyring.Signer
|
2022-05-31 16:28:46 +00:00
|
|
|
denom string
|
|
|
|
}
|
|
|
|
|
|
|
|
var s *KeeperTestSuite
|
|
|
|
|
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
s = new(KeeperTestSuite)
|
|
|
|
suite.Run(t, s)
|
|
|
|
|
|
|
|
// Run Ginkgo integration tests
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "Keeper Suite")
|
2021-11-17 13:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 10:30:55 +00:00
|
|
|
// SetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`.
|
2022-05-31 16:28:46 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2021-11-17 13:32:05 +00:00
|
|
|
checkTx := false
|
2022-05-31 16:28:46 +00:00
|
|
|
suite.app = app.Setup(checkTx, nil)
|
|
|
|
suite.SetupApp(checkTx)
|
|
|
|
}
|
2021-11-17 13:32:05 +00:00
|
|
|
|
2022-05-31 16:28:46 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupApp(checkTx bool) {
|
|
|
|
t := suite.T()
|
2021-11-17 13:32:05 +00:00
|
|
|
// account key
|
|
|
|
priv, err := ethsecp256k1.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
suite.address = common.BytesToAddress(priv.PubKey().Address().Bytes())
|
|
|
|
suite.signer = tests.NewSigner(priv)
|
|
|
|
|
|
|
|
// consensus key
|
|
|
|
priv, err = ethsecp256k1.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
|
|
|
|
|
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
|
|
|
|
Height: 1,
|
|
|
|
ChainID: "ethermint_9000-1",
|
|
|
|
Time: time.Now().UTC(),
|
|
|
|
ProposerAddress: suite.consAddress.Bytes(),
|
|
|
|
Version: tmversion.Consensus{
|
|
|
|
Block: version.BlockProtocol,
|
|
|
|
},
|
|
|
|
LastBlockId: tmproto.BlockID{
|
|
|
|
Hash: tmhash.Sum([]byte("block_id")),
|
|
|
|
PartSetHeader: tmproto.PartSetHeader{
|
|
|
|
Total: 11,
|
|
|
|
Hash: tmhash.Sum([]byte("partset_header")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AppHash: tmhash.Sum([]byte("app")),
|
|
|
|
DataHash: tmhash.Sum([]byte("data")),
|
|
|
|
EvidenceHash: tmhash.Sum([]byte("evidence")),
|
|
|
|
ValidatorsHash: tmhash.Sum([]byte("validators")),
|
|
|
|
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
|
|
|
|
ConsensusHash: tmhash.Sum([]byte("consensus")),
|
|
|
|
LastResultsHash: tmhash.Sum([]byte("last_result")),
|
|
|
|
})
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.FeeMarketKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
|
|
|
|
acc := ðermint.EthAccount{
|
|
|
|
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
|
|
|
|
CodeHash: common.BytesToHash(crypto.Keccak256(nil)).String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
|
|
|
valAddr := sdk.ValAddress(suite.address.Bytes())
|
|
|
|
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
|
|
|
|
require.NoError(t, err)
|
2022-05-31 16:28:46 +00:00
|
|
|
validator = stakingkeeper.TestingUpdateValidator(suite.app.StakingKeeper, suite.ctx, validator, true)
|
2022-07-28 13:43:49 +00:00
|
|
|
err = suite.app.StakingKeeper.AfterValidatorCreated(suite.ctx, validator.GetOperator())
|
|
|
|
require.NoError(t, err)
|
2022-05-31 16:28:46 +00:00
|
|
|
|
2021-11-17 13:32:05 +00:00
|
|
|
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
|
|
|
|
require.NoError(t, err)
|
|
|
|
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
|
|
|
|
|
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
|
|
|
|
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
2022-07-28 13:43:49 +00:00
|
|
|
suite.appCodec = encodingConfig.Codec
|
2022-05-31 16:28:46 +00:00
|
|
|
suite.denom = evmtypes.DefaultEVMDenom
|
2021-11-17 13:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 16:28:46 +00:00
|
|
|
// Commit commits and starts a new block with an updated context.
|
|
|
|
func (suite *KeeperTestSuite) Commit() {
|
|
|
|
suite.CommitAfter(time.Second * 0)
|
2021-11-17 13:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 16:28:46 +00:00
|
|
|
// Commit commits a block at a given time.
|
|
|
|
func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
|
|
|
|
header := suite.ctx.BlockHeader()
|
|
|
|
suite.app.EndBlock(abci.RequestEndBlock{Height: header.Height})
|
|
|
|
_ = suite.app.Commit()
|
|
|
|
|
|
|
|
header.Height += 1
|
|
|
|
header.Time = header.Time.Add(t)
|
|
|
|
suite.app.BeginBlock(abci.RequestBeginBlock{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
|
|
|
|
// update ctx
|
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(false, header)
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.FeeMarketKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
2021-11-17 13:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 09:22:33 +00:00
|
|
|
func (suite *KeeperTestSuite) TestSetGetBlockGasWanted() {
|
2021-11-17 13:32:05 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
|
|
|
expGas uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"with last block given",
|
|
|
|
func() {
|
2022-06-05 09:22:33 +00:00
|
|
|
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, uint64(1000000))
|
2021-11-17 13:32:05 +00:00
|
|
|
},
|
|
|
|
uint64(1000000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc.malleate()
|
|
|
|
|
2022-06-05 09:22:33 +00:00
|
|
|
gas := suite.app.FeeMarketKeeper.GetBlockGasWanted(suite.ctx)
|
2021-11-17 13:32:05 +00:00
|
|
|
suite.Require().Equal(tc.expGas, gas, tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestSetGetGasFee() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
|
|
|
expFee *big.Int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"with last block given",
|
|
|
|
func() {
|
|
|
|
suite.app.FeeMarketKeeper.SetBaseFee(suite.ctx, sdk.OneDec().BigInt())
|
|
|
|
},
|
|
|
|
sdk.OneDec().BigInt(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
fee := suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx)
|
|
|
|
suite.Require().Equal(tc.expFee, fee, tc.name)
|
|
|
|
}
|
|
|
|
}
|