Browse Source

Fix for initial network-peer state.

cl-refactor
Gav Wood 11 years ago
parent
commit
0250d2f696
  1. 2
      libethereum/BlockChain.cpp
  2. 20
      libethereum/PeerNetwork.cpp
  3. 8
      libethereum/PeerNetwork.h

2
libethereum/BlockChain.cpp

@ -199,7 +199,7 @@ void BlockChain::import(bytes const& _block, Overlay const& _db)
{ {
m_lastBlockHash = newHash; m_lastBlockHash = newHash;
m_detailsDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32)); m_detailsDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32));
clog(BlockChainNote) << " Imported and best. Has" << details(bi.parentHash).children.size() << "siblings."; clog(BlockChainNote) << " Imported and best. Has" << (details(bi.parentHash).children.size() - 1) << "siblings.";
} }
else else
{ {

20
libethereum/PeerNetwork.cpp

@ -169,13 +169,18 @@ bool PeerSession::interpret(RLP const& _r)
// Grab their block chain off them. // Grab their block chain off them.
{ {
clogS(NetNote) << "Want chain. Latest:" << m_server->m_latestBlockSent << ", number:" << m_server->m_chain->details(m_server->m_latestBlockSent).number;
unsigned count = std::min<unsigned>(c_maxHashes, m_server->m_chain->details(m_server->m_latestBlockSent).number + 1); unsigned count = std::min<unsigned>(c_maxHashes, m_server->m_chain->details(m_server->m_latestBlockSent).number + 1);
RLPStream s; RLPStream s;
prep(s).appendList(2 + count); prep(s).appendList(2 + count);
s << GetChainPacket; s << GetChainPacket;
auto h = m_server->m_latestBlockSent; auto h = m_server->m_latestBlockSent;
for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent) for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent)
{
clogS(NetNote) << " " << i << ":" << h;
s << h; s << h;
}
s << c_maxBlocksAsk; s << c_maxBlocksAsk;
sealAndSend(s); sealAndSend(s);
s.clear(); s.clear();
@ -781,7 +786,7 @@ void PeerServer::ensureAccepting()
} }
m_accepting = false; m_accepting = false;
if (m_mode == NodeMode::PeerServer || m_peers.size() < m_idealPeerCount) if (m_mode == NodeMode::PeerServer || m_peers.size() < m_idealPeerCount * 2)
ensureAccepting(); ensureAccepting();
}); });
} }
@ -832,6 +837,7 @@ void PeerServer::connect(bi::tcp::endpoint const& _ep)
bool PeerServer::sync() bool PeerServer::sync()
{ {
bool ret = false; bool ret = false;
if (isInitialised())
for (auto i = m_peers.begin(); i != m_peers.end();) for (auto i = m_peers.begin(); i != m_peers.end();)
{ {
auto p = i->second.lock(); auto p = i->second.lock();
@ -847,10 +853,8 @@ bool PeerServer::sync()
return ret; return ret;
} }
bool PeerServer::sync(BlockChain& _bc, TransactionQueue& _tq, Overlay& _o) bool PeerServer::ensureInitialised(BlockChain& _bc, TransactionQueue& _tq)
{ {
bool ret = false;
if (m_latestBlockSent == h256()) if (m_latestBlockSent == h256())
{ {
// First time - just initialise. // First time - just initialise.
@ -860,8 +864,14 @@ bool PeerServer::sync(BlockChain& _bc, TransactionQueue& _tq, Overlay& _o)
for (auto const& i: _tq.transactions()) for (auto const& i: _tq.transactions())
m_transactionsSent.insert(i.first); m_transactionsSent.insert(i.first);
m_lastPeersRequest = chrono::steady_clock::time_point::min(); m_lastPeersRequest = chrono::steady_clock::time_point::min();
ret = true; return true;
} }
return false;
}
bool PeerServer::sync(BlockChain& _bc, TransactionQueue& _tq, Overlay& _o)
{
bool ret = ensureInitialised(_bc, _tq);
if (sync()) if (sync())
ret = true; ret = true;

8
libethereum/PeerNetwork.h

@ -168,7 +168,7 @@ public:
/// Conduct I/O, polling, syncing, whatever. /// Conduct I/O, polling, syncing, whatever.
/// Ideally all time-consuming I/O is done in a background thread or otherwise asynchronously, but you get this call every 100ms or so anyway. /// Ideally all time-consuming I/O is done in a background thread or otherwise asynchronously, but you get this call every 100ms or so anyway.
/// This won't touch alter the blockchain. /// This won't touch alter the blockchain.
void process() { m_ioService.poll(); } void process() { if (isInitialised()) m_ioService.poll(); }
/// Set ideal number of peers. /// Set ideal number of peers.
void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; } void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; }
@ -195,6 +195,12 @@ private:
void populateAddresses(); void populateAddresses();
void determinePublic(std::string const& _publicAddress, bool _upnp); void determinePublic(std::string const& _publicAddress, bool _upnp);
void ensureAccepting(); void ensureAccepting();
/// Check to see if the network peer-state initialisation has happened.
bool isInitialised() const { return m_latestBlockSent; }
/// Initialises the network peer-state, doing the stuff that needs to be once-only. @returns true if it really was first.
bool ensureInitialised(BlockChain& _bc, TransactionQueue& _tq);
std::map<Public, bi::tcp::endpoint> potentialPeers(); std::map<Public, bi::tcp::endpoint> potentialPeers();
std::string m_clientVersion; std::string m_clientVersion;

Loading…
Cancel
Save