diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a36051..36d2fcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Improvements +- [#219](https://github.com/babylonlabs-io/babylon-sdk/pull/219) bugfix: + cacheContext not passed to sudo - [#217](https://github.com/babylonlabs-io/babylon-sdk/pull/217) chore: fix go mod - [#215](https://github.com/babylonlabs-io/babylon-sdk/pull/215) chore: bump diff --git a/x/babylon/keeper/wasm.go b/x/babylon/keeper/wasm.go index 861b338..01f55f2 100644 --- a/x/babylon/keeper/wasm.go +++ b/x/babylon/keeper/wasm.go @@ -129,8 +129,10 @@ func (k Keeper) doSudoCallWithGasLimit(ctx sdk.Context, contractAddr sdk.AccAddr return } - // Create a gas-limited context - gasCtx := ctx.WithGasMeter(storetypes.NewGasMeter(maxGas)) + // Create a cache context to ensure atomicity; commit only on success + cacheCtx, write := ctx.CacheContext() + // Create a gas-limited context on top of the cache context + gasCtx := cacheCtx.WithGasMeter(storetypes.NewGasMeter(maxGas)) // Use defer to recover from panics that might occur during contract execution defer func() { @@ -151,6 +153,8 @@ func (k Keeper) doSudoCallWithGasLimit(ctx sdk.Context, contractAddr sdk.AccAddr return } + // Commit cached writes only after successful execution + write() gasConsumed = gasCtx.GasMeter().GasConsumed() return