Browse Source

Repot.

No memsize increase on 0.
cl-refactor
Gav Wood 11 years ago
parent
commit
a3740efebb
  1. 2
      exp/main.cpp
  2. 6
      libethereum/Client.cpp
  3. 2
      libethereum/Client.h
  4. 2
      libethereum/CommonNet.h
  5. 4
      libethereum/EthereumHost.cpp
  6. 8
      libethereum/EthereumPeer.cpp
  7. 6
      libethereum/EthereumPeer.h
  8. 14
      libevm/VM.h
  9. 6
      libp2p/All.h
  10. 58
      libp2p/Capability.cpp
  11. 62
      libp2p/Capability.h
  12. 51
      libp2p/Common.cpp
  13. 83
      libp2p/Common.h
  14. 65
      libp2p/Host.cpp
  15. 20
      libp2p/Host.h
  16. 44
      libp2p/HostCapability.cpp
  17. 70
      libp2p/HostCapability.h
  18. 41
      libp2p/Session.cpp
  19. 16
      libp2p/Session.h
  20. 2
      libwhisper/Common.h
  21. 4
      libwhisper/WhisperPeer.cpp
  22. 8
      libwhisper/WhisperPeer.h
  23. 4
      test/peer.cpp

2
exp/main.cpp

@ -320,7 +320,7 @@ int main(int argc, char** argv)
remoteHost = argv[i];
}
PeerHost ph("Test", listenPort, "", false, true);
Host ph("Test", listenPort, "", false, true);
ph.registerCapability(new WhisperHost());
auto wh = ph.cap<WhisperHost>();

6
libethereum/Client.cpp

@ -25,7 +25,7 @@
#include <thread>
#include <boost/filesystem.hpp>
#include <libethential/Log.h>
#include <libp2p/PeerHost.h>
#include <libp2p/Host.h>
#include "Defaults.h"
#include "EthereumHost.h"
using namespace std;
@ -215,13 +215,13 @@ void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHo
try
{
m_net.reset(new PeerHost(m_clientVersion, _listenPort, _publicIP, _upnp));
m_net.reset(new Host(m_clientVersion, _listenPort, _publicIP, _upnp));
}
catch (std::exception const&)
{
// Probably already have the port open.
cwarn << "Could not initialize with specified/default port. Trying system-assigned port";
m_net.reset(new PeerHost(m_clientVersion, 0, _publicIP, _upnp));
m_net.reset(new Host(m_clientVersion, 0, _publicIP, _upnp));
}
if (_mode == NodeMode::Full)
m_net->registerCapability(new EthereumHost(m_bc, _networkId));

2
libethereum/Client.h

@ -305,7 +305,7 @@ private:
std::unique_ptr<std::thread> m_workNet; ///< The network thread.
std::atomic<ClientWorkState> m_workNetState;
mutable boost::shared_mutex x_net; ///< Lock for the network existance.
std::unique_ptr<p2p::PeerHost> m_net; ///< Should run in background and send us events when blocks found and allow us to send blocks as required.
std::unique_ptr<p2p::Host> m_net; ///< Should run in background and send us events when blocks found and allow us to send blocks as required.
std::unique_ptr<std::thread> m_work; ///< The work thread.
std::atomic<ClientWorkState> m_workState;

2
libethereum/CommonNet.h

@ -18,7 +18,7 @@
* @author Gav Wood <i@gavwood.com>
* @date 2014
*
* Miscellanea required for the PeerServer/PeerSession classes.
* Miscellanea required for the PeerServer/Session classes.
*/
#pragma once

4
libethereum/EthereumHost.cpp

@ -27,8 +27,8 @@
#include <chrono>
#include <thread>
#include <libethential/Common.h>
#include <libp2p/PeerHost.h>
#include <libp2p/PeerSession.h>
#include <libp2p/Host.h>
#include <libp2p/Session.h>
#include <libethcore/Exceptions.h>
#include "BlockChain.h"
#include "TransactionQueue.h"

8
libethereum/EthereumPeer.cpp

@ -24,7 +24,7 @@
#include <chrono>
#include <libethential/Common.h>
#include <libethcore/Exceptions.h>
#include <libp2p/PeerSession.h>
#include <libp2p/Session.h>
#include "BlockChain.h"
#include "EthereumHost.h"
using namespace std;
@ -33,8 +33,8 @@ 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(Session* _s, HostCapabilityFace* _h):
Capability(_s, _h)
{
sendStatus();
}
@ -46,7 +46,7 @@ EthereumPeer::~EthereumPeer()
EthereumHost* EthereumPeer::host() const
{
return static_cast<EthereumHost*>(PeerCapability::hostCapability());
return static_cast<EthereumHost*>(Capability::hostCapability());
}
void EthereumPeer::sendStatus()

