MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / ProduceBlock

Function ProduceBlock

src/miner/miner.cpp:651–728  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

649
650
651static bool ProduceBlock(int64_t startMiningMs, CBlockIndex *pPrevIndex, Miner &miner, const uint32_t totalDelegateNum) {
652 // has done LOCK(cs_main);
653 int64_t lastTime = 0;
654 bool success = false;
655 int32_t blockHeight = 0;
656 std::unique_ptr<CBlock> pBlock(new CBlock());
657 if (!pBlock.get())
658 throw runtime_error("ProduceBlock() : failed to create new block");
659
660 blockHeight = pPrevIndex->height + 1;
661 const CRegID &minerId = miner.account.regid;
662
663 if (!CheckPackBlockTime(startMiningMs, blockHeight)) {
664 LogPrint(BCLog::MINER, "[%d] no time left to pack block! start_ms=%lld, miner_regid=%s\n",
665 blockHeight, startMiningMs, minerId.ToString());
666 return false;
667 }
668
669 lastTime = GetTimeMillis();
670 auto spCW = std::make_shared<CCacheWrapper>(pCdMan);
671
672 pBlock->SetTime(MillisToSecond(startMiningMs)); // set block time first
673
674 if (blockHeight == (int32_t)SysCfg().GetVer2GenesisHeight()) {
675 success = CreateStableCoinGenesisBlock(pBlock); // stable coin genesis
676
677 } else if (GetFeatureForkVersion(blockHeight) == MAJOR_VER_R1) {
678 success = CreateNewBlockForPreStableCoinRelease(miner, *spCW, pBlock); // pre-stable coin release
679
680 } else {
681 success = CreateNewBlockForStableCoinRelease(startMiningMs, miner, *spCW, pBlock); // stable coin release
682 }
683
684 if (!success) {
685 LogPrint(BCLog::MINER, "[%d] failed to add a new block: regid=%s, "
686 "used_time_ms=%lld\n", blockHeight, minerId.ToString(),
687 GetTimeMillis() - lastTime);
688 return false;
689 }
690 LogPrint(BCLog::MINER,
691 "[%d] succeeded in adding a new block: regid=%s, tx_count=%u, "
692 "used_time_ms=%lld\n", blockHeight, minerId.ToString(),
693 pBlock->vptx.size(), GetTimeMillis() - lastTime);
694
695 lastTime = GetTimeMillis();
696 success = CreateBlockRewardTx(miner, *spCW, pBlock.get(), totalDelegateNum);
697 if (!success) {
698 LogPrint(BCLog::MINER, "[%d] failed in CreateBlockRewardTx: regid=%s, "
699 "used_time_ms=%lld\n", blockHeight, minerId.ToString(), GetTimeMillis() - lastTime);
700 return false;
701 }
702 LogPrint(BCLog::MINER, "[%d] succeeded in CreateBlockRewardTx: regid=%s, reward_txid=%s, "
703 "used_time_ms=%lld\n", blockHeight, minerId.ToString(), pBlock->vptx[0]->GetHash().ToString(),
704 GetTimeMillis() - lastTime);
705
706 lastTime = GetTimeMillis();
707 success = CheckWork(pBlock.get());
708 if (!success) {

Callers 1

ThreadBlockProducingFunction · 0.85

Calls 15

CheckPackBlockTimeFunction · 0.85
GetFeatureForkVersionFunction · 0.85
CreateBlockRewardTxFunction · 0.85
CheckWorkFunction · 0.85
SetTimeMethod · 0.80
GetIdStrMethod · 0.80
GetTimeMillisFunction · 0.50
getMethod · 0.45
ToStringMethod · 0.45

Tested by

no test coverage detected