From 192c64461dfe86c5b7ec62def347fd9018ba0c53 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 28 Aug 2012 15:35:47 -0700 Subject: [PATCH] Simplify and improve performance a bit. --- src/LedgerConsensus.cpp | 115 +++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 55 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 52e4173c1c..fdc822f7a1 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "../json/writer.h" @@ -19,6 +20,9 @@ // #define LC_DEBUG +typedef std::pair u160_prop_pair; +typedef std::pair u256_lct_pair; + TransactionAcquire::TransactionAcquire(const uint256& hash) : PeerSet(hash, TX_ACQUIRE_TIMEOUT), mHaveRoot(false) { mMap = boost::make_shared(); @@ -214,9 +218,9 @@ LedgerConsensus::LedgerConsensus(const uint256& prevLCLHash, const Ledger::point mHaveCorrectLCL = mProposing = mValidating = false; mAcquiringLedger = theApp->getMasterLedgerAcquire().findCreate(prevLCLHash); std::vector peerList = theApp->getConnectionPool().getPeerVector(); - for (std::vector::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) - if ((*it)->hasLedger(prevLCLHash)) - mAcquiringLedger->peerHas(*it); + BOOST_FOREACH(const Peer::pointer& peer, peerList) + if (peer->hasLedger(prevLCLHash)) + mAcquiringLedger->peerHas(peer); } else if (mValSeed.isValid()) { @@ -238,12 +242,16 @@ void LedgerConsensus::checkLCL() int netLgrCount = 0; { boost::unordered_map vals = theApp->getValidations().getCurrentValidations(); - for (boost::unordered_map::iterator it = vals.begin(), end = vals.end(); it != end; ++it) - if ((it->second > netLgrCount) && !theApp->getValidations().isDeadLedger(it->first)) + + typedef std::pair u256_int_pair; + BOOST_FOREACH(u256_int_pair& it, vals) + { + if ((it.second > netLgrCount) && !theApp->getValidations().isDeadLedger(it.first)) { - netLgr = it->first; - netLgrCount = it->second; + netLgr = it.first; + netLgrCount = it.second; } + } } if (netLgr != mPrevLedgerHash) { // LCL change @@ -255,15 +263,19 @@ void LedgerConsensus::checkLCL() mProposing = false; mValidating = false; bool found = false; - for (std::vector::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) - if ((*it)->hasLedger(mPrevLedgerHash)) + BOOST_FOREACH(Peer::pointer& peer, peerList) + { + if (peer->hasLedger(mPrevLedgerHash)) { found = true; - mAcquiringLedger->peerHas(*it); + mAcquiringLedger->peerHas(peer); } + } if (!found) - for (std::vector::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) - mAcquiringLedger->peerHas(*it); + { + BOOST_FOREACH(Peer::pointer& peer, peerList) + mAcquiringLedger->peerHas(peer); + } } } @@ -275,15 +287,14 @@ void LedgerConsensus::takeInitialPosition(Ledger& initialLedger) // if any peers have taken a contrary position, process disputes boost::unordered_set found; - for (boost::unordered_map::iterator it = mPeerPositions.begin(), - end = mPeerPositions.end(); it != end; ++it) + BOOST_FOREACH(u160_prop_pair& it, mPeerPositions) { - uint256 set = it->second->getCurrentHash(); + uint256 set = it.second->getCurrentHash(); if (found.insert(set).second) { - boost::unordered_map::iterator it = mComplete.find(set); - if (it != mComplete.end()) - createDisputes(initialSet, it->second); + boost::unordered_map::iterator iit = mComplete.find(set); + if (iit != mComplete.end()) + createDisputes(initialSet, iit->second); } } @@ -347,11 +358,10 @@ void LedgerConsensus::mapComplete(const uint256& hash, const SHAMap::pointer& ma // Adjust tracking for each peer that takes this position std::vector peers; - for (boost::unordered_map::iterator it = mPeerPositions.begin(), - end = mPeerPositions.end(); it != end; ++it) + BOOST_FOREACH(u160_prop_pair& it, mPeerPositions) { - if (it->second->getCurrentHash() == map->getHash()) - peers.push_back(it->second->getPeerID()); + if (it.second->getCurrentHash() == map->getHash()) + peers.push_back(it.second->getPeerID()); } if (!peers.empty()) adjustCount(map, peers); @@ -372,12 +382,11 @@ void LedgerConsensus::sendHaveTxSet(const uint256& hash, bool direct) void LedgerConsensus::adjustCount(const SHAMap::pointer& map, const std::vector& peers) { // Adjust the counts on all disputed transactions based on the set of peers taking this position - for (boost::unordered_map::iterator it = mDisputes.begin(), end = mDisputes.end(); - it != end; ++it) + BOOST_FOREACH(u256_lct_pair& it, mDisputes) { - bool setHas = map->hasItem(it->second->getTransactionID()); - for (std::vector::const_iterator pit = peers.begin(), pend = peers.end(); pit != pend; ++pit) - it->second->setVote(*pit, setHas); + bool setHas = map->hasItem(it.second->getTransactionID()); + BOOST_FOREACH(const uint160& pit, peers) + it.second->setVote(pit, setHas); } } @@ -502,33 +511,32 @@ void LedgerConsensus::updateOurPositions() SHAMap::pointer ourPosition; std::vector addedTx, removedTx; - for (boost::unordered_map::iterator it = mDisputes.begin(), - end = mDisputes.end(); it != end; ++it) + BOOST_FOREACH(u256_lct_pair& it, mDisputes) { - if (it->second->updatePosition(mClosePercent, mProposing)) + if (it.second->updatePosition(mClosePercent, mProposing)) { if (!changes) { ourPosition = mComplete[mOurPosition->getCurrentHash()]->snapShot(true); changes = true; } - if (it->second->getOurPosition()) // now a yes + if (it.second->getOurPosition()) // now a yes { - ourPosition->addItem(SHAMapItem(it->first, it->second->peekTransaction()), true, false); - addedTx.push_back(it->first); + ourPosition->addItem(SHAMapItem(it.first, it.second->peekTransaction()), true, false); + addedTx.push_back(it.first); } else // now a no { - ourPosition->delItem(it->first); - removedTx.push_back(it->first); + ourPosition->delItem(it.first); + removedTx.push_back(it.first); } } } std::map closeTimes; - for (boost::unordered_map::iterator it = mPeerPositions.begin(), - end = mPeerPositions.end(); it != end; ++it) - ++closeTimes[it->second->getCloseTime() - (it->second->getCloseTime() % mCloseResolution)]; + + BOOST_FOREACH(u160_prop_pair& it, mPeerPositions) + ++closeTimes[it.second->getCloseTime() - (it.second->getCloseTime() % mCloseResolution)]; int neededWeight; if (mClosePercent < AV_MID_CONSENSUS_TIME) @@ -594,10 +602,9 @@ bool LedgerConsensus::haveConsensus() { int agree = 0, disagree = 0; uint256 ourPosition = mOurPosition->getCurrentHash(); - for (boost::unordered_map::iterator it = mPeerPositions.begin(), - end = mPeerPositions.end(); it != end; ++it) + BOOST_FOREACH(u160_prop_pair& it, mPeerPositions) { - if (it->second->getCurrentHash() == ourPosition) + if (it.second->getCurrentHash() == ourPosition) ++agree; else ++disagree; @@ -703,13 +710,12 @@ void LedgerConsensus::addDisputedTransaction(const uint256& txID, const std::vec LCTransaction::pointer txn = boost::make_shared(txID, tx, ourPosition); mDisputes[txID] = txn; - for (boost::unordered_map::iterator pit = mPeerPositions.begin(), - pend = mPeerPositions.end(); pit != pend; ++pit) + BOOST_FOREACH(u160_prop_pair& pit, mPeerPositions) { boost::unordered_map::const_iterator cit = - mComplete.find(pit->second->getCurrentHash()); + mComplete.find(pit.second->getCurrentHash()); if (cit != mComplete.end() && cit->second) - txn->setVote(pit->first, cit->second->hasItem(txID)); + txn->setVote(pit.first, cit->second->hasItem(txID)); } } @@ -736,9 +742,8 @@ bool LedgerConsensus::peerPosition(const LedgerProposal::pointer& newPosition) SHAMap::pointer set = getTransactionTree(newPosition->getCurrentHash(), true); if (set) { - for (boost::unordered_map::iterator it = mDisputes.begin(), - end = mDisputes.end(); it != end; ++it) - it->second->setVote(newPosition->getPeerID(), set->hasItem(it->first)); + BOOST_FOREACH(u256_lct_pair& it, mDisputes) + it.second->setVote(newPosition->getPeerID(), set->hasItem(it.first)); } else Log(lsTRACE) << "Don't have that tx set"; @@ -752,8 +757,8 @@ bool LedgerConsensus::peerHasSet(const Peer::pointer& peer, const uint256& hashS return true; std::vector< boost::weak_ptr >& set = mPeerData[hashSet]; - for (std::vector< boost::weak_ptr >::iterator iit = set.begin(), iend = set.end(); iit != iend; ++iit) - if (iit->lock() == peer) + BOOST_FOREACH(boost::weak_ptr& iit, set) + if (iit.lock() == peer) return false; set.push_back(peer); @@ -934,15 +939,14 @@ void LedgerConsensus::accept(const SHAMap::pointer& set) // Apply disputed transactions that didn't get in TransactionEngine engine(newOL); - for (boost::unordered_map::iterator it = mDisputes.begin(), - end = mDisputes.end(); it != end; ++it) + BOOST_FOREACH(u256_lct_pair& it, mDisputes) { - if (!it->second->getOurPosition()) + if (!it.second->getOurPosition()) { // we voted NO try { Log(lsINFO) << "Test applying disputed transaction that did not get in"; - SerializerIterator sit(it->second->peekTransaction()); + SerializerIterator sit(it.second->peekTransaction()); SerializedTransaction::pointer txn = boost::make_shared(boost::ref(sit)); applyTransaction(engine, txn, newOL, failedTransactions, false); } @@ -966,7 +970,8 @@ void LedgerConsensus::accept(const SHAMap::pointer& set) Log(lsINFO) << "We closed at " << boost::lexical_cast(mCloseTime); uint64 closeTotal = mCloseTime; int closeCount = 1; - for (std::map::iterator it = mCloseTimes.begin(), end = mCloseTimes.end(); it != end; ++it) + for (std::map::iterator it = mCloseTimes.begin(), end = + mCloseTimes.end(); it != end; ++it) { Log(lsINFO) << boost::lexical_cast(it->second) << " time votes for " << boost::lexical_cast(it->first);