diff --git a/src/main.cpp b/src/main.cpp index 4ebfd943d..5a17873ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3243,7 +3243,6 @@ bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDis LogPrintf("%s : Update Masternode Last Paid Time - %d\n", __func__, chainActive.Tip()->nHeight); } - darkSendPool.NewBlock(); masternodePayments.ProcessBlock(GetHeight()+10); mnscan.DoMasternodePOSChecks(); diff --git a/src/masternode-budget.cpp b/src/masternode-budget.cpp index 6b9efabb6..609bcebb7 100644 --- a/src/masternode-budget.cpp +++ b/src/masternode-budget.cpp @@ -19,6 +19,13 @@ std::map mapMasternodeBudgetVotes; std::map mapFinalizedBudgets; std::map mapFinalizedBudgetVotes; +int GetBudgetPaymentCycleBlocks(){ + if(Params().NetworkID() == CBaseChainParams::MAIN) return 16616; //(60*24*30)/2.6 + + //for testing purposes + return 50; +} + // // CBudgetDB // @@ -245,9 +252,9 @@ CBudgetProposalBroadcast::CBudgetProposalBroadcast(CTxIn vinIn, std::string strP nBlockStart = nBlockStartIn; - int nCycleStart = (nBlockStart-(nBlockStart % PAYMENT_CYCLE_BLOCKS)); + int nCycleStart = (nBlockStart-(nBlockStart % GetBudgetPaymentCycleBlocks())); //calculate the end of the cycle for this vote, add half a cycle (vote will be deleted after that block) - nBlockEnd = nCycleStart + (PAYMENT_CYCLE_BLOCKS*nPaymentCount) + PAYMENT_CYCLE_BLOCKS/2; + nBlockEnd = nCycleStart + (GetBudgetPaymentCycleBlocks()*nPaymentCount) + GetBudgetPaymentCycleBlocks()/2; address = addressIn; nAmount = nAmountIn; @@ -288,7 +295,7 @@ bool CBudgetProposal::IsValid() if(pindexPrev->nHeight - nBlockStart < 0) return false; if(pindexPrev->nHeight > nBlockEnd) return false; - if(nBlockEnd - PAYMENT_CYCLE_BLOCKS <= nBlockStart) return false; + if(nBlockEnd - GetBudgetPaymentCycleBlocks() <= nBlockStart) return false; return true; } @@ -528,8 +535,7 @@ void CBudgetManager::UpdateProposal(CBudgetVote& vote) { LOCK(cs); if(!mapProposals.count(vote.nProposalHash)){ - //LogPrintf("Unknown proposal %d\n", vote.nProposalHash); - //should prob ask for it + LogPrintf("ERROR : Unknown proposal %d\n", vote.nProposalHash.ToString().c_str()); return; } @@ -613,7 +619,7 @@ int CBudgetProposal::GetBlockStartCycle() { //end block is half way through the next cycle (so the proposal will be removed much after the payment is sent) - return (nBlockStart-(nBlockStart % PAYMENT_CYCLE_BLOCKS)); + return (nBlockStart-(nBlockStart % GetBudgetPaymentCycleBlocks())); } int CBudgetProposal::GetBlockCurrentCycle() @@ -623,35 +629,24 @@ int CBudgetProposal::GetBlockCurrentCycle() if(pindexPrev->nHeight >= GetBlockEndCycle()) return -1; - return (pindexPrev->nHeight-(pindexPrev->nHeight % PAYMENT_CYCLE_BLOCKS)); + return (pindexPrev->nHeight-(pindexPrev->nHeight % GetBudgetPaymentCycleBlocks())); } int CBudgetProposal::GetBlockEndCycle() { //end block is half way through the next cycle (so the proposal will be removed much after the payment is sent) - return nBlockEnd-(PAYMENT_CYCLE_BLOCKS/2); + return nBlockEnd-(GetBudgetPaymentCycleBlocks()/2); } int CBudgetProposal::GetPaymentCountTotal() { - return (GetBlockEndCycle()-GetBlockStartCycle())/PAYMENT_CYCLE_BLOCKS; + return (GetBlockEndCycle()-GetBlockStartCycle())/GetBudgetPaymentCycleBlocks(); } int CBudgetProposal::GetPaymentCountLeft() { - return (GetBlockEndCycle()-GetBlockCurrentCycle())/PAYMENT_CYCLE_BLOCKS; -} - -bool CBudgetManager::IsBudgetPaymentBlock(int nHeight) -{ - if(nHeight > 158000+((576*30)*15)) return (nHeight % 7) == 0 ; // 417200 - 57.5% - 2016-02-08 -- 12.5% of blocks - if(nHeight > 158000+((576*30)*13)) return (nHeight % 10) == 0 ; // 382640 - 55.0% - 2015-12-07 -- 10.0% of blocks - if(nHeight > 158000+((576*30)*11)) return (nHeight % 13) == 0 ; // 348080 - 52.5% - 2015-10-05 -- 7.5 of blocks - if(nHeight > 158000+((576*30)* 9)) return (nHeight % 20) == 0 ; // 313520 - 50.0% - 2015-08-03 -- 5.0% of blocks - if(nHeight > 158000+((576*30)* 7)) return (nHeight % 40) == 0 ; // 278960 - 47.5% - 2015-06-01 -- 2.5% of blocks - - return false; + return (GetBlockEndCycle()-GetBlockCurrentCycle())/GetBudgetPaymentCycleBlocks(); } int64_t CBudgetManager::GetTotalBudget() @@ -831,10 +826,11 @@ CFinalizedBudget *CBudgetManager::Find(uint256 nHash) bool CBudgetManager::PropExists(uint256 nHash) { - return mapProposals.count(nHash); + if(mapProposals.count(nHash)) return true; + return false; } -bool CBudgetManager::IsBudgetBlock(int nBlockHeight){ +bool CBudgetManager::IsBudgetPaymentBlock(int nBlockHeight){ std::map::iterator it = mapFinalizedBudgets.begin(); while(it != mapFinalizedBudgets.end()) { @@ -966,8 +962,7 @@ bool CFinalizedBudgetBroadcast::SignatureValid() bool CFinalizedBudget::IsValid() { //must be the correct block for payment to happen (once a month) - if(nBlockStart % PAYMENT_CYCLE_BLOCKS != 0) return false; - + if(nBlockStart % GetBudgetPaymentCycleBlocks() != 0) return false; if(GetBlockEnd() - nBlockStart > 100) return false; //make sure all prop names exist @@ -1073,7 +1068,8 @@ void CBudgetManager::UpdateFinalizedBudget(CFinalizedBudgetVote& vote) LOCK(cs); if(!mapFinalizedBudgets.count(vote.nBudgetHash)){ - //LogPrintf("Unknown proposal %d\n", vote.nBudgetHash); + LogPrintf("ERROR: Unknown Finalized Proposal %s\n", vote.nBudgetHash.ToString().c_str()); + //should ask for it return; } @@ -1152,4 +1148,28 @@ CFinalizedBudgetBroadcast::CFinalizedBudgetBroadcast(CTxIn& vinIn, std::string s nBlockStart = nBlockStartIn; BOOST_FOREACH(uint256 hash, vecProposalsIn) vecProposals.push_back(hash); mapVotes.clear(); +} + +std::string CBudgetManager::GetRequiredPaymentsString(int64_t nBlockHeight) +{ + std::string ret = "unknown-budget"; + + std::map::iterator it = mapFinalizedBudgets.begin(); + while(it != mapFinalizedBudgets.end()) + { + CFinalizedBudget* prop = &((*it).second); + if(nBlockHeight >= prop->GetBlockStart() && nBlockHeight <= prop->GetBlockEnd()){ + uint256 nPropHash = prop->GetProposalByBlock(nBlockHeight); + if(ret == "unknown-budget"){ + ret = nPropHash.ToString().c_str(); + } else { + ret += ","; + ret += nPropHash.ToString().c_str(); + } + } + + it++; + } + + return ret; } \ No newline at end of file diff --git a/src/masternode-budget.h b/src/masternode-budget.h index b39077885..cf03943d4 100644 --- a/src/masternode-budget.h +++ b/src/masternode-budget.h @@ -34,14 +34,14 @@ extern std::map mapMasternodeBudgetVotes; extern std::map mapFinalizedBudgets; extern std::map mapFinalizedBudgetVotes; -//Amount of blocks in a months period of time (using 2.6 minutes per) -#define PAYMENT_CYCLE_BLOCKS 16615 //(60*24*30)/2.6 - extern CBudgetManager budget; void DumpBudgets(); void GetMasternodeBudgetEscrow(CScript& payee); +//Amount of blocks in a months period of time (using 2.6 minutes per) +int GetBudgetPaymentCycleBlocks(); + /** Save Budget Manager (budget.dat) */ class CBudgetDB @@ -93,20 +93,21 @@ public: CBudgetProposal *Find(const std::string &strProposalName); CFinalizedBudget *Find(uint256 nHash); std::pair GetVotes(std::string strProposalName); + void CleanUp(); int64_t GetTotalBudget(); std::vector GetBudget(); std::vector GetFinalizedBudgets(); - bool IsBudgetPaymentBlock(int nHeight); + bool IsBudgetPaymentBlock(int nBlockHeight); void AddProposal(CBudgetProposal& prop); void UpdateProposal(CBudgetVote& vote); void AddFinalizedBudget(CFinalizedBudget& prop); void UpdateFinalizedBudget(CFinalizedBudgetVote& vote); bool PropExists(uint256 nHash); bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight); - bool IsBudgetBlock(int nBlockHeight); + std::string GetRequiredPaymentsString(int64_t nBlockHeight); void Clear(){ printf("Not implemented\n"); @@ -169,6 +170,13 @@ public: std::string GetSubmittedBy() {return vin.prevout.ToStringShort();} int GetVoteCount() {return (int)mapVotes.size();} bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight); + uint256 GetProposalByBlock(int64_t nBlockHeight) + { + int i = nBlockHeight - GetBlockStart(); + if(i < 0) return 0; + if(i > (int)vecProposals.size()-1) return 0; + return vecProposals[i]; + } uint256 GetHash(){ /* diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index d4c044b08..147fe1d20 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -21,7 +21,7 @@ map mapMasternodeBlocks; bool IsBlockPayeeValid(const CTransaction& txNew, int64_t nBlockHeight) { //check if it's a budget block - if(budget.IsBudgetBlock(nBlockHeight)){ + if(budget.IsBudgetPaymentBlock(nBlockHeight)){ if(budget.IsTransactionValid(txNew, nBlockHeight)){ return true; } @@ -36,6 +36,15 @@ bool IsBlockPayeeValid(const CTransaction& txNew, int64_t nBlockHeight) return false; } +std::string GetRequiredPaymentsString(int64_t nBlockHeight) +{ + if(budget.IsBudgetPaymentBlock(nBlockHeight)){ + return budget.GetRequiredPaymentsString(nBlockHeight); + } else { + return masternodePayments.GetRequiredPaymentsString(nBlockHeight); + } +} + void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv) { if(IsInitialBlockDownload()) return; @@ -54,7 +63,6 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st LogPrintf("mnget - Sent Masternode winners to %s\n", pfrom->addr.ToString().c_str()); } else if (strCommand == "mnw") { //Masternode Payments Declare Winner - LOCK(cs_masternodepayments); //this is required in litemode @@ -130,7 +138,7 @@ bool CMasternodePayments::GetBlockPayee(int nBlockHeight, CScript& payee) bool CMasternodePayments::AddWinningMasternode(CMasternodePaymentWinner& winnerIn) { uint256 blockHash = 0; - if(!GetBlockHash(blockHash, winnerIn.nBlockHeight-576)) { + if(!GetBlockHash(blockHash, winnerIn.nBlockHeight-100)) { return false; } @@ -203,16 +211,14 @@ std::string CMasternodeBlockPayees::GetRequiredPaymentsString() BOOST_FOREACH(CMasternodePayee& payee, vecPayments) { - if(payee.nVotes >= MNPAYMENTS_SIGNATURES_REQUIRED){ - CTxDestination address1; - ExtractDestination(payee.scriptPubKey, address1); - CBitcoinAddress address2(address1); + CTxDestination address1; + ExtractDestination(payee.scriptPubKey, address1); + CBitcoinAddress address2(address1); - if(ret != "Unknown"){ - ret += ", " + address2.ToString() + ":" + boost::lexical_cast(payee.nValue); - } else { - ret = address2.ToString() + ":" + boost::lexical_cast(payee.nValue); - } + if(ret != "Unknown"){ + ret += ", " + address2.ToString() + ":" + boost::lexical_cast(payee.nValue); + } else { + ret = address2.ToString() + ":" + boost::lexical_cast(payee.nValue); } } @@ -272,7 +278,7 @@ bool CMasternodePaymentWinner::IsValid() { if(IsReferenceNode(vinMasternode)) return true; - int n = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight, MIN_MNPAYMENTS_PROTO_VERSION); + int n = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight-100, MIN_MNPAYMENTS_PROTO_VERSION); if(n == -1) { @@ -293,13 +299,12 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) { LOCK(cs_masternodepayments); - if(!fMasterNode) return false; //reference node - hybrid mode if(!IsReferenceNode(activeMasternode.vin)){ - int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight, MIN_MNPAYMENTS_PROTO_VERSION); + int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight-100, MIN_MNPAYMENTS_PROTO_VERSION); if(n == -1) { @@ -330,10 +335,9 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) newWinner.AddPayee(payee, masternodePayment); } else { - CScript payeeSource; uint256 hash; - if(!GetBlockHash(hash, nBlockHeight-10)) return false; + if(!GetBlockHash(hash, nBlockHeight-100)) return false; unsigned int nHash; memcpy(&nHash, &hash, 2); @@ -353,7 +357,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) } else { payee = GetScriptForDestination(pmn->pubkey.GetID()); } - + payeeSource = GetScriptForDestination(pmn->pubkey.GetID()); newWinner.AddPayee(payee, masternodePayment); diff --git a/src/masternode-payments.h b/src/masternode-payments.h index 7e91f7598..0bd19ce3a 100644 --- a/src/masternode-payments.h +++ b/src/masternode-payments.h @@ -27,6 +27,7 @@ static const int MIN_MNPAYMENTS_PROTO_VERSION = 70066; void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); bool IsReferenceNode(CTxIn& vin); bool IsBlockPayeeValid(const CTransaction& txNew, int64_t nBlockHeight); +std::string GetRequiredPaymentsString(int64_t nBlockHeight); class CMasternodePayee : public CTxOut { @@ -37,11 +38,11 @@ public: scriptPubKey = CScript(); nValue = 0; nVotes = 0; - } + } - CMasternodePayee(CAmount nValue, CScript payee) { + CMasternodePayee(CAmount nValueIn, CScript payee) { scriptPubKey = payee; - nValue = nValue; + nValue = nValueIn; nVotes = 0; } }; diff --git a/src/rpcmasternode-budget.cpp b/src/rpcmasternode-budget.cpp index c15f56eb8..0bac700b6 100644 --- a/src/rpcmasternode-budget.cpp +++ b/src/rpcmasternode-budget.cpp @@ -63,7 +63,7 @@ Value mnbudget(const Array& params, bool fHelp) return "Invalid payment count, must be more than zero."; //set block min - if(pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - PAYMENT_CYCLE_BLOCKS*(nPaymentCount+1); + if(pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - GetBudgetPaymentCycleBlocks()*(nPaymentCount+1); int nBlockStart = params[4].get_int(); if(nBlockStart < nBlockMin) @@ -159,7 +159,7 @@ Value mnbudget(const Array& params, bool fHelp) return "Invalid payment count, must be more than zero."; //set block min - if(pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - PAYMENT_CYCLE_BLOCKS*(nPaymentCount+1); + if(pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - GetBudgetPaymentCycleBlocks()*(nPaymentCount+1); int nBlockStart = params[4].get_int(); if(nBlockStart < nBlockMin) @@ -195,6 +195,7 @@ Value mnbudget(const Array& params, bool fHelp) mapMasternodeBudgetProposals.insert(make_pair(prop.GetHash(), prop)); prop.Relay(); + budget.AddProposal(prop); CBudgetVote vote(activeMasternode.vin, prop.GetHash(), nVote); if(!vote.Sign(keyMasternode, pubKeyMasternode)){ @@ -203,6 +204,7 @@ Value mnbudget(const Array& params, bool fHelp) mapMasternodeBudgetVotes.insert(make_pair(vote.GetHash(), vote)); vote.Relay(); + budget.UpdateProposal(vote); } @@ -317,7 +319,6 @@ Value mnfinalbudget(const Array& params, bool fHelp) " show - Show existing finalized budgets\n" ); - if(strCommand == "suggest") { std::vector mnEntries; @@ -328,22 +329,21 @@ Value mnfinalbudget(const Array& params, bool fHelp) return "Must be synced to suggest"; if (params.size() < 3) - throw runtime_error("Correct usage of vote-many is 'mnfinalbudget suggest BUDGET_NAME PROPNAME [PROP2 PROP3 PROP4]'"); + throw runtime_error("Correct usage of suggest is 'mnfinalbudget suggest BUDGET_NAME PROPNAME [PROP2 PROP3 PROP4]'"); std::string strBudgetName = params[1].get_str(); if(strBudgetName.size() > 20) return "Invalid budget name, limit of 20 characters."; - int nBlockStart = pindexPrev->nHeight-(pindexPrev->nHeight % PAYMENT_CYCLE_BLOCKS)+PAYMENT_CYCLE_BLOCKS; + int nBlockStart = pindexPrev->nHeight-(pindexPrev->nHeight % GetBudgetPaymentCycleBlocks())+GetBudgetPaymentCycleBlocks(); std::vector vecProps; - for(int i = 3; i < (int)params.size(); i++) + for(int i = 2; i < (int)params.size(); i++) { - std::string strHash = params[1].get_str(); + std::string strHash = params[i].get_str(); uint256 hash(strHash); vecProps.push_back(hash); - printf("%s\n", hash.ToString().c_str()); } CPubKey pubKeyMasternode; @@ -364,6 +364,7 @@ Value mnfinalbudget(const Array& params, bool fHelp) mapFinalizedBudgets.insert(make_pair(prop.GetHash(), prop)); prop.Relay(); + budget.AddFinalizedBudget(prop); CFinalizedBudgetVote vote(activeMasternode.vin, prop.GetHash()); if(!vote.Sign(keyMasternode, pubKeyMasternode)){ @@ -372,6 +373,7 @@ Value mnfinalbudget(const Array& params, bool fHelp) mapFinalizedBudgetVotes.insert(make_pair(vote.GetHash(), vote)); vote.Relay(); + budget.UpdateFinalizedBudget(vote); return "success"; @@ -426,6 +428,7 @@ Value mnfinalbudget(const Array& params, bool fHelp) mapFinalizedBudgetVotes.insert(make_pair(vote.GetHash(), vote)); vote.Relay(); + budget.UpdateFinalizedBudget(vote); success++; } @@ -458,6 +461,7 @@ Value mnfinalbudget(const Array& params, bool fHelp) mapFinalizedBudgetVotes.insert(make_pair(vote.GetHash(), vote)); vote.Relay(); + budget.UpdateFinalizedBudget(vote); return "success"; } diff --git a/src/rpcmasternode.cpp b/src/rpcmasternode.cpp index d7c5efafe..15e2b5822 100644 --- a/src/rpcmasternode.cpp +++ b/src/rpcmasternode.cpp @@ -396,7 +396,7 @@ Value masternode(const Array& params, bool fHelp) for(int nHeight = chainActive.Tip()->nHeight-10; nHeight < chainActive.Tip()->nHeight+20; nHeight++) { - obj.push_back(Pair(boost::lexical_cast(nHeight), masternodePayments.GetRequiredPaymentsString(nHeight).c_str())); + obj.push_back(Pair(boost::lexical_cast(nHeight), GetRequiredPaymentsString(nHeight).c_str())); } return obj;