Skip to content

Commit 0c6e44d

Browse files
authored
evm: remove CommitStateDB, journal and stateObject (#84)
* evm: remove CommitStateDB and stateObject * imported build fixes * lint * rm set nonce * update account response * changelog
1 parent 6eadc8f commit 0c6e44d

32 files changed

Lines changed: 269 additions & 4259 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
### State Machine Breaking
4141

42+
* (evm) [tharsis#84](https://github.com/tharsis/ethermint/pull/84) Remove `journal`, `CommitStateDB` and `stateObjects`.
4243
* (rpc, evm) [tharsis#81](https://github.com/tharsis/ethermint/pull/81) Remove tx `Receipt` from store and replace it with fields obtained from the Tendermint RPC client.
4344
* (evm) [tharsis#72](https://github.com/tharsis/ethermint/issues/72) Update `AccessList` to use `TransientStore` instead of map.
4445
* (evm) [tharsis#68](https://github.com/tharsis/ethermint/issues/68) Replace block hash storage map to use staking `HistoricalInfo`.

app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func NewEthermintApp(
385385
params.NewAppModule(app.ParamsKeeper),
386386
transferModule,
387387
// Ethermint app modules
388-
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper, app.BankKeeper),
388+
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper),
389389
)
390390

391391
// During begin block slashing happens after distr.BeginBlocker so that

docs/core/proto-docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ QueryAccountResponse is the response type for the Query/Account RPC method.
427427
| Field | Type | Label | Description |
428428
| ----- | ---- | ----- | ----------- |
429429
| `balance` | [string](#string) | | balance is the balance of the EVM denomination. |
430-
| `code_hash` | [bytes](#bytes) | | code_hash is the code bytes from the EOA. |
430+
| `code_hash` | [string](#string) | | code hash is the hex-formatted code bytes from the EOA. |
431431
| `nonce` | [uint64](#uint64) | | nonce is the account's sequence number. |
432432

433433

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/tyler-smith/go-bip39 v1.1.0
4646
github.com/xlab/closer v0.0.0-20190328110542-03326addb7c2
4747
github.com/xlab/suplog v1.3.0
48-
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
48+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
4949
google.golang.org/genproto v0.0.0-20210607140030-00d4fb20b1ae
5050
google.golang.org/grpc v1.38.0
5151
gopkg.in/yaml.v2 v2.4.0

proto/ethermint/evm/v1alpha1/query.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ message QueryAccountRequest {
7575
message QueryAccountResponse {
7676
// balance is the balance of the EVM denomination.
7777
string balance = 1;
78-
// code_hash is the code bytes from the EOA.
79-
bytes code_hash = 2;
78+
// code hash is the hex-formatted code bytes from the EOA.
79+
string code_hash = 2;
8080
// nonce is the account's sequence number.
8181
uint64 nonce = 3;
8282
}

tests/importer/importer_test.go

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak authkeeper.
107107
genBlock := ethcore.DefaultGenesisBlock()
108108
ms := cms.CacheMultiStore()
109109
ctx := sdk.NewContext(ms, tmproto.Header{}, false, logger)
110-
evmKeeper.CommitStateDB.WithContext(ctx)
110+
evmKeeper.WithContext(ctx)
111111

112112
// Set the default Ethermint parameters to the parameter keeper store
113113
evmKeeper.SetParams(ctx, evmtypes.DefaultParams())
@@ -126,25 +126,19 @@ func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak authkeeper.
126126
addr := ethcmn.HexToAddress(addrStr)
127127
acc := genBlock.Alloc[addr]
128128

129-
evmKeeper.CommitStateDB.AddBalance(addr, acc.Balance)
130-
evmKeeper.CommitStateDB.SetCode(addr, acc.Code)
131-
evmKeeper.CommitStateDB.SetNonce(addr, acc.Nonce)
129+
evmKeeper.AddBalance(addr, acc.Balance)
130+
evmKeeper.SetCode(addr, acc.Code)
131+
evmKeeper.SetNonce(addr, acc.Nonce)
132132

133133
for key, value := range acc.Storage {
134-
evmKeeper.CommitStateDB.SetState(addr, key, value)
134+
evmKeeper.SetState(addr, key, value)
135135
}
136136
}
137137

138138
// get balance of one of the genesis account having 400 ETH
139-
b := evmKeeper.CommitStateDB.GetBalance(genInvestor)
139+
b := evmKeeper.GetBalance(genInvestor)
140140
require.Equal(t, "200000000000000000000", b.String())
141141

142-
// commit the stateDB with 'false' to delete empty objects
143-
//
144-
// NOTE: Commit does not yet return the intra merkle root (version)
145-
_, err := evmKeeper.CommitStateDB.Commit(false)
146-
require.NoError(t, err)
147-
148142
// persist multi-store cache state
149143
ms.Write()
150144

@@ -267,15 +261,13 @@ func TestImportBlocks(t *testing.T) {
267261
ms := cms.CacheMultiStore()
268262
ctx := sdk.NewContext(ms, tmproto.Header{}, false, logger)
269263
ctx = ctx.WithBlockHeight(int64(block.NumberU64()))
270-
evmKeeper.CommitStateDB.WithContext(ctx)
264+
evmKeeper.WithContext(ctx)
271265

272266
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
273267
applyDAOHardFork(evmKeeper)
274268
}
275269

276-
for i, tx := range block.Transactions() {
277-
evmKeeper.CommitStateDB.Prepare(tx.Hash(), block.Hash(), i)
278-
// evmKeeper.CommitStateDB.Set(block.Hash())
270+
for _, tx := range block.Transactions() {
279271

280272
receipt, gas, err := applyTransaction(
281273
chainConfig, chainContext, nil, gp, evmKeeper, header, tx, usedGas, vmConfig,
@@ -287,10 +279,6 @@ func TestImportBlocks(t *testing.T) {
287279
// apply mining rewards
288280
accumulateRewards(chainConfig, evmKeeper, header, block.Uncles())
289281

290-
// commit stateDB
291-
_, err := evmKeeper.CommitStateDB.Commit(chainConfig.IsEIP158(block.Number()))
292-
require.NoError(t, err, "failed to commit StateDB")
293-
294282
// simulate BaseApp EndBlocker commitment
295283
ms.Write()
296284
cms.Commit()
@@ -325,12 +313,12 @@ func accumulateRewards(
325313
r.Sub(r, header.Number)
326314
r.Mul(r, blockReward)
327315
r.Div(r, rewardBig8)
328-
evmKeeper.CommitStateDB.AddBalance(uncle.Coinbase, r)
316+
evmKeeper.AddBalance(uncle.Coinbase, r)
329317
r.Div(blockReward, rewardBig32)
330318
reward.Add(reward, r)
331319
}
332320

333-
evmKeeper.CommitStateDB.AddBalance(header.Coinbase, reward)
321+
evmKeeper.AddBalance(header.Coinbase, reward)
334322
}
335323

336324
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
@@ -341,14 +329,13 @@ func accumulateRewards(
341329
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
342330
func applyDAOHardFork(evmKeeper *evmkeeper.Keeper) {
343331
// Retrieve the contract to refund balances into
344-
if !evmKeeper.CommitStateDB.Exist(ethparams.DAORefundContract) {
345-
evmKeeper.CommitStateDB.CreateAccount(ethparams.DAORefundContract)
332+
if !evmKeeper.Exist(ethparams.DAORefundContract) {
333+
evmKeeper.CreateAccount(ethparams.DAORefundContract)
346334
}
347335

348336
// Move every DAO account and extra-balance account funds into the refund contract
349337
for _, addr := range ethparams.DAODrainList() {
350-
evmKeeper.CommitStateDB.AddBalance(ethparams.DAORefundContract, evmKeeper.CommitStateDB.GetBalance(addr))
351-
evmKeeper.CommitStateDB.SetBalance(addr, new(big.Int))
338+
evmKeeper.AddBalance(ethparams.DAORefundContract, evmKeeper.GetBalance(addr))
352339
}
353340
}
354341

@@ -374,7 +361,7 @@ func applyTransaction(
374361

375362
// Create a new environment which holds all relevant information
376363
// about the transaction and calling mechanisms.
377-
vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper.CommitStateDB, config, cfg)
364+
vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper, config, cfg)
378365

379366
// Apply the transaction to the current state (included in the env)
380367
execResult, err := ethcore.ApplyMessage(vmenv, msg, gp)
@@ -383,19 +370,11 @@ func applyTransaction(
383370
return &ethtypes.Receipt{}, 0, nil
384371
}
385372

386-
// Update the state with pending changes
387-
var intRoot ethcmn.Hash
388-
if config.IsByzantium(header.Number) {
389-
err = evmKeeper.CommitStateDB.Finalise(true)
390-
} else {
391-
intRoot, err = evmKeeper.CommitStateDB.IntermediateRoot(config.IsEIP158(header.Number))
392-
}
393-
394373
if err != nil {
395374
return nil, execResult.UsedGas, err
396375
}
397376

398-
root := intRoot.Bytes()
377+
root := ethcmn.Hash{}.Bytes()
399378
*usedGas += execResult.UsedGas
400379

401380
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
@@ -410,11 +389,11 @@ func applyTransaction(
410389
}
411390

412391
// Set the receipt logs and create a bloom for filtering
413-
receipt.Logs, err = evmKeeper.CommitStateDB.GetLogs(tx.Hash())
392+
receipt.Logs = evmKeeper.GetTxLogs(tx.Hash())
414393
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})
415-
receipt.BlockHash = evmKeeper.CommitStateDB.BlockHash()
394+
receipt.BlockHash = header.Hash()
416395
receipt.BlockNumber = header.Number
417-
receipt.TransactionIndex = uint(evmKeeper.CommitStateDB.TxIndex())
396+
receipt.TransactionIndex = uint(evmKeeper.GetTxIndexTransient())
418397

419398
return receipt, execResult.UsedGas, err
420399
}

x/evm/genesis.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ func InitGenesis(
1818
ctx sdk.Context,
1919
k *keeper.Keeper,
2020
accountKeeper types.AccountKeeper, // nolint: interfacer
21-
bankKeeper types.BankKeeper,
2221
data types.GenesisState,
2322
) []abci.ValidatorUpdate {
2423
k.WithContext(ctx)
2524
k.WithChainID(ctx)
2625

27-
k.CommitStateDB.WithContext(ctx)
28-
2926
k.SetParams(ctx, data.Params)
30-
evmDenom := data.Params.EvmDenom
3127

3228
for _, account := range data.Accounts {
3329
address := ethcmn.HexToAddress(account.Address)
@@ -47,46 +43,25 @@ func InitGenesis(
4743
)
4844
}
4945

50-
evmBalance := bankKeeper.GetBalance(ctx, accAddress, evmDenom)
51-
k.CommitStateDB.SetBalance(address, evmBalance.Amount.BigInt())
52-
k.CommitStateDB.SetNonce(address, acc.GetSequence())
53-
k.CommitStateDB.SetCode(address, ethcmn.Hex2Bytes(account.Code))
46+
k.SetCode(address, ethcmn.Hex2Bytes(account.Code))
5447

5548
for _, storage := range account.Storage {
5649
k.SetState(address, ethcmn.HexToHash(storage.Key), ethcmn.HexToHash(storage.Value))
5750
}
5851
}
5952

60-
var err error
6153
for _, txLog := range data.TxsLogs {
62-
err = k.CommitStateDB.SetLogs(ethcmn.HexToHash(txLog.Hash), txLog.EthLogs())
63-
if err != nil {
64-
panic(err)
65-
}
54+
k.SetLogs(ethcmn.HexToHash(txLog.Hash), txLog.EthLogs())
6655
}
6756

6857
k.SetChainConfig(ctx, data.ChainConfig)
6958

70-
// set state objects and code to store
71-
_, err = k.CommitStateDB.Commit(false)
72-
if err != nil {
73-
panic(err)
74-
}
75-
76-
// set storage to store
77-
// NOTE: don't delete empty object to prevent import-export simulation failure
78-
err = k.CommitStateDB.Finalise(false)
79-
if err != nil {
80-
panic(err)
81-
}
82-
8359
return []abci.ValidatorUpdate{}
8460
}
8561

8662
// ExportGenesis exports genesis state of the EVM module
8763
func ExportGenesis(ctx sdk.Context, k *keeper.Keeper, ak types.AccountKeeper) *types.GenesisState {
8864
k.WithContext(ctx)
89-
k.CommitStateDB.WithContext(ctx)
9065

9166
// nolint: prealloc
9267
var ethGenAccounts []types.GenesisAccount
@@ -106,7 +81,7 @@ func ExportGenesis(ctx sdk.Context, k *keeper.Keeper, ak types.AccountKeeper) *t
10681

10782
genAccount := types.GenesisAccount{
10883
Address: addr.String(),
109-
Code: ethcmn.Bytes2Hex(k.CommitStateDB.GetCode(addr)),
84+
Code: ethcmn.Bytes2Hex(k.GetCode(addr)),
11085
Storage: storage,
11186
}
11287

x/evm/genesis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ func (suite *EvmTestSuite) TestInitGenesis() {
9191
if tc.expPanic {
9292
suite.Require().Panics(
9393
func() {
94-
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *tc.genState)
94+
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
9595
},
9696
)
9797
} else {
9898
suite.Require().NotPanics(
9999
func() {
100-
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, suite.app.BankKeeper, *tc.genState)
100+
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
101101
},
102102
)
103103
}

x/evm/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
sdk "github.com/cosmos/cosmos-sdk/types"
99
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1010

11-
"github.com/cosmos/ethermint/x/evm/keeper"
1211
"github.com/cosmos/ethermint/x/evm/types"
1312
)
1413

1514
// NewHandler returns a handler for Ethermint type messages.
16-
func NewHandler(k *keeper.Keeper) sdk.Handler {
15+
func NewHandler(server types.MsgServer) sdk.Handler {
1716
return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) {
1817
defer Recover(&err)
1918

@@ -22,7 +21,7 @@ func NewHandler(k *keeper.Keeper) sdk.Handler {
2221
switch msg := msg.(type) {
2322
case *types.MsgEthereumTx:
2423
// execute state transition
25-
res, err := k.EthereumTx(sdk.WrapSDKContext(ctx), msg)
24+
res, err := server.EthereumTx(sdk.WrapSDKContext(ctx), msg)
2625
return sdk.WrapServiceResult(ctx, res, err)
2726

2827
default:

x/evm/handler_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (suite *EvmTestSuite) SetupTest() {
4949

5050
suite.app = app.Setup(checkTx)
5151
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint-1", Time: time.Now().UTC()})
52-
suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
52+
suite.app.EvmKeeper.WithContext(suite.ctx)
5353
suite.handler = evm.NewHandler(suite.app.EvmKeeper)
5454
suite.codec = suite.app.AppCodec()
5555
suite.chainID = suite.app.EvmKeeper.ChainID()
@@ -84,7 +84,6 @@ func (suite *EvmTestSuite) TestHandleMsgEthereumTx() {
8484
{
8585
"passed",
8686
func() {
87-
suite.app.EvmKeeper.CommitStateDB.SetBalance(suite.from, big.NewInt(100))
8887
to := ethcmn.BytesToAddress(suite.to)
8988
tx = types.NewMsgEthereumTx(suite.chainID, 0, &to, big.NewInt(100), 0, big.NewInt(10000), nil, nil)
9089
tx.From = suite.from.String()
@@ -189,11 +188,10 @@ func (suite *EvmTestSuite) TestHandlerLogs() {
189188
suite.Require().Equal(len(txResponse.Logs[0].Topics), 2)
190189

191190
hash := []byte{1}
192-
err = suite.app.EvmKeeper.CommitStateDB.SetLogs(ethcmn.BytesToHash(hash), types.LogsToEthereum(txResponse.Logs))
191+
suite.app.EvmKeeper.SetLogs(ethcmn.BytesToHash(hash), types.LogsToEthereum(txResponse.Logs))
193192
suite.Require().NoError(err)
194193

195-
logs, err := suite.app.EvmKeeper.CommitStateDB.GetLogs(ethcmn.BytesToHash(hash))
196-
suite.Require().NoError(err, "failed to get logs")
194+
logs := suite.app.EvmKeeper.GetTxLogs(ethcmn.BytesToHash(hash))
197195

198196
suite.Require().Equal(logs, txResponse.Logs)
199197
}
@@ -310,8 +308,6 @@ func (suite *EvmTestSuite) TestSendTransaction() {
310308
gasLimit := uint64(21000)
311309
gasPrice := big.NewInt(0x55ae82600)
312310

313-
suite.app.EvmKeeper.CommitStateDB.SetBalance(suite.from, big.NewInt(100))
314-
315311
// send simple value transfer with gasLimit=21000
316312
tx := types.NewMsgEthereumTx(suite.chainID, 1, &ethcmn.Address{0x1}, big.NewInt(1), gasLimit, gasPrice, nil, nil)
317313
tx.From = suite.from.String()
@@ -390,12 +386,12 @@ func (suite *EvmTestSuite) TestOutOfGasWhenDeployContract() {
390386
err := tx.Sign(suite.ethSigner, suite.signer)
391387
suite.Require().NoError(err)
392388

393-
snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper.CommitStateDB)
389+
snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper)
394390
suite.Require().Nil(err)
395391

396392
defer func() {
397393
if r := recover(); r != nil {
398-
currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper.CommitStateDB)
394+
currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper)
399395
suite.Require().Nil(err)
400396
suite.Require().Equal(snapshotCommitStateDBJson, currentCommitStateDBJson)
401397
} else {
@@ -419,13 +415,13 @@ func (suite *EvmTestSuite) TestErrorWhenDeployContract() {
419415
err := tx.Sign(suite.ethSigner, suite.signer)
420416
suite.Require().NoError(err)
421417

422-
snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper.CommitStateDB)
418+
snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper)
423419
suite.Require().Nil(err)
424420

425421
_, sdkErr := suite.handler(suite.ctx, tx)
426422
suite.Require().NotNil(sdkErr)
427423

428-
currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper.CommitStateDB)
424+
currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper)
429425
suite.Require().Nil(err)
430426
suite.Require().Equal(snapshotCommitStateDBJson, currentCommitStateDBJson)
431427
}

0 commit comments

Comments
 (0)