This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* evm: fix begin and endblock * fix tests and changelog * fix gas * update GetBlockBloom
- Loading branch information
Showing
6 changed files
with
115 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package keeper | ||
|
||
import ( | ||
"math/big" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
// BeginBlock sets the block hash -> block height map for the previous block height | ||
// and resets the Bloom filter and the transaction count to 0. | ||
func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { | ||
if req.Header.LastBlockId.GetHash() == nil || req.Header.GetHeight() < 1 { | ||
return | ||
} | ||
|
||
// Gas costs are handled within msg handler so costs should be ignored | ||
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) | ||
|
||
k.SetBlockHash(ctx, req.Header.LastBlockId.GetHash(), req.Header.GetHeight()-1) | ||
|
||
// reset counters that are used on CommitStateDB.Prepare | ||
k.Bloom = big.NewInt(0) | ||
k.TxCount = 0 | ||
} | ||
|
||
// EndBlock updates the accounts and commits state objects to the KV Store, while | ||
// deleting the empty ones. It also sets the bloom filers for the request block to | ||
// the store. The EVM end block loginc doesn't update the validator set, thus it returns | ||
// an empty slice. | ||
func (k Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { | ||
// Gas costs are handled within msg handler so costs should be ignored | ||
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) | ||
|
||
// Update account balances before committing other parts of state | ||
k.UpdateAccounts(ctx) | ||
|
||
// Commit state objects to KV store | ||
_, err := k.Commit(ctx, true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Clear accounts cache after account data has been committed | ||
k.ClearStateObjects(ctx) | ||
|
||
// set the block bloom filter bytes to store | ||
bloom := ethtypes.BytesToBloom(k.Bloom.Bytes()) | ||
k.SetBlockBloom(ctx, req.Height, bloom) | ||
|
||
return []abci.ValidatorUpdate{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package keeper_test | ||
|
||
import ( | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestBeginBlock() { | ||
req := abci.RequestBeginBlock{ | ||
Header: abci.Header{ | ||
LastBlockId: abci.BlockID{ | ||
Hash: []byte("hash"), | ||
}, | ||
Height: 10, | ||
}, | ||
} | ||
|
||
// get the initial consumption | ||
initialConsumed := suite.ctx.GasMeter().GasConsumed() | ||
|
||
// update the counters | ||
suite.app.EvmKeeper.Bloom.SetInt64(10) | ||
suite.app.EvmKeeper.TxCount = 10 | ||
|
||
suite.app.EvmKeeper.BeginBlock(suite.ctx, abci.RequestBeginBlock{}) | ||
suite.Require().NotZero(suite.app.EvmKeeper.Bloom.Int64()) | ||
suite.Require().NotZero(suite.app.EvmKeeper.TxCount) | ||
|
||
suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed())) | ||
|
||
suite.app.EvmKeeper.BeginBlock(suite.ctx, req) | ||
suite.Require().Zero(suite.app.EvmKeeper.Bloom.Int64()) | ||
suite.Require().Zero(suite.app.EvmKeeper.TxCount) | ||
|
||
suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed())) | ||
|
||
lastHeight, found := suite.app.EvmKeeper.GetBlockHash(suite.ctx, req.Header.LastBlockId.Hash) | ||
suite.Require().True(found) | ||
suite.Require().Equal(int64(9), lastHeight) | ||
} | ||
|
||
func (suite *KeeperTestSuite) TestEndBlock() { | ||
// update the counters | ||
suite.app.EvmKeeper.Bloom.SetInt64(10) | ||
|
||
// set gas limit to 1 to ensure no gas is consumed during the operation | ||
initialConsumed := suite.ctx.GasMeter().GasConsumed() | ||
|
||
_ = suite.app.EvmKeeper.EndBlock(suite.ctx, abci.RequestEndBlock{Height: 100}) | ||
|
||
suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed())) | ||
|
||
bloom, found := suite.app.EvmKeeper.GetBlockBloom(suite.ctx, 100) | ||
suite.Require().True(found) | ||
suite.Require().Equal(int64(10), bloom.Big().Int64()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters