2021-08-26 10:08:11 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2022-09-23 16:06:25 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-08-26 10:08:11 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2022-07-28 13:43:49 +00:00
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
2021-08-26 10:08:11 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
2022-09-23 16:06:25 +00:00
|
|
|
v010 "github.com/evmos/ethermint/x/feemarket/migrations/v010"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/x/feemarket/types"
|
2021-08-26 10:08:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper grants access to the Fee Market module state.
|
|
|
|
type Keeper struct {
|
|
|
|
// Protobuf codec
|
|
|
|
cdc codec.BinaryCodec
|
|
|
|
// Store key required for the Fee Market Prefix KVStore.
|
2022-07-28 13:43:49 +00:00
|
|
|
storeKey storetypes.StoreKey
|
|
|
|
transientKey storetypes.StoreKey
|
2021-08-26 10:08:11 +00:00
|
|
|
// module specific parameter space that can be configured through governance
|
|
|
|
paramSpace paramtypes.Subspace
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper generates new fee market module keeper
|
|
|
|
func NewKeeper(
|
2022-07-28 13:43:49 +00:00
|
|
|
cdc codec.BinaryCodec, paramSpace paramtypes.Subspace, storeKey, transientKey storetypes.StoreKey,
|
2021-08-26 10:08:11 +00:00
|
|
|
) Keeper {
|
|
|
|
// set KeyTable if it has not already been set
|
|
|
|
if !paramSpace.HasKeyTable() {
|
|
|
|
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
2022-06-05 09:22:33 +00:00
|
|
|
cdc: cdc,
|
|
|
|
storeKey: storeKey,
|
|
|
|
paramSpace: paramSpace,
|
|
|
|
transientKey: transientKey,
|
2021-08-26 10:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger returns a module-specific logger.
|
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", types.ModuleName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Parent Block Gas Used
|
|
|
|
// Required by EIP1559 base fee calculation.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2022-06-05 09:22:33 +00:00
|
|
|
// SetBlockGasWanted sets the block gas wanted to the store.
|
|
|
|
// CONTRACT: this should be only called during EndBlock.
|
|
|
|
func (k Keeper) SetBlockGasWanted(ctx sdk.Context, gas uint64) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
gasBz := sdk.Uint64ToBigEndian(gas)
|
|
|
|
store.Set(types.KeyPrefixBlockGasWanted, gasBz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockGasWanted returns the last block gas wanted value from the store.
|
|
|
|
func (k Keeper) GetBlockGasWanted(ctx sdk.Context) uint64 {
|
2021-08-26 10:08:11 +00:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2022-06-05 09:22:33 +00:00
|
|
|
bz := store.Get(types.KeyPrefixBlockGasWanted)
|
2021-08-26 10:08:11 +00:00
|
|
|
if len(bz) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.BigEndianToUint64(bz)
|
|
|
|
}
|
|
|
|
|
2022-06-05 09:22:33 +00:00
|
|
|
// GetTransientGasWanted returns the gas wanted in the current block from transient store.
|
|
|
|
func (k Keeper) GetTransientGasWanted(ctx sdk.Context) uint64 {
|
|
|
|
store := ctx.TransientStore(k.transientKey)
|
|
|
|
bz := store.Get(types.KeyPrefixTransientBlockGasWanted)
|
|
|
|
if len(bz) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return sdk.BigEndianToUint64(bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTransientBlockGasWanted sets the block gas wanted to the transient store.
|
|
|
|
func (k Keeper) SetTransientBlockGasWanted(ctx sdk.Context, gasWanted uint64) {
|
|
|
|
store := ctx.TransientStore(k.transientKey)
|
|
|
|
gasBz := sdk.Uint64ToBigEndian(gasWanted)
|
|
|
|
store.Set(types.KeyPrefixTransientBlockGasWanted, gasBz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTransientGasWanted adds the cumulative gas wanted in the transient store
|
|
|
|
func (k Keeper) AddTransientGasWanted(ctx sdk.Context, gasWanted uint64) (uint64, error) {
|
|
|
|
result := k.GetTransientGasWanted(ctx) + gasWanted
|
|
|
|
k.SetTransientBlockGasWanted(ctx, result)
|
|
|
|
return result, nil
|
2021-08-26 10:08:11 +00:00
|
|
|
}
|
2022-09-23 16:06:25 +00:00
|
|
|
|
|
|
|
// GetBaseFeeV1 get the base fee from v1 version of states.
|
|
|
|
// return nil if base fee is not enabled
|
|
|
|
func (k Keeper) GetBaseFeeV1(ctx sdk.Context) *big.Int {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
bz := store.Get(v010.KeyPrefixBaseFeeV1)
|
|
|
|
if len(bz) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return new(big.Int).SetBytes(bz)
|
|
|
|
}
|