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-12-15 18:43:06 +00:00
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
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"
|
2021-01-06 20:56:40 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/keeper"
|
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
|
2021-01-06 20:56:40 +00:00
|
|
|
func InitGenesis(
|
|
|
|
ctx sdk.Context,
|
|
|
|
k keeper.Keeper,
|
|
|
|
accountKeeper types.AccountKeeper, // nolint: interfacer
|
|
|
|
data GenesisState,
|
|
|
|
) []abci.ValidatorUpdate {
|
2021-01-04 13:41:38 +00:00
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
k.SetParams(ctx, data.Params)
|
2020-11-27 18:42:04 +00:00
|
|
|
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)
|
|
|
|
|
2020-12-15 18:43:06 +00:00
|
|
|
k.SetNonce(ctx, address, acc.GetSequence())
|
|
|
|
k.SetBalance(ctx, address, evmBalance.BigInt())
|
2021-01-06 20:56:40 +00:00
|
|
|
k.SetCode(ctx, address, ethcmn.Hex2Bytes(account.Code))
|
2020-12-15 18:43:06 +00:00
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, storage := range account.Storage {
|
2021-01-06 20:56:40 +00:00
|
|
|
k.SetState(ctx, address, ethcmn.HexToHash(storage.Key), ethcmn.HexToHash(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 {
|
2021-01-06 20:56:40 +00:00
|
|
|
if err = k.SetLogs(ctx, ethcmn.HexToHash(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)
|
|
|
|
|
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
|
2021-01-06 20:56:40 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k keeper.Keeper, ak types.AccountKeeper) GenesisState {
|
2020-05-18 19:21:12 +00:00
|
|
|
// nolint: prealloc
|
2020-06-26 22:26:55 +00:00
|
|
|
var ethGenAccounts []types.GenesisAccount
|
2020-12-15 18:43:06 +00:00
|
|
|
ak.IterateAccounts(ctx, func(account authexported.Account) bool {
|
2020-11-27 18:42:04 +00:00
|
|
|
ethAccount, ok := account.(*ethermint.EthAccount)
|
2020-05-18 19:21:12 +00:00
|
|
|
if !ok {
|
2020-12-15 18:43:06 +00:00
|
|
|
// ignore non EthAccounts
|
|
|
|
return false
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
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-06-26 22:26:55 +00:00
|
|
|
genAccount := types.GenesisAccount{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: addr.String(),
|
2021-01-06 20:56:40 +00:00
|
|
|
Code: ethcmn.Bytes2Hex(k.GetCode(ctx, addr)),
|
2020-05-18 19:21:12 +00:00
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
|
|
|
|
ethGenAccounts = append(ethGenAccounts, genAccount)
|
2020-12-15 18:43:06 +00:00
|
|
|
return false
|
|
|
|
})
|
2020-05-18 19:21:12 +00:00
|
|
|
|
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
|
|
|
}
|