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"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-04-18 15:54:18 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/keeper"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/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,
|
2021-05-31 09:05:32 +00:00
|
|
|
k *keeper.Keeper,
|
2021-09-05 11:03:06 +00:00
|
|
|
accountKeeper types.AccountKeeper,
|
2021-04-17 10:00:07 +00:00
|
|
|
data types.GenesisState,
|
2021-01-06 20:56:40 +00:00
|
|
|
) []abci.ValidatorUpdate {
|
2021-05-31 09:05:32 +00:00
|
|
|
k.WithContext(ctx)
|
|
|
|
k.WithChainID(ctx)
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
k.SetParams(ctx, data.Params)
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
// ensure evm module account is set
|
|
|
|
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
|
|
|
|
panic("the EVM module account has not been set")
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, account := range data.Accounts {
|
2021-09-03 18:06:36 +00:00
|
|
|
address := common.HexToAddress(account.Address)
|
2020-11-27 18:42:04 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
k.SetCode(address, common.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-09-03 18:06:36 +00:00
|
|
|
k.SetState(address, common.HexToHash(storage.Key), common.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
|
|
|
|
|
|
|
for _, txLog := range data.TxsLogs {
|
2021-09-03 18:06:36 +00:00
|
|
|
k.SetLogs(common.HexToHash(txLog.Hash), txLog.EthLogs())
|
2020-06-04 10:40:21 +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-05-31 09:05:32 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k *keeper.Keeper, ak types.AccountKeeper) *types.GenesisState {
|
|
|
|
k.WithContext(ctx)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var ethGenAccounts []types.GenesisAccount
|
2021-04-17 10:00:07 +00:00
|
|
|
ak.IterateAccounts(ctx, func(account authtypes.AccountI) 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-09-03 18:06:36 +00:00
|
|
|
Code: common.Bytes2Hex(k.GetCode(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
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return &types.GenesisState{
|
2021-07-14 09:13:55 +00:00
|
|
|
Accounts: ethGenAccounts,
|
|
|
|
TxsLogs: k.GetAllTxLogs(ctx),
|
|
|
|
Params: k.GetParams(ctx),
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|