From 44706dc88acf2eab07695f5093bda31ab278d2bf Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Wed, 4 Apr 2018 17:20:54 +0200 Subject: [PATCH] Implement projection of MN reward winners in "masternode winners" --- src/masternode-payments.cpp | 72 ++++++++++++++++++++++++++----------- src/masternode-payments.h | 2 +- src/rpc/masternode.cpp | 8 ++--- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index bc1e08087..dc50651fd 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -254,13 +254,10 @@ void FillBlockPayments(CMutableTransaction& txNew, int nBlockHeight, CAmount blo nBlockHeight, blockReward, voutMasternodeStr, txNew.ToString()); } -std::string GetRequiredPaymentsString(int nBlockHeight) +std::string GetLegacyRequiredPaymentsString(int nBlockHeight) { // IF WE HAVE A ACTIVATED TRIGGER FOR THIS HEIGHT - IT IS A SUPERBLOCK, GET THE REQUIRED PAYEES if(CSuperblockManager::IsSuperblockTriggered(nBlockHeight)) { - if (deterministicMNManager->IsDeterministicMNsSporkActive(nBlockHeight)) { - return mnpayments.GetRequiredPaymentsString(nBlockHeight) + ", " + CSuperblockManager::GetRequiredPaymentsString(nBlockHeight); - } return CSuperblockManager::GetRequiredPaymentsString(nBlockHeight); } @@ -268,6 +265,54 @@ std::string GetRequiredPaymentsString(int nBlockHeight) return mnpayments.GetRequiredPaymentsString(nBlockHeight); } +std::string GetRequiredPaymentsString(int nBlockHeight, const CDeterministicMNCPtr &payee) +{ + std::string strPayee = "Unknown"; + if (payee) { + CTxDestination dest; + if (!ExtractDestination(payee->pdmnState->scriptPayout, dest)) + assert(false); + strPayee = CBitcoinAddress(dest).ToString(); + } + if (CSuperblockManager::IsSuperblockTriggered(nBlockHeight)) { + strPayee += ", " + CSuperblockManager::GetRequiredPaymentsString(nBlockHeight); + } + return strPayee; +} + +std::map GetRequiredPaymentsStrings(int nStartHeight, int nEndHeight) +{ + std::map mapPayments; + + LOCK(cs_main); + int nChainTipHeight = chainActive.Height(); + + bool doProjection = false; + for(int h = nStartHeight; h < nEndHeight; h++) { + if (deterministicMNManager->IsDeterministicMNsSporkActive(h)) { + if (h <= nChainTipHeight) { + auto payee = deterministicMNManager->GetListForBlock(chainActive[h - 1]->GetBlockHash()).GetMNPayee(); + mapPayments.emplace(h, GetRequiredPaymentsString(h, payee)); + } else { + doProjection = true; + break; + } + } else { + mapPayments.emplace(h, GetLegacyRequiredPaymentsString(h)); + } + } + if (doProjection) { + auto projection = deterministicMNManager->GetListAtChainTip().GetProjectedMNPayees(nEndHeight - nChainTipHeight); + for (size_t i = 0; i < projection.size(); i++) { + auto payee = projection[i]; + int h = nChainTipHeight + 1 + i; + mapPayments.emplace(h, GetRequiredPaymentsString(h, payee)); + } + } + + return mapPayments; +} + void CMasternodePayments::Clear() { LOCK2(cs_mapMasternodeBlocks, cs_mapMasternodePaymentVotes); @@ -731,22 +776,9 @@ std::string CMasternodeBlockPayees::GetRequiredPaymentsString() const std::string CMasternodePayments::GetRequiredPaymentsString(int nBlockHeight) const { - if (deterministicMNManager->IsDeterministicMNsSporkActive(nBlockHeight)) { - LOCK(cs_main); - auto pindex = chainActive[nBlockHeight - 1]; - auto payee = deterministicMNManager->GetListForBlock(pindex->GetBlockHash()).GetMNPayee(); - if (!payee) { - return "Unknown"; - } - CTxDestination dest; - if (!ExtractDestination(payee->pdmnState->scriptPayout, dest)) - assert(false); - return CBitcoinAddress(dest).ToString(); - } else { - LOCK(cs_mapMasternodeBlocks); - const auto it = mapMasternodeBlocks.find(nBlockHeight); - return it == mapMasternodeBlocks.end() ? "Unknown" : it->second.GetRequiredPaymentsString(); - } + LOCK(cs_mapMasternodeBlocks); + const auto it = mapMasternodeBlocks.find(nBlockHeight); + return it == mapMasternodeBlocks.end() ? "Unknown" : it->second.GetRequiredPaymentsString(); } bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward) const diff --git a/src/masternode-payments.h b/src/masternode-payments.h index e5cc6698b..4c4a3c7ac 100644 --- a/src/masternode-payments.h +++ b/src/masternode-payments.h @@ -36,7 +36,7 @@ extern CMasternodePayments mnpayments; bool IsBlockValueValid(const CBlock& block, int nBlockHeight, CAmount blockReward, std::string& strErrorRet); bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward); void FillBlockPayments(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, std::vector& voutMasternodePaymentsRet, std::vector& voutSuperblockPaymentsRet); -std::string GetRequiredPaymentsString(int nBlockHeight); +std::map GetRequiredPaymentsStrings(int nStartHeight, int nEndHeight); class CMasternodePayee { diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 1fe361fd8..3761bd5a4 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -801,11 +801,9 @@ UniValue masternode_winners(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'masternode winners ( \"count\" \"filter\" )'"); UniValue obj(UniValue::VOBJ); - - for (int i = nHeight - nLast; i < nHeight + 20; i++) { - std::string strPayment = GetRequiredPaymentsString(i); - if (strFilter !="" && strPayment.find(strFilter) == std::string::npos) continue; - obj.push_back(Pair(strprintf("%d", i), strPayment)); + auto mapPayments = GetRequiredPaymentsStrings(nHeight - nLast, nHeight + 20); + for (const auto &p : mapPayments) { + obj.push_back(Pair(strprintf("%d", p.first), p.second)); } return obj;