Skip to content

solver: fetch stale contract states from the confidants, verified against the chain; sync_contract_execution - #83

Open
EnzinoBB wants to merge 2 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/contract-state-fetch
Open

EnzinoBB wants to merge 2 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/contract-state-fetch

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown

Why this PR

A node that replays contract calls while syncing cannot reproduce every execution (a contract whose result depends on its own balance, for instance). That contract's state stays stale: the node never gets the state the chain declares, and its later executions start from a wrong state. The existing state request path accepts a peer's state when no matching new_state is found in the following blocks, so a state is also taken unverified when its block is not stored. Reported privately to the Credits team (part of problem 8 of our report).

This PR builds on the fee-rule PR ("charge an emitted transaction to the contract's latest successful execution"): its branch contains that commit first.

What changes

  • Each contract's StateItem (solver/include/solver/smartcontracts.hpp) records the call and hash of the last new_state with a state (declared_ref, declared_hash), whether or not the node obtained it (is_stale, count_stale, record_declared). The declaration is serialized after the existing fields only when known, so items of existing checkpoints keep their bytes and hashes; the quick-start validation fills it from the chain.
  • StateFetcher (new solver/include/solver/statefetcher.hpp): once the node is live (first live block after read ones) every stale contract is requested from the confidants with the existing StateRequest, at most 16 a round, oldest first, retried after 50, 100, ... up to 5000 rounds.
  • net_update_contract_state takes a StateReply only for a contract this node asked for, and only for the declared call with the declared hash (verify_state_reply); newer, older or other states are ignored; the cache is overwritten (a re-execution may have stored a wrong state for the same call). Without a declaration (old checkpoint) the state is checked against its block as before, but no longer taken when that block is not stored; a state of an unknown contract is refused.
  • Removing a block moves the declaration back to the latest successful new_state left on the chain.
  • get_actual_state warns when a state a block declares is not available.
  • [params] sync_contract_execution (client/config, default true = current behaviour): false skips re-executing contract calls while reading the blockchain; every state is then fetched once live, and syncing needs no executor.

Effect on the network

Local: the request and reply messages are the existing ones; a node only changes what it requests and accepts. A node with this change answers state requests as before. With the default configuration, sync behaves as today plus the fetch of stale states.

Tests

tests/csnode/smartcontracts_tests.cpp: declared state across a checkpoint and the earlier layout, is_stale, the reply check, the fetcher's pacing and backoff. Built and run on 4a95aaa (GCC 13). In the fork, private-network scenarios with a dice contract check that a node joining through the plays ends with the live nodes' contract state, with sync_contract_execution on and off.

Same change in the community fork: CreditsCommunity#95 and #96 (and the warning of #83), running on mainnet community nodes since community.12.

EnzinoBB and others added 2 commits November 1, 2026 12:13
…sful execution in its block

A live node applies every new_state of a block (Conveyer::statesCreated -> on_update) before it
charges any transaction the contract emitted, so it charges the fee of every payout of the block to
the contract's latest successful execution in that block. A node that replays the block while
syncing charges SmartContracts' known `ref_execute` instead: when it could not reproduce an
execution (a contract whose result depends on its own balance, for instance), that is an earlier
execution, and the fee goes to an earlier caller. The syncing node then ends with balances that
differ from the live nodes'.

- SmartContracts::emitting_new_state: the highest RefStart among the contract's new_states of the
  block with a non-zero hash (a failed execution does not count)
- on_next_block_impl charges the emitted transaction's fee to that execution's initiator, and to
  the last known execution only when the block has none (logged as a warning)
- on_remove_block refunds by the same rule, so that a rollback undoes what was charged
- unit test: SmartContract.EmittedTransactionIsChargedToTheLatestExecutionInTheBlock

Consensus-relevant: compared with an official node that syncs the same blocks, the balances of the
callers of such contracts change by one fee per affected payout, and agree with the nodes that
executed the contract live. All nodes should take this rule together.

Same change in the community fork: #94.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…inst the chain

A node that replays contract calls while syncing cannot reproduce every execution (a contract
whose result depends on its own balance, for instance). Such a contract's state stays stale, the
node never obtains the state the chain declares, and its later executions start from a wrong
state. This change lets the node notice and repair that.

- each contract's StateItem records the call and the hash of the last new_state with a state
  (declared_ref, declared_hash), whether or not the node obtained the state, so a node knows which
  contracts it holds stale (is_stale, count_stale). The declaration is serialized after the
  existing fields only when one is known: an item of an earlier checkpoint keeps its bytes and its
  hash; the quick-start validation fills it from the chain when the block is stored
- once the node is live (the first live block after read ones), every stale contract is asked for
  from the confidants with the existing StateRequest (StateFetcher, solver/statefetcher.hpp): at most
  16 a round, oldest first, each again after 50, 100, ... up to 5000 rounds
- a StateReply is taken only for a contract this node asked for, and only for the declared call
  with the declared hash (newer, older or other states are ignored); the cache is overwritten (a
  re-execution may have stored a wrong state for the same call). A contract without a declaration
  is checked against its block as before, but no longer taken unverified when the block is not
  stored; a state of an unknown contract is refused
- removing a block moves the declaration back to the latest successful new_state left on the chain
- get_actual_state warns when a state the block declares is not available
- [params] sync_contract_execution (default true, the current behaviour): false skips the
  re-execution of contract calls while the node reads the blockchain; every state is then fetched
  from the confidants once the node is live, and syncing needs no executor
- unit tests in tests/csnode/smartcontracts_tests.cpp (state bookkeeping, earlier layout, the reply
  check)

Local: the request and reply messages are the existing ones, and a node only changes what it
accepts. It builds on the PR that charges emitted transactions to the latest execution (fee rule).

Same change in the community fork: #95, #96 (and the warning of #83).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EnzinoBB

EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown
Author

Builds on #82: this branch contains its commit first, so merge #82 before this one (or merge this one alone, which brings #82 with it). If this PR is merged, #94 is no longer needed: the state fetch here only asks the confidants for states it needs and verifies them against the chain.

This branch has not been deployed

No deployments
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.

1 participant