| 509 | } |
| 510 | |
| 511 | int GetMasternodeRank(CTxIn& vin, int64_t nBlockHeight, int minProtocol) { |
| 512 | //LOCK(cs_masternodes); |
| 513 | std::vector< pair<unsigned int, CTxIn> > vecMasternodeScores; |
| 514 | |
| 515 | for (CMasterNode & mn : vecMasternodes) { |
| 516 | mn.Check(); |
| 517 | |
| 518 | if (mn.protocolVersion < minProtocol) continue; |
| 519 | if (!mn.IsEnabled()) { |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | uint256 n = mn.CalculateScore(1, nBlockHeight); |
| 524 | unsigned int n2 = 0; |
| 525 | memcpy(&n2, &n, sizeof(n2)); |
| 526 | |
| 527 | vecMasternodeScores.push_back(make_pair(n2, mn.vin)); |
| 528 | } |
| 529 | |
| 530 | sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareValueOnly()); |
| 531 | |
| 532 | unsigned int rank = 0; |
| 533 | for (PAIRTYPE(unsigned int, CTxIn) &s : vecMasternodeScores) { |
| 534 | rank++; |
| 535 | if (s.second == vin) { |
| 536 | return rank; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | //Get the last hash that matches the modulus given. Processed in reverse order |
| 544 | bool GetBlockHash(uint256& hash, int nBlockHeight) { |
| 545 | if (chainActive.Tip() == NULL) return false; |
| 546 | |
| 547 | if (nBlockHeight == 0) |
| 548 | nBlockHeight = chainActive.Height(); |
| 549 | |
| 550 | if (mapCacheBlockHashes.count(nBlockHeight)) { |
| 551 | hash = mapCacheBlockHashes[nBlockHeight]; |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | const CBlockIndex* BlockLastSolved = chainActive.Tip(); |
| 556 | const CBlockIndex* BlockReading = chainActive.Tip(); |
| 557 | |
| 558 | if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || chainActive.Height() + 1 < nBlockHeight) return false; |
| 559 | |
| 560 | int nBlocksAgo = 0; |
| 561 | if (nBlockHeight > 0) nBlocksAgo = (chainActive.Height() + 1) - nBlockHeight; |
| 562 | assert(nBlocksAgo >= 0); |
| 563 | |
| 564 | int n = 0; |
| 565 | for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { |
| 566 | if (n >= nBlocksAgo) { |
| 567 | hash = BlockReading->GetBlockHash(); |
| 568 | mapCacheBlockHashes[nBlockHeight] = hash; |
no test coverage detected