2020-05-18 19:21:12 +00:00
|
|
|
package evm_test
|
|
|
|
|
|
|
|
import (
|
2020-05-29 15:50:22 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/crypto"
|
2020-05-18 19:21:12 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm"
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2020-05-29 15:50:22 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-05-18 19:21:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *EvmTestSuite) TestExportImport() {
|
|
|
|
var genState types.GenesisState
|
|
|
|
suite.Require().NotPanics(func() {
|
|
|
|
genState = evm.ExportGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper)
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState)
|
|
|
|
}
|
2020-05-29 15:50:22 +00:00
|
|
|
|
|
|
|
func (suite *EvmTestSuite) TestContractExportImport() {
|
|
|
|
gasLimit := uint64(5000000)
|
|
|
|
gasPrice := big.NewInt(1)
|
|
|
|
|
|
|
|
priv, err := crypto.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
|
2020-07-13 20:01:45 +00:00
|
|
|
suite.app.EvmKeeper.SetCode(suite.ctx, address, nil)
|
2020-05-29 15:50:22 +00:00
|
|
|
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState)
|
|
|
|
|
2020-07-13 20:01:45 +00:00
|
|
|
resCode := suite.app.EvmKeeper.GetCode(suite.ctx, address)
|
2020-05-29 15:50:22 +00:00
|
|
|
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(3), priv)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
result, err := suite.handler(suite.ctx, tx)
|
|
|
|
suite.Require().NoError(err, "failed to handle eth tx msg")
|
|
|
|
resData, err := types.DecodeResultData(result.Data)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
return resData.ContractAddress
|
|
|
|
}
|