2019-07-08 16:02:20 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
2020-05-18 19:21:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
2019-07-08 16:02:20 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
|
|
|
emint "github.com/cosmos/ethermint/types"
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
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-03-09 13:17:23 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) []abci.ValidatorUpdate {
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, account := range data.Accounts {
|
|
|
|
csdb := k.CommitStateDB.WithContext(ctx)
|
2020-05-28 21:22:41 +00:00
|
|
|
// FIXME: this will override bank InitGenesis balance!
|
2020-05-18 19:21:12 +00:00
|
|
|
csdb.SetBalance(account.Address, account.Balance)
|
|
|
|
csdb.SetCode(account.Address, account.Code)
|
|
|
|
for _, storage := range account.Storage {
|
|
|
|
csdb.SetState(account.Address, storage.Key, storage.Value)
|
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2020-05-28 21:22:41 +00:00
|
|
|
// TODO: Commit?
|
2019-07-25 20:38:55 +00:00
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
// ExportGenesis exports genesis state
|
2020-05-18 19:21:12 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisState {
|
|
|
|
// nolint: prealloc
|
|
|
|
var ethGenAccounts []GenesisAccount
|
|
|
|
accounts := ak.GetAllAccounts(ctx)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, account := range accounts {
|
|
|
|
ethAccount, ok := account.(emint.EthAccount)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := common.BytesToAddress(ethAccount.GetAddress().Bytes())
|
|
|
|
|
|
|
|
var storage []GenesisStorage
|
|
|
|
err = k.CommitStateDB.ForEachStorage(addr, func(key, value common.Hash) bool {
|
|
|
|
storage = append(storage, NewGenesisStorage(key, value))
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genAccount := GenesisAccount{
|
|
|
|
Address: addr,
|
|
|
|
Balance: k.GetBalance(ctx, addr),
|
|
|
|
Code: k.GetCode(ctx, addr),
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
|
|
|
|
ethGenAccounts = append(ethGenAccounts, genAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenesisState{Accounts: ethGenAccounts}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|