Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

evm: add missing genesis fields and export genesis state logic #255

Merged
merged 20 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#236](https://github.com/ChainSafe/ethermint/pull/236) Changes from upgrade [@fedekunze](https://github.com/fedekunze)
* (`app/ante`) Moved `AnteHandler` implementation to `app/ante`
* (keys) Marked `ExportEthKeyCommand` as **UNSAFE**
* (`x/evm`) Moved `BeginBlock` and `EndBlock` to `x/evm/abci.go`
* (x/evm) Moved `BeginBlock` and `EndBlock` to `x/evm/abci.go`
* (`x/evm`) [\#255](https://github.com/ChainSafe/ethermint/pull/255) Add missing `GenesisState` fields and support `ExportGenesis` functionality.
* [\#272](https://github.com/ChainSafe/ethermint/pull/272) Add `Logger` for evm module.

### Features
Expand Down
2 changes: 1 addition & 1 deletion app/ethermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func NewEthermintApp(
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper, app.StakingKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
evm.NewAppModule(app.EvmKeeper),
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
7 changes: 5 additions & 2 deletions x/evm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ const (

// nolint
var (
NewKeeper = keeper.NewKeeper
TxDecoder = types.TxDecoder
NewKeeper = keeper.NewKeeper
TxDecoder = types.TxDecoder
NewGenesisStorage = types.NewGenesisStorage
)

//nolint
type (
Keeper = keeper.Keeper
QueryResAccount = types.QueryResAccount
GenesisState = types.GenesisState
GenesisAccount = types.GenesisAccount
GenesisStorage = types.GenesisStorage
)
51 changes: 46 additions & 5 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
package evm

import (
"github.com/ethereum/go-ethereum/common"

sdk "github.com/cosmos/cosmos-sdk/types"

emint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) []abci.ValidatorUpdate {
for _, record := range data.Accounts {
k.SetCode(ctx, record.Address, record.Code)
k.CreateGenesisAccount(ctx, record)
for _, account := range data.Accounts {
csdb := k.CommitStateDB.WithContext(ctx)
csdb.SetBalance(account.Address, account.Balance)
csdb.SetCode(account.Address, account.Code)
for _, storage := range account.Storage {
csdb.SetState(account.Address, storage.Key, storage.Value)
}
}
return []abci.ValidatorUpdate{}
}

// ExportGenesis exports genesis state
func ExportGenesis(ctx sdk.Context, _ Keeper) GenesisState {
return GenesisState{Accounts: nil}
func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisState {
// nolint: prealloc
var ethGenAccounts []GenesisAccount
accounts := ak.GetAllAccounts(ctx)

var err error
for _, account := range accounts {
ethAccount, ok := account.(emint.EthAccount)
if !ok {
continue
}

addr := common.BytesToAddress(ethAccount.GetAddress().Bytes())

var storage []GenesisStorage
err = k.CommitStateDB.ForEachStorage(addr, func(key, value common.Hash) bool {
storage = append(storage, NewGenesisStorage(key, value))
return false
})
if err != nil {
panic(err)
}

genAccount := GenesisAccount{
Address: addr,
Balance: k.GetBalance(ctx, addr),
Code: k.GetCode(ctx, addr),
Storage: storage,
}

ethGenAccounts = append(ethGenAccounts, genAccount)
}

return GenesisState{Accounts: ethGenAccounts}
}
15 changes: 15 additions & 0 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package evm_test

import (
"github.com/cosmos/ethermint/x/evm"
"github.com/cosmos/ethermint/x/evm/types"
)

func (suite *EvmTestSuite) TestExportImport() {
var genState types.GenesisState
suite.Require().NotPanics(func() {
genState = evm.ExportGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper)
})

_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, genState)
}
10 changes: 2 additions & 8 deletions x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func handleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) (*s
k.Bloom.Or(k.Bloom, executionResult.Bloom)

// update transaction logs in KVStore
err = k.SetTransactionLogs(ctx, executionResult.Logs, txHash)
if err != nil {
return nil, err
}
k.SetTransactionLogs(ctx, txHash, executionResult.Logs)

// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
Expand Down Expand Up @@ -150,10 +147,7 @@ func handleMsgEthermint(ctx sdk.Context, k Keeper, msg types.MsgEthermint) (*sdk
k.Bloom.Or(k.Bloom, executionResult.Bloom)

// update transaction logs in KVStore
err = k.SetTransactionLogs(ctx, executionResult.Logs, txHash)
if err != nil {
return nil, err
}
k.SetTransactionLogs(ctx, txHash, executionResult.Logs)

// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
Expand Down
3 changes: 1 addition & 2 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ func (suite *EvmTestSuite) TestHandlerLogs() {
suite.Require().Equal(len(resultData.Logs[0].Topics), 2)

hash := []byte{1}
err = suite.app.EvmKeeper.SetTransactionLogs(suite.ctx, resultData.Logs, hash)
suite.Require().NoError(err, "failed to set logs")
suite.app.EvmKeeper.SetTransactionLogs(suite.ctx, hash, resultData.Logs)

logs, err := suite.app.EvmKeeper.GetTransactionLogs(suite.ctx, hash)
suite.Require().NoError(err, "failed to get logs")
Expand Down
Loading