Browse Source

Merge branch 'develop' into changetracking

Conflicts:
	libethereum/PeerSession.cpp
cl-refactor
Gav Wood 11 years ago
parent
commit
931827140c
  1. 57
      alethzero/Main.ui
  2. 4
      alethzero/MainWin.cpp
  3. 13
      libethereum/PeerServer.cpp
  4. 121
      libethereum/PeerSession.cpp
  5. 8
      libethereum/PeerSession.h
  6. 4
      walleth/MainWin.cpp

57
alethzero/Main.ui

@ -269,7 +269,7 @@
</widget>
<widget class="QWidget" name="layoutWidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<item row="1" column="2">
<widget class="QSpinBox" name="port">
<property name="minimum">
<number>1024</number>
@ -282,53 +282,66 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="idealPeers">
<property name="minimum">
<number>1</number>
<item row="1" column="1">
<widget class="QLineEdit" name="forceAddress">
<property name="inputMask">
<string/>
</property>
<property name="value">
<number>5</number>
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>Automatic</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Ideal &amp;Peers</string>
<string>&amp;Listen on</string>
</property>
<property name="buddy">
<cstring>idealPeers</cstring>
<cstring>port</cstring>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Client Name</string>
<string>Ideal &amp;Peers</string>
</property>
<property name="buddy">
<cstring>clientName</cstring>
<cstring>idealPeers</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Listen on</string>
<item row="0" column="1" colspan="2">
<widget class="QSpinBox" name="idealPeers">
<property name="minimum">
<number>1</number>
</property>
<property name="buddy">
<cstring>port</cstring>
<property name="value">
<number>5</number>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="clientName">
<property name="placeholderText">
<string>Anonymous</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>&amp;Client Name</string>
</property>
<property name="buddy">
<cstring>clientName</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</widget>

4
alethzero/MainWin.cpp

@ -469,6 +469,7 @@ void Main::writeSettings()
s.setValue("address", b);
s.setValue("upnp", ui->upnp->isChecked());
s.setValue("forceAddress", ui->forceAddress->text());
s.setValue("usePast", ui->usePast->isChecked());
s.setValue("paranoia", ui->paranoia->isChecked());
s.setValue("showAll", ui->showAll->isChecked());
@ -515,6 +516,7 @@ void Main::readSettings()
m_client->setAddress(m_myKeys.back().address());
m_peers = s.value("peers").toByteArray();
ui->upnp->setChecked(s.value("upnp", true).toBool());
ui->forceAddress->setText(s.value("forceAddress", "").toString());
ui->usePast->setChecked(s.value("usePast", true).toBool());
ui->paranoia->setChecked(s.value("paranoia", false).toBool());
ui->showAll->setChecked(s.value("showAll", false).toBool());
@ -1386,7 +1388,7 @@ void Main::on_net_triggered()
m_client->setClientVersion(n);
if (ui->net->isChecked())
{
m_client->startNetwork(ui->port->value(), string(), 0, NodeMode::Full, ui->idealPeers->value(), std::string(), ui->upnp->isChecked());
m_client->startNetwork(ui->port->value(), string(), 0, NodeMode::Full, ui->idealPeers->value(), ui->forceAddress->text().toStdString(), ui->upnp->isChecked());
if (m_peers.size() && ui->usePast->isChecked())
m_client->peerServer()->restorePeers(bytesConstRef((byte*)m_peers.data(), m_peers.size()));
}

13
libethereum/PeerServer.cpp

