Skip to content

bugfix: cacheContext not passed to sudo#219

Merged
SebastianElvis merged 2 commits intomainfrom
bugfix/wrong_context_passed
Sep 8, 2025
Merged

bugfix: cacheContext not passed to sudo#219
SebastianElvis merged 2 commits intomainfrom
bugfix/wrong_context_passed

Conversation

@0xAleksaOpacic
Copy link
Contributor

@0xAleksaOpacic 0xAleksaOpacic commented Sep 2, 2025

Description

This PR ensures that we always work with a cachedContext when executing sudo logic. The store will only be updated if execution completes successfully (no error or panic). This prevents partial writes and ensures state consistency.

Why it matters

Without using a CacheContext, passing a plain ctx (sdk.UnwrapSDKContext(c)) means that any call to store.set or save inside a contract is written directly to the persistent MultiStore. This creates the risk of partial state commits:

BLOCKS.save(store, block.height, block)?;    // committed
// Out of gas happens here
NEXT_HEIGHT.save(store, &(block.height + 1))?;   // never reached

If an out-of-gas panic occurs after the first save, your defer may recover and return an error, but the earlier write has already been persisted. This leaves the chain in an inconsistent or unexpected state.

Solution

By wrapping the context with CacheContext, all writes are applied to a cached store. Only if execution completes without errors or panics do we commit the changes back to the main store. If an error or panic is encountered, the cached writes are discarded, ensuring atomicity of state changes.

@0xAleksaOpacic 0xAleksaOpacic self-assigned this Sep 2, 2025
Copilot AI review requested due to automatic review settings September 2, 2025 17:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where state changes from sudo contract calls were persisted even when the call failed or panicked. The fix implements proper transaction atomicity by using a cache context that only commits changes on successful execution.

  • Introduces cache context for atomic operations during sudo calls
  • Ensures state changes are only committed on successful execution
  • Prevents partial state modifications when contract execution fails

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@SebastianElvis SebastianElvis merged commit 99968bd into main Sep 8, 2025
20 checks passed
@SebastianElvis SebastianElvis deleted the bugfix/wrong_context_passed branch September 8, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants