2020-10-20 17:53:13 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
|
2020-12-09 19:11:15 +00:00
|
|
|
// Set the hash -> height and height -> hash mapping.
|
|
|
|
hash := req.Header.LastBlockId.GetHash()
|
|
|
|
height := req.Header.GetHeight() - 1
|
|
|
|
|
|
|
|
k.SetHeightHash(ctx, uint64(height), common.BytesToHash(hash))
|
|
|
|
k.SetBlockHash(ctx, hash, height)
|
2020-10-20 17:53:13 +00:00
|
|
|
|
|
|
|
// 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
|
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 {
|
|
|
|
// 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)
|
|
|
|
|
2020-12-30 15:39:21 +00:00
|
|
|
root, err := k.Commit(ctx, 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
|
|
|
|
if err = k.Reset(ctx, root); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-20 17:53:13 +00:00
|
|
|
|
|
|
|
// set the block bloom filter bytes to store
|
|
|
|
bloom := ethtypes.BytesToBloom(k.Bloom.Bytes())
|
|
|
|
k.SetBlockBloom(ctx, req.Height, bloom)
|
|
|
|
|
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|