Send a smarter list of peers.

This commit is contained in:
David Schwartz
2013-08-21 16:11:50 -07:00
parent c5a40141fe
commit c17cfe9ea6
3 changed files with 38 additions and 10 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -147,19 +147,37 @@ void Peers::start ()
bool Peers::getTopNAddrs (int n, std::vector<std::string>& 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<Peer::pointer> 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;
}