2019-07-08 16:02:20 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
2020-11-27 18:42:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-07-08 16:02:20 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-07-08 16:02:20 +00:00
|
|
|
)
|
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
// InitGenesis initializes genesis state based on exported genesis
|
2020-11-27 18:42:04 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, data GenesisState) []abci.ValidatorUpdate { // nolint: interfacer
|
|
|
|
evmDenom := data.Params.EvmDenom
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, account := range data.Accounts {
|
2020-11-27 18:42:04 +00:00
|
|
|
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)
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, storage := range account.Storage {
|
2020-11-27 18:42:04 +00:00
|
|
|
k.SetState(ctx, address, storage.Key, storage.Value)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, txLog := range data.TxsLogs {
|
2020-11-27 18:42:04 +00:00
|
|
|
if err = k.SetLogs(ctx, txLog.Hash, txLog.Logs); err != nil {
|
2020-06-04 10:40:21 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
k.SetChainConfig(ctx, data.ChainConfig)
|
|
|
|
k.SetParams(ctx, data.Params)
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// set state objects and code to store
|
|
|
|
_, err = k.Commit(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set storage to store
|
2020-08-23 21:41:54 +00:00
|
|
|
// NOTE: don't delete empty object to prevent import-export simulation failure
|
|
|
|
err = k.Finalise(ctx, false)
|
2020-06-04 10:40:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-23 21:41:54 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:38:47 +00:00
|
|
|
// ExportGenesis exports genesis state of the EVM module
|
2020-05-18 19:21:12 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisState {
|
|
|
|
// nolint: prealloc
|
2020-06-26 22:26:55 +00:00
|
|
|
var ethGenAccounts []types.GenesisAccount
|
2020-05-18 19:21:12 +00:00
|
|
|
accounts := ak.GetAllAccounts(ctx)
|
|
|
|
|
|
|
|
for _, account := range accounts {
|
2020-11-27 18:42:04 +00:00
|
|
|
ethAccount, ok := account.(*ethermint.EthAccount)
|
2020-05-18 19:21:12 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
addr := ethAccount.EthAddress()
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
storage, err := k.GetAccountStorage(ctx, addr)
|
2020-05-18 19:21:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
balanceInt := k.GetBalance(ctx, addr)
|
|
|
|
balance := sdk.NewIntFromBigInt(balanceInt)
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
genAccount := types.GenesisAccount{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: addr.String(),
|
|
|
|
Balance: balance,
|
2020-05-18 19:21:12 +00:00
|
|
|
Code: k.GetCode(ctx, addr),
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
|
|
|
|
ethGenAccounts = append(ethGenAccounts, genAccount)
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
config, _ := k.GetChainConfig(ctx)
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
return GenesisState{
|
2020-09-02 19:41:05 +00:00
|
|
|
Accounts: ethGenAccounts,
|
|
|
|
TxsLogs: k.GetAllTxLogs(ctx),
|
|
|
|
ChainConfig: config,
|
|
|
|
Params: k.GetParams(ctx),
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|