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
9 changes: 9 additions & 0 deletions .github/workflows/testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ on:
contracts:
description: 'Run a contract executor per node and call a contract across the restart test (0 = off)'
default: '0'
tokens:
description: 'With contracts: deploy a v1 and a v0 token and use them through the API of tokens_via, checking balances, states and the token API (ci/testnet/tokens.py; 0 = off)'
default: '0'
tokens_via:
description: 'With tokens: the node whose API the token calls go through (e.g. the restarted one; default 1)'
default: '1'
executor_ref:
description: 'With contracts: branch or commit of EnzinoBB/contract-executor built and run by the even nodes, the odd nodes keep the release executor (empty = all release)'
default: ''
Expand Down Expand Up @@ -270,6 +276,9 @@ jobs:
DELEGATE: ${{ inputs.delegate || '0' }}
MIN_STAKE: ${{ inputs.min_stake || '0' }}
CONTRACTS: ${{ inputs.contracts || '0' }}
TOKENS: ${{ inputs.tokens || '0' }}
TOKENS_VIA: ${{ inputs.tokens_via || '1' }}
TOKENS_STRICT_API: ${{ inputs.tokens || '0' }}
CONTRACT_LOAD: ${{ inputs.contract_load || '' }}
RESTART_NODES: ${{ inputs.restart_nodes || '' }}
FUND: '1'
Expand Down
2 changes: 2 additions & 0 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ add_library(csconnector
include/debuglog.hpp
include/tokens.hpp
src/tokens.cpp
include/callcheck.hpp
src/callcheck.cpp
include/profiler/profilerprocessor.hpp
src/profilerprocessor.cpp
include/profiler/profilereventhandler.hpp
Expand Down
3 changes: 3 additions & 0 deletions api/include/apihandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ class APIHandler : public APIHandlerInterface {
::csdb::Transaction makeTransaction(const ::api::Transaction&);
void dumbTransactionFlow(api::TransactionFlowResult& _return, const csdb::Transaction& tr);
void smartTransactionFlow(api::TransactionFlowResult& _return, const ::api::Transaction&, csdb::Transaction& send_transaction);
std::vector<general::MethodDescription> contractMethods(const csdb::Address& contract, const std::vector<general::ByteCodeObject>& byteCode);
std::mutex contractMethodsLock_;
std::unordered_map<csdb::Address, std::vector<general::MethodDescription>> contractMethods_;
void processTransaction(csdb::Transaction trx, uint64_t requestId);
void processSmartTransaction(const Transaction& transaction, csdb::Transaction transactionToSend, uint64_t requestId);

Expand Down
25 changes: 25 additions & 0 deletions api/include/callcheck.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CALLCHECK_HPP
#define CALLCHECK_HPP

#include <string>
#include <vector>

#include <general_types.h>

namespace api {

// Why the executor cannot find the method a call names, or an empty string when it may.
//
// The executor picks the method with commons-beanutils' getMatchingAccessibleMethod: same name,
// same number of arguments, and every argument's class (from its Variant: v_string is a String,
// v_big_decimal a BigDecimal, v_int an Integer, ...) assignable to the declared type, a wrapper
// also to its primitive. A call that matches no method fails on every node, and its fee is paid
// all the same. Only certain mismatches are reported: an argument or a declared type outside
// String, BigDecimal, boolean, int, long, double (and their wrappers) and byte[] is taken as a
// possible match.
std::string checkCallArguments(const std::vector<general::MethodDescription>& methods, const std::string& method,
const std::vector<general::Variant>& params);

} // namespace api

#endif // CALLCHECK_HPP
29 changes: 29 additions & 0 deletions api/src/apihandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <src/priv_crypto.hpp>

#include "csconnector/csconnector.hpp"
#include "callcheck.hpp"

#include "stdafx.h"
#include "serializer.hpp"
Expand Down Expand Up @@ -1166,6 +1167,25 @@ void APIHandler::processSmartTransaction(const Transaction& transaction, csdb::T
}


// the public methods of a deployed contract, as the executor lists them; its byte code never changes,
// so they are asked once. Empty when the executor does not answer: nothing is checked then
std::vector<general::MethodDescription> APIHandler::contractMethods(const csdb::Address& contract, const std::vector<general::ByteCodeObject>& byteCode) {
{
std::lock_guard lock(contractMethodsLock_);
if (const auto it = contractMethods_.find(contract); it != contractMethods_.end()) {
return it->second;
}
}
executor::GetContractMethodsResult result;
executor_.getContractMethods(result, byteCode);
if (result.status.code != 0) {
return {};
}
std::lock_guard lock(contractMethodsLock_);
contractMethods_[contract] = result.methods;
return result.methods;
}

void APIHandler::smartTransactionFlow(api::TransactionFlowResult& _return, const ::api::Transaction& transaction, csdb::Transaction& send_transaction) {
auto input_smart = transaction.__isset.smartContract ? transaction.smartContract : SmartContractInvocation{};
//auto send_transaction = makeTransaction(transaction);
Expand Down Expand Up @@ -1198,6 +1218,15 @@ void APIHandler::smartTransactionFlow(api::TransactionFlowResult& _return, const
SetResponseStatus(_return.status, APIRequestStatusType::FAILURE);
return;
}
// a call whose method the executor cannot find fails on every node, and its fee is paid
// all the same: refused here, before it reaches the network
if (transaction.__isset.smartContract && !origin_bytecode.empty()) {
const auto why = checkCallArguments(contractMethods(smart_addr, origin_bytecode), input_smart.method, input_smart.params);
if (!why.empty()) {
SetResponseStatus(_return.status, APIRequestStatusType::FAILURE, why);
return;
}
}
}
else {
csdb::Address addr = blockchain_.getAddressByType(send_transaction.target(), BlockChain::AddressType::PublicKey);
Expand Down
150 changes: 150 additions & 0 deletions api/src/callcheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#include "callcheck.hpp"

#include <map>
#include <sstream>

namespace {

// the class the executor gives an argument (ContractExecutorServiceUtils.getArgTypes), for the
// Variants whose class is certain; empty for the others
std::string argumentClass(const general::Variant& v) {
if (v.__isset.v_string) return "java.lang.String";
if (v.__isset.v_big_decimal) return "java.math.BigDecimal";
if (v.__isset.v_boolean || v.__isset.v_boolean_box) return "java.lang.Boolean";
if (v.__isset.v_int || v.__isset.v_int_box) return "java.lang.Integer";
if (v.__isset.v_long || v.__isset.v_long_box) return "java.lang.Long";
if (v.__isset.v_double || v.__isset.v_double_box) return "java.lang.Double";
if (v.__isset.v_byte_array) return "byte[]";
return {};
}

std::string variantName(const general::Variant& v) {
if (v.__isset.v_string) return "v_string";
if (v.__isset.v_big_decimal) return "v_big_decimal";
if (v.__isset.v_boolean) return "v_boolean";
if (v.__isset.v_boolean_box) return "v_boolean_box";
if (v.__isset.v_int) return "v_int";
if (v.__isset.v_int_box) return "v_int_box";
if (v.__isset.v_long) return "v_long";
if (v.__isset.v_long_box) return "v_long_box";
if (v.__isset.v_double) return "v_double";
if (v.__isset.v_double_box) return "v_double_box";
if (v.__isset.v_byte_array) return "v_byte_array";
return "another type";
}

// declared types whose assignability from the classes above is certain, with the class each accepts
const std::map<std::string, std::string>& knownTypes() {
static const std::map<std::string, std::string> types = {
{"java.lang.String", "java.lang.String"},
{"java.math.BigDecimal", "java.math.BigDecimal"},
{"java.lang.Boolean", "java.lang.Boolean"},
{"boolean", "java.lang.Boolean"},
{"java.lang.Integer", "java.lang.Integer"},
{"int", "java.lang.Integer"},
{"java.lang.Long", "java.lang.Long"},
{"long", "java.lang.Long"},
{"java.lang.Double", "java.lang.Double"},
{"double", "java.lang.Double"},
{"byte[]", "byte[]"},
};
return types;
}

// the Variant a wallet sends for a declared type
std::string variantFor(const std::string& type) {
static const std::map<std::string, std::string> variants = {
{"java.lang.String", "v_string"}, {"java.math.BigDecimal", "v_big_decimal"},
{"java.lang.Boolean", "v_boolean_box"}, {"boolean", "v_boolean"},
{"java.lang.Integer", "v_int_box"}, {"int", "v_int"},
{"java.lang.Long", "v_long_box"}, {"long", "v_long"},
{"java.lang.Double", "v_double_box"}, {"double", "v_double"},
{"byte[]", "v_byte_array"},
};
const auto it = variants.find(type);
return it == variants.end() ? std::string{} : it->second;
}

bool certainlyIncompatible(const std::string& declared, const general::Variant& argument) {
const std::string actual = argumentClass(argument);
const auto it = knownTypes().find(declared);
return !actual.empty() && it != knownTypes().end() && it->second != actual;
}

std::string signature(const general::MethodDescription& m) {
std::ostringstream os;
os << m.name << '(';
for (size_t i = 0; i < m.arguments.size(); ++i) {
os << (i ? ", " : "") << m.arguments[i].type;
}
os << ')';
return os.str();
}

} // namespace

namespace api {

std::string checkCallArguments(const std::vector<general::MethodDescription>& methods, const std::string& method,
const std::vector<general::Variant>& params) {
// the methods of Object are callable but not listed by the executor (getContractMethods)
static const char* objectMethods[] = {"getClass", "hashCode", "equals", "toString", "notify", "notifyAll", "wait", "finalize"};
for (const char* o : objectMethods) {
if (method == o) {
return {};
}
}

std::vector<const general::MethodDescription*> named;
for (const auto& m : methods) {
if (m.name == method) {
named.push_back(&m);
}
}
if (named.empty()) {
return "the contract has no public method " + method + "(): the call would fail and its fee be paid";
}

std::vector<const general::MethodDescription*> sameCount;
for (const auto* m : named) {
if (m->arguments.size() == params.size()) {
sameCount.push_back(m);
}
}
std::ostringstream os;
if (sameCount.empty()) {
os << method << " takes ";
for (size_t i = 0; i < named.size(); ++i) {
os << (i ? " or " : "") << named[i]->arguments.size();
}
os << " arguments, the call passes " << params.size() << ": it would fail and its fee be paid ("
<< signature(*named.front()) << ")";
return os.str();
}

std::string firstMismatch;
for (const auto* m : sameCount) {
std::string mismatch;
for (size_t i = 0; i < params.size() && mismatch.empty(); ++i) {
if (certainlyIncompatible(m->arguments[i].type, params[i])) {
std::ostringstream one;
one << signature(*m) << ": argument " << i + 1 << " is " << m->arguments[i].type;
const auto expected = variantFor(m->arguments[i].type);
if (!expected.empty()) {
one << " (" << expected << ")";
}
one << ", the call passes " << variantName(params[i]);
mismatch = one.str();
}
}
if (mismatch.empty()) {
return {}; // this method may match
}
if (firstMismatch.empty()) {
firstMismatch = mismatch;
}
}
return "no method matches the arguments, the call would fail and its fee be paid: " + firstMismatch;
}

} // namespace api
22 changes: 15 additions & 7 deletions ci/contracts/executor_harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

Each step records the status code, the return value, a hash of every contract state the executor
returns and the emitted transactions. The first jar is the reference (the release executor): with
two or more jars, any step whose record differs is a FAIL, because the node accepts a contract
result only if the confidants' executors agree. Every scenario also checks its own expectations
two or more jars, any answer that differs is a FAIL, because the node accepts a contract result
only if the confidants' executors agree. What only the node's API reads (the token standard of
getContractMethods) may differ. Every scenario also checks its own expectations
(an expectation that does not hold is reported as a finding, not as a difference).
"""
import argparse
Expand Down Expand Up @@ -364,10 +365,17 @@ def expect(self, condition, finding, severity="bug"):
return condition


# what only the node's API reads, never contract execution: an executor may improve it
API_ONLY = ("tokenStandard", "stopped_when_the_client_left")


def comparable(step):
# the executor's error text carries stack traces and line numbers; the status code and the
# first line are what the node keeps (ret_val of a failed call is the message)
return json.dumps(step, sort_keys=True)
"""What two executors must answer alike: the answers of deploys, calls and getters. The
scenarios' own expectations are left out (a fixed executor meets more of them), and so are the
fields only the API reads."""
if "expect" in step:
return None
return json.dumps({k: v for k, v in step.items() if k not in API_ONLY}, sort_keys=True)


def main():
Expand Down Expand Up @@ -421,8 +429,8 @@ def main():
for f in run["findings"]:
print(f" [{f['severity']}] {f['scenario']}: {f['finding']}")
for other in runs[1:]:
a = [comparable(s) for s in ref["steps"]]
b = [comparable(s) for s in other["steps"]]
a = [c for c in map(comparable, ref["steps"]) if c is not None]
b = [c for c in map(comparable, other["steps"]) if c is not None]
diffs = [(i, x, y) for i, (x, y) in enumerate(zip(a, b)) if x != y]
if len(a) != len(b):
diffs.append((min(len(a), len(b)), f"{len(a)} steps", f"{len(b)} steps"))
Expand Down
54 changes: 50 additions & 4 deletions ci/contracts/token_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,16 @@ def slow_token_name(ex, rec, Contract):
"""A token whose getName() never returns: the reserved-name check runs it without a time limit"""
s = Contract(ex, "SlowName", SLOW_NAME, OWNER, rec)
d = s.deploy(timeout=40000)
rec.expect(d is not None, "the deploy of a token whose getName() loops answers within 40 s", severity="security")
# build 1518 never answers and its thread loops for good; a fixed executor stops the code after
# its limit and holds the answer back until the node has stopped waiting (pass
# -Dcontract.executor.unbounded.code.limit.ms / .hold.ms with --java-opt to see it answer)
rec.expect(d is not None, "the deploy of a token whose getName() loops gets an answer within 40 s", severity="info")
probe = Contract(ex, "BasicV0probe", BASIC_V0.replace("BasicV0", "BasicV0probe"), OWNER, rec)
try:
ok = probe.compile()
except Exception:
ok = False
rec.expect(ok, "the executor still answers afterwards", severity="security")
rec.expect(ok, "the executor still answers afterwards", severity="bug")


def serialization_hang(ex, rec, Contract):
Expand All @@ -509,7 +512,7 @@ def serialization_hang(ex, rec, Contract):
answered = False
finally:
c.close()
rec.expect(answered, "a call whose new state never finishes serializing answers within 40 s", severity="security")
rec.expect(answered, "a call whose new state never finishes serializing gets an answer within 40 s", severity="info")


def emitted_precision(ex, rec, Contract):
Expand All @@ -525,6 +528,49 @@ def emitted_precision(ex, rec, Contract):
severity="info")


SPIN = """import com.credits.scapi.v3.SmartContract;
import java.math.BigDecimal;

public class Spin extends SmartContract {
private long last;
public Spin() { super(); }
public long spin(long rounds) { long x = 0; for (long i = 0; i < rounds; i++) { x += i ^ (x >>> 3); } last = x; return x; }
public String payable(BigDecimal amount, byte[] userData) { return "ok"; }
}
"""


def abandoned_call(ex, rec, Contract):
"""A method that runs longer than the node waits: the node closes the connection"""
s = Contract(ex, "Spin", SPIN, OWNER, rec)
if not rec.expect((s.deploy() or {}).get("ok"), "Spin deploys"):
return
from executor_harness import EXECUTION_TIME, EXECUTOR_VERSION, executor, general
c = ex.client(3000) # the node's executor_receive_timeout is 4 s
try:
c.executeByteCode(ex.next_access_id(), ALICE, s._binary(s.state),
[executor.MethodHeader(methodName="spin", params=[general.Variant(v_long=10 ** 12)])],
EXECUTION_TIME, EXECUTOR_VERSION)
answered = True
except Exception:
answered = False
finally:
c.close()
rec.expect(not answered, "a method running past the client's wait gets no answer in time", severity="info")
import time
time.sleep(2)
# the executor logs to log/executor-log-<day>.txt in its folder (its logback.xml), or to stdout
stopped = False
for folder, _, files in os.walk(ex.work):
for name in files:
if name.endswith((".log", ".txt")):
with open(os.path.join(folder, name), errors="replace") as f:
stopped = stopped or f"{b58(s.address)}: the node closed the connection" in f.read()
rec.step("abandoned call", stopped_when_the_client_left=stopped)
rec.expect(stopped, "the executor stops a method whose client closed the connection", severity="info")
rec.expect(s.call(ALICE, "spin", general.Variant(v_long=1000))["ok"], "the executor answers the next call")


def general_int(i):
from executor_harness import v_int
return v_int(i)
Expand All @@ -536,5 +582,5 @@ def general_double(x):


SCENARIOS = [token_standards, token_v1_lifecycle, token_v0_lifecycle, failed_call_state, getter_writes,
reserved_names, root_class, platform_calls, emitted_precision, contract_calls, slow_token_name,
reserved_names, root_class, platform_calls, emitted_precision, contract_calls, abandoned_call, slow_token_name,
serialization_hang]
Loading
Loading