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.cpp98
1 files changed, 93 insertions, 5 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 7b3e555..be4ab5e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -14,6 +14,7 @@
14#include <atomic> 14#include <atomic>
15#include <mutex> 15#include <mutex>
16#include <thread> 16#include <thread>
17#include <system_error>
17#include "bip32.h" 18#include "bip32.h"
18#include "btfaddr.h" 19#include "btfaddr.h"
19#include "btftunnel.h" 20#include "btftunnel.h"
@@ -3899,9 +3900,32 @@ connected:
3899 std::vector<ParticipantShareHit> shareHits; 3900 std::vector<ParticipantShareHit> shareHits;
3900 std::mutex shareHitsMtx; 3901 std::mutex shareHitsMtx;
3901 std::vector<std::thread> hashThreads; 3902 std::vector<std::thread> hashThreads;
3902 for (int i = 0; i < nWorkers; i++) 3903 try
3903 hashThreads.emplace_back(ParticipantHashWorker, &jobShared, i, nWorkers, 3904 {
3904 &shareHits, &shareHitsMtx); 3905 for (int i = 0; i < nWorkers; i++)
3906 hashThreads.emplace_back(ParticipantHashWorker, &jobShared, i, nWorkers,
3907 &shareHits, &shareHitsMtx);
3908 }
3909 catch (const std::system_error& e)
3910 {
3911 // A refused pthread_create used to reach std::terminate here and
3912 // kill the worker mid-session -- observed twice on an otherwise
3913 // healthy participant. Stop the workers that did start the same way
3914 // the disconnect path below does, drop this pool connection, and let
3915 // the caller's reconnect loop try again once the pressure clears.
3916 LogPrint("worker", "[worker] cannot spawn hash threads (%s)"
3917 " -- dropping pool connection\n", e.what());
3918 SetParticipantMiningStatus("hash thread spawn failed");
3919 {
3920 std::lock_guard<std::mutex> lk(jobShared.mtx);
3921 jobShared.stop = true;
3922 jobShared.haveJob = false;
3923 }
3924 for (std::thread& t : hashThreads)
3925 t.join();
3926 closesocket(s);
3927 return false;
3928 }
3905 LogPrint("worker", "[worker] spawned %d hash threads on shared RandomX dataset\n", 3929 LogPrint("worker", "[worker] spawned %d hash threads on shared RandomX dataset\n",
3906 nWorkers); 3930 nWorkers);
3907 const uint64 sessionStartHashes = gParticipantHashes.load(); 3931 const uint64 sessionStartHashes = gParticipantHashes.load();
@@ -3912,6 +3936,14 @@ connected:
3912 std::string jobId; 3936 std::string jobId;
3913 unsigned char header[80] = {}; 3937 unsigned char header[80] = {};
3914 uint256 target; 3938 uint256 target;
3939 // Cached answer to "which block is this job built on", for the
3940 // mining.notify handler below -- see the comment there. Session-local, so
3941 // a reconnect re-resolves rather than trusting an answer obtained before
3942 // a possible change of pool.
3943 uint256 hashPrevResolved; // prevhash the cached answer describes
3944 bool fPrevResolved = false; // whether that answer exists yet
3945 int nPrevHeight = -1; // its height, or -1 == unknown to us
3946 int nBestHeightResolved = -1; // nBestHeight when we last resolved
3915 double currentDifficulty = 1.0; // updated by mining.set_difficulty from pool 3947 double currentDifficulty = 1.0; // updated by mining.set_difficulty from pool
3916 bool haveJob = false; 3948 bool haveJob = false;
3917 bool gotSubscribeAck = false; 3949 bool gotSubscribeAck = false;
@@ -4040,8 +4072,64 @@ connected:
4040 jobShared.jobSeq++; 4072 jobShared.jobSeq++;
4041 jobShared.haveJob = true; 4073 jobShared.haveJob = true;
4042 } 4074 }
4043 LogPrint("worker", "[pool->worker] new job %s received\n", 4075 // Log the chain position the pool is
4044 jobId.c_str()); 4076 // building on, so a stale or private
4077 // fork is auditable from worker logs.
4078 // The prevhash comes from the header we
4079 // actually hash (== p[2] of the notify);
4080 // a brief cs_main lookup is safe here --
4081 // this thread holds no other lock.
4082 //
4083 // Resolving it is the expensive half, and
4084 // this is the socket-reading thread: block
4085 // validation can hold cs_main for a long
4086 // time, and every moment spent waiting on
4087 // it here is a moment set_difficulty and
4088 // share responses are not being handled.
4089 // Most notifies in a round are a new job
4090 // on the SAME tip, so the answer is cached
4091 // and re-resolved only when it could come
4092 // out differently: a prevhash we have not
4093 // resolved, or one we could NOT resolve
4094 // last time while the chain has since
4095 // moved (unknown -> known). A block's
4096 // height is fixed once mapBlockIndex knows
4097 // it, so a cached "known" never goes
4098 // stale. nBestHeight is read unlocked on
4099 // purpose -- it only decides whether to
4100 // repeat a lookup, never what gets
4101 // printed, so a stale read costs at most
4102 // one deferred or one extra lookup.
4103 // The line still prints in full for every
4104 // job, cached or not.
4105 uint256 hashPrev;
4106 memcpy(&hashPrev, header + 4, 32);
4107 if (LogAcceptsCategory("worker"))
4108 {
4109 int nBestHeightNow = nBestHeight;
4110 if (!fPrevResolved
4111 || hashPrev != hashPrevResolved
4112 || (nPrevHeight < 0 && nBestHeightNow != nBestHeightResolved))
4113 {
4114 nPrevHeight = -1;
4115 CRITICAL_BLOCK(cs_main)
4116 {
4117 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrev);
4118 if (mi != mapBlockIndex.end())
4119 nPrevHeight = (*mi).second->nHeight;
4120 }
4121 hashPrevResolved = hashPrev;
4122 nBestHeightResolved = nBestHeightNow;
4123 fPrevResolved = true;
4124 }
4125 if (nPrevHeight >= 0)
4126 LogPrint("worker", "[pool->worker] new job %s received, prevhash %s (known, job height %d)\n",
4127 jobId.c_str(), hashPrev.GetHex().c_str(),
4128 nPrevHeight + 1);
4129 else
4130 LogPrint("worker", "[pool->worker] new job %s received, prevhash %s (unknown to our chain)\n",
4131 jobId.c_str(), hashPrev.GetHex().c_str());
4132 }
4045 SetParticipantMiningStatus("hashing job " + jobId); 4133 SetParticipantMiningStatus("hashing job " + jobId);
4046 } 4134 }
4047 } 4135 }