From 6ee6e7921aa69b7c56fc15e90cc292fb8befc236 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 12 Aug 2014 20:28:35 +0200 Subject: [PATCH] Add private blockchain feature. --- alethzero/Main.ui | 15 +++++++++++++++ alethzero/MainWin.cpp | 25 ++++++++++++++++++++++--- alethzero/MainWin.h | 2 ++ libethereum/Client.cpp | 4 ++-- libethereum/Client.h | 2 +- libethereum/PeerServer.cpp | 6 +++--- libethereum/PeerServer.h | 10 +++++----- libethereum/PeerSession.cpp | 4 ++-- libethereum/PeerSession.h | 6 +++--- 9 files changed, 55 insertions(+), 19 deletions(-) diff --git a/alethzero/Main.ui b/alethzero/Main.ui index a01ac7856..5ef2b28b3 100644 --- a/alethzero/Main.ui +++ b/alethzero/Main.ui @@ -185,6 +185,8 @@ + + @@ -1664,6 +1666,19 @@ font-size: 14pt &Refresh + + + true + + + &Use Private Chain... + + + + + &Join Private Chain + + diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 6c18d39bd..30b02c626 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -527,6 +527,7 @@ void Main::writeSettings() s.setValue("idealPeers", ui->idealPeers->value()); s.setValue("port", ui->port->value()); s.setValue("url", ui->urlEdit->text()); + s.setValue("privateChain", m_privateChain); bytes d = m_client->savePeers(); if (d.size()) @@ -573,7 +574,10 @@ void Main::readSettings(bool _skipGeometry) ui->clientName->setText(s.value("clientName", "").toString()); ui->idealPeers->setValue(s.value("idealPeers", ui->idealPeers->value()).toInt()); ui->port->setValue(s.value("port", ui->port->value()).toInt()); - ui->nameReg->setText(s.value("NameReg", "").toString()); + ui->nameReg->setText(s.value("nameReg", "").toString()); + m_privateChain = s.value("privateChain", "").toString(); + ui->usePrivate->setChecked(m_privateChain.size()); + ui->urlEdit->setText(s.value("url", "about:blank").toString()); //http://gavwood.com/gavcoin.html on_urlEdit_returnPressed(); } @@ -607,6 +611,21 @@ void Main::on_exportKey_triggered() } } +void Main::on_usePrivate_triggered() +{ + if (ui->usePrivate->isChecked()) + { + m_privateChain = QInputDialog::getText(this, "Enter Name", "Enter the name of your private chain", QLineEdit::Normal, QString("NewChain-%1").arg(time(0))); + if (m_privateChain.isEmpty()) + ui->usePrivate->setChecked(false); + } + else + { + m_privateChain.clear(); + } + on_killBlockchain_triggered(); +} + void Main::on_urlEdit_returnPressed() { QString s = ui->urlEdit->text(); @@ -779,7 +798,7 @@ void Main::refreshBlockCount() cwatch << "refreshBlockCount()"; auto d = m_client->blockChain().details(); auto diff = BlockInfo(m_client->blockChain().block()).difficulty; - ui->blockCount->setText(QString("#%1 @%3 T%2 N%4 D%5").arg(d.number).arg(toLog2(d.totalDifficulty)).arg(toLog2(diff)).arg(eth::c_protocolVersion).arg(eth::c_databaseVersion)); + ui->blockCount->setText(QString("%6 #%1 @%3 T%2 N%4 D%5").arg(d.number).arg(toLog2(d.totalDifficulty)).arg(toLog2(diff)).arg(eth::c_protocolVersion).arg(eth::c_databaseVersion).arg(m_privateChain.size() ? "[" + m_privateChain + "] " : "testnet")); } static bool blockMatch(string const& _f, eth::BlockDetails const& _b, h256 _h, BlockChain const& _bc) @@ -1475,7 +1494,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(), ui->forceAddress->text().toStdString(), ui->upnp->isChecked()); + m_client->startNetwork(ui->port->value(), string(), 0, NodeMode::Full, ui->idealPeers->value(), ui->forceAddress->text().toStdString(), ui->upnp->isChecked(), m_privateChain.size() ? sha3(m_privateChain.toStdString()) : 0); if (m_peers.size() && ui->usePast->isChecked()) m_client->restorePeers(bytesConstRef((byte*)m_peers.data(), m_peers.size())); } diff --git a/alethzero/MainWin.h b/alethzero/MainWin.h index f54a64053..8272b8506 100644 --- a/alethzero/MainWin.h +++ b/alethzero/MainWin.h @@ -138,6 +138,7 @@ private slots: void on_debugDumpState_triggered(int _add = 1); void on_debugDumpStatePre_triggered(); void on_refresh_triggered(); + void on_usePrivate_triggered(); signals: void poll(); @@ -206,6 +207,7 @@ private: QByteArray m_peers; QStringList m_servers; QList m_myKeys; + QString m_privateChain = 0; bool m_keysChanged = false; eth::bytes m_data; eth::Address m_nameReg; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 673973fbd..86c26d1db 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -197,7 +197,7 @@ void Client::noteChanged(h256Set const& _filters) } } -void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHost, unsigned short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp) +void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHost, unsigned short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp, u256 _networkId) { static const char* c_threadName = "net"; @@ -220,7 +220,7 @@ void Client::startNetwork(unsigned short _listenPort, std::string const& _seedHo try { - m_net.reset(new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode, _publicIP, _upnp)); + m_net.reset(new PeerServer(m_clientVersion, m_bc, _networkId, _listenPort, _mode, _publicIP, _upnp)); } catch (std::exception const&) { diff --git a/libethereum/Client.h b/libethereum/Client.h index f20b3538f..d765494eb 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -263,7 +263,7 @@ public: void setIdealPeerCount(size_t _n) const; /// Start the network subsystem. - void startNetwork(unsigned short _listenPort = 30303, std::string const& _remoteHost = std::string(), unsigned short _remotePort = 30303, NodeMode _mode = NodeMode::Full, unsigned _peers = 5, std::string const& _publicIP = std::string(), bool _upnp = true); + void startNetwork(unsigned short _listenPort = 30303, std::string const& _remoteHost = std::string(), unsigned short _remotePort = 30303, NodeMode _mode = NodeMode::Full, unsigned _peers = 5, std::string const& _publicIP = std::string(), bool _upnp = true, u256 _networkId = 0); /// Connect to a particular peer. void connect(std::string const& _seedHost, unsigned short _port = 30303); /// Stop the network subsystem. diff --git a/libethereum/PeerServer.cpp b/libethereum/PeerServer.cpp index ccc044f93..a4330497d 100644 --- a/libethereum/PeerServer.cpp +++ b/libethereum/PeerServer.cpp @@ -55,7 +55,7 @@ static const set c_rejectAddresses = { {bi::address_v6::from_string("::")} }; -PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, unsigned short _port, NodeMode _m, string const& _publicAddress, bool _upnp): +PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, unsigned short _port, NodeMode _m, string const& _publicAddress, bool _upnp): m_clientVersion(_clientVersion), m_mode(_m), m_listenPort(_port), @@ -71,7 +71,7 @@ PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, clog(NetNote) << "Id:" << toHex(m_key.address().ref().cropped(0, 4)) << "Mode: " << (_m == NodeMode::PeerServer ? "PeerServer" : "Full"); } -PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, NodeMode _m, string const& _publicAddress, bool _upnp): +PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, NodeMode _m, string const& _publicAddress, bool _upnp): m_clientVersion(_clientVersion), m_mode(_m), m_listenPort(0), @@ -90,7 +90,7 @@ PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, clog(NetNote) << "Id:" << toHex(m_key.address().ref().cropped(0, 4)) << "Mode: " << (m_mode == NodeMode::PeerServer ? "PeerServer" : "Full"); } -PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, NodeMode _m): +PeerServer::PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, NodeMode _m): m_clientVersion(_clientVersion), m_mode(_m), m_listenPort(0), diff --git a/libethereum/PeerServer.h b/libethereum/PeerServer.h index 8439cce79..d3c2da65c 100644 --- a/libethereum/PeerServer.h +++ b/libethereum/PeerServer.h @@ -50,11 +50,11 @@ class PeerServer public: /// Start server, listening for connections on the given port. - PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, unsigned short _port, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string(), bool _upnp = true); + PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, unsigned short _port, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string(), bool _upnp = true); /// Start server, listening for connections on a system-assigned port. - PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string(), bool _upnp = true); + PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string(), bool _upnp = true); /// Start server, but don't listen. - PeerServer(std::string const& _clientVersion, BlockChain const& _ch, unsigned int _networkId, NodeMode _m = NodeMode::Full); + PeerServer(std::string const& _clientVersion, BlockChain const& _ch, u256 _networkId, NodeMode _m = NodeMode::Full); /// Will block on network process events. ~PeerServer(); @@ -63,7 +63,7 @@ public: void disconnectPeers(); static unsigned protocolVersion(); - unsigned networkId() { return m_networkId; } + u256 networkId() { return m_networkId; } /// Connect to a peer explicitly. void connect(std::string const& _addr, unsigned short _port = 30303) noexcept; @@ -137,7 +137,7 @@ private: bi::tcp::endpoint m_public; KeyPair m_key; - unsigned m_networkId; + u256 m_networkId; mutable std::mutex x_peers; std::map> m_peers; diff --git a/libethereum/PeerSession.cpp b/libethereum/PeerSession.cpp index 281e834f0..3a61430ed 100644 --- a/libethereum/PeerSession.cpp +++ b/libethereum/PeerSession.cpp @@ -35,7 +35,7 @@ static const eth::uint c_maxHashes = 4096; ///< Maximum number of hashes GetCha 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): +PeerSession::PeerSession(PeerServer* _s, bi::tcp::socket _socket, u256 _rNId, bi::address _peerAddress, unsigned short _peerPort): m_server(_s), m_socket(std::move(_socket)), m_reqNetworkId(_rNId), @@ -78,7 +78,7 @@ bool PeerSession::interpret(RLP const& _r) case HelloPacket: { m_protocolVersion = _r[1].toInt(); - m_networkId = _r[2].toInt(); + m_networkId = _r[2].toInt(); auto clientVersion = _r[3].toString(); m_caps = _r[4].toInt(); m_listenPort = _r[5].toInt(); diff --git a/libethereum/PeerSession.h b/libethereum/PeerSession.h index 6c7e56d7c..562d27e50 100644 --- a/libethereum/PeerSession.h +++ b/libethereum/PeerSession.h @@ -38,7 +38,7 @@ class PeerSession: public std::enable_shared_from_this friend class PeerServer; public: - PeerSession(PeerServer* _server, bi::tcp::socket _socket, uint _rNId, bi::address _peerAddress, unsigned short _peerPort = 0); + PeerSession(PeerServer* _server, bi::tcp::socket _socket, u256 _rNId, bi::address _peerAddress, unsigned short _peerPort = 0); ~PeerSession(); void start(); @@ -79,8 +79,8 @@ private: bytes m_incoming; uint m_protocolVersion; - uint m_networkId; - uint m_reqNetworkId; + u256 m_networkId; + u256 m_reqNetworkId; unsigned short m_listenPort; ///< Port that the remote client is listening on for connections. Useful for giving to peers. uint m_caps;