diff --git a/CHANGELOG.md b/CHANGELOG.md index 036987ef..d39f0b7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased +### Bug Fixes + +* (evm) [\#621](https://github.com/cosmos/ethermint/issues/621) EVM `GenesisAccount` fields now share the same format as the auth module `Account`. +* (app) [\#617](https://github.com/cosmos/ethermint/issues/617) Fix genesis export functionality. + +## [v0.3.1] - 2020-11-24 + ### Improvements * (deps) [\#615](https://github.com/cosmos/ethermint/pull/615) Bump Cosmos SDK version to [v0.39.2](https://github.com/cosmos/cosmos-sdk/tag/v0.39.2) diff --git a/cmd/ethermintd/main.go b/cmd/ethermintd/main.go index b94a5781..506c22f8 100644 --- a/cmd/ethermintd/main.go +++ b/cmd/ethermintd/main.go @@ -111,14 +111,16 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application func exportAppStateAndTMValidators( logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, ) (json.RawMessage, []tmtypes.GenesisValidator, error) { - - ethermintApp := app.NewEthermintApp(logger, db, traceStore, true, map[int64]bool{}, 0) + var ethermintApp *app.EthermintApp if height != -1 { - err := ethermintApp.LoadHeight(height) - if err != nil { + ethermintApp = app.NewEthermintApp(logger, db, traceStore, false, map[int64]bool{}, 0) + + if err := ethermintApp.LoadHeight(height); err != nil { return nil, nil, err } + } else { + ethermintApp = app.NewEthermintApp(logger, db, traceStore, true, map[int64]bool{}, 0) } return ethermintApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) diff --git a/x/evm/genesis.go b/x/evm/genesis.go index da55321d..1b21fa03 100644 --- a/x/evm/genesis.go +++ b/x/evm/genesis.go @@ -1,29 +1,61 @@ package evm import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" - emint "github.com/cosmos/ethermint/types" + ethcmn "github.com/ethereum/go-ethereum/common" + + ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm/types" abci "github.com/tendermint/tendermint/abci/types" ) // InitGenesis initializes genesis state based on exported genesis -func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) []abci.ValidatorUpdate { +func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, data GenesisState) []abci.ValidatorUpdate { // nolint: interfacer + evmDenom := data.Params.EvmDenom + for _, account := range data.Accounts { - // FIXME: this will override bank InitGenesis balance! - k.SetBalance(ctx, account.Address, account.Balance) - k.SetCode(ctx, account.Address, account.Code) + address := ethcmn.HexToAddress(account.Address) + accAddress := sdk.AccAddress(address.Bytes()) + + // check that the EVM balance the matches the account balance + acc := accountKeeper.GetAccount(ctx, accAddress) + if acc == nil { + panic(fmt.Errorf("account not found for address %s", account.Address)) + } + + _, ok := acc.(*ethermint.EthAccount) + if !ok { + panic( + fmt.Errorf("account %s must be an %T type, got %T", + account.Address, ðermint.EthAccount{}, acc, + ), + ) + } + + evmBalance := acc.GetCoins().AmountOf(evmDenom) + if !evmBalance.Equal(account.Balance) { + panic( + fmt.Errorf( + "balance mismatch for account %s, expected %s%s, got %s%s", + account.Address, evmBalance, evmDenom, account.Balance, evmDenom, + ), + ) + } + + k.SetBalance(ctx, address, account.Balance.BigInt()) + k.SetCode(ctx, address, account.Code) for _, storage := range account.Storage { - k.SetState(ctx, account.Address, storage.Key, storage.Value) + k.SetState(ctx, address, storage.Key, storage.Value) } } var err error for _, txLog := range data.TxsLogs { - err = k.SetLogs(ctx, txLog.Hash, txLog.Logs) - if err != nil { + if err = k.SetLogs(ctx, txLog.Hash, txLog.Logs); err != nil { panic(err) } } @@ -54,8 +86,7 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta accounts := ak.GetAllAccounts(ctx) for _, account := range accounts { - - ethAccount, ok := account.(*emint.EthAccount) + ethAccount, ok := account.(*ethermint.EthAccount) if !ok { continue } @@ -67,9 +98,12 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta panic(err) } + balanceInt := k.GetBalance(ctx, addr) + balance := sdk.NewIntFromBigInt(balanceInt) + genAccount := types.GenesisAccount{ - Address: addr, - Balance: k.GetBalance(ctx, addr), + Address: addr.String(), + Balance: balance, Code: k.GetCode(ctx, addr), Storage: storage, } diff --git a/x/evm/genesis_test.go b/x/evm/genesis_test.go index 40194b31..37b39310 100644 --- a/x/evm/genesis_test.go +++ b/x/evm/genesis_test.go @@ -1,14 +1,16 @@ package evm_test import ( - "crypto/ecdsa" - "math/big" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/ethermint/crypto/ethsecp256k1" + ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/evm/types" "github.com/ethereum/go-ethereum/common" + ethcmn "github.com/ethereum/go-ethereum/common" ) func (suite *EvmTestSuite) TestExportImport() { @@ -17,48 +19,119 @@ func (suite *EvmTestSuite) TestExportImport() { genState = evm.ExportGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper) }) - _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState) + _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, genState) } -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("0x608060405234801561001057600080fd5b50612033806100206000396000f3006080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffffffffffffffffffffffff1690565b60009081526020819052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600083815260208190526040812054849073ffffffffffffffffffffffffffffffffffffffff16331461025757600080fd5b6040805186815260208082018790528251918290038301822073ffffffffffffffffffffffffffffffffffffffff871683529251929450869288927fce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e8292908290030190a350600090815260208190526040902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff929092169190911790555050565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461034357600080fd5b6040805167ffffffffffffffff84168152905184917f1d4f9bbfc9cab89d66e1a1562f2233ccbf1308cb4f63de2ead5787adddb8fa68919081900360200190a250600091825260208290526040909120600101805467ffffffffffffffff90921674010000000000000000000000000000000000000000027fffffffff0000000000000000ffffffffffffffffffffffffffffffffffffffff909216919091179055565b60009081526020819052604090206001015474010000000000000000000000000000000000000000900467ffffffffffffffff1690565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461045057600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917f335721b01866dc23fbee8b6b2c7b1e14d6f05c28cd35a2c934239f94095602a0919081900360200190a25060009182526020829052604090912060010180547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff909216919091179055565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461052557600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917fd4735d920b0f87494915f556dd9b54c8f309026070caea5c737245152564d266919081900360200190a25060009182526020829052604090912080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff9092169190911790555600a165627a7a723058203213a96a5c5e630e44a93f7fa415f3c625e46c7a560debc4dcf02cff9018ee6e0029608060405234801561001057600080fd5b5060405160208061141e833981016040525160008054600160a060020a03909216600160a060020a03199092169190911790556113cc806100526000396000f3006080604052600436106100c45763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166301ffc9a781146100c957806310f13a8c146101175780632203ab56146101b557806329cd62ea1461024f5780632dff69411461026d5780633b3b57de1461029757806359d1d43c146102d8578063623195b0146103ab578063691f34311461040b5780637737221314610423578063c3d014d614610481578063c86902331461049c578063d5fa2b00146104cd575b600080fd5b3480156100d557600080fd5b506101037fffffffff00000000000000000000000000000000000000000000000000000000600435166104fe565b604080519115158252519081900360200190f35b34801561012357600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b395833595369560449491939091019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a9998810197919650918201945092508291508401838280828437509497506107139650505050505050565b005b3480156101c157600080fd5b506101d060043560243561098f565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156102135781810151838201526020016101fb565b50505050905090810190601f1680156102405780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561025b57600080fd5b506101b3600435602435604435610a9b565b34801561027957600080fd5b50610285600435610bcb565b60408051918252519081900360200190f35b3480156102a357600080fd5b506102af600435610be1565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156102e457600080fd5b5060408051602060046024803582810135601f8101859004850286018501909652858552610336958335953695604494919390910191908190840183828082843750949750610c099650505050505050565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610370578181015183820152602001610358565b50505050905090810190601f16801561039d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103b757600080fd5b50604080516020600460443581810135601f81018490048402850184019095528484526101b3948235946024803595369594606494920191908190840183828082843750949750610d309650505050505050565b34801561041757600080fd5b50610336600435610e61565b34801561042f57600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b3958335953695604494919390910191908190840183828082843750949750610f059650505050505050565b34801561048d57600080fd5b506101b360043560243561108b565b3480156104a857600080fd5b506104b460043561119c565b6040805192835260208301919091528051918290030190f35b3480156104d957600080fd5b506101b360043573ffffffffffffffffffffffffffffffffffffffff602435166111b9565b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f3b3b57de00000000000000000000000000000000000000000000000000000000148061059157507fffffffff0000000000000000000000000000000000000000000000000000000082167fd8389dc500000000000000000000000000000000000000000000000000000000145b806105dd57507fffffffff0000000000000000000000000000000000000000000000000000000082167f691f343100000000000000000000000000000000000000000000000000000000145b8061062957507fffffffff0000000000000000000000000000000000000000000000000000000082167f2203ab5600000000000000000000000000000000000000000000000000000000145b8061067557507fffffffff0000000000000000000000000000000000000000000000000000000082167fc869023300000000000000000000000000000000000000000000000000000000145b806106c157507fffffffff0000000000000000000000000000000000000000000000000000000082167f59d1d43c00000000000000000000000000000000000000000000000000000000145b8061070d57507fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a700000000000000000000000000000000000000000000000000000000145b92915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561078957600080fd5b505af115801561079d573d6000803e3d6000fd5b505050506040513d60208110156107b357600080fd5b505173ffffffffffffffffffffffffffffffffffffffff16146107d557600080fd5b6000848152600160209081526040918290209151855185936005019287929182918401908083835b6020831061083a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107fd565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845161087b9591949190910192509050611305565b50826040518082805190602001908083835b602083106108ca57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161088d565b51815160209384036101000a60001901801990921691161790526040805192909401829003822081835289518383015289519096508a95507fd8c9334b1a9c2f9da342a0a2b32629c1a229b6445dad78947f674b44444a7550948a94508392908301919085019080838360005b8381101561094f578181015183820152602001610937565b50505050905090810190601f16801561097c5780820380516001836020036101000a031916815260200191505b509250505060405180910390a350505050565b60008281526001602081905260409091206060905b838311610a8e57828416158015906109dd5750600083815260068201602052604081205460026000196101006001841615020190911604115b15610a8357600083815260068201602090815260409182902080548351601f600260001961010060018616150201909316929092049182018490048402810184019094528084529091830182828015610a775780601f10610a4c57610100808354040283529160200191610a77565b820191906000526020600020905b815481529060010190602001808311610a5a57829003601f168201915b50505050509150610a93565b6002909202916109a4565b600092505b509250929050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610b1157600080fd5b505af1158015610b25573d6000803e3d6000fd5b505050506040513d6020811015610b3b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610b5d57600080fd5b604080518082018252848152602080820185815260008881526001835284902092516003840155516004909201919091558151858152908101849052815186927f1d6f5e03d3f63eb58751986629a5439baee5079ff04f345becb66e23eb154e46928290030190a250505050565b6000908152600160208190526040909120015490565b60009081526001602052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600082815260016020908152604091829020915183516060936005019285929182918401908083835b60208310610c6f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c32565b518151600019602094850361010090810a820192831692199390931691909117909252949092019687526040805197889003820188208054601f6002600183161590980290950116959095049283018290048202880182019052818752929450925050830182828015610d235780601f10610cf857610100808354040283529160200191610d23565b820191906000526020600020905b815481529060010190602001808311610d0657829003601f168201915b5050505050905092915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610da657600080fd5b505af1158015610dba573d6000803e3d6000fd5b505050506040513d6020811015610dd057600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610df257600080fd5b6000198301831615610e0357600080fd5b600084815260016020908152604080832086845260060182529091208351610e2d92850190611305565b50604051839085907faa121bbeef5f32f5961a2a28966e769023910fc9479059ee3495d4c1a696efe390600090a350505050565b6000818152600160208181526040928390206002908101805485516000199582161561010002959095011691909104601f81018390048302840183019094528383526060939091830182828015610ef95780601f10610ece57610100808354040283529160200191610ef9565b820191906000526020600020905b815481529060010190602001808311610edc57829003601f168201915b50505050509050919050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610f7b57600080fd5b505af1158015610f8f573d6000803e3d6000fd5b505050506040513d6020811015610fa557600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610fc757600080fd5b60008381526001602090815260409091208351610fec92600290920191850190611305565b50604080516020808252845181830152845186937fb7d29e911041e8d9b843369e890bcb72c9388692ba48b65ac54e7214c4c348f79387939092839283019185019080838360005b8381101561104c578181015183820152602001611034565b50505050905090810190601f1680156110795780820380516001836020036101000a031916815260200191505b509250505060405180910390a2505050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561110157600080fd5b505af1158015611115573d6000803e3d6000fd5b505050506040513d602081101561112b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461114d57600080fd5b6000838152600160208181526040928390209091018490558151848152915185927f0424b6fe0d9c3bdbece0e7879dc241bb0c22e900be8b6c168b4ee08bd9bf83bc92908290030190a2505050565b600090815260016020526040902060038101546004909101549091565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561122f57600080fd5b505af1158015611243573d6000803e3d6000fd5b505050506040513d602081101561125957600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461127b57600080fd5b60008381526001602090815260409182902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff86169081179091558251908152915185927f52d7d861f09ab3d26239d492e8968629f95e9e318cf0b73bfddc441522a15fd292908290030190a2505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061134657805160ff1916838001178555611373565b82800160010185558215611373579182015b82811115611373578251825591602001919060010190611358565b5061137f929150611383565b5090565b61139d91905b8082111561137f5760008155600101611389565b905600a165627a7a72305820494d2089cb484863f6172bb6f0ed3b148d6c8eb2a0dd86d330bf08813f99f65d0029a165627a7a72305820df7d6399d923bc8a4fe3d290869ee8614cd2e7d4ac7481c4de85e2df61d183370029") - 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("0x6080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffffffffffffffffffffffff1690565b60009081526020819052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600083815260208190526040812054849073ffffffffffffffffffffffffffffffffffffffff16331461025757600080fd5b6040805186815260208082018790528251918290038301822073ffffffffffffffffffffffffffffffffffffffff871683529251929450869288927fce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e8292908290030190a350600090815260208190526040902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff929092169190911790555050565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461034357600080fd5b6040805167ffffffffffffffff84168152905184917f1d4f9bbfc9cab89d66e1a1562f2233ccbf1308cb4f63de2ead5787adddb8fa68919081900360200190a250600091825260208290526040909120600101805467ffffffffffffffff90921674010000000000000000000000000000000000000000027fffffffff0000000000000000ffffffffffffffffffffffffffffffffffffffff909216919091179055565b60009081526020819052604090206001015474010000000000000000000000000000000000000000900467ffffffffffffffff1690565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461045057600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917f335721b01866dc23fbee8b6b2c7b1e14d6f05c28cd35a2c934239f94095602a0919081900360200190a25060009182526020829052604090912060010180547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff909216919091179055565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461052557600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917fd4735d920b0f87494915f556dd9b54c8f309026070caea5c737245152564d266919081900360200190a25060009182526020829052604090912080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff9092169190911790555600a165627a7a723058203213a96a5c5e630e44a93f7fa415f3c625e46c7a560debc4dcf02cff9018ee6e0029608060405234801561001057600080fd5b5060405160208061141e833981016040525160008054600160a060020a03909216600160a060020a03199092169190911790556113cc806100526000396000f3006080604052600436106100c45763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166301ffc9a781146100c957806310f13a8c146101175780632203ab56146101b557806329cd62ea1461024f5780632dff69411461026d5780633b3b57de1461029757806359d1d43c146102d8578063623195b0146103ab578063691f34311461040b5780637737221314610423578063c3d014d614610481578063c86902331461049c578063d5fa2b00146104cd575b600080fd5b3480156100d557600080fd5b506101037fffffffff00000000000000000000000000000000000000000000000000000000600435166104fe565b604080519115158252519081900360200190f35b34801561012357600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b395833595369560449491939091019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a9998810197919650918201945092508291508401838280828437509497506107139650505050505050565b005b3480156101c157600080fd5b506101d060043560243561098f565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156102135781810151838201526020016101fb565b50505050905090810190601f1680156102405780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561025b57600080fd5b506101b3600435602435604435610a9b565b34801561027957600080fd5b50610285600435610bcb565b60408051918252519081900360200190f35b3480156102a357600080fd5b506102af600435610be1565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156102e457600080fd5b5060408051602060046024803582810135601f8101859004850286018501909652858552610336958335953695604494919390910191908190840183828082843750949750610c099650505050505050565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610370578181015183820152602001610358565b50505050905090810190601f16801561039d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103b757600080fd5b50604080516020600460443581810135601f81018490048402850184019095528484526101b3948235946024803595369594606494920191908190840183828082843750949750610d309650505050505050565b34801561041757600080fd5b50610336600435610e61565b34801561042f57600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b3958335953695604494919390910191908190840183828082843750949750610f059650505050505050565b34801561048d57600080fd5b506101b360043560243561108b565b3480156104a857600080fd5b506104b460043561119c565b6040805192835260208301919091528051918290030190f35b3480156104d957600080fd5b506101b360043573ffffffffffffffffffffffffffffffffffffffff602435166111b9565b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f3b3b57de00000000000000000000000000000000000000000000000000000000148061059157507fffffffff0000000000000000000000000000000000000000000000000000000082167fd8389dc500000000000000000000000000000000000000000000000000000000145b806105dd57507fffffffff0000000000000000000000000000000000000000000000000000000082167f691f343100000000000000000000000000000000000000000000000000000000145b8061062957507fffffffff0000000000000000000000000000000000000000000000000000000082167f2203ab5600000000000000000000000000000000000000000000000000000000145b8061067557507fffffffff0000000000000000000000000000000000000000000000000000000082167fc869023300000000000000000000000000000000000000000000000000000000145b806106c157507fffffffff0000000000000000000000000000000000000000000000000000000082167f59d1d43c00000000000000000000000000000000000000000000000000000000145b8061070d57507fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a700000000000000000000000000000000000000000000000000000000145b92915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561078957600080fd5b505af115801561079d573d6000803e3d6000fd5b505050506040513d60208110156107b357600080fd5b505173ffffffffffffffffffffffffffffffffffffffff16146107d557600080fd5b6000848152600160209081526040918290209151855185936005019287929182918401908083835b6020831061083a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107fd565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845161087b9591949190910192509050611305565b50826040518082805190602001908083835b602083106108ca57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161088d565b51815160209384036101000a60001901801990921691161790526040805192909401829003822081835289518383015289519096508a95507fd8c9334b1a9c2f9da342a0a2b32629c1a229b6445dad78947f674b44444a7550948a94508392908301919085019080838360005b8381101561094f578181015183820152602001610937565b50505050905090810190601f16801561097c5780820380516001836020036101000a031916815260200191505b509250505060405180910390a350505050565b60008281526001602081905260409091206060905b838311610a8e57828416158015906109dd5750600083815260068201602052604081205460026000196101006001841615020190911604115b15610a8357600083815260068201602090815260409182902080548351601f600260001961010060018616150201909316929092049182018490048402810184019094528084529091830182828015610a775780601f10610a4c57610100808354040283529160200191610a77565b820191906000526020600020905b815481529060010190602001808311610a5a57829003601f168201915b50505050509150610a93565b6002909202916109a4565b600092505b509250929050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610b1157600080fd5b505af1158015610b25573d6000803e3d6000fd5b505050506040513d6020811015610b3b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610b5d57600080fd5b604080518082018252848152602080820185815260008881526001835284902092516003840155516004909201919091558151858152908101849052815186927f1d6f5e03d3f63eb58751986629a5439baee5079ff04f345becb66e23eb154e46928290030190a250505050565b6000908152600160208190526040909120015490565b60009081526001602052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600082815260016020908152604091829020915183516060936005019285929182918401908083835b60208310610c6f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c32565b518151600019602094850361010090810a820192831692199390931691909117909252949092019687526040805197889003820188208054601f6002600183161590980290950116959095049283018290048202880182019052818752929450925050830182828015610d235780601f10610cf857610100808354040283529160200191610d23565b820191906000526020600020905b815481529060010190602001808311610d0657829003601f168201915b5050505050905092915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610da657600080fd5b505af1158015610dba573d6000803e3d6000fd5b505050506040513d6020811015610dd057600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610df257600080fd5b6000198301831615610e0357600080fd5b600084815260016020908152604080832086845260060182529091208351610e2d92850190611305565b50604051839085907faa121bbeef5f32f5961a2a28966e769023910fc9479059ee3495d4c1a696efe390600090a350505050565b6000818152600160208181526040928390206002908101805485516000199582161561010002959095011691909104601f81018390048302840183019094528383526060939091830182828015610ef95780601f10610ece57610100808354040283529160200191610ef9565b820191906000526020600020905b815481529060010190602001808311610edc57829003601f168201915b50505050509050919050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610f7b57600080fd5b505af1158015610f8f573d6000803e3d6000fd5b505050506040513d6020811015610fa557600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610fc757600080fd5b60008381526001602090815260409091208351610fec92600290920191850190611305565b50604080516020808252845181830152845186937fb7d29e911041e8d9b843369e890bcb72c9388692ba48b65ac54e7214c4c348f79387939092839283019185019080838360005b8381101561104c578181015183820152602001611034565b50505050905090810190601f1680156110795780820380516001836020036101000a031916815260200191505b509250505060405180910390a2505050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561110157600080fd5b505af1158015611115573d6000803e3d6000fd5b505050506040513d602081101561112b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461114d57600080fd5b6000838152600160208181526040928390209091018490558151848152915185927f0424b6fe0d9c3bdbece0e7879dc241bb0c22e900be8b6c168b4ee08bd9bf83bc92908290030190a2505050565b600090815260016020526040902060038101546004909101549091565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561122f57600080fd5b505af1158015611243573d6000803e3d6000fd5b505050506040513d602081101561125957600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461127b57600080fd5b60008381526001602090815260409182902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff86169081179091558251908152915185927f52d7d861f09ab3d26239d492e8968629f95e9e318cf0b73bfddc441522a15fd292908290030190a2505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061134657805160ff1916838001178555611373565b82800160010185558215611373579182015b82811115611373578251825591602001919060010190611358565b5061137f929150611383565b5090565b61139d91905b8082111561137f5760008155600101611389565b905600a165627a7a72305820494d2089cb484863f6172bb6f0ed3b148d6c8eb2a0dd86d330bf08813f99f65d0029a165627a7a72305820df7d6399d923bc8a4fe3d290869ee8614cd2e7d4ac7481c4de85e2df61d183370029") - 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, 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(3), priv) +func (suite *EvmTestSuite) TestInitGenesis() { + privkey, err := ethsecp256k1.GenerateKey() 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 + address := ethcmn.HexToAddress(privkey.PubKey().Address().String()) + + testCases := []struct { + name string + malleate func() + 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) + err := acc.SetCoins(sdk.NewCoins(ethermint.NewPhotonCoinInt64(1))) + suite.Require().NoError(err) + suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + }, + types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Balance: sdk.OneInt(), + Storage: types.Storage{ + {Key: common.BytesToHash([]byte("key")), Value: common.BytesToHash([]byte("value"))}, + }, + }, + }, + }, + false, + }, + { + "account not found", + func() {}, + types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + }, + }, + }, + true, + }, + { + "invalid account type", + func() { + acc := authtypes.NewBaseAccountWithAddress(address.Bytes()) + suite.app.AccountKeeper.SetAccount(suite.ctx, &acc) + }, + types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Balance: sdk.OneInt(), + }, + }, + }, + true, + }, + { + "balance mismatch", + func() { + acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes()) + suite.Require().NotNil(acc) + suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + }, + types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Balance: sdk.OneInt(), + }, + }, + }, + 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) + }, + ) + } + }) + } } diff --git a/x/evm/keeper/querier.go b/x/evm/keeper/querier.go index 9b062849..df824e9a 100644 --- a/x/evm/keeper/querier.go +++ b/x/evm/keeper/querier.go @@ -200,7 +200,8 @@ func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) } func queryExportAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) { - addr := ethcmn.HexToAddress(path[1]) + hexAddress := path[1] + addr := ethcmn.HexToAddress(hexAddress) var storage types.Storage err := keeper.ForEachStorage(ctx, addr, func(key, value ethcmn.Hash) bool { @@ -211,9 +212,12 @@ func queryExportAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, return nil, err } + balanceInt := keeper.GetBalance(ctx, addr) + balance := sdk.NewIntFromBigInt(balanceInt) + res := types.GenesisAccount{ - Address: addr, - Balance: keeper.GetBalance(ctx, addr), + Address: hexAddress, + Balance: balance, Code: keeper.GetCode(ctx, addr), Storage: storage, } diff --git a/x/evm/module.go b/x/evm/module.go index 28d09bc9..1bef9494 100644 --- a/x/evm/module.go +++ b/x/evm/module.go @@ -124,7 +124,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState types.ModuleCdc.MustUnmarshalJSON(data, &genesisState) - return InitGenesis(ctx, am.keeper, genesisState) + return InitGenesis(ctx, am.keeper, am.ak, genesisState) } // ExportGenesis exports the genesis state to be used by daemon diff --git a/x/evm/module_test.go b/x/evm/module_test.go deleted file mode 100644 index 561210c8..00000000 --- a/x/evm/module_test.go +++ /dev/null @@ -1,38 +0,0 @@ -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" - } - ], - "params": { - "evm_denom": "aphoton" - } - }` - -func (suite *EvmTestSuite) TestInitGenesis() { - am := evm.NewAppModule(suite.app.EvmKeeper, suite.app.AccountKeeper) - in := json.RawMessage([]byte(testJSON)) - _ = am.InitGenesis(suite.ctx, in) - - testAddr := common.HexToAddress("0x2cc7fdf9fde6746731d7f11979609d455c2c197a") - - res := suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx).GetCode(testAddr) - expectedCode := common.FromHex("0x60806040") - suite.Require().Equal(expectedCode, res) -} diff --git a/x/evm/types/genesis.go b/x/evm/types/genesis.go index 82db992d..8d944301 100644 --- a/x/evm/types/genesis.go +++ b/x/evm/types/genesis.go @@ -1,10 +1,10 @@ package types import ( - "bytes" "errors" "fmt" - "math/big" + + sdk "github.com/cosmos/cosmos-sdk/types" ethcmn "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -23,22 +23,22 @@ type ( // Its main difference between with Geth's GenesisAccount is that it uses a custom // storage type and that it doesn't contain the private key field. GenesisAccount struct { - Address ethcmn.Address `json:"address"` - Balance *big.Int `json:"balance"` - Code hexutil.Bytes `json:"code,omitempty"` - Storage Storage `json:"storage,omitempty"` + Address string `json:"address"` + Balance sdk.Int `json:"balance"` + Code hexutil.Bytes `json:"code,omitempty"` + Storage Storage `json:"storage,omitempty"` } ) // Validate performs a basic validation of a GenesisAccount fields. func (ga GenesisAccount) Validate() error { - if bytes.Equal(ga.Address.Bytes(), ethcmn.Address{}.Bytes()) { - return fmt.Errorf("address cannot be the zero address %s", ga.Address.String()) + if ga.Address == (ethcmn.Address{}.String()) { + return fmt.Errorf("address cannot be the zero address %s", ga.Address) } - if ga.Balance == nil { + if ga.Balance.IsNil() { return errors.New("balance cannot be nil") } - if ga.Balance.Sign() == -1 { + if ga.Balance.IsNegative() { return errors.New("balance cannot be negative") } if ga.Code != nil && len(ga.Code) == 0 { @@ -65,14 +65,15 @@ func (gs GenesisState) Validate() error { seenAccounts := make(map[string]bool) seenTxs := make(map[string]bool) for _, acc := range gs.Accounts { - if seenAccounts[acc.Address.String()] { - return fmt.Errorf("duplicated genesis account %s", acc.Address.String()) + if seenAccounts[acc.Address] { + return fmt.Errorf("duplicated genesis account %s", acc.Address) } if err := acc.Validate(); err != nil { - return fmt.Errorf("invalid genesis account %s: %w", acc.Address.String(), err) + return fmt.Errorf("invalid genesis account %s: %w", acc.Address, err) } - seenAccounts[acc.Address.String()] = true + seenAccounts[acc.Address] = true } + for _, tx := range gs.TxsLogs { if seenTxs[tx.Hash.String()] { return fmt.Errorf("duplicated logs from transaction %s", tx.Hash.String()) diff --git a/x/evm/types/genesis_test.go b/x/evm/types/genesis_test.go index 7ac722da..1358a67f 100644 --- a/x/evm/types/genesis_test.go +++ b/x/evm/types/genesis_test.go @@ -1,7 +1,6 @@ package types import ( - "math/big" "testing" "github.com/stretchr/testify/require" @@ -10,9 +9,13 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ) +var address = ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}) + func TestValidateGenesisAccount(t *testing.T) { testCases := []struct { name string @@ -22,8 +25,8 @@ func TestValidateGenesisAccount(t *testing.T) { { "valid genesis account", GenesisAccount{ - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})), @@ -34,32 +37,32 @@ func TestValidateGenesisAccount(t *testing.T) { { "empty account address bytes", GenesisAccount{ - Address: ethcmn.Address{}, - Balance: big.NewInt(1), + Address: ethcmn.Address{}.String(), + Balance: sdk.NewInt(1), }, false, }, { "nil account balance", GenesisAccount{ - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: nil, + Address: address.String(), + Balance: sdk.Int{}, }, false, }, { "nil account balance", GenesisAccount{ - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(-1), + Address: address.String(), + Balance: sdk.NewInt(-1), }, false, }, { "empty code bytes", GenesisAccount{ - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{}, }, false, @@ -97,8 +100,8 @@ func TestValidateGenesis(t *testing.T) { genState: GenesisState{ Accounts: []GenesisAccount{ { - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ {Key: ethcmn.BytesToHash([]byte{1, 2, 3})}, @@ -138,7 +141,7 @@ func TestValidateGenesis(t *testing.T) { genState: GenesisState{ Accounts: []GenesisAccount{ { - Address: ethcmn.Address{}, + Address: ethcmn.Address{}.String(), }, }, }, @@ -149,16 +152,16 @@ func TestValidateGenesis(t *testing.T) { genState: GenesisState{ Accounts: []GenesisAccount{ { - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})), }, }, { - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})), @@ -173,8 +176,8 @@ func TestValidateGenesis(t *testing.T) { genState: GenesisState{ Accounts: []GenesisAccount{ { - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ {Key: ethcmn.BytesToHash([]byte{1, 2, 3})}, @@ -223,8 +226,8 @@ func TestValidateGenesis(t *testing.T) { genState: GenesisState{ Accounts: []GenesisAccount{ { - Address: ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5}), - Balance: big.NewInt(1), + Address: address.String(), + Balance: sdk.NewInt(1), Code: []byte{1, 2, 3}, Storage: Storage{ {Key: ethcmn.BytesToHash([]byte{1, 2, 3})},