Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/robustness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion ci/testnet/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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")

Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dir>` |
| #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

Expand Down
1 change: 1 addition & 0 deletions solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 59 additions & 37 deletions solver/include/solver/smartcontracts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <csdb/address.hpp>
#include <csdb/pool.hpp>
#include <solver/statefetcher.hpp>
#include <csdb/transaction.hpp>
#include <csdb/user_field.hpp>
#include <lib/system/common.hpp>
Expand Down Expand Up @@ -359,6 +360,57 @@ class SmartContracts final {
static std::optional<SmartContractRef> emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr,
const std::function<csdb::Address(const csdb::Address&)>& 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<std::string, std::map<csdb::Address, std::string>> 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<csdb::Address> stale_locks(const std::unordered_set<csdb::Address>& locked, const std::vector<csdb::Address>& in_use);

Expand Down Expand Up @@ -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<std::string, std::map<csdb::Address, std::string>> uses;
};

// last contract's state storage
std::unordered_map<csdb::Address, StateItem> known_contracts;

std::unordered_set<csdb::Address> blacklistedContracts_;

std::unordered_set<csdb::Address> 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<csdb::Address, cs::RoundNumber> 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;

Expand Down
88 changes: 88 additions & 0 deletions solver/include/solver/statefetcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef STATEFETCHER_HPP
#define STATEFETCHER_HPP

#include <algorithm>
#include <cstdint>
#include <map>
#include <vector>

#include <csdb/address.hpp>
#include <lib/system/common.hpp>

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<csdb::Address> due(cs::RoundNumber round, size_t limit = kRequestsPerRound) {
std::vector<std::pair<cs::RoundNumber, csdb::Address>> 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<csdb::Address> 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<csdb::Address, Entry> entries_;
};

} // namespace cs

#endif // STATEFETCHER_HPP
Loading
Loading