Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5378 from ethereum/aleth-bootnode
Browse files Browse the repository at this point in the history
Create aleth-bootnode executable and remove "mode" argument from Aleth
  • Loading branch information
gumb0 authored Dec 7, 2018
2 parents cadadc5 + 9e162e9 commit 21c3492
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 151 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ if (TOOLS)
add_subdirectory(aleth-key)
add_subdirectory(aleth-vm)
add_subdirectory(rlp)
add_subdirectory(aleth-bootnode)
endif()

if (TESTS)
Expand Down
12 changes: 12 additions & 0 deletions aleth-bootnode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(
sources
main.cpp
)

add_executable(aleth-bootnode ${sources})
target_link_libraries(
aleth-bootnode
PRIVATE p2p devcore Boost::program_options
)

install(TARGETS aleth-bootnode DESTINATION bin)
150 changes: 150 additions & 0 deletions aleth-bootnode/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
This file is part of aleth.
aleth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
aleth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with aleth. If not, see <http://www.gnu.org/licenses/>.
*/

#include <libdevcore/FileSystem.h>
#include <libdevcore/LoggingProgramOptions.h>
#include <libp2p/Host.h>
#include <boost/program_options.hpp>
#include <boost/program_options/options_description.hpp>
#include <iostream>
#include <thread>

namespace po = boost::program_options;
namespace fs = boost::filesystem;

using namespace dev;
using namespace dev::p2p;
using namespace std;

namespace
{
string const c_programName = "aleth-bootnode";
string const c_networkConfigFileName = c_programName + "-network.rlp";
} // namespace

int main(int argc, char** argv)
{
setDefaultOrCLocale();

po::options_description generalOptions("GENERAL OPTIONS", c_lineWidth);
auto addGeneralOption = generalOptions.add_options();
addGeneralOption("help,h", "Show this help message and exit\n");

LoggingOptions loggingOptions;
po::options_description loggingProgramOptions(
createLoggingProgramOptions(c_lineWidth, loggingOptions));

po::options_description clientNetworking("NETWORKING", c_lineWidth);
auto addNetworkingOption = clientNetworking.add_options();
#if ETH_MINIUPNPC
addNetworkingOption(
"upnp", po::value<string>()->value_name("<on/off>"), "Use UPnP for NAT (default: on)");
#endif
addNetworkingOption("public-ip", po::value<string>()->value_name("<ip>"),
"Force advertised public IP to the given IP (default: auto)");
addNetworkingOption("listen-ip", po::value<string>()->value_name("<ip>(:<port>)"),
"Listen on the given IP for incoming connections (default: 0.0.0.0)");
addNetworkingOption("listen", po::value<unsigned short>()->value_name("<port>"),
"Listen on the given port for incoming connections (default: 30303)\n");

po::options_description allowedOptions("Allowed options");
allowedOptions.add(generalOptions).add(loggingProgramOptions).add(clientNetworking);

po::variables_map vm;
try
{
po::parsed_options parsed = po::parse_command_line(argc, argv, allowedOptions);
po::store(parsed, vm);
po::notify(vm);
}
catch (po::error const& e)
{
cout << e.what() << "\n";
return -1;
}

if (vm.count("help"))
{
cout << "NAME:\n"
<< " " << c_programName << "\n"
<< "USAGE:\n"
<< " " << c_programName << " [options]\n\n";
cout << generalOptions << clientNetworking << loggingProgramOptions;
return 0;
}

/// Networking params.
string listenIP;
unsigned short listenPort = c_defaultIPPort;
string publicIP;
bool upnp = true;

#if ETH_MINIUPNPC
if (vm.count("upnp"))
{
string m = vm["upnp"].as<string>();
if (isTrue(m))
upnp = true;
else if (isFalse(m))
upnp = false;
else
{
cerr << "Bad "
<< "--upnp"
<< " option: " << m << "\n";
return -1;
}
}
#endif

if (vm.count("public-ip"))
publicIP = vm["public-ip"].as<string>();
if (vm.count("listen-ip"))
listenIP = vm["listen-ip"].as<string>();
if (vm.count("listen"))
listenPort = vm["listen"].as<unsigned short>();

setupLogging(loggingOptions);
if (loggingOptions.verbosity > 0)
cout << EthGrayBold << c_programName << ", a C++ Ethereum bootnode implementation" EthReset
<< "\n";

auto netPrefs = publicIP.empty() ? NetworkConfig(listenIP, listenPort, upnp) :
NetworkConfig(publicIP, listenIP, listenPort, upnp);
auto netData = contents(getDataDir() / fs::path(c_networkConfigFileName));

Host h(c_programName, netPrefs, &netData);
h.start();

cout << "Node ID: " << h.enode() << endl;

ExitHandler exitHandler;
signal(SIGTERM, &ExitHandler::exitHandler);
signal(SIGABRT, &ExitHandler::exitHandler);
signal(SIGINT, &ExitHandler::exitHandler);

while (!exitHandler.shouldExit())
this_thread::sleep_for(chrono::milliseconds(1000));

h.stop();

netData = h.saveNetwork();
if (!netData.empty())
writeFile(getDataDir() / fs::path(c_networkConfigFileName), &netData);

return 0;
}
2 changes: 0 additions & 2 deletions aleth-vm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ namespace po = boost::program_options;

