Skip to content

Commit

Permalink
Merge pull request #439 from zhizhubeiwang/dev202005041753-grandpool-…
Browse files Browse the repository at this point in the history
…localshare

add grand pool support
  • Loading branch information
YihaoPeng authored May 6, 2020
2 parents 34e16d8 + f891aa4 commit 55eff22
Show file tree
Hide file tree
Showing 54 changed files with 1,050 additions and 106 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ install/run_*/
/src/config/*.h
/src/*.pro.pb.h
/src/*.pro.pb.cc

cmake-build-debug
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ else()
message(" WARNING: Bytom share checking will be too slow without Nvidia CUDA.")
endif()

option(USE_LOCAL_SHARE_BITCOIN "Use LocalShareBitcoin in build" OFF)
if(USE_LOCAL_SHARE_BITCOIN)
add_definitions(-DLOCAL_SHARE_NO_GRAND_FIELD)
message("-- Use LocalShareBitcoin in build: Enabled (-DUSE_LOCAL_SHARE_BITCOIN=ON)")
else()
message("-- Use LocalShareBitcoinGrand in build: Enabled (-DUSE_LOCAL_SHARE_BITCOIN=OFF)")
endif()

#
# Chain Type
#
Expand Down
56 changes: 11 additions & 45 deletions src/Stratum.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,61 +209,27 @@ class StratumJob {
virtual uint64_t height() const = 0;
};

// shares submitted by this session, for duplicate share check
// TODO: Move bitcoin-specific fields to the subclass
struct LocalShare {
uint64_t exNonce2_; // extra nonce2 fixed 8 bytes
uint32_t nonce_; // nonce in block header
uint32_t time_; // nTime in block header
uint32_t versionMask_; // block version mask

LocalShare(
uint64_t exNonce2, uint32_t nonce, uint32_t time, uint32_t versionMask)
: exNonce2_(exNonce2)
, nonce_(nonce)
, time_(time)
, versionMask_(versionMask) {}

LocalShare(uint64_t exNonce2, uint32_t nonce, uint32_t time)
: exNonce2_(exNonce2)
, nonce_(nonce)
, time_(time)
, versionMask_(0) {}

LocalShare &operator=(const LocalShare &other) {
exNonce2_ = other.exNonce2_;
nonce_ = other.nonce_;
time_ = other.time_;
versionMask_ = other.versionMask_;
return *this;
}

bool operator<(const LocalShare &r) const {
if (exNonce2_ < r.exNonce2_ ||
(exNonce2_ == r.exNonce2_ && nonce_ < r.nonce_) ||
(exNonce2_ == r.exNonce2_ && nonce_ == r.nonce_ && time_ < r.time_) ||
(exNonce2_ == r.exNonce2_ && nonce_ == r.nonce_ && time_ == r.time_ &&
versionMask_ < r.versionMask_)) {
return true;
}
return false;
}
};

struct LocalJob {
size_t chainId_;
uint64_t jobId_;
std::set<LocalShare> submitShares_;

LocalJob(size_t chainId, uint64_t jobId)
: chainId_(chainId)
, jobId_(jobId) {}

bool addLocalShare(const LocalShare &localShare) {
bool operator==(uint64_t jobId) const { return jobId_ == jobId; }
};

template <typename LocalShareType>
struct LocalJobBase : public LocalJob {
std::set<LocalShareType> submitShares_;

LocalJobBase(size_t chainId, uint64_t jobId)
: LocalJob(chainId,jobId) {}

bool addLocalShare(const LocalShareType &localShare) {
return submitShares_.insert(localShare).second;
}

bool operator==(uint64_t jobId) const { return jobId_ == jobId; }
};

namespace sharebase {
Expand Down
5 changes: 5 additions & 0 deletions src/StratumMiner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ StratumMiner::StratumMiner(
, curDiff_(0)
, clientAgent_(clientAgent)
, isNiceHashClient_(isNiceHashAgent(clientAgent))
, isGrandPoolClient_(false)
, overrideDifficulty_(false)
, workerName_(workerName)
, workerId_(workerId)
Expand All @@ -59,6 +60,10 @@ void StratumMiner::resetCurDiff(uint64_t curDiff) {
diffController_->resetCurDiff(curDiff);
}

void StratumMiner::setGrandPoolClient(bool isGrandPoolClient) {
isGrandPoolClient_ = isGrandPoolClient;
}

uint64_t StratumMiner::calcCurDiff() {
if (!overrideDifficulty_ &&
(session_.niceHashForced() || isNiceHashClient_)) {
Expand Down
6 changes: 6 additions & 0 deletions src/StratumMiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class StratumMiner {

public:
static const size_t kExtraNonce1Size_ = 4;
static const size_t kExtraGrandNonce1Size_ = 4;
static const size_t kExtraNonce2Size_ = 8;

virtual ~StratumMiner() = default;
Expand All @@ -62,6 +63,7 @@ class StratumMiner {
const std::string &exMessage){}; // No agent support by default
void setMinDiff(uint64_t minDiff);
void resetCurDiff(uint64_t curDiff);
void setGrandPoolClient(bool isGrandPoolClient);
uint64_t getCurDiff() const { return curDiff_; };
uint64_t calcCurDiff();
virtual uint64_t addLocalJob(LocalJob &localJob) = 0;
Expand All @@ -83,6 +85,7 @@ class StratumMiner {
uint64_t curDiff_;
std::string clientAgent_;
bool isNiceHashClient_;
bool isGrandPoolClient_ = false;
bool overrideDifficulty_;
std::string workerName_;
int64_t workerId_;
Expand All @@ -96,6 +99,9 @@ class StratumMinerBase : public StratumMiner {
using SessionType = typename StratumTraits::SessionType;
using JobDiffType = typename StratumTraits::JobDiffType;

public:
using LocalShareType = typename StratumTraits::LocalShareType;

protected:
StratumMinerBase(
SessionType &session,
Expand Down
24 changes: 23 additions & 1 deletion src/StratumServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ bool StratumServer::setup(const libconfig::Config &config) {
<< ". This option should not be enabled in a production environment!";
}

//grandPool 4+4+8
config.lookupValue("sserver.grandPoolEnabled", grandPoolEnabled_);
#ifdef LOCAL_SHARE_NO_GRAND_FIELD
grandPoolEnabled_ = false;
#endif

// ------------------- Diff Controller Options -------------------

string defDiffStr = config.lookup("sserver.default_difficulty");
Expand Down Expand Up @@ -1109,6 +1115,20 @@ void StratumServer::sendMiningNotifyToAll(shared_ptr<StratumJobEx> exJobPtr) {
// of course, for iterators that actually point to the element that is
// being erased.
//

if(grandPoolEnabled_ && exJobPtr->isClean_){
auto itr = connections_.begin();
while (itr != connections_.end()) {
auto &conn = *itr;
if (conn->isGrandPoolClient() && (!conn->isDead()) ) {
if (conn->getChainId() == exJobPtr->chainId_) {
conn->sendMiningNotify(exJobPtr);
}
}
++itr;
}
}

auto itr = connections_.begin();
while (itr != connections_.end()) {
auto &conn = *itr;
Expand All @@ -1117,7 +1137,9 @@ void StratumServer::sendMiningNotifyToAll(shared_ptr<StratumJobEx> exJobPtr) {
sessionIDManager_->freeSessionId(conn->getSessionId());
#endif
itr = connections_.erase(itr);
} else {
}else if(grandPoolEnabled_ && exJobPtr->isClean_ && conn->isGrandPoolClient() ){
++itr;
}else {
if (conn->getChainId() == exJobPtr->chainId_) {
conn->sendMiningNotify(exJobPtr);
}
Expand Down
2 changes: 2 additions & 0 deletions src/StratumServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ class StratumServer {
// WARNING: difficulty to send to miners.
float devFixedDifficulty_;

bool grandPoolEnabled_;

#ifndef WORK_WITH_STRATUM_SWITCHER
SessionIDManager *sessionIDManager_;
#endif
Expand Down
6 changes: 5 additions & 1 deletion src/StratumSession.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static const uint32_t ReadTimeout = 15;
static const uint32_t WriteTimeout = 120;
static const string PoolWatcherAgent = "__PoolWatcher__";
static const string BtccomAgentPrefix = "btccom-agent/";
static const string PoolGrandPoolWatcher = "__PoolGrandPoolWatcher__";

class ProxyStrategyDecodeAddress : public ProxyStrategy {
public:
Expand Down Expand Up @@ -545,8 +546,11 @@ void StratumSession::setClientAgent(const string &clientAgent) {
isAgentClient_ =
(0 ==
clientAgent_.compare(0, BtccomAgentPrefix.size(), BtccomAgentPrefix));

isGrandPoolClient_ = this->getServer().grandPoolEnabled_ && clientAgent_ == PoolGrandPoolWatcher ;

isLongTimeout_ =
(isAgentClient_ || isNiceHashClient_ ||
(isAgentClient_ || isNiceHashClient_ || isGrandPoolClient_ ||
clientAgent_ == PoolWatcherAgent ||
std::regex_search(clientAgent, getServer().longTimeoutPattern_));
}
Expand Down
4 changes: 4 additions & 0 deletions src/StratumSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class StratumSession : public IStratumSession {
std::string clientAgent_; // eg. bfgminer/4.4.0-32-gac4e9b3
bool isAgentClient_;
bool isNiceHashClient_;
bool isGrandPoolClient_ = false;
std::unique_ptr<StratumMessageDispatcher> dispatcher_;

State state_;
Expand Down Expand Up @@ -213,6 +214,7 @@ class StratumSession : public IStratumSession {
size_t getChainId() const { return worker_.chainId_; }
State getState() const { return state_; }
string getUserName() const { return worker_.userName_; }
bool isGrandPoolClient() { return isGrandPoolClient_;}

bool isDead() const;
void markAsDead();
Expand Down Expand Up @@ -280,6 +282,8 @@ class StratumSessionBase : public StratumSession {
size_t kMaxNumLocalJobs_;
static constexpr size_t kNumLocalJobsToKeep_ = 4;

using LocalShareType = typename StratumTraits::LocalShareType;

public:
size_t maxNumLocalJobs() const { return kMaxNumLocalJobs_; }

Expand Down
11 changes: 11 additions & 0 deletions src/Watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ bool PoolWatchClient::handleMessage() {
string line;
if (tryReadLine(line, bev_)) {
handleStratumMessage(line);

if(state_ == TODO_RECONNECT){
triggerReconnect(this);
return false;
}

return true;
}

Expand Down Expand Up @@ -282,6 +288,11 @@ void PoolWatchClient::eventCallback(
LOG(ERROR) << "unhandled upsession events: " << events;
}

triggerReconnect(client);
}

//static fun
void PoolWatchClient::triggerReconnect(PoolWatchClient *client){
timeval reconnectTimeout{0, 0};
time_t sleepTime = 10 - (time(nullptr) - client->upTime_);
if (sleepTime > 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/Watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

// see PoolWatcherAgent in StratumSession.cc
#define BTCCOM_WATCHER_AGENT "__PoolWatcher__"
#define BTCCOM_GRANDPOOL_WATCHER_AGENT "__PoolGrandPoolWatcher__"

class PoolWatchClient;
class ClientContainer;
Expand Down Expand Up @@ -91,7 +92,7 @@ class PoolWatchClient {
virtual void handleStratumMessage(const string &line) = 0;

public:
enum State { INIT = 0, CONNECTED = 1, SUBSCRIBED = 2, AUTHENTICATED = 3 };
enum State { INIT = 0, CONNECTED = 1, SUBSCRIBED = 2, AUTHENTICATED = 3,TODO_RECONNECT=4 };

State state_;
ClientContainer *container_;
Expand Down Expand Up @@ -123,6 +124,8 @@ class PoolWatchClient {
static void readCallback(struct bufferevent *bev, void *ptr);
static void eventCallback(struct bufferevent *bev, short events, void *ptr);
static void reconnectCallback(evutil_socket_t fd, short events, void *ptr);

static void triggerReconnect(PoolWatchClient *client);
};

#endif
28 changes: 26 additions & 2 deletions src/beam/StratumBeam.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,36 @@ class StratumJobBeam : public StratumJob {
string rpcUserPwd_;
};

struct LocalShareBeam {
uint64_t exNonce2_; // extra nonce2 fixed 8 bytes
uint32_t nonce_; // nonce in block header

LocalShareBeam(uint64_t exNonce2, uint32_t nonce)
: exNonce2_(exNonce2)
, nonce_(nonce){}

LocalShareBeam &operator=(const LocalShareBeam &other) {
exNonce2_ = other.exNonce2_;
nonce_ = other.nonce_;
return *this;
}

bool operator<(const LocalShareBeam &r) const {
if (exNonce2_ < r.exNonce2_ ||
(exNonce2_ == r.exNonce2_ && nonce_ < r.nonce_) ) {
return true;
}
return false;
}
};

class ServerBeam;
class StratumSessionBeam;

struct StratumTraitsBeam {
using ServerType = ServerBeam;
using SessionType = StratumSessionBeam;
using LocalShareType = LocalShareBeam;
struct JobDiffType {
// difficulty of this job (due to difficulty adjustment,
// there can be multiple diffs in the same job)
Expand All @@ -146,9 +170,9 @@ struct StratumTraitsBeam {
return *this;
}
};
struct LocalJobType : public LocalJob {
struct LocalJobType : public LocalJobBase<LocalShareType>{
LocalJobType(size_t chainId, uint64_t jobId, uint32_t inputHash)
: LocalJob(chainId, jobId)
: LocalJobBase<LocalShareType>(chainId, jobId)
, inputHash_(inputHash) {}
bool operator==(uint32_t inputHash) const {
return inputHash_ == inputHash;
Expand Down
3 changes: 2 additions & 1 deletion src/beam/StratumMinerBeam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ void StratumMinerBeam::handleRequest_Submit(
ip.fromIpv4Int(session.getClientIp());
share.set_ip(ip.toString());

LocalShare localShare(nonce, outputHash, 0);
//LocalShare localShare(nonce, outputHash, 0);
LocalShareType localShare(nonce, outputHash);
// can't add local share
if (!localJob->addLocalShare(localShare)) {
handleShare(
Expand Down
14 changes: 10 additions & 4 deletions src/bitcoin/BlockMakerBitcoin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ void BlockMakerBitcoin::_saveBlockToDBThread(
if (jobId2SubPool_.find(foundBlock.jobId_) != jobId2SubPool_.end()) {
auto subPool = jobId2SubPool_[foundBlock.jobId_];
for (const auto &itr : subPool) {
if (coinbaseTxBin.find(itr.coinbase1_) != coinbaseTxBin.npos &&
if ((coinbaseTxBin.find(itr.coinbase1_) != coinbaseTxBin.npos || (itr.grandCoinbase1_.size()>0 && coinbaseTxBin.find(itr.grandCoinbase1_) != coinbaseTxBin.npos) ) &&
coinbaseTxBin.find(itr.coinbase2_) != coinbaseTxBin.npos) {
subPoolName = filterTableName("_" + itr.name_);
break;
Expand Down Expand Up @@ -1063,19 +1063,25 @@ void BlockMakerBitcoin::consumeStratumJob(rd_kafka_message_t *rkmessage) {

jobId2SubPool_[sjob->jobId_].clear();
for (const auto &itr : sjob->subPool_) {
vector<char> coinbase1, coinbase2;
vector<char> coinbase1, coinbase2, grandCoinbase1;
Hex2Bin(
itr.second.coinbase1_.data(),
itr.second.coinbase1_.size(),
coinbase1);
Hex2Bin(
itr.second.coinbase2_.data(),
itr.second.coinbase2_.size(),
coinbase1);
coinbase2);
Hex2Bin(
itr.second.grandCoinbase1_.data(),
itr.second.grandCoinbase1_.size(),
grandCoinbase1);

jobId2SubPool_[sjob->jobId_].push_back(
{itr.first,
string(coinbase1.begin(), coinbase1.end()),
string(coinbase2.begin(), coinbase2.end())});
string(coinbase2.begin(), coinbase2.end()),
string(grandCoinbase1.begin(), grandCoinbase1.end())});
}

// Maps (and sets) are sorted, so the first element is the smallest,
Expand Down
Loading

0 comments on commit 55eff22

Please sign in to comment.