Finds a block from the candidate blocks in vSortedCandidates, excluding already selected blocks in vSelectedBlocks, and with timestamp up to nSelectionTime.
| 167 | // already selected blocks in vSelectedBlocks, and with timestamp up to |
| 168 | // nSelectionTime. |
| 169 | static bool FindModifierBlockFromCandidates(vector <pair<int64_t, uint256>>& vSortedCandidates, map<uint256, const CBlockIndex*>& mSelectedBlocks, int64_t nSelectionTime, uint64_t nStakeModifierPrev, const CBlockIndex*& pindexSelected) |
| 170 | { |
| 171 | uint256 hashBest = 0; |
| 172 | |
| 173 | pindexSelected = nullptr; |
| 174 | |
| 175 | for (auto& item : vSortedCandidates) { |
| 176 | if (!mapBlockIndex.count(item.second)) |
| 177 | return error("%s: invalid candidate block %s", __func__, item.second.GetHex()); |
| 178 | |
| 179 | const CBlockIndex* pindex = mapBlockIndex[item.second]; |
| 180 | if (pindex->IsProofOfStake() && pindex->hashProofOfStake == 0) { |
| 181 | return error("%s: zero stake (block %s)", __func__, item.second.GetHex()); |
| 182 | } |
| 183 | |
| 184 | if (pindexSelected && pindex->GetBlockTime() > nSelectionTime) break; |
| 185 | if (mSelectedBlocks.count(pindex->GetBlockHash()) > 0) continue; |
| 186 | |
| 187 | // compute the selection hash by hashing an input that is unique to that block |
| 188 | CDataStream ss(SER_GETHASH, 0); |
| 189 | ss << uint256(pindex->IsProofOfStake() ? pindex->hashProofOfStake : pindex->GetBlockHash()) |
| 190 | << nStakeModifierPrev; |
| 191 | |
| 192 | uint256 hashSelection(Hash(ss.begin(), ss.end())); |
| 193 | |
| 194 | // the selection hash is divided by 2**32 so that proof-of-stake block |
| 195 | // is always favored over proof-of-work block. this is to preserve |
| 196 | // the energy efficiency property |
| 197 | if (pindex->IsProofOfStake()) |
| 198 | hashSelection >>= 32; |
| 199 | |
| 200 | if (pindexSelected == nullptr || hashSelection < hashBest) { |
| 201 | pindexSelected = pindex; |
| 202 | hashBest = hashSelection; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | # if defined(DEBUG_DUMP_STAKING_INFO) && false |
| 207 | if (GetBoolArg("-printstakemodifier", false)) |
| 208 | LogPrintf("%s: selected block %d %s %s\n", __func__, |
| 209 | pindexSelected->nHeight, |
| 210 | pindexSelected->GetBlockHash().GetHex(), |
| 211 | hashBest.ToString()); |
| 212 | # endif |
| 213 | |
| 214 | if (pindexSelected) { |
| 215 | // add the selected block from candidates to selected list |
| 216 | mSelectedBlocks.insert(make_pair(pindexSelected->GetBlockHash(), pindexSelected)); |
| 217 | } |
| 218 | return pindexSelected != nullptr; |
| 219 | } |
| 220 | |
| 221 | // Stake Modifier (hash modifier of proof-of-stake): |
| 222 | // The purpose of stake modifier is to prevent a txout (coin) owner from |
no test coverage detected