6
libethereum/EthereumPeer.h

@ -28,7 +28,7 @@
#include <utility>
#include <libethential/RLP.h>
#include <libethcore/CommonEth.h>
#include <libp2p/Common.h>
#include <libp2p/Capability.h>
#include "CommonNet.h"
namespace eth
@ -38,12 +38,12 @@ namespace eth
* @brief The EthereumPeer class
* @todo Document fully.
*/
class EthereumPeer: public p2p::PeerCapability
class EthereumPeer: public p2p::Capability
{
friend class EthereumHost;
public:
EthereumPeer(p2p::PeerSession* _s, p2p::HostCapabilityFace* _h);
EthereumPeer(p2p::Session* _s, p2p::HostCapabilityFace* _h);
virtual ~EthereumPeer();
static std::string name() { return "eth"; }

14
libevm/VM.h

@ -89,6 +89,8 @@ private:
// INLINE:
template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _onOp, uint64_t _steps)
{
auto memNeed = [](eth::u256 _offset, eth::u256 _size) { return _size ? _offset + _size : 0; };
u256 nextPC = m_curPC + 1;
auto osteps = _steps;
for (bool stopped = false; !stopped && _steps--; m_curPC = nextPC, nextPC = m_curPC + 1)
@ -138,20 +140,20 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
break;
case Instruction::RETURN:
require(2);
newTempSize = m_stack.back() + m_stack[m_stack.size() - 2];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::SHA3:
require(2);
runGas = c_sha3Gas;
newTempSize = m_stack.back() + m_stack[m_stack.size() - 2];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::CALLDATACOPY:
require(3);
newTempSize = m_stack.back() + m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::CODECOPY:
require(3);
newTempSize = m_stack.back() + m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::BALANCE:
@ -161,13 +163,13 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
case Instruction::CALL:
require(7);
runGas = c_callGas + m_stack[m_stack.size() - 1];
newTempSize = std::max(m_stack[m_stack.size() - 6] + m_stack[m_stack.size() - 7], m_stack[m_stack.size() - 4] + m_stack[m_stack.size() - 5]);
newTempSize = std::max(memNeed(m_stack[m_stack.size() - 6], m_stack[m_stack.size() - 7]), memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]));
break;
case Instruction::POST:
require(5);
runGas = c_callGas + m_stack[m_stack.size() - 1];
newTempSize = m_stack[m_stack.size() - 4] + m_stack[m_stack.size() - 5];
newTempSize = memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]);
break;
case Instruction::CREATE:

6
libp2p/All.h

@ -1,6 +1,8 @@
#pragma once
#include "Common.h"
#include "PeerHost.h"
#include "PeerSession.h"
#include "HostCapability.h"
#include "Capability.h"
#include "Host.h"
#include "Session.h"

58
libp2p/Capability.cpp

@ -0,0 +1,58 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Capability.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "Capability.h"
#include "Session.h"
using namespace std;
using namespace eth;
using namespace p2p;
void Capability::disable(std::string const& _problem)
{
clog(NetConnect) << "Disabling capability '" << m_host->name() << "'. Reason:" << _problem;
m_enabled = false;
}
RLPStream& Capability::prep(RLPStream& _s)
{
return Session::prep(_s);
}
void Capability::sealAndSend(RLPStream& _s)
{
m_session->sealAndSend(_s);
}
void Capability::sendDestroy(bytes& _msg)
{
m_session->sendDestroy(_msg);
}
void Capability::send(bytesConstRef _msg)
{
m_session->send(_msg);
}
void Capability::addRating(unsigned _r)
{
m_session->addRating(_r);
}

62
libp2p/Capability.h

@ -0,0 +1,62 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Capability.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#pragma once
#include "Common.h"
#include "HostCapability.h"
namespace p2p
{
class Capability
{
friend class Session;
public:
Capability(Session* _s, HostCapabilityFace* _h): m_session(_s), m_host(_h) {}
virtual ~Capability() {}
/// Must return the capability name.
static std::string name() { return ""; }
Session* session() const { return m_session; }
HostCapabilityFace* hostCapability() const { return m_host; }
protected:
virtual bool interpret(RLP const&) = 0;
void disable(std::string const& _problem);
static RLPStream& prep(RLPStream& _s);
void sealAndSend(RLPStream& _s);
void sendDestroy(bytes& _msg);
void send(bytesConstRef _msg);
void addRating(unsigned _r);
private:
Session* m_session;
HostCapabilityFace* m_host;
bool m_enabled = true;
};
}

