Cryptocurrency mining pool written in C++ for speed. Supports Stratum.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.3 KiB

#include "ShareLimiter.h"
#include "Server.h"
#include "Client.h"
#include "Config.h"
#include "Log.h"
namespace Stratum
{
// Returning false will stop any further share verifications (DoS prevention, etc)
bool ShareLimiter::Submit(uint64 diff)
{
++_totalShares;
uint64 curTime = Util::Date();
uint64 sinceLast = curTime - _lastRetarget;
_shares.push_back(ShareLimiterRecord(diff, curTime));
if (sinceLast < sConfig.Get<uint32>("RetargetInterval"))
return true;
_lastRetarget = curTime;
// Check if miner is ok
if (_totalShares > 50 && (double(_totalBadShares)/double(_totalShares)) > 0.8) {
_client->Ban(600);
return false;
}
// Drop old shares
while (_shares.size()) {
if (_shares.front().time > curTime - sConfig.Get<uint32>("RetargetTimeBuffer"))
break;
_shares.pop_front();
}
double totalWeighted = 0;
for (uint32 i = 0; i < _shares.size(); ++i)
totalWeighted += _shares[i].diff;
double interval = std::min(curTime - _startTime, uint64(sConfig.Get<uint32>("RetargetTimeBuffer")));
// Calculate hashrate in MH/s
double hashrate = (MEGAHASHCONST*totalWeighted)/interval;
// Calculate new diff
uint64 newDiff = (hashrate * sConfig.Get<double>("RetargetTimePerShare")) / MEGAHASHCONST;
// Check Limits
if (newDiff < sConfig.Get<uint32>("RetargetMinDiff"))
newDiff = sConfig.Get<uint32>("RetargetMinDiff");
if (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);
return true;
}
}