diff --git a/.github/workflows/robustness.yml b/.github/workflows/robustness.yml index f8316e870..a71c11778 100644 --- a/.github/workflows/robustness.yml +++ b/.github/workflows/robustness.yml @@ -292,6 +292,10 @@ jobs: - 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 + # the same with sync_contract_execution=false: no re-execution while syncing, every state fetched + - name: contracts-dice-join-nosync-exec + env: DB_BACKEND=lmdb CONTRACTS=1 CONTRACT_DICE=1 JOIN_PRUNED_AT=500 JOIN_FULL=1 CONTRACTS_AFTER_AT=700 TARGET_SEQ=1300 SYNC_CONTRACT_EXECUTION=0 + contracts: true - name: contracts-crash env: DB_BACKEND=lmdb CRASH_NODE=6 CONTRACTS=1 TARGET_SEQ=1600 contracts: true diff --git a/ci/testnet/run.sh b/ci/testnet/run.sh index 3902c816a..a0a00bb1f 100755 --- a/ci/testnet/run.sh +++ b/ci/testnet/run.sh @@ -34,6 +34,8 @@ # node reached CONTRACTS_AFTER_AT (default 0); the states are compared once every node, # a joining one too, reached TARGET_SEQ. contracts.py sends everything through n1: a # contract scenario never kills, partitions or slows n1 +# SYNC_CONTRACT_EXECUTION=0: every node runs with sync_contract_execution=false (no contract +# re-execution while reading the blockchain; states fetched from the confidants once live) # CONTRACT_DICE=1 (with CONTRACTS): also a Dice contract that reads its own balance, played three # times before the restart test; a node that syncs through the plays re-executes them # CONTRACT_LOAD_SECONDS (default 0, with CONTRACTS): after the calls, a load phase of that many @@ -179,6 +181,7 @@ JOIN_FULL=${JOIN_FULL:-0} CONTRACTS=${CONTRACTS:-0} CONTRACTS_AFTER_AT=${CONTRACTS_AFTER_AT:-0} CONTRACT_DICE=${CONTRACT_DICE:-0} +SYNC_CONTRACT_EXECUTION=${SYNC_CONTRACT_EXECUTION:-1} [ "$CONTRACTS_AFTER_AT" -eq 0 ] || [ "$CONTRACTS_AFTER_AT" -le $(( ${TARGET_SEQ:-1100} - 100 )) ] \ || { echo "CONTRACTS_AFTER_AT must be at most TARGET_SEQ - 100"; exit 1; } PRUNE_NODE=${PRUNE_NODE:-0} @@ -369,6 +372,7 @@ ipv6=false traverse_nat=false min_neighbours=1 max_neighbours=16 +sync_contract_execution=$([ "$SYNC_CONTRACT_EXECUTION" = 0 ] && echo false || echo true) round_elapse_time=$ROUND_ELAPSE_MS [host_input] diff --git a/client/config/config.cpp b/client/config/config.cpp index 278d7fd0c..49ef405d9 100644 --- a/client/config/config.cpp +++ b/client/config/config.cpp @@ -53,6 +53,7 @@ const std::string PARAM_NAME_OBSERVER_WAIT_TIME = "observer_wait_time"; const std::string PARAM_NAME_ROUND_ELAPSE_TIME = "round_elapse_time"; const std::string PARAM_NAME_STORE_BLOCK_ELAPSE_TIME = "store_block_elapse_time"; const std::string PARAM_NAME_ALWAYS_EXECUTE_CONTRACTS = "always_execute_contracts"; +const std::string PARAM_NAME_SYNC_CONTRACT_EXECUTION = "sync_contract_execution"; const std::string PARAM_NAME_MIN_COMPATIBLE_VERSION = "min_compatible_version"; const std::string PARAM_NAME_COMPATIBLE_VERSION = "compatible_version"; const std::string PARAM_NAME_TRAVERSE_NAT = "traverse_nat"; @@ -873,6 +874,9 @@ Config Config::readFromFile(const std::string& fileName) { csdebug() << "No initial confidants specified."; } + if (params.count(PARAM_NAME_SYNC_CONTRACT_EXECUTION) > 0) { + result.syncContractExecution_ = params.get(PARAM_NAME_SYNC_CONTRACT_EXECUTION); + } if (params.count(PARAM_NAME_ALWAYS_EXECUTE_CONTRACTS) > 0) { result.alwaysExecuteContracts_ = params.get(PARAM_NAME_ALWAYS_EXECUTE_CONTRACTS); } @@ -1343,6 +1347,7 @@ bool operator==(const Config& lhs, const Config& rhs) { lhs.apiData_ == rhs.apiData_ && lhs.snapshotData_ == rhs.snapshotData_ && lhs.alwaysExecuteContracts_ == rhs.alwaysExecuteContracts_ && + lhs.syncContractExecution_ == rhs.syncContractExecution_ && lhs.compatibleVersion_ == rhs.compatibleVersion_ && lhs.recreateIndex_ == rhs.recreateIndex_ && lhs.observerWaitTime_ == rhs.observerWaitTime_ && diff --git a/client/config/config.hpp b/client/config/config.hpp index 1e551601f..4945c51ff 100644 --- a/client/config/config.hpp +++ b/client/config/config.hpp @@ -289,6 +289,12 @@ class Config { void dumpJSONKeys(const std::string& fName) const; + // re-execute contract calls while reading the blockchain (sync, start-up) to get their states + // provisionally; off: never, the states are fetched from the confidants once the node is live + bool syncContractExecution() const { + return syncContractExecution_; + } + bool alwaysExecuteContracts() const { return alwaysExecuteContracts_; } @@ -442,6 +448,7 @@ class Config { bool showBalanceChange_ = false; bool alwaysExecuteContracts_ = false; + bool syncContractExecution_ = true; bool recreateIndex_ = false; bool newBlockchainTop_ = false; bool autoShutdownEnabled_ = true; diff --git a/packaging/mainnet/config.ini b/packaging/mainnet/config.ini index adb180eac..b09059c05 100644 --- a/packaging/mainnet/config.ini +++ b/packaging/mainnet/config.ini @@ -8,6 +8,10 @@ min_compatible_version=503 ; replaying some contracts (games that read their own balance) is not reproducible, so a syncing ; node can disagree with the network on a few balances by a fee and would then reject valid blocks compatible_version=true +; true: contract calls are re-executed while syncing to get their states provisionally; the +; states that do not match the chain are fetched from the confidants once the node is live. +; false: no re-execution while syncing, every state is fetched (no executor needed to sync) +sync_contract_execution=true [host_input] port=9510 diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp index 02888d7b7..6aaa563c6 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -665,6 +665,8 @@ public slots: 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; + // get_actual_state skipped a re-execution because of sync_contract_execution=off + bool sync_execution_skipped_ = false; cs::RoundNumber last_state_summary_ = 0; void on_live_block(cs::RoundNumber round); diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index 2a94f8f9f..d5c3989cf 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -1316,6 +1316,13 @@ csdb::Transaction SmartContracts::get_actual_state(const csdb::Transaction& hash } } } + // sync_contract_execution off: no re-execution while reading the blockchain, the state is + // fetched from the confidants once the node is live (an empty value marks it stale) + if (reading_db && tr_state.user_field_ids().count(new_state::Value) == 0 && + !cs::ConfigHolder::instance().config()->syncContractExecution()) { + tr_state.add_user_field(new_state::Value, std::string{}); + sync_execution_skipped_ = true; + } // execute contract to get last state if state is not found in cache if (tr_state.user_field_ids().count(new_state::Value) == 0) { csdb::Transaction tr_start = get_transaction(ref_start, req_abs_addr); @@ -1428,7 +1435,12 @@ csdb::Transaction SmartContracts::get_actual_state(const csdb::Transaction& hash // a contract whose execution is not reproducible (e.g. one that reads its own balance) leaves its // state unknown here, and the fees of its later emitted transactions can then be charged to an // earlier initiator: this node's balances may differ from the network's by such fees - if (state_expected && tr_state.user_field(new_state::Value).value().empty()) { + if (sync_execution_skipped_) { + // expected with sync_contract_execution off: not a warning + sync_execution_skipped_ = false; + csdebug() << kLogPrefix << "state of " << to_base58(absolute_address(hashed_state.target())) << " not re-executed while reading the blockchain"; + } + else if (state_expected && tr_state.user_field(new_state::Value).value().empty()) { cswarning() << kLogPrefix << "state of " << to_base58(absolute_address(hashed_state.target())) << " declared by the block is not available" << (reading_db ? " while reading the blockchain" : "") << ", the contract state is out of date";