bool static MiningBlock(CBlock *pblock,CWallet *pwallet,CBlockIndex* pindexPrev,unsigned int nTransactionsUpdatedLast,CAccountViewCache &view, CTransactionDBCache &txCache, CScriptDBViewCache &scriptCache){ int64_t nStart = GetTime(); unsigned int lasttime = 0xFFFFFFFF; while (true) { // Check for stop or if block needs to be rebuilt boost::this_thread::interruption_point(); if (vNodes.empty() && SysCfg().NetworkID() != CBaseParams::REGTEST) return false; if (pindexPrev != chainActive.Tip()) return false; //获取时间 同时等待下次时间到 auto GetNextTimeAndSleep = [&]() { while(max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime()) == lasttime) { ::MilliSleep(800); } return (lasttime = max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime())); }; GetNextTimeAndSleep(); // max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime()); UpdateTime(*pblock, pindexPrev); if (pindexPrev != chainActive.Tip()) return false; set<CKeyID> setCreateKey; setCreateKey.clear(); int64_t lasttime = GetTimeMillis(); bool increatedfalg =CreatePosTx(pindexPrev, pblock, setCreateKey,view,txCache,scriptCache); LogPrint("MINER","CreatePosTx used time :%d ms\n", GetTimeMillis() - lasttime); if (increatedfalg == true) { SetThreadPriority(THREAD_PRIORITY_NORMAL); { int64_t lasttime1 = GetTimeMillis(); CheckWork(pblock, *pwallet); LogPrint("MINER","CheckWork used time :%d ms\n", GetTimeMillis() - lasttime1); } SetThreadPriority(THREAD_PRIORITY_LOWEST); return true; } if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast || GetTime() - nStart > 60) return false; } return false; }
uint256 CreateBlockWithAppointedAddr(CKeyID const &keyID) { if (SysCfg().NetworkID() == CBaseParams::REGTEST) { // unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); mempool.GetTransactionsUpdated(); CBlockIndex* pindexPrev = chainActive.Tip(); CAccountViewCache accview(*pAccountViewTip, true); CTransactionDBCache txCache(*pTxCacheTip, true); CScriptDBViewCache ScriptDbTemp(*pScriptDBTip, true); shared_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(accview,txCache,ScriptDbTemp)); if (!pblocktemplate.get()) return uint256(); CBlock *pblock = &pblocktemplate.get()->block; // int nBlockSize = pblock->GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION); pblock->GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION); // int64_t nStart = GetTime(); while (true) { pblock->SetTime(max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime())); set<CKeyID> setCreateKey; setCreateKey.clear(); setCreateKey.insert(keyID); if (CreatePosTx(pindexPrev, pblock,setCreateKey,accview,txCache,ScriptDbTemp)) { CheckWork(pblock, *pwalletMain); // int nBlockSize = pblock->GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION); } if(setCreateKey.empty()) { LogPrint("postx", "%s is not exist in the wallet\r\n",keyID.ToAddress()); break; } ::MilliSleep(1); if (pindexPrev != chainActive.Tip()) { return chainActive.Tip()->GetBlockHash() ; } } } return uint256(); }
void Motogame::onReadAvailable() { while (true) { QByteArray Msg = m_Motogame.readLine(); if (Msg.size() == 0) break; MotoWork Work; MotoPoW PoW; if (motoParseMessage(Msg.data(), Work)) { auto iter = findBlock(Work); if (iter != m_Templates.end()) m_Templates.erase(iter); } else if (motoParseMessage(Msg.data(), Work, PoW)) { auto iter = findBlock(Work); if (iter != m_Templates.end()) { CBlock *pBlock = &(*iter)->block; pBlock->Nonce = PoW; CheckWork(pBlock, *m_pWallet, m_ReserveKey); } updateBlock(); } else if (strncmp(Msg.data(), "***Config:*", 11) == 0) { QString Controls(Msg.data() + 11); Controls.remove("\n"); QSettings settings; settings.setValue("GameControls", Controls); } } }
int PrimeWorker::HandleRequest(zmq_pollitem_t *item) { proto::Request& req = mRequest; zmsg_t* msg = ReceiveRequest(req, item->socket); if(!msg) return 0; //req.PrintDebugString(); proto::Request::Type rtype = req.type(); proto::Reply::ErrType etype = proto::Reply::NONE; proto::Reply& rep = mReply; rep.Clear(); rep.set_type(rtype); rep.set_reqid(req.reqid()); if(!proto::Request::Type_IsValid(rtype)){ printf("ERROR: !proto::Request::Type_IsValid.\n"); rtype = proto::Request::NONE; etype = proto::Reply::INVALID; } while(etype == proto::Reply::NONE) { int vstatus = CheckVersion(req.version()); if(vstatus <= 0){ rep.set_errstr("Your miner version is no longer supported. Please upgrade."); etype = proto::Reply::VERSION; break; } if(rtype == proto::Request::CONNECT){ rep.mutable_sinfo()->CopyFrom(mServerInfo); if(vstatus == 1){ etype = proto::Reply::VERSION; rep.set_errstr("Your miner version will no longer be supported in the near future. Please upgrade."); } }else if(rtype == proto::Request::GETWORK){ if(!mCurrBlock.has_height()){ etype = proto::Reply::HEIGHT; break; } if(req.height() != mCurrHeight){ etype = proto::Reply::HEIGHT; break; } CBlock *pblock = &mBlockTemplate->block; IncrementExtraNonce(pblock, mIndexPrev, mExtraNonce); pblock->nTime = std::max(pblock->nTime, (unsigned int)GetAdjustedTime()); mNonceMap[pblock->hashMerkleRoot] = mExtraNonce; proto::Work* work = rep.mutable_work(); work->set_height(mCurrHeight); work->set_merkle(pblock->hashMerkleRoot.GetHex()); work->set_time(pblock->nTime); work->set_bits(pblock->nBits); }else if(rtype == proto::Request::SHARE){ if(!mCurrBlock.has_height()){ etype = proto::Reply::STALE; break; } if(!req.has_share()){ printf("ERROR: !req.has_share().\n"); etype = proto::Reply::INVALID; break; } const proto::Share& share = req.share(); if(share.height() != mCurrHeight){ etype = proto::Reply::STALE; break; } if(share.length() < mCurrBlock.minshare()){ printf("ERROR: share.length too short.\n"); etype = proto::Reply::INVALID; break; } uint256 merkleRoot; merkleRoot.SetHex(share.merkle()); unsigned extraNonce = mNonceMap[merkleRoot]; if(!extraNonce){ etype = proto::Reply::STALE; break; } unsigned nCandidateType = share.chaintype(); if(nCandidateType > 2){ printf("ERROR: share.chaintype invalid.\n"); etype = proto::Reply::INVALID; break; } CBlock *pblock = &mBlockTemplate->block; extraNonce--; IncrementExtraNonce(pblock, mIndexPrev, extraNonce); pblock->nTime = share.time(); pblock->nBits = share.bits(); pblock->nNonce = share.nonce(); uint256 headerHash = pblock->GetHeaderHash(); { uint256 headerHashClient; headerHashClient.SetHex(share.hash()); if(headerHashClient != headerHash){ printf("ERROR: headerHashClient != headerHash.\n"); etype = proto::Reply::INVALID; break; } } pblock->bnPrimeChainMultiplier.SetHex(share.multi()); uint256 blockhash = pblock->GetHash(); if(!mShares.insert(blockhash).second){ etype = proto::Reply::DUPLICATE; break; } CBigNum bnChainOrigin = CBigNum(headerHash) * pblock->bnPrimeChainMultiplier; unsigned int nChainLength = 0; bool isblock = ProbablePrimeChainTestForMiner(bnChainOrigin, pblock->nBits, nCandidateType+1, nChainLength); nChainLength = TargetGetLength(nChainLength); if(nChainLength >= mCurrBlock.minshare()){ if(isblock) isblock = CheckWork(pblock, *mWallet, mReserveKey); if(share.length() != nChainLength){ printf("ERROR: share.length() != nChainLength.\n"); etype = proto::Reply::INVALID; } mData.Clear(); proto::Share* mshare = mData.mutable_share(); mshare->CopyFrom(share); mshare->set_blockhash(blockhash.GetHex()); mshare->set_length(nChainLength); mshare->set_isblock(isblock); if(isblock){ mshare->set_genvalue(pblock->vtx[0].vout[0].nValue); } SendData(mData, mBackend); CBitcoinAddress address(share.addr()); if(!address.IsValid()){ printf("ERROR: invalid address for share: %s\n", share.addr().c_str()); etype = proto::Reply::INVALID; std::string errstr = "Your payment address '"; errstr.append(share.addr()); errstr.append("' is INVALID!!!"); rep.set_errstr(errstr); break; } }else{ printf("ERROR: share.length too short after test: %d/%d\n", nChainLength, share.length()); etype = proto::Reply::INVALID; break; } }else if(rtype == proto::Request::STATS){ if(!req.has_stats()){ printf("ERROR: !req.has_stats().\n"); etype = proto::Reply::INVALID; break; } const proto::ClientStats& stats = req.stats(); std::pair<std::string,uint64> key = std::make_pair(stats.addr(), stats.clientid() * stats.instanceid()); std::map<std::pair<std::string,uint64>, proto::Data>::iterator iter = mStats.find(key); if(iter != mStats.end()){ proto::ClientStats* s = mStats[key].mutable_clientstats(); s->set_version(std::min(s->version(), stats.version())); s->set_cpd(s->cpd() + stats.cpd()); s->set_errors(s->errors() + stats.errors()); s->set_temp(std::max(s->temp(), stats.temp())); s->set_latency(std::max(s->latency(), stats.latency())); s->set_ngpus(s->ngpus() + stats.ngpus()); /*if(s->name() != stats.name()){ s->mutable_name()->append("+"); s->mutable_name()->append(stats.name()); }*/ }else if(mStats.size() < 100000){ mStats[key].mutable_clientstats()->CopyFrom(stats); } } break; } if(req.height() < mCurrHeight){ rep.mutable_block()->CopyFrom(mCurrBlock); } mReqStats[std::make_pair(rtype,etype)]++; rep.set_error(etype); SendReply(rep, &msg, item->socket); zmsg_destroy(&msg); return 0; }