laconicd/x/evm/genesis.go

101 lines
2.5 KiB
Go
Raw Normal View History

package evm
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
2021-04-17 10:00:07 +00:00
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
ethcmn "github.com/ethereum/go-ethereum/common"
2021-04-18 15:54:18 +00:00
abci "github.com/tendermint/tendermint/abci/types"
ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/keeper"
"github.com/tharsis/ethermint/x/evm/types"
)
// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(
ctx sdk.Context,
k *keeper.Keeper,
accountKeeper types.AccountKeeper, // nolint: interfacer
2021-04-17 10:00:07 +00:00
data types.GenesisState,
) []abci.ValidatorUpdate {
k.WithContext(ctx)
k.WithChainID(ctx)
k.SetParams(ctx, data.Params)
for _, account := range data.Accounts {
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, &ethermint.EthAccount{}, acc,
),
)
}
k.SetCode(address, ethcmn.Hex2Bytes(account.Code))
for _, storage := range account.Storage {
k.SetState(address, ethcmn.HexToHash(storage.Key), ethcmn.HexToHash(storage.Value))
}
}
for _, txLog := range data.TxsLogs {
k.SetLogs(ethcmn.HexToHash(txLog.Hash), txLog.EthLogs())
}
k.SetChainConfig(ctx, data.ChainConfig)
return []abci.ValidatorUpdate{}
}
2020-07-23 19:38:47 +00:00
// ExportGenesis exports genesis state of the EVM module
func ExportGenesis(ctx sdk.Context, k *keeper.Keeper, ak types.AccountKeeper) *types.GenesisState {
k.WithContext(ctx)
// nolint: prealloc
var ethGenAccounts []types.GenesisAccount
2021-04-17 10:00:07 +00:00
ak.IterateAccounts(ctx, func(account authtypes.AccountI) bool {
ethAccount, ok := account.(*ethermint.EthAccount)
if !ok {
// ignore non EthAccounts
return false
}
addr := ethAccount.EthAddress()
storage, err := k.GetAccountStorage(ctx, addr)
if err != nil {
panic(err)
}
genAccount := types.GenesisAccount{
Address: addr.String(),
Code: ethcmn.Bytes2Hex(k.GetCode(addr)),
Storage: storage,
}
ethGenAccounts = append(ethGenAccounts, genAccount)
return false
})
config, _ := k.GetChainConfig(ctx)
2021-04-17 10:00:07 +00:00
return &types.GenesisState{
Accounts: ethGenAccounts,
TxsLogs: k.GetAllTxLogs(ctx),
ChainConfig: config,
Params: k.GetParams(ctx),
}
}