Skip to content

Commit efe52f2

Browse files
author
Alex Johnson
authored
fix: gas sim (#129)
* small updates * protect * update ante * testing * lint fix
1 parent 9a2a3ee commit efe52f2

File tree

7 files changed

+107
-78
lines changed

7 files changed

+107
-78
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
@@ -133,13 +133,14 @@ func (s *TestSuite) SetupHandlers(mock bool) {
133133

134134
// TestCase represents a test case used in test tables.
135135
type TestCase struct {
136-
Name string
137-
Malleate func(*TestSuite) TestCaseArgs
138-
RunAnte bool
139-
RunPost bool
140-
Simulate bool
141-
ExpPass bool
142-
ExpErr error
136+
Name string
137+
Malleate func(*TestSuite) TestCaseArgs
138+
RunAnte bool
139+
RunPost bool
140+
Simulate bool
141+
ExpPass bool
142+
ExpErr error
143+
ExpectConsumedGas uint64
143144
}
144145

145146
type TestCaseArgs struct {
@@ -193,6 +194,11 @@ func (s *TestSuite) RunTestCase(t *testing.T, tc TestCase, args TestCaseArgs) {
193194
require.NotNil(t, newCtx)
194195

195196
s.Ctx = newCtx
197+
if tc.RunPost {
198+
consumedGas := newCtx.GasMeter().GasConsumed()
199+
require.Equal(t, tc.ExpectConsumedGas, consumedGas)
200+
}
201+
196202
} else {
197203
switch {
198204
case txErr != nil:

x/feemarket/post/fee.go

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

8686
var feeCoin sdk.Coin
8787
if simulate && len(feeCoins) == 0 {
88-
feeCoin = sdk.NewCoin(params.FeeDenom, math.ZeroInt())
88+
// if simulating and user did not provider a fee - create a dummy value for them
89+
feeCoin = sdk.NewCoin(params.FeeDenom, math.OneInt())
8990
} else {
9091
feeCoin = feeCoins[0]
9192
}

x/feemarket/post/fee_test.go

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ 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 = 33339
130+
gasLimit = expectedConsumedGas
129131
)
130132

131-
// exact cost of transaction
132-
gasLimit := uint64(27284)
133133
validFeeAmount := types.DefaultMinBaseGasPrice.MulInt64(int64(gasLimit))
134134
validFeeAmountWithTip := validFeeAmount.Add(math.LegacyNewDec(100))
135135
validFee := sdk.NewCoins(sdk.NewCoin(baseDenom, validFeeAmount.TruncateInt()))
@@ -204,11 +204,12 @@ func TestPostHandle(t *testing.T) {
204204
FeeAmount: validFee,
205205
}
206206
},
207-
RunAnte: true,
208-
RunPost: true,
209-
Simulate: true,
210-
ExpPass: true,
211-
ExpErr: nil,
207+
RunAnte: true,
208+
RunPost: true,
209+
Simulate: true,
210+
ExpPass: true,
211+
ExpErr: nil,
212+
ExpectConsumedGas: expectedConsumedGas,
212213
},
213214
{
214215
Name: "signer has enough funds, should pass, no tip",
@@ -223,11 +224,12 @@ func TestPostHandle(t *testing.T) {
223224
FeeAmount: validFee,
224225
}
225226
},
226-
RunAnte: true,
227-
RunPost: true,
228-
Simulate: false,
229-
ExpPass: true,
230-
ExpErr: nil,
227+
RunAnte: true,
228+
RunPost: true,
229+
Simulate: false,
230+
ExpPass: true,
231+
ExpErr: nil,
232+
ExpectConsumedGas: expectedConsumedGas,
231233
},
232234
{
233235
Name: "signer has enough funds, should pass with tip",
@@ -242,11 +244,12 @@ func TestPostHandle(t *testing.T) {
242244
FeeAmount: validFeeWithTip,
243245
}
244246
},
245-
RunAnte: true,
246-
RunPost: true,
247-
Simulate: false,
248-
ExpPass: true,
249-
ExpErr: nil,
247+
RunAnte: true,
248+
RunPost: true,
249+
Simulate: false,
250+
ExpPass: true,
251+
ExpErr: nil,
252+
ExpectConsumedGas: expectedConsumedGas,
250253
},
251254
{
252255
Name: "signer has enough funds, should pass with tip - simulate",
@@ -261,11 +264,12 @@ func TestPostHandle(t *testing.T) {
261264
FeeAmount: validFeeWithTip,
262265
}
263266
},
264-
RunAnte: true,
265-
RunPost: true,
266-
Simulate: true,
267-
ExpPass: true,
268-
ExpErr: nil,
267+
RunAnte: true,
268+
RunPost: true,
269+
Simulate: true,
270+
ExpPass: true,
271+
ExpErr: nil,
272+
ExpectConsumedGas: expectedConsumedGas,
269273
},
270274
{
271275
Name: "signer has enough funds, should pass, no tip - resolvable denom",
@@ -280,11 +284,12 @@ func TestPostHandle(t *testing.T) {
280284
FeeAmount: validResolvableFee,
281285
}
282286
},
283-
RunAnte: true,
284-
RunPost: true,
285-
Simulate: false,
286-
ExpPass: true,
287-
ExpErr: nil,
287+
RunAnte: true,
288+
RunPost: true,
289+
Simulate: false,
290+
ExpPass: true,
291+
ExpErr: nil,
292+
ExpectConsumedGas: expectedConsumedGas,
288293
},
289294
{
290295
Name: "signer has enough funds, should pass, no tip - resolvable denom - simulate",
@@ -299,11 +304,12 @@ func TestPostHandle(t *testing.T) {
299304
FeeAmount: validResolvableFee,
300305
}
301306
},
302-
RunAnte: true,
303-
RunPost: true,
304-
Simulate: true,
305-
ExpPass: true,
306-
ExpErr: nil,
307+
RunAnte: true,
308+
RunPost: true,
309+
Simulate: true,
310+
ExpPass: true,
311+
ExpErr: nil,
312+
ExpectConsumedGas: expectedConsumedGas,
307313
},
308314
{
309315
Name: "signer has enough funds, should pass with tip - resolvable denom",
@@ -318,11 +324,12 @@ func TestPostHandle(t *testing.T) {
318324
FeeAmount: validResolvableFeeWithTip,
319325
}
320326
},
321-
RunAnte: true,
322-
RunPost: true,
323-
Simulate: false,
324-
ExpPass: true,
325-
ExpErr: nil,
327+
RunAnte: true,
328+
RunPost: true,
329+
Simulate: false,
330+
ExpPass: true,
331+
ExpErr: nil,
332+
ExpectConsumedGas: expectedConsumedGas,
326333
},
327334
{
328335
Name: "signer has enough funds, should pass with tip - resolvable denom - simulate",
@@ -337,11 +344,12 @@ func TestPostHandle(t *testing.T) {
337344
FeeAmount: validResolvableFeeWithTip,
338345
}
339346
},
340-
RunAnte: true,
341-
RunPost: true,
342-
Simulate: true,
343-
ExpPass: true,
344-
ExpErr: nil,
347+
RunAnte: true,
348+
RunPost: true,
349+
Simulate: true,
350+
ExpPass: true,
351+
ExpErr: nil,
352+
ExpectConsumedGas: expectedConsumedGas,
345353
},
346354
{
347355
Name: "0 gas given should pass in simulate - no fee",
@@ -354,11 +362,12 @@ func TestPostHandle(t *testing.T) {
354362
FeeAmount: nil,
355363
}
356364
},
357-
RunAnte: true,
358-
RunPost: false,
359-
Simulate: true,
360-
ExpPass: true,
361-
ExpErr: nil,
365+
RunAnte: true,
366+
RunPost: false,
367+
Simulate: true,
368+
ExpPass: true,
369+
ExpErr: nil,
370+
ExpectConsumedGas: expectedConsumedGas,
362371
},
363372
{
364373
Name: "0 gas given should pass in simulate - fee",
@@ -371,11 +380,12 @@ func TestPostHandle(t *testing.T) {
371380
FeeAmount: validFee,
372381
}
373382
},
374-
RunAnte: true,
375-
RunPost: false,
376-
Simulate: true,
377-
ExpPass: true,
378-
ExpErr: nil,
383+
RunAnte: true,
384+
RunPost: false,
385+
Simulate: true,
386+
ExpPass: true,
387+
ExpErr: nil,
388+
ExpectConsumedGas: expectedConsumedGas,
379389
},
380390
{
381391
Name: "no fee - fail",

0 commit comments

Comments
 (0)