Skip to content

Commit dbc959b

Browse files
mergify[bot]Alex Johnson
andauthored
fix: nil check in post (backport #118) (#119)
* fix: nil check in post (#118) * fix * fix with check * add early exit * fix (cherry picked from commit f0997fb) # Conflicts: # x/feemarket/post/fee.go # x/feemarket/post/fee_test.go * ok * ok * fix --------- Co-authored-by: Alex Johnson <[email protected]>
1 parent 8f88057 commit dbc959b

File tree

6 files changed

+163
-212
lines changed

6 files changed

+163
-212
lines changed

docs/INTEGRATIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ There are two ways to construct a transaction with `gasPrice`:
6767

6868
### Understanding Fee Deducted
6969

70-
The actual amount of fee deducted from the fee payer is based on gas consumed, not `gasLimit`.
70+
The actual amount of fee deducted from the fee payer is based on gas consumed, not `gasLimit`. The total amount deducted (`fee + tip`) will be equal to the amount of fee specified on your transaction.
7171

7272
The amount consumed is equal to the `inferredTip + gasPrice * gasConsumed`, where `inferredTip = feeAmount - gasLimit * gasPrice` (This may be different than the tip you specified when building the transaction because the `gasPrice` on chain may have changed since when you queried it.)
7373

tests/e2e/e2e_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ import (
99
"github.com/cosmos/cosmos-sdk/x/auth"
1010
"github.com/cosmos/cosmos-sdk/x/bank"
1111
"github.com/cosmos/cosmos-sdk/x/gov"
12-
<<<<<<< HEAD
1312
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
1413
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
1514
"github.com/strangelove-ventures/interchaintest/v7/ibc"
1615
ictestutil "github.com/strangelove-ventures/interchaintest/v7/testutil"
17-
=======
18-
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
19-
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
20-
"github.com/strangelove-ventures/interchaintest/v8/ibc"
21-
>>>>>>> daca8c8 (test: make extendable (#112))
2216
"github.com/stretchr/testify/suite"
2317

2418
"github.com/skip-mev/feemarket/tests/e2e"
@@ -121,11 +115,7 @@ var (
121115
func TestE2ETestSuite(t *testing.T) {
122116
s := e2e.NewIntegrationSuite(
123117
spec,
124-
<<<<<<< HEAD
125-
=======
126-
oracleImage,
127118
txCfg,
128-
>>>>>>> daca8c8 (test: make extendable (#112))
129119
)
130120

131121
suite.Run(t, s)

tests/e2e/suite.go

Lines changed: 29 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -37,83 +37,6 @@ func init() {
3737
r = rand.New(s)
3838
}
3939

40-
<<<<<<< HEAD
41-
=======
42-
func DefaultOracleSidecar(image ibc.DockerImage) ibc.SidecarConfig {
43-
return ibc.SidecarConfig{
44-
ProcessName: "oracle",
45-
Image: image,
46-
HomeDir: "/oracle",
47-
Ports: []string{"8080", "8081"},
48-
StartCmd: []string{
49-
"slinky",
50-
"--oracle-config", "/oracle/oracle.json",
51-
},
52-
ValidatorProcess: true,
53-
PreStart: true,
54-
}
55-
}
56-
57-
func DefaultOracleConfig(url string) oracleconfig.OracleConfig {
58-
cfg := marketmap.DefaultAPIConfig
59-
cfg.Endpoints = []oracleconfig.Endpoint{
60-
{
61-
URL: url,
62-
},
63-
}
64-
65-
// Create the oracle config
66-
oracleConfig := oracleconfig.OracleConfig{
67-
UpdateInterval: 500 * time.Millisecond,
68-
MaxPriceAge: 1 * time.Minute,
69-
Host: "0.0.0.0",
70-
Port: "8080",
71-
Providers: map[string]oracleconfig.ProviderConfig{
72-
marketmap.Name: {
73-
Name: marketmap.Name,
74-
API: cfg,
75-
Type: "market_map_provider",
76-
},
77-
},
78-
}
79-
80-
return oracleConfig
81-
}
82-
83-
func DefaultMarketMap() mmtypes.MarketMap {
84-
return mmtypes.MarketMap{}
85-
}
86-
87-
func GetOracleSideCar(node *cosmos.ChainNode) *cosmos.SidecarProcess {
88-
if len(node.Sidecars) == 0 {
89-
panic("no sidecars found")
90-
}
91-
return node.Sidecars[0]
92-
}
93-
94-
type TestTxConfig struct {
95-
SmallSendsNum int
96-
LargeSendsNum int
97-
TargetIncreaseGasPrice math.LegacyDec
98-
}
99-
100-
func (tx *TestTxConfig) Validate() error {
101-
if tx.SmallSendsNum < 1 || tx.LargeSendsNum < 1 {
102-
return fmt.Errorf("sends num should be greater than 1")
103-
}
104-
105-
if tx.TargetIncreaseGasPrice.IsNil() {
106-
return fmt.Errorf("target increase gas price is nil")
107-
}
108-
109-
if tx.TargetIncreaseGasPrice.LTE(math.LegacyZeroDec()) {
110-
return fmt.Errorf("target increase gas price is less than or equal to 0")
111-
}
112-
113-
return nil
114-
}
115-
116-
>>>>>>> daca8c8 (test: make extendable (#112))
11740
// TestSuite runs the feemarket e2e test-suite against a given interchaintest specification
11841
type TestSuite struct {
11942
suite.Suite
@@ -191,30 +114,18 @@ func WithChainConstructor(cc ChainConstructor) Option {
191114
}
192115
}
193116

194-
<<<<<<< HEAD
195-
func NewIntegrationSuite(spec *interchaintest.ChainSpec, opts ...Option) *TestSuite {
117+
func NewIntegrationSuite(spec *interchaintest.ChainSpec, txCfg TestTxConfig, opts ...Option) *TestSuite {
118+
if err := txCfg.Validate(); err != nil {
119+
panic(err)
120+
}
121+
196122
suite := &TestSuite{
197123
spec: spec,
198124
denom: defaultDenom,
199125
authority: authtypes.NewModuleAddress(govtypes.ModuleName),
200126
icc: DefaultInterchainConstructor,
201127
cc: DefaultChainConstructor,
202-
=======
203-
func NewIntegrationSuite(spec *interchaintest.ChainSpec, oracleImage ibc.DockerImage, txCfg TestTxConfig, opts ...Option) *TestSuite {
204-
if err := txCfg.Validate(); err != nil {
205-
panic(err)
206-
}
207-
208-
suite := &TestSuite{
209-
spec: spec,
210-
oracleConfig: DefaultOracleSidecar(oracleImage),
211-
denom: defaultDenom,
212-
gasPrices: "",
213-
authority: authtypes.NewModuleAddress(govtypes.ModuleName),
214-
icc: DefaultInterchainConstructor,
215-
cc: DefaultChainConstructor,
216-
txConfig: txCfg,
217-
>>>>>>> daca8c8 (test: make extendable (#112))
128+
txConfig: txCfg,
218129
}
219130

220131
for _, opt := range opts {
@@ -224,6 +135,28 @@ func NewIntegrationSuite(spec *interchaintest.ChainSpec, oracleImage ibc.DockerI
224135
return suite
225136
}
226137

138+
type TestTxConfig struct {
139+
SmallSendsNum int
140+
LargeSendsNum int
141+
TargetIncreaseGasPrice math.LegacyDec
142+
}
143+
144+
func (tx *TestTxConfig) Validate() error {
145+
if tx.SmallSendsNum < 1 || tx.LargeSendsNum < 1 {
146+
return fmt.Errorf("sends num should be greater than 1")
147+
}
148+
149+
if tx.TargetIncreaseGasPrice.IsNil() {
150+
return fmt.Errorf("target increase gas price is nil")
151+
}
152+
153+
if tx.TargetIncreaseGasPrice.LTE(math.LegacyZeroDec()) {
154+
return fmt.Errorf("target increase gas price is less than or equal to 0")
155+
}
156+
157+
return nil
158+
}
159+
227160
func (s *TestSuite) WithKeyringOptions(cdc codec.Codec, opts keyring.Option) {
228161
s.broadcasterOverrides = &KeyringOverride{
229162
cdc: cdc,
@@ -353,13 +286,6 @@ func (s *TestSuite) TestSendTxDecrease() {
353286

354287
s.Run("expect fee market state to decrease", func() {
355288
s.T().Log("performing sends...")
356-
<<<<<<< HEAD
357-
for {
358-
// send with the exact expected fee
359-
height, err := s.chain.(*cosmos.CosmosChain).Height(context.Background())
360-
s.Require().NoError(err)
361-
// send with the exact expected defaultGasPrice
362-
=======
363289
sig := make(chan struct{})
364290
quit := make(chan struct{})
365291
defer close(quit)
@@ -384,7 +310,6 @@ func (s *TestSuite) TestSendTxDecrease() {
384310
break
385311

386312
case <-time.After(100 * time.Millisecond):
387-
>>>>>>> daca8c8 (test: make extendable (#112))
388313
wg := sync.WaitGroup{}
389314
wg.Add(3)
390315

@@ -436,17 +361,6 @@ func (s *TestSuite) TestSendTxDecrease() {
436361
s.Require().Equal(uint32(0), txResp.DeliverTx.Code, txResp.DeliverTx)
437362
}()
438363
wg.Wait()
439-
<<<<<<< HEAD
440-
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+1)
441-
442-
gasPrice := s.QueryDefaultGasPrice()
443-
s.T().Log("base defaultGasPrice", gasPrice.String())
444-
445-
if gasPrice.Amount.Equal(params.MinBaseGasPrice) {
446-
break
447-
}
448-
=======
449-
>>>>>>> daca8c8 (test: make extendable (#112))
450364
}
451365

452366
// wait for 5 blocks
@@ -475,13 +389,6 @@ func (s *TestSuite) TestSendTxIncrease() {
475389
nodes := cosmosChain.Nodes()
476390
s.Require().True(len(nodes) > 0)
477391

478-
<<<<<<< HEAD
479-
baseGasPrice := s.QueryDefaultGasPrice()
480-
gas := int64(20000100)
481-
sendAmt := int64(100)
482-
483-
=======
484-
>>>>>>> daca8c8 (test: make extendable (#112))
485392
params := s.QueryParams()
486393

487394
gas := int64(params.MaxBlockUtilization)
@@ -519,7 +426,7 @@ func (s *TestSuite) TestSendTxIncrease() {
519426
// add headroom
520427
minBaseFeeCoins := sdk.NewCoins(sdk.NewCoin(minBaseFee.Denom, minBaseFee.Amount.Add(math.LegacyNewDec(10)).TruncateInt()))
521428

522-
height, err := s.chain.(*cosmos.CosmosChain).Height(context.Background())
429+
_, err := s.chain.(*cosmos.CosmosChain).Height(context.Background())
523430
s.Require().NoError(err)
524431
wg := sync.WaitGroup{}
525432
wg.Add(3)
@@ -572,17 +479,6 @@ func (s *TestSuite) TestSendTxIncrease() {
572479
s.Require().Equal(uint32(0), txResp.DeliverTx.Code, txResp.DeliverTx)
573480
}()
574481
wg.Wait()
575-
<<<<<<< HEAD
576-
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+1)
577-
578-
baseGasPrice = s.QueryDefaultGasPrice()
579-
s.T().Log("gas price", baseGasPrice.String())
580-
581-
if baseGasPrice.Amount.GT(params.MinBaseGasPrice.Mul(math.LegacyNewDec(10))) {
582-
break
583-
}
584-
=======
585-
>>>>>>> daca8c8 (test: make extendable (#112))
586482
}
587483

588484
// wait for 5 blocks

x/feemarket/ante/fee.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ func (dfd feeMarketCheckDecorator) anteHandle(ctx sdk.Context, tx sdk.Tx, simula
135135
}
136136

137137
ctx = ctx.WithPriority(GetTxPriority(priorityFee, int64(gas), baseGasPrice))
138-
return next(ctx, tx, simulate)
139138
}
140139
return next(ctx, tx, simulate)
141140
}

x/feemarket/post/fee.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
errorsmod "cosmossdk.io/errors"
7-
7+
"cosmossdk.io/math"
88
sdk "github.com/cosmos/cosmos-sdk/types"
99
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1010
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -53,11 +53,6 @@ func (dfd FeeMarketDeductDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simul
5353
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
5454
}
5555

56-
var (
57-
tip sdk.Coin
58-
payCoin sdk.Coin
59-
)
60-
6156
// update fee market params
6257
params, err := dfd.feemarketKeeper.GetParams(ctx)
6358
if err != nil {
@@ -88,6 +83,11 @@ func (dfd FeeMarketDeductDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simul
8883
feeCoin := feeCoins[0]
8984
feeGas := int64(feeTx.GetGas())
9085

86+
var (
87+
tip = sdk.NewCoin(feeCoin.Denom, math.ZeroInt())
88+
payCoin = feeCoin
89+
)
90+
9191
minGasPrice, err := dfd.feemarketKeeper.GetMinGasPrice(ctx, feeCoin.GetDenom())
9292
if err != nil {
9393
return ctx, errorsmod.Wrapf(err, "unable to get min gas price for denom %s", feeCoins[0].GetDenom())
@@ -106,7 +106,7 @@ func (dfd FeeMarketDeductDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simul
106106
}
107107

108108
ctx.Logger().Info("fee deduct post handle",
109-
"fee", feeCoins,
109+
"fee", payCoin,
110110
"tip", tip,
111111
)
112112

@@ -159,9 +159,11 @@ func (dfd FeeMarketDeductDecorator) DeductFeeAndTip(ctx sdk.Context, sdkTx sdk.T
159159
if dfd.feegrantKeeper == nil {
160160
return sdkerrors.ErrInvalidRequest.Wrap("fee grants are not enabled")
161161
} else if !feeGranter.Equals(feePayer) {
162-
err := dfd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, sdk.NewCoins(fee), sdkTx.GetMsgs())
163-
if err != nil {
164-
return errorsmod.Wrapf(err, "%s does not allow to pay fees for %s", feeGranter, feePayer)
162+
if !fee.IsNil() {
163+
err := dfd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, sdk.NewCoins(fee), sdkTx.GetMsgs())
164+
if err != nil {
165+
return errorsmod.Wrapf(err, "%s does not allow to pay fees for %s", feeGranter, feePayer)
166+
}
165167
}
166168
}
167169

@@ -176,7 +178,7 @@ func (dfd FeeMarketDeductDecorator) DeductFeeAndTip(ctx sdk.Context, sdkTx sdk.T
176178
var events sdk.Events
177179

178180
// deduct the fees and tip
179-
if !fee.Amount.IsNil() && !fee.IsZero() {
181+
if !fee.IsNil() {
180182
err := DeductCoins(dfd.bankKeeper, ctx, deductFeesFromAcc, sdk.NewCoins(fee), distributeFees)
181183
if err != nil {
182184
return err
@@ -190,7 +192,7 @@ func (dfd FeeMarketDeductDecorator) DeductFeeAndTip(ctx sdk.Context, sdkTx sdk.T
190192
}
191193

192194
proposer := sdk.AccAddress(ctx.BlockHeader().ProposerAddress)
193-
if !tip.Amount.IsNil() && !tip.IsZero() {
195+
if !tip.IsNil() {
194196
err := SendTip(dfd.bankKeeper, ctx, deductFeesFromAcc.GetAddress(), proposer, sdk.NewCoins(tip))
195197
if err != nil {
196198
return err
@@ -211,10 +213,6 @@ func (dfd FeeMarketDeductDecorator) DeductFeeAndTip(ctx sdk.Context, sdkTx sdk.T
211213
// DeductCoins deducts coins from the given account.
212214
// Coins can be sent to the default fee collector (causes coins to be distributed to stakers) or sent to the feemarket fee collector account (causes coins to be burned).
213215
func DeductCoins(bankKeeper BankKeeper, ctx sdk.Context, acc authtypes.AccountI, coins sdk.Coins, distributeFees bool) error {
214-
if !coins.IsValid() {
215-
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "invalid coin amount: %s", coins)
216-
}
217-
218216
targetModuleAcc := feemarkettypes.FeeCollectorName
219217
if distributeFees {
220218
targetModuleAcc = authtypes.FeeCollectorName
@@ -230,10 +228,6 @@ func DeductCoins(bankKeeper BankKeeper, ctx sdk.Context, acc authtypes.AccountI,
230228

231229
// SendTip sends a tip to the current block proposer.
232230
func SendTip(bankKeeper BankKeeper, ctx sdk.Context, acc, proposer sdk.AccAddress, coins sdk.Coins) error {
233-
if !coins.IsValid() {
234-
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "invalid coin amount: %s", coins)
235-
}
236-
237231
err := bankKeeper.SendCoins(ctx, acc, proposer, coins)
238232
if err != nil {
239233
return err

0 commit comments

Comments
 (0)