diff --git a/src/miner.cpp b/src/miner.cpp index 6434ab872..42c7fc3f2 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1161,19 +1161,30 @@ void static RandomXMiner() } - // TODO: use same block key for groups of blocks - if(Mining_height > 1024 && (Mining_height % 1024 == 64)) { - // At block height 1024*N+64 we should use the blockhash of block 1024*N as randomx key - // use blockchain data to set a new key+input for a group of blocks - uint256 randomxBlockKey; - randomxBlockKey = chainActive[Mining_height - 64]->GetBlockHash(); + // TODO: we may want to tweak these params to more closely match the 2.8 days and ~2 hrs + // which is what they work out to on XMR + // The key block will change every ~21.3 hours with a 75s block time + // and every ~17 hours with the default 60s block time for HSCs + int randomxInterval = 1024; + // This lag is 80 mins for 75s blocktime and 64 mins for 60s (default) blocktime for HSCs + int randomxBlockLag = 64; + + // Use the initial key at the start of the chain + if( (Mining_height-randomxBlockLag) < randomxInterval) { + LogPrintf("RandomX using initial key\n"); + randomx_init_cache(randomxCache, &randomxKey, sizeof randomxKey); + } else { + // At heights between intervals, we use the same block key and wait randomxBlockLag blocks until changing + int keyHeight = ((Mining_height - randomxBlockLag) / randomxInterval) * randomxInterval; + LogPrintf("RandomX key height=%d\n", keyHeight); + uint256 randomxBlockKey = chainActive[keyHeight]->GetBlockHash(); crypto_generichash_blake2b_state new_state; - crypto_generichash_blake2b_update(&state, (unsigned char*)&randomxBlockKey, 32); + crypto_generichash_blake2b_update(&state, (unsigned char *)&randomxBlockKey, 32); crypto_generichash_blake2b_final(&state, randomxInput, 32); + + randomx_init_cache(randomxCache, &randomxBlockKey, sizeof randomxKey); } - // TODO: use correct variables - randomx_init_cache(randomxCache, &randomxKey, sizeof randomxKey); randomx_vm *myVM = randomx_create_vm(flags, randomxCache, NULL); if(myVM == NULL) { LogPrintf("Cannot create RandomX VM, aborting!\n");