namespace
{
unsigned const c_lineWidth = 160;

int64_t maxBlockGasLimit()
{
static int64_t limit =
Expand Down
90 changes: 12 additions & 78 deletions aleth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file main.cpp
* @author Gav Wood <i@gavwood.com>
* @author Tasha Carl <tasha@carl.pro> - I here by place all my contributions in this file under MIT licence, as specified by http://opensource.org/licenses/MIT.
* @date 2014
* Ethereum client.
*/

#include <thread>
#include <fstream>
Expand Down Expand Up @@ -74,7 +67,6 @@ namespace
{

std::atomic<bool> g_silence = {false};
unsigned const c_lineWidth = 160;

void version()
{
Expand All @@ -85,28 +77,12 @@ void version()
cout << "Build: " << buildinfo->system_name << "/" << buildinfo->build_type << "\n";
}

bool isTrue(std::string const& _m)
{
return _m == "on" || _m == "yes" || _m == "true" || _m == "1";
}

bool isFalse(std::string const& _m)
{
return _m == "off" || _m == "no" || _m == "false" || _m == "0";
}

void importPresale(KeyManager& _km, string const& _file, function<string()> _pass)
{
KeyPair k = _km.presaleSecret(contentsString(_file), [&](bool){ return _pass(); });
_km.import(k.secret(), "Presale wallet" + _file + " (insecure)");
}

enum class NodeMode
{
PeerServer,
Full
};

enum class OperationMode
{
Node,
Expand Down Expand Up @@ -138,19 +114,6 @@ void stopSealingAfterXBlocks(eth::Client* _c, unsigned _start, unsigned& io_mini

this_thread::sleep_for(chrono::milliseconds(100));
}

class ExitHandler
{
public:
static void exitHandler(int) { s_shouldExit = true; }
bool shouldExit() const { return s_shouldExit; }

private:
static bool s_shouldExit;
};

bool ExitHandler::s_shouldExit = false;

}

int main(int argc, char** argv)
Expand All @@ -176,9 +139,6 @@ int main(int argc, char** argv)
string exportTo = "latest";
Format exportFormat = Format::Binary;

/// General params for Node operation
NodeMode nodeMode = NodeMode::Full;

bool ipc = true;

string jsonAdmin;
Expand Down Expand Up @@ -262,8 +222,6 @@ int main(int argc, char** argv)
addClientOption("test", "Testing mode; disable PoW and provide test rpc interface");
addClientOption("config", po::value<string>()->value_name("<file>"),
"Configure specialised blockchain using given JSON information\n");
addClientOption("mode,o", po::value<string>()->value_name("<full/peer>"),
"Start a full node or a peer node (default: full)\n");
addClientOption("ipc", "Enable IPC server (default: on)");
addClientOption("ipcpath", po::value<string>()->value_name("<path>"),
"Set .ipc socket path (default: data directory)");
Expand Down Expand Up @@ -475,19 +433,6 @@ int main(int argc, char** argv)
}
}

if (vm.count("mode"))
{
string m = vm["mode"].as<string>();
if (m == "full")
nodeMode = NodeMode::Full;
else if (m == "peer")
nodeMode = NodeMode::PeerServer;
else
{
cerr << "Unknown mode: " << m << "\n";
return -1;
}
}
if (vm.count("import-presale"))
presaleImports.push_back(vm["import-presale"].as<string>());
if (vm.count("admin"))
Expand Down Expand Up @@ -822,14 +767,12 @@ int main(int argc, char** argv)
netPrefs.pin = vm.count("pin") != 0;

auto nodesState = contents(getDataDir() / fs::path("network.rlp"));
auto caps = set<string>{"eth"};

if (testingMode)
chainParams.allowFutureBlocks = true;

dev::WebThreeDirect web3(WebThreeDirect::composeClientVersion("aleth"), db::databasePath(),
snapshotPath, chainParams, withExisting, nodeMode == NodeMode::Full ? caps : set<string>(),
netPrefs, &nodesState, testingMode);
snapshotPath, chainParams, withExisting, netPrefs, &nodesState, testingMode);

