upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp51
1 files changed, 45 insertions, 6 deletions
diff --git a/src/main.cpp b/src/main.cpp
index c882b0b..d4fc6d0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -72,6 +72,13 @@ bool fSoloMineTest = false; // /solomine: mine without requiring a peer (local t
72 72
73// Settings 73// Settings
74int fGenerateBitcoins; 74int fGenerateBitcoins;
75// How many threads to hash with. 0 means "decide from the hardware" -- see
76// MinerThreadCount(). This was effectively 1 forever, because Bitcoin 0.1.0
77// started exactly one miner thread and nothing here ever changed that. It made
78// sense for SHA-256d in 2009; RandomX is built to be fed by every core at once,
79// so a 32-core machine was mining at a thirty-second of its capacity and the
80// only visible symptom was a suspiciously idle CPU.
81int nMinerThreads = 0;
75int64 nTransactionFee = 0; 82int64 nTransactionFee = 0;
76CAddress addrIncoming; 83CAddress addrIncoming;
77int nMineMode = MINE_RELAY; 84int nMineMode = MINE_RELAY;
@@ -119,6 +126,26 @@ void GetParticipantMiningStats(uint64& sharesSent, uint64& sharesAccepted, doubl
119// mapKeys 126// mapKeys
120// 127//
121 128
129// Threads to hash with, resolving the automatic case.
130//
131// Automatic leaves one core to the rest of the node -- accepting blocks,
132// talking to peers, serving the GUI -- because a machine that mines perfectly
133// while falling behind the chain has won nothing. Anyone who disagrees can say
134// so with /genproclimit and get exactly what they ask for.
135//
136// Memory is not the reason to hold back: the ~2 GB RandomX dataset is shared by
137// every thread, and each one adds only its own 2 MB scratchpad. Sixteen threads
138// cost about 32 MB more than one.
139int MinerThreadCount()
140{
141 if (nMinerThreads > 0)
142 return nMinerThreads;
143 unsigned int nCores = std::thread::hardware_concurrency();
144 if (nCores <= 1)
145 return 1;
146 return (int)(nCores - 1);
147}
148
122bool AddKey(const CKey& key) 149bool AddKey(const CKey& key)
123{ 150{
124 CRITICAL_BLOCK(cs_mapKeys) 151 CRITICAL_BLOCK(cs_mapKeys)
@@ -2769,7 +2796,7 @@ static bool PoolParticipantMiner()
2769 return true; 2796 return true;
2770} 2797}
2771 2798
2772bool BitcoinMiner() 2799bool BitcoinMiner(int nThreadId)
2773{ 2800{
2774 // Relay mode: never mines, just relays/syncs. (Shouldn't normally get here 2801 // Relay mode: never mines, just relays/syncs. (Shouldn't normally get here
2775 // since the GUI hides Start Mining for Relay, but guard it regardless.) 2802 // since the GUI hides Start Mining for Relay, but guard it regardless.)
@@ -2809,11 +2836,17 @@ bool BitcoinMiner()
2809 while (fGenerateBitcoins) 2836 while (fGenerateBitcoins)
2810 { 2837 {
2811 Sleep(50); 2838 Sleep(50);
2812 CheckForShutdown(3); 2839 // Return instead of _endthread()ing. With more than one miner thread the
2840 // bookkeeping in ThreadBitcoinMiner has to run on the way out, and
2841 // _endthread() does not unwind on Windows, so a normal return is the
2842 // only exit that works the same on both platforms.
2843 if (fShutdown)
2844 return true;
2813 while (vNodes.empty() && !fSoloMineTest) 2845 while (vNodes.empty() && !fSoloMineTest)
2814 { 2846 {
2815 Sleep(1000); 2847 Sleep(1000);
2816 CheckForShutdown(3); 2848 if (fShutdown)
2849 return true;
2817 } 2850 }
2818 2851
2819 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; 2852 unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
@@ -2908,8 +2941,11 @@ bool BitcoinMiner()
2908 Sleep(1000); 2941 Sleep(1000);
2909 continue; 2942 continue;
2910 } 2943 }
2911 LogPrint("net", "BitcoinMiner: mining with RandomX (%s)\n", 2944 // The thread number is in the line on purpose: every miner would
2912 RandomXFastReady() ? "fast 2GB" : "light 256MB"); 2945 // otherwise print the same text, and the dedup filter would fold them
2946 // into one, leaving no way to tell four threads from one.
2947 LogPrint("net", "BitcoinMiner: thread %d hashing with RandomX (%s)\n",
2948 nThreadId, RandomXFastReady() ? "fast 2GB" : "light 256MB");
2913 2949
2914 unsigned int nStart = GetTime(); 2950 unsigned int nStart = GetTime();
2915 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); 2951 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
@@ -2950,7 +2986,8 @@ bool BitcoinMiner()
2950 // ~256 hashes is already plenty of time) 2986 // ~256 hashes is already plenty of time)
2951 if ((++pblock->nNonce & 0xff) == 0) 2987 if ((++pblock->nNonce & 0xff) == 0)
2952 { 2988 {
2953 CheckForShutdown(3); 2989 if (fShutdown)
2990 break; // falls through to RandomXDestroyMinerVM below
2954 if (pblock->nNonce == 0) 2991 if (pblock->nNonce == 0)
2955 break; 2992 break;
2956 if (pindexPrev != pindexBest) 2993 if (pindexPrev != pindexBest)
@@ -2963,6 +3000,8 @@ bool BitcoinMiner()
2963 } 3000 }
2964 } 3001 }
2965 RandomXDestroyMinerVM(rxvm); 3002 RandomXDestroyMinerVM(rxvm);
3003 if (fShutdown)
3004 break;
2966 } 3005 }
2967 3006
2968 return true; 3007 return true;