From b02d5260fdc03d560ed9ab3162150bc0b0ae8fe7 Mon Sep 17 00:00:00 2001 From: Evan Duffield Date: Thu, 30 Apr 2015 10:11:34 -0700 Subject: [PATCH] fixed most of the budget functions --- src/masternode-budget.cpp | 238 ++++++++++++++++----------------- src/masternode-budget.h | 10 +- src/primitives/transaction.cpp | 5 + src/primitives/transaction.h | 2 + src/rpcmasternode-budget.cpp | 12 +- 5 files changed, 137 insertions(+), 130 deletions(-) diff --git a/src/masternode-budget.cpp b/src/masternode-budget.cpp index 5ebf71552..e84fd1b5e 100644 --- a/src/masternode-budget.cpp +++ b/src/masternode-budget.cpp @@ -308,31 +308,31 @@ bool CBudgetVote::SignatureValid() void CBudgetManager::AddOrUpdateProposal(CBudgetVote& vote) { - // LOCK(cs); + LOCK(cs); - // uint256 hash = GetBudgetProposalHash(vote.strProposalName); + uint256 hash = GetBudgetProposalHash(vote.strProposalName); - // if(!mapProposals.count(hash)){ - // CBudgetProposal prop(vote.strProposalName); - // mapProposals.insert(make_pair(hash, prop)); - // return; - // } + if(!mapProposals.count(hash)){ + CBudgetProposal prop(vote.strProposalName); + //mapProposals.insert(make_pair(hash, prop)); + return; + } - // mapProposals[hash].AddOrUpdateVote(vote); + mapProposals[hash].AddOrUpdateVote(vote); } void CBudgetProposal::AddOrUpdateVote(CBudgetVote& vote) { - // LOCK(cs); + LOCK(cs); - // uint256 hash = vote.vin.prevout.GetHash(); + uint256 hash = vote.vin.prevout.GetHash(); - // if(!mapProposals.count(hash)){ - // mapProposals.insert(make_pair(hash, vote)); - // return; - // } + if(!mapVotes.count(hash)){ + mapVotes.insert(make_pair(hash, vote)); + return; + } - // vote[hash] = vote; + mapVotes[hash] = vote; } inline void CBudgetManager::NewBlock() @@ -345,11 +345,11 @@ inline void CBudgetManager::NewBlock() CBudgetProposal *CBudgetManager::Find(const std::string &strProposalName) { - // uint256 hash = GetBudgetProposalHash(vote.strProposalName); + uint256 hash = GetBudgetProposalHash(strProposalName); - // if(mapProposals.count(hash)){ - // return &mapProposals[hash]; - // } + if(mapProposals.count(hash)){ + return &mapProposals[hash]; + } return NULL; } @@ -359,6 +359,11 @@ CBudgetProposal::CBudgetProposal() strProposalName = ""; } +CBudgetProposal::CBudgetProposal(const CBudgetProposal& other) +{ + strProposalName = other.strProposalName; +} + CBudgetProposal::CBudgetProposal(std::string strProposalNameIn) { strProposalName = strProposalNameIn; @@ -371,38 +376,43 @@ std::string CBudgetProposal::GetName() int CBudgetProposal::GetBlockStart() { - /*std::map mapList; + std::map mapList; - map::iterator it; - for(it=mapVotes.begin();itsecond.mapVotes.nVote != VOTE_YEA) continue; - if(mapList.count((*it).nBlockStart)){ - mapList[(*it).nBlockStart]++; + std::map::iterator it = mapVotes.begin(); + + while(it != mapVotes.end()) { + if ((*it).second.nVote != VOTE_YES) continue; + + if(mapList.count((*it).second.nBlockStart)){ + mapList[(*it).second.nBlockStart]++; } else { - mapList[(*it)].insert(make_pair<(*it).nBlockStart, 1>); + mapList[(*it).second.nBlockStart] = 1; } - }*/ + } + //sort(mapList.begin(), mapList.end()); - // return myMap.begin()->second; + //return myMap.begin()->second; + return 0; } int CBudgetProposal::GetBlockEnd() { std::map mapList; + + std::map::iterator it = mapVotes.begin(); - // map::iterator it; - // for(it=mapVotes.begin();itsecond.nVote != VOTE_YEA) continue; + while(it != mapVotes.end()) { + if ((*it).second.nVote != VOTE_YES) continue; - // if(mapList.count((*it).nBlockEnd)){ - // mapList[(*it).nBlockEnd]++; - // } else { - // mapList[(*it).nBlockEnd] = 1; - // } - // } + if(mapList.count((*it).second.nBlockEnd)){ + mapList[(*it).second.nBlockEnd]++; + } else { + mapList[(*it).second.nBlockEnd] = 1; + } + } // sort(mapList.begin(), mapList.end()); // return myMap.begin()->first; @@ -413,44 +423,47 @@ int64_t CBudgetProposal::GetAmount() { std::map mapList; - // map::iterator it; - // for(it=mapVotes.begin();itsecond.nVote != VOTE_YEA) continue; + std::map::iterator it = mapVotes.begin(); - // if(mapList.count((*it).nAmount)){ - // mapList[(*it).nAmount]++; - // } else { - // mapList[(*it).nAmount] = 1; - // } - // } + while(it != mapVotes.end()) { + if ((*it).second.nVote != VOTE_YES) continue; + + if(mapList.count((*it).second.nAmount)){ + mapList[(*it).second.nAmount]++; + } else { + mapList[(*it).second.nAmount] = 1; + } + } // sort(mapList.begin(), mapList.end()); // return myMap.begin()->first; return 0; } -std::string CBudgetProposal::GetPaymentAddress() +CScript CBudgetProposal::GetPayee() { - std::map mapList; + std::map mapList; - // map::iterator it; - // for(it=prop->mapVotes.begin();itmapVotes.end();it++){ - // if ((*it)->second.nVote != VOTE_YEA) continue; + std::map::iterator it = mapVotes.begin(); - // CTxDestination address1; - // ExtractDestination((*it)->second.payee, address1); - // CBitcoinAddress address2(address1); + while(it != mapVotes.end()) { + if ((*it).second.nVote != VOTE_YES) continue; - // if(mapList.count(address2){ - // mapList[address2]++; - // } else { - // mapList[address2] = 1; - // } - // } + CTxDestination address1; + ExtractDestination((*it).second.address, address1); + CBitcoinAddress address2(address1); + if(mapList.count((*it).second.address)){ + mapList[(*it).second.address]++; + } else { + mapList[(*it).second.address] = 1; + } + + } + // sort(mapList.begin(), mapList.end()); // return myMap.begin()->first; - return 0; + return CScript(); } double CBudgetProposal::GetRatio() @@ -458,11 +471,12 @@ double CBudgetProposal::GetRatio() int yeas = 0; int nays = 0; - // map::iterator it; - // for(it=mapVotes.begin();itsecond.prop->mapVotes.nVote != VOTE_YEA) yeas++; - // if ((*it)->second.nVote != VOTE_NAY) nays++; - // } + std::map::iterator it = mapVotes.begin(); + + while(it != mapVotes.end()) { + if ((*it).second.nVote == VOTE_YES) yeas++; + if ((*it).second.nVote == VOTE_NO) nays++; + } return ((double)yeas / (double)yeas+nays); } @@ -471,9 +485,9 @@ int CBudgetProposal::GetYeas() { int ret = 0; - // map::iterator it; - // for(it=mapVotes.begin();itsecond.mapVotes.nVote != VOTE_YEA) ret++; + std::map::iterator it = mapVotes.begin(); + while(it != mapVotes.end()) + if ((*it).second.nVote == VOTE_YES) ret++; return ret; } @@ -482,9 +496,9 @@ int CBudgetProposal::GetNays() { int ret = 0; - // map::iterator it; - // for(it=mapVotes.begin();itsecond.nVote != VOTE_NAY) ret++; + std::map::iterator it = mapVotes.begin(); + while(it != mapVotes.end()) + if ((*it).second.nVote == VOTE_NO) ret++; return ret; } @@ -493,9 +507,9 @@ int CBudgetProposal::GetAbstains() { int ret = 0; - // map::iterator it; - // for(it=mapVotes.begin();itsecond.mapVotes.nVote != VOTE_ABSTAIN) ret++; + std::map::iterator it = mapVotes.begin(); + while(it != mapVotes.end()) + if ((*it).second.nVote == VOTE_ABSTAIN) ret++; return ret; } @@ -511,28 +525,6 @@ bool CBudgetManager::IsBudgetPaymentBlock(int nHeight) return false; } -void CBudgetManager::FillBlockTx(CMutableTransaction& txNew, int& payments) -{ -/* std::vector budget = GetBudget(); - - LogPrintf("Masternode Budget payment to"); - - BOOST_FOREACH(CBudgetProposal* prop, budget){ - payments++; - txNew.vout.resize(payments); - - txNew.vout[payments-1].scriptPubKey = prop.GetPayee(); - txNew.vout[payments-1].nValue = prop.GetAmount(); - - CTxDestination address1; - ExtractDestination(pblock->payee, address1); - CBitcoinAddress address2(address1); - - LogPrintf(" (%s|%d)", address2.ToString().c_str(), prop.GetAmount()); - } - LogPrintf("\n");*/ -} - int64_t CBudgetManager::GetTotalBudget() { if(chainActive.Tip() == NULL) return 0; @@ -541,40 +533,41 @@ int64_t CBudgetManager::GetTotalBudget() return (GetBlockValue(pindex->pprev->nBits, pindex->pprev->nHeight, 0)/100)*15; } +//Need to review this function std::vector CBudgetManager::GetBudget() { -/* std::map mapList; + std::map mapList; - map::iterator it; - for(it=prop->mapProposals.begin();itmapProposals.end();it++){ - uint256 hash = GetBudgetProposalHash((*it)->second.strProposalName); - if(!mapList.count(hash)){ - mapList[hash] = (*it)->second.GetYeas(); - } +/* std::map::iterator it = mapProposals.begin(); + while(it != mapProposals.end()) + uint256 hash = GetBudgetProposalHash((*it).second.strProposalName); + mapList[hash] = (*it).second.GetYeas(); } //sort by yeas - sort(mapList.begin(), mapList.end());*/ + sort(mapList.begin(), mapList.end()); + */ std::vector ret; - -/* int64_t nBudgetAllocated = 0; + /* + int64_t nBudgetAllocated = 0; int64_t nTotalBudget = GetTotalBudget(); - map::iterator it; - for(it=prop->mapProposals.begin();itmapProposals.end();it++){ + std::map::iterator it = mapProposals.begin(); + while(it != mapProposals.end()) + { if(nTotalBudget == nBudgetAllocated){ - (*it).SetAllotted(0); - } else if((*it).GetAmount() + nBudgetAllocated <= nTotalBudget()) { - (*it).SetAllotted((*it).GetAmount()); - nBudgetAllocated += (*it).GetAmount(); + (*it).second.SetAllotted(0); + } else if((*it).second.GetAmount() + nBudgetAllocated <= nTotalBudget()) { + (*it).second..SetAllotted((*it).GetAmount()); + nBudgetAllocated += (*it).second.GetAmount(); } else { //couldn't pay for the entire budget, so it'll be partially paid. - (*it).SetAllotted(nTotalBudget - nBudgetAllocated); + (*it).second.SetAllotted(nTotalBudget - nBudgetAllocated); nBudgetAllocated = nTotalBudget; } - ret.insert(make_pair((*it).strProposalName, &(*it))) + ret.insert(make_pair((*it).second.strProposalName, &(*it))) }*/ return ret; } @@ -582,16 +575,15 @@ std::vector CBudgetManager::GetBudget() void CBudgetManager::Sync(CNode* node) { - // map::iterator it; - // for(it=prop->mapProposals.begin();itmapProposals.end();it++){ - // (*it)->second.Sync(node); - // } + std::map::iterator it = mapProposals.begin(); + while(it != mapProposals.end()) + (*it).second.Sync(node); + } void CBudgetProposal::Sync(CNode* node) { - // map::iterator it; - // for(it=mapVotes.begin();itPushMessage("mvote", mapVotes[(*it)->first]); - // } + std::map::iterator it = mapVotes.begin(); + while(it != mapVotes.end()) + node->PushMessage("mvote", (*it).second); } \ No newline at end of file diff --git a/src/masternode-budget.h b/src/masternode-budget.h index df3ab581d..f5f5b8bdc 100644 --- a/src/masternode-budget.h +++ b/src/masternode-budget.h @@ -28,7 +28,7 @@ class CBudgetVote; extern CBudgetManager budget; void DumpBudgets(); - +uint256 GetBudgetProposalHash(std::string strProposalName); /** Save Budget Manager (budget.dat) */ @@ -78,13 +78,12 @@ public: CBudgetProposal *Find(const std::string &strProposalName); inline void NewBlock(); std::pair GetVotes(std::string strProposalName); - uint256 GetBudgetProposalHash(std::string strProposalName); + void CleanUp(); int64_t GetTotalBudget(); std::vector GetBudget(); bool IsBudgetPaymentBlock(int nHeight); - void FillBlockTx(CMutableTransaction& txNew, int& payments); void AddOrUpdateProposal(CBudgetVote& vote); void Clear(){ @@ -114,7 +113,7 @@ class CBudgetProposal { private: // critical section to protect the inner data structures - //mutable CCriticalSection cs; + mutable CCriticalSection cs; int64_t nAlloted; public: @@ -124,6 +123,7 @@ public: //cache object CBudgetProposal(); + CBudgetProposal(const CBudgetProposal& other); CBudgetProposal(std::string strProposalNameIn); void Sync(CNode* node); @@ -136,7 +136,7 @@ public: std::string GetName(); int GetBlockStart(); int GetBlockEnd(); - std::string GetPaymentAddress(); + CScript GetPayee(); double GetRatio(); int GetYeas(); int GetNays(); diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index b51a30ed9..27c79cad0 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -19,6 +19,11 @@ std::string COutPoint::ToStringShort() const return strprintf("%s-%u", hash.ToString().substr(0,64), n); } +uint256 COutPoint::GetHash() +{ + return Hash(BEGIN(hash), END(hash), BEGIN(n), END(n)); +} + CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) { prevout = prevoutIn; diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index a9dc8defa..851bf7db8 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -49,6 +49,8 @@ public: std::string ToString() const; std::string ToStringShort() const; + uint256 GetHash(); + }; /** An input of a transaction. It contains the location of the previous diff --git a/src/rpcmasternode-budget.cpp b/src/rpcmasternode-budget.cpp index a810f08e8..e141f2120 100644 --- a/src/rpcmasternode-budget.cpp +++ b/src/rpcmasternode-budget.cpp @@ -166,11 +166,15 @@ Value mnbudget(const Array& params, bool fHelp) { nTotalAlloted += prop->GetAlloted(); + CTxDestination address1; + ExtractDestination(prop->GetPayee(), address1); + CBitcoinAddress address2(address1); + Object bObj; bObj.push_back(Pair("Name", prop->GetName().c_str())); bObj.push_back(Pair("BlockStart", (int64_t)prop->GetBlockStart())); bObj.push_back(Pair("BlockEnd", (int64_t)prop->GetBlockEnd())); - bObj.push_back(Pair("PaymentAddress", prop->GetPaymentAddress())); + bObj.push_back(Pair("PaymentAddress", address2.ToString().c_str())); bObj.push_back(Pair("Ratio", prop->GetRatio())); bObj.push_back(Pair("Yeas", (int64_t)prop->GetYeas())); bObj.push_back(Pair("Nays", (int64_t)prop->GetNays())); @@ -194,11 +198,15 @@ Value mnbudget(const Array& params, bool fHelp) if(prop == NULL) return "Unknown proposal name"; + CTxDestination address1; + ExtractDestination(prop->GetPayee(), address1); + CBitcoinAddress address2(address1); + Object obj; obj.push_back(Pair("Name", prop->GetName().c_str())); obj.push_back(Pair("BlockStart", (int64_t)prop->GetBlockStart())); obj.push_back(Pair("BlockEnd", (int64_t)prop->GetBlockEnd())); - obj.push_back(Pair("PaymentAddress", prop->GetPaymentAddress())); + obj.push_back(Pair("PaymentAddress", address2.ToString().c_str())); obj.push_back(Pair("Ratio", prop->GetRatio())); obj.push_back(Pair("Yeas", (int64_t)prop->GetYeas())); obj.push_back(Pair("Nays", (int64_t)prop->GetNays()));