laconicd/x/evm/genesis_test.go

124 lines
2.6 KiB
Go
Raw Normal View History

package evm_test
import (
"math/big"
2021-04-18 15:54:18 +00:00
"github.com/ethereum/go-ethereum/common"
2021-04-18 15:54:18 +00:00
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"github.com/tharsis/ethermint/x/evm"
"github.com/tharsis/ethermint/x/evm/types"
)
func (suite *EvmTestSuite) TestInitGenesis() {
privkey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
2021-04-18 15:54:18 +00:00
address := common.HexToAddress(privkey.PubKey().Address().String())
testCases := []struct {
name string
malleate func()
2021-04-17 10:00:07 +00:00
genState *types.GenesisState
expPanic bool
}{
{
"default",
func() {},
types.DefaultGenesisState(),
false,
},
{
"valid account",
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
suite.Require().NotNil(acc)
2021-04-17 10:00:07 +00:00
suite.app.EvmKeeper.AddBalance(address, big.NewInt(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
2021-04-17 10:00:07 +00:00
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Storage: types.Storage{
{Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()},
},
},
},
},
false,
},
{
"account not found",
func() {},
2021-04-17 10:00:07 +00:00
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
},
},
},
true,
},
{
"invalid account type",
func() {
acc := authtypes.NewBaseAccountWithAddress(address.Bytes())
2021-04-17 10:00:07 +00:00
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
2021-04-17 10:00:07 +00:00
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
},
},
},
true,
},
{
"invalid code hash",
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "ffffffff",
},
},
},
true,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset values
tc.malleate()
if tc.expPanic {
suite.Require().Panics(
func() {
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
},
)
} else {
suite.Require().NotPanics(
func() {
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
},
)
}
})
}
}