Browse Source

Merge branch 'master' into peercoin

peercoin
Intel 11 years ago
parent
commit
b11eb3db8c
  1. 2
      src/server/poolserver/Main.cpp
  2. 30
      src/server/poolserver/Stratum/Client.cpp
  3. 1
      src/server/poolserver/Stratum/Client.h
  4. 38
      src/server/poolserver/Stratum/ShareLimiter.cpp
  5. 17
      src/server/poolserver/Stratum/ShareLimiter.h

2
src/server/poolserver/Main.cpp

@ -49,7 +49,7 @@ bool InitConfig(int argc, char *argv[])
("StratumBlockCheckTime", boost::program_options::value<uint32>()->default_value(2000), "Time between block checks in ms") ("StratumBlockCheckTime", boost::program_options::value<uint32>()->default_value(2000), "Time between block checks in ms")
("RetargetInterval", boost::program_options::value<uint32>()->default_value(20), "Time between difficulty checks in seconds") ("RetargetInterval", boost::program_options::value<uint32>()->default_value(20), "Time between difficulty checks in seconds")
("RetargetTimeBuffer", boost::program_options::value<uint32>()->default_value(60*5), "Buffer of shares to keep (in seconds)") ("RetargetTimeBuffer", boost::program_options::value<uint32>()->default_value(60*5), "Buffer of shares to keep (in seconds)")
("RetargetSharesPerMin", boost::program_options::value<uint32>()->default_value(15), "How many shares per min to target for") ("RetargetTimePerShare", boost::program_options::value<double>()->default_value(10), "Target in seconds between shares")
("RetargetVariance", boost::program_options::value<uint32>()->default_value(40), "Maximum allowed variance in percent before difficulty change") ("RetargetVariance", boost::program_options::value<uint32>()->default_value(40), "Maximum allowed variance in percent before difficulty change")
("RetargetMinDiff", boost::program_options::value<uint32>()->default_value(1), "Minimum difficulty (also starting difficulty)") ("RetargetMinDiff", boost::program_options::value<uint32>()->default_value(1), "Minimum difficulty (also starting difficulty)")
("RetargetMaxDiff", boost::program_options::value<uint32>()->default_value(1000000), "Maximum difficulty we can reach") ("RetargetMaxDiff", boost::program_options::value<uint32>()->default_value(1000000), "Maximum difficulty we can reach")

30
src/server/poolserver/Stratum/Client.cpp

@ -79,18 +79,6 @@ namespace Stratum
{ {
JSON params = msg["params"]; JSON params = msg["params"];
// Share limiter
if (!_shareLimiter.Submit()) {
JSON response;
response["id"] = msg["id"];
response["result"];
response["error"].Add(int64(20));
response["error"].Add("Blocked by share limiter");
response["error"].Add(JSON());
SendMessage(response);
return;
}
// check username // check username
std::string username = params[0].GetString(); std::string username = params[0].GetString();
if (!_workers.count(username)) { if (!_workers.count(username)) {
@ -124,6 +112,21 @@ namespace Stratum
return; return;
} }
// Get job
Job& job = _jobs[jobid];
// Share limiter
if (!_shareLimiter.Submit(job.diff)) {
JSON response;
response["id"] = msg["id"];
response["result"];
response["error"].Add(int64(20));
response["error"].Add("Blocked by share limiter");
response["error"].Add(JSON());
SendMessage(response);
return;
}
BinaryData extranonce2 = Util::ASCIIToBin(params[2].GetString()); BinaryData extranonce2 = Util::ASCIIToBin(params[2].GetString());
if (extranonce2.size() != 4) { if (extranonce2.size() != 4) {
sLog.Error(LOG_STRATUM, "Wrong extranonce size"); sLog.Error(LOG_STRATUM, "Wrong extranonce size");
@ -162,9 +165,6 @@ namespace Stratum
SendMessage(response); SendMessage(response);
return; return;
} }
// Get job
Job& job = _jobs[jobid];
ByteBuffer share; ByteBuffer share;
share << extranonce2 << timebuf << noncebuf; share << extranonce2 << timebuf << noncebuf;

1
src/server/poolserver/Stratum/Client.h

@ -143,7 +143,6 @@ namespace Stratum
_socket, _socket,
boost::asio::buffer(redirect.c_str(), redirect.length()), boost::asio::buffer(redirect.c_str(), redirect.length()),
_ioStrand.wrap(boost::bind(&Client::_OnSend, shared_from_this(), boost::asio::placeholders::error))); _ioStrand.wrap(boost::bind(&Client::_OnSend, shared_from_this(), boost::asio::placeholders::error)));
Disconnect();
} }
void CloseSocket() void CloseSocket()

38
src/server/poolserver/Stratum/ShareLimiter.cpp

