From 7744f7c11111956860ddc331d952c93134dbd338 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 16 Sep 2012 20:58:38 -0700 Subject: [PATCH] Cleanups and improved comments. Fix a race condition if a peer ledger comes in before we take our initial position. --- src/LedgerConsensus.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index aff9f7bdaf..48b26bdc12 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -359,6 +359,13 @@ void LedgerConsensus::takeInitialPosition(Ledger& initialLedger) uint256 txSet = initialSet->getHash(); Log(lsINFO) << "initial position " << txSet; + if (mValidating) + mOurPosition = boost::make_shared + (mValSeed, initialLedger.getParentHash(), txSet, mCloseTime); + else + mOurPosition = boost::make_shared(initialLedger.getParentHash(), txSet, mCloseTime); + mapComplete(txSet, initialSet, false); + // if any peers have taken a contrary position, process disputes boost::unordered_set found; BOOST_FOREACH(u160_prop_pair& it, mPeerPositions) @@ -372,12 +379,6 @@ void LedgerConsensus::takeInitialPosition(Ledger& initialLedger) } } - if (mValidating) - mOurPosition = boost::make_shared - (mValSeed, initialLedger.getParentHash(), txSet, mCloseTime); - else - mOurPosition = boost::make_shared(initialLedger.getParentHash(), txSet, mCloseTime); - mapComplete(txSet, initialSet, false); if (mProposing) propose(); } @@ -389,16 +390,17 @@ void LedgerConsensus::createDisputes(SHAMap::ref m1, SHAMap::ref m2) for (SHAMap::SHAMapDiff::iterator pos = differences.begin(), end = differences.end(); pos != end; ++pos) { // create disputed transactions (from the ledger that has them) if (pos->second.first) - { + { // transaction is in first map assert(!pos->second.second); addDisputedTransaction(pos->first, pos->second.first->peekData()); } else if (pos->second.second) - { + { // transaction is in second map assert(!pos->second.first); addDisputedTransaction(pos->first, pos->second.second->peekData()); } - else assert(false); + else // No other disagreement over a transaction should be possible + assert(false); } } @@ -417,7 +419,10 @@ void LedgerConsensus::mapComplete(const uint256& hash, SHAMap::ref map, bool acq assert(hash == map->getHash()); if (mAcquired.find(hash) != mAcquired.end()) + { + mAcquiring.erase(hash); return; // we already have this map + } if (mOurPosition && (!mOurPosition->isBowOut()) && (hash != mOurPosition->getCurrentHash())) { // this could create disputed transactions @@ -799,19 +804,20 @@ void LedgerConsensus::addDisputedTransaction(const uint256& txID, const std::vec { Log(lsTRACE) << "Transaction " << txID << " is disputed"; boost::unordered_map::iterator it = mDisputes.find(txID); - if (it != mDisputes.end()) return; + if (it != mDisputes.end()) + return; - bool ourPosition = false; + bool ourVote = false; if (mOurPosition) { boost::unordered_map::iterator mit = mAcquired.find(mOurPosition->getCurrentHash()); if (mit != mAcquired.end()) - ourPosition = mit->second->hasItem(txID); + ourVote = mit->second->hasItem(txID); else assert(false); // We don't have our own position? } - LCTransaction::pointer txn = boost::make_shared(txID, tx, ourPosition); + LCTransaction::pointer txn = boost::make_shared(txID, tx, ourVote); mDisputes[txID] = txn; BOOST_FOREACH(u160_prop_pair& pit, mPeerPositions)