Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/DeFiCh/ain
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Mar 18, 2022
2 parents be46f8a + 54557c2 commit d0f3a71
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const CLogCategoryDesc LogCategories[] =
{BCLog::SPV, "spv"},
{BCLog::ORACLE, "oracle"},
{BCLog::LOAN, "loan"},
{BCLog::ACCOUNTCHANGE, "accountchange"},
{BCLog::ALL, "1"},
{BCLog::ALL, "all"},
};
Expand Down
88 changes: 61 additions & 27 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#include <fs.h>
#include <tinyformat.h>
#include <util/time.h>

#include <atomic>
#include <cstdint>
#include <list>
#include <mutex>
#include <string>
#include <vector>
#include <unordered_map>

static const bool DEFAULT_LOGTIMEMICROS = false;
static const bool DEFAULT_LOGIPS = false;
Expand All @@ -32,33 +34,34 @@ struct CLogCategoryActive

namespace BCLog {
enum LogFlags : uint32_t {
NONE = 0,
NET = (1 << 0),
TOR = (1 << 1),
MEMPOOL = (1 << 2),
HTTP = (1 << 3),
BENCH = (1 << 4),
ZMQ = (1 << 5),
DB = (1 << 6),
RPC = (1 << 7),
ESTIMATEFEE = (1 << 8),
ADDRMAN = (1 << 9),
SELECTCOINS = (1 << 10),
REINDEX = (1 << 11),
CMPCTBLOCK = (1 << 12),
RAND = (1 << 13),
PRUNE = (1 << 14),
PROXY = (1 << 15),
MEMPOOLREJ = (1 << 16),
LIBEVENT = (1 << 17),
COINDB = (1 << 18),
LEVELDB = (1 << 20),
STAKING = (1 << 21),
ANCHORING = (1 << 22),
SPV = (1 << 23),
ORACLE = (1 << 24),
LOAN = (1 << 25),
ALL = ~(uint32_t)0,
NONE = 0,
NET = (1 << 0),
TOR = (1 << 1),
MEMPOOL = (1 << 2),
HTTP = (1 << 3),
BENCH = (1 << 4),
ZMQ = (1 << 5),
DB = (1 << 6),
RPC = (1 << 7),
ESTIMATEFEE = (1 << 8),
ADDRMAN = (1 << 9),
SELECTCOINS = (1 << 10),
REINDEX = (1 << 11),
CMPCTBLOCK = (1 << 12),
RAND = (1 << 13),
PRUNE = (1 << 14),
PROXY = (1 << 15),
MEMPOOLREJ = (1 << 16),
LIBEVENT = (1 << 17),
COINDB = (1 << 18),
LEVELDB = (1 << 20),
STAKING = (1 << 21),
ANCHORING = (1 << 22),
SPV = (1 << 23),
ORACLE = (1 << 24),
LOAN = (1 << 25),
ACCOUNTCHANGE = (1 << 26),
ALL = ~(uint32_t)0,
};

class Logger
Expand Down Expand Up @@ -167,4 +170,35 @@ static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args
}
}

/** Implementation that logs at most every x milliseconds. If the category is enabled, it does not time throttle */
template <typename... Args>
static inline void LogPrintCategoryOrThreadThrottled(const BCLog::LogFlags& category, std::string message_key, uint64_t milliseconds, const Args&... args)
{
// Map containing pairs of message key and timestamp of last log
// In case different threads use the same message key, use thread_local
thread_local std::unordered_map<std::string, uint64_t> last_log_timestamps;

// Log directly if category is enabled..
if (LogAcceptCategory((category))) {
LogPrintf(args...);
} else { // .. and otherwise time throttle logging

int64_t current_time = GetTimeMillis();
auto it = last_log_timestamps.find(message_key);

if (it != last_log_timestamps.end()) {
if ((current_time - it->second) > milliseconds)
{
LogPrintf(args...);
it->second = current_time;
}
}
else {
// No entry yet -> log directly and save timestamp
last_log_timestamps.insert(std::make_pair(message_key, current_time));
LogPrintf(args...);
}
}
}

#endif // DEFI_LOGGING_H
3 changes: 3 additions & 0 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ void CHistoryErasers::Flush(const uint32_t height, const uint32_t txn, const uin
CHistoryWriters::CHistoryWriters(CAccountHistoryStorage* historyView, CBurnHistoryStorage* burnView, CVaultHistoryStorage* vaultView)
: historyView(historyView), burnView(burnView), vaultView(vaultView) {}

extern std::string ScriptToString(CScript const& script);

void CHistoryWriters::AddBalance(const CScript& owner, const CTokenAmount amount, const uint256& vaultID)
{
if (historyView) {
Expand Down Expand Up @@ -188,6 +190,7 @@ void CHistoryWriters::Flush(const uint32_t height, const uint256& txid, const ui
{
if (historyView) {
for (const auto& diff : diffs) {
LogPrint(BCLog::ACCOUNTCHANGE, "AccountChange: txid=%s addr=%s change=%s\n", txid.GetHex(), ScriptToString(diff.first), (CBalances{diff.second}.ToString()));
historyView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second});
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include <chain.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/tx_check.h>
#include <consensus/consensus.h>
#include <consensus/merkle.h>
#include <consensus/tx_check.h>
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <masternodes/anchors.h>
#include <masternodes/masternodes.h>
#include <masternodes/mn_checks.h>
#include <memory.h>
#include <net.h>
#include <policy/feerate.h>
#include <policy/policy.h>
Expand Down Expand Up @@ -960,10 +961,10 @@ void ThreadStaker::operator()(std::vector<ThreadStaker::Args> args, CChainParams
nMinted[arg.operatorID]++;
}
else if (status == Staker::Status::initWaiting) {
LogPrintf("ThreadStaker: (%s) waiting init...\n", operatorName);
LogPrintCategoryOrThreadThrottled(BCLog::STAKING, "init_waiting", 1000 * 60 * 10, "ThreadStaker: (%s) waiting init...\n", operatorName);
}
else if (status == Staker::Status::stakeWaiting) {
LogPrint(BCLog::STAKING, "ThreadStaker: (%s) Staked, but no kernel found yet.\n", operatorName);
LogPrintCategoryOrThreadThrottled(BCLog::STAKING, "no_kernel_found", 1000 * 60 * 10,"ThreadStaker: (%s) Staked, but no kernel found yet.\n", operatorName);
}
}
catch (const std::runtime_error &e) {
Expand Down
2 changes: 1 addition & 1 deletion src/pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ boost::optional<std::string> CheckSignedBlock(const std::shared_ptr<CBlock>& pbl
if (!CheckProofOfStake(*(CBlockHeader*)pblock.get(), pindexPrev, chainparams.GetConsensus(), pcustomcsview.get()))
return {std::string{} + "proof-of-stake checking failed"};

LogPrint(BCLog::STAKING, "new proof-of-stake block found hash: %s\n", hashBlock.GetHex());
LogPrintf("new proof-of-stake block found hash: %s\n", hashBlock.GetHex());

// Found a solution
if (pblock->hashPrevBlock != pindexPrev->GetBlockHash())
Expand Down

0 comments on commit d0f3a71

Please sign in to comment.