2020-05-18 19:21:12 +00:00
|
|
|
package evm_test
|
|
|
|
|
|
|
|
import (
|
2021-04-18 15:54:18 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2020-11-27 18:42:04 +00:00
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm"
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *EvmTestSuite) TestExportImport() {
|
2021-04-17 10:00:07 +00:00
|
|
|
var genState *types.GenesisState
|
2020-05-18 19:21:12 +00:00
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-07 11:55:01 +00:00
|
|
|
genState = evm.ExportGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper)
|
2020-05-18 19:21:12 +00:00
|
|
|
})
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *genState)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-05-29 15:50:22 +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
|
|
|
|
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() {
|
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
|
|
|
|
suite.Require().NotNil(acc)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
err := suite.app.BankKeeper.SetBalance(suite.ctx, address.Bytes(), ethermint.NewInjectiveCoinInt64(1))
|
2020-11-27 18:42:04 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
},
|
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,
|
|
|
|
},
|
|
|
|
}
|
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
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
tc.malleate()
|
2020-05-29 15:50:22 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
if tc.expPanic {
|
|
|
|
suite.Require().Panics(
|
|
|
|
func() {
|
2021-04-17 10:00:07 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *tc.genState)
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
suite.Require().NotPanics(
|
|
|
|
func() {
|
2021-04-17 10:00:07 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *tc.genState)
|
2020-11-27 18:42:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 15:50:22 +00:00
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
func (suite *EvmTestSuite) TestContractExportImport() {
|
|
|
|
gasLimit := uint64(5000000)
|
|
|
|
gasPrice := big.NewInt(1)
|
|
|
|
|
|
|
|
priv, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err, "failed to create key")
|
|
|
|
|
|
|
|
ensFactoryCode := common.FromHex("0x608060405234801561001057600080fd5b50612033806100206000396000f3006080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60
|
|
|
|
address := suite.deployContract(ensFactoryCode, 1, gasLimit, gasPrice, priv.ToECDSA())
|
|
|
|
|
|
|
|
var genState *types.GenesisState
|
|
|
|
suite.Require().NotPanics(func() {
|
|
|
|
genState = evm.ExportGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper)
|
|
|
|
})
|
|
|
|
|
|
|
|
// sanity check that contract was deployed
|
|
|
|
deployedEnsFactoryCode := common.FromHex("0x6080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffff
|
|
|
|
code := suite.app.EvmKeeper.GetCode(suite.ctx, address)
|
|
|
|
suite.Require().Equal(deployedEnsFactoryCode, code)
|
|
|
|
|
|
|
|
suite.T().Logf("account address 0x%s", priv.PubKey().Address())
|
|
|
|
suite.T().Logf("contract addr 0x%x", address)
|
|
|
|
|
|
|
|
// clear keeper code and re-initialize
|
|
|
|
suite.app.EvmKeeper.SetCode(suite.ctx, address, nil)
|
|
|
|
_ = evm.InitGenesis(suite.ctx, *suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *genState)
|
|
|
|
|
|
|
|
resCode := suite.app.EvmKeeper.GetCode(suite.ctx, address)
|
|
|
|
suite.Require().Equal(deployedEnsFactoryCode, resCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *EvmTestSuite) deployContract(code []byte, nonce, gasLimit uint64, gasPrice *big.Int, priv *ecdsa.PrivateKey) common.Address {
|
|
|
|
tx := types.NewMsgEthereumTx(nonce, nil, big.NewInt(0), gasLimit, gasPrice, code)
|
|
|
|
err := tx.Sign(big.NewInt(888), priv)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
result, err := suite.handler(suite.ctx, tx)
|
|
|
|
suite.Require().NoError(err, "failed to handle eth tx msg")
|
|
|
|
|
|
|
|
var resp types.MsgEthereumTxResponse
|
|
|
|
err = proto.Unmarshal(result.Data, &resp)
|
|
|
|
suite.Require().NoError(err, "failed to unmarshal tx result")
|
|
|
|
|
|
|
|
return common.HexToAddress(resp.ContractAddress)
|
|
|
|
}
|