Skip to content

Commit

Permalink
Add batch mode usage of RandomX API and enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcartel committed May 25, 2024
1 parent 9248279 commit 033ae18
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ randomx_flags rx_flags;
bool want_largepages = false;
bool want_affinity = true;
bool want_softaes = false;
bool want_batchmode = true;
static int opt_nonces = 1000;
pthread_mutex_t dataset_lock;
pthread_barrier_t dataset_barrier;
Expand Down Expand Up @@ -187,6 +188,7 @@ Options:\n\
randomx RandomX (default)\n\
--largepages enable large pages\n\
--softaes disable hardware AES\n\
--no-batch disable batch mode\n\
--no-affinity disable thread binding\n\
-o, --url=URL URL of mining server\n\
-O, --userpass=U:P username:password pair for mining server\n\
Expand Down Expand Up @@ -243,6 +245,7 @@ static struct option const options[] = {
{ "no-affinity", 0, NULL, 2001 },
{ "nonces", 1000, NULL, 2002 },
{ "softaes", 0, NULL, 2003 },
{ "no-batch", 0, NULL, 2004 },
// !RANDOMX END
#ifndef WIN32
{ "background", 0, NULL, 'B' },
Expand Down Expand Up @@ -1437,8 +1440,11 @@ static void *miner_thread(void *userdata)
/* scan nonces for a proof-of-work hash */
switch (opt_algo) {
case ALGO_RANDOMX:
rc = scanhash_randomx(thr_id, work.data, work.target,
max_nonce, rx_vm, &hashes_done, work.rx_hash);
if (want_batchmode) {
rc = scanhash_randomx_batch(thr_id, work.data, work.target, max_nonce, rx_vm, &hashes_done, work.rx_hash);
} else {
rc = scanhash_randomx(thr_id, work.data, work.target, max_nonce, rx_vm, &hashes_done, work.rx_hash);
}
break;

default:
Expand Down Expand Up @@ -1967,6 +1973,9 @@ static void parse_arg(int key, char *arg, char *pname)
case 2003:
want_softaes = true;
break;
case 2004:
want_batchmode = false;
break;
// !RANDOMX END
case 1001:
free(opt_cert);
Expand Down
3 changes: 3 additions & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ extern int scanhash_sha256d(int thr_id, uint32_t *pdata,
extern int scanhash_randomx(int thr_id, uint32_t *pdata,
const uint32_t *ptarget, uint32_t max_nonce, randomx_vm *vm, unsigned long *hashes_done, uint8_t *out_hash);

extern int scanhash_randomx_batch(int thr_id, uint32_t *pdata,
const uint32_t *ptarget, uint32_t max_nonce, randomx_vm *vm, unsigned long *hashes_done, uint8_t *out_hash);

struct thr_info {
int id;
pthread_t pth;
Expand Down
64 changes: 64 additions & 0 deletions randomx-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,67 @@ int scanhash_randomx(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
pdata[19] = n;
return 0;
}


int scanhash_randomx_batch(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint32_t max_nonce, randomx_vm *vm, unsigned long *hashes_done, uint8_t *out_rx_hash)
{
const uint32_t Htarg = ptarget[7];
uint32_t n = pdata[19];
uint8_t rx_hash[RANDOMX_HASH_SIZE];
uint8_t rx_cm[RANDOMX_HASH_SIZE];

// Construct blockheader in serialized little endian form (code from submit work function)
uint32_t blockHeader[28];
memset(blockHeader, 0, sizeof(blockHeader)); // make sure rx_hash field is zero for rx hash and cm computation
for (int i = 0; i < 20; i++)
{
be32enc(blockHeader + i, pdata[i]);
}

void *blockHeaderPtr = (void *)blockHeader;
void *noncePtr = &blockHeader[19];
const uint32_t *cmCheckPtr = &((const uint32_t *)&rx_cm)[7];

// First round of batch mode
le32enc(noncePtr, n);
randomx_calculate_hash_first(vm, blockHeaderPtr, sizeof(blockHeader));

while (n < max_nonce && !work_restart[thr_id].restart)
{
// serialize nonce N+1 into blockheader in little endian
le32enc(noncePtr, n + 1);

// prepare to calculate hash for N+1, return hash for N
//randomx_calculate_hash(vm, blockHeaderPtr, sizeof(blockHeader), &rx_hash);
randomx_calculate_hash_next(vm, blockHeaderPtr, sizeof(blockHeader), &rx_hash);

// serialize N into blockheader to compute commitment
le32enc(noncePtr, n);

// calculate commitment for nonce N
randomx_calculate_commitment(blockHeaderPtr, sizeof(blockHeader), &rx_hash, &rx_cm);

if (*cmCheckPtr < Htarg)
{
if (fulltest((void *)rx_cm, ptarget))
{
*hashes_done = n - pdata[19] + 1;

// Note: Encode nonce into buffer as big endian, so it gets flipped back to little endian when submitting to node
be32enc((void *)&pdata[19], n);

// rx_hash is already in little endian order for uint256, so just copy it into block header for submission
memcpy(out_rx_hash, rx_hash, RANDOMX_HASH_SIZE);

return 1;
}
}

n++;
}

*hashes_done = n - pdata[19];
pdata[19] = n;
return 0;
}

0 comments on commit 033ae18

Please sign in to comment.