forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
160 lines (136 loc) · 5.22 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Aleth: Ethereum C++ client, tools and libraries.
// Copyright 2018-2019 Aleth Authors.
// Licensed under the GNU General Public License, Version 3.
#include <libdevcore/FileSystem.h>
#include <libdevcore/LoggingProgramOptions.h>
#include <libethcore/Common.h>
#include <libp2p/Common.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;
namespace bi = boost::asio::ip;
using namespace dev;
using namespace dev::p2p;
using namespace dev::eth;
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();
bool allowLocalDiscovery = false;
bool noBootstrap = false;
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)");
addNetworkingOption("allow-local-discovery", po::bool_switch(&allowLocalDiscovery),
"Include local addresses in the discovery process. Used for testing purposes.");
addNetworkingOption("no-bootstrap", po::bool_switch(&noBootstrap),
"Do not connect to the default Ethereum bootnode servers");
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 AlethErrors::ArgumentProcessingFailure;
}
if (vm.count("help"))
{
cout << "NAME:\n"
<< " " << c_programName << "\n"
<< "USAGE:\n"
<< " " << c_programName << " [options]\n\n";
cout << generalOptions << clientNetworking << loggingProgramOptions;
return AlethErrors::Success;
}
/// Networking params.
string listenIP;
unsigned short listenPort = c_defaultListenPort;
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);
netPrefs.allowLocalDiscovery = allowLocalDiscovery;
auto netData = contents(getDataDir() / fs::path(c_networkConfigFileName));
Host h(c_programName, netPrefs, &netData);
h.start();
if (!h.haveNetwork())
return AlethErrors::NetworkStartFailure;
cout << "Node ID: " << h.enode() << endl;
if (!noBootstrap)
{
for (auto const& bn : defaultBootNodes())
{
bi::tcp::endpoint ep = Network::resolveHost(bn.second);
h.addNode(
bn.first, NodeIPEndpoint{ep.address(), ep.port() /* udp */, ep.port() /* tcp */});
}
}
ExitHandler exitHandler;
signal(SIGTERM, &ExitHandler::exitHandler);
signal(SIGABRT, &ExitHandler::exitHandler);
signal(SIGINT, &ExitHandler::exitHandler);
while (!exitHandler.shouldExit())
this_thread::sleep_for(chrono::seconds(1));
h.stop();
netData = h.saveNetwork();
if (!netData.empty())
writeFile(getDataDir() / fs::path(c_networkConfigFileName), &netData);
return AlethErrors::Success;
}