Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: charge gas when querying cw contracts #7209

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#7106](https://github.com/osmosis-labs/osmosis/pull/7106) Halve the time of log2 calculation (speeds up TWAP code)
* [#7093](https://github.com/osmosis-labs/osmosis/pull/7093),[#7100](https://github.com/osmosis-labs/osmosis/pull/7100),[#7172](https://github.com/osmosis-labs/osmosis/pull/7093) Lower CPU overheads of the Osmosis epoch.

### 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 @@ -30,7 +30,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.20231215070550-ca294af82738
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231211161501-28475581c846
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231225235837-6b24d039bebc
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231211161501-28475581c846
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231211161501-28475581c846
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,8 @@ github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231215070550-ca294af82738 h1
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231215070550-ca294af82738/go.mod h1:FQ45vPTc2l+NUA/gk7wZTxUh/hrDRWrRwZj8Ua5cpSU=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231211161501-28475581c846 h1:+xo3n7+SwSiH0VZSYSSHWPkXx8XD/l01OnTEfYafAFw=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231211161501-28475581c846/go.mod h1:BsZRNH2ypxCdG8VObfFzbzjSflX5umvCziyBTAS9Ftw=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231225235837-6b24d039bebc h1:4Az8lkK3/YLFVeROe5C0dtF0V7Lw56m960I4QfN43Fc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231225235837-6b24d039bebc/go.mod h1:BsZRNH2ypxCdG8VObfFzbzjSflX5umvCziyBTAS9Ftw=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231211161501-28475581c846 h1:ZnZKvZGVcirtNYZ+nk+qr0GO4ljxGe/z7X5+7+UklsM=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231211161501-28475581c846/go.mod h1:lFy4X89dA7UUm+//XXTtIfJnq/zxkqIUOwxdMx+HD5k=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231211161501-28475581c846 h1:/phDqythG9OvuobKmEkeHHRHGjhEl/AIc4YQYM372xw=
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