Skip to content

Commit ad2bdcb

Browse files
mergify[bot]Alex Johnson
andauthored
fix: gas sim (backport #129) (#130)
* fix: gas sim (#129) * small updates * protect * update ante * testing * lint fix (cherry picked from commit efe52f2) # Conflicts: # x/feemarket/post/fee_test.go * fix --------- Co-authored-by: Alex Johnson <[email protected]>
1 parent 651e8f1 commit ad2bdcb

File tree

7 files changed

+107
-77
lines changed

7 files changed

+107
-77
lines changed

.github/workflows/ictest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-go@v4
1414
with:
15-
go-version: 1.22.3
15+
go-version: 1.22.5
1616
cache: true
1717
cache-dependency-path: go.sum
1818
- uses: technote-space/[email protected]

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/setup-go@v4
2020
with:
21-
go-version: 1.22.3
21+
go-version: 1.22.5
2222
- uses: actions/checkout@v4
2323
- name: golangci-lint
2424
uses: golangci/golangci-lint-action@v3

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v4
2121
- uses: actions/setup-go@v4
2222
with:
23-
go-version: 1.22.3
23+
go-version: 1.22.5
2424
cache: true
2525
cache-dependency-path: go.sum
2626
- uses: technote-space/[email protected]
@@ -40,7 +40,7 @@ jobs:
4040
- uses: actions/checkout@v4
4141
- uses: actions/setup-go@v4
4242
with:
43-
go-version: 1.22.3
43+
go-version: 1.22.5
4444
cache: true
4545
cache-dependency-path: go.sum
4646
- uses: technote-space/[email protected]
@@ -60,7 +60,7 @@ jobs:
6060
- uses: actions/checkout@v4
6161
- uses: actions/setup-go@v4
6262
with:
63-
go-version: 1.22.3
63+
go-version: 1.22.5
6464
cache: true
6565
cache-dependency-path: go.sum
6666
- uses: technote-space/[email protected]

x/feemarket/ante/fee.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ func (dfd feeMarketCheckDecorator) anteHandle(ctx sdk.Context, tx sdk.Tx, simula
104104

105105
var feeCoin sdk.Coin
106106
if simulate && len(feeCoins) == 0 {
107-
feeCoin = sdk.NewCoin(params.FeeDenom, sdkmath.ZeroInt())
107+
// if simulating and user did not provider a fee - create a dummy value for them
108+
feeCoin = sdk.NewCoin(params.FeeDenom, sdkmath.OneInt())
108109
} else {
109110
feeCoin = feeCoins[0]
110111
}
@@ -128,19 +129,20 @@ func (dfd feeMarketCheckDecorator) anteHandle(ctx sdk.Context, tx sdk.Tx, simula
128129
if err != nil {
129130
return ctx, errorsmod.Wrapf(err, "error checking fee")
130131
}
132+
}
131133

132-
priorityFee, err := dfd.resolveTxPriorityCoins(ctx, feeCoin, params.FeeDenom)
133-
if err != nil {
134-
return ctx, errorsmod.Wrapf(err, "error resolving fee priority")
135-
}
136-
137-
baseGasPrice, err := dfd.feemarketKeeper.GetMinGasPrice(ctx, params.FeeDenom)
138-
if err != nil {
139-
return ctx, err
140-
}
134+
priorityFee, err := dfd.resolveTxPriorityCoins(ctx, feeCoin, params.FeeDenom)
135+
if err != nil {
136+
return ctx, errorsmod.Wrapf(err, "error resolving fee priority")
137+
}
141138

142-
ctx = ctx.WithPriority(GetTxPriority(priorityFee, int64(gas), baseGasPrice))
139+
baseGasPrice, err := dfd.feemarketKeeper.GetMinGasPrice(ctx, params.FeeDenom)
140+
if err != nil {
141+
return ctx, err
143142
}
143+
144+
ctx = ctx.WithPriority(GetTxPriority(priorityFee, int64(gas), baseGasPrice))
145+
144146
return next(ctx, tx, simulate)
145147
}
146148

@@ -220,6 +222,16 @@ const (
220222
// normalizedGasPrice = effectiveGasPrice / currentGasPrice (floor is 1. The minimum effective gas price can ever be is current gas price)
221223
// scaledGasPrice = normalizedGasPrice * 10 ^ gasPricePrecision (amount of decimal places in the normalized gas price to consider when converting to int64).
222224
func GetTxPriority(fee sdk.Coin, gasLimit int64, currentGasPrice sdk.DecCoin) int64 {
225+
// protections from dividing by 0
226+
if gasLimit == 0 {
227+
return 0
228+
}
229+
230+
// if the gas price is 0, just use a raw amount
231+
if currentGasPrice.IsZero() {
232+
return fee.Amount.Int64()
233+
}
234+
223235
effectiveGasPrice := fee.Amount.ToLegacyDec().QuoInt64(gasLimit)
224236
normalizedGasPrice := effectiveGasPrice.Quo(currentGasPrice.Amount)
225237
scaledGasPrice := normalizedGasPrice.MulInt64(int64(math.Pow10(gasPricePrecision)))

x/feemarket/ante/suite/suite.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ func (s *TestSuite) SetupHandlers(mock bool) {
126126

127127
// TestCase represents a test case used in test tables.
128128
type TestCase struct {
129-
Name string
130-
Malleate func(*TestSuite) TestCaseArgs
131-
RunAnte bool
132-
RunPost bool
133-
Simulate bool
134-
ExpPass bool
135-
ExpErr error
129+
Name string
130+
Malleate func(*TestSuite) TestCaseArgs
131+
RunAnte bool
132+
RunPost bool
133+
Simulate bool
134+
ExpPass bool
135+
ExpErr error
136+
ExpectConsumedGas uint64
136137
}
137138

138139
type TestCaseArgs struct {
@@ -186,6 +187,11 @@ func (s *TestSuite) RunTestCase(t *testing.T, tc TestCase, args TestCaseArgs) {
186187
require.NotNil(t, newCtx)
187188

188189
s.Ctx = newCtx
190+
if tc.RunPost {
191+
consumedGas := newCtx.GasMeter().GasConsumed()
192+
require.Equal(t, tc.ExpectConsumedGas, consumedGas)
193+
}
194+
189195
} else {
190196
switch {
191197
case txErr != nil:

x/feemarket/post/fee.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func (dfd FeeMarketDeductDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simul
8383

8484
var feeCoin sdk.Coin
8585
if simulate && len(feeCoins) == 0 {
86-
feeCoin = sdk.NewCoin(params.FeeDenom, math.ZeroInt())
86+
// if simulating and user did not provider a fee - create a dummy value for them
87+
feeCoin = sdk.NewCoin(params.FeeDenom, math.OneInt())
8788
} else {
8889
feeCoin = feeCoins[0]
8990
}

x/feemarket/post/fee_test.go

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ func TestSendTip(t *testing.T) {
124124
func TestPostHandle(t *testing.T) {
125125
// Same data for every test case
126126
const (
127-
baseDenom = "stake"
128-
resolvableDenom = "atom"
127+
baseDenom = "stake"
128+
resolvableDenom = "atom"
129+
expectedConsumedGas = 31690
130+
gasLimit = expectedConsumedGas
129131
)
130132

131133
// exact cost of transaction
132-
gasLimit := uint64(25635)
133134
validFeeAmount := types.DefaultMinBaseGasPrice.MulInt64(int64(gasLimit))
134135
validFeeAmountWithTip := validFeeAmount.Add(math.LegacyNewDec(100))
135136
validFee := sdk.NewCoins(sdk.NewCoin(baseDenom, validFeeAmount.TruncateInt()))
@@ -204,11 +205,12 @@ func TestPostHandle(t *testing.T) {
204205
FeeAmount: validFee,
205206
}
206207
},
207-
RunAnte: true,
208-
RunPost: true,
209-
Simulate: true,
210-
ExpPass: true,
211-
ExpErr: nil,
208+
RunAnte: true,
209+
RunPost: true,
210+
Simulate: true,
211+
ExpPass: true,
212+
ExpErr: nil,
213+
ExpectConsumedGas: expectedConsumedGas,
212214
},
213215
{
214216
Name: "signer has enough funds, should pass, no tip",
@@ -223,11 +225,12 @@ func TestPostHandle(t *testing.T) {
223225
FeeAmount: validFee,
224226
}
225227
},
226-
RunAnte: true,
227-
RunPost: true,
228-
Simulate: false,
229-
ExpPass: true,
230-
ExpErr: nil,
228+
RunAnte: true,
229+
RunPost: true,
230+
Simulate: false,
231+
ExpPass: true,
232+
ExpErr: nil,
233+
ExpectConsumedGas: expectedConsumedGas,
231234
},
232235
{
233236
Name: "signer has enough funds, should pass with tip",
@@ -242,11 +245,12 @@ func TestPostHandle(t *testing.T) {
242245
FeeAmount: validFeeWithTip,
243246
}
244247
},
245-
RunAnte: true,
246-
RunPost: true,
247-
Simulate: false,
248-
ExpPass: true,
249-
ExpErr: nil,
248+
RunAnte: true,
249+
RunPost: true,
250+
Simulate: false,
251+
ExpPass: true,
252+
ExpErr: nil,
253+
ExpectConsumedGas: expectedConsumedGas,
250254
},
251255
{
252256
Name: "signer has enough funds, should pass with tip - simulate",
@@ -261,11 +265,12 @@ func TestPostHandle(t *testing.T) {
261265
FeeAmount: validFeeWithTip,
262266
}
263267
},
264-
RunAnte: true,
265-
RunPost: true,
266-
Simulate: true,
267-
ExpPass: true,
268-
ExpErr: nil,
268+
RunAnte: true,
269+
RunPost: true,
270+
Simulate: true,
271+
ExpPass: true,
272+
ExpErr: nil,
273+
ExpectConsumedGas: expectedConsumedGas,
269274
},
270275
{
271276
Name: "signer has enough funds, should pass, no tip - resolvable denom",
@@ -280,11 +285,12 @@ func TestPostHandle(t *testing.T) {
280285
FeeAmount: validResolvableFee,
281286
}
282287
},
283-
RunAnte: true,
284-
RunPost: true,
285-
Simulate: false,
286-
ExpPass: true,
287-
ExpErr: nil,
288+
RunAnte: true,
289+
RunPost: true,
290+
Simulate: false,
291+
ExpPass: true,
292+
ExpErr: nil,
293+
ExpectConsumedGas: expectedConsumedGas,
288294
},
289295
{
290296
Name: "signer has enough funds, should pass, no tip - resolvable denom - simulate",
@@ -299,11 +305,12 @@ func TestPostHandle(t *testing.T) {
299305
FeeAmount: validResolvableFee,
300306
}
301307
},
302-
RunAnte: true,
303-
RunPost: true,
304-
Simulate: true,
305-
ExpPass: true,
306-
ExpErr: nil,
308+
RunAnte: true,
309+
RunPost: true,
310+
Simulate: true,
311+
ExpPass: true,
312+
ExpErr: nil,
313+
ExpectConsumedGas: expectedConsumedGas,
307314
},
308315
{
309316
Name: "signer has enough funds, should pass with tip - resolvable denom",
@@ -318,11 +325,12 @@ func TestPostHandle(t *testing.T) {
318325
FeeAmount: validResolvableFeeWithTip,
319326
}
320327
},
321-
RunAnte: true,
322-
RunPost: true,
323-
Simulate: false,
324-
ExpPass: true,
325-
ExpErr: nil,
328+
RunAnte: true,
329+
RunPost: true,
330+
Simulate: false,
331+
ExpPass: true,
332+
ExpErr: nil,
333+
ExpectConsumedGas: expectedConsumedGas,
326334
},
327335
{
328336
Name: "signer has enough funds, should pass with tip - resolvable denom - simulate",
@@ -337,11 +345,12 @@ func TestPostHandle(t *testing.T) {
337345
FeeAmount: validResolvableFeeWithTip,
338346
}
339347
},
340-
RunAnte: true,
341-
RunPost: true,
342-
Simulate: true,
343-
ExpPass: true,
344-
ExpErr: nil,
348+
RunAnte: true,
349+
RunPost: true,
350+
Simulate: true,
351+
ExpPass: true,
352+
ExpErr: nil,
353+
ExpectConsumedGas: expectedConsumedGas,
345354
},
346355
{
347356
Name: "0 gas given should pass in simulate - no fee",
@@ -354,11 +363,12 @@ func TestPostHandle(t *testing.T) {
354363
FeeAmount: nil,
355364
}
356365
},
357-
RunAnte: true,
358-
RunPost: false,
359-
Simulate: true,
360-
ExpPass: true,
361-
ExpErr: nil,
366+
RunAnte: true,
367+
RunPost: false,
368+
Simulate: true,
369+
ExpPass: true,
370+
ExpErr: nil,
371+
ExpectConsumedGas: expectedConsumedGas,
362372
},
363373
{
364374
Name: "0 gas given should pass in simulate - fee",
@@ -371,11 +381,12 @@ func TestPostHandle(t *testing.T) {
371381
FeeAmount: validFee,
372382
}
373383
},
374-
RunAnte: true,
375-
RunPost: false,
376-
Simulate: true,
377-
ExpPass: true,
378-
ExpErr: nil,
384+
RunAnte: true,
385+
RunPost: false,
386+
Simulate: true,
387+
ExpPass: true,
388+
ExpErr: nil,
389+
ExpectConsumedGas: expectedConsumedGas,
379390
},
380391
{
381392
Name: "no fee - fail",

0 commit comments

Comments
 (0)