2020-05-18 19:21:12 +00:00
|
|
|
package evm_test
|
|
|
|
|
|
|
|
import (
|
2021-06-29 17:02:21 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/crypto/ethsecp256k1"
|
2023-01-04 11:04:38 +00:00
|
|
|
etherminttypes "github.com/cerc-io/laconicd/types"
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/x/evm"
|
|
|
|
"github.com/cerc-io/laconicd/x/evm/statedb"
|
|
|
|
"github.com/cerc-io/laconicd/x/evm/types"
|
2021-04-18 15:54:18 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
)
|
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
func (suite *EvmTestSuite) TestInitGenesis() {
|
|
|
|
privkey, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err)
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
address := common.HexToAddress(privkey.PubKey().Address().String())
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
var vmdb *statedb.StateDB
|
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
2021-04-17 10:00:07 +00:00
|
|
|
genState *types.GenesisState
|
2020-11-27 18:42:04 +00:00
|
|
|
expPanic bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
func() {},
|
|
|
|
types.DefaultGenesisState(),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid account",
|
|
|
|
func() {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddBalance(address, big.NewInt(1))
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
2021-04-17 10:00:07 +00:00
|
|
|
&types.GenesisState{
|
2020-11-27 18:42:04 +00:00
|
|
|
Params: types.DefaultParams(),
|
|
|
|
Accounts: []types.GenesisAccount{
|
|
|
|
{
|
|
|
|
Address: address.String(),
|
|
|
|
Storage: types.Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()},
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"account not found",
|
|
|
|
func() {},
|
2021-04-17 10:00:07 +00:00
|
|
|
&types.GenesisState{
|
2020-11-27 18:42:04 +00:00
|
|
|
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)
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
2021-04-17 10:00:07 +00:00
|
|
|
&types.GenesisState{
|
2020-11-27 18:42:04 +00:00
|
|
|
Params: types.DefaultParams(),
|
|
|
|
Accounts: []types.GenesisAccount{
|
|
|
|
{
|
|
|
|
Address: address.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2022-01-04 14:21:32 +00:00
|
|
|
{
|
|
|
|
"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,
|
|
|
|
},
|
2022-10-19 12:29:24 +00:00
|
|
|
{
|
|
|
|
"ignore empty account code checking",
|
|
|
|
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: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ignore empty account code checking with non-empty codehash",
|
|
|
|
func() {
|
|
|
|
ethAcc := ðerminttypes.EthAccount{
|
|
|
|
BaseAccount: authtypes.NewBaseAccount(address.Bytes(), nil, 0, 0),
|
|
|
|
CodeHash: common.BytesToHash([]byte{1, 2, 3}).Hex(),
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, ethAcc)
|
|
|
|
},
|
|
|
|
&types.GenesisState{
|
|
|
|
Params: types.DefaultParams(),
|
|
|
|
Accounts: []types.GenesisAccount{
|
|
|
|
{
|
|
|
|
Address: address.String(),
|
|
|
|
Code: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-11-27 18:42:04 +00:00
|
|
|
}
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest() // reset values
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb = suite.StateDB()
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
tc.malleate()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.Commit()
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
if tc.expPanic {
|
|
|
|
suite.Require().Panics(
|
|
|
|
func() {
|
2021-06-08 17:10:29 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
suite.Require().NotPanics(
|
|
|
|
func() {
|
2021-06-08 17:10:29 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 15:50:22 +00:00
|
|
|
}
|