From e5079954dc27a3208cf51f2ed84c37952c6f2fee Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Tue, 13 Oct 2026 10:56:50 +0200 Subject: [PATCH] solver: accept a contract state from a peer only in answer to a request getStateReply accepted a contract state from any signed peer, and net_update_contract_state cached it whenever no new_state with the same ref was found in the following MaxRoundsCancelContract blocks. A state with a future ref was cached too, and then every real update looked outdated, so a single message froze the contract on this node. Remember the contracts this node asked the confidants about (the round of the last request, kept for 1000 rounds because several confidants answer) and ignore states for any other contract, or with a ref later than the current round. A state whose new_state is not stored yet is still accepted: that is how a lagging node catches up, and refusing it would leave it without the state. Co-Authored-By: Claude Opus 5.5 --- solver/include/solver/smartcontracts.hpp | 3 +++ solver/src/smartcontracts.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp index 302bd9c95..3946f05b1 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -621,6 +621,9 @@ 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; //uint64_t testValue = 0; diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index f11b683bb..6a75ace79 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -3267,6 +3267,7 @@ 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()); } @@ -3281,6 +3282,23 @@ 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"; + return; + } + // 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) { + cswarning() << kLogPrefix << "ignore " << cs::SmartContracts::to_base58(contract_abs_addr) << " state of future " << ref; + return; + } if (in_known_contracts(contract_abs_addr)) { auto& item = known_contracts[contract_abs_addr]; @@ -3482,6 +3500,7 @@ void SmartContracts::clear() { exe_queue.clear(); blacklistedContracts_.clear(); locked_contracts.clear(); + pending_state_requests.clear(); }