From 365764020f3f52776aee505bfd26cfd0d542c3c0 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 30 Oct 2026 15:28:05 +0100 Subject: [PATCH 1/2] api: token API leftovers (pages of transfers, TokenTransferGet, counters, cache lock in checkpoints) - TokenTransfersGet/TokenWalletTransfersGet: a failed transfer is not listed, but it was counted by the offset and the limit before addTokenResult dropped it, so pages came back short. It is now skipped before them. - TokenTransferGet listed any transaction id given, of any contract or none, and the state lookup ((*opers)[id]) added an entry for every id asked. It now answers NOT_FOUND unless the transaction is a transfer call to that token; count is the number listed. - The state of an operation is read without adding to the map (operationState). - 64-bit token counters in i32 API fields report the largest i32 instead of wrapping. - TokensMaster_Serializer saves, loads, clears and hashes the cache under TokensMaster's lock: the API changes it from the block store thread and, since #128, from the background refresh, which can run while a checkpoint is saved. tokens.py: a page of 2 transfers holds 2 though a failed transfer lies between them; TokenTransferGet returns a transfer and refuses an approve(). --- api/include/apihandler.hpp | 1 + api/src/apihandler.cpp | 51 +++++++++++++++------ ci/testnet/tokens.py | 14 ++++++ csnode/include/csnode/tokens_serializer.hpp | 3 ++ csnode/src/tokens_serializer.cpp | 5 ++ docs/CHANGES.md | 1 + 6 files changed, 60 insertions(+), 15 deletions(-) diff --git a/api/include/apihandler.hpp b/api/include/apihandler.hpp index f112a28b4..fd98bb8c6 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); void processTransaction(csdb::Transaction trx, uint64_t requestId); void processSmartTransaction(const Transaction& transaction, csdb::Transaction transactionToSend, uint64_t requestId); diff --git a/api/src/apihandler.cpp b/api/src/apihandler.cpp index c9a2c9c23..6a47b0406 100644 --- a/api/src/apihandler.cpp +++ b/api/src/apihandler.cpp @@ -2475,6 +2475,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; @@ -2496,8 +2503,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); @@ -2515,21 +2521,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))); } @@ -2553,15 +2563,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; @@ -2587,6 +2597,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; @@ -2814,14 +2828,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); } @@ -3059,7 +3080,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)) { @@ -3074,7 +3095,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 d04268f96..11a81dec7 100644 --- a/ci/testnet/tokens.py +++ b/ci/testnet/tokens.py @@ -273,6 +273,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 816f8ef07..59a4c2c48 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -45,6 +45,7 @@ lines under the new release. | #127 | ci: token tests: `ci/contracts/executor_harness.py` compares contract executors on token contracts without a node, and `TOKENS=1` runs `ci/testnet/tokens.py` (v1 and v0 tokens, balances and states on every node, the token API; `TOKENS_VIA` through another node) on the private network | | #128 | api: the token cache (`[api] tokens_cache=true`, any build) follows v1/v2 tokens (BigDecimal amounts, standards 3 and 4) and only successful calls. It reads balances from the contract for the wallets a call involves, refreshes them in the background at start, and is rebuilt from the chain when the checkpoint holds none. `approve()` without arguments no longer reads out of bounds. `TokensListGet` filters before paging, and the token lists sort without exceptions or invalid orderings. `TokenTransfersListGet` reports the signer as the sender. See [tokens.md](tokens.md) | | #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) | +| — | 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 From 82b74de49c3c1d2441e16c478857049a658c681d Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 30 Oct 2026 15:29:11 +0100 Subject: [PATCH 2/2] docs: CHANGES line for #134 --- docs/CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CHANGES.md b/docs/CHANGES.md index 59a4c2c48..0fc43ec64 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -45,7 +45,7 @@ lines under the new release. | #127 | ci: token tests: `ci/contracts/executor_harness.py` compares contract executors on token contracts without a node, and `TOKENS=1` runs `ci/testnet/tokens.py` (v1 and v0 tokens, balances and states on every node, the token API; `TOKENS_VIA` through another node) on the private network | | #128 | api: the token cache (`[api] tokens_cache=true`, any build) follows v1/v2 tokens (BigDecimal amounts, standards 3 and 4) and only successful calls. It reads balances from the contract for the wallets a call involves, refreshes them in the background at start, and is rebuilt from the chain when the checkpoint holds none. `approve()` without arguments no longer reads out of bounds. `TokensListGet` filters before paging, and the token lists sort without exceptions or invalid orderings. `TokenTransfersListGet` reports the signer as the sender. See [tokens.md](tokens.md) | | #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) | -| — | 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) | +| #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