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 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (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`) [\#255](https://github.com/ChainSafe/ethermint/pull/255) Add `ExportGenesis` functionality.

### Features

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

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
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 _, key := range account.Storage {
csdb.SetState(account.Address, key, account.Storage[key])
}
}
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 {
var ethGenAccounts []types.GenesisAccount
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

accounts := ak.GetAllAccounts(ctx)

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

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

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

genAccount := types.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)
}
11 changes: 7 additions & 4 deletions x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ func HandleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) sdk
k.Bloom.Or(k.Bloom, returnData.Bloom)

// update transaction logs in KVStore
err = k.SetTransactionLogs(ctx, returnData.Logs, txHash[:])
if err != nil {
return sdk.ResultFromError(err)
}
k.SetTransactionLogs(ctx, txHash[:], returnData.Logs)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
Expand Down Expand Up @@ -145,6 +142,12 @@ func HandleMsgEthermint(ctx sdk.Context, k Keeper, msg types.MsgEthermint) sdk.R
return sdk.ResultFromError(err)
}

// update block bloom filter
k.Bloom.Or(k.Bloom, returnData.Bloom)

// update transaction logs in KVStore
k.SetTransactionLogs(ctx, txHash[:], returnData.Logs)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthermint,
Expand Down
2 changes: 1 addition & 1 deletion x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (suite *EvmTestSuite) TestHandler_Logs() {
suite.Require().Equal(len(resultData.Logs[0].Topics), 2)

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

logs, err := suite.app.EvmKeeper.GetTransactionLogs(suite.ctx, hash)
Expand Down
Loading