From 54477c989397d22cdfd01ce496ccfeeebba4084b Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 16:38:12 +0200 Subject: [PATCH 1/5] solver: contracts record the state the chain declares Each contract's StateItem keeps the call and the hash of the last new_state with a state (declared_ref, declared_hash), whether or not this node obtained the state: a node knows which contracts it holds stale (is_stale, count_stale) and can verify a state from a peer without the block. Serialized after the existing fields (checkpoints of earlier builds load without them; the quick-start validation fills them from the chain when the block is still stored and logs how many are stale). Step 2 of the contract state sync plan. Co-Authored-By: Claude Opus 5.5 --- solver/include/solver/smartcontracts.hpp | 81 ++++++++++++++---------- solver/src/smartcontracts.cpp | 73 ++++++++++++++++++++- tests/csnode/smartcontracts_tests.cpp | 38 +++++++++++ 3 files changed, 157 insertions(+), 35 deletions(-) diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp index dd3feec0d..a88b7b6e6 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -360,6 +360,53 @@ 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); + 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); @@ -603,40 +650,6 @@ 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; diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index e78d1771d..fcf35ba96 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -2609,6 +2609,51 @@ void SmartContracts::on_update(const std::vector< csdb::Transaction >& states) { } } +void SmartContracts::record_declared(const csdb::Address& abs_addr, const SmartContractRef& ref_start, const csdb::Transaction& new_state) { + const auto it = known_contracts.find(abs_addr); + if (it == known_contracts.end()) { + return; + } + const csdb::UserField fld = new_state.user_field(trx_uf::new_state::Hash); + if (!fld.is_valid()) { + return; + } + const std::string bytes = fld.value(); + cs::Hash hash{}; + if (bytes.size() != hash.size()) { + return; + } + std::copy(bytes.cbegin(), bytes.cend(), hash.begin()); + // a failed execution (zero hash) leaves the state of the last successful call in place + if (hash == cs::Zero::hash) { + return; + } + StateItem& item = it->second; + if (!item.declared_ref.is_valid() || item.declared_ref < ref_start || item.declared_ref == ref_start) { + item.declared_ref = ref_start; + item.declared_hash = hash; + } +} + +/*static*/ +bool SmartContracts::is_stale(const StateItem& item) { + if (item.declared_hash == cs::Zero::hash) { + return false; + } + if (item.state.empty()) { + return true; + } + return cscrypto::calculateHash(reinterpret_cast(item.state.data()), item.state.size()) != item.declared_hash; +} + +size_t SmartContracts::count_stale() const { + size_t n = 0; + for (const auto& [addr, item] : known_contracts) { + n += is_stale(item) ? 1 : 0; + } + return n; +} + bool SmartContracts::update_contract_state(const csdb::Transaction& t, bool reading_db) { //csdebug() << kLogPrefix << __func__; using namespace trx_uf; @@ -2634,6 +2679,9 @@ bool SmartContracts::update_contract_state(const csdb::Transaction& t, bool read return false; } + // what the chain declares, whether or not the state can be obtained below + record_declared(abs_addr, ref_start, t); + if (in_known_contracts(abs_addr)) { //csdebug() << kLogPrefix << __func__ << " __2"; StateItem& item = known_contracts[abs_addr]; @@ -2683,6 +2731,7 @@ bool SmartContracts::update_contract_state(const csdb::Transaction& t, bool read // create or get contract state item StateItem& item = known_contracts[abs_addr]; + record_declared(abs_addr, ref_start, t); // a deploy creates the item only here // update state value in cache if it is older then or equal to or unset constexpr bool force_update_contracts_cache = false; //csdebug() << kLogPrefix << __func__ << " __8: " @@ -3241,6 +3290,7 @@ size_t SmartContracts::validateRestoredStatesAgainstChain(BlockChain& blockchain size_t invalidated_hash_mismatch = 0; size_t invalidated_tx_missing = 0; size_t unverifiable = 0; + size_t declared_from_chain = 0; for (auto& kv : known_contracts) { const auto& abs_addr = kv.first; auto& item = kv.second; @@ -3273,6 +3323,15 @@ size_t SmartContracts::validateRestoredStatesAgainstChain(BlockChain& blockchain cs::Hash chain_hash; if (h_str.size() != chain_hash.size()) { ++unverifiable; continue; } std::copy(h_str.cbegin(), h_str.cend(), chain_hash.begin()); + // a checkpoint of an earlier build has no declaration: the chain still has it here + if (item.declared_hash == cs::Zero::hash && chain_hash != cs::Zero::hash) { + const csdb::UserField ref_fld = t.user_field(new_state::RefStart); + if (ref_fld.is_valid()) { + item.declared_ref = SmartContractRef(ref_fld); + item.declared_hash = chain_hash; + ++declared_from_chain; + } + } if (local_hash != chain_hash) { cswarning() << kLogPrefix << to_base58(abs_addr) << ": qs-restored state hash diverges from chain at " @@ -3311,7 +3370,8 @@ size_t SmartContracts::validateRestoredStatesAgainstChain(BlockChain& blockchain cslog() << kLogPrefix << "qs state validation: " << verified << " verified, " << invalidated_hash_mismatch << " invalidated (hash mismatch), " << invalidated_tx_missing << " invalidated (tx missing), " - << unverifiable << " unverifiable (no hash/value on chain)"; + << unverifiable << " unverifiable (no hash/value on chain), " + << declared_from_chain << " declared from chain, " << count_stale() << " stale"; return invalidated_hash_mismatch + invalidated_tx_missing; } @@ -3696,6 +3756,9 @@ Bytes SmartContracts::StateItem::to_bytes() { ++it; } } + // appended: a checkpoint of an earlier build has none, and an earlier build ignores them + os << declared_ref.sequence << declared_ref.transaction; + os << declared_hash; return data; } @@ -3732,6 +3795,14 @@ SmartContracts::StateItem SmartContracts::StateItem::from_bytes(Bytes& data) { } res.uses.try_emplace(u1, iMap); } + if (is.isAvailable(sizeof(res.declared_ref.sequence) + sizeof(res.declared_ref.transaction) + res.declared_hash.size())) { + is >> res.declared_ref.sequence >> res.declared_ref.transaction; + is >> res.declared_hash; + if (!is.isValid()) { + res.declared_ref = SmartContractRef{}; + res.declared_hash = cs::Hash{}; + } + } return res; } diff --git a/tests/csnode/smartcontracts_tests.cpp b/tests/csnode/smartcontracts_tests.cpp index 4e3ff8470..75dbf3880 100644 --- a/tests/csnode/smartcontracts_tests.cpp +++ b/tests/csnode/smartcontracts_tests.cpp @@ -142,3 +142,41 @@ TEST(SmartContract, EmittedTransactionBelongsToTheNewStateBeforeIt) { EXPECT_FALSE(cs::SmartContracts::emitting_new_state(block, 3, B, identity).has_value()); EXPECT_TRUE(cs::SmartContracts::emitting_new_state(block, 100, A, identity).has_value()); } + +// step 2 of the contract state sync plan: the declared call and hash survive a checkpoint, an item of +// an earlier build loads without them, and staleness compares the state with the declared hash +TEST(SmartContract, DeclaredStateSurvivesCheckpointAndOldItemsLoad) { + cs::SmartContracts::StateItem item; + item.ref_execute.sequence = 7; + item.ref_execute.transaction = 1; + item.state = "state after 7.1"; + item.declared_ref = item.ref_execute; + item.declared_hash = cscrypto::calculateHash(reinterpret_cast(item.state.data()), item.state.size()); + + auto bytes = item.to_bytes(); + const auto back = cs::SmartContracts::StateItem::from_bytes(bytes); + EXPECT_EQ(back.declared_ref.sequence, 7u); + EXPECT_EQ(back.declared_ref.transaction, 1u); + EXPECT_EQ(back.declared_hash, item.declared_hash); + EXPECT_EQ(back.state, item.state); + + // an earlier build wrote no declaration: the same bytes without the tail + auto old = item.to_bytes(); + old.resize(old.size() - (sizeof(item.declared_ref.sequence) + sizeof(item.declared_ref.transaction) + item.declared_hash.size())); + const auto earlier = cs::SmartContracts::StateItem::from_bytes(old); + EXPECT_EQ(earlier.state, item.state); + EXPECT_FALSE(earlier.declared_ref.is_valid()); + EXPECT_EQ(earlier.declared_hash, cs::Hash{}); +} + +TEST(SmartContract, StaleMeansNotTheDeclaredState) { + cs::SmartContracts::StateItem item; + item.state = "current"; + EXPECT_FALSE(cs::SmartContracts::is_stale(item)); // nothing declared: unknown, not stale + item.declared_hash = cscrypto::calculateHash(reinterpret_cast(item.state.data()), item.state.size()); + EXPECT_FALSE(cs::SmartContracts::is_stale(item)); // holds the declared state + item.state = "an earlier one"; + EXPECT_TRUE(cs::SmartContracts::is_stale(item)); // another state + item.state.clear(); + EXPECT_TRUE(cs::SmartContracts::is_stale(item)); // no state at all +} From ca01f727ffb53b9e37fb16fb4349691b3b2c1e31 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 16:49:39 +0200 Subject: [PATCH 2/5] solver: stale contract states are fetched from the confidants and verified Once the node is live (the first live block after read ones), every contract whose state is not the one the chain declares is asked for from the confidants with the existing StateRequest, at most 16 a round, oldest first, each one again after 50, 100, ... up to 5000 rounds. A reply is taken only for the declared call with the declared hash (newer, older or other states are ignored), and the cache is overwritten (a re-execution may have stored a wrong state for the same call). A contract without a declaration (a checkpoint of an earlier build) 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. The node logs each verified state and a summary of known, stale and pending contracts when it becomes live and every 1000 rounds. contracts-dice-join is no longer manual: the joining node must end with the same balances and Dice state as the live nodes. Step 3 of the contract state sync plan. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/robustness.yml | 7 +- solver/CMakeLists.txt | 1 + solver/include/solver/smartcontracts.hpp | 15 +- solver/include/solver/statefetcher.hpp | 88 +++++++++++ solver/src/smartcontracts.cpp | 187 +++++++++++++++-------- tests/csnode/smartcontracts_tests.cpp | 49 ++++++ 6 files changed, 279 insertions(+), 68 deletions(-) create mode 100644 solver/include/solver/statefetcher.hpp diff --git a/.github/workflows/robustness.yml b/.github/workflows/robustness.yml index f4e53e5ec..7c786e7b1 100644 --- a/.github/workflows/robustness.yml +++ b/.github/workflows/robustness.yml @@ -276,13 +276,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/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 a88b7b6e6..b2eaf0792 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -402,6 +403,10 @@ class SmartContracts final { // 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); @@ -656,9 +661,13 @@ public slots: 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 +#include + +#include +#include + +namespace cs { + +// The contracts whose state this node holds is not the one the chain declares, and when to ask the +// confidants for it again: at most kRequestsPerRound requests a round, oldest first, each contract +// waiting min(kBaseBackoff * 2^attempts, kMaxBackoff) rounds after a request +// (docs/specs/2026-10-24-contract-state-sync-design.md, 3.4). It only keeps the bookkeeping: the +// caller sends the requests and verifies the replies. +class StateFetcher { +public: + static constexpr size_t kRequestsPerRound = 16; + static constexpr cs::RoundNumber kBaseBackoff = 50; + static constexpr cs::RoundNumber kMaxBackoff = 5000; + + void markStale(const csdb::Address& contract) { + entries_.try_emplace(contract); + } + + // the state arrived and verified, or the contract is no longer stale + void done(const csdb::Address& contract) { + entries_.erase(contract); + } + + // a reply about this contract is an answer to this node + bool isWanted(const csdb::Address& contract) const { + return entries_.count(contract) != 0; + } + + // the contracts to ask about in this round; each one counts as asked + std::vector due(cs::RoundNumber round, size_t limit = kRequestsPerRound) { + std::vector> ready; + for (const auto& [contract, e] : entries_) { + if (!e.asked || round >= e.lastRequest + backoff(e.attempts)) { + ready.emplace_back(e.asked ? e.lastRequest : 0, contract); + } + } + std::sort(ready.begin(), ready.end(), [](const auto& a, const auto& b) { return a.first < b.first; }); + std::vector out; + for (size_t i = 0; i < ready.size() && out.size() < limit; ++i) { + auto& e = entries_[ready[i].second]; + if (e.asked) { + ++e.attempts; + } + e.asked = true; + e.lastRequest = round; + out.push_back(ready[i].second); + } + return out; + } + + size_t size() const { + return entries_.size(); + } + + void reset() { + entries_.clear(); + } + + static cs::RoundNumber backoff(uint32_t attempts) { + cs::RoundNumber b = kBaseBackoff; + for (uint32_t i = 0; i < attempts && b < kMaxBackoff; ++i) { + b *= 2; + } + return std::min(b, kMaxBackoff); + } + +private: + struct Entry { + bool asked = false; + cs::RoundNumber lastRequest = 0; + uint32_t attempts = 0; + }; + std::map entries_; +}; + +} // namespace cs + +#endif // STATEFETCHER_HPP diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index fcf35ba96..913f39a8a 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -1428,12 +1428,12 @@ void SmartContracts::on_store_block(const csdb::Pool& block) { cs::Lock lock(public_access_lock); bool should_stop = false; - if (cur_round > last_block && cur_round - last_block > 1) { - on_next_block_impl(block, true, &should_stop); - } - else { - on_next_block_impl(block, false, &should_stop); + const bool reading_db = cur_round > last_block && cur_round - last_block > 1; + on_next_block_impl(block, reading_db, &should_stop); + if (!reading_db) { + on_live_block(cur_round); } + was_reading_db_ = reading_db; } /*public*/ @@ -3397,8 +3397,58 @@ bool SmartContracts::wait_until_executor(unsigned int test_freq, unsigned int ma } void SmartContracts::net_request_contract_state(const csdb::Address& abs_addr) { - pending_state_requests[abs_addr] = cs::Conveyer::instance().currentRoundNumber(); - pnode->sendStateRequest(abs_addr, cs::Conveyer::instance().confidants()); + // asked at the next live block, paced with the other stale contracts + state_fetcher_.markStale(abs_addr); +} + +/*static*/ +SmartContracts::StateReplyCheck SmartContracts::verify_state_reply(const SmartContractRef& reply_ref, const std::string& state, const StateItem& item) { + if (item.declared_hash == cs::Zero::hash || !item.declared_ref.is_valid()) { + return StateReplyCheck::Undeclared; + } + if (item.declared_ref < reply_ref) { + return StateReplyCheck::Newer; // the peer executed a call this node has not reached yet + } + if (reply_ref < item.declared_ref) { + return StateReplyCheck::Older; // the peer's state is stale + } + if (state.empty() || cscrypto::calculateHash(reinterpret_cast(state.data()), state.size()) != item.declared_hash) { + return StateReplyCheck::HashMismatch; + } + return StateReplyCheck::Accepted; +} + +void SmartContracts::on_live_block(cs::RoundNumber round) { + if (was_reading_db_) { + // the node has just become live: every contract whose state is not the declared one is asked for + for (const auto& [addr, item] : known_contracts) { + if (is_stale(item)) { + state_fetcher_.markStale(addr); + } + } + cslog() << kLogPrefix << "contract states: " << known_contracts.size() << " known, " << count_stale() << " stale, " + << state_fetcher_.size() << " to fetch from the confidants"; + last_state_summary_ = round; + } + if (state_fetcher_.size() > 0) { + const auto confidants = cs::Conveyer::instance().confidants(); + if (!confidants.empty()) { + for (const auto& addr : state_fetcher_.due(round)) { + // updated meanwhile, by a live new_state with its state + const auto it = known_contracts.find(addr); + if (it != known_contracts.end() && !is_stale(it->second) && it->second.declared_hash != cs::Zero::hash) { + state_fetcher_.done(addr); + continue; + } + pnode->sendStateRequest(addr, confidants); + } + } + if (round >= last_state_summary_ + 1000) { + cslog() << kLogPrefix << "contract states: " << known_contracts.size() << " known, " << count_stale() << " stale, " + << state_fetcher_.size() << " to fetch from the confidants"; + last_state_summary_ = round; + } + } } /*public*/ @@ -3412,17 +3462,12 @@ void SmartContracts::net_update_contract_state(const csdb::Address& contract_abs stream >> ref.sequence >> ref.transaction >> state; if (stream.isValid() && !stream.isAvailable(1)) { - // several confidants answer one request, so the entry stays until it expires - constexpr cs::RoundNumber kStateRequestRounds = 1000; - const cs::RoundNumber round = cs::Conveyer::instance().currentRoundNumber(); - const auto pending = pending_state_requests.find(contract_abs_addr); - if (pending == pending_state_requests.end() || pending->second + kStateRequestRounds < round) { - if (pending != pending_state_requests.end()) { - pending_state_requests.erase(pending); - } - cswarning() << kLogPrefix << "ignore " << cs::SmartContracts::to_base58(contract_abs_addr) << " state, it was not requested"; + // several confidants answer one request: the contract stays wanted until a state verifies + if (!state_fetcher_.isWanted(contract_abs_addr)) { + csdebug() << kLogPrefix << "ignore " << cs::SmartContracts::to_base58(contract_abs_addr) << " state, it was not requested"; return; } + const cs::RoundNumber round = cs::Conveyer::instance().currentRoundNumber(); // a state cannot refer to a block later than the current round, and caching one would make every // real update look outdated if (ref.sequence > round) { @@ -3430,60 +3475,78 @@ void SmartContracts::net_update_contract_state(const csdb::Address& contract_abs return; } + bool declared = false; if (in_known_contracts(contract_abs_addr)) { auto& item = known_contracts[contract_abs_addr]; - if (item.ref_execute > ref) { - cswarning() << kLogPrefix << "ignore outdated " << cs::SmartContracts::to_base58(contract_abs_addr) << " state"; - return; + // the chain says which call's state the contract holds and its hash: nothing else is taken + switch (verify_state_reply(ref, state, item)) { + case StateReplyCheck::Accepted: + declared = true; + break; + case StateReplyCheck::Newer: + csdebug() << kLogPrefix << "ignore " << to_base58(contract_abs_addr) << " state after " << ref << ", newer than the declared " + << item.declared_ref << ": asked again later"; + return; + case StateReplyCheck::Older: + csdebug() << kLogPrefix << "ignore " << to_base58(contract_abs_addr) << " state after " << ref << ", older than the declared " + << item.declared_ref; + return; + case StateReplyCheck::HashMismatch: + cswarning() << kLogPrefix << to_base58(contract_abs_addr) << " state received for " << ref << " does not have the declared hash"; + return; + case StateReplyCheck::Undeclared: + break; } - // test state hash before dbcache_update - csdebug() << kLogPrefix << "test " << cs::SmartContracts::to_base58(contract_abs_addr) << " state received"; - bool state_found = false; - bool state_test_passed = false; - SmartContractRef ref_state; - for (cs::Sequence i = ref.sequence; i < ref.sequence + Consensus::MaxRoundsCancelContract; ++i) { - csdb::Pool b = bc.loadBlock(i); - if (b.is_valid() && b.transactions_count() > 0) { + if (!declared) { + if (item.ref_execute > ref) { + cswarning() << kLogPrefix << "ignore outdated " << cs::SmartContracts::to_base58(contract_abs_addr) << " state"; + return; + } + // no declaration (a checkpoint of an earlier build): the new_state of that call must be in a stored block + bool state_found = false; + bool state_test_passed = false; + for (cs::Sequence i = ref.sequence; i < ref.sequence + Consensus::MaxRoundsCancelContract && !state_found; ++i) { + csdb::Pool b = bc.loadBlock(i); + if (!b.is_valid() || b.transactions_count() == 0) { + continue; + } for (const auto& t : b.transactions()) { - if (is_new_state(t)) { - csdb::UserField fld = t.user_field(trx_uf::new_state::RefStart); - if (fld.is_valid()) { - if (SmartContractRef(fld) == ref) { - // corresponding new state found, test it - state_found = true; - ref_state.sequence = t.id().pool_seq(); - ref_state.transaction = t.id().index(); - fld = t.user_field(trx_uf::new_state::Hash); - if (fld.is_valid()) { - std::string hash_bytes = fld.value(); - cs::Hash hash; - if (hash.size() == hash_bytes.size()) { - std::copy(hash_bytes.cbegin(), hash_bytes.cend(), hash.begin()); - if (state.empty()) { - state_test_passed = (hash == cs::Zero::hash); - } - else { - state_test_passed = (hash == cscrypto::calculateHash((cs::Byte*)state.data(), state.size())); - } - } - } - break; // transactions cycle - } + if (!is_new_state(t)) { + continue; + } + csdb::UserField fld = t.user_field(trx_uf::new_state::RefStart); + if (!fld.is_valid() || !(SmartContractRef(fld) == ref)) { + continue; + } + state_found = true; + fld = t.user_field(trx_uf::new_state::Hash); + if (fld.is_valid()) { + std::string hash_bytes = fld.value(); + cs::Hash hash; + if (hash.size() == hash_bytes.size()) { + std::copy(hash_bytes.cbegin(), hash_bytes.cend(), hash.begin()); + state_test_passed = state.empty() ? (hash == cs::Zero::hash) + : (hash == cscrypto::calculateHash((cs::Byte*)state.data(), state.size())); } } + break; } } - if (state_found) { - break; + // a state whose call this node cannot find on its chain is not taken unverified + if (!state_found || !state_test_passed) { + cswarning() << kLogPrefix << to_base58(contract_abs_addr) << " state received for " << ref + << (state_found ? " does not match the hash on the chain" : " cannot be verified: its block is not stored"); + return; } } - if (state_found && !state_test_passed) { - cswarning() << kLogPrefix << to_base58(contract_abs_addr) << " state received does not match hash value in " << ref_state; - return; - } } - - if (dbcache_update(contract_abs_addr, ref, state, false)) { + else { + cswarning() << kLogPrefix << "ignore a state of " << to_base58(contract_abs_addr) << ", a contract this node does not know"; + return; + } + + // forced: the cache may hold a wrong state with the same call from a re-execution + if (dbcache_update(contract_abs_addr, ref, state, true)) { if (in_known_contracts(contract_abs_addr)) { auto& item = known_contracts[contract_abs_addr]; cscrypto::Bytes st1(item.state.data(), item.state.data() + item.state.size()); @@ -3494,8 +3557,9 @@ void SmartContracts::net_update_contract_state(const csdb::Address& contract_abs csdebug() << kLogPrefix << kLogPrefix << __func__ << " updated value value: " << cs::Utils::byteStreamToHex(st1); item.ref_cache = ref; item.ref_execute = ref; - csdebug() << kLogPrefix << to_base58(contract_abs_addr) << " state has updated from net package with " << ref << " state value"; + cslog() << kLogPrefix << "state of " << to_base58(contract_abs_addr) << " after " << ref << " received from peer, verified"; } + state_fetcher_.done(contract_abs_addr); return; } else { @@ -3630,7 +3694,8 @@ void SmartContracts::clear() { exe_queue.clear(); blacklistedContracts_.clear(); locked_contracts.clear(); - pending_state_requests.clear(); + state_fetcher_.reset(); + was_reading_db_ = true; } diff --git a/tests/csnode/smartcontracts_tests.cpp b/tests/csnode/smartcontracts_tests.cpp index 75dbf3880..bfbf7353d 100644 --- a/tests/csnode/smartcontracts_tests.cpp +++ b/tests/csnode/smartcontracts_tests.cpp @@ -1,6 +1,7 @@ #define TESTING #include +#include #include "clientconfigmock.hpp" #include "gtest/gtest.h" #include @@ -180,3 +181,51 @@ TEST(SmartContract, StaleMeansNotTheDeclaredState) { item.state.clear(); EXPECT_TRUE(cs::SmartContracts::is_stale(item)); // no state at all } + +// step 3 of the contract state sync plan: a peer's state is taken only for the declared call and hash +TEST(SmartContract, StateReplyIsCheckedAgainstTheDeclaration) { + using Check = cs::SmartContracts::StateReplyCheck; + cs::SmartContracts::StateItem item; + const std::string good = "state after 9.2"; + cs::SmartContractRef declared; + declared.sequence = 9; + declared.transaction = 2; + cs::SmartContractRef older = declared, newer = declared; + older.sequence = 8; + newer.sequence = 10; + + EXPECT_EQ(cs::SmartContracts::verify_state_reply(declared, good, item), Check::Undeclared); + item.declared_ref = declared; + item.declared_hash = cscrypto::calculateHash(reinterpret_cast(good.data()), good.size()); + EXPECT_EQ(cs::SmartContracts::verify_state_reply(declared, good, item), Check::Accepted); + EXPECT_EQ(cs::SmartContracts::verify_state_reply(declared, "a forged state", item), Check::HashMismatch); + EXPECT_EQ(cs::SmartContracts::verify_state_reply(declared, "", item), Check::HashMismatch); + EXPECT_EQ(cs::SmartContracts::verify_state_reply(older, good, item), Check::Older); + EXPECT_EQ(cs::SmartContracts::verify_state_reply(newer, good, item), Check::Newer); +} + +TEST(SmartContract, StateFetcherPacesAndBacksOff) { + cs::StateFetcher f; + std::vector contracts; + for (uint8_t i = 1; i <= 20; ++i) { + contracts.push_back(csdb::Address::from_public_key(cs::Bytes(32, i))); + f.markStale(contracts.back()); + } + EXPECT_TRUE(f.isWanted(contracts[0])); + // at most 16 a round, the rest the next round + EXPECT_EQ(f.due(100).size(), cs::StateFetcher::kRequestsPerRound); + EXPECT_EQ(f.due(101).size(), 4u); + EXPECT_TRUE(f.due(102).empty()); + // asked again after the backoff, which doubles with each attempt up to the cap + EXPECT_EQ(f.due(100 + cs::StateFetcher::kBaseBackoff).size(), cs::StateFetcher::kRequestsPerRound); + EXPECT_EQ(cs::StateFetcher::backoff(0), 50u); + EXPECT_EQ(cs::StateFetcher::backoff(1), 100u); + EXPECT_EQ(cs::StateFetcher::backoff(20), cs::StateFetcher::kMaxBackoff); + // a verified state ends the requests, and a reply for a contract not asked for is not an answer + f.done(contracts[0]); + EXPECT_FALSE(f.isWanted(contracts[0])); + EXPECT_FALSE(f.isWanted(csdb::Address::from_public_key(cs::Bytes(32, 99)))); + EXPECT_EQ(f.size(), 19u); + f.reset(); + EXPECT_EQ(f.size(), 0u); +} From 291a0ad39688b1816bb225f228929013ff50a05a Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 17:59:36 +0200 Subject: [PATCH 3/5] solver: contract state review fixes - StateItem bytes gain the declaration only when one is known, so an item of a checkpoint from an earlier build serializes to the same bytes and the checkpoint's hashes still match: before, every such checkpoint was quarantined at the first start of the new build. - Removing a block whose new_state declared a contract's state moves the declaration back to the latest successful new_state left on the chain (unknown without one): the restored state was otherwise stale for good and every peer reply looked older. - A state of a contract this node does not know ends its requests. - Test: an item without a declaration has the earlier layout. Co-Authored-By: Claude Opus 5.5 --- solver/src/smartcontracts.cpp | 42 +++++++++++++++++++++++++-- tests/csnode/smartcontracts_tests.cpp | 7 +++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index 687ea1208..2a94f8f9f 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -1533,6 +1533,38 @@ void SmartContracts::on_remove_block(const csdb::Pool& block) { prev_seq = tmp; } + // the removed new_state declared the contract's state: the declaration goes back to the + // latest successful new_state of the contract left on the chain, unknown without one + if (item.declared_ref == ref) { + item.declared_ref = SmartContractRef{}; + item.declared_hash = cs::Hash{}; + for (auto it_seq = all_contract_blocks.crbegin(); it_seq != all_contract_blocks.crend() && item.declared_hash == cs::Zero::hash; ++it_seq) { + const csdb::Pool b = bc.loadBlock(*it_seq); + const auto& trs = b.transactions(); + for (auto it_t = trs.crbegin(); it_t != trs.crend(); ++it_t) { + if (!is_new_state(*it_t) || absolute_address(it_t->target()) != abs_addr) { + continue; + } + const csdb::UserField h = it_t->user_field(trx_uf::new_state::Hash); + const csdb::UserField r = it_t->user_field(trx_uf::new_state::RefStart); + if (!h.is_valid() || !r.is_valid()) { + continue; + } + const std::string bytes = h.value(); + cs::Hash hash{}; + if (bytes.size() != hash.size()) { + continue; + } + std::copy(bytes.cbegin(), bytes.cend(), hash.begin()); + if (hash != cs::Zero::hash) { + item.declared_ref = SmartContractRef(r); + item.declared_hash = hash; + break; + } + } + } + } + // go through all blocks and re-execute contract std::string executed_state; csdb::Transaction executed_transaction; @@ -3558,6 +3590,7 @@ void SmartContracts::net_update_contract_state(const csdb::Address& contract_abs } else { cswarning() << kLogPrefix << "ignore a state of " << to_base58(contract_abs_addr) << ", a contract this node does not know"; + state_fetcher_.done(contract_abs_addr); return; } @@ -3837,9 +3870,12 @@ Bytes SmartContracts::StateItem::to_bytes() { ++it; } } - // appended: a checkpoint of an earlier build has none, and an earlier build ignores them - os << declared_ref.sequence << declared_ref.transaction; - os << declared_hash; + // appended only when known: an item without a declaration keeps the bytes of earlier builds, whose + // checkpoints are checked against hashes of these bytes (quick_start_hashes.dat) when they load + if (declared_ref.is_valid() && declared_hash != cs::Zero::hash) { + os << declared_ref.sequence << declared_ref.transaction; + os << declared_hash; + } return data; } diff --git a/tests/csnode/smartcontracts_tests.cpp b/tests/csnode/smartcontracts_tests.cpp index 044de78c4..4c8df986f 100644 --- a/tests/csnode/smartcontracts_tests.cpp +++ b/tests/csnode/smartcontracts_tests.cpp @@ -162,6 +162,13 @@ TEST(SmartContract, DeclaredStateSurvivesCheckpointAndOldItemsLoad) { EXPECT_EQ(back.declared_hash, item.declared_hash); EXPECT_EQ(back.state, item.state); + // an item without a declaration keeps the bytes of earlier builds (their checkpoints are + // verified against hashes of these bytes) + cs::SmartContracts::StateItem plainItem = item; + plainItem.declared_ref = cs::SmartContractRef{}; + plainItem.declared_hash = cs::Hash{}; + EXPECT_EQ(plainItem.to_bytes().size(), item.to_bytes().size() - (sizeof(item.declared_ref.sequence) + sizeof(item.declared_ref.transaction) + item.declared_hash.size())); + // an earlier build wrote no declaration: the same bytes without the tail auto old = item.to_bytes(); old.resize(old.size() - (sizeof(item.declared_ref.sequence) + sizeof(item.declared_ref.transaction) + item.declared_hash.size())); From 665c5c44bd523d664e50629bc249ae115b647b69 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 17:59:53 +0200 Subject: [PATCH 4/5] ci(testnet): two Dice plays at once, both executions in one block Co-Authored-By: Claude Opus 5.5 --- ci/testnet/contracts.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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") From afd2273d0e3bef92bf11690658df1480c63a3bbf Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 20:31:11 +0200 Subject: [PATCH 5/5] docs: CHANGES.md, #93-#95 Co-Authored-By: Claude Opus 5.5 --- docs/CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) 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