2020-10-20 17:53:13 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2021-05-14 21:44:58 +00:00
|
|
|
"time"
|
2020-10-20 17:53:13 +00:00
|
|
|
|
2020-12-09 19:11:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-10-20 17:53:13 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-05-14 21:44:58 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-05-14 21:44:58 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2020-10-20 17:53:13 +00:00
|
|
|
)
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
// BeginBlock sets the block hash -> block height map for the previous block height
|
2020-10-20 17:53:13 +00:00
|
|
|
// and resets the Bloom filter and the transaction count to 0.
|
|
|
|
func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
|
2021-05-14 21:44:58 +00:00
|
|
|
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
|
2021-05-31 14:54:59 +00:00
|
|
|
k.WithContext(ctx)
|
2021-05-14 21:44:58 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
if req.Header.Height < 1 {
|
2020-10-20 17:53:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gas costs are handled within msg handler so costs should be ignored
|
|
|
|
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
k.headerHash = common.BytesToHash(req.Hash)
|
2020-12-09 19:11:15 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
// set height -> hash
|
|
|
|
|
|
|
|
// TODO: prune
|
|
|
|
k.SetHeaderHash(ctx, req.Header.Height, k.headerHash)
|
2020-10-20 17:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-12-07 20:09:09 +00:00
|
|
|
// the store. The EVM end block logic doesn't update the validator set, thus it returns
|
2020-10-20 17:53:13 +00:00
|
|
|
// an empty slice.
|
|
|
|
func (k Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
|
2021-05-14 21:44:58 +00:00
|
|
|
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
|
|
|
|
|
2020-10-20 17:53:13 +00:00
|
|
|
// Gas costs are handled within msg handler so costs should be ignored
|
|
|
|
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
2021-05-25 12:56:36 +00:00
|
|
|
k.CommitStateDB.WithContext(ctx)
|
2021-05-27 09:26:01 +00:00
|
|
|
k.WithContext(ctx)
|
2020-10-20 17:53:13 +00:00
|
|
|
|
|
|
|
// Update account balances before committing other parts of state
|
2021-05-25 12:56:36 +00:00
|
|
|
k.CommitStateDB.UpdateAccounts()
|
2020-10-20 17:53:13 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
root, err := k.CommitStateDB.Commit(true)
|
2020-10-20 17:53:13 +00:00
|
|
|
// Commit state objects to KV store
|
2020-12-30 15:39:21 +00:00
|
|
|
if err != nil {
|
2020-12-07 20:09:09 +00:00
|
|
|
k.Logger(ctx).Error("failed to commit state objects", "error", err, "height", ctx.BlockHeight())
|
2020-10-20 17:53:13 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:39:21 +00:00
|
|
|
// reset all cache after account data has been committed, that make sure node state consistent
|
2021-05-25 12:56:36 +00:00
|
|
|
if err = k.CommitStateDB.Reset(root); err != nil {
|
2020-12-30 15:39:21 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-20 17:53:13 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// get the block bloom bytes from the transient store and set it to the persistent storage
|
|
|
|
bloomBig, found := k.GetBlockBloomTransient()
|
|
|
|
if !found {
|
|
|
|
bloomBig = big.NewInt(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
bloom := ethtypes.BytesToBloom(bloomBig.Bytes())
|
2020-10-20 17:53:13 +00:00
|
|
|
k.SetBlockBloom(ctx, req.Height, bloom)
|
|
|
|
|
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|