genesis functions cleanup, testing (#318)

This commit is contained in:
noot authored and GitHub committed 2020-05-29 11:50:22 -04:00
1 parent 915a9018d4
commit 4fd9ec3a26
7 files changed
+108 -3

No files matched your search

+1 -1
View File
@@ -33,7 +33,7 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta
var err error
for _, account := range accounts {
ethAccount, ok := account.(emint.EthAccount)
ethAccount, ok := account.(*emint.EthAccount)
if !ok {
continue
}
+49
View File
@@ -1,8 +1,14 @@
package evm_test
import (
"crypto/ecdsa"
"math/big"
"github.com/cosmos/ethermint/crypto"
"github.com/cosmos/ethermint/x/evm"
"github.com/cosmos/ethermint/x/evm/types"
"github.com/ethereum/go-ethereum/common"
)
func (suite *EvmTestSuite) TestExportImport() {
@@ -13,3 +19,46 @@ func (suite *EvmTestSuite) TestExportImport() {
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState)
}
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("0x608060405234801561001057600080fd5b50612033806100206000396000f3006080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffffffffffffffffffffffff1690565b60009081526020819052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600083815260208190526040812054849073ffffffffffffffffffffffffffffffffffffffff16331461025757600080fd5b6040805186815260208082018790528251918290038301822073ffffffffffffffffffffffffffffffffffffffff871683529251929450869288927fce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e8292908290030190a350600090815260208190526040902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff929092169190911790555050565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461034357600080fd5b6040805167ffffffffffffffff84168152905184917f1d4f9bbfc9cab89d66e1a1562f2233ccbf1308cb4f63de2ead5787adddb8fa68919081900360200190a25060009182526020829052604090912060010180Line truncated
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("0x6080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffffffffffffffffffffffff1690565b60009081526020819052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600083815260208190526040812054849073ffffffffffffffffffffffffffffffffffffffff16331461025757600080fd5b6040805186815260208082018790528251918290038301822073ffffffffffffffffffffffffffffffffffffffff871683529251929450869288927fce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e8292908290030190a350600090815260208190526040902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff929092169190911790555050565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461034357600080fd5b6040805167ffffffffffffffff84168152905184917f1d4f9bbfc9cab89d66e1a1562f2233ccbf1308cb4f63de2ead5787adddb8fa68919081900360200190a250600091825260208290526040909120600101805467ffffffffffffffff909216740100000000000000000000000000Line truncated
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.CommitStateDB.WithContext(suite.ctx).SetCode(address, nil)
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState)
resCode := suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx).GetCode(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(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
}
+35
View File
@@ -0,0 +1,35 @@
package evm_test
import (
"encoding/json"
"github.com/cosmos/ethermint/x/evm"
"github.com/ethereum/go-ethereum/common"
)
var testJSON = `{
"accounts": [
{
"address": "0x00cabdd44664b73cfc3194b9d32eb6c351ef7652",
"balance": 34
},
{
"address": "0x2cc7fdf9fde6746731d7f11979609d455c2c197a",
"balance": 0,
"code": "0x60806040"
}
]
}`
func (suite *EvmTestSuite) TestInitGenesis() {
am := evm.NewAppModule(suite.app.EvmKeeper, suite.app.AccountKeeper)
in := json.RawMessage([]byte(testJSON))
_ = am.InitGenesis(suite.ctx, suite.codec, in)
testAddr := common.HexToAddress("0x2cc7fdf9fde6746731d7f11979609d455c2c197a")
res := suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx).GetCode(testAddr)
expectedCode := common.FromHex("0x60806040")
suite.Require().Equal(expectedCode, res)
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"math/big"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type (
@@ -29,7 +30,7 @@ type (
GenesisAccount struct {
Address ethcmn.Address `json:"address"`
Balance *big.Int `json:"balance"`
Code []byte `json:"code,omitempty"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage []GenesisStorage `json:"storage,omitempty"`
}
)
+9
View File
@@ -63,6 +63,11 @@ func NewMsgEthermint(
}
}
func (msg MsgEthermint) String() string {
return fmt.Sprintf("nonce=%d gasPrice=%d gasLimit=%d recipient=%s amount=%d data=0x%x from=%s",
msg.AccountNonce, msg.Price, msg.GasLimit, msg.Recipient, msg.Amount, msg.Payload, msg.From)
}
// Route should return the name of the module
func (msg MsgEthermint) Route() string { return RouterKey }
@@ -166,6 +171,10 @@ func newMsgEthereumTx(
return MsgEthereumTx{Data: txData}
}
func (msg MsgEthereumTx) String() string {
return msg.Data.String()
}
// Route returns the route value of an MsgEthereumTx.
func (msg MsgEthereumTx) Route() string { return RouterKey }
+1 -1
View File
@@ -590,7 +590,7 @@ func (csdb *CommitStateDB) UpdateAccounts() {
currAcc := csdb.accountKeeper.GetAccount(csdb.ctx, sdk.AccAddress(addr.Bytes()))
emintAcc, ok := currAcc.(*emint.EthAccount)
if !ok {
return
continue
}
balance := csdb.bankKeeper.GetBalance(csdb.ctx, emintAcc.GetAddress(), emint.DenomDefault)
+11
View File
@@ -1,6 +1,7 @@
package types
import (
"fmt"
"math/big"
"github.com/cosmos/ethermint/utils"
@@ -46,6 +47,16 @@ type encodableTxData struct {
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
}
func (td TxData) String() string {
if td.Recipient != nil {
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=%s amount=%s data=0x%x v=%s r=%s s=%s",
td.AccountNonce, td.Price, td.GasLimit, td.Recipient.Hex(), td.Amount, td.Payload, td.V, td.R, td.S)
}
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=nil amount=%s data=0x%x v=%s r=%s s=%s",
td.AccountNonce, td.Price, td.GasLimit, td.Amount, td.Payload, td.V, td.R, td.S)
}
// MarshalAmino defines custom encoding scheme for TxData
func (td TxData) MarshalAmino() ([]byte, error) {
gasPrice, err := utils.MarshalBigInt(td.Price)