From 1c600a492ff8670bf354b4418820950230115bfa Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Tue, 1 Oct 2019 02:43:00 -0700 Subject: Fix randomx cache selection for RPCs Was using the wrong cache slot, and returning invalid PoW hashes to RPC clients --- src/crypto/rx-slow-hash.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/crypto/rx-slow-hash.c b/src/crypto/rx-slow-hash.c index ada372864..6d2f50016 100644 --- a/src/crypto/rx-slow-hash.c +++ b/src/crypto/rx-slow-hash.c @@ -50,7 +50,7 @@ typedef struct rx_state { CTHR_MUTEX_TYPE rs_mutex; - char rs_hash[32]; + char rs_hash[HASH_SIZE]; uint64_t rs_height; randomx_cache *rs_cache; } 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 uint64_t rx_dataset_height; 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) { @@ -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) { uint64_t s_height = rx_seedheight(mainheight); int changed = 0; - int toggle = is_alt ? s_height : seedheight; + int toggle = (s_height & SEEDHASH_EPOCH_BLOCKS) != 0; randomx_flags flags = RANDOMX_FLAG_DEFAULT; rx_state *rx_sp; randomx_cache *cache; - toggle = (toggle & SEEDHASH_EPOCH_BLOCKS) != 0; CTHR_MUTEX_LOCK(rx_mutex); /* 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))) - is_alt = 0; - + if (is_alt) { + if (s_height == seedheight && !memcmp(rx_s[toggle].rs_hash, seedhash, HASH_SIZE)) + is_alt = 0; + } else { /* RPC could request an earlier block on mainchain */ - if (!is_alt && s_height > seedheight) - is_alt = 1; + if (s_height > seedheight) + is_alt = 1; + /* miner can be ahead of mainchain */ + else if (s_height < seedheight) + toggle ^= 1; + } toggle ^= (is_alt != 0); 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"); } } - if (rx_sp->rs_height != seedheight || rx_sp->rs_cache == NULL || memcmp(seedhash, rx_sp->rs_hash, sizeof(rx_sp->rs_hash))) { - randomx_init_cache(cache, seedhash, 32); + if (rx_sp->rs_height != seedheight || rx_sp->rs_cache == NULL || memcmp(seedhash, rx_sp->rs_hash, HASH_SIZE)) { + randomx_init_cache(cache, seedhash, HASH_SIZE); rx_sp->rs_cache = cache; 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; } + /* only non-miners use rx_seedhash */ + if (!miners && !changed && memcmp(seedhash, rx_seedhash, HASH_SIZE)) + changed = 1; if (rx_vm == NULL) { randomx_flags flags = RANDOMX_FLAG_DEFAULT; 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"); } 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); 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); } else if (changed) { 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 */ if (!is_alt) -- cgit v1.2.3 From 2675cf448309c20487c0877e0e680a96424fc383 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Sat, 5 Oct 2019 18:51:20 +0100 Subject: Update to RandomX v1.1.3, simplify We don't need to detect if the cache has changed, just always call to set it on the VM. The call will be a no-op if the cache hasn't changed. --- external/randomx | 2 +- src/crypto/rx-slow-hash.c | 21 ++------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/external/randomx b/external/randomx index 519b9cf70..298cc7709 160000 --- a/external/randomx +++ b/external/randomx @@ -1 +1 @@ -Subproject commit 519b9cf70540bdd996e806251cde335c8eef8aca +Subproject commit 298cc77095c8992e30ecdd4ca0aa09a969a62bc1 diff --git a/src/crypto/rx-slow-hash.c b/src/crypto/rx-slow-hash.c index 6d2f50016..11aa5584d 100644 --- a/src/crypto/rx-slow-hash.c +++ b/src/crypto/rx-slow-hash.c @@ -63,8 +63,6 @@ static rx_state rx_s[2] = {{CTHR_MUTEX_INIT,{0},0,0},{CTHR_MUTEX_INIT,{0},0,0}}; static randomx_dataset *rx_dataset; static uint64_t rx_dataset_height; static THREADV randomx_vm *rx_vm = NULL; -static THREADV int rx_toggle = -1; -static THREADV char rx_seedhash[HASH_SIZE]; static void local_abort(const char *msg) { @@ -234,7 +232,6 @@ static void rx_initdata(randomx_cache *rs_cache, const int miners, const uint64_ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const char *seedhash, const void *data, size_t length, char *hash, int miners, int is_alt) { uint64_t s_height = rx_seedheight(mainheight); - int changed = 0; int toggle = (s_height & SEEDHASH_EPOCH_BLOCKS) != 0; randomx_flags flags = RANDOMX_FLAG_DEFAULT; rx_state *rx_sp; @@ -256,9 +253,6 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch } toggle ^= (is_alt != 0); - if (toggle != rx_toggle) - changed = 1; - rx_toggle = toggle; rx_sp = &rx_s[toggle]; CTHR_MUTEX_LOCK(rx_sp->rs_mutex); @@ -283,11 +277,7 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch rx_sp->rs_cache = cache; rx_sp->rs_height = seedheight; memcpy(rx_sp->rs_hash, seedhash, HASH_SIZE); - changed = 1; } - /* only non-miners use rx_seedhash */ - if (!miners && !changed && memcmp(seedhash, rx_seedhash, HASH_SIZE)) - changed = 1; if (rx_vm == NULL) { randomx_flags flags = RANDOMX_FLAG_DEFAULT; if (use_rx_jit()) { @@ -315,9 +305,6 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch mwarning(RX_LOGCAT, "Couldn't allocate RandomX dataset for miner"); } 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); if(rx_vm == NULL) { //large pages failed @@ -335,13 +322,9 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch if (rx_dataset != NULL && rx_dataset_height != seedheight) rx_initdata(cache, miners, seedheight); CTHR_MUTEX_UNLOCK(rx_dataset_mutex); - } else if (changed) { + } else { + /* this is a no-op if the cache hasn't changed */ 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 */ if (!is_alt) -- cgit v1.2.3 From f54301dd752ac1c6ff43d1e9e69f07b16313bf41 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Mon, 7 Oct 2019 09:50:00 +0100 Subject: Fix for miners on reorg Make sure dataset gets re-init'd if a reorg changes the epoch --- src/crypto/rx-slow-hash.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/crypto/rx-slow-hash.c b/src/crypto/rx-slow-hash.c index 11aa5584d..59bd89d13 100644 --- a/src/crypto/rx-slow-hash.c +++ b/src/crypto/rx-slow-hash.c @@ -161,8 +161,11 @@ void rx_reorg(const uint64_t split_height) { int i; CTHR_MUTEX_LOCK(rx_mutex); for (i=0; i<2; i++) { - if (split_height < rx_s[i].rs_height) + if (split_height <= rx_s[i].rs_height) { + if (rx_s[i].rs_height == rx_dataset_height) + rx_dataset_height = 1; rx_s[i].rs_height = 1; /* set to an invalid seed height */ + } } CTHR_MUTEX_UNLOCK(rx_mutex); } -- cgit v1.2.3