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 {
|
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
|
|
|
|
err = k.Finalise(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
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
|
2020-06-26 22:26:55 +00:00
|
|
|
var ethGenAccounts []types.GenesisAccount
|
2020-05-18 19:21:12 +00:00
|
|
|
accounts := ak.GetAllAccounts(ctx)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, account := range accounts {
|
2020-05-29 15:50:22 +00:00
|
|
|
ethAccount, ok := account.(*emint.EthAccount)
|
2020-05-18 19:21:12 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := common.BytesToAddress(ethAccount.GetAddress().Bytes())
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var storage []types.GenesisStorage
|
2020-05-18 19:21:12 +00:00
|
|
|
err = k.CommitStateDB.ForEachStorage(addr, func(key, value common.Hash) bool {
|
2020-06-26 22:26:55 +00:00
|
|
|
storage = append(storage, types.NewGenesisStorage(key, value))
|
2020-05-18 19:21:12 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|