51
libp2p/Common.cpp

@ -20,10 +20,6 @@
*/
#include "Common.h"
#include <libethential/RLP.h>
#include "PeerSession.h"
#include "PeerHost.h"
using namespace std;
using namespace eth;
using namespace p2p;
@ -63,50 +59,3 @@ std::string p2p::reasonOf(DisconnectReason _r)
default: return "Unknown reason.";
}
}
void PeerCapability::disable(std::string const& _problem)
{
clog(NetConnect) << "Disabling capability '" << m_host->name() << "'. Reason:" << _problem;
m_enabled = false;
}
void HostCapabilityFace::seal(bytes& _b)
{
m_host->seal(_b);
}
std::vector<std::shared_ptr<PeerSession> > HostCapabilityFace::peers() const
{
Guard l(m_host->x_peers);
std::vector<std::shared_ptr<PeerSession> > ret;
for (auto const& i: m_host->m_peers)
if (std::shared_ptr<PeerSession> p = i.second.lock())
if (p->m_capabilities.count(name()))
ret.push_back(p);
return ret;
}
RLPStream& PeerCapability::prep(RLPStream& _s)
{
return PeerSession::prep(_s);
}
void PeerCapability::sealAndSend(RLPStream& _s)
{
m_session->sealAndSend(_s);
}
void PeerCapability::sendDestroy(bytes& _msg)
{
m_session->sendDestroy(_msg);
}
void PeerCapability::send(bytesConstRef _msg)
{
m_session->send(_msg);
}
void PeerCapability::addRating(unsigned _r)
{
m_session->addRating(_r);
}

83
libp2p/Common.h

@ -18,7 +18,7 @@
* @author Gav Wood <i@gavwood.com>
* @date 2014
*
* Miscellanea required for the PeerHost/PeerSession classes.
* Miscellanea required for the Host/Session classes.
*/
#pragma once
@ -52,8 +52,10 @@ using eth::RLPStream;
bool isPrivateAddress(bi::address _addressToCheck);
class PeerHost;
class PeerSession;
class UPnP;
class Capability;
class Host;
class Session;
struct NetWarn: public LogChannel { static const char* name() { return "!N!"; } static const int verbosity = 0; };
struct NetNote: public LogChannel { static const char* name() { return "*N*"; } static const int verbosity = 1; };
@ -102,79 +104,4 @@ struct PeerInfo
std::chrono::steady_clock::duration lastPing;
};
class UPnP;
class PeerCapability;
class HostCapabilityFace
{
friend class PeerHost;
template <class T> friend class HostCapability;
friend class PeerCapability;
public:
HostCapabilityFace() {}
virtual ~HostCapabilityFace() {}
PeerHost* host() const { return m_host; }
std::vector<std::shared_ptr<PeerSession> > peers() const;
protected:
virtual std::string name() const = 0;
virtual PeerCapability* newPeerCapability(PeerSession* _s) = 0;
virtual bool isInitialised() const { return true; }
void seal(bytes& _b);
private:
PeerHost* m_host = nullptr;
};
template<class PeerCap>
class HostCapability: public HostCapabilityFace
{
public:
HostCapability() {}
virtual ~HostCapability() {}
static std::string staticName() { return PeerCap::name(); }
protected:
virtual std::string name() const { return PeerCap::name(); }
virtual PeerCapability* newPeerCapability(PeerSession* _s) { return new PeerCap(_s, this); }
};
class PeerCapability
{
friend class PeerSession;
public:
PeerCapability(PeerSession* _s, HostCapabilityFace* _h): m_session(_s), m_host(_h) {}
virtual ~PeerCapability() {}
/// Must return the capability name.
static std::string name() { return ""; }
PeerSession* session() const { return m_session; }
HostCapabilityFace* hostCapability() const { return m_host; }
protected:
virtual bool interpret(RLP const&) = 0;
void disable(std::string const& _problem);
static RLPStream& prep(RLPStream& _s);
void sealAndSend(RLPStream& _s);
void sendDestroy(bytes& _msg);
void send(bytesConstRef _msg);
void addRating(unsigned _r);
private:
PeerSession* m_session;
HostCapabilityFace* m_host;
bool m_enabled = true;
};
}

