Skip to content

Commit

Permalink
fix: charge gas when querying cw contracts (#7209)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn authored Jan 9, 2024
1 parent 875a12a commit 370b381
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Epoch and CPU time optimizations

Epoch optimizations are in this release, see a subset of PR links in v21.1.3 section.

### Bug Fixes

* [#7209](https://github.com/osmosis-labs/osmosis/pull/7209) Charge gas on input context when querying cw contracts.

## v21.0.0

### API
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109065741-1aea8952d330
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1491,8 +1491,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109034818-d2a4cb704d18 h1:yK4tv3+4C+SebP1LeTkle4EuLAOSKsqpaXenlDwysrU=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109034818-d2a4cb704d18/go.mod h1:qhaCO81Ur3+qyO/jZ5qeiLsB0qlCLB2GiAEkUOzf1Jk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109034818-d2a4cb704d18 h1:gLVliRJfS5obrBtc90bUcxuCM9agyX8ASuEq9xW9YcE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109034818-d2a4cb704d18/go.mod h1:YwgF7dbNF/toLe0TcL3EruyuDzugRDtISFed40Z/95g=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109065741-1aea8952d330 h1:EKLwhlS2gUTE5RtmUS2VB6fkuazy4sIdJ7rx3c1adL4=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109065741-1aea8952d330/go.mod h1:g6QOo3cvOHn0lZtzX1c+qiHUIx4d79hvhGSAhWDa4DQ=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109034818-d2a4cb704d18 h1:wGgEPQXXeCH+l7QBFSs9VpY+nUOEyShcaI8oL5DYnYo=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109034818-d2a4cb704d18/go.mod h1:YgoPyLVFXeY/ekpmAHUeydkhRFR+TW2f2zIapeFIGjQ=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109034818-d2a4cb704d18 h1:QYAC/o4u1UXBJiqo0UqM/38osA/78pXREQCnYR3KQ7Q=
Expand Down
6 changes: 4 additions & 2 deletions osmoutils/cosmwasm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ func Query[T any, K any](ctx sdk.Context, wasmKeeper WasmKeeper, contractAddress
return response, err
}

ctx = ctx.WithGasMeter(storetypes.NewGasMeter(wasmKeeper.QueryGasLimit()))
responseBz, err := wasmKeeper.QuerySmart(ctx, sdk.MustAccAddressFromBech32(contractAddress), bz)
childCtx := ctx.WithGasMeter(storetypes.NewGasMeter(wasmKeeper.QueryGasLimit()))
responseBz, err := wasmKeeper.QuerySmart(childCtx, sdk.MustAccAddressFromBech32(contractAddress), bz)
if err != nil {
return response, err
}

ctx.GasMeter().ConsumeGas(childCtx.GasMeter().GasConsumed(), "query smart")

if err := json.Unmarshal(responseBz, &response); err != nil {
return response, err
}
Expand Down
11 changes: 8 additions & 3 deletions x/cosmwasmpool/model/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/osmomath"
Expand Down Expand Up @@ -41,15 +42,19 @@ func (s *CosmWasmPoolSuite) TestGetSpreadFactor() {

// TestSpotPrice validates that spot price is returned as one.
func (s *CosmWasmPoolSuite) TestSpotPrice() {
var (
expectedSpotPrice = osmomath.OneBigDec()
)
var expectedSpotPrice = osmomath.OneBigDec()

pool := s.PrepareCosmWasmPool()

s.Ctx = s.Ctx.WithGasMeter(sdk.NewGasMeter(1000000))

actualSpotPrice, err := pool.SpotPrice(s.Ctx, denomA, denomB)
s.Require().NoError(err)

// Validate that the gas was charged on the input context
endGas := s.Ctx.GasMeter().GasConsumed()
s.Require().NotZero(endGas)

s.Require().Equal(expectedSpotPrice, actualSpotPrice)

actualSpotPrice, err = pool.SpotPrice(s.Ctx, denomB, denomA)
Expand Down

0 comments on commit 370b381

Please sign in to comment.