if (!extraData.empty())
web3.ethereum()->setExtraData(extraData);
Expand Down Expand Up @@ -989,15 +932,12 @@ int main(int argc, char** argv)
web3.setPeerStretch(peerStretch);
std::shared_ptr<eth::TrivialGasPricer> gasPricer =
make_shared<eth::TrivialGasPricer>(askPrice, bidPrice);
eth::Client* c = nodeMode == NodeMode::Full ? web3.ethereum() : nullptr;
if (c)
{
c->setGasPricer(gasPricer);
c->setSealer(miner.minerType());
c->setAuthor(author);
if (networkID != NoNetworkID)
c->setNetworkId(networkID);
}
Client& c = *(web3.ethereum());
c.setGasPricer(gasPricer);
c.setSealer(miner.minerType());
c.setAuthor(author);
if (networkID != NoNetworkID)
c.setNetworkId(networkID);

auto renderFullAddress = [&](Address const& _a) -> std::string
{
Expand Down Expand Up @@ -1100,18 +1040,12 @@ int main(int argc, char** argv)
signal(SIGTERM, &ExitHandler::exitHandler);
signal(SIGINT, &ExitHandler::exitHandler);

if (c)
{
unsigned n = c->blockChain().details().number;
if (mining)
c->startSealing();
unsigned n = c.blockChain().details().number;
if (mining)
c.startSealing();

while (!exitHandler.shouldExit())
stopSealingAfterXBlocks(c, n, mining);
}
else
while (!exitHandler.shouldExit())
this_thread::sleep_for(chrono::milliseconds(1000));
while (!exitHandler.shouldExit())
stopSealingAfterXBlocks(&c, n, mining);

if (jsonrpcIpcServer.get())
jsonrpcIpcServer->StopListening();
Expand Down
13 changes: 13 additions & 0 deletions libdevcore/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,17 @@ void setDefaultOrCLocale()
SetConsoleOutputCP(CP_UTF8);
#endif
}

bool ExitHandler::s_shouldExit = false;

bool isTrue(std::string const& _m)
{
return _m == "on" || _m == "yes" || _m == "true" || _m == "1";
}

bool isFalse(std::string const& _m)
{
return _m == "off" || _m == "no" || _m == "false" || _m == "0";
}

} // namespace dev
17 changes: 16 additions & 1 deletion libdevcore/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,19 @@ enum class WithExisting: int
int64_t utcTime();

void setDefaultOrCLocale();
}

static constexpr unsigned c_lineWidth = 160;

class ExitHandler
{
public:
static void exitHandler(int) { s_shouldExit = true; }
bool shouldExit() const { return s_shouldExit; }

private:
static bool s_shouldExit;
};

bool isTrue(std::string const& _m);
bool isFalse(std::string const& _m);
} // namespace dev
Loading

0 comments on commit 21c3492

Please sign in to comment.