Skip to content

Feature/2491 refund ctx - #2492

Open
PropzSaladaz wants to merge 10 commits into
developfrom
feature/2491-refund-CTX
Open

Feature/2491 refund ctx#2492
PropzSaladaz wants to merge 10 commits into
developfrom
feature/2491-refund-CTX

Conversation

@PropzSaladaz

@PropzSaladaz PropzSaladaz commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR introduces timestamp-gated refund handling for CTX transactions.

After a CTX finishes execution, its remaining ephemeral-account balance is transferred back to the target contract that originated the CTX. The ephemeral sender nonce is then reset to zero, allowing the empty account to be pruned during state commit.

CTX Behavior

CTX is always refunded. Even if it is reverted, the remainder is sent to where CTX is aimed at - the contract's balance.

Changes

A. New patch timestamp

  • Added RefundCTXPatch.
  • Added support for the RefundCTXPatchTimestamp chain configuration field.
  • Preserved legacy behavior when the patch is disabled or has not reached its activation timestamp.
  • Limited the patch to BITE-enabled builds.

B. CTX refund and ephemeral-account cleanup

Implemented equivalent behavior in both execution paths:

  • Current execution through Executive.
  • Historic execution through AlethExecutive.

When the patch is active and the transaction is a CTX:

  1. Normal gas accounting refunds unused gas to the ephemeral sender.
  2. The sender’s remaining balance is transferred to the CTX target contract.
  3. The ephemeral sender nonce is reset to zero.
  4. The now-empty account is removed by normal state pruning.

The regular consensus transaction path also rejects CTX-shaped transactions once the patch is active, preventing user-submitted transactions from triggering destructive ephemeral-account cleanup.
There were already checks at RPC level, but this PR adds additional checks during protocol parsing layer.

C. CTX test refactoring

  • Extracted reusable helpers for deploying the CTX test contract, encoding submitCTX input, and executing the complete CTX lifecycle.
  • Added assertions for balances and nonce cleanup when RefundCTXPatch is enabled.
  • Added explicit coverage for legacy behavior when the patch is disabled.

Compatibility

Before activation, CTX gas refunds retain their previous behavior. Regular transactions are unaffected.

@PropzSaladaz PropzSaladaz self-assigned this Jun 18, 2026
@github-actions

Copy link
Copy Markdown

@PropzSaladaz PropzSaladaz linked an issue Jun 19, 2026 that may be closed by this pull request
@PropzSaladaz
PropzSaladaz marked this pull request as ready for review June 19, 2026 17:06
Copilot AI review requested due to automatic review settings June 19, 2026 17:06
@PropzSaladaz
PropzSaladaz requested a review from kladkogex as a code owner June 19, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new BITE-gated chain patch (RefundCTXPatch) that changes CTX post-processing to sweep remaining ephemeral CTX sender balance back to the originating contract and reset the ephemeral sender nonce, plus tests and safety guards around CTX classification.

Changes:

  • Introduce RefundCTXPatch (enum + name mapping) and apply it in Executive/AlethExecutive finalization for CTX transactions.
  • Add a SkaleHost safeguard to reject any “regular” transaction that would be interpreted as CTX when the refund patch is active.
  • Expand/adjust unit tests to validate refund behavior (patch on/off), plus add a test-only block-import barrier helper to reduce snapshot/estimateGas race conditions.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/unittests/libweb3jsonrpc/jsonrpc.cpp Adds config overrides + new CTX crafting/refund behavior tests and updates BITE config JSON to include RefundCTXPatchTimestamp.
test/unittests/libskale/HashSnapshot.cpp Serializes snapshot creation with block import via a new test helper barrier.
test/unittests/libethereum/PrecompiledTest.cpp Adds RAII guard for global BITE host wiring/teardown in submitCTX ciphertext validation tests.
test/unittests/libethereum/ClientTest.cpp Wraps estimateGas() calls with the new block-import barrier to avoid races.
libhistoric/AlethExecutive.h Declares CTX refund+ephemeral nonce reset helper under #ifdef BITE.
libhistoric/AlethExecutive.cpp Implements refund logic for historic execution path and gates it behind RefundCTXPatch.
libethereum/SkaleHost.cpp Adds runtime check to prevent CTX-marked txs from appearing in the regular tx list when refund patch is enabled.
libethereum/SchainPatchEnum.h Adds RefundCTXPatch to the patch enum (BITE builds).
libethereum/SchainPatch.h Defines the RefundCTXPatch patch type and documents its purpose.
libethereum/SchainPatch.cpp Adds string↔enum mapping for RefundCTXPatch.
libethereum/Executive.h Declares CTX refund+ephemeral nonce reset helper under #ifdef BITE.
libethereum/Executive.cpp Applies refund behavior in normal execution path finalization when patch is enabled.
libethereum/ClientTest.h Declares ClientTest::withBlockImportBarrier() test-only helper.
libethereum/ClientTest.cpp Implements withBlockImportBarrier() using m_blockImportMutex.

Comment thread test/unittests/libweb3jsonrpc/jsonrpc.cpp Outdated
Comment thread test/unittests/libweb3jsonrpc/jsonrpc.cpp
Comment thread test/unittests/libweb3jsonrpc/jsonrpc.cpp Outdated
Comment thread libethereum/SkaleHost.cpp Outdated
Comment thread libethereum/Executive.cpp
Comment thread libhistoric/AlethExecutive.cpp Outdated
badrogger
badrogger previously approved these changes Jun 24, 2026
Base automatically changed from v5.2.0 to develop July 6, 2026 10:57
@DmytroNazarenko
DmytroNazarenko dismissed badrogger’s stale review July 6, 2026 10:57

The base branch was changed.

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.

Refund CTX to Smart Contract that issued CTX

3 participants