Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #15334, #13076, #15266, #13128 #4420

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/batchedlogger.cpp
Expand Up @@ -20,6 +20,6 @@ void CBatchedLogger::Flush()
if (!accept || msg.empty()) {
return;
}
g_logger->LogPrintStr(strprintf("%s:\n%s", header, msg));
LogInstance().LogPrintStr(strprintf("%s:\n%s", header, msg));
msg.clear();
}
4 changes: 2 additions & 2 deletions src/httpserver.cpp
Expand Up @@ -373,8 +373,8 @@ bool InitHTTPServer()
// Update libevent's log handling. Returns false if our version of
// libevent doesn't support debug logging, in which case we should
// clear the BCLog::LIBEVENT flag.
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
g_logger->DisableCategory(BCLog::LIBEVENT);
if (!UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT))) {
LogInstance().DisableCategory(BCLog::LIBEVENT);
}

#ifdef WIN32
Expand Down
30 changes: 15 additions & 15 deletions src/init.cpp
Expand Up @@ -416,7 +416,7 @@ static void HandleSIGTERM(int)

static void HandleSIGHUP(int)
{
g_logger->m_reopen_file = true;
LogInstance().m_reopen_file = true;
}
#else
static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType)
Expand Down Expand Up @@ -1185,12 +1185,12 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
*/
void InitLogging()
{
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
g_logger->m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
g_logger->m_log_threadnames = gArgs.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
LogInstance().m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
LogInstance().m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
LogInstance().m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
LogInstance().m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
LogInstance().m_log_threadnames = gArgs.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);

fLogIPs = gArgs.GetBoolArg("-logips", DEFAULT_LOGIPS);

Expand Down Expand Up @@ -1366,7 +1366,7 @@ bool AppInitParameterInteraction()
if (std::none_of(categories.begin(), categories.end(),
[](std::string cat){return cat == "0" || cat == "none";})) {
for (const auto& cat : categories) {
if (!g_logger->EnableCategory(cat)) {
if (!LogInstance().EnableCategory(cat)) {
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
}
}
Expand All @@ -1375,7 +1375,7 @@ bool AppInitParameterInteraction()

// Now remove the logging categories which were explicitly excluded
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
if (!g_logger->DisableCategory(cat)) {
if (!LogInstance().DisableCategory(cat)) {
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
}
}
Expand Down Expand Up @@ -1619,19 +1619,19 @@ bool AppInitMain()
return false;
}
#endif
if (g_logger->m_print_to_file) {
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {
if (LogInstance().m_print_to_file) {
if (gArgs.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
// Do this first since it both loads a bunch of debug.log into memory,
// and because this needs to happen before any other debug.log printing
g_logger->ShrinkDebugFile();
LogInstance().ShrinkDebugFile();
}
}
if (!g_logger->StartLogging()) {
if (!LogInstance().StartLogging()) {
return InitError(strprintf("Could not open debug log file %s",
g_logger->m_file_path.string()));
LogInstance().m_file_path.string()));
}
vijaydasmp marked this conversation as resolved.
Show resolved Hide resolved

if (!g_logger->m_log_timestamps)
if (!LogInstance().m_log_timestamps)
LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
LogPrintf("Using data directory %s\n", GetDataDir().string());
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/node.cpp
Expand Up @@ -175,7 +175,7 @@ class NodeImpl : public Node
void initLogging() override { InitLogging(); }
void initParameterInteraction() override { InitParameterInteraction(); }
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
uint64_t getLogCategories() override { return g_logger->GetCategoryMask(); }
uint64_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
bool baseInitialize() override
{
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_commitment.cpp
Expand Up @@ -24,7 +24,7 @@ CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const ui
}

#define LogPrintfFinalCommitment(...) do { \
g_logger->LogPrintStr(strprintf("CFinalCommitment::%s -- %s", __func__, tinyformat::format(__VA_ARGS__))); \
LogInstance().LogPrintStr(strprintf("CFinalCommitment::%s -- %s", __func__, tinyformat::format(__VA_ARGS__))); \
} while(0)

bool CFinalCommitment::Verify(const CBlockIndex* pQuorumIndex, bool checkSigs) const
Expand Down
12 changes: 9 additions & 3 deletions src/logging.cpp
Expand Up @@ -12,6 +12,8 @@

const char * const DEFAULT_DEBUGLOGFILE = "debug.log";

BCLog::Logger& LogInstance()
{
/**
* NOTE: the logger instances is leaked on exit. This is ugly, but will be
* cleaned up by the OS/libc. Defining a logger as a global object doesn't work
Expand All @@ -21,11 +23,15 @@ const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
* access the logger. When the shutdown sequence is fully audited and tested,
* explicit destruction of these objects can be implemented by changing this
* from a raw pointer to a std::unique_ptr.
* Since the destructor is never called, the logger and all its members must
* have a trivial destructor.
*
* This method of initialization was originally introduced in
* ee3374234c60aba2cc4c5cd5cac1c0aefc2d817c.
*/
BCLog::Logger* const g_logger = new BCLog::Logger();
static BCLog::Logger* g_logger{new BCLog::Logger()};
return *g_logger;
}

bool fLogIPs = DEFAULT_LOGIPS;

Expand Down Expand Up @@ -204,9 +210,9 @@ std::vector<CLogCategoryActive> ListActiveLogCategories()

std::string ListActiveLogCategoriesString()
{
if (g_logger->GetCategoryMask() == BCLog::NONE)
if (LogInstance().GetCategoryMask() == BCLog::NONE)
return "0";
if (g_logger->GetCategoryMask() == BCLog::ALL)
if (LogInstance().GetCategoryMask() == BCLog::ALL)
return "1";

std::string ret;
Expand Down
8 changes: 4 additions & 4 deletions src/logging.h
Expand Up @@ -142,12 +142,12 @@ namespace BCLog {

} // namespace BCLog

extern BCLog::Logger* const g_logger;
BCLog::Logger& LogInstance();

/** Return true if log accepts specified category */
static inline bool LogAcceptCategory(BCLog::LogFlags category)
{
return g_logger->WillLogCategory(category);
return LogInstance().WillLogCategory(category);
}

/** Returns a string with the log categories. */
Expand Down Expand Up @@ -182,15 +182,15 @@ std::string SafeStringFormat(const std::string& fmt, const Args&... args)
template <typename... Args>
static inline void LogPrintf(const char* fmt, const Args&... args)
{
if (g_logger->Enabled()) {
if (LogInstance().Enabled()) {
std::string log_msg;
try {
log_msg = tfm::format(fmt, args...);
} catch (tinyformat::format_error& fmterr) {
/* Original format string will have newline so don't add one here */
log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
}
g_logger->LogPrintStr(log_msg);
LogInstance().LogPrintStr(log_msg);
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/policy/fees.cpp
Expand Up @@ -511,7 +511,7 @@ void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHe
// of no harm to try to remove them again.
bool CBlockPolicyEstimator::removeTx(uint256 hash, bool inBlock)
{
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find(hash);
if (pos != mapMemPoolTxs.end()) {
feeStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
Expand Down Expand Up @@ -548,7 +548,7 @@ CBlockPolicyEstimator::~CBlockPolicyEstimator()

void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate)
{
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
unsigned int txHeight = entry.GetHeight();
uint256 hash = entry.GetTx().GetHash();
if (mapMemPoolTxs.count(hash)) {
Expand Down Expand Up @@ -614,7 +614,7 @@ bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxM
void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
std::vector<const CTxMemPoolEntry*>& entries)
{
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
if (nBlockHeight <= nBestSeenHeight) {
// Ignore side chains and re-orgs; assuming they are random
// they don't affect the estimate.
Expand Down Expand Up @@ -692,7 +692,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr
}
}

LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
// Return failure if trying to analyze a target we're not tracking
if (confTarget <= 0 || (unsigned int)confTarget > stats->GetMaxConfirms())
return CFeeRate(0);
Expand All @@ -709,6 +709,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr

unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon horizon) const
{
LOCK(m_cs_fee_estimator);
switch (horizon) {
case FeeEstimateHorizon::SHORT_HALFLIFE: {
return shortStats->GetMaxConfirms();
Expand Down Expand Up @@ -818,7 +819,7 @@ double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget,
*/
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
{
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);

if (feeCalc) {
feeCalc->desiredTarget = confTarget;
Expand Down Expand Up @@ -898,7 +899,7 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
{
try {
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
fileout << 140100; // version required to read: 0.14.1 or later
fileout << CLIENT_VERSION; // version that wrote the file
fileout << nBestSeenHeight;
Expand All @@ -923,7 +924,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
bool CBlockPolicyEstimator::Read(CAutoFile& filein)
{
try {
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
int nVersionRequired, nVersionThatWrote;
unsigned int nFileBestSeenHeight;
filein >> nVersionRequired >> nVersionThatWrote;
Expand Down Expand Up @@ -982,7 +983,7 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)

void CBlockPolicyEstimator::FlushUnconfirmed() {
int64_t startclear = GetTimeMicros();
LOCK(cs_feeEstimator);
LOCK(m_cs_fee_estimator);
size_t num_entries = mapMemPoolTxs.size();
// Remove every entry in mapMemPoolTxs
while (!mapMemPoolTxs.empty()) {
Expand Down
40 changes: 20 additions & 20 deletions src/policy/fees.h
Expand Up @@ -228,10 +228,12 @@ class CBlockPolicyEstimator
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;

private:
unsigned int nBestSeenHeight;
unsigned int firstRecordedHeight;
unsigned int historicalFirst;
unsigned int historicalBest;
mutable CCriticalSection m_cs_fee_estimator;

unsigned int nBestSeenHeight GUARDED_BY(m_cs_fee_estimator);
unsigned int firstRecordedHeight GUARDED_BY(m_cs_fee_estimator);
unsigned int historicalFirst GUARDED_BY(m_cs_fee_estimator);
unsigned int historicalBest GUARDED_BY(m_cs_fee_estimator);

struct TxStatsInfo
{
Expand All @@ -241,34 +243,32 @@ class CBlockPolicyEstimator
};

// map of txids to information about that transaction
std::map<uint256, TxStatsInfo> mapMemPoolTxs;
std::map<uint256, TxStatsInfo> mapMemPoolTxs GUARDED_BY(m_cs_fee_estimator);

/** Classes to track historical data on transaction confirmations */
std::unique_ptr<TxConfirmStats> feeStats;
std::unique_ptr<TxConfirmStats> shortStats;
std::unique_ptr<TxConfirmStats> longStats;

unsigned int trackedTxs;
unsigned int untrackedTxs;
std::unique_ptr<TxConfirmStats> feeStats PT_GUARDED_BY(m_cs_fee_estimator);
std::unique_ptr<TxConfirmStats> shortStats PT_GUARDED_BY(m_cs_fee_estimator);
std::unique_ptr<TxConfirmStats> longStats PT_GUARDED_BY(m_cs_fee_estimator);

std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
unsigned int trackedTxs GUARDED_BY(m_cs_fee_estimator);
unsigned int untrackedTxs GUARDED_BY(m_cs_fee_estimator);

mutable CCriticalSection cs_feeEstimator;
std::vector<double> buckets GUARDED_BY(m_cs_fee_estimator); // The upper-bound of the range for the bucket (inclusive)
std::map<double, unsigned int> bucketMap GUARDED_BY(m_cs_fee_estimator); // Map of bucket upper-bound to index into all vectors by bucket

/** Process a transaction confirmed in a block*/
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry) EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);

/** Helper for estimateSmartFee */
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
/** Helper for estimateSmartFee */
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
/** Number of blocks of data recorded while fee estimates have been running */
unsigned int BlockSpan() const;
unsigned int BlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
/** Number of blocks of recorded fee estimate data represented in saved data file */
unsigned int HistoricalBlockSpan() const;
unsigned int HistoricalBlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
/** Calculation of highest target that reasonable estimate can be provided for */
unsigned int MaxUsableEstimate() const;
unsigned int MaxUsableEstimate() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
};

#endif // BITCOIN_POLICY_FEES_H
8 changes: 7 additions & 1 deletion src/qt/test/wallettests.cpp
Expand Up @@ -118,7 +118,13 @@ void TestGUI()
LOCK(cs_main);
WalletRescanReserver reserver(wallet.get());
reserver.reserve();
wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, true);
const CBlockIndex* const null_block = nullptr;
const CBlockIndex *stop_block, *failed_block;
QCOMPARE(
wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, failed_block, stop_block, true /* fUpdate */),
CWallet::ScanResult::SUCCESS);
QCOMPARE(stop_block, chainActive.Tip());
QCOMPARE(failed_block, null_block);
}
wallet->SetBroadcastTransactions(true);

Expand Down
16 changes: 8 additions & 8 deletions src/rpc/misc.cpp
Expand Up @@ -59,14 +59,14 @@ static UniValue debug(const JSONRPCRequest& request)
);

std::string strMode = request.params[0].get_str();
g_logger->DisableCategory(BCLog::ALL);
LogInstance().DisableCategory(BCLog::ALL);

std::vector<std::string> categories;
boost::split(categories, strMode, boost::is_any_of("+"));

if (std::find(categories.begin(), categories.end(), std::string("0")) == categories.end()) {
for (const auto& cat : categories) {
g_logger->EnableCategory(cat);
LogInstance().EnableCategory(cat);
}
}

Expand Down Expand Up @@ -1023,9 +1023,9 @@ static void EnableOrDisableLogCategories(UniValue cats, bool enable) {

bool success;
if (enable) {
success = g_logger->EnableCategory(cat);
success = LogInstance().EnableCategory(cat);
} else {
success = g_logger->DisableCategory(cat);
success = LogInstance().DisableCategory(cat);
}

if (!success) {
Expand Down Expand Up @@ -1072,14 +1072,14 @@ static UniValue logging(const JSONRPCRequest& request)
);
}

uint64_t original_log_categories = g_logger->GetCategoryMask();
uint64_t original_log_categories = LogInstance().GetCategoryMask();
if (request.params[0].isArray()) {
EnableOrDisableLogCategories(request.params[0], true);
}
if (request.params[1].isArray()) {
EnableOrDisableLogCategories(request.params[1], false);
}
uint64_t updated_log_categories = g_logger->GetCategoryMask();
uint64_t updated_log_categories = LogInstance().GetCategoryMask();
uint64_t changed_log_categories = original_log_categories ^ updated_log_categories;

// Update libevent logging if BCLog::LIBEVENT has changed.
Expand All @@ -1088,8 +1088,8 @@ static UniValue logging(const JSONRPCRequest& request)
// Throw an error if the user has explicitly asked to change only the libevent
// flag and it failed.
if (changed_log_categories & BCLog::LIBEVENT) {
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
g_logger->DisableCategory(BCLog::LIBEVENT);
if (!UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT))) {
LogInstance().DisableCategory(BCLog::LIBEVENT);
if (changed_log_categories == BCLog::LIBEVENT) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "libevent logging cannot be updated when using libevent before v2.1.1.");
}
Expand Down