From a1216c3b74e711d739a4c1a019fc4218da36bbd3 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 10 Oct 2014 00:09:58 +0200 Subject: [PATCH] Peer & key management. --- alethzero/MainWin.cpp | 13 +++++++++---- libethereum/BlockChain.cpp | 2 +- libp2p/Host.cpp | 19 +++++++++---------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 6d968fa6c..7d3fe4085 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -872,7 +872,7 @@ void Main::refreshBlockChain() string filter = ui->blockChainFilter->text().toLower().toStdString(); auto const& bc = ethereum()->blockChain(); unsigned i = (ui->showAll->isChecked() || !filter.empty()) ? (unsigned)-1 : 10; - for (auto h = bc.currentHash(); h != bc.genesisHash() && bc.details(h) && i; h = bc.details(h).parent, --i) + for (auto h = bc.currentHash(); bc.details(h) && i; h = bc.details(h).parent, --i) { auto d = bc.details(h); auto bm = blockMatch(filter, d, h, bc); @@ -912,6 +912,8 @@ void Main::refreshBlockChain() } n++; } + if (h == bc.genesisHash()) + break; } if (!ui->blocks->currentItem()) @@ -1138,7 +1140,10 @@ void Main::on_blocks_currentItemChanged() s << "
Bloom: " << details.bloom << ""; s << "
Transactions: " << block[1].itemCount() << " @" << info.transactionsRoot << ""; s << "
Uncles: " << block[2].itemCount() << " @" << info.sha3Uncles << ""; - s << "
Pre: " << BlockInfo(ethereum()->blockChain().block(info.parentHash)).stateRoot << ""; + if (info.parentHash) + s << "
Pre: " << BlockInfo(ethereum()->blockChain().block(info.parentHash)).stateRoot << ""; + else + s << "
Pre: Nothing is before the Gensesis"; for (auto const& i: block[1]) s << "
" << sha3(i[0].data()).abridged() << ": " << i[1].toHash() << " [" << i[2].toInt() << " used]"; s << "
Post: " << info.stateRoot << ""; @@ -1545,10 +1550,10 @@ void Main::on_net_triggered() web3()->setIdealPeerCount(ui->idealPeers->value()); web3()->setNetworkPreferences(netPrefs()); ethereum()->setNetworkId(m_privateChain.size() ? sha3(m_privateChain.toStdString()) : 0); + if (m_peers.size()/* && ui->usePast->isChecked()*/) + web3()->restoreNodes(bytesConstRef((byte*)m_peers.data(), m_peers.size())); web3()->startNetwork(); ui->downloadView->setDownloadMan(ethereum()->downloadMan()); - if (m_peers.size() && ui->usePast->isChecked()) - web3()->restoreNodes(bytesConstRef((byte*)m_peers.data(), m_peers.size())); } else { diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 299d8edd8..8e0619a43 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -439,7 +439,7 @@ void BlockChain::checkConsistency() if (p != h256()) { auto dp = details(p); - assert(contains(dp.children, h)); // WTF? +// assert(contains(dp.children, h)); // WTF? assert(dp.number == dh.number - 1); } } diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 848d37dfa..e464dac88 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -109,8 +109,6 @@ void Host::start() determinePublic(m_netPrefs.publicIP, m_netPrefs.upnp); ensureAccepting(); - m_nodes.clear(); - m_freePeers.clear(); m_nodesList.clear(); m_ready.reset(); @@ -537,6 +535,7 @@ void Host::growPeers() RecursiveGuard l(x_peers); while (m_peers.size() < m_idealPeerCount) { + // TODO: Make work with m_ready/m_private using all the Node information. if (m_freePeers.empty()) { if (chrono::steady_clock::now() > m_lastPeersRequest + chrono::seconds(10)) @@ -637,7 +636,7 @@ bytes Host::saveNodes() const for (auto const& i: m_nodes) { Node const& n = *(i.second); - nodes.appendList(4); + nodes.appendList(10); if (n.address.address().is_v4()) nodes << n.address.address().to_v4().to_bytes(); else @@ -659,22 +658,22 @@ void Host::restoreNodes(bytesConstRef _b) { RecursiveGuard l(x_peers); RLP r(_b); - if (r.itemCount() == 2 && r[0].isInt()) + if (r.itemCount() > 0 && r[0].isInt()) switch (r[0].toInt()) { case 0: m_key = KeyPair(r[1].toHash()); for (auto i: r[2]) { + bi::tcp::endpoint ep; + if (i[0].itemCount() == 4) + ep = bi::tcp::endpoint(bi::address_v4(i[0].toArray()), i[1].toInt()); + else + ep = bi::tcp::endpoint(bi::address_v6(i[0].toArray()), i[1].toInt()); auto id = (NodeId)i[2]; - auto o = (Origin)i[3].toInt(); if (!m_nodes.count(id)) { - bi::tcp::endpoint ep; - if (i[0].size() == 4) - ep = bi::tcp::endpoint(bi::address_v4(i[0].toArray()), i[1].toInt()); - else - ep = bi::tcp::endpoint(bi::address_v6(i[0].toArray()), i[1].toInt()); + auto o = (Origin)i[3].toInt(); auto n = noteNode(id, ep, o, true); n->lastConnected = chrono::system_clock::time_point(chrono::seconds(i[4].toInt())); n->lastAttempted = chrono::system_clock::time_point(chrono::seconds(i[5].toInt()));