From 2c9565a9fbfd17cbeb33dce2ec2701a5ccf72cba Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 15:36:56 +0200 Subject: [PATCH 1/3] ci(testnet): reproduce the contract state divergence with a dice contract Dice reads its own balance and pays 10 CS when it holds at least 20: a node re-executing it while it syncs sees the balance after the same block's payout, throws, keeps a stale state and charges a later payout's fee to the previous player. contracts.py --dice (run.sh CONTRACT_DICE=1) deploys it, funds it, and has two players play it three times in turn before the restart test; its state is compared on every node. Scenario contracts-dice-join (manual): a node syncs from block 0 through the plays. It is expected to fail until contract states are fetched instead of re-executed (step 0 of the contract state sync plan). Co-Authored-By: Claude Opus 5.5 --- .github/workflows/robustness.yml | 7 +++ ci/testnet/contracts.py | 77 +++++++++++++++++++++++++++++++- ci/testnet/run.sh | 5 ++- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/.github/workflows/robustness.yml b/.github/workflows/robustness.yml index 094d542cf..f4e53e5ec 100644 --- a/.github/workflows/robustness.yml +++ b/.github/workflows/robustness.yml @@ -276,6 +276,13 @@ 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 + - 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 79ab65ddf..25865b733 100644 --- a/ci/testnet/contracts.py +++ b/ci/testnet/contracts.py @@ -114,6 +114,44 @@ } """ +# a game that reads its own balance, as the mainnet dice contracts do: executed live it sees the +# balance before its payout; a node re-executing it while it syncs sees the balance after the payout +# of the same block, throws, keeps a stale state, and charges the fee of a later payout to the +# previous player (docs/specs/2026-10-24-contract-state-sync-design.md, 4.2) +DICE = """import com.credits.scapi.v3.SmartContract; + +import java.math.BigDecimal; + +public class Dice extends SmartContract { + private int plays; + private String lastPlayer; + + public Dice() { + super(); + } + + public int play() { + BigDecimal balance = getBalance(contractAddress); + if (balance.compareTo(new BigDecimal("20")) < 0) { + throw new RuntimeException("insufficient balance " + balance); + } + sendTransaction(contractAddress, initiator, 10.0, new byte[0]); + plays += 1; + lastPlayer = initiator; + return plays; + } + + public int plays() { + return plays; + } + + @Override + public String payable(BigDecimal amount, byte[] userData) { + return "ok"; + } +} +""" + # no @UsingContract: the node refuses any call to a method that declares one (Violations::SubsequentCall, # checked by the API and by IterValidator), so the call is a plain invokeExternalContract CALLER = """import com.credits.scapi.v0.SmartContract; @@ -167,6 +205,8 @@ def write_sources(directory): f.write(CALLER_DECLARED % (placeholder, placeholder)) with open(os.path.join(directory, "Ledger.java"), "w") as f: f.write(LEDGER) + with open(os.path.join(directory, "Dice.java"), "w") as f: + f.write(DICE) def invocation(method="", used=(), deploy=None): @@ -270,6 +310,35 @@ def deploy(client, master, name, source): sys.exit(f"{name} not deployed after 300 s") +def dice_plays(client_for, master, dice): + """25 CS into the contract, then three plays by two players in turn, each followed by a 10 CS + top-up from the master. Returns the number of failures.""" + funder = Client(9090, 300) + players = [Account(hashlib.sha256(master.pk + b"dice player %d" % i).digest()) for i in (1, 2)] + master.next_inner_id = None + for p in players: + ok, message = send(funder, master, p.pk, 100) + print(f"Dice: fund player {base58.b58encode(p.pk).decode()[:8]}: {'ok' if ok else 'FAILED ' + str(message)}", flush=True) + ok, message = send(funder, master, dice, 25) + print(f"Dice: 25 CS to the contract: {'ok' if ok else 'FAILED ' + str(message)}", flush=True) + deadline = time.time() + 300 + while time.time() < deadline and (balance(funder, dice) < 25 or any(balance(funder, p.pk) < 100 for p in players)): + time.sleep(5) + print(f"Dice: contract balance {balance(funder, dice)}", flush=True) + failures = 0 + for n in range(3): + player = players[n % 2] + failures += 0 if call(client_for(1), player, dice, "play", f"Dice play {n + 1} by player {n % 2 + 1}", n + 1) else 1 + master.next_inner_id = None + before = balance(funder, dice) + ok, message = send(funder, master, dice, 10) + deadline = time.time() + 120 + 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) + return failures + + def call(client, master, contract, method, label, expected=None): deadline = time.time() + 120 while True: @@ -426,6 +495,8 @@ def main(): parser.add_argument("--stats-file", default="") # nodes left out of the reads (still compared at the end) parser.add_argument("--skip-read-nodes", default="") + parser.add_argument("--dice", action="store_true", + help="deploy Dice and play it before the restart (a contract whose re-execution is not reproducible)") args = parser.parse_args() check_nodes = args.check_nodes or args.nodes skipped = {int(n) for n in args.skip_read_nodes.split() if n.strip()} @@ -447,6 +518,7 @@ def client_for(i): # contract-to-contract calls fail on mainnet today; what matters is that every node fails them the # same way, also a node that restarted from a checkpoint taken after the callee was deployed callers = (("Caller", caller), ("CallerDeclared", declared)) + dice = deploy(client_for(1), master, "Dice", DICE) if args.dice else None def call_callers(when, via): for name, contract in callers: @@ -487,6 +559,9 @@ def one(i): print(f" n{i}: {v!r} {msg!r}", flush=True) return len(bad) + if dice: + failures += dice_plays(client_for, master, dice) + failures += read_all(dice, "plays", 3, "Dice.plays") failures += read_all(counter, "get", calls, "Counter.get before restart") failures += read_all(ledger, "entries", calls, "Ledger.entries before restart") @@ -551,7 +626,7 @@ def footprint(): time.sleep(5) print(f"final file {'found' if os.path.exists(args.final_file) else 'MISSING'}", flush=True) time.sleep(60) # let the last state reach every node - for name, contract in (("Counter", counter), ("Ledger", ledger)) + callers: + for name, contract in (("Counter", counter), ("Ledger", ledger)) + callers + ((("Dice", dice),) if dice else ()): states = {} for i in range(1, (args.check_nodes or args.nodes) + 1): try: diff --git a/ci/testnet/run.sh b/ci/testnet/run.sh index 508a8b32d..62e3040a7 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 +# 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 # seconds: CONTRACT_WRITE_RATE (default 1) Counter.next writes a second from every node's # account through its own API, and CONTRACT_READ_RATE (default 5) Counter.get reads (getters, @@ -175,6 +177,7 @@ SYNCRO_ROUND=${SYNCRO_ROUND:-0} JOIN_FULL=${JOIN_FULL:-0} CONTRACTS=${CONTRACTS:-0} CONTRACTS_AFTER_AT=${CONTRACTS_AFTER_AT:-0} +CONTRACT_DICE=${CONTRACT_DICE:-0} [ "$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} @@ -970,7 +973,7 @@ PY --ready-file "$WORK/contracts-ready" --wait-file "$WORK/restart-done" --wait-timeout $((TIMEOUT_MIN * 60)) \ --check-nodes "$ALL_NODES" --final-file "$WORK/contracts-final" \ --load-seconds "$CONTRACT_LOAD_SECONDS" --write-rate "$CONTRACT_WRITE_RATE" --read-rate "$CONTRACT_READ_RATE" \ - --stats-file "$WORK/contracts-stats.json") > "$WORK/contracts.log" 2>&1 & + --stats-file "$WORK/contracts-stats.json" $([ "$CONTRACT_DICE" = 1 ] && echo --dice)) > "$WORK/contracts.log" 2>&1 & CONTRACTS_PID=$! fi # contracts.py ended before its first calls were done: nothing would release the restart test From c7f742dced95e3ce8c5435e51f49f9f5a48bc5f8 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 15:58:35 +0200 Subject: [PATCH 2/3] solver: an emitted transaction's fee goes to the execution whose new_state precedes it in the block A node that syncs re-executes contract calls to rebuild their states; for a contract whose result depends on its own balance the re-execution is not reproducible, the state stays stale and the contract's last known execution is an earlier one: the fee of a later payout was charged to the previous initiator, and the node's balances differed from the live nodes', which have the new state before the block. The fee now goes to the initiator of the contract's nearest new_state before the emitted transaction in the same block, the rule the rollback already applies (now through the same helper, which also bounds its search to the block); the last known execution only when the block has no new_state. Consensus-relevant: a syncing node's balances change by one fee per affected payout compared with an official node that syncs the same blocks, and agree with the nodes that executed the contract. Step 1 of the contract state sync plan. Co-Authored-By: Claude Opus 5.5 --- solver/include/solver/smartcontracts.hpp | 8 ++++ solver/src/smartcontracts.cpp | 56 +++++++++++++++++------- tests/csnode/smartcontracts_tests.cpp | 54 +++++++++++++++++++++++ 3 files changed, 101 insertions(+), 17 deletions(-) diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp index 56d0d29a5..dd3feec0d 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -352,6 +352,14 @@ class SmartContracts final { // new state of contract, result of invocation of executable transaction static bool is_new_state(const csdb::Transaction&); + // the start of the execution that emitted transaction tr_idx of the block: the RefStart of the + // contract's new_state nearest before it in the same block (the smart consensus puts every + // emitted transaction after its new_state); nullopt when there is none. The live nodes, which + // have the new state before the block, charge an emitted transaction's fee to that execution's + // initiator; this finds it from the block alone, whatever state the node holds. + static std::optional emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr, + const std::function& absolute); + // 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); diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index f07737a7e..e78d1771d 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -318,6 +318,23 @@ bool SmartContracts::is_start(const csdb::Transaction& tr) { } /*static*/ +/*static*/ +std::optional SmartContracts::emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr, + const std::function& absolute) { + const auto& transactions = block.transactions(); + for (size_t i = std::min(tr_idx, transactions.size()); i > 0; --i) { + const auto& t = transactions[i - 1]; + if (is_new_state(t) && absolute(t.target()) == abs_addr) { + const csdb::UserField fld = t.user_field(trx_uf::new_state::RefStart); + if (!fld.is_valid()) { + return std::nullopt; + } + return SmartContractRef(fld); + } + } + return std::nullopt; +} + bool SmartContracts::is_new_state(const csdb::Transaction& tr) { //csdebug() << kLogPrefix << __func__; if (!tr.is_valid()) { @@ -1673,20 +1690,14 @@ void SmartContracts::on_remove_block(const csdb::Pool& block) { else { csdb::Address abs_addr = absolute_address(t.source()); if (in_known_contracts(abs_addr)) { - // contract emitted transaction, look up corresponding new state - for (auto tmp = itr_t + 1; tmp != block.transactions().crbegin(); ++tmp) { - if (SmartContracts::is_new_state(*tmp) && absolute_address(tmp->target()) == abs_addr) { - // proper new state found - const auto& last_new_state_transaction = *tmp; - csdb::UserField fld = last_new_state_transaction.user_field(trx_uf::new_state::RefStart); - if (fld.is_valid()) { - SmartContractRef ref(fld); - csdb::Transaction start_transaction = get_transaction(ref, abs_addr); - if (start_transaction.is_valid()) { - rollback_emitted_accepted(t, start_transaction); - } - } - break; + // contract emitted transaction, look up corresponding new state (the same rule as + // on_next_block_impl, so that a rollback refunds the fee to whom it was charged) + const size_t tr_idx = static_cast(std::distance(itr_t, block.transactions().crend())) - 1; + const auto absolute = [this](const csdb::Address& a) { return absolute_address(a); }; + if (const auto ref = emitting_new_state(block, tr_idx, abs_addr, absolute); ref.has_value()) { + csdb::Transaction start_transaction = get_transaction(*ref, abs_addr); + if (start_transaction.is_valid()) { + rollback_emitted_accepted(t, start_transaction); } } } @@ -1838,12 +1849,23 @@ void SmartContracts::on_next_block_impl(const csdb::Pool& block, bool reading_db csdb::Address abs_addr = absolute_address(tr.source()); const auto it = known_contracts.find(abs_addr); if (it != known_contracts.cend()) { - // is emitted by contract + // is emitted by contract: its fee goes to the initiator of the execution whose new_state + // precedes it in this block, as the live nodes charge it (and as on_remove_block + // refunds it); the contract's last known execution only when the block has none, + // since a node that could not obtain a state (a re-execution that is not + // reproducible while it syncs) holds an earlier one const auto& contract_item = it->second; - csdb::Transaction starter = get_transaction(contract_item.ref_execute, abs_addr); + const auto absolute = [this](const csdb::Address& a) { return absolute_address(a); }; + const auto emitting = emitting_new_state(block, tr_idx, abs_addr, absolute); + if (!emitting.has_value()) { + cswarning() << kLogPrefix << "no new_state of the contract before its emitted transaction " << FormatRef(block.sequence(), tr_idx) + << ", charging the fee to the last known execution " << contract_item.ref_execute; + } + const SmartContractRef ref_starter = emitting.value_or(contract_item.ref_execute); + csdb::Transaction starter = get_transaction(ref_starter, abs_addr); if (starter.is_valid()) { if (!reading_db) { - csdetails() << kLogPrefix << "found emitted transaction in " << contract_item.ref_execute; + csdetails() << kLogPrefix << "found emitted transaction in " << ref_starter; } emit signal_emitted_accepted(tr, starter); } diff --git a/tests/csnode/smartcontracts_tests.cpp b/tests/csnode/smartcontracts_tests.cpp index 3e6bf18ae..4e3ff8470 100644 --- a/tests/csnode/smartcontracts_tests.cpp +++ b/tests/csnode/smartcontracts_tests.cpp @@ -88,3 +88,57 @@ TEST(SmartContract, stale_locks_keeps_used_contracts) { ASSERT_TRUE(SmartContracts::stale_locks(locked, {running, used, stale}).empty()); ASSERT_EQ(SmartContracts::stale_locks(locked, {address(9)}).size(), 3u); } + +namespace { +csdb::Address addressOf(uint8_t b) { + return csdb::Address::from_public_key(cs::Bytes(32, b)); +} + +csdb::Transaction plain(csdb::Address src, csdb::Address tgt, int64_t id) { + return csdb::Transaction(id, src, tgt, csdb::Currency{}, csdb::Amount(1), csdb::AmountCommission(0.0), csdb::AmountCommission(0.0), cs::Signature{}); +} + +// a new_state of contract `contract` for the execution started at ref +csdb::Transaction newState(csdb::Address contract, cs::Sequence seq, cs::Sequence index, int64_t id) { + auto t = plain(contract, contract, id); + t.add_user_field(cs::trx_uf::new_state::Hash, std::string(32, 'h')); + cs::SmartContractRef ref; + ref.sequence = seq; + ref.transaction = index; + t.add_user_field(cs::trx_uf::new_state::RefStart, ref.to_user_field()); + return t; +} +} // namespace + +// the execution that emitted a transaction is the contract's nearest new_state before it in the +// block: the fee of an emitted transaction goes to that execution's initiator (step 1 of the +// contract state sync plan), whatever state the node holds +TEST(SmartContract, EmittedTransactionBelongsToTheNewStateBeforeIt) { + const auto A = addressOf(1), B = addressOf(2), player = addressOf(3); + csdb::Pool block(csdb::PoolHash{}, 50); + block.add_transaction(plain(A, player, 1)); // 0: emitted by A before any new_state of A + block.add_transaction(newState(A, 40, 0, 2)); // 1: A's execution started at 40.0 + block.add_transaction(plain(A, player, 3)); // 2: emitted by it + block.add_transaction(newState(B, 41, 1, 4)); // 3: B's execution started at 41.1 + block.add_transaction(plain(B, player, 5)); // 4: emitted by it + block.add_transaction(newState(A, 45, 2, 6)); // 5: A's next execution, started at 45.2 + block.add_transaction(plain(A, player, 7)); // 6: emitted by that one, not by 40.0 + block.add_transaction(plain(player, A, 8)); // 7: a plain transfer to A + + const auto identity = [](const csdb::Address& a) { return a; }; + EXPECT_FALSE(cs::SmartContracts::emitting_new_state(block, 0, A, identity).has_value()); + auto ref = cs::SmartContracts::emitting_new_state(block, 2, A, identity); + ASSERT_TRUE(ref.has_value()); + EXPECT_EQ(ref->sequence, 40u); + EXPECT_EQ(ref->transaction, 0u); + ref = cs::SmartContracts::emitting_new_state(block, 4, B, identity); + ASSERT_TRUE(ref.has_value()); + EXPECT_EQ(ref->sequence, 41u); + ref = cs::SmartContracts::emitting_new_state(block, 6, A, identity); + ASSERT_TRUE(ref.has_value()); + EXPECT_EQ(ref->sequence, 45u); + EXPECT_EQ(ref->transaction, 2u); + // B's new_state is no answer for A, and an index past the block is bounded + 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()); +} From 8b2bfe5ade17ed578becbd743f722f107eb3e175 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 17:58:56 +0200 Subject: [PATCH 3/3] solver: charge an emitted transaction to the contract's latest successful execution in the block Review of step 1: a live node applies every new_state of the block (Conveyer::statesCreated -> on_update) before it charges any emitted transaction, so with several executions of one contract in a block it charges all their payouts to the latest successful one, not to the execution that precedes each payout. The helper now returns the highest RefStart among the contract's new_states with a non-zero hash, wherever the emitted transaction stands; the rollback uses the same rule. Co-Authored-By: Claude Opus 5.5 --- solver/include/solver/smartcontracts.hpp | 9 ++-- solver/src/smartcontracts.cpp | 34 ++++++++++---- tests/csnode/smartcontracts_tests.cpp | 57 ++++++++++++------------ 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/solver/include/solver/smartcontracts.hpp b/solver/include/solver/smartcontracts.hpp index dd3feec0d..9e57effa2 100644 --- a/solver/include/solver/smartcontracts.hpp +++ b/solver/include/solver/smartcontracts.hpp @@ -352,11 +352,10 @@ class SmartContracts final { // new state of contract, result of invocation of executable transaction static bool is_new_state(const csdb::Transaction&); - // the start of the execution that emitted transaction tr_idx of the block: the RefStart of the - // contract's new_state nearest before it in the same block (the smart consensus puts every - // emitted transaction after its new_state); nullopt when there is none. The live nodes, which - // have the new state before the block, charge an emitted transaction's fee to that execution's - // initiator; this finds it from the block alone, whatever state the node holds. + // the execution an emitted transaction of the contract is charged to, from the block alone: the + // contract's latest successful execution in the block (highest RefStart of its new_states with a + // non-zero hash), which is what a live node charges since it applies every new_state of the block + // before the emitted transactions; nullopt when the block has none. tr_idx is not used by the rule. static std::optional emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr, const std::function& absolute); diff --git a/solver/src/smartcontracts.cpp b/solver/src/smartcontracts.cpp index e78d1771d..6c17cc37d 100644 --- a/solver/src/smartcontracts.cpp +++ b/solver/src/smartcontracts.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -321,18 +322,33 @@ bool SmartContracts::is_start(const csdb::Transaction& tr) { /*static*/ std::optional SmartContracts::emitting_new_state(const csdb::Pool& block, size_t tr_idx, const csdb::Address& abs_addr, const std::function& absolute) { - const auto& transactions = block.transactions(); - for (size_t i = std::min(tr_idx, transactions.size()); i > 0; --i) { - const auto& t = transactions[i - 1]; - if (is_new_state(t) && absolute(t.target()) == abs_addr) { - const csdb::UserField fld = t.user_field(trx_uf::new_state::RefStart); - if (!fld.is_valid()) { - return std::nullopt; + // A live node applies every new_state of the block (Conveyer::statesCreated -> on_update) before + // it charges any emitted transaction, so the execution it charges is the contract's latest + // successful one in the block, wherever the emitted transaction stands in it. A failed execution + // (zero hash) does not change the contract's execution and is not counted. + (void)tr_idx; + std::optional latest; + for (const auto& t : block.transactions()) { + if (!is_new_state(t) || absolute(t.target()) != abs_addr) { + continue; + } + const csdb::UserField hash = t.user_field(trx_uf::new_state::Hash); + if (hash.is_valid()) { + const std::string bytes = hash.value(); + if (bytes.size() == cs::Hash{}.size() && std::all_of(bytes.begin(), bytes.end(), [](char c) { return c == 0; })) { + continue; } - return SmartContractRef(fld); + } + const csdb::UserField fld = t.user_field(trx_uf::new_state::RefStart); + if (!fld.is_valid()) { + continue; + } + const SmartContractRef ref(fld); + if (!latest.has_value() || *latest < ref) { + latest = ref; } } - return std::nullopt; + return latest; } bool SmartContracts::is_new_state(const csdb::Transaction& tr) { diff --git a/tests/csnode/smartcontracts_tests.cpp b/tests/csnode/smartcontracts_tests.cpp index 4e3ff8470..fb7ad3405 100644 --- a/tests/csnode/smartcontracts_tests.cpp +++ b/tests/csnode/smartcontracts_tests.cpp @@ -99,9 +99,9 @@ csdb::Transaction plain(csdb::Address src, csdb::Address tgt, int64_t id) { } // a new_state of contract `contract` for the execution started at ref -csdb::Transaction newState(csdb::Address contract, cs::Sequence seq, cs::Sequence index, int64_t id) { +csdb::Transaction newState(csdb::Address contract, cs::Sequence seq, cs::Sequence index, int64_t id, char hashByte = 'h') { auto t = plain(contract, contract, id); - t.add_user_field(cs::trx_uf::new_state::Hash, std::string(32, 'h')); + t.add_user_field(cs::trx_uf::new_state::Hash, std::string(32, hashByte)); cs::SmartContractRef ref; ref.sequence = seq; ref.transaction = index; @@ -110,35 +110,36 @@ csdb::Transaction newState(csdb::Address contract, cs::Sequence seq, cs::Sequenc } } // namespace -// the execution that emitted a transaction is the contract's nearest new_state before it in the -// block: the fee of an emitted transaction goes to that execution's initiator (step 1 of the -// contract state sync plan), whatever state the node holds -TEST(SmartContract, EmittedTransactionBelongsToTheNewStateBeforeIt) { +// the execution an emitted transaction is charged to is the contract's latest successful execution +// in the block (step 1 of the contract state sync plan): a live node applies every new_state of the +// block before it charges the emitted transactions, so it charges that one, wherever they stand +TEST(SmartContract, EmittedTransactionIsChargedToTheLatestExecutionInTheBlock) { const auto A = addressOf(1), B = addressOf(2), player = addressOf(3); csdb::Pool block(csdb::PoolHash{}, 50); - block.add_transaction(plain(A, player, 1)); // 0: emitted by A before any new_state of A - block.add_transaction(newState(A, 40, 0, 2)); // 1: A's execution started at 40.0 - block.add_transaction(plain(A, player, 3)); // 2: emitted by it - block.add_transaction(newState(B, 41, 1, 4)); // 3: B's execution started at 41.1 - block.add_transaction(plain(B, player, 5)); // 4: emitted by it - block.add_transaction(newState(A, 45, 2, 6)); // 5: A's next execution, started at 45.2 - block.add_transaction(plain(A, player, 7)); // 6: emitted by that one, not by 40.0 + block.add_transaction(newState(A, 40, 0, 1)); // 0: A's execution started at 40.0 + block.add_transaction(plain(A, player, 2)); // 1: emitted by it + block.add_transaction(newState(B, 41, 1, 3)); // 2: B's execution started at 41.1 + block.add_transaction(plain(B, player, 4)); // 3: emitted by it + block.add_transaction(newState(A, 45, 2, 5)); // 4: A's next execution, started at 45.2 + block.add_transaction(plain(A, player, 6)); // 5: emitted by that one + const auto failed = newState(A, 47, 0, 7, '\0'); // 6: A's last execution failed (zero hash) + block.add_transaction(failed); block.add_transaction(plain(player, A, 8)); // 7: a plain transfer to A const auto identity = [](const csdb::Address& a) { return a; }; - EXPECT_FALSE(cs::SmartContracts::emitting_new_state(block, 0, A, identity).has_value()); - auto ref = cs::SmartContracts::emitting_new_state(block, 2, A, identity); - ASSERT_TRUE(ref.has_value()); - EXPECT_EQ(ref->sequence, 40u); - EXPECT_EQ(ref->transaction, 0u); - ref = cs::SmartContracts::emitting_new_state(block, 4, B, identity); - ASSERT_TRUE(ref.has_value()); - EXPECT_EQ(ref->sequence, 41u); - ref = cs::SmartContracts::emitting_new_state(block, 6, A, identity); - ASSERT_TRUE(ref.has_value()); - EXPECT_EQ(ref->sequence, 45u); - EXPECT_EQ(ref->transaction, 2u); - // B's new_state is no answer for A, and an index past the block is bounded - 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()); + // both payouts of A go to 45.2, the latest successful execution: the failed 47.0 does not count + for (size_t i : {size_t(1), size_t(5)}) { + const auto ref = cs::SmartContracts::emitting_new_state(block, i, A, identity); + ASSERT_TRUE(ref.has_value()); + EXPECT_EQ(ref->sequence, 45u); + EXPECT_EQ(ref->transaction, 2u); + } + const auto refB = cs::SmartContracts::emitting_new_state(block, 3, B, identity); + ASSERT_TRUE(refB.has_value()); + EXPECT_EQ(refB->sequence, 41u); + // a contract without a successful execution in the block: none (the caller falls back) + csdb::Pool other(csdb::PoolHash{}, 51); + other.add_transaction(failed); + other.add_transaction(plain(A, player, 9)); + EXPECT_FALSE(cs::SmartContracts::emitting_new_state(other, 1, A, identity).has_value()); }