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)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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-01-05 18:18:02 +00:00
|
|
|
if !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) {
|
2022-01-04 14:21:32 +00:00
|
|
|
panic("code don't match codeHash")
|
|
|
|
}
|
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
|
|
|
}
|