laconicd-deprecated/x/evm/genesis.go

118 lines
3.7 KiB
Go
Raw Normal View History

// 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
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)
chore(evm): Deprecate x/params usage in x/evm (#1472) * imp(evm): Migrate from old Cosmos SDK params module to new way of keeping params in module Keeper * Updated changelog * Apply changes from code review * (impv): Added Shanghai and Cancun blocks to current types and latest migration * (tests): Update unit tests to include Shanghai and Cancun blocks * (fix) - ran golangci-lint on the entire project * (fix) - remove deprecated params method * (impv): added marshaling of booleans per individual param key * (impv): added individual param getting and setting * (impv): replaced getters with individual param * (impv): added amino codec for MsgEthereumTx * Added changes suggested in code review * (fix): updated the migration files for v4 * (fix): fixed unit tests panic for incorrect interface * (fix): updated module msg handler * (fix): rename to original params getter method * (refactor): registered implementation for the new msg * (refactor): added correct amino codec for MsgUpdateParams and removed for MsgEthTx * Applied changes from code review * (fix): removed unnecessary duplicate * (fix): removed params_legacy from the types and moved logic to migration * (fix): Added v4 mocks to the migrations_test * (fix): undo all the non related work regarding the Cancun and Shanghai blocks * (fix): reverted linting the entire project - will make a separate PR for it * Applied changes from review * Applied changes from code review * (fix): removed comments * (fix): Ran formatter and fixed linting issues on unsed functions * (fix): Linting issues resolved * (fix): refactor migrations and added default EIPs * (fix): Combined into one call * (fix): Added more straightforward way to handle migration * (fix): corrected migration test * Applied changes from code review * (fix): Linter fix * (fix): Linter * (fix): Lint proto files * Apply suggestions from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> * (fix): Added new block to migration * (fix): Added additional comments and formatted proto files * (fix): Added name to unit test cases * (fix): removed unused import * Apply changes from code review * (fix): typo * (fix): remove HTTP endpoint exposure * Apply suggestions from code review Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * applied changes from code review * fix: extra line added in merge removed * fix: applied changes from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
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))
}
// 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)
// 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))
}
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),
}
}