2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2019-07-08 16:02:20 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
2022-01-04 14:21:32 +00:00
|
|
|
"bytes"
|
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"
|
2022-01-04 14:21:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-04-18 15:54:18 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
|
|
|
"github.com/cerc-io/laconicd/x/evm/keeper"
|
|
|
|
"github.com/cerc-io/laconicd/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.WithChainID(ctx)
|
|
|
|
|
2023-01-04 14:28:45 +00:00
|
|
|
err := k.SetParams(ctx, data.Params)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error setting params %s", err))
|
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
ethAcct, ok := acc.(ethermint.EthAccountI)
|
2020-11-27 18:42:04 +00:00
|
|
|
if !ok {
|
|
|
|
panic(
|
2022-01-05 18:18:02 +00:00
|
|
|
fmt.Errorf("account %s must be an EthAccount interface, got %T",
|
|
|
|
account.Address, acc,
|
2020-11-27 18:42:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-01-04 14:21:32 +00:00
|
|
|
code := common.Hex2Bytes(account.Code)
|
|
|
|
codeHash := crypto.Keccak256Hash(code)
|
2022-10-19 12:29:24 +00:00
|
|
|
|
|
|
|
// we ignore the empty Code hash checking, see ethermint PR#1234
|
|
|
|
if len(account.Code) != 0 && !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) {
|
|
|
|
s := "the evm state code doesn't match with the codehash\n"
|
|
|
|
panic(fmt.Sprintf("%s account: %s , evm state codehash: %v, ethAccount codehash: %v, evm state code: %s\n",
|
|
|
|
s, account.Address, codeHash, ethAcct.GetCodeHash(), account.Code))
|
2022-01-04 14:21:32 +00:00
|
|
|
}
|
2022-01-05 18:18:02 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
k.SetCode(ctx, codeHash.Bytes(), code)
|
2020-12-15 18:43:06 +00:00
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, storage := range account.Storage {
|
2022-01-05 07:28:27 +00:00
|
|
|
k.SetState(ctx, address, common.HexToHash(storage.Key), common.HexToHash(storage.Value).Bytes())
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
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 {
|
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 {
|
2022-01-05 18:18:02 +00:00
|
|
|
ethAccount, ok := account.(ethermint.EthAccountI)
|
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
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
storage := k.GetAccountStorage(ctx, addr)
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
genAccount := types.GenesisAccount{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: addr.String(),
|
2022-01-05 07:28:27 +00:00
|
|
|
Code: common.Bytes2Hex(k.GetCode(ctx, ethAccount.GetCodeHash())),
|
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,
|
|
|
|
Params: k.GetParams(ctx),
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|