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

Method ProcessBlockDelegates

src/chain/blockdelegates.cpp:69–143  ·  view source on GitHub ↗

process block delegates, call at the tail of block executing

Source from the content-addressed store, hash-verified

67
68// process block delegates, call at the tail of block executing
69bool 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;

Callers

nothing calls this directly

Calls 15

GetFeatureForkVersionFunction · 0.85
GenPendingDelegatesFunction · 0.85
GetParamMethod · 0.80
DoSMethod · 0.80
GetIdStrMethod · 0.80
GetPendingDelegatesMethod · 0.80
GetActiveDelegatesMethod · 0.80
GetLastVoteHeightMethod · 0.80
GetTotalBpsSizeMethod · 0.80
SetPendingDelegatesMethod · 0.80

Tested by

no test coverage detected