Skip to content

Commit edfd992

Browse files
committed
Support for Difficulty Offsets to beat Records while Mining, Bump to 2507.
Signed-off-by: Pttn <[email protected]>
1 parent 2451fdd commit edfd992

File tree

8 files changed

+32
-19
lines changed

8 files changed

+32
-19
lines changed

Client.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ std::vector<uint8_t> BlockHeader::toV8() const {
2222
return v8;
2323
}
2424

25-
mpz_class BlockHeader::target(const int32_t powVersion) const {
26-
const uint32_t difficultyIntegerPart(decodeBits(bits, powVersion));
25+
mpz_class BlockHeader::target(const int32_t powVersion, const uint16_t difficultyOffset) const {
26+
uint64_t bits64(bits);
27+
bits64 += uint64_t{difficultyOffset} << 13ULL;
28+
if (bits64 > 4294967295ULL) bits64 = 4294967295ULL;
29+
const uint32_t difficultyIntegerPart(decodeBits(bits64, powVersion));
2730
uint32_t trailingZeros;
2831
const std::array<uint8_t, 32> hash(sha256sha256(toV8().data(), 80));
2932
mpz_class target;
3033
if (powVersion == 1) {
3134
if (difficultyIntegerPart < 264U) return 0;
32-
const uint32_t df(bits & 255U);
35+
const uint32_t df(bits64 & 255U);
3336
target = 256 + ((10U*df*df*df + 7383U*df*df + 5840720U*df + 3997440U) >> 23U);
3437
target <<= 256;
3538
mpz_class hashGmp;
@@ -43,11 +46,11 @@ mpz_class BlockHeader::target(const int32_t powVersion) const {
4346
return target;
4447
}
4548

46-
std::array<uint8_t, 32> encodedOffset(const Stella::Result &result) {
49+
std::array<uint8_t, 32> encodedOffset(const Stella::Result &result, const uint16_t difficultyOffset) {
4750
std::array<uint8_t, 32> nOffset;
4851
for (auto &byte : nOffset) byte = 0;
4952
// [31-30 Primorial Number|29-14 Primorial Factor|13-2 Primorial Offset|1-0 Reserved/Version]
50-
*reinterpret_cast<uint16_t*>(&nOffset.data()[ 0]) = 2;
53+
*reinterpret_cast<uint16_t*>(&nOffset.data()[ 0]) = 2U + (difficultyOffset << 5U);
5154
*reinterpret_cast<uint64_t*>(&nOffset.data()[ 2]) = result.primorialOffset; // Only 64 bits used out of 96
5255
*reinterpret_cast<uint64_t*>(&nOffset.data()[14]) = result.primorialFactor; // Only 64 bits used out of 128
5356
*reinterpret_cast<uint16_t*>(&nOffset.data()[30]) = result.primorialNumber;

Client.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Decodes the nBits field from a Block Header
1616
double decodeBits(const uint32_t, const int32_t);
1717

18-
std::array<uint8_t, 32> encodedOffset(const Stella::Result&);
18+
std::array<uint8_t, 32> encodedOffset(const Stella::Result&, const uint16_t);
1919

2020
// Riecoin Block Header structure, total 896 bits/112 bytes (224 hex chars)
2121
struct BlockHeader { // The fields are named according to the GetBlockTemplate labels
@@ -28,8 +28,7 @@ struct BlockHeader { // The fields are named according to the GetBlockTemplate l
2828

2929
BlockHeader() : version(0), previousblockhash{0}, merkleRoot{0}, curtime(0), bits(0), nOffset{0} {}
3030
std::vector<uint8_t> toV8() const;
31-
mpz_class target(const int32_t) const;
32-
mpz_class targetOffsetMax(const int32_t) const;
31+
mpz_class target(const int32_t, const uint16_t) const;
3332
};
3433

3534
// Client or Network Parameters/Context
@@ -74,6 +73,7 @@ class GBTClient : public NetworkedClient {
7473
const std::vector<std::string> _rules;
7574
const std::string _host, _url, _proxy, _cookie;
7675
std::string _credentials;
76+
const uint16_t _difficultyOffset;
7777
const std::vector<uint8_t> _scriptPubKey;
7878
// Client State Variables
7979
CURL *_curl;
@@ -107,6 +107,7 @@ class GBTClient : public NetworkedClient {
107107
_proxy(options.proxy),
108108
_cookie(options.cookie),
109109
_credentials(options.username + ":" + options.password),
110+
_difficultyOffset(options.difficultyOffset),
110111
_scriptPubKey(bech32ToScriptPubKey(options.payoutAddress)),
111112
_curl(curl_easy_init()) {}
112113
void connect();
@@ -123,7 +124,7 @@ class GBTClient : public NetworkedClient {
123124
class StratumClient : public NetworkedClient {
124125
// Options
125126
const std::string _username, _password, _host, _proxy;
126-
const uint16_t _port;
127+
const uint16_t _port, _difficultyOffset;
127128
// Client State Variables
128129
CURL *_curl;
129130
std::mutex _submitMutex;
@@ -153,7 +154,7 @@ class StratumClient : public NetworkedClient {
153154

154155
void _processMessage(const std::string&); // Processes a message received from the pool
155156
public:
156-
StratumClient(const Options &options) : _username(options.username), _password(options.password), _host(options.host), _proxy(options.proxy), _port(options.port), _curl(curl_easy_init()) {}
157+
StratumClient(const Options &options) : _username(options.username), _password(options.password), _host(options.host), _proxy(options.proxy), _port(options.port), _difficultyOffset(options.difficultyOffset), _curl(curl_easy_init()) {}
157158
void connect(); // Also sends mining.subscribe
158159
void process(); // Get messages from the server and calls _processMessage to handle them
159160
std::optional<ClientInfo> info() const;

GBTClient.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "Stella.hpp"
55
#include "main.hpp"
66

7-
const std::string cbMsg("/rM51/");
7+
const std::string cbMsg("/rM57/");
88
static std::vector<uint8_t> coinbaseGen(const std::vector<uint8_t> &scriptPubKey, const uint32_t height, const uint64_t coinbasevalue, const std::vector<uint8_t> &dwc) {
99
std::vector<uint8_t> coinbase;
1010
// Version (01000000)
@@ -238,7 +238,7 @@ void GBTClient::process() {
238238
if (job.id == block.jobId) { // Sends a pending result via submitblock
239239
logger.log("Submitting block with "s + std::to_string(job.txCount) + " transaction(s) (including coinbase)...\n"s, MessageType::BOLD);
240240
BlockHeader bh(job.bh);
241-
bh.nOffset = encodedOffset(block);
241+
bh.nOffset = encodedOffset(block, _difficultyOffset);
242242
std::ostringstream oss;
243243
oss << v8ToHexStr(bh.toV8());
244244
// Variable Length Integer Format
@@ -298,7 +298,7 @@ std::optional<Stella::Job> GBTClient::getJob() {
298298
else
299299
_currentJobs.push_back(job);
300300
stellaJob.id = _currentJobId;
301-
stellaJob.target = job.bh.target(_currentJobTemplate.clientInfo->powVersion);
301+
stellaJob.target = job.bh.target(_currentJobTemplate.clientInfo->powVersion, _difficultyOffset);
302302
_currentJobId++;
303303
return stellaJob;
304304
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VER = 2501
1+
VER = 2507
22
CXX = g++
33
M4 = m4
44
AS = as

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ rieMiner proposes the following Modes depending on what you want to do. Use the
218218
* `SieveBits`: the size of the primorial factors table for the sieve is 2^SieveBits bits. 25 seems to be an optimal value, or 24 if there are many SieveWorkers. Though, if you have less than 8 MiB of L3 cache, you can try to decrement this value. Default: 25 if SieveWorkers <= 4, 24 otherwise;
219219
* `SieveIterations`: how many times the primorial factors table is reused for sieving. Increasing will decrease the frequency of new jobs, so less time would be "lost" in sieving, but this will also increase the memory usage. It is not clear however how this actually plays performance wise, 16 seems to be a good value. Default: 16;
220220
* `SieveWorkers`: the number of threads to use for sieving. Increasing it may solve some CPU underuse problems, but will use more memory. 0 for choosing automatically. Default: 0;
221+
* `DifficultyOffset`: while Mining, the raw Difficulty Offset as per the Improved PoW Version 1 Implementation (integer between 0 and 2047). Useful for record attempts while mining rather than via the Search Mode. Default: 0;
221222
* `RestartDifficultyFactor`: if the Difficulty changes by the given factor, the miner will restart. Useful to let it retune some parameters once a while to optimize for lower or higher Difficulties as it varies. This value must be at least 1 and the closer it is to 1 and the more often there will be restarts. Default: 1.03;
222223
* `ConstellationPattern`: which sort of constellations to look for, as offsets separated by commas. Note that they are not cumulative, so '0, 2, 4, 2, 4, 6, 2' corresponds to n + (0, 2, 6, 8, 12, 18, 20). If empty (or not accepted by the server), a valid pattern will be chosen (0, 2, 4, 2, 4, 6, 2 in Search and Benchmark Modes). Default: empty;
223224
* `PrimorialOffsets`: list of offsets from a primorial multiple to use for the sieve process, separated by commas. If empty, a default one will be chosen if possible (see main.hpp source file), otherwise rieMiner will not start (if the chosen constellation pattern is not in main.hpp). Default: empty;

StratumClient.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "Client.hpp"
66
#include "Stella.hpp"
77

8-
constexpr const char* userAgent("rieMiner/2501");
8+
constexpr const char* userAgent("rieMiner/2507");
99

1010
static std::array<uint8_t, 32> calculateMerkleRootStratum(const std::vector<std::array<uint8_t, 32>> &merkleBranches) {
1111
std::array<uint8_t, 32> merkleRoot{};
@@ -312,7 +312,7 @@ void StratumClient::process() {
312312
<< job.jobId << "\", \""
313313
<< v8ToHexStr(job.extraNonce2) << "\", \""
314314
<< std::setfill('0') << std::setw(16) << std::hex << job.bh.curtime << "\", \""
315-
<< v8ToHexStr(reverse(a8ToV8(encodedOffset(share)))) << "\"]}\n";
315+
<< v8ToHexStr(reverse(a8ToV8(encodedOffset(share, _difficultyOffset)))) << "\"]}\n";
316316
logger.logDebug("Sending: "s + oss.str());
317317
size_t bytesSent(0);
318318
CURLcode cc(curl_easy_send(_curl, oss.str().c_str(), oss.str().size(), &bytesSent));
@@ -393,7 +393,7 @@ std::optional<Stella::Job> StratumClient::getJob() {
393393
else
394394
_currentJobs.push_back(job);
395395
stellaJob.id = _currentJobId;
396-
stellaJob.target = job.bh.target(_currentJobTemplate.clientInfo->powVersion);
396+
stellaJob.target = job.bh.target(_currentJobTemplate.clientInfo->powVersion, _difficultyOffset);
397397
_currentJobId++;
398398
return stellaJob;
399399
}

main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ bool Configuration::parse(const int argc, char** argv, std::string &parsingMessa
134134
try {_options.stellaConfig.sieveIterations = std::stoi(value);}
135135
catch (...) {_options.stellaConfig.sieveIterations = 0;}
136136
}
137+
else if (key == "DifficultyOffset") {
138+
try {_options.difficultyOffset = std::stoi(value);}
139+
catch (...) {_options.difficultyOffset = 0U;}
140+
if (_options.difficultyOffset > 2047U) _options.difficultyOffset = 2047U;
141+
}
137142
else if (key == "RestartDifficultyFactor") {
138143
try {_options.restartDifficultyFactor = std::stod(value);}
139144
catch (...) {_options.restartDifficultyFactor = 1.03;}
@@ -365,6 +370,8 @@ int main(int argc, char** argv) {
365370
if (configuration.options().rules.size() > 0)
366371
logger.log("Consensus Rules: "s + Stella::formatContainer(configuration.options().rules) + "\n"s);
367372
}
373+
if (configuration.options().difficultyOffset != 0)
374+
std::cout << "Difficulty Offset " << configuration.options().difficultyOffset << " (+ " << 32*configuration.options().difficultyOffset << ")" << std::endl;
368375
logger.log("Auto retune when the Difficulty varies by a factor "s + Stella::doubleToString(configuration.options().restartDifficultyFactor) + "\n"s);
369376
}
370377
if (configuration.options().refreshInterval > 0.)
@@ -448,7 +455,7 @@ int main(int argc, char** argv) {
448455
stellaInstance->addJob(job.value());
449456
stellaInstance->startThreads();
450457
logger.hr();
451-
logger.log(Stella::formattedClockTimeNow() + " Started mining at block "s + std::to_string(clientInfo->height) + ", difficulty "s + Stella::doubleToString(clientInfo->difficulty, 3U) + "\n"s, MessageType::BOLD);
458+
logger.log(Stella::formattedClockTimeNow() + " Started mining at block "s + std::to_string(clientInfo->height) + ", difficulty "s + Stella::doubleToString(clientInfo->difficulty + 32U*configuration.options().difficultyOffset, 3U) + (32U*configuration.options().difficultyOffset > 0U ? " (offset +"s + std::to_string(32U*configuration.options().difficultyOffset) + ")"s : ""s)+ "\n"s, MessageType::BOLD);
452459
timer = std::chrono::steady_clock::now();
453460
continue;
454461
}
@@ -465,7 +472,7 @@ int main(int argc, char** argv) {
465472
tupleCountsRecent[countsRecentEntryPos] = std::make_pair(std::chrono::steady_clock::now(), stellaInstance->getTupleCounts());
466473
// Notify the event
467474
logger.log(Stella::formattedClockTimeNow());
468-
logger.log(" Block "s + std::to_string(currentHeight) + ", average "s + Stella::doubleToString(averageBlockTime, 1) + " s, difficulty "s + Stella::doubleToString(clientInfo->difficulty, 3) + "\n"s);
475+
logger.log(" Block "s + std::to_string(currentHeight) + ", average "s + Stella::doubleToString(averageBlockTime, 1) + " s, difficulty "s + Stella::doubleToString(clientInfo->difficulty + 32U*configuration.options().difficultyOffset, 3U) + (32U*configuration.options().difficultyOffset > 0U ? " (offset +"s + std::to_string(32U*configuration.options().difficultyOffset) + ")"s : ""s)+ "\n"s);
469476

470477
// Restart if needed to retune parameters.
471478
if (!stellaInstance->hasAcceptedPatterns(clientInfo->acceptedPatterns)) { // Pattern changed and no longer compatible with the current one.

main.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct Options {
4343
uint16_t port{28332U};
4444
double refreshInterval{30.}, difficulty{1024.}, benchmarkBlockInterval{150.}, benchmarkTimeLimit{960.};
4545
uint64_t benchmarkPrimeCountLimit{10000000};
46+
uint16_t difficultyOffset{0U};
4647
double restartDifficultyFactor{1.03};
4748
std::vector<std::string> rules{};
4849
uint16_t apiPort{0U};

0 commit comments

Comments
 (0)