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 +#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 6c17cc37d..2a94f8f9f 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -1444,12 +1444,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*/ @@ -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; @@ -2625,6 +2657,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; @@ -2650,6 +2727,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]; @@ -2699,6 +2779,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: " @@ -3257,6 +3338,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; @@ -3289,6 +3371,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 " @@ -3327,7 +3418,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; } @@ -3353,8 +3445,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*/ @@ -3368,17 +3510,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) { @@ -3386,60 +3523,79 @@ 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"; + state_fetcher_.done(contract_abs_addr); + 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()); @@ -3450,8 +3606,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 { @@ -3586,7 +3743,8 @@ void SmartContracts::clear() { exe_queue.clear(); blacklistedContracts_.clear(); locked_contracts.clear(); - pending_state_requests.clear(); + state_fetcher_.reset(); + was_reading_db_ = true; } @@ -3712,6 +3870,12 @@ Bytes SmartContracts::StateItem::to_bytes() { ++it; } } + // 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; } @@ -3748,6 +3912,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 fb7ad3405..4c8df986f 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 @@ -143,3 +144,96 @@ TEST(SmartContract, EmittedTransactionIsChargedToTheLatestExecutionInTheBlock) { other.add_transaction(plain(A, player, 9)); EXPECT_FALSE(cs::SmartContracts::emitting_new_state(other, 1, 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 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())); + 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 +} + +// 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); +}