65
libp2p/PeerHost.cpp → libp2p/Host.cpp

@ -14,14 +14,14 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file PeerHost.cpp
/** @file Host.cpp
* @authors:
* Gav Wood <i@gavwood.com>
* Eric Lombrozo <elombrozo@gmail.com>
* @date 2014
*/
#include "PeerHost.h"
#include "Host.h"
#include <sys/types.h>
#ifdef _WIN32
@ -36,7 +36,8 @@
#include <thread>
#include <libethential/Common.h>
#include <libethcore/Exceptions.h>
#include "PeerSession.h"
#include "Session.h"
#include "Capability.h"
#include "UPnP.h"
using namespace std;
using namespace eth;
@ -53,7 +54,7 @@ static const set<bi::address> c_rejectAddresses = {
{bi::address_v6::from_string("::")}
};
PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, string const& _publicAddress, bool _upnp, bool _localNetworking):
Host::Host(std::string const& _clientVersion, unsigned short _port, string const& _publicAddress, bool _upnp, bool _localNetworking):
m_clientVersion(_clientVersion),
m_listenPort(_port),
m_localNetworking(_localNetworking),
@ -68,7 +69,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, stri
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddress, bool _upnp, bool _localNetworking):
Host::Host(std::string const& _clientVersion, string const& _publicAddress, bool _upnp, bool _localNetworking):
m_clientVersion(_clientVersion),
m_listenPort(0),
m_localNetworking(_localNetworking),
@ -86,7 +87,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::PeerHost(std::string const& _clientVersion):
Host::Host(std::string const& _clientVersion):
m_clientVersion(_clientVersion),
m_listenPort(0),
m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)),
@ -99,17 +100,17 @@ PeerHost::PeerHost(std::string const& _clientVersion):
clog(NetNote) << "Id:" << m_id.abridged();
}
PeerHost::~PeerHost()
Host::~Host()
{
disconnectPeers();
}
unsigned PeerHost::protocolVersion() const
unsigned Host::protocolVersion() const
{
return 0;
}
void PeerHost::registerPeer(std::shared_ptr<PeerSession> _s, vector<string> const& _caps)
void Host::registerPeer(std::shared_ptr<Session> _s, vector<string> const& _caps)
{
{
Guard l(x_peers);
@ -117,10 +118,10 @@ void PeerHost::registerPeer(std::shared_ptr<PeerSession> _s, vector<string> cons
}
for (auto const& i: _caps)
if (haveCapability(i))
_s->m_capabilities[i] = shared_ptr<PeerCapability>(m_capabilities[i]->newPeerCapability(_s.get()));
_s->m_capabilities[i] = shared_ptr<Capability>(m_capabilities[i]->newPeerCapability(_s.get()));
}
void PeerHost::disconnectPeers()
void Host::disconnectPeers()
{
for (unsigned n = 0;; n = 0)
{
@ -142,7 +143,7 @@ void PeerHost::disconnectPeers()
delete m_upnp;
}
void PeerHost::seal(bytes& _b)
void Host::seal(bytes& _b)
{
_b[0] = 0x22;
_b[1] = 0x40;
@ -155,7 +156,7 @@ void PeerHost::seal(bytes& _b)
_b[7] = len & 0xff;
}
void PeerHost::determinePublic(string const& _publicAddress, bool _upnp)
void Host::determinePublic(string const& _publicAddress, bool _upnp)
{
if (_upnp)
try
@ -197,7 +198,7 @@ void PeerHost::determinePublic(string const& _publicAddress, bool _upnp)
}
}
void PeerHost::populateAddresses()
void Host::populateAddresses()
{
#ifdef _WIN32
WSAData wsaData;
@ -272,7 +273,7 @@ void PeerHost::populateAddresses()
#endif
}
std::map<h512, bi::tcp::endpoint> PeerHost::potentialPeers()
std::map<h512, bi::tcp::endpoint> Host::potentialPeers()
{
std::map<h512, bi::tcp::endpoint> ret;
if (!m_public.address().is_unspecified())
@ -290,7 +291,7 @@ std::map<h512, bi::tcp::endpoint> PeerHost::potentialPeers()
return ret;
}
void PeerHost::ensureAccepting()
void Host::ensureAccepting()
{
if (m_accepting == false)
{
@ -306,7 +307,7 @@ void PeerHost::ensureAccepting()
} catch (...){}
bi::address remoteAddress = m_socket.remote_endpoint().address();
// Port defaults to 0 - we let the hello tell us which port the peer listens to
auto p = std::make_shared<PeerSession>(this, std::move(m_socket), remoteAddress);
auto p = std::make_shared<Session>(this, std::move(m_socket), remoteAddress);
p->start();
}
catch (std::exception const& _e)
@ -320,7 +321,7 @@ void PeerHost::ensureAccepting()
}
}
void PeerHost::connect(std::string const& _addr, unsigned short _port) noexcept
void Host::connect(std::string const& _addr, unsigned short _port) noexcept
{
try
{
@ -333,7 +334,7 @@ void PeerHost::connect(std::string const& _addr, unsigned short _port) noexcept
}
}
void PeerHost::connect(bi::tcp::endpoint const& _ep)
void Host::connect(bi::tcp::endpoint const& _ep)
{
clog(NetConnect) << "Attempting connection to " << _ep;
bi::tcp::socket* s = new bi::tcp::socket(m_ioService);
@ -354,7 +355,7 @@ void PeerHost::connect(bi::tcp::endpoint const& _ep)
}
else
{
auto p = make_shared<PeerSession>(this, std::move(*s), _ep.address(), _ep.port());
auto p = make_shared<Session>(this, std::move(*s), _ep.address(), _ep.port());
clog(NetConnect) << "Connected to " << _ep;
p->start();
}
@ -362,7 +363,7 @@ void PeerHost::connect(bi::tcp::endpoint const& _ep)
});
}
bool PeerHost::havePeer(h512 _id) const
bool Host::havePeer(h512 _id) const
{
Guard l(x_peers);
@ -376,7 +377,7 @@ bool PeerHost::havePeer(h512 _id) const
return !!m_peers.count(_id);
}
void PeerHost::growPeers()
void Host::growPeers()
{
Guard l(x_peers);
while (m_peers.size() < m_idealPeerCount)
@ -387,7 +388,7 @@ void PeerHost::growPeers()
{
RLPStream s;
bytes b;
(PeerSession::prep(s).appendList(1) << GetPeersPacket).swapOut(b);
(Session::prep(s).appendList(1) << GetPeersPacket).swapOut(b);
seal(b);
for (auto const& i: m_peers)
if (auto p = i.second.lock())
@ -410,7 +411,7 @@ void PeerHost::growPeers()
}
}
void PeerHost::prunePeers()
void Host::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.
@ -419,11 +420,11 @@ void PeerHost::prunePeers()
{
// look for worst peer to kick off
// first work out how many are old enough to kick off.
shared_ptr<PeerSession> worst;
shared_ptr<Session> worst;
unsigned agedPeers = 0;
for (auto i: m_peers)
if (auto p = i.second.lock())
if (/*(m_mode != NodeMode::PeerHost || p->m_caps != 0x01) &&*/ chrono::steady_clock::now() > p->m_connect + chrono::milliseconds(old)) // don't throw off new peers; peer-servers should never kick off other peer-servers.
if (/*(m_mode != NodeMode::Host || p->m_caps != 0x01) &&*/ chrono::steady_clock::now() > p->m_connect + chrono::milliseconds(old)) // don't throw off new peers; peer-servers should never kick off other peer-servers.
{
++agedPeers;
if ((!worst || p->m_rating < worst->m_rating || (p->m_rating == worst->m_rating && p->m_connect > worst->m_connect))) // kill older ones
@ -442,11 +443,11 @@ void PeerHost::prunePeers()
i = m_peers.erase(i);
}
std::vector<PeerInfo> PeerHost::peers(bool _updatePing) const
std::vector<PeerInfo> Host::peers(bool _updatePing) const
{
Guard l(x_peers);
if (_updatePing)
const_cast<PeerHost*>(this)->pingAll();
const_cast<Host*>(this)->pingAll();
this_thread::sleep_for(chrono::milliseconds(200));
std::vector<PeerInfo> ret;
for (auto& i: m_peers)
@ -456,7 +457,7 @@ std::vector<PeerInfo> PeerHost::peers(bool _updatePing) const
return ret;
}
void PeerHost::process()
void Host::process()
{
for (auto const& i: m_capabilities)
if (!i.second->isInitialised())
@ -466,7 +467,7 @@ void PeerHost::process()
m_ioService.poll();
}
void PeerHost::pingAll()
void Host::pingAll()
{
Guard l(x_peers);
for (auto& i: m_peers)
@ -474,7 +475,7 @@ void PeerHost::pingAll()
j->ping();
}
bytes PeerHost::savePeers() const
bytes Host::savePeers() const
{
Guard l(x_peers);
RLPStream ret;
@ -489,7 +490,7 @@ bytes PeerHost::savePeers() const
return RLPStream(n).appendRaw(ret.out(), n).out();
}
void PeerHost::restorePeers(bytesConstRef _b)
void Host::restorePeers(bytesConstRef _b)
{
for (auto i: RLP(_b))
{

20
libp2p/PeerHost.h → libp2p/Host.h

@ -29,7 +29,7 @@
#include <utility>
#include <thread>
#include <libethential/Guards.h>
#include "Common.h"
#include "HostCapability.h"
namespace ba = boost::asio;
namespace bi = boost::asio::ip;
@ -43,24 +43,24 @@ class BlockQueue;
using eth::Guard;
/**
* @brief The PeerHost class
* @brief The Host class
* Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe.
*/
class PeerHost
class Host
{
friend class PeerSession;
friend class Session;
friend class HostCapabilityFace;
public:
/// Start server, listening for connections on the given port.
PeerHost(std::string const& _clientVersion, unsigned short _port, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false);
Host(std::string const& _clientVersion, unsigned short _port, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false);
/// Start server, listening for connections on a system-assigned port.
PeerHost(std::string const& _clientVersion, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false);
Host(std::string const& _clientVersion, std::string const& _publicAddress = std::string(), bool _upnp = true, bool _localNetworking = false);
/// Start server, but don't listen.
PeerHost(std::string const& _clientVersion);
Host(std::string const& _clientVersion);
/// Will block on network process events.
virtual ~PeerHost();
virtual ~Host();
/// Closes all peers.
void disconnectPeers();
@ -110,7 +110,7 @@ public:
h512 id() const { return m_id; }
void registerPeer(std::shared_ptr<PeerSession> _s, std::vector<std::string> const& _caps);
void registerPeer(std::shared_ptr<Session> _s, std::vector<std::string> const& _caps);
protected:
/// Called when the session has provided us with a new peer we can connect to.
@ -140,7 +140,7 @@ protected:
h512 m_id;
mutable std::mutex x_peers;
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.
mutable std::map<h512, std::weak_ptr<Session>> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method.
std::map<h512, std::pair<bi::tcp::endpoint, unsigned>> m_incomingPeers; // TODO: does this need a lock?
std::vector<h512> m_freePeers;

44
libp2p/HostCapability.cpp

@ -0,0 +1,44 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file HostCapability.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "HostCapability.h"
#include "Session.h"
#include "Host.h"
using namespace std;
using namespace eth;
using namespace p2p;
void HostCapabilityFace::seal(bytes& _b)
{
m_host->seal(_b);
}
std::vector<std::shared_ptr<Session> > HostCapabilityFace::peers() const
{
Guard l(m_host->x_peers);
std::vector<std::shared_ptr<Session> > ret;
for (auto const& i: m_host->m_peers)
if (std::shared_ptr<Session> p = i.second.lock())
if (p->m_capabilities.count(name()))
ret.push_back(p);
return ret;
}

70
libp2p/HostCapability.h

@ -0,0 +1,70 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Common.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*
* Miscellanea required for the Host/Session classes.
*/
#pragma once
#include "Common.h"
namespace p2p
{
class HostCapabilityFace
{
friend class Host;
template <class T> friend class HostCapability;
friend class Capability;
public:
HostCapabilityFace() {}
virtual ~HostCapabilityFace() {}
Host* host() const { return m_host; }
std::vector<std::shared_ptr<Session> > peers() const;
protected:
virtual std::string name() const = 0;
virtual Capability* newPeerCapability(Session* _s) = 0;
virtual bool isInitialised() const { return true; }
void seal(bytes& _b);
private:
Host* m_host = nullptr;
};
template<class PeerCap>
class HostCapability: public HostCapabilityFace
{
public:
HostCapability() {}
virtual ~HostCapability() {}
static std::string staticName() { return PeerCap::name(); }
protected:
virtual std::string name() const { return PeerCap::name(); }
virtual Capability* newPeerCapability(Session* _s) { return new PeerCap(_s, this); }
};
}

41
libp2p/PeerSession.cpp → libp2p/Session.cpp

@ -14,24 +14,25 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file PeerSession.cpp
/** @file Session.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "PeerSession.h"
#include "Session.h"
#include <chrono>
#include <libethential/Common.h>
#include <libethcore/Exceptions.h>
#include "PeerHost.h"
#include "Host.h"
#include "Capability.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() << "] "
PeerSession::PeerSession(PeerHost* _s, bi::tcp::socket _socket, bi::address _peerAddress, unsigned short _peerPort):
Session::Session(Host* _s, bi::tcp::socket _socket, bi::address _peerAddress, unsigned short _peerPort):
m_server(_s),
m_socket(std::move(_socket)),
m_listenPort(_peerPort),
@ -42,7 +43,7 @@ PeerSession::PeerSession(PeerHost* _s, bi::tcp::socket _socket, bi::address _pee
m_info = PeerInfo({"?", _peerAddress.to_string(), m_listenPort, std::chrono::steady_clock::duration(0)});
}
PeerSession::~PeerSession()
Session::~Session()
{
// Read-chain finished for one reason or another.
for (auto& i: m_capabilities)
@ -56,7 +57,7 @@ PeerSession::~PeerSession()
catch (...){}
}
bi::tcp::endpoint PeerSession::endpoint() const
bi::tcp::endpoint Session::endpoint() const
{
if (m_socket.is_open())
try
@ -68,7 +69,7 @@ bi::tcp::endpoint PeerSession::endpoint() const
return bi::tcp::endpoint();
}
bool PeerSession::interpret(RLP const& _r)
bool Session::interpret(RLP const& _r)
{
clogS(NetRight) << _r;
switch (_r[0].toInt<unsigned>())
@ -192,25 +193,25 @@ bool PeerSession::interpret(RLP const& _r)
return true;
}
void PeerSession::ping()
void Session::ping()
{
RLPStream s;
sealAndSend(prep(s).appendList(1) << PingPacket);
m_ping = std::chrono::steady_clock::now();
}
void PeerSession::getPeers()
void Session::getPeers()
{
RLPStream s;
sealAndSend(prep(s).appendList(1) << GetPeersPacket);
}
RLPStream& PeerSession::prep(RLPStream& _s)
RLPStream& Session::prep(RLPStream& _s)
{
return _s.appendRaw(bytes(8, 0));
}
void PeerSession::sealAndSend(RLPStream& _s)
void Session::sealAndSend(RLPStream& _s)
{
bytes b;
_s.swapOut(b);
@ -218,7 +219,7 @@ void PeerSession::sealAndSend(RLPStream& _s)
sendDestroy(b);
}
bool PeerSession::checkPacket(bytesConstRef _msg)
bool Session::checkPacket(bytesConstRef _msg)
{
if (_msg.size() < 8)
return false;
@ -233,7 +234,7 @@ bool PeerSession::checkPacket(bytesConstRef _msg)
return true;
}
void PeerSession::sendDestroy(bytes& _msg)
void Session::sendDestroy(bytes& _msg)
{
clogS(NetLeft) << RLP(bytesConstRef(&_msg).cropped(8));
@ -246,7 +247,7 @@ void PeerSession::sendDestroy(bytes& _msg)
writeImpl(buffer);
}
void PeerSession::send(bytesConstRef _msg)
void Session::send(bytesConstRef _msg)
{
clogS(NetLeft) << RLP(_msg.cropped(8));
@ -259,7 +260,7 @@ void PeerSession::send(bytesConstRef _msg)
writeImpl(buffer);
}
void PeerSession::writeImpl(bytes& _buffer)
void Session::writeImpl(bytes& _buffer)
{
// cerr << (void*)this << " writeImpl" << endl;
if (!m_socket.is_open())
@ -271,7 +272,7 @@ void PeerSession::writeImpl(bytes& _buffer)
write();
}
void PeerSession::write()
void Session::write()
{
// cerr << (void*)this << " write" << endl;
lock_guard<recursive_mutex> l(m_writeLock);
@ -298,7 +299,7 @@ void PeerSession::write()
});
}
void PeerSession::dropped()
void Session::dropped()
{
// cerr << (void*)this << " dropped" << endl;
if (m_socket.is_open())
@ -310,7 +311,7 @@ void PeerSession::dropped()
catch (...) {}
}
void PeerSession::disconnect(int _reason)
void Session::disconnect(int _reason)
{
clogS(NetConnect) << "Disconnecting (reason:" << reasonOf((DisconnectReason)_reason) << ")";
if (m_socket.is_open())
@ -328,7 +329,7 @@ void PeerSession::disconnect(int _reason)
}
}
void PeerSession::start()
void Session::start()
{
RLPStream s;
prep(s);
@ -345,7 +346,7 @@ void PeerSession::start()
doRead();
}
void PeerSession::doRead()
void Session::doRead()
{
// ignore packets received while waiting to disconnect
if (chrono::steady_clock::now() - m_disconnect > chrono::seconds(0))

16
libp2p/PeerSession.h → libp2p/Session.h

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file PeerSession.h
/** @file Session.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
@ -33,17 +33,17 @@ namespace p2p
{
/**
* @brief The PeerSession class
* @brief The Session class
* @todo Document fully.
*/
class PeerSession: public std::enable_shared_from_this<PeerSession>
class Session: public std::enable_shared_from_this<Session>
{
friend class PeerHost;
friend class Host;
friend class HostCapabilityFace;
public:
PeerSession(PeerHost* _server, bi::tcp::socket _socket, bi::address _peerAddress, unsigned short _peerPort = 0);
virtual ~PeerSession();
Session(Host* _server, bi::tcp::socket _socket, bi::address _peerAddress, unsigned short _peerPort = 0);
virtual ~Session();
void start();
void disconnect(int _reason);
@ -80,7 +80,7 @@ private:
/// @returns true iff the _msg forms a valid message for sending or receiving on the network.
static bool checkPacket(bytesConstRef _msg);
PeerHost* m_server;
Host* m_server;
std::recursive_mutex m_writeLock;
std::deque<bytes> m_writeQueue;
@ -100,7 +100,7 @@ private:
uint m_rating;
std::map<std::string, std::shared_ptr<PeerCapability>> m_capabilities;
std::map<std::string, std::shared_ptr<Capability>> m_capabilities;
bool m_willBeDeleted = false; ///< True if we already posted a deleter on the strand.
};

2
libwhisper/Common.h

@ -26,7 +26,7 @@
#include <libethential/Common.h>
#include <libethential/Log.h>
#include <libethcore/CommonEth.h>
#include <libp2p/Common.h>
#include <libp2p/Capability.h>
namespace shh
{

4
libwhisper/WhisperPeer.cpp

@ -30,7 +30,7 @@ using namespace shh;
#define clogS(X) eth::LogOutputStream<X, true>(false) << "| " << std::setw(2) << session()->socketId() << "] "
WhisperPeer::WhisperPeer(PeerSession* _s, HostCapabilityFace* _h): PeerCapability(_s, _h)
WhisperPeer::WhisperPeer(Session* _s, HostCapabilityFace* _h): Capability(_s, _h)
{
RLPStream s;
prep(s);
@ -43,7 +43,7 @@ WhisperPeer::~WhisperPeer()
WhisperHost* WhisperPeer::host() const
{
return static_cast<WhisperHost*>(PeerCapability::hostCapability());
return static_cast<WhisperHost*>(Capability::hostCapability());
}
bool WhisperPeer::interpret(RLP const& _r)

8
libwhisper/WhisperPeer.h

@ -34,10 +34,10 @@
namespace shh
{
using p2p::PeerSession;
using p2p::Session;
using p2p::HostCapabilityFace;
using p2p::HostCapability;
using p2p::PeerCapability;
using p2p::Capability;
struct Message
{
@ -64,12 +64,12 @@ struct Message
/**
*/
class WhisperPeer: public PeerCapability
class WhisperPeer: public Capability
{
friend class WhisperHost;
public:
WhisperPeer(PeerSession* _s, HostCapabilityFace* _h);
WhisperPeer(Session* _s, HostCapabilityFace* _h);
virtual ~WhisperPeer();
static std::string name() { return "shh"; }

4
test/peer.cpp

@ -22,7 +22,7 @@
#include <chrono>
#include <thread>
#include <libp2p/PeerHost.h>
#include <libp2p/Host.h>
using namespace std;
using namespace eth;
using namespace p2p;
@ -46,7 +46,7 @@ int peerTest(int argc, char** argv)
remoteHost = argv[i];
}
PeerHost ph("Test", listenPort);
Host ph("Test", listenPort);
if (!remoteHost.empty())
ph.connect(remoteHost, remotePort);

Loading…
Cancel
Save