From c17cfe9ea69ac85212111d13b51f9bf01129d435 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Wed, 21 Aug 2013 16:11:50 -0700 Subject: [PATCH] Send a smarter list of peers. --- modules/ripple_app/peers/ripple_Peer.cpp | 10 ++++++- modules/ripple_app/peers/ripple_Peer.h | 2 ++ modules/ripple_app/peers/ripple_Peers.cpp | 36 +++++++++++++++++------ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/modules/ripple_app/peers/ripple_Peer.cpp b/modules/ripple_app/peers/ripple_Peer.cpp index 7c75db0c0d..6bbc33d48a 100644 --- a/modules/ripple_app/peers/ripple_Peer.cpp +++ b/modules/ripple_app/peers/ripple_Peer.cpp @@ -106,6 +106,14 @@ public: { return mIpPort.second; } + bool getConnectString(std::string& connect) const + { + if (!mHello.has_ipv4port() || mIpPortConnect.first.empty()) + return false; + connect = boost::str(boost::format("%s:%d") % mIpPortConnect.first % mHello.ipv4port()); + return true; + } + void setIpPort (const std::string & strIP, int iPort); @@ -1533,7 +1541,7 @@ void PeerImp::recvPeers (protocol::TMPeers& packet) if (strIP != "0.0.0.0" && strIP != "127.0.0.1") { - //WriteLog (lsINFO, Peer) << "Peer: Learning: " << addressToString(this) << ": " << i << ": " << strIP << " " << iPort; + WriteLog (lsDEBUG, Peer) << "Peer: Learning: " << addressToString(this) << ": " << i << ": " << strIP << " " << iPort; getApp().getPeers ().savePeer (strIP, iPort, UniqueNodeList::vsTold); } diff --git a/modules/ripple_app/peers/ripple_Peer.h b/modules/ripple_app/peers/ripple_Peer.h index 731ba78c55..84de57855b 100644 --- a/modules/ripple_app/peers/ripple_Peer.h +++ b/modules/ripple_app/peers/ripple_Peer.h @@ -73,6 +73,8 @@ public: virtual bool isOutbound () const = 0; + virtual bool getConnectString(std::string&) const = 0; + virtual uint256 const& getClosedLedgerHash () const = 0; virtual bool hasLedger (uint256 const& hash, uint32 seq) const = 0; diff --git a/modules/ripple_app/peers/ripple_Peers.cpp b/modules/ripple_app/peers/ripple_Peers.cpp index 67e54ceac9..f42c3c260b 100644 --- a/modules/ripple_app/peers/ripple_Peers.cpp +++ b/modules/ripple_app/peers/ripple_Peers.cpp @@ -147,19 +147,37 @@ void Peers::start () bool Peers::getTopNAddrs (int n, std::vector& addrs) { - // XXX Filter out other local addresses (like ipv6) - Database* db = getApp().getWalletDB ()->getDB (); - ScopedLock sl (getApp().getWalletDB ()->getDBLock ()); - SQL_FOREACH (db, str (boost::format ("SELECT IpPort FROM PeerIps LIMIT %d") % n) ) + // Try current connections first + std::vector peers = getPeerVector(); + BOOST_FOREACH(Peer::ref peer, peers) { - std::string str; - - db->getStr (0, str); - - addrs.push_back (str); + if (peer->isConnected()) + { + std::string connectString; + if (peer->getConnectString(connectString)) + addrs.push_back(connectString); + } } + if (addrs.size() < n) + { + // XXX Filter out other local addresses (like ipv6) + Database* db = getApp().getWalletDB ()->getDB (); + ScopedLock sl (getApp().getWalletDB ()->getDBLock ()); + + SQL_FOREACH (db, str (boost::format ("SELECT IpPort FROM PeerIps LIMIT %d") % n)) + { + std::string str; + + db->getStr (0, str); + + addrs.push_back (str); + } + } + + // FIXME: Should uniqify addrs + return true; }