diff --git a/api/include/apihandler.hpp b/api/include/apihandler.hpp index 799bfe773..3a63969f2 100644 --- a/api/include/apihandler.hpp +++ b/api/include/apihandler.hpp @@ -308,6 +308,7 @@ 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); + SmartOperationState operationState(const csdb::TransactionID& id); std::vector contractMethods(const csdb::Address& contract, const std::vector& byteCode); std::mutex contractMethodsLock_; std::unordered_map> contractMethods_; diff --git a/api/src/apihandler.cpp b/api/src/apihandler.cpp index e1023fc51..66ec4895a 100644 --- a/api/src/apihandler.cpp +++ b/api/src/apihandler.cpp @@ -2504,6 +2504,13 @@ void APIHandler::ContractAllMethodsGet(ContractAllMethodsGetResult& _return, con } } +// the state of a contract call the API knows, Pending for one it does not (never adds an entry) +SmartOperationState APIHandler::operationState(const csdb::TransactionID& id) { + auto opers = cs::lockedReference(this->smart_operations); + const auto it = opers->find(id); + return static_cast(it == opers->end() ? SmartOperation::State::Pending : it->second.state); +} + void APIHandler::addTokenResult(api::TokenTransfersResult& _return, const csdb::Address& token, const std::string& code, const csdb::Pool& pool, const csdb::Transaction& tr, const api::SmartContractInvocation& smart, const std::pair& addrPair) { api::TokenTransfer transfer; @@ -2525,8 +2532,7 @@ void APIHandler::addTokenResult(api::TokenTransfersResult& _return, const csdb:: if (ufd.is_valid()) { transfer.__set_userFields(ufd.value()); } - auto opers = cs::lockedReference(this->smart_operations); - transfer.state = static_cast((*opers)[tr.id()].state); + transfer.state = operationState(tr.id()); if (transfer.state == SOS_Success) { _return.transfers.push_back(transfer); @@ -2544,21 +2550,25 @@ void APIHandler::addTokenResult(api::TokenTransactionsResult& _return, const csd trans.method = smart.method; trans.params = smart.params; - auto opers = cs::lockedReference(this->smart_operations); - trans.state = static_cast((*opers)[tr.id()].state); + trans.state = operationState(tr.id()); _return.transactions.push_back(trans); } +// a 64-bit counter in an i32 field of the API: the largest i32 instead of a wrapped value +static int32_t toInt32(uint64_t value) { + return static_cast(std::min(value, static_cast(std::numeric_limits::max()))); +} + void putTokenInfo(api::TokenInfo& ti, const general::Address& addr, const Token& token) { ti.address = addr; ti.code = token.symbol; ti.name = token.name; ti.totalSupply = token.totalSupply; ti.owner = fromByteArray(token.owner.public_key()); - ti.transfersCount = int32_t(token.transfersCount); - ti.transactionsCount = int32_t(token.transactionsCount); - ti.holdersCount = int32_t(token.realHoldersCount); + ti.transfersCount = toInt32(token.transfersCount); + ti.transactionsCount = toInt32(token.transactionsCount); + ti.holdersCount = toInt32(token.realHoldersCount); ti.tokenStandard = decltype(api::TokenInfo::tokenStandard)(uint32_t((token.tokenStandard))); } @@ -2582,15 +2592,15 @@ void APIHandler::tokenTransactionsInternal(ResultType& _return, APIHandler& hand //cslog() << __func__ << ": Tokens found"; code = it->second.symbol; if (transfersOnly && !filterByWallet) { - _return.count = static_cast(it->second.transfersCount); + _return.count = toInt32(it->second.transfersCount); } else if (!transfersOnly) { - _return.count = static_cast(it->second.transactionsCount); + _return.count = toInt32(it->second.transactionsCount); } else if (transfersOnly && filterByWallet) { auto hIt = it->second.holders.find(wallet); if (hIt != it->second.holders.end()) { - _return.count = static_cast(hIt->second.transfersCount); + _return.count = toInt32(hIt->second.transfersCount); } else { _return.count = 0; @@ -2616,6 +2626,10 @@ void APIHandler::tokenTransactionsInternal(ResultType& _return, APIHandler& hand if (filterByWallet && addrPair.first != wallet && addrPair.second != wallet) { return true; } + // a failed transfer moved nothing and is not listed: it must not take a place in the page + if (transfersOnly && handler.operationState(tr.id()) != SOS_Success) { + return true; + } if (--offset >= 0) { return true; @@ -2843,14 +2857,21 @@ void APIHandler::TokenTransferGet(api::TokenTransfersResult& _return, const gene return; } - const auto pool = executor_.loadBlockApi(trxn.id().pool_seq()); + if (!trxn.is_valid() || !is_smart(trxn) || blockchain_.getAddressByType(trxn.target(), BlockChain::AddressType::PublicKey) != addr) { + SetResponseStatus(_return.status, APIRequestStatusType::NOT_FOUND, "no call to this token with this id"); + return; + } const auto smart = fetch_smart(trxn); + if (!TokensMaster::isTransfer(smart.method, smart.params)) { + SetResponseStatus(_return.status, APIRequestStatusType::NOT_FOUND, "the transaction is not a transfer of this token"); + return; + } + const auto pool = executor_.loadBlockApi(trxn.id().pool_seq()); const auto addr_pk = blockchain_.getAddressByType(trxn.source(), BlockChain::AddressType::PublicKey); const auto addrPair = TokensMaster::getTransferData(addr_pk, smart.method, smart.params); - _return.count = 1; - addTokenResult(_return, addr, code, pool, trxn, smart, addrPair); + _return.count = static_cast(_return.transfers.size()); SetResponseStatus(_return.status, APIRequestStatusType::SUCCESS); } @@ -3088,7 +3109,7 @@ void APIHandler::TokenHoldersGet(api::TokenHoldersResult& _return, const general auto tIt = tm_.find(addr); if (tIt != tm_.end()) { found = true; - _return.count = static_cast(tIt->second.realHoldersCount); + _return.count = toInt32(tIt->second.realHoldersCount); applyToSortedMap(tIt->second.holders, comparator, [&offset, &limit, &_return, &token](const HMap::value_type& t) { if (TokensMaster::isZeroAmount(t.second.balance)) { @@ -3103,7 +3124,7 @@ void APIHandler::TokenHoldersGet(api::TokenHoldersResult& _return, const general th.holder = fromByteArray(t.first.public_key()); th.token = token; th.balance = t.second.balance; - th.transfersCount = static_cast(t.second.transfersCount); + th.transfersCount = toInt32(t.second.transfersCount); _return.holders.push_back(th); diff --git a/ci/testnet/tokens.py b/ci/testnet/tokens.py index c761fff28..5f7f08013 100644 --- a/ci/testnet/tokens.py +++ b/ci/testnet/tokens.py @@ -285,6 +285,20 @@ def main(): f"listed {len(tr.transfers or [])}", flush=True) run.check(tr.status.code == 0 and len(tr.transfers or []) >= 3, f"TokenTransfersGet lists the v1 transfers (got {len(tr.transfers or [])})", kind="api") + # a page of 2 transfers: the failed transfer in between takes no place in it + page = c.TokenTransfersGet(v1, 0, 2) + run.check(len(page.transfers or []) == 2, f"TokenTransfersGet with limit 2 lists 2 transfers (got {len(page.transfers or [])})", + kind="api") + if tr.transfers: + one = c.TokenTransferGet(v1, tr.transfers[0].transaction) + run.check(one.status.code == 0 and len(one.transfers or []) == 1 and one.count == 1, + "TokenTransferGet of a transfer returns it", kind="api") + txs = c.TokenTransactionsGet(v1, 0, 100) + approve = next((t for t in (txs.transactions or []) if t.method == "approve"), None) + if approve is not None: + other = c.TokenTransferGet(v1, approve.transaction) + run.check(other.status.code != 0 and not other.transfers, "TokenTransferGet of an approve() is not a transfer", + kind="api") holders = c.TokenHoldersGet(v1, 0, 100, 0, True) print(f" TokenHoldersGet TokenV1: status {holders.status.code} " f"{[(b58(h.holder)[:8], h.balance) for h in (holders.holders or [])]}", flush=True) diff --git a/csnode/include/csnode/tokens_serializer.hpp b/csnode/include/csnode/tokens_serializer.hpp index 8e20d9a0c..a8b383b86 100644 --- a/csnode/include/csnode/tokens_serializer.hpp +++ b/csnode/include/csnode/tokens_serializer.hpp @@ -1,6 +1,7 @@ #ifndef TOKENS_SERIALIZER_HPP #define TOKENS_SERIALIZER_HPP #include +#include #include #include @@ -74,6 +75,8 @@ class TokensMaster_Serializer { TokensMap *tokens_; HoldersMap *holders_; + // TokensMaster::dataMut_: the API updates the cache from other threads (block store, refresh) + std::mutex* mutex_ = nullptr; }; } // namespace cs #endif // TOKENS_SERIALIZER_HPP diff --git a/csnode/src/tokens_serializer.cpp b/csnode/src/tokens_serializer.cpp index 39be7b973..e2e226b7f 100644 --- a/csnode/src/tokens_serializer.cpp +++ b/csnode/src/tokens_serializer.cpp @@ -21,16 +21,19 @@ namespace cs { void TokensMaster_Serializer::bind(TokensMaster& tokens) { tokens_ = reinterpret_cast(&tokens.tokens_); holders_ = reinterpret_cast(&tokens.holders_); + mutex_ = &tokens.dataMut_; csdebug() << "TokensMaster bindings made"; } void TokensMaster_Serializer::clear(const std::filesystem::path& rootDir) { (void)rootDir; + std::lock_guard lock(*mutex_); tokens_->clear(); holders_->clear(); } void TokensMaster_Serializer::save(const std::filesystem::path& rootDir) { + std::lock_guard lock(*mutex_); std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); boost::archive::text_oarchive oa(ofs); oa << *tokens_; @@ -39,6 +42,7 @@ void TokensMaster_Serializer::save(const std::filesystem::path& rootDir) { ::cscrypto::Hash TokensMaster_Serializer::hash() { { + std::lock_guard lock(*mutex_); std::ofstream ofs(kDataFileName, std::ios::binary); // text mode on Windows would write \r\n and change the hash ofs.imbue(std::locale::classic()); // with no_codecvt the archive formats numbers in the stream's locale boost::archive::text_oarchive oa( @@ -62,6 +66,7 @@ ::cscrypto::Hash TokensMaster_Serializer::hash() { } void TokensMaster_Serializer::load(const std::filesystem::path& rootDir) { + std::lock_guard lock(*mutex_); std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); boost::archive::text_iarchive ia(ifs); ia >> *tokens_; diff --git a/docs/CHANGES.md b/docs/CHANGES.md index de5bed507..b06cf4e56 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -48,6 +48,7 @@ lines under the new release. | #131 | packaging: a guided set-up in every package (`setup.sh`, `setup.bat`): pruned or full, database, how the first start gets the chain, snapshot sharing, and the contract executor (community or official: it shows the build in the folder, can download either, checked, and sets `executor_commit_min/max` so that the node starts it; an upgrade by copying only the node binary keeps the old executor) | | #132 | api: `TransactionFlow` refuses a contract call that no method of the contract matches (unknown method, wrong argument count, or a Variant that cannot match the declared type, such as a token amount as `v_string` for a `BigDecimal`), which would fail on every node with its fee paid; ci: `testnet.yml` inputs `tokens`/`tokens_via`, the executor harness compares only the answers | | #135 | samples: a token template (`samples/contracts/Token.java`: checked amounts and decimals, balances at the token's scale, ERC-20 `approve()`, sale through `payable()`), run by the executor harness, and a Python client that calls a contract with the argument types it declares (`samples/python/token_client.py`) | +| #134 | api: token API leftovers: a failed transfer no longer takes a place in the pages of `TokenTransfersGet`/`TokenWalletTransfersGet`; `TokenTransferGet` answers only for a transfer of that token (it listed any transaction and added entries to the operations map); token counters above 2^31 report the largest i32 instead of wrapping; the token cache is saved, loaded and hashed under its lock (the background refresh could change it during a checkpoint) | ## community.14 — 2026-10-27