2019-07-08 16:02:20 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
|
|
|
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"
|
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-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 {
|
2020-05-28 21:22:41 +00:00
|
|
|
// FIXME: this will override bank InitGenesis balance!
|
2020-06-04 10:40:21 +00:00
|
|
|
k.SetBalance(ctx, account.Address, account.Balance)
|
|
|
|
k.SetCode(ctx, account.Address, account.Code)
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, storage := range account.Storage {
|
2020-06-04 10:40:21 +00:00
|
|
|
k.SetState(ctx, account.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 {
|
|
|
|
err = k.SetLogs(ctx, txLog.Hash, txLog.Logs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-23 21:41:54 +00:00
|
|
|
|
2020-05-29 15:50:22 +00:00
|
|
|
ethAccount, ok := account.(*emint.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-06-26 22:26:55 +00:00
|
|
|
genAccount := types.GenesisAccount{
|
2020-05-18 19:21:12 +00:00
|
|
|
Address: addr,
|
|
|
|
Balance: k.GetBalance(ctx, addr),
|
|
|
|
Code: k.GetCode(ctx, addr),
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
|
|
|
|
ethGenAccounts = append(ethGenAccounts, genAccount)
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
return GenesisState{
|
|
|
|
Accounts: ethGenAccounts,
|
|
|
|
TxsLogs: k.GetAllTxLogs(ctx),
|
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|