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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This repository is the community continuation of [CREDITSCOM/node](https://githu
| What changes from the official node | [docs/differences.md](docs/differences.md) |
| Every change, pull request by pull request | [docs/CHANGES.md](docs/CHANGES.md) |
| Block database, pruned storage, conversion | [docs/storage.md](docs/storage.md) |
| Tokens, the token API and the token tests | [docs/tokens.md](docs/tokens.md) |
| Docker (pruned node from a snapshot) | [packaging/docker/README.md](packaging/docker/README.md) |
| Packages (Linux x86-64, Windows x64, macOS Apple Silicon; `config.ini` full and `config.pruned.ini` pruned, one package per system) | [Releases](https://github.com/EnzinoBB/node/releases) |

Expand Down
8 changes: 8 additions & 0 deletions api/include/apihandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <atomic>
#include <deque>
#include <thread>
#include <queue>
#include <tuple>

Expand Down Expand Up @@ -184,6 +185,13 @@ class APIHandler : public APIHandlerInterface {
cs::DumbCv dumbCv_;

std::atomic<bool> isBDLoaded_{ false };
// the stored blocks are read (the last one, or run() after a quick start that read none): the
// token cache is refreshed once
void onBaseLoaded(bool refreshInBackground);
// an empty token cache on a node that has contracts (the cache was off when its checkpoint
// was saved): find the tokens among the contracts and their holders in the chain
void rebuildTokenCache();
std::thread tokensRefresh_;

struct smart_trxns_queue {
cs::SpinLock lock{ATOMIC_FLAG_INIT};
Expand Down
40 changes: 35 additions & 5 deletions api/include/tokens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class SmartContractInvocation;
using TokenId = csdb::Address;
using HolderKey = csdb::Address;

// the ids the executor reports (com.credits.scapi.misc.TokenStandardId)
enum TokenStandard {
NotAToken = 0,
CreditsBasic = 1,
CreditsExtended = 2
CreditsBasic = 1, // v0 BasicStandard: amounts as strings
CreditsExtended = 2, // v0 ExtensionStandard
CreditsBasicToken = 3, // v1 (and v2) BasicTokenStandard: amounts as BigDecimal
CreditsExtendedToken = 4 // v1 (and v2) ExtensionTokenStandard
};

struct Token {
Expand Down Expand Up @@ -60,9 +63,26 @@ class TokensMaster {
TokensMaster(api::APIHandler*);
~TokensMaster();

// [api] tokens_cache, or a RelMonitor/RelWallet build: the token cache is kept and served
bool enabled() const {
return enabled_;
}

void checkNewDeploy(const csdb::Address& sc, const csdb::Address& deployer, const api::SmartContractInvocation&);

void checkNewState(const csdb::Address& sc, const csdb::Address& initiator, const api::SmartContractInvocation&, const std::string& newState);
// refresh = false: holders and counters only, the balances are read later (rebuild)
void checkNewState(const csdb::Address& sc, const csdb::Address& initiator, const api::SmartContractInvocation&, const std::string& newState,
bool refresh = true);

bool isToken(const csdb::Address& addr) {
std::lock_guard<std::mutex> l(dataMut_);
return tokens_.count(addr) != 0;
}

bool empty() {
std::lock_guard<std::mutex> l(dataMut_);
return tokens_.empty();
}

void loadTokenInfo(const std::function<void(const TokensMap&, const HoldersMap&)>);

Expand All @@ -87,14 +107,24 @@ class TokensMaster {
std::list<Params> invocations;
};

void updateTokenChaches(const csdb::Address& addr, const std::string& newState, const TokenInvocationData::Params& params);
void updateTokenChaches(const csdb::Address& addr, const std::string& newState, const TokenInvocationData::Params& params, bool refresh = true);

void refreshTokenState(const csdb::Address& token, const std::string& newState, bool checkBalance = false);
// name, symbol and total supply; with checkBalance the balances of `only` (every holder when empty)
void refreshTokenState(const csdb::Address& token, const std::string& newState, bool checkBalance = false,
const std::set<csdb::Address>& only = {});

// every token's name, symbol, supply and balances, one token at a time under the lock
void refreshAll();

static bool isExtended(int64_t tokenStandard) {
return tokenStandard == CreditsExtended || tokenStandard == CreditsExtendedToken;
}

private:
void initiateHolder(Token&, const csdb::Address& token, const csdb::Address& holder, bool increaseTransfers = false);

api::APIHandler* api_;
bool enabled_ = false;

std::mutex cvMut_;

Expand Down
Loading
Loading