@ -117,6 +117,19 @@ unsigned PeerServer::protocolVersion()
return c_protocolVersion;
}
void PeerServer::seal(bytes& _b)
{
_b[0] = 0x22;
_b[1] = 0x40;
_b[2] = 0x08;
_b[3] = 0x91;
uint32_t len = (uint32_t)_b.size() - 8;
_b[4] = (len >> 24) & 0xff;
_b[5] = (len >> 16) & 0xff;
_b[6] = (len >> 8) & 0xff;
_b[7] = len & 0xff;
}
void PeerServer::determinePublic(string const& _publicAddress, bool _upnp)
{
if (_upnp)

121
libethereum/PeerSession.cpp

@ -31,13 +31,12 @@ using namespace eth;
#define clogS(X) eth::LogOutputStream<X, true>(false) << "| " << std::setw(2) << m_socket.native_handle() << "] "
static const eth::uint c_maxHashes = 32; ///< Maximum number of hashes GetChain will ever send.
static const eth::uint c_maxBlocks = 64; ///< Maximum number of blocks Blocks will ever send. BUG: if this gets too big (e.g. 2048) stuff starts going wrong.
static const eth::uint c_maxBlocksAsk = 256; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain).
static const eth::uint c_maxHashes = 4096; ///< Maximum number of hashes GetChain will ever send.
static const eth::uint c_maxBlocks = 2048; ///< Maximum number of blocks Blocks will ever send.
static const eth::uint c_maxBlocksAsk = 512; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain).
PeerSession::PeerSession(PeerServer* _s, bi::tcp::socket _socket, uint _rNId, bi::address _peerAddress, unsigned short _peerPort):
m_server(_s),
m_strand(_socket.get_io_service()),
m_socket(std::move(_socket)),
m_reqNetworkId(_rNId),
m_listenPort(_peerPort),
@ -50,24 +49,23 @@ PeerSession::PeerSession(PeerServer* _s, bi::tcp::socket _socket, uint _rNId, bi
PeerSession::~PeerSession()
{
m_strand.post([=]()
// Read-chain finished for one reason or another.
try
{
if (!m_writeq.empty())
m_writeq.clear();
try {
if (m_socket.is_open())
m_socket.close();
}catch (...){}
});
if (m_socket.is_open())
m_socket.close();
}
catch (...){}
}
bi::tcp::endpoint PeerSession::endpoint() const
{
if (m_socket.is_open())
try {
try
{
return bi::tcp::endpoint(m_socket.remote_endpoint().address(), m_listenPort);
} catch (...){}
}
catch (...){}
return bi::tcp::endpoint();
}
@ -395,19 +393,6 @@ RLPStream& PeerSession::prep(RLPStream& _s)
return _s.appendRaw(bytes(8, 0));
}
void PeerServer::seal(bytes& _b)
{
_b[0] = 0x22;
_b[1] = 0x40;
_b[2] = 0x08;
_b[3] = 0x91;
uint32_t len = (uint32_t)_b.size() - 8;
_b[4] = (len >> 24) & 0xff;
_b[5] = (len >> 16) & 0xff;
_b[6] = (len >> 8) & 0xff;
_b[7] = len & 0xff;
}
void PeerSession::sealAndSend(RLPStream& _s)
{
bytes b;
@ -441,7 +426,7 @@ void PeerSession::sendDestroy(bytes& _msg)
}
bytes buffer = bytes(std::move(_msg));
m_strand.post(boost::bind(&PeerSession::writeImpl, this, buffer));
writeImpl(buffer);
}
void PeerSession::send(bytesConstRef _msg)
@ -454,59 +439,66 @@ void PeerSession::send(bytesConstRef _msg)
}
bytes buffer = bytes(_msg.toBytes());
m_strand.post(boost::bind(&PeerSession::writeImpl, this, buffer));
writeImpl(buffer);
}
void PeerSession::writeImpl(bytes& _buffer)
{
m_writeq.push_back(_buffer);
if (m_writeq.size() > 1)
// cerr << (void*)this << " writeImpl" << endl;
if (!m_socket.is_open())
return;
write();
lock_guard<recursive_mutex> l(m_writeLock);
m_writeQueue.push_back(_buffer);
if (m_writeQueue.size() == 1)
write();
}
void PeerSession::write()
{
if (m_writeq.empty())
// cerr << (void*)this << " write" << endl;
lock_guard<recursive_mutex> l(m_writeLock);
if (m_writeQueue.empty())
return;
const bytes& bytes = m_writeq[0];
if (m_socket.is_open())
ba::async_write(m_socket, ba::buffer(bytes), m_strand.wrap([this](boost::system::error_code ec, std::size_t /*length*/)
const bytes& bytes = m_writeQueue[0];
auto self(shared_from_this());
ba::async_write(m_socket, ba::buffer(bytes), [this, self](boost::system::error_code ec, std::size_t /*length*/)
{
// cerr << (void*)this << " write.callback" << endl;
// must check queue, as write callback can occur following dropped()
if (ec)
{
// must check que, as write callback can occur following dropped()
if (!m_writeq.empty())
m_writeq.pop_front();
if (ec)
{
cwarn << "Error sending: " << ec.message();
dropped();
} else
m_strand.post(boost::bind(&PeerSession::write, this));
}));
cwarn << "Error sending: " << ec.message();
dropped();
}
else
{
m_writeQueue.pop_front();
write();
}
});
}
void PeerSession::dropped()
{
// cerr << (void*)this << " dropped" << endl;
if (m_socket.is_open())
try {
try
{
clogS(NetNote) << "Closing " << m_socket.remote_endpoint();
m_socket.close();
}catch (...){}
// block future writes by running in strand and clearing queue
m_strand.post([=]()
{
m_writeq.clear();
for (auto i = m_server->m_peers.begin(); i != m_server->m_peers.end(); ++i)
if (i->second.lock().get() == this)
{
m_server->m_peers.erase(i);
break;
}
});
}
catch (...) {}
// Remove from peer server
for (auto i = m_server->m_peers.begin(); i != m_server->m_peers.end(); ++i)
if (i->second.lock().get() == this)
{
m_server->m_peers.erase(i);
break;
}
}
void PeerSession::disconnect(int _reason)
@ -555,7 +547,7 @@ void PeerSession::doRead()
cwarn << "Error reading: " << ec.message();
dropped();
}
else if(ec && length == 0)
else if (ec && length == 0)
{
return;
}
@ -568,10 +560,7 @@ void PeerSession::doRead()
while (m_incoming.size() > 8)
{
if (m_incoming[0] != 0x22 || m_incoming[1] != 0x40 || m_incoming[2] != 0x08 || m_incoming[3] != 0x91)
{
doRead();
}
else
{
uint32_t len = fromBigEndian<uint32_t>(bytesConstRef(m_incoming.data() + 4, 4));

8
libethereum/PeerSession.h

@ -21,6 +21,7 @@
#pragma once
#include <mutex>
#include <array>
#include <set>
#include <memory>
@ -65,8 +66,9 @@ private:
void writeImpl(bytes& _buffer);
void write();
PeerServer* m_server;
boost::asio::strand m_strand;
std::deque<bytes> m_writeq;
std::recursive_mutex m_writeLock;
std::deque<bytes> m_writeQueue;
bi::tcp::socket m_socket;
std::array<byte, 65536> m_data;
@ -89,6 +91,8 @@ private:
std::set<h256> m_knownBlocks;
std::set<h256> m_knownTransactions;
bool m_willBeDeleted = false; ///< True if we already posted a deleter on the strand.
};
}

4
walleth/MainWin.cpp

@ -239,10 +239,10 @@ void Main::on_net_triggered(bool _auto)
if (_auto)
{
QString s = m_servers[rand() % m_servers.size()];
client()->startNetwork(m_port, s.section(':', 0, 0).toStdString(), s.section(':', 1).toInt(), NodeMode::Full, m_idealPeers, std::string(), ui->upnp->isChecked());
client()->startNetwork(m_port, s.section(':', 0, 0).toStdString(), s.section(':', 1).toInt(), NodeMode::Full, m_idealPeers, "", ui->upnp->isChecked());
}
else
client()->startNetwork(m_port, string(), 0, NodeMode::Full, m_idealPeers, std::string(), ui->upnp->isChecked());
client()->startNetwork(m_port, string(), 0, NodeMode::Full, m_idealPeers, "", ui->upnp->isChecked());
if (m_peers.size())
client()->peerServer()->restorePeers(bytesConstRef((byte*)m_peers.data(), m_peers.size()));
}

Loading…
Cancel
Save