diff --git a/src/miner.cpp b/src/miner.cpp index 42c7fc3f2..6037be939 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1012,9 +1012,6 @@ void static RandomXMiner() break; } - if ( notaryid != My_notaryid ) - My_notaryid = notaryid; - std::mutex m_cs; bool cancelSolver = false; boost::signals2::connection c = uiInterface.NotifyBlockTip.connect( @@ -1026,9 +1023,8 @@ void static RandomXMiner() miningTimer.start(); try { - if ( SMART_CHAIN_SYMBOL[0] != 0 ) { - fprintf(stderr,"trying %s Mining with randomx\n",SMART_CHAIN_SYMBOL); - } + fprintf(stderr,"trying %s Mining with randomx\n",SMART_CHAIN_SYMBOL); + while (true) { if (chainparams.MiningRequiresPeers()) { @@ -1073,7 +1069,7 @@ void static RandomXMiner() { miningTimer.stop(); c.disconnect(); - LogPrintf("HushMiner terminated\n"); + LogPrintf("HushRandomXMiner terminated\n"); return; } static uint32_t counter; @@ -1087,10 +1083,10 @@ void static RandomXMiner() if (!pblocktemplate.get()) { if (GetArg("-mineraddress", "").empty()) { - LogPrintf("Error in HushMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); + LogPrintf("Error in HushRandomXMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); } else { // Should never reach here, because -mineraddress validity is checked in init.cpp - LogPrintf("Error in HushMiner: Invalid -mineraddress\n"); + LogPrintf("Error in HushRandomXMiner: Invalid -mineraddress\n"); } return; } @@ -1119,12 +1115,12 @@ void static RandomXMiner() savebits = pblock->nBits; HASHTarget = arith_uint256().SetCompact(savebits); roundrobin_delay = ROUNDROBIN_DELAY; + Mining_start = 0; - // HASHTarget_POW = hush_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); gotinvalid = 0; while (true) { - //fprintf(stderr,"gotinvalid.%d\n",gotinvalid); + fprintf(stderr,"gotinvalid.%d\n",gotinvalid); if ( gotinvalid != 0 ) break; hush_longestchain(); @@ -1160,9 +1156,6 @@ void static RandomXMiner() return; } - - // 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; @@ -1204,9 +1197,79 @@ void static RandomXMiner() printf("\n"); // TODO: use randomx hash to build a block - } + std::function)> validBlock = +#ifdef ENABLE_WALLET + [&pblock, &hashTarget, &pwallet, &reservekey, &m_cs, &cancelSolver, &chainparams] +#else + [&pblock, &hashTarget, &m_cs, &cancelSolver, &chainparams] +#endif + (std::vector soln) { + int32_t z; arith_uint256 h; CBlock B; + // Write the solution to the hash and compute the result. + LogPrint("pow", "- Checking solution against target\n"); + pblock->nSolution = soln; + solutionTargetChecks.increment(); + B = *pblock; + h = UintToArith256(B.GetHash()); + if ( h > hashTarget ) + { + return false; + } - } + CValidationState state; + if ( !TestBlockValidity(state,B, chainActive.LastTip(), true, false)) + { + h = UintToArith256(B.GetHash()); + //for (z=31; z>=0; z--) + // fprintf(stderr,"%02x",((uint8_t *)&h)[z]); + fprintf(stderr," Invalid block mined, try again\n"); + gotinvalid = 1; + return(false); + } + SetThreadPriority(THREAD_PRIORITY_NORMAL); + LogPrintf("HushRandomXMiner:\n"); + LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", B.GetHash().GetHex(), HASHTarget.GetHex()); +#ifdef ENABLE_WALLET + if (ProcessBlockFound(&B, *pwallet, reservekey)) { +#else + if (ProcessBlockFound(&B)) { +#endif + // Ignore chain updates caused by us + std::lock_guard lock{m_cs}; + cancelSolver = false; + } + SetThreadPriority(THREAD_PRIORITY_LOWEST); + // In regression test mode, stop mining after a block is found. + if (chainparams.MineBlocksOnDemand()) { + // Increment here because throwing skips the call below + // TODO: equivalent of ehSolverRuns.increment(); + throw boost::thread_interrupted(); + } + return true; + }; + // TODO: solver specific stuff + // TODO: Convert solution indices to byte array (decompress) and pass it to validBlock method. + + boost::this_thread::interruption_point(); + + if (vNodes.empty() && chainparams.MiningRequiresPeers()) + { + if ( Mining_height > ASSETCHAINS_MINHEIGHT ) + { + fprintf(stderr,"no nodes, break\n"); + break; + } + } + if ((UintToArith256(pblock->nNonce) & 0xffff) == 0xffff) + { + fprintf(stderr,"nonce & 0xffff == 0xffff, break\n"); + break; + } + // Update nNonce and nTime + pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1); + pblock->nBits = savebits; + } + } } catch (const boost::thread_interrupted&) {