@ -7,14 +7,14 @@
namespace Stratum namespace Stratum
{ {
// Returning false will stop any further share verifications (DoS prevention, etc) // Returning false will stop any further share verifications (DoS prevention, etc)
bool ShareLimiter::Submit() bool ShareLimiter::Submit(uint64 diff)
{ {
++_totalShares; ++_totalShares;
uint64 curTime = Util::Date(); uint64 curTime = Util::Date();
uint64 sinceLast = curTime - _lastRetarget; uint64 sinceLast = curTime - _lastRetarget;
_shares.push_back(curTime); _shares.push_back(ShareLimiterRecord(diff, curTime));
if (sinceLast < sConfig.Get<uint32>("RetargetInterval")) if (sinceLast < sConfig.Get<uint32>("RetargetInterval"))
return true; return true;
@ -27,34 +27,40 @@ namespace Stratum
return false; return false;
} }
// Drop old shares
while (_shares.size()) { while (_shares.size()) {
if (_shares.front() > curTime - sConfig.Get<uint32>("RetargetTimeBuffer")) if (_shares.front().time > curTime - sConfig.Get<uint32>("RetargetTimeBuffer"))
break; break;
_shares.pop_front(); _shares.pop_front();
} }
uint32 interval = sConfig.Get<uint32>("RetargetTimeBuffer"); double totalWeighted = 0;
for (uint32 i = 0; i < _shares.size(); ++i)
totalWeighted += _shares[i].diff;
// Calculate shares/min double interval = std::min(curTime - _startTime, uint64(sConfig.Get<uint32>("RetargetTimeBuffer")));
double speed = double(_shares.size()*60) / double(interval);
// Calculate difference from pool target in % // Calculate hashrate in MH/s
double variance = speed / double(sConfig.Get<uint32>("RetargetSharesPerMin")); double hashrate = (MEGAHASHCONST*totalWeighted)/interval;
sLog.Debug(LOG_STRATUM, "Miner variance: %f speed: %f", variance, speed); // Calculate new diff
uint64 newDiff = (hashrate * sConfig.Get<double>("RetargetTimePerShare")) / MEGAHASHCONST;
// Check if we need to retarget
if (variance*100 < sConfig.Get<uint32>("RetargetVariance"))
return true;
uint64 newDiff = double(_client->GetDifficulty()) * variance;
// Check Limits
if (newDiff < sConfig.Get<uint32>("RetargetMinDiff")) if (newDiff < sConfig.Get<uint32>("RetargetMinDiff"))
newDiff = sConfig.Get<uint32>("RetargetMinDiff"); newDiff = sConfig.Get<uint32>("RetargetMinDiff");
if (newDiff > sConfig.Get<uint32>("RetargetMaxDiff")) if (newDiff > sConfig.Get<uint32>("RetargetMaxDiff"))
newDiff = sConfig.Get<uint32>("RetargetMaxDiff"); newDiff = sConfig.Get<uint32>("RetargetMaxDiff");
// Calculate variance in %
uint32 variance = abs(((newDiff - _client->GetDifficulty()) * 100) / _client->GetDifficulty());
sLog.Debug(LOG_STRATUM, "Miner new diff: %u Variance: %u%% Hashrate: %f MH/s", newDiff, variance, hashrate);
// Check if we need to retarget
if (variance < sConfig.Get<uint32>("RetargetVariance"))
return true;
_client->SetDifficulty(newDiff, true); _client->SetDifficulty(newDiff, true);
return true; return true;

17
src/server/poolserver/Stratum/ShareLimiter.h

@ -6,20 +6,31 @@
#include <deque> #include <deque>
// 2^32 * 10^-6
#define MEGAHASHCONST 4294.967296
namespace Stratum namespace Stratum
{ {
class Client; class Client;
struct ShareLimiterRecord
{
ShareLimiterRecord(uint64 adiff, uint64 atime) : diff(adiff), time(atime) {}
uint64 diff;
uint64 time;
};
class ShareLimiter class ShareLimiter
{ {
public: public:
ShareLimiter(Client* client) : _client(client), _totalShares(0), _totalBadShares(0) ShareLimiter(Client* client) : _client(client), _totalShares(0), _totalBadShares(0)
{ {
_startTime = Util::Date(); // Minus one to prevent crash when interval is zero
_startTime = Util::Date()-1;
_lastRetarget = _startTime; _lastRetarget = _startTime;
} }
bool Submit(); bool Submit(uint64 diff);
void LogBad() void LogBad()
{ {
@ -27,7 +38,7 @@ namespace Stratum
} }
private: private:
std::deque<uint64> _shares; std::deque<ShareLimiterRecord> _shares;
Client* _client; Client* _client;
uint64 _lastRetarget; uint64 _lastRetarget;
uint64 _startTime; uint64 _startTime;

Loading…
Cancel
Save