Fabcoin Core  0.16.2
P2P Digital Currency
Peer.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #include "Peer.h"
24 using namespace std;
25 using namespace dev;
26 using namespace dev::p2p;
27 
28 namespace dev
29 {
30 
31 namespace p2p
32 {
33 
34 bool Peer::shouldReconnect() const
35 {
36  return id && endpoint && chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds());
37 }
38 
39 unsigned Peer::fallbackSeconds() const
40 {
41  if (peerType == PeerType::Required)
42  return 5;
43  switch (m_lastDisconnect)
44  {
45  case BadProtocol:
46  return 30 * (m_failedAttempts + 1);
47  case UselessPeer:
48  case TooManyPeers:
49  return 25 * (m_failedAttempts + 1);
50  case ClientQuit:
51  return 15 * (m_failedAttempts + 1);
52  case NoDisconnect:
53  default:
54  if (m_failedAttempts < 5)
55  return m_failedAttempts ? m_failedAttempts * 5 : 5;
56  else if (m_failedAttempts < 15)
57  return 25 + (m_failedAttempts - 5) * 10;
58  else
59  return 25 + 100 + (m_failedAttempts - 15) * 20;
60  }
61 }
62 
63 bool Peer::operator<(Peer const& _p) const
64 {
65  if (isOffline() != _p.isOffline())
66  return isOffline();
67  else if (isOffline())
68  if (m_lastAttempted == _p.m_lastAttempted)
69  return m_failedAttempts < _p.m_failedAttempts;
70  else
71  return m_lastAttempted < _p.m_lastAttempted;
72  else
73  if (m_score == _p.m_score)
74  if (m_rating == _p.m_rating)
75  if (m_failedAttempts == _p.m_failedAttempts)
76  return id < _p.id;
77  else
78  return m_failedAttempts < _p.m_failedAttempts;
79  else
80  return m_rating < _p.m_rating;
81  else
82  return m_score < _p.m_score;
83 }
84 
85 }
86 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bool isOffline() const
Definition: Peer.h:63
std::hash for asio::adress
Definition: Common.h:323
unsigned m_failedAttempts
Definition: Peer.h:93
NodeID id
Definition: Common.h:254
int m_score
All time cumulative.
Definition: Peer.h:86
int m_rating
Trending.
Definition: Peer.h:87
std::chrono::system_clock::time_point m_lastAttempted
Definition: Peer.h:92
Representation of connectivity state and all other pertinent Peer metadata.
Definition: Peer.h:52
bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:562