66#endif
77#include < climits>
88
9- // Constructor
109Job::Job (const Notification ¬ification, const Subscribe &subscribe, double difficulty) : difficulty(difficulty)
1110{
1211 const char *version = notification.version .c_str ();
1312 const char *prevhash = notification.prevhash .c_str ();
1413 const char *ntime_string = notification.ntime .c_str ();
1514 const char *nbits = notification.nbits .c_str ();
16- uint8_t merkle_branch_size = notification.merkle_branch .size ();
17- std::vector<std::string> merkle_branch (merkle_branch_size);
18- for (uint8_t i = 0 ; i < merkle_branch_size; i++)
19- {
20- merkle_branch[i] = notification.merkle_branch [i].c_str ();
21- }
2215
2316 job_id = notification.job_id ;
2417 ntime = ntime_string;
@@ -28,45 +21,33 @@ Job::Job(const Notification ¬ification, const Subscribe &subscribe, double di
2821#else
2922 extranonce2 = " 00000002" ;
3023#endif
31- std::string coinbase = notification.coinb1 + subscribe.extranonce1 + extranonce2 + notification.coinb2 ;
32- l_info (TAG_JOB, " [%s] - Coinbase: %s" , job_id.c_str (), coinbase.c_str ());
33-
34- #if defined(ESP8266)
35- ESP.wdtFeed ();
36- #endif
24+ const std::string coinbase = notification.coinb1 + subscribe.extranonce1 + extranonce2 + notification.coinb2 ;
3725
3826 std::string coinbase_hash;
3927 generateCoinbaseHash (coinbase, coinbase_hash);
4028
4129 std::string merkle_root;
42- calculateMerkleRoot (coinbase_hash, merkle_branch, merkle_root);
30+ calculateMerkleRoot (coinbase_hash, notification. merkle_branch , merkle_root);
4331
44- block.version = strtoul (version, NULL , 16 );
32+ block.version = strtoul (version, nullptr , 16 );
4533 hexStringToByteArray (prevhash, block.previous_block );
4634 reverseBytesAndFlip (block.previous_block , 32 );
4735
4836 hexStringToByteArray (merkle_root.c_str (), block.merkle_root );
49- block.ntime = strtoul (ntime.c_str (), NULL , 16 );
50- block.nbits = strtoul (nbits, NULL , 16 );
37+ block.ntime = strtoul (ntime.c_str (), nullptr , 16 );
38+ block.nbits = strtoul (nbits, nullptr , 16 );
5139 block.nonce = 0 ;
5240
5341 target.calculate (nbits);
5442
55- nerd_mids (&sha, (unsigned char *)&block);
56- }
57-
58- // Destructor
59- Job::~Job ()
60- {
61- // No need to manually release resources for std::string members
43+ nerd_mids (&sha, reinterpret_cast <unsigned char *>(&block));
6244}
6345
6446uint8_t Job::pickaxe (uint32_t core, uint8_t *hash, uint32_t &winning_nonce)
6547{
6648 nextNonce (core);
6749 winning_nonce = block.nonce ;
68- uint8_t is_valid = nerd_sha256d (&sha, (unsigned char *)&block + 64 , hash);
69- return is_valid;
50+ return nerd_sha256d (&sha, reinterpret_cast <unsigned char *>(&block) + 64 , hash);
7051}
7152
7253void Job::setStartNonce (uint32_t start_nonce)
@@ -81,34 +62,25 @@ void Job::nextNonce(uint32_t core)
8162
8263void Job::generateCoinbaseHash (const std::string &coinbase, std::string &coinbase_hash)
8364{
84- // Convert hex string to byte array
85- size_t len = coinbase.length ();
65+ const size_t len = coinbase.length ();
8666 uint8_t coinbaseBytes[len / 2 ];
8767 hexStringToByteArray (coinbase.c_str (), coinbaseBytes);
88-
89- // Calculate SHA256 hash
9068 uint8_t hash[SHA256M_BLOCK_SIZE];
9169 sha256_double (coinbaseBytes, len / 2 , hash);
92-
93- // Convert binary hash to hex string
9470 coinbase_hash = byteArrayToHexString (hash, SHA256M_BLOCK_SIZE);
95- l_info (TAG_JOB, " [%s] - Coinbase hash: %s" , job_id.c_str (), coinbase_hash.c_str ());
9671}
9772
9873void Job::calculateMerkleRoot (const std::string &coinbase_hash, const std::vector<std::string> &merkle_branch, std::string &merkle_root)
9974{
10075 uint8_t hash[SHA256M_BLOCK_SIZE] = {};
10176 hexStringToByteArray (coinbase_hash.c_str (), hash);
10277
103- for (size_t i = 0 ; i < merkle_branch. size (); i++ )
78+ for (const auto &branch : merkle_branch)
10479 {
10580 uint8_t merkle_branch_bin[32 ];
106- hexStringToByteArray (merkle_branch[i].c_str (), merkle_branch_bin);
107-
108- l_info (TAG_JOB, " [%s] - Merkle Branch [%d]: %s" , job_id.c_str (), i, merkle_branch[i].c_str ());
81+ hexStringToByteArray (branch.c_str (), merkle_branch_bin);
10982
11083 uint8_t merkle_concatenated[SHA256M_BLOCK_SIZE * 2 ];
111-
11284 for (size_t j = 0 ; j < 32 ; j++)
11385 {
11486 merkle_concatenated[j] = hash[j];
@@ -123,7 +95,7 @@ void Job::calculateMerkleRoot(const std::string &coinbase_hash, const std::vecto
12395
12496std::string Job::generate_extra_nonce2 (int extranonce2_size)
12597{
126- // Generate a random number between 0 and ULONG_MAX
98+ // Generate a random number between 0 and INT_MAX
12799#if defined(ESP8266)
128100 uint32_t randomValue = esp.random (0 , INT_MAX);
129101#else
@@ -139,17 +111,11 @@ std::string Job::generate_extra_nonce2(int extranonce2_size)
139111
140112 // Convert the random number to a hex string
141113 snprintf (extranonce2, hexStringLength, " %u" , randomValue);
142-
143- // Ensure the hex string is padded to the required length
144- while ((int )strlen (extranonce2) < hexStringLength - 1 )
114+ while (static_cast <int >(strlen (extranonce2)) < hexStringLength - 1 )
145115 {
146- char * temp = new char [hexStringLength];
116+ char temp[hexStringLength];
147117 snprintf (temp, hexStringLength, " 0%s" , extranonce2);
148- delete[] extranonce2;
149- extranonce2 = temp;
118+ strcpy (extranonce2, temp);
150119 }
151-
152- l_info (TAG_JOB, " ExtraNonce2: %s" , extranonce2);
153-
154120 return std::string (extranonce2);
155- }
121+ }
0 commit comments