Browse Source

Merge branch 'netFix' of https://github.com/subtly/cpp-ethereum-micro into subtly-netFix

Conflicts:
	libethereum/EthereumHost.cpp
	libethereum/EthereumHost.h
	libp2p/HostCapability.h
cl-refactor
Gav Wood 10 years ago
parent
commit
6d50f795b6
  1. 4
      libdevcore/TransientDirectory.cpp
  2. 40
      libethereum/EthereumHost.cpp
  3. 4
      libethereum/EthereumHost.h
  4. 10
      libp2p/Session.cpp
  5. 2
      libp2p/UDP.h

4
libdevcore/TransientDirectory.cpp

@ -46,7 +46,7 @@ TransientDirectory::~TransientDirectory()
{ {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::remove_all(m_path, ec); boost::filesystem::remove_all(m_path, ec);
if (0 == ec) if (!ec)
return; return;
// In some cases, antivirus runnig on Windows will scan all the newly created directories. // In some cases, antivirus runnig on Windows will scan all the newly created directories.
@ -57,6 +57,6 @@ TransientDirectory::~TransientDirectory()
ec.clear(); ec.clear();
boost::filesystem::remove_all(m_path, ec); boost::filesystem::remove_all(m_path, ec);
if (ec != 0) if (!ec)
cwarn << "Failed to delete directory '" << m_path << "': " << ec.message(); cwarn << "Failed to delete directory '" << m_path << "': " << ec.message();
} }

40
libethereum/EthereumHost.cpp

@ -120,7 +120,7 @@ void EthereumHost::maintainTransactions()
for (auto const& i: ts) for (auto const& i: ts)
{ {
bool unsent = !m_transactionsSent.count(i.first); bool unsent = !m_transactionsSent.count(i.first);
for (auto const& p: randomSelection(0, [&](EthereumPeer* p) { return p->m_requireTransactions || (unsent && !p->m_knownTransactions.count(i.first)); }).second) for (auto const& p: get<1>(randomSelection(0, [&](EthereumPeer* p) { return p->m_requireTransactions || (unsent && !p->m_knownTransactions.count(i.first)); })))
peerTransactions[p].push_back(i.first); peerTransactions[p].push_back(i.first);
} }
for (auto const& t: ts) for (auto const& t: ts)
@ -165,24 +165,32 @@ void EthereumHost::forEachPeerPtr(std::function<void(std::shared_ptr<EthereumPee
_f(s.first->cap<EthereumPeer>(c_oldProtocolVersion)); _f(s.first->cap<EthereumPeer>(c_oldProtocolVersion));
} }
pair<vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<EthereumPeer>>> EthereumHost::randomSelection(unsigned _percent, std::function<bool(EthereumPeer*)> const& _allow) tuple<vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<Session>>> EthereumHost::randomSelection(unsigned _percent, std::function<bool(EthereumPeer*)> const& _allow)
{ {
pair<vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<EthereumPeer>>> ret; vector<shared_ptr<EthereumPeer>> chosen;
forEachPeerPtr([&](shared_ptr<EthereumPeer> _p) vector<shared_ptr<EthereumPeer>> allowed;
vector<shared_ptr<Session>> sessions;
auto const& ps = peerSessions();
allowed.reserve(ps.size());
for (auto const& j: ps)
{ {
if (_p && _allow(_p.get())) auto pp = j.first->cap<EthereumPeer>();
ret.second.push_back(_p); if (_allow(pp.get()))
}); {
allowed.push_back(move(pp));
sessions.push_back(move(j.first));
}
}
size_t size = (ret.second.size() * _percent + 99) / 100; chosen.reserve((ps.size() * _percent + 99) / 100);
ret.second.reserve(size); for (unsigned i = (ps.size() * _percent + 99) / 100; i-- && allowed.size();)
for (unsigned i = size; i-- && ret.second.size();)
{ {
unsigned n = rand() % ret.second.size(); unsigned n = rand() % allowed.size();
ret.first.push_back(std::move(ret.second[n])); chosen.push_back(std::move(allowed[n]));
ret.second.erase(ret.second.begin() + n); allowed.erase(allowed.begin() + n);
} }
return ret; return make_tuple(move(chosen), move(allowed), move(sessions));
} }
void EthereumHost::maintainBlocks(h256 const& _currentHash) void EthereumHost::maintainBlocks(h256 const& _currentHash)
@ -200,7 +208,7 @@ void EthereumHost::maintainBlocks(h256 const& _currentHash)
h256s blocks = get<0>(m_chain.treeRoute(m_latestBlockSent, _currentHash, false, false, true)); h256s blocks = get<0>(m_chain.treeRoute(m_latestBlockSent, _currentHash, false, false, true));
auto s = randomSelection(25, [&](EthereumPeer* p){ DEV_GUARDED(p->x_knownBlocks) return !p->m_knownBlocks.count(_currentHash); return false; }); auto s = randomSelection(25, [&](EthereumPeer* p){ DEV_GUARDED(p->x_knownBlocks) return !p->m_knownBlocks.count(_currentHash); return false; });
for (shared_ptr<EthereumPeer> const& p: s.first) for (shared_ptr<EthereumPeer> const& p: get<0>(s))
for (auto const& b: blocks) for (auto const& b: blocks)
{ {
RLPStream ts; RLPStream ts;
@ -210,7 +218,7 @@ void EthereumHost::maintainBlocks(h256 const& _currentHash)
p->sealAndSend(ts); p->sealAndSend(ts);
p->m_knownBlocks.clear(); p->m_knownBlocks.clear();
} }
for (shared_ptr<EthereumPeer> const& p: s.second) for (shared_ptr<EthereumPeer> const& p: get<1>(s))
{ {
RLPStream ts; RLPStream ts;
p->prep(ts, NewBlockHashesPacket, blocks.size()); p->prep(ts, NewBlockHashesPacket, blocks.size());

4
libethereum/EthereumHost.h

@ -90,8 +90,10 @@ public:
BlockChain const& chain() { return m_chain; } BlockChain const& chain() { return m_chain; }
static unsigned const c_oldProtocolVersion; static unsigned const c_oldProtocolVersion;
private: private:
std::pair<std::vector<std::shared_ptr<EthereumPeer>>, std::vector<std::shared_ptr<EthereumPeer>>> randomSelection(unsigned _percent = 25, std::function<bool(EthereumPeer*)> const& _allow = [](EthereumPeer const*){ return true; }); std::tuple<std::vector<std::shared_ptr<EthereumPeer>>, std::vector<std::shared_ptr<EthereumPeer>>, std::vector<std::shared_ptr<p2p::Session>>> randomSelection(unsigned _percent = 25, std::function<bool(EthereumPeer*)> const& _allow = [](EthereumPeer const*){ return true; });
void forEachPeerPtr(std::function<void(std::shared_ptr<EthereumPeer>)> const& _f) const; void forEachPeerPtr(std::function<void(std::shared_ptr<EthereumPeer>)> const& _f) const;
void forEachPeer(std::function<void(EthereumPeer*)> const& _f) const; void forEachPeer(std::function<void(EthereumPeer*)> const& _f) const;

10
libp2p/Session.cpp

@ -319,10 +319,14 @@ void Session::send(bytes&& _msg)
void Session::write() void Session::write()
{ {
const bytes& bytes = m_writeQueue[0]; bytes const* out;
m_io->writeSingleFramePacket(&bytes, m_writeQueue[0]); DEV_GUARDED(x_writeQueue)
{
m_io->writeSingleFramePacket(&m_writeQueue[0], m_writeQueue[0]);
out = &m_writeQueue[0];
}
auto self(shared_from_this()); auto self(shared_from_this());
ba::async_write(m_socket, ba::buffer(bytes), [this, self](boost::system::error_code ec, std::size_t /*length*/) ba::async_write(m_socket, ba::buffer(*out), [this, self](boost::system::error_code ec, std::size_t /*length*/)
{ {
ThreadContext tc(info().id.abridged()); ThreadContext tc(info().id.abridged());
ThreadContext tc2(info().clientVersion); ThreadContext tc2(info().clientVersion);

2
libp2p/UDP.h

@ -99,7 +99,7 @@ struct UDPSocketFace
*/ */
struct UDPSocketEvents struct UDPSocketEvents
{ {
virtual void onDisconnected(UDPSocketFace*) {}; virtual void onDisconnected(UDPSocketFace*) {}
virtual void onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytesConstRef _packetData) = 0; virtual void onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytesConstRef _packetData) = 0;
}; };

Loading…
Cancel
Save