Fix randomx cache selection for RPCs

Was using the wrong cache slot, and returning invalid PoW hashes to RPC clients
This commit is contained in:
Howard Chu 2019-10-01 02:43:00 -07:00 committed by Howard Chu
parent b60cf6a938
commit 1c600a492f
No known key found for this signature in database
GPG Key ID: FD2A70B44AB11BA7
1 changed files with 28 additions and 12 deletions

View File

@ -50,7 +50,7 @@
typedef struct rx_state { typedef struct rx_state {
CTHR_MUTEX_TYPE rs_mutex; CTHR_MUTEX_TYPE rs_mutex;
char rs_hash[32]; char rs_hash[HASH_SIZE];
uint64_t rs_height; uint64_t rs_height;
randomx_cache *rs_cache; randomx_cache *rs_cache;
} rx_state; } rx_state;
@ -63,7 +63,8 @@ static rx_state rx_s[2] = {{CTHR_MUTEX_INIT,{0},0,0},{CTHR_MUTEX_INIT,{0},0,0}};
static randomx_dataset *rx_dataset; static randomx_dataset *rx_dataset;
static uint64_t rx_dataset_height; static uint64_t rx_dataset_height;
static THREADV randomx_vm *rx_vm = NULL; static THREADV randomx_vm *rx_vm = NULL;
static THREADV int rx_toggle; static THREADV int rx_toggle = -1;
static THREADV char rx_seedhash[HASH_SIZE];
static void local_abort(const char *msg) static void local_abort(const char *msg)
{ {
@ -234,21 +235,25 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch
char *hash, int miners, int is_alt) { char *hash, int miners, int is_alt) {
uint64_t s_height = rx_seedheight(mainheight); uint64_t s_height = rx_seedheight(mainheight);
int changed = 0; int changed = 0;
int toggle = is_alt ? s_height : seedheight; int toggle = (s_height & SEEDHASH_EPOCH_BLOCKS) != 0;
randomx_flags flags = RANDOMX_FLAG_DEFAULT; randomx_flags flags = RANDOMX_FLAG_DEFAULT;
rx_state *rx_sp; rx_state *rx_sp;
randomx_cache *cache; randomx_cache *cache;
toggle = (toggle & SEEDHASH_EPOCH_BLOCKS) != 0;
CTHR_MUTEX_LOCK(rx_mutex); CTHR_MUTEX_LOCK(rx_mutex);
/* if alt block but with same seed as mainchain, no need for alt cache */ /* if alt block but with same seed as mainchain, no need for alt cache */
if (is_alt && s_height == seedheight && !memcmp(rx_s[toggle].rs_hash, seedhash, sizeof(rx_s[toggle].rs_hash))) if (is_alt) {
if (s_height == seedheight && !memcmp(rx_s[toggle].rs_hash, seedhash, HASH_SIZE))
is_alt = 0; is_alt = 0;
} else {
/* RPC could request an earlier block on mainchain */ /* RPC could request an earlier block on mainchain */
if (!is_alt && s_height > seedheight) if (s_height > seedheight)
is_alt = 1; is_alt = 1;
/* miner can be ahead of mainchain */
else if (s_height < seedheight)
toggle ^= 1;
}
toggle ^= (is_alt != 0); toggle ^= (is_alt != 0);
if (toggle != rx_toggle) if (toggle != rx_toggle)
@ -273,13 +278,16 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch
local_abort("Couldn't allocate RandomX cache"); local_abort("Couldn't allocate RandomX cache");
} }
} }
if (rx_sp->rs_height != seedheight || rx_sp->rs_cache == NULL || memcmp(seedhash, rx_sp->rs_hash, sizeof(rx_sp->rs_hash))) { if (rx_sp->rs_height != seedheight || rx_sp->rs_cache == NULL || memcmp(seedhash, rx_sp->rs_hash, HASH_SIZE)) {
randomx_init_cache(cache, seedhash, 32); randomx_init_cache(cache, seedhash, HASH_SIZE);
rx_sp->rs_cache = cache; rx_sp->rs_cache = cache;
rx_sp->rs_height = seedheight; rx_sp->rs_height = seedheight;
memcpy(rx_sp->rs_hash, seedhash, sizeof(rx_sp->rs_hash)); memcpy(rx_sp->rs_hash, seedhash, HASH_SIZE);
changed = 1; changed = 1;
} }
/* only non-miners use rx_seedhash */
if (!miners && !changed && memcmp(seedhash, rx_seedhash, HASH_SIZE))
changed = 1;
if (rx_vm == NULL) { if (rx_vm == NULL) {
randomx_flags flags = RANDOMX_FLAG_DEFAULT; randomx_flags flags = RANDOMX_FLAG_DEFAULT;
if (use_rx_jit()) { if (use_rx_jit()) {
@ -307,6 +315,9 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch
mwarning(RX_LOGCAT, "Couldn't allocate RandomX dataset for miner"); mwarning(RX_LOGCAT, "Couldn't allocate RandomX dataset for miner");
} }
CTHR_MUTEX_UNLOCK(rx_dataset_mutex); CTHR_MUTEX_UNLOCK(rx_dataset_mutex);
} else {
/* only non-miners use rx_seedhash */
memcpy(rx_seedhash, seedhash, HASH_SIZE);
} }
rx_vm = randomx_create_vm(flags | RANDOMX_FLAG_LARGE_PAGES, rx_sp->rs_cache, rx_dataset); rx_vm = randomx_create_vm(flags | RANDOMX_FLAG_LARGE_PAGES, rx_sp->rs_cache, rx_dataset);
if(rx_vm == NULL) { //large pages failed if(rx_vm == NULL) { //large pages failed
@ -326,6 +337,11 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch
CTHR_MUTEX_UNLOCK(rx_dataset_mutex); CTHR_MUTEX_UNLOCK(rx_dataset_mutex);
} else if (changed) { } else if (changed) {
randomx_vm_set_cache(rx_vm, rx_sp->rs_cache); randomx_vm_set_cache(rx_vm, rx_sp->rs_cache);
/* remember the seedhash being used by the current VM. Other threads may set
* rx_sp->rs_cache to the correct value, but we still have to know if the
* current rx_vm has been set to use it or not.
*/
memcpy(rx_seedhash, seedhash, HASH_SIZE);
} }
/* mainchain users can run in parallel */ /* mainchain users can run in parallel */
if (!is_alt) if (!is_alt)