process block delegates, call at the tail of block executing
| 67 | |
| 68 | // process block delegates, call at the tail of block executing |
| 69 | bool chain::ProcessBlockDelegates(CBlock &block, CCacheWrapper &cw, CValidationState &state) { |
| 70 | // required preparing the undo for cw |
| 71 | |
| 72 | auto version = GetFeatureForkVersion(block.GetHeight()); |
| 73 | |
| 74 | if (version >= MAJOR_VER_R3_5) { |
| 75 | CBlockInflatedReward blockInflatedReward; |
| 76 | cw.blockCache.GetBlockInflatedReward(blockInflatedReward); |
| 77 | if (blockInflatedReward.start_height != 0 && block.GetHeight() >= blockInflatedReward.start_height) { |
| 78 | uint64_t new_rewards = 0; |
| 79 | cw.sysParamCache.GetParam(SysParamType::BLOCK_INFLATED_REWARD_AMOUNT, new_rewards); |
| 80 | blockInflatedReward.new_rewards += new_rewards; |
| 81 | if (!cw.blockCache.SetBlockInflatedReward(blockInflatedReward)) { |
| 82 | return state.DoS(100, ERRORMSG("[%d] Save BlockInflatedReward failed! block=%s", |
| 83 | block.GetHeight(), block.GetHash().ToString())); |
| 84 | } |
| 85 | LogPrint(BCLog::DELEGATE, "Inflated rewards=%llu at block=%s", new_rewards, block.GetIdStr()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool isR3Fork = version >= MAJOR_VER_R3; |
| 90 | int32_t countVoteInterval = (isR3Fork) ? COUNT_VOTE_INTERVAL_AFTER_V3 : COUNT_VOTE_INTERVAL_BEFORE_V3; // the interval to count the vote |
| 91 | int32_t activateDelegateInterval = (isR3Fork) ? ACTIVATE_DELEGATE_DELAY_AFTER_V3 : ACTIVATE_DELEGATE_DELAY_BEFORE_V3; |
| 92 | |
| 93 | PendingDelegates pendingDelegates; |
| 94 | cw.delegateCache.GetPendingDelegates(pendingDelegates); |
| 95 | |
| 96 | // get last update height of vote |
| 97 | if (pendingDelegates.state != VoteDelegateState::PENDING && |
| 98 | (countVoteInterval == 0 || (block.GetHeight() % countVoteInterval == 0))) { |
| 99 | VoteDelegateVector activeDelegates; |
| 100 | if (!cw.delegateCache.GetActiveDelegates(activeDelegates)) { |
| 101 | LogPrint(BCLog::INFO, "[%d] Active delegates do not exist, will be initialized later! block=%s\n", |
| 102 | block.GetHeight(), block.GetHash().ToString()); |
| 103 | } |
| 104 | int32_t lastVoteHeight = cw.delegateCache.GetLastVoteHeight(); |
| 105 | uint32_t delegateNum = cw.sysParamCache.GetTotalBpsSize(block.GetHeight()) ; |
| 106 | |
| 107 | if (pendingDelegates.counted_vote_height == 0 || |
| 108 | lastVoteHeight > (int32_t)pendingDelegates.counted_vote_height || |
| 109 | activeDelegates.size() != delegateNum) { |
| 110 | |
| 111 | if (!GenPendingDelegates(block, delegateNum, cw, activeDelegates, pendingDelegates, isR3Fork)) { |
| 112 | return state.DoS(100, ERRORMSG("[%d] GenPendingDelegates failed! block=%s", |
| 113 | block.GetHeight(), block.GetHash().ToString())); |
| 114 | } |
| 115 | |
| 116 | if (!cw.delegateCache.SetPendingDelegates(pendingDelegates)) { |
| 117 | return state.DoS(100, ERRORMSG("[%d] Save pending delegates failed! block=%s", |
| 118 | block.GetHeight(), block.GetHash().ToString())); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // must execute below separately because activateDelegateInterval may be 0 |
| 124 | if (pendingDelegates.state != VoteDelegateState::ACTIVATED) { |
| 125 | if (block.GetHeight() - pendingDelegates.counted_vote_height >= (uint32_t)activateDelegateInterval) { |
| 126 | VoteDelegateVector activeDelegates = pendingDelegates.top_vote_delegates; |
nothing calls this directly
no test coverage detected