diff --git a/.github/workflows/robustness.yml b/.github/workflows/robustness.yml
index f9370a71e..f8316e870 100644
--- a/.github/workflows/robustness.yml
+++ b/.github/workflows/robustness.yml
@@ -286,13 +286,12 @@ jobs:
env: DB_BACKEND=berkeleydb CONVERT_AT=1500 TARGET_SEQ=2500
# smart contracts (each node with its executor, contracts.py through n1) across the faults:
# six SIGKILLs of n6 and its executor while contracts are deployed and called
- # reproduction of the contract state divergence (docs/specs/2026-10-24-contract-state-sync-design.md):
- # a node that syncs from block 0 through plays of a contract reading its own balance; fails
- # until contract states are fetched instead of re-executed
+ # a node that syncs from block 0 through plays of a contract reading its own balance
+ # (docs/specs/2026-10-24-contract-state-sync-design.md): its balances and the contract's
+ # state must end as the live nodes', the state fetched from the confidants once it is live
- name: contracts-dice-join
env: DB_BACKEND=lmdb CONTRACTS=1 CONTRACT_DICE=1 JOIN_PRUNED_AT=500 JOIN_FULL=1 CONTRACTS_AFTER_AT=700 TARGET_SEQ=1300
contracts: true
- manual: true
- name: contracts-crash
env: DB_BACKEND=lmdb CRASH_NODE=6 CONTRACTS=1 TARGET_SEQ=1600
contracts: true
diff --git a/ci/testnet/contracts.py b/ci/testnet/contracts.py
index 25865b733..49aff8379 100644
--- a/ci/testnet/contracts.py
+++ b/ci/testnet/contracts.py
@@ -336,6 +336,18 @@ def dice_plays(client_for, master, dice):
while time.time() < deadline and balance(funder, dice) < before + 10:
time.sleep(3)
print(f"Dice: top-up after play {n + 1}: {'ok' if ok else 'FAILED ' + str(message)}, contract balance {balance(funder, dice)}", flush=True)
+ # two plays at once: both executions in one block, so that a node charges both payouts the way the
+ # live nodes do (to the latest successful execution of the block, not to the one before each)
+ master.next_inner_id = None
+ ok, message = send(funder, master, dice, 20)
+ deadline = time.time() + 120
+ while time.time() < deadline and balance(funder, dice) < 45:
+ time.sleep(3)
+ print(f"Dice: 20 CS more for two plays at once: {'ok' if ok else 'FAILED ' + str(message)}, contract balance {balance(funder, dice)}", flush=True)
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ results = list(pool.map(lambda i: call(client_for(1 + i), players[i], dice, "play", f"Dice play at once by player {i + 1}"),
+ range(2)))
+ print(f"Dice: two plays at once sent: {results}", flush=True)
return failures
@@ -561,7 +573,7 @@ def one(i):
if dice:
failures += dice_plays(client_for, master, dice)
- failures += read_all(dice, "plays", 3, "Dice.plays")
+ failures += read_all(dice, "plays", 5, "Dice.plays")
failures += read_all(counter, "get", calls, "Counter.get before restart")
failures += read_all(ledger, "entries", calls, "Ledger.entries before restart")
diff --git a/docs/CHANGES.md b/docs/CHANGES.md
index 5a6ad9bdc..0f46c789b 100644
--- a/docs/CHANGES.md
+++ b/docs/CHANGES.md
@@ -39,6 +39,9 @@ lines under the new release.
| #90 | csnode: `BlockVerifier` for blocks that come from outside the consensus (hash, signatures against the block's confidants, round confirmations), for the snapshot import and backward sync |
| #89 | csnode, client: snapshot format 2 built by the node from its own checkpoint and blocks on any backend (lz4, per-chunk SHA-256, manifest signed with the node key); `--export-snapshot
` |
| #92 | csnode, client: snapshot importer and `--import-snapshot`: a format-2 snapshot is checked file by file and block by block (signatures, round confirmations, hash chain to the declared head, checkpoint digest) and written into a new LMDB or RocksDB database; the CI snapshot test uses export and import; digest_offline waits for the node to stop |
+| #93 | ci(testnet): a Dice contract that reads its own balance, played by two players (also twice in one block); scenario `contracts-dice-join`, where a node syncing through the plays must end with the live nodes' balances and contract state |
+| #94 | solver: an emitted transaction's fee is charged to the contract's latest successful execution in its block, as the live nodes charge it; a syncing node whose re-execution of a contract failed charged it to an earlier caller. Consensus-relevant: balances of such callers change by a fee compared with an official node that syncs the same blocks |
+| #95 | solver: each contract records the call and hash of the state the chain declares; once live, a node asks the confidants for the states it holds stale (paced, with backoff) and takes only the declared one; a state whose block is not stored is no longer taken unverified |
## community.11 — 2026-10-24
diff --git a/solver/CMakeLists.txt b/solver/CMakeLists.txt
index 2bcafc43c..ff752252f 100644
--- a/solver/CMakeLists.txt
+++ b/solver/CMakeLists.txt
@@ -21,6 +21,7 @@ add_library(${PROJECT_NAME} STATIC
include/solver/callsqueuescheduler.hpp
include/solver/timeouttracking.hpp
include/solver/smartcontracts.hpp
+ include/solver/statefetcher.hpp
include/solver/smartconsensus.hpp
include/solver/states/defaultstatebehavior.hpp
diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp
index 9e57effa2..02888d7b7 100644
--- a/solver/include/solver/smartcontracts.hpp
+++ b/solver/include/solver/smartcontracts.hpp
@@ -2,6 +2,7 @@
#include
#include
+#include
#include
#include
#include
@@ -359,6 +360,57 @@ class SmartContracts final {
static std::optional emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr,
const std::function& absolute);
+ // public for the tests of the state bookkeeping (StateItem::to_bytes/from_bytes, is_stale)
+ enum class PayableStatus : int
+ {
+ Unknown = -1,
+ Absent = 0,
+ Implemented = 1,
+ ImplementedVer1 = 2
+ };
+
+ // defines current contract state, the contracts cache is a container of every contract state
+ struct StateItem {
+ std::string toString();
+ std::string transactionToString(const csdb::Transaction& tr);
+ Bytes to_bytes();
+ static StateItem from_bytes(Bytes& data);
+ // payable() method is implemented
+ PayableStatus payable{ PayableStatus::Unknown };
+ // reference to deploy transaction
+ SmartContractRef ref_deploy;
+ // reference to last successful execution which state is stored by item, may be equal to ref_deploy
+ SmartContractRef ref_execute;
+ // Reference to execution which state is cached in DB
+ SmartContractRef ref_cache;
+ // Reference to last state update transaction
+ SmartContractRef ref_state;
+ // deploy transaction referenced by ref_deploy
+ csdb::Transaction deploy{};
+ // the last execute transaction referenced by ref_execute
+ csdb::Transaction execute{};
+ // current state which is result of last successful execution / deploy
+ std::string state;
+ // using other contracts: [own_method] - [ [other_contract - its_method], ... ], ...
+ std::map> uses;
+ // what the chain declares the contract's state is: the call of the last new_state with a
+ // state (RefStart) and that state's hash, recorded whether or not this node obtained the
+ // state; zero hash: unknown (a checkpoint of an earlier build whose block is pruned)
+ SmartContractRef declared_ref;
+ cs::Hash declared_hash{};
+ };
+
+ // the state the item holds is not the one the chain declares (an unknown declaration is not stale)
+ static bool is_stale(const StateItem& item);
+
+ // a state a peer sent for the call reply_ref, against what the chain declares for the contract
+ enum class StateReplyCheck { Accepted, Newer, Older, HashMismatch, Undeclared };
+ static StateReplyCheck verify_state_reply(const SmartContractRef& reply_ref, const std::string& state, const StateItem& item);
+ size_t count_stale() const;
+ // records the call and hash a new_state declares into the contract's item, if it has one
+ void record_declared(const csdb::Address& abs_addr, const SmartContractRef& ref_start, const csdb::Transaction& new_state);
+
+
// locked contracts that are neither executed now nor used by an executed contract
static std::vector stale_locks(const std::unordered_set& locked, const std::vector& in_use);
@@ -602,49 +654,19 @@ public slots:
CallsQueueScheduler::CallTag tag_cancel_running_contract;
- enum class PayableStatus : int
- {
- Unknown = -1,
- Absent = 0,
- Implemented = 1,
- ImplementedVer1 = 2
- };
-
- // defines current contract state, the contracts cache is a container of every contract state
- struct StateItem {
- std::string toString();
- std::string transactionToString(const csdb::Transaction& tr);
- Bytes to_bytes();
- static StateItem from_bytes(Bytes& data);
- // payable() method is implemented
- PayableStatus payable{ PayableStatus::Unknown };
- // reference to deploy transaction
- SmartContractRef ref_deploy;
- // reference to last successful execution which state is stored by item, may be equal to ref_deploy
- SmartContractRef ref_execute;
- // Reference to execution which state is cached in DB
- SmartContractRef ref_cache;
- // Reference to last state update transaction
- SmartContractRef ref_state;
- // deploy transaction referenced by ref_deploy
- csdb::Transaction deploy{};
- // the last execute transaction referenced by ref_execute
- csdb::Transaction execute{};
- // current state which is result of last successful execution / deploy
- std::string state;
- // using other contracts: [own_method] - [ [other_contract - its_method], ... ], ...
- std::map> uses;
- };
-
// last contract's state storage
std::unordered_map known_contracts;
std::unordered_set blacklistedContracts_;
std::unordered_set locked_contracts;
- // contracts whose state this node asked the confidants for, with the round of the last request;
- // a StateReply for any other contract is not an answer to this node
- std::unordered_map pending_state_requests;
+ // contracts whose state is not the one the chain declares: asked for at the head, paced; a
+ // StateReply for any other contract is not an answer to this node
+ StateFetcher state_fetcher_;
+ // the previous stored block was read (sync, start-up), not live: the next live one starts a fetch
+ bool was_reading_db_ = true;
+ cs::RoundNumber last_state_summary_ = 0;
+ void on_live_block(cs::RoundNumber round);
//uint64_t testValue = 0;
diff --git a/solver/include/solver/statefetcher.hpp b/solver/include/solver/statefetcher.hpp
new file mode 100644
index 000000000..07f214b5b
--- /dev/null
+++ b/solver/include/solver/statefetcher.hpp
@@ -0,0 +1,88 @@
+#ifndef STATEFETCHER_HPP
+#define STATEFETCHER_HPP
+
+#include
+#include
+#include