Skip to content

Commit

Permalink
refactored queries, ante/post handlers and DenomResolver (#81)
Browse files Browse the repository at this point in the history
* refactored queries, ante/post handlers and DenomResolver

* fixed tests

* disabling Ante when params.enabled == false

* renamed AllowedDenom -> ExtraDenoms. Renamed fee entities to gasprice

* tiny fixes

* reuse variable instaed of getting it by index

* Update x/feemarket/fuzz/aimd_eip1559_test.go

Co-authored-by: Alex Johnson <[email protected]>

* Update x/feemarket/keeper/query_server.go

Co-authored-by: Alex Johnson <[email protected]>

* Update x/feemarket/keeper/query_server.go

Co-authored-by: Alex Johnson <[email protected]>

* Update x/feemarket/keeper/feemarket.go

Co-authored-by: Alex Johnson <[email protected]>

* some reviews fixes (fee -> gasPrice)

* added cli query gas-prices. updated output format for gas-price(s) queries

---------

Co-authored-by: Alex Johnson <[email protected]>
(cherry picked from commit cf0c02f)

# Conflicts:
#	tests/e2e/suite.go
  • Loading branch information
swelf19 authored and mergify[bot] committed May 29, 2024
1 parent 21a27cb commit c3fef7e
Show file tree
Hide file tree
Showing 38 changed files with 1,171 additions and 553 deletions.
2 changes: 1 addition & 1 deletion api/feemarket/feemarket/v1/genesis.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/feemarket/feemarket/v1/params.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions api/feemarket/feemarket/v1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/feemarket/feemarket/v1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions proto/feemarket/feemarket/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ message GenesisState {
// the current base fee, learning rate, and block utilization within the
// specified AIMD window.
message State {
// BaseFee is the current base fee. This is denominated in the fee per gas
// BaseGasPrice is the current base fee. This is denominated in the fee per gas
// unit.
string base_fee = 1 [
string base_gas_price = 1 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
Expand Down
6 changes: 3 additions & 3 deletions proto/feemarket/feemarket/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ message Params {
(gogoproto.nullable) = false
];

// MinBaseFee determines the initial base fee of the module and the global
// minimum for the network. This is denominated in fee per gas unit.
string min_base_fee = 5 [
// MinBaseGasPrice determines the initial gas price of the module and the global
// minimum for the network.
string min_base_gas_price = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
Expand Down
44 changes: 35 additions & 9 deletions proto/feemarket/feemarket/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ service Query {
};
};

// BaseFee returns the current feemarket module base fee.
rpc BaseFee(BaseFeeRequest) returns (BaseFeeResponse) {
// GasPrice returns the current feemarket module gas price
// for specified denom.
rpc GasPrice(GasPriceRequest) returns (GasPriceResponse) {
option (google.api.http) = {
get : "/feemarket/v1/base_fee"
get : "/feemarket/v1/gas_price/{denom}"
};
};

// GasPrices returns the current feemarket module list of gas prices
// in all available denoms.
rpc GasPrices(GasPricesRequest) returns (GasPricesResponse) {
option (google.api.http) = {
get : "/feemarket/v1/gas_prices"
};
};
}
Expand All @@ -46,14 +55,31 @@ message StateRequest {}
// StateResponse is the response type for the Query/State RPC method.
message StateResponse { State state = 1 [ (gogoproto.nullable) = false ]; }

// BaseFeeRequest is the request type for the Query/BaseFee RPC method.
message BaseFeeRequest {}
// GasPriceRequest is the request type for the Query/GasPrice RPC method.
message GasPriceRequest {
// denom we are querying gas price in
string denom = 1;
}

// GasPriceResponse is the response type for the Query/GasPrice RPC method.
// Returns a gas price in specified denom.
message GasPriceResponse {
cosmos.base.v1beta1.DecCoin price = 1 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
}

// StateResponse is the response type for the Query/BaseFee RPC method.
message BaseFeeResponse {
repeated cosmos.base.v1beta1.DecCoin fees = 1 [
// GasPriceRequest is the request type for the Query/GasPrices RPC method.
message GasPricesRequest {
}

// GasPricesResponse is the response type for the Query/GasPrices RPC method.
// Returns a gas price in all available denoms.
message GasPricesResponse {
repeated cosmos.base.v1beta1.DecCoin prices = 1 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
}
}
8 changes: 4 additions & 4 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

var (
minBaseFee = sdkmath.LegacyNewDec(10)
baseFee = sdkmath.LegacyNewDec(1000000)
minBaseGasPrice = sdkmath.LegacyNewDec(10)
baseGasPrice = sdkmath.LegacyNewDec(1000000)

// config params
numValidators = 3
Expand All @@ -43,7 +43,7 @@ var (
Beta: feemarkettypes.DefaultBeta,
Theta: feemarkettypes.DefaultTheta,
Delta: feemarkettypes.DefaultDelta,
MinBaseFee: minBaseFee,
MinBaseGasPrice: minBaseGasPrice,
MinLearningRate: feemarkettypes.DefaultMinLearningRate,
MaxLearningRate: feemarkettypes.DefaultMaxLearningRate,
TargetBlockUtilization: feemarkettypes.DefaultTargetBlockUtilization / 4,
Expand All @@ -57,7 +57,7 @@ var (
{
Key: "app_state.feemarket.state",
Value: feemarkettypes.State{
BaseFee: baseFee,
BaseGasPrice: baseGasPrice,
LearningRate: feemarkettypes.DefaultMaxLearningRate,
Window: make([]uint64, feemarkettypes.DefaultWindow),
Index: 0,
Expand Down
8 changes: 5 additions & 3 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *TestSuite) QueryState() types.State {
return resp.State
}

func (s *TestSuite) QueryBaseFee() sdk.DecCoins {
func (s *TestSuite) QueryDefaultGasPrice() sdk.DecCoin {
s.T().Helper()

// get grpc address
Expand All @@ -162,10 +162,12 @@ func (s *TestSuite) QueryBaseFee() sdk.DecCoins {
// create the oracle client
c := types.NewQueryClient(cc)

resp, err := c.BaseFee(context.Background(), &types.BaseFeeRequest{})
resp, err := c.GasPrice(context.Background(), &types.GasPriceRequest{
Denom: sdk.DefaultBondDenom,
})
s.Require().NoError(err)

return resp.Fees
return resp.GetPrice()
}

// QueryValidators queries for all the network's validators
Expand Down
61 changes: 37 additions & 24 deletions tests/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"math/rand"
"sync"

feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -123,8 +121,8 @@ func (s *TestSuite) SetupSubTest() {

state := s.QueryState()
s.T().Log("state at block height", height+1, ":", state.String())
fee := s.QueryBaseFee()
s.T().Log("fee at block height", height+1, ":", fee.String())
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("gas price at block height", height+1, ":", gasPrice.String())
}

func (s *TestSuite) TestQueryParams() {
Expand All @@ -147,17 +145,17 @@ func (s *TestSuite) TestQueryState() {
})
}

func (s *TestSuite) TestQueryBaseFee() {
s.Run("query base fee", func() {
// query base fee
fees := s.QueryBaseFee()
func (s *TestSuite) TestQueryGasPrice() {
s.Run("query gas price", func() {
// query gas price
gasPrice := s.QueryDefaultGasPrice()

// expect validate to pass
require.NoError(s.T(), fees.Validate(), fees)
require.NoError(s.T(), gasPrice.Validate(), gasPrice)
})
}

// TestSendTxDecrease tests that the feemarket will decrease until it hits the min base fee
// TestSendTxDecrease tests that the feemarket will decrease until it hits the min gas price
// when gas utilization is below the target block utilization.
func (s *TestSuite) TestSendTxDecrease() {
// cast chain to cosmos-chain
Expand All @@ -169,18 +167,23 @@ func (s *TestSuite) TestSendTxDecrease() {

params := s.QueryParams()

baseFee := s.QueryBaseFee()
defaultGasPrice := s.QueryDefaultGasPrice()
gas := int64(200000)
minBaseFee := baseFee.MulDec(math.LegacyNewDec(gas))[0]
minBaseFee := sdk.NewDecCoinFromDec(defaultGasPrice.Denom, defaultGasPrice.Amount.Mul(math.LegacyNewDec(gas)))
minBaseFeeCoins := sdk.NewCoins(sdk.NewCoin(minBaseFee.Denom, minBaseFee.Amount.TruncateInt()))
sendAmt := int64(100000)

s.Run("expect fee market state to decrease", func() {
s.T().Log("performing sends...")
for {
<<<<<<< HEAD
// send with the exact expected fee
height, err := s.chain.(*cosmos.CosmosChain).Height(context.Background())
s.Require().NoError(err)
=======
// send with the exact expected defaultGasPrice

>>>>>>> cf0c02f (refactored queries, ante/post handlers and DenomResolver (#81))
wg := sync.WaitGroup{}
wg.Add(3)

Expand Down Expand Up @@ -232,12 +235,17 @@ func (s *TestSuite) TestSendTxDecrease() {
s.Require().Equal(uint32(0), txResp.DeliverTx.Code, txResp.DeliverTx)
}()
wg.Wait()
<<<<<<< HEAD
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+1)

fee := s.QueryBaseFee()
s.T().Log("base fee", fee.String())
=======
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("base defaultGasPrice", gasPrice.String())
>>>>>>> cf0c02f (refactored queries, ante/post handlers and DenomResolver (#81))

if fee.AmountOf(feemarkettypes.DefaultFeeDenom).Equal(params.MinBaseFee) {
if gasPrice.Amount.Equal(params.MinBaseGasPrice) {
break
}
}
Expand All @@ -248,8 +256,8 @@ func (s *TestSuite) TestSendTxDecrease() {
s.Require().NoError(err)
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+5)

fee := s.QueryBaseFee()
s.T().Log("base fee", fee.String())
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("gas price", gasPrice.String())

amt, err := s.chain.GetBalance(context.Background(), s.user1.FormattedAddress(), minBaseFee.Denom)
s.Require().NoError(err)
Expand All @@ -268,16 +276,16 @@ func (s *TestSuite) TestSendTxIncrease() {
nodes := cosmosChain.Nodes()
s.Require().True(len(nodes) > 0)

baseFee := s.QueryBaseFee()
baseGasPrice := s.QueryDefaultGasPrice()
gas := int64(20000100)
sendAmt := int64(100)

s.Run("expect fee market fee to increase", func() {
s.Run("expect fee market gas price to increase", func() {
s.T().Log("performing sends...")
for {
// send with the exact expected fee
baseFee = s.QueryBaseFee()
minBaseFee := baseFee.MulDec(math.LegacyNewDec(gas))[0]
// send with the exact expected baseGasPrice
baseGasPrice = s.QueryDefaultGasPrice()
minBaseFee := sdk.NewDecCoinFromDec(baseGasPrice.Denom, baseGasPrice.Amount.Mul(math.LegacyNewDec(gas)))
// add headroom
minBaseFeeCoins := sdk.NewCoins(sdk.NewCoin(minBaseFee.Denom, minBaseFee.Amount.Add(math.LegacyNewDec(10)).TruncateInt()))

Expand Down Expand Up @@ -334,12 +342,17 @@ func (s *TestSuite) TestSendTxIncrease() {
s.Require().Equal(uint32(0), txResp.DeliverTx.Code, txResp.DeliverTx)
}()
wg.Wait()
<<<<<<< HEAD
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+1)

baseFee = s.QueryBaseFee()
s.T().Log("base fee", baseFee.String())
=======
baseGasPrice = s.QueryDefaultGasPrice()
s.T().Log("gas price", baseGasPrice.String())
>>>>>>> cf0c02f (refactored queries, ante/post handlers and DenomResolver (#81))

if baseFee.AmountOf(feemarkettypes.DefaultFeeDenom).GT(math.LegacyNewDec(1000000)) {
if baseGasPrice.Amount.GT(math.LegacyNewDec(1000000)) {
break
}
}
Expand All @@ -350,10 +363,10 @@ func (s *TestSuite) TestSendTxIncrease() {
s.Require().NoError(err)
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+5)

fee := s.QueryBaseFee()
s.T().Log("base fee", fee.String())
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("gas price", gasPrice.String())

amt, err := s.chain.GetBalance(context.Background(), s.user1.FormattedAddress(), baseFee[0].Denom)
amt, err := s.chain.GetBalance(context.Background(), s.user1.FormattedAddress(), gasPrice.Denom)
s.Require().NoError(err)
s.Require().True(amt.LT(math.NewInt(initBalance)), amt)
s.T().Log("balance:", amt.String())
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *IntegrationTestSuite) TestParams() {
Beta: math.LegacyMustNewDecFromStr("0.1"),
Theta: math.LegacyMustNewDecFromStr("0.1"),
Delta: math.LegacyMustNewDecFromStr("0.1"),
MinBaseFee: math.LegacyNewDec(10),
MinBaseGasPrice: math.LegacyNewDec(10),
MinLearningRate: math.LegacyMustNewDecFromStr("0.1"),
MaxLearningRate: math.LegacyMustNewDecFromStr("0.1"),
TargetBlockUtilization: 5,
Expand Down
2 changes: 1 addition & 1 deletion x/feemarket/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type BankKeeper interface {
//go:generate mockery --name FeeMarketKeeper --filename mock_feemarket_keeper.go
type FeeMarketKeeper interface {
GetState(ctx sdk.Context) (feemarkettypes.State, error)
GetMinGasPrices(ctx sdk.Context) (sdk.DecCoins, error)
GetMinGasPrice(ctx sdk.Context, denom string) (sdk.DecCoin, error)
GetParams(ctx sdk.Context) (feemarkettypes.Params, error)
SetState(ctx sdk.Context, state feemarkettypes.State) error
SetParams(ctx sdk.Context, params feemarkettypes.Params) error
Expand Down
Loading

0 comments on commit c3fef7e

Please sign in to comment.