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: 7 additions & 0 deletions .github/workflows/robustness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 76 additions & 1 deletion ci/testnet/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()}
Expand All @@ -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:
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion ci/testnet/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions solver/include/solver/smartcontracts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ class SmartContracts final {
// new state of contract, result of invocation of executable transaction
static bool is_new_state(const csdb::Transaction&);

// 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<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);

// 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
72 changes: 55 additions & 17 deletions solver/src/smartcontracts.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <smartcontracts.hpp>
#include <solvercontext.hpp>

Expand Down Expand Up @@ -318,6 +319,38 @@ bool SmartContracts::is_start(const csdb::Transaction& tr) {
}

/*static*/
/*static*/
std::optional<SmartContractRef> SmartContracts::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) {
// 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<SmartContractRef> 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<std::string>();
if (bytes.size() == cs::Hash{}.size() && std::all_of(bytes.begin(), bytes.end(), [](char c) { return c == 0; })) {
continue;
}
}
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 latest;
}

bool SmartContracts::is_new_state(const csdb::Transaction& tr) {
//csdebug() << kLogPrefix << __func__;
if (!tr.is_valid()) {
Expand Down Expand Up @@ -1673,20 +1706,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<size_t>(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);
}
}
}
Expand Down Expand Up @@ -1838,12 +1865,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);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/csnode/smartcontracts_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,58 @@ 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, char hashByte = 'h') {
auto t = plain(contract, contract, id);
t.add_user_field(cs::trx_uf::new_state::Hash, std::string(32, hashByte));
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 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(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; };
// 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());
}
Loading