From bda64fac2ad8f2b0aadfe9536427942fc969ee07 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 7 Jun 2012 12:25:44 -0700 Subject: [PATCH] Tx set exchange improvements. --- src/LedgerConsensus.cpp | 47 ++++++++++++++++++----------------------- src/LedgerConsensus.h | 4 ++-- src/newcoin.proto | 20 ++++++++++++------ 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 0c14e9bdc4..4232c57e24 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -244,17 +244,15 @@ void LedgerConsensus::mapComplete(const uint256& hash, SHAMap::pointer map) if (!peers.empty()) adjustCount(map, peers); - std::vector hashes; - hashes.push_back(hash); - sendHaveTxSet(hashes); + sendHaveTxSet(hash, true); } -void LedgerConsensus::sendHaveTxSet(const std::vector& hashes) +void LedgerConsensus::sendHaveTxSet(const uint256& hash, bool direct) { - newcoin::TMHaveTransactionSet set; - for (std::vector::const_iterator it = hashes.begin(), end = hashes.end(); it != end; ++it) - set.add_hashes(it->begin(), 256 / 8); - PackedMessage::pointer packet = boost::make_shared(set, newcoin::mtHAVE_SET); + newcoin::TMHaveTransactionSet msg; + msg.set_hash(hash.begin(), 256 / 8); + msg.set_status(direct ? newcoin::tsHAVE : newcoin::tsCAN_GET); + PackedMessage::pointer packet = boost::make_shared(msg, newcoin::mtHAVE_SET); theApp->getConnectionPool().relayMessage(NULL, packet); } @@ -411,9 +409,7 @@ bool LedgerConsensus::updateOurPositions(int sinceClose) uint256 newHash = ourPosition->getHash(); mOurPosition->changePosition(newHash); propose(addedTx, removedTx); - std::vector hashes; - hashes.push_back(newHash); - sendHaveTxSet(hashes); + sendHaveTxSet(newHash, true); } return stable; @@ -535,23 +531,20 @@ bool LedgerConsensus::peerPosition(LedgerProposal::pointer newPosition) return true; } -bool LedgerConsensus::peerHasSet(Peer::pointer peer, const std::vector& sets) +bool LedgerConsensus::peerHasSet(Peer::pointer peer, const uint256& hashSet, newcoin::TxSetStatus status) { - for (std::vector::const_iterator it = sets.begin(), end = sets.end(); it != end; ++it) - { - std::vector< boost::weak_ptr >& set = mPeerData[*it]; - bool found = false; - for (std::vector< boost::weak_ptr >::iterator iit = set.begin(), iend = set.end(); iit != iend; ++iit) - if (iit->lock() == peer) - found = true; - if (!found) - { - set.push_back(peer); - boost::unordered_map::iterator acq = mAcquiring.find(*it); - if (acq != mAcquiring.end()) - acq->second->peerHas(peer); - } - } + if (status != newcoin::tsHAVE) // Indirect requests are for future support + 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) + return false; + + set.push_back(peer); + boost::unordered_map::iterator acq = mAcquiring.find(hashSet); + if (acq != mAcquiring.end()) + acq->second->peerHas(peer); return true; } diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index ea77f87aff..820d65a65c 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -111,7 +111,7 @@ protected: void addPosition(LedgerProposal&, bool ours); void removePosition(LedgerProposal&, bool ours); - void sendHaveTxSet(const std::vector& txSetHashes); + void sendHaveTxSet(const uint256& set, bool direct); void applyTransactions(SHAMap::pointer transactionSet, Ledger::pointer targetLedger, std::list& failedTransactions); @@ -148,7 +148,7 @@ public: bool peerPosition(LedgerProposal::pointer); - bool peerHasSet(Peer::pointer peer, const std::vector& sets); + bool peerHasSet(Peer::pointer peer, const uint256& set, newcoin::TxSetStatus status); bool peerGaveNodes(Peer::pointer peer, const uint256& setHash, const std::list& nodeIDs, const std::list< std::vector >& nodeData); diff --git a/src/newcoin.proto b/src/newcoin.proto index 5e3a0c4bc7..2e0fa5b678 100644 --- a/src/newcoin.proto +++ b/src/newcoin.proto @@ -108,9 +108,15 @@ message TMProposeSet { repeated bytes removedTransactions = 6; // not required if number is large } -// Announce to a peer that we have fully acquired a transaction set +enum TxSetStatus { + tsHAVE = 1; // We have this set locally + tsCAN_GET = 2; // We have a peer with this set + tsNEED = 3; // We need this set and can't get it +} + message TMHaveTransactionSet { - repeated bytes hashes = 1; + required TxSetStatus status = 1; + required bytes hash = 2; } @@ -221,11 +227,12 @@ enum TMLedgerType { } message TMGetLedger { - optional TMLedgerType ltype = 1; - optional bytes ledgerHash = 2; // Can also be the transaction set hash if liTS_CANDIDATE - optional uint32 ledgerSeq = 3; - required TMLedgerInfoType itype = 4; + required TMLedgerInfoType itype = 1; + optional TMLedgerType ltype = 2; + optional bytes ledgerHash = 3; // Can also be the transaction set hash if liTS_CANDIDATE + optional uint32 ledgerSeq = 4; repeated bytes nodeIDs = 5; + optional uint32 requestCookie = 6; } message TMLedgerData { @@ -233,6 +240,7 @@ message TMLedgerData { required uint32 ledgerSeq = 2; required TMLedgerInfoType type = 3; repeated TMLedgerNode nodes = 4; + optional uint32 requestCookie = 5; }