laconicd/x/evm/genesis.go

97 lines
2.5 KiB
Go
Raw Normal View History

package evm
import (
"bytes"
"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"
2021-09-03 18:06:36 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
2021-04-18 15:54:18 +00:00
abci "github.com/tendermint/tendermint/abci/types"
ethermint "github.com/cerc-io/laconicd/types"
"github.com/cerc-io/laconicd/x/evm/keeper"
"github.com/cerc-io/laconicd/x/evm/types"
)
// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(
ctx sdk.Context,
k *keeper.Keeper,
accountKeeper types.AccountKeeper,
2021-04-17 10:00:07 +00:00
data types.GenesisState,
) []abci.ValidatorUpdate {
k.WithChainID(ctx)
k.SetParams(ctx, data.Params)
// ensure evm module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic("the EVM module account has not been set")
}
for _, account := range data.Accounts {
2021-09-03 18:06:36 +00:00
address := common.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))
}
ethAcct, ok := acc.(ethermint.EthAccountI)
if !ok {
panic(
fmt.Errorf("account %s must be an EthAccount interface, got %T",
account.Address, acc,
),
)
}
code := common.Hex2Bytes(account.Code)
codeHash := crypto.Keccak256Hash(code)
if !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) {
panic("code don't match codeHash")
}
k.SetCode(ctx, codeHash.Bytes(), code)
for _, storage := range account.Storage {
k.SetState(ctx, address, common.HexToHash(storage.Key), common.HexToHash(storage.Value).Bytes())
}
}
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 {
var ethGenAccounts []types.GenesisAccount
2021-04-17 10:00:07 +00:00
ak.IterateAccounts(ctx, func(account authtypes.AccountI) bool {
ethAccount, ok := account.(ethermint.EthAccountI)
if !ok {
// ignore non EthAccounts
return false
}
addr := ethAccount.EthAddress()
storage := k.GetAccountStorage(ctx, addr)
genAccount := types.GenesisAccount{
Address: addr.String(),
Code: common.Bytes2Hex(k.GetCode(ctx, ethAccount.GetCodeHash())),
Storage: storage,
}
ethGenAccounts = append(ethGenAccounts, genAccount)
return false
})
2021-04-17 10:00:07 +00:00
return &types.GenesisState{
Accounts: ethGenAccounts,
Params: k.GetParams(ctx),
}
}