Browse Source

Quick commit.

cl-refactor
Gav Wood 11 years ago
parent
commit
54a21ab3bd
  1. 2
      libethcore/BlockInfo.h
  2. 4
      libethential/FixedHash.h
  3. 1
      libethereum/EthereumHost.cpp
  4. 2
      libethereum/EthereumHost.h
  5. 4
      libethereum/EthereumPeer.cpp
  6. 6
      libethereum/EthereumPeer.h
  7. 3
      libethnet/CMakeLists.txt
  8. 5
      libethnet/Common.cpp
  9. 17
      libethnet/Common.h
  10. 25
      libethnet/PeerHost.cpp
  11. 19
      libethnet/PeerHost.h
  12. 9
      libethnet/PeerSession.cpp
  13. 7
      libethnet/PeerSession.h
  14. 3
      libethnet/UPnP.cpp
  15. 2
      libethnet/UPnP.h
  16. 1
      libwhisper/WhisperPeer.cpp
  17. 9
      libwhisper/WhisperPeer.h

2
libethcore/BlockInfo.h

@ -48,7 +48,7 @@ extern u256 c_genesisDifficulty;
* and calculateGasLimit() and the object serialised to RLP with fillStream. To determine the
* header hash without the nonce (for mining), the method headerHashWithoutNonce() is provided.
*
* The defualt constructor creates an empty object, which can be tested against with the boolean
* The default constructor creates an empty object, which can be tested against with the boolean
* conversion operator.
*/
struct BlockInfo

4
libethential/FixedHash.h

@ -127,7 +127,7 @@ public:
/// @returns a randomly-valued hash
template <class Engine>
static FixedHash random(Engine& _eng = s_fixedHashEngine)
static FixedHash random(Engine& _eng)
{
FixedHash ret;
for (auto& i: ret.m_data)
@ -135,6 +135,8 @@ public:
return ret;
}
static FixedHash random() { return random(s_fixedHashEngine); }
/// A generic std::hash compatible function object.
struct hash
{

1
libethereum/EthereumHost.cpp

@ -36,6 +36,7 @@
#include "EthereumPeer.h"
using namespace std;
using namespace eth;
using namespace p2p;
EthereumHost::EthereumHost(BlockChain const& _ch, u256 _networkId):
HostCapability<EthereumPeer>(),

2
libethereum/EthereumHost.h

@ -45,7 +45,7 @@ class BlockQueue;
* @brief The EthereumHost class
* @warning None of this is thread-safe. You have been warned.
*/
class EthereumHost: public HostCapability<EthereumPeer>
class EthereumHost: public p2p::HostCapability<EthereumPeer>
{
friend class EthereumPeer;

4
libethereum/EthereumPeer.cpp

@ -29,10 +29,12 @@
#include "EthereumHost.h"
using namespace std;
using namespace eth;
using namespace p2p;
#define clogS(X) eth::LogOutputStream<X, true>(false) << "| " << std::setw(2) << session()->socketId() << "] "
EthereumPeer::EthereumPeer(PeerSession* _s, HostCapabilityFace* _h): PeerCapability(_s, _h)
EthereumPeer::EthereumPeer(PeerSession* _s, HostCapabilityFace* _h):
PeerCapability(_s, _h)
{
sendStatus();
}

6
libethereum/EthereumPeer.h

@ -34,18 +34,16 @@
namespace eth
{
class HostCapabilityFace;
/**
* @brief The EthereumPeer class
* @todo Document fully.
*/
class EthereumPeer: public PeerCapability
class EthereumPeer: public p2p::PeerCapability
{
friend class EthereumHost;
public:
EthereumPeer(PeerSession* _s, HostCapabilityFace* _h);
EthereumPeer(p2p::PeerSession* _s, p2p::HostCapabilityFace* _h);
virtual ~EthereumPeer();
static std::string name() { return "eth"; }

3
libethnet/CMakeLists.txt

@ -17,9 +17,6 @@ file(GLOB HEADERS "*.h")
include_directories(..)
target_link_libraries(${EXECUTABLE} evm)
target_link_libraries(${EXECUTABLE} lll)
target_link_libraries(${EXECUTABLE} ethcore)
target_link_libraries(${EXECUTABLE} secp256k1)
if(MINIUPNPC_LS)
target_link_libraries(${EXECUTABLE} ${MINIUPNPC_LS})

5
libethnet/Common.cpp

@ -26,12 +26,13 @@
#include "PeerHost.h"
using namespace std;
using namespace eth;
using namespace p2p;
// Helper function to determine if an address falls within one of the reserved ranges
// For V4:
// Class A "10.*", Class B "172.[16->31].*", Class C "192.168.*"
// Not implemented yet for V6
bool eth::isPrivateAddress(bi::address _addressToCheck)
bool p2p::isPrivateAddress(bi::address _addressToCheck)
{
if (_addressToCheck.is_v4())
{
@ -47,7 +48,7 @@ bool eth::isPrivateAddress(bi::address _addressToCheck)
return false;
}
std::string eth::reasonOf(DisconnectReason _r)
std::string p2p::reasonOf(DisconnectReason _r)
{
switch (_r)
{

17
libethnet/Common.h

@ -29,16 +29,29 @@
#include <chrono>
#include <libethential/Common.h>
#include <libethential/Log.h>
#include <libethential/FixedHash.h>
namespace ba = boost::asio;
namespace bi = boost::asio::ip;
namespace eth
{
class RLP;
class RLPStream;
}
namespace p2p
{
using eth::LogChannel;
using eth::bytes;
using eth::h256;
using eth::h512;
using eth::bytesConstRef;
using eth::RLP;
using eth::RLPStream;
bool isPrivateAddress(bi::address _addressToCheck);
class RLP;
class RLPStream;
class PeerHost;
class PeerSession;

25
libethnet/PeerHost.cpp

@ -40,6 +40,7 @@
#include "UPnP.h"
using namespace std;
using namespace eth;
using namespace p2p;
// Addresses we will skip during network interface discovery
// Use a vector as the list is small
@ -58,13 +59,13 @@ PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, stri
m_localNetworking(_localNetworking),
m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), _port)),
m_socket(m_ioService),
m_key(KeyPair::create())
m_id(h512::random())
{
populateAddresses();
determinePublic(_publicAddress, _upnp);
ensureAccepting();
m_lastPeersRequest = chrono::steady_clock::time_point::min();
clog(NetNote) << "Id:" << m_key.address().abridged();
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddress, bool _upnp, bool _localNetworking):
@ -73,7 +74,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre
m_localNetworking(_localNetworking),
m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)),
m_socket(m_ioService),
m_key(KeyPair::create())
m_id(h512::random())
{
m_listenPort = m_acceptor.local_endpoint().port();
@ -82,7 +83,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre
determinePublic(_publicAddress, _upnp);
ensureAccepting();
m_lastPeersRequest = chrono::steady_clock::time_point::min();
clog(NetNote) << "Id:" << m_key.address().abridged();
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::PeerHost(std::string const& _clientVersion):
@ -90,12 +91,12 @@ PeerHost::PeerHost(std::string const& _clientVersion):
m_listenPort(0),
m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)),
m_socket(m_ioService),
m_key(KeyPair::create())
m_id(h512::random())
{
// populate addresses.
populateAddresses();
m_lastPeersRequest = chrono::steady_clock::time_point::min();
clog(NetNote) << "Id:" << m_key.address().abridged();
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::~PeerHost()
@ -271,11 +272,11 @@ void PeerHost::populateAddresses()
#endif
}
std::map<Public, bi::tcp::endpoint> PeerHost::potentialPeers()
std::map<h512, bi::tcp::endpoint> PeerHost::potentialPeers()
{
std::map<Public, bi::tcp::endpoint> ret;
std::map<h512, bi::tcp::endpoint> ret;
if (!m_public.address().is_unspecified())
ret.insert(make_pair(m_key.pub(), m_public));
ret.insert(make_pair(m_id, m_public));
Guard l(x_peers);
for (auto i: m_peers)
if (auto j = i.second.lock())
@ -361,7 +362,7 @@ void PeerHost::connect(bi::tcp::endpoint const& _ep)
});
}
bool PeerHost::havePeer(Public _id) const
bool PeerHost::havePeer(h512 _id) const
{
Guard l(x_peers);
@ -413,7 +414,7 @@ void PeerHost::prunePeers()
{
Guard l(x_peers);
// We'll keep at most twice as many as is ideal, halfing what counts as "too young to kill" until we get there.
for (uint old = 15000; m_peers.size() > m_idealPeerCount * 2 && old > 100; old /= 2)
for (unsigned old = 15000; m_peers.size() > m_idealPeerCount * 2 && old > 100; old /= 2)
while (m_peers.size() > m_idealPeerCount)
{
// look for worst peer to kick off
@ -492,7 +493,7 @@ void PeerHost::restorePeers(bytesConstRef _b)
{
for (auto i: RLP(_b))
{
auto k = (Public)i[2];
auto k = (h512)i[2];
if (!m_incomingPeers.count(k))
{
m_incomingPeers.insert(make_pair(k, make_pair(bi::tcp::endpoint(bi::address_v4(i[0].toArray<byte, 4>()), i[1].toInt<short>()), 0)));

19
libethnet/PeerHost.h

@ -29,18 +29,19 @@
#include <utility>
#include <thread>
#include <libethential/Guards.h>
#include <libethcore/CommonEth.h>
#include "Common.h"
namespace ba = boost::asio;
namespace bi = boost::asio::ip;
namespace eth
namespace p2p
{
class RLPStream;
class TransactionQueue;
class BlockQueue;
using eth::Guard;
/**
* @brief The PeerHost class
* Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe.
@ -84,7 +85,7 @@ public:
void process();
/// @returns true iff we have the a peer of the given id.
bool havePeer(Public _id) const;
bool havePeer(h512 _id) const;
/// Set ideal number of peers.
void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; }
@ -107,7 +108,7 @@ public:
/// Deserialise the data and populate the set of known peers.
void restorePeers(bytesConstRef _b);
h512 id() const { return m_key.pub(); }
h512 id() const { return m_id; }
void registerPeer(std::shared_ptr<PeerSession> _s, std::vector<std::string> const& _caps);
@ -123,7 +124,7 @@ protected:
void growPeers();
void prunePeers();
std::map<Public, bi::tcp::endpoint> potentialPeers();
std::map<h512, bi::tcp::endpoint> potentialPeers();
std::string m_clientVersion;
@ -136,13 +137,13 @@ protected:
UPnP* m_upnp = nullptr;
bi::tcp::endpoint m_public;
KeyPair m_key;
h512 m_id;
mutable std::mutex x_peers;
mutable std::map<Public, std::weak_ptr<PeerSession>> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method.
mutable std::map<h512, std::weak_ptr<PeerSession>> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method.
std::map<Public, std::pair<bi::tcp::endpoint, unsigned>> m_incomingPeers; // TODO: does this need a lock?
std::vector<Public> m_freePeers;
std::map<h512, std::pair<bi::tcp::endpoint, unsigned>> m_incomingPeers; // TODO: does this need a lock?
std::vector<h512> m_freePeers;
std::chrono::steady_clock::time_point m_lastPeersRequest;
unsigned m_idealPeerCount = 5;

9
libethnet/PeerSession.cpp

@ -27,6 +27,7 @@
#include "PeerHost.h"
using namespace std;
using namespace eth;
using namespace p2p;
#define clogS(X) eth::LogOutputStream<X, true>(false) << "| " << std::setw(2) << m_socket.native_handle() << "] "
@ -74,7 +75,7 @@ bool PeerSession::interpret(RLP const& _r)
{
case HelloPacket:
{
m_protocolVersion = _r[1].toInt<uint>();
m_protocolVersion = _r[1].toInt<unsigned>();
auto clientVersion = _r[2].toString();
auto caps = _r[3].toVector<string>();
m_listenPort = _r[4].toInt<unsigned short>();
@ -156,14 +157,14 @@ bool PeerSession::interpret(RLP const& _r)
{
bi::address_v4 peerAddress(_r[i][0].toHash<FixedHash<4>>().asArray());
auto ep = bi::tcp::endpoint(peerAddress, _r[i][1].toInt<short>());
Public id = _r[i][2].toHash<Public>();
h512 id = _r[i][2].toHash<h512>();
if (isPrivateAddress(peerAddress) && !m_server->m_localNetworking)
goto CONTINUE;
clogS(NetAllDetail) << "Checking: " << ep << "(" << id.abridged() << ")";
// check that it's not us or one we already know:
if (id && (m_server->m_key.pub() == id || m_server->havePeer(id) || m_server->m_incomingPeers.count(id)))
if (id && (m_server->m_id == id || m_server->havePeer(id) || m_server->m_incomingPeers.count(id)))
goto CONTINUE;
// check that we're not already connected to addr:
@ -336,7 +337,7 @@ void PeerSession::start()
<< m_server->m_clientVersion
<< m_server->caps()
<< m_server->m_public.port()
<< m_server->m_key.pub();
<< m_server->m_id;
sealAndSend(s);
ping();
getPeers();

7
libethnet/PeerSession.h

@ -27,10 +27,9 @@
#include <memory>
#include <utility>
#include <libethential/RLP.h>
#include <libethcore/CommonEth.h>
#include "Common.h"
namespace eth
namespace p2p
{
/**
@ -89,10 +88,10 @@ private:
mutable bi::tcp::socket m_socket; ///< Mutable to ask for native_handle().
std::array<byte, 65536> m_data;
PeerInfo m_info;
Public m_id;
h512 m_id;
bytes m_incoming;
uint m_protocolVersion;
unsigned m_protocolVersion;
unsigned short m_listenPort; ///< Port that the remote client is listening on for connections. Useful for giving to peers.
std::chrono::steady_clock::time_point m_ping;

3
libethnet/UPnP.cpp

@ -34,6 +34,7 @@
#include <libethential/Log.h>
using namespace std;
using namespace eth;
using namespace p2p;
UPnP::UPnP()
{
@ -126,7 +127,7 @@ int UPnP::addRedirect(char const* _addr, int _port)
return _port;
// Failed - now try (random external, port internal) and cycle up to 10 times.
for (uint i = 0; i < 10; ++i)
for (unsigned i = 0; i < 10; ++i)
{
_port = rand() % 65535 - 1024 + 1024;
sprintf(port_str, "%d", _port);

2
libethnet/UPnP.h

@ -29,7 +29,7 @@
struct UPNPUrls;
struct IGDdatas;
namespace eth
namespace p2p
{
class UPnP

1
libwhisper/WhisperPeer.cpp

@ -25,6 +25,7 @@
#include <libethnet/All.h>
using namespace std;
using namespace eth;
using namespace p2p;
using namespace shh;
#define clogS(X) eth::LogOutputStream<X, true>(false) << "| " << std::setw(2) << session()->socketId() << "] "

9
libwhisper/WhisperPeer.h

@ -34,9 +34,10 @@
namespace shh
{
using eth::PeerSession;
using eth::HostCapabilityFace;
using eth::HostCapability;
using p2p::PeerSession;
using p2p::HostCapabilityFace;
using p2p::HostCapability;
using p2p::PeerCapability;
struct Message
{
@ -63,7 +64,7 @@ struct Message
/**
*/
class WhisperPeer: public eth::PeerCapability
class WhisperPeer: public PeerCapability
{
friend class WhisperHost;

Loading…
Cancel
Save