From 35dde25d98e661f00cddde23f48680c662bc1624 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sun, 23 Jan 2022 00:32:01 -0500 Subject: [PATCH] Fill out some RandomXMiner code, which is BitcoinMiner without all the extraneous junk --- src/miner.cpp | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/src/miner.cpp b/src/miner.cpp index c0d80fdee..da19909c8 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -985,6 +985,176 @@ CBlockIndex *get_chainactive(int32_t height) int32_t gotinvalid; +#ifdef ENABLE_WALLET +void static RandomXMiner(CWallet *pwallet) +#else +void static RandomXMiner() +#endif +{ + LogPrintf("HushRandomXMiner started\n"); + SetThreadPriority(THREAD_PRIORITY_LOWEST); + RenameThread("hush-randomx"); + const CChainParams& chainparams = Params(); + +#ifdef ENABLE_WALLET + // Each thread has its own key + CReserveKey reservekey(pwallet); +#endif + // Each thread has its own counter + unsigned int nExtraNonce = 0; + + uint8_t *script; uint64_t total; int32_t i,j,gpucount=HUSH_MAXGPUCOUNT,notaryid = -1; + while ( (ASSETCHAIN_INIT == 0 || HUSH_INITDONE == 0) ) + { + sleep(1); + if ( hush_baseid(SMART_CHAIN_SYMBOL) < 0 ) + break; + } + + if ( notaryid != My_notaryid ) + My_notaryid = notaryid; + + std::mutex m_cs; + bool cancelSolver = false; + boost::signals2::connection c = uiInterface.NotifyBlockTip.connect( + [&m_cs, &cancelSolver](const uint256& hashNewTip) mutable { + std::lock_guard lock{m_cs}; + cancelSolver = true; + } + ); + miningTimer.start(); + + try { + if ( SMART_CHAIN_SYMBOL[0] != 0 ) + fprintf(stderr,"try %s Mining with %s\n",SMART_CHAIN_SYMBOL,solver.c_str()); + while (true) + { + if (chainparams.MiningRequiresPeers()) { + //if ( ASSETCHAINS_SEED != 0 && chainActive.LastTip()->GetHeight() < 100 ) + // break; + // Busy-wait for the network to come online so we don't waste time mining + // on an obsolete chain. In regtest mode we expect to fly solo. + miningTimer.stop(); + do { + bool fvNodesEmpty; + { + //LOCK(cs_vNodes); + fvNodesEmpty = vNodes.empty(); + } + if (!fvNodesEmpty )//&& !IsInitialBlockDownload()) + break; + MilliSleep(15000); + //fprintf(stderr,"fvNodesEmpty %d IsInitialBlockDownload(%s) %d\n",(int32_t)fvNodesEmpty,SMART_CHAIN_SYMBOL,(int32_t)IsInitialBlockDownload()); + + } while (true); + //fprintf(stderr,"%s Found peers\n",SMART_CHAIN_SYMBOL); + miningTimer.start(); + } + // + // Create new block + // + unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); + CBlockIndex* pindexPrev = chainActive.LastTip(); + if ( Mining_height != pindexPrev->GetHeight()+1 ) + { + Mining_height = pindexPrev->GetHeight()+1; + Mining_start = (uint32_t)time(NULL); + } +#ifdef ENABLE_WALLET + CBlockTemplate *ptr = CreateNewBlockWithKey(reservekey, pindexPrev->GetHeight()+1, gpucount, 0); +#else + CBlockTemplate *ptr = CreateNewBlockWithKey(); +#endif + if ( ptr == 0 ) + { + if ( !GetBoolArg("-gen",false)) + { + miningTimer.stop(); + c.disconnect(); + LogPrintf("HushMiner terminated\n"); + return; + } + static uint32_t counter; + if ( counter++ < 10 && ASSETCHAINS_STAKED == 0 ) + fprintf(stderr,"created illegal blockB, retry\n"); + sleep(1); + continue; + } + //fprintf(stderr,"get template\n"); + unique_ptr pblocktemplate(ptr); + if (!pblocktemplate.get()) + { + if (GetArg("-mineraddress", "").empty()) { + LogPrintf("Error in HushMiner: 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"); + } + return; + } + CBlock *pblock = &pblocktemplate->block; + if ( SMART_CHAIN_SYMBOL[0] != 0 ) + { + if ( ASSETCHAINS_REWARD[0] == 0 && !ASSETCHAINS_LASTERA ) + { + if ( pblock->vtx.size() == 1 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) + { + static uint32_t counter; + if ( counter++ < 10 ) + fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",SMART_CHAIN_SYMBOL); + sleep(10); + continue; + } else fprintf(stderr,"%s vouts.%d mining.%d vs %d\n",SMART_CHAIN_SYMBOL,(int32_t)pblock->vtx[0].vout.size(),Mining_height,ASSETCHAINS_MINHEIGHT); + } + } + IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); + //fprintf(stderr,"Running HushMiner.%s with %u transactions in block\n",solver.c_str(),(int32_t)pblock->vtx.size()); + LogPrintf("Running HushRandomXMiner with %u transactions in block (%u bytes)\n",,pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); + + // Search + uint8_t pubkeys[66][33]; arith_uint256 bnMaxPoSdiff; uint32_t blocktimes[66]; int mids[256],nonzpkeys,i,j,externalflag; uint32_t savebits; int64_t nStart = GetTime(); + pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); + savebits = pblock->nBits; + HASHTarget = arith_uint256().SetCompact(savebits); + roundrobin_delay = ROUNDROBIN_DELAY; + + // HASHTarget_POW = hush_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); + gotinvalid = 0; + while (true) + { + //fprintf(stderr,"gotinvalid.%d\n",gotinvalid); + if ( gotinvalid != 0 ) + break; + hush_longestchain(); + + LogPrint("pow", "Running RandomX solver with nNonce = %s\n",pblock->nNonce.ToString()); + arith_uint256 hashTarget; + hashTarget = HASHTarget; + + // TODO: RandomX solver code + + } + + } + } + catch (const boost::thread_interrupted&) + { + miningTimer.stop(); + c.disconnect(); + LogPrintf("HushRandomXMiner terminated\n"); + throw; + } + catch (const std::runtime_error &e) + { + miningTimer.stop(); + c.disconnect(); + LogPrintf("HushRandomXMiner runtime error: %s\n", e.what()); + return; + } + miningTimer.stop(); + c.disconnect(); +} + #ifdef ENABLE_WALLET void static BitcoinMiner(CWallet *pwallet) #else