Skip to content

Commit

Permalink
mining: convert clock from boost to std::chrono
Browse files Browse the repository at this point in the history
  • Loading branch information
xanimo committed Feb 1, 2025
1 parent e09b375 commit 538a8fa
Show file tree
Hide file tree
Showing 23 changed files with 408 additions and 208 deletions.
2 changes: 2 additions & 0 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "serialize.h"
#include "streams.h"

#include <cmath>

int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash();
Expand Down
2 changes: 1 addition & 1 deletion src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class CAddrMan
{
private:
//! critical section to protect the inner data structures
mutable CCriticalSection cs;
mutable RecursiveMutex cs;

//! last used nId
int nIdCount;
Expand Down
10 changes: 5 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void Interrupt(boost::thread_group& threadGroup)
void Shutdown()
{
LogPrintf("%s: In progress...\n", __func__);
static CCriticalSection cs_Shutdown;
static RecursiveMutex cs_Shutdown;
TRY_LOCK(cs_Shutdown, lockShutdown);
if (!lockShutdown)
return;
Expand Down Expand Up @@ -552,14 +552,14 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
}

static bool fHaveGenesis = false;
static boost::mutex cs_GenesisWait;
static CConditionVariable condvar_GenesisWait;
static Mutex cs_GenesisWait;
static std::condition_variable condvar_GenesisWait;

static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
{
if (pBlockIndex != NULL) {
{
boost::unique_lock<boost::mutex> lock_GenesisWait(cs_GenesisWait);
std::unique_lock<std::mutex> lock_GenesisWait(cs_GenesisWait);
fHaveGenesis = true;
}
condvar_GenesisWait.notify_all();
Expand Down Expand Up @@ -1691,7 +1691,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)

// Wait for genesis block to be processed
{
boost::unique_lock<boost::mutex> lock(cs_GenesisWait);
std::unique_lock<std::mutex> lock(cs_GenesisWait);
while (!fHaveGenesis) {
condvar_GenesisWait.wait(lock);
}
Expand Down
2 changes: 1 addition & 1 deletion src/keystore.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class CKeyStore
{
protected:
mutable CCriticalSection cs_KeyStore;
mutable RecursiveMutex cs_KeyStore;

public:
virtual ~CKeyStore() {}
Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // S
bool fDiscover = true;
bool fListen = true;
bool fRelayTxes = true;
CCriticalSection cs_mapLocalHost;
RecursiveMutex cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {};
std::string strSubVersion;
Expand Down
38 changes: 19 additions & 19 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ class CConnman
static bool NodeFullyConnected(const CNode* pnode);

// Network usage totals
CCriticalSection cs_totalBytesRecv;
CCriticalSection cs_totalBytesSent;
RecursiveMutex cs_totalBytesRecv;
RecursiveMutex cs_totalBytesSent;
uint64_t nTotalBytesRecv = 0;
uint64_t nTotalBytesSent = 0;

Expand All @@ -355,25 +355,25 @@ class CConnman
// Whitelisted ranges. Any node connecting from these is automatically
// whitelisted (as well as those connecting to whitelisted binds).
std::vector<CSubNet> vWhitelistedRange;
CCriticalSection cs_vWhitelistedRange;
RecursiveMutex cs_vWhitelistedRange;

unsigned int nSendBufferMaxSize;
unsigned int nReceiveFloodSize;

std::vector<ListenSocket> vhListenSocket;
std::atomic<bool> fNetworkActive;
banmap_t setBanned;
CCriticalSection cs_setBanned;
RecursiveMutex cs_setBanned;
bool setBannedIsDirty;
bool fAddressesInitialized;
CAddrMan addrman;
std::deque<std::string> vOneShots;
CCriticalSection cs_vOneShots;
RecursiveMutex cs_vOneShots;
std::vector<std::string> vAddedNodes;
CCriticalSection cs_vAddedNodes;
RecursiveMutex cs_vAddedNodes;
std::vector<CNode*> vNodes;
std::list<CNode*> vNodesDisconnected;
mutable CCriticalSection cs_vNodes;
mutable RecursiveMutex cs_vNodes;
std::atomic<NodeId> nLastNodeId;

/** Services this instance offers */
Expand Down Expand Up @@ -483,7 +483,7 @@ struct LocalServiceInfo {
int nPort;
};

extern CCriticalSection cs_mapLocalHost;
extern RecursiveMutex cs_mapLocalHost;
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
typedef std::map<std::string, uint64_t> mapMsgCmdSize; //command, total bytes

Expand Down Expand Up @@ -582,15 +582,15 @@ class CNode
size_t nSendOffset; // offset inside the first vSendMsg already sent
uint64_t nSendBytes;
std::deque<std::vector<unsigned char>> vSendMsg;
CCriticalSection cs_vSend;
CCriticalSection cs_hSocket;
CCriticalSection cs_vRecv;
RecursiveMutex cs_vSend;
RecursiveMutex cs_hSocket;
RecursiveMutex cs_vRecv;

CCriticalSection cs_vProcessMsg;
RecursiveMutex cs_vProcessMsg;
std::list<CNetMessage> vProcessMsg;
size_t nProcessQueueSize;

CCriticalSection cs_sendProcessing;
RecursiveMutex cs_sendProcessing;

std::deque<CInv> vRecvGetData;
uint64_t nRecvBytes;
Expand All @@ -610,7 +610,7 @@ class CNode
// store the sanitized version in cleanSubVer. The original should be used when dealing with
// the network or wire types and the cleaned string used when displayed or logged.
std::string strSubVer, cleanSubVer;
CCriticalSection cs_SubVer; // used for both cleanSubVer and strSubVer
RecursiveMutex cs_SubVer; // used for both cleanSubVer and strSubVer
bool fWhitelisted; // This peer can bypass DoS banning.
bool fFeeler; // If true this node is being used as a short lived feeler.
bool fOneShot;
Expand All @@ -626,7 +626,7 @@ class CNode
bool fRelayTxes; //protected by cs_filter
bool fSentAddr;
CSemaphoreGrant grantOutbound;
CCriticalSection cs_filter;
RecursiveMutex cs_filter;
CBloomFilter* pfilter;
std::atomic<int> nRefCount;
const NodeId id;
Expand Down Expand Up @@ -668,7 +668,7 @@ class CNode
// There is no final sorting before sending, as they are always sent immediately
// and in the order requested.
std::vector<uint256> vInventoryBlockToSend;
CCriticalSection cs_inventory;
RecursiveMutex cs_inventory;
int64_t nNextInvSend;
// Used for headers announcements - unfiltered blocks to relay
// Also protected by cs_inventory
Expand Down Expand Up @@ -696,7 +696,7 @@ class CNode
std::atomic<bool> fPingQueued;
// Minimum fee rate with which to filter inv's to this node
CAmount minFeeFilter;
CCriticalSection cs_feeFilter;
RecursiveMutex cs_feeFilter;
CAmount lastSentFeeFilter;
int64_t nextSendTimeFeeFilter;

Expand All @@ -719,12 +719,12 @@ class CNode
int nSendVersion;
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread

mutable CCriticalSection cs_addrName;
mutable RecursiveMutex cs_addrName;
std::string addrName;

// Our address, as reported by the peer
CService addrLocal;
mutable CCriticalSection cs_addrLocal;
mutable RecursiveMutex cs_addrLocal;
public:

NodeId GetId() const {
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
// g_txrequest.ForgetTxHash(tx.GetHash());
}

static CCriticalSection cs_most_recent_block;
static RecursiveMutex cs_most_recent_block;
static std::shared_ptr<const CBlock> most_recent_block;
static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
static uint256 most_recent_block_hash;
Expand Down
2 changes: 1 addition & 1 deletion src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// Settings
static proxyType proxyInfo[NET_MAX];
static proxyType nameProxy;
static CCriticalSection cs_proxyInfos;
static RecursiveMutex cs_proxyInfos;
uint64_t nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
bool fNameLookup = DEFAULT_NAME_LOOKUP;

Expand Down
22 changes: 16 additions & 6 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "validationinterface.h"
#include "wallet/wallet.h"

#include <chrono>
#include <memory>
#include <stdint.h>

Expand Down Expand Up @@ -311,6 +312,14 @@ std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
return s;
}

static uint64_t getUnixTimeStamp(const std::time_t* t = nullptr)
{
//if specific time is not passed then get current time
std::time_t st = t == nullptr ? std::time(nullptr) : *t;
auto secs = static_cast<std::chrono::seconds>(st).count();
return static_cast<uint64_t>(secs);
}

UniValue getblocktemplate(const JSONRPCRequest& request)
{
// Dogecoin: Never mine witness tx
Expand Down Expand Up @@ -474,7 +483,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
{
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
uint256 hashWatchedChain;
boost::system_time checktxtime;
std::chrono::steady_clock::time_point checktxtime;
unsigned int nTransactionsUpdatedLastLP;

if (lpval.isStr())
Expand All @@ -495,17 +504,18 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
// Release the wallet and main lock while waiting
LEAVE_CRITICAL_SECTION(cs_main);
{
checktxtime = boost::get_system_time() + boost::posix_time::minutes(1);
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);

boost::unique_lock<boost::mutex> lock(csBestBlock);
WAIT_LOCK(csBestBlock, lock);
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
{
if (!cvBlockChange.timed_wait(lock, checktxtime))
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)
{
// Timeout: Check transactions for update
// without holding the mempool lock to avoid deadlocks
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
break;
checktxtime += boost::posix_time::seconds(10);
checktxtime += std::chrono::seconds(10);
}
}
}
Expand Down Expand Up @@ -940,7 +950,7 @@ bool fUseNamecoinApi;
* RPC threads running in parallel.
*/

static CCriticalSection cs_auxblockCache;
static RecursiveMutex cs_auxblockCache;
static std::map<uint256, CBlock*> mapNewBlock;
static std::vector<std::unique_ptr<CBlockTemplate>> vNewBlockTemplate;

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace std;
static std::atomic<bool> g_rpc_running{false};
static bool fRPCInWarmup = true;
static std::string rpcWarmupStatus("RPC server started");
static CCriticalSection cs_rpcWarmup;
static RecursiveMutex cs_rpcWarmup;
/* Timer-creating functions */
static RPCTimerInterface* timerInterface = NULL;
/* Map of name to timer. */
Expand Down
Loading

0 comments on commit 538a8fa

Please sign in to comment.