From 07e353d8a27415b72465754368f3fb842448d69c Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 23 May 2012 17:26:45 -0700 Subject: [PATCH] More ledger consensus work. --- src/LedgerConsensus.cpp | 52 ++++++++++++++++++++++++++++++++++++----- src/LedgerConsensus.h | 16 ++++++++----- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 25a7953571..1d291d604a 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -105,12 +105,33 @@ void LedgerConsensus::closeTime(Ledger::pointer& current) current->updateHash(); uint256 txSet = current->getTransHash(); mOurPosition = boost::make_shared(nodePrivKey, current->getParentHash(), txSet); - mComplete[txSet] = current->peekTransactionMap()->snapShot(); + mapComplete(current->peekTransactionMap()->snapShot()); // WRITME: Broadcast an IHAVE for this set } +void LedgerConsensus::mapComplete(SHAMap::pointer map) +{ + boost::unordered_map::iterator it = mComplete.find(map->getHash()); + if (it != mComplete.end()) return; + if (mOurPosition && (map->getHash() != mOurPosition->getCurrentHash())) + { // this creates disputed transactions + boost::unordered_map::iterator it2 = mComplete.find(mOurPosition->getCurrentHash()); + if (it2 != mComplete.end()) + { + SHAMap::SHAMapDiff differences; + it2->second->compare(it->second, differences, 16384); + for(SHAMap::SHAMapDiff::iterator pos = differences.begin(), end = differences.end(); pos != end; ++pos) + { // create disputed transactions + addDisputedTransaction(pos->first); + } + } + } + mComplete[map->getHash()] = map; +} + void LedgerConsensus::abort() { + mState = lcsABORTED; } int LedgerConsensus::startup() @@ -165,14 +186,33 @@ void LedgerConsensus::startAcquiring(TransactionAcquire::pointer acquire) } } -void LedgerConsensus::removePosition(LedgerProposal& position) +void LedgerConsensus::removePosition(LedgerProposal& position, bool ours) +{ +// int threshold = getThreshold(); +// uint256 txSet = position.getCurrentHash(); + + // WRITEME +} + +void LedgerConsensus::addPosition(LedgerProposal& position, bool ours) { // WRITEME } -void LedgerConsensus::addPosition(LedgerProposal& position) +void LedgerConsensus::addDisputedTransaction(const uint256& txID) { - // WRITEME + boost::unordered_map::iterator it = mDisputes.find(txID); + if (it != mDisputes.end()) return; + + bool ourPosition = false; + if (mOurPosition) + { + boost::unordered_map::iterator mit = mComplete.find(mOurPosition->getCurrentHash()); + if (mit != mComplete.end()) + ourPosition = mit->second->hasItem(txID); + } + + mDisputes[txID] = boost::make_shared(txID, ourPosition); } bool LedgerConsensus::peerPosition(LedgerProposal::pointer newPosition) @@ -184,11 +224,11 @@ bool LedgerConsensus::peerPosition(LedgerProposal::pointer newPosition) return false; // change in position - removePosition(*currentPosition); + removePosition(*currentPosition, false); } currentPosition = newPosition; - addPosition(*currentPosition); + addPosition(*currentPosition, false); return true; } diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index e1b58db0fc..eae885ffd4 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -45,7 +45,6 @@ protected: int mYays, mNays; bool mOurPosition; boost::unordered_map mVotes; - Transaction::pointer mTransaction; public: typedef boost::shared_ptr pointer; @@ -56,9 +55,6 @@ public: const uint256& getTransactionID() const { return mTransactionID; } bool getOurPosition() const { return mOurPosition; } - bool haveTransaction() const { return !!mTransaction; } - Transaction::pointer getTransaction() { return mTransaction; } - void setVote(const uint256& peer, bool votesYes); bool updatePosition(int timePassed); @@ -71,6 +67,7 @@ enum LCState lcsCUTOFF, // Past the cutoff for consensus lcsFINSHED, // We have closed on a transaction set lcsACCEPTED, // We have accepted/validated a new last closed ledger + lcsABORTED // Abandoned }; class LedgerConsensus @@ -90,12 +87,19 @@ protected: // Peer sets boost::unordered_map >, hash_SMN> mPeerData; + // Disputed transactions + boost::unordered_map mDisputes; + void weHave(const uint256& id, Peer::pointer avoidPeer); void startAcquiring(TransactionAcquire::pointer); SHAMap::pointer find(const uint256& hash); - void addPosition(LedgerProposal&); - void removePosition(LedgerProposal&); + void mapComplete(SHAMap::pointer map); + void addDisputedTransaction(const uint256&); + + void addPosition(LedgerProposal&, bool ours); + void removePosition(LedgerProposal&, bool ours); + int getThreshold(); public: LedgerConsensus(Ledger::pointer previousLedger);