Merge pull request #3982

08b85a8 cryptonote_config: add get_config to refactor x = testnet ? config::testnet::X : stagenet ? config::stagenet::X : config::X (stoffu)
0cf80ba net_node: resolve host for node addresses given via command line flags (stoffu)
This commit is contained in:
luigi1111 2018-06-27 15:38:23 -05:00
commit 3ad4ecd4ff
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
6 changed files with 113 additions and 57 deletions

View File

@ -162,10 +162,7 @@ namespace cryptonote {
, account_public_address const & adr , account_public_address const & adr
) )
{ {
uint64_t address_prefix = nettype == TESTNET ? uint64_t address_prefix = subaddress ? get_config(nettype).CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : get_config(nettype).CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX;
(subaddress ? config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) : nettype == STAGENET ?
(subaddress ? config::stagenet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::stagenet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) :
(subaddress ? config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX);
return tools::base58::encode_addr(address_prefix, t_serializable_object_to_blob(adr)); return tools::base58::encode_addr(address_prefix, t_serializable_object_to_blob(adr));
} }
@ -176,7 +173,7 @@ namespace cryptonote {
, crypto::hash8 const & payment_id , crypto::hash8 const & payment_id
) )
{ {
uint64_t integrated_address_prefix = nettype == TESTNET ? config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : nettype == STAGENET ? config::stagenet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX; uint64_t integrated_address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
integrated_address iadr = { integrated_address iadr = {
adr, payment_id adr, payment_id
@ -201,15 +198,9 @@ namespace cryptonote {
, std::string const & str , std::string const & str
) )
{ {
uint64_t address_prefix = nettype == TESTNET ? uint64_t address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX;
config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX : nettype == STAGENET ? uint64_t integrated_address_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
config::stagenet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; uint64_t subaddress_prefix = get_config(nettype).CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX;
uint64_t integrated_address_prefix = nettype == TESTNET ?
config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : nettype == STAGENET ?
config::stagenet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
uint64_t subaddress_prefix = nettype == TESTNET ?
config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : nettype == STAGENET ?
config::stagenet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX;
if (2 * sizeof(public_address_outer_blob) != str.size()) if (2 * sizeof(public_address_outer_blob) != str.size())
{ {

View File

@ -203,4 +203,60 @@ namespace cryptonote
FAKECHAIN, FAKECHAIN,
UNDEFINED = 255 UNDEFINED = 255
}; };
struct config_t
{
uint64_t const CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX;
uint64_t const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
uint64_t const CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX;
uint16_t const P2P_DEFAULT_PORT;
uint16_t const RPC_DEFAULT_PORT;
uint16_t const ZMQ_RPC_DEFAULT_PORT;
boost::uuids::uuid const NETWORK_ID;
std::string const GENESIS_TX;
uint32_t const GENESIS_NONCE;
};
inline const config_t& get_config(network_type nettype)
{
static const config_t mainnet = {
::config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX,
::config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX,
::config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX,
::config::P2P_DEFAULT_PORT,
::config::RPC_DEFAULT_PORT,
::config::ZMQ_RPC_DEFAULT_PORT,
::config::NETWORK_ID,
::config::GENESIS_TX,
::config::GENESIS_NONCE
};
static const config_t testnet = {
::config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX,
::config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX,
::config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX,
::config::testnet::P2P_DEFAULT_PORT,
::config::testnet::RPC_DEFAULT_PORT,
::config::testnet::ZMQ_RPC_DEFAULT_PORT,
::config::testnet::NETWORK_ID,
::config::testnet::GENESIS_TX,
::config::testnet::GENESIS_NONCE
};
static const config_t stagenet = {
::config::stagenet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX,
::config::stagenet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX,
::config::stagenet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX,
::config::stagenet::P2P_DEFAULT_PORT,
::config::stagenet::RPC_DEFAULT_PORT,
::config::stagenet::ZMQ_RPC_DEFAULT_PORT,
::config::stagenet::NETWORK_ID,
::config::stagenet::GENESIS_TX,
::config::stagenet::GENESIS_NONCE
};
switch (nettype)
{
case MAINNET: return mainnet;
case TESTNET: return testnet;
case STAGENET: return stagenet;
case FAKECHAIN: return mainnet;
default: throw std::runtime_error("Invalid network type");
}
};
} }

View File

@ -394,18 +394,7 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
MINFO("Blockchain not loaded, generating genesis block."); MINFO("Blockchain not loaded, generating genesis block.");
block bl = boost::value_initialized<block>(); block bl = boost::value_initialized<block>();
block_verification_context bvc = boost::value_initialized<block_verification_context>(); block_verification_context bvc = boost::value_initialized<block_verification_context>();
if (m_nettype == TESTNET) generate_genesis_block(bl, get_config(m_nettype).GENESIS_TX, get_config(m_nettype).GENESIS_NONCE);
{
generate_genesis_block(bl, config::testnet::GENESIS_TX, config::testnet::GENESIS_NONCE);
}
else if (m_nettype == STAGENET)
{
generate_genesis_block(bl, config::stagenet::GENESIS_TX, config::stagenet::GENESIS_NONCE);
}
else
{
generate_genesis_block(bl, config::GENESIS_TX, config::GENESIS_NONCE);
}
add_new_block(bl, bvc); add_new_block(bl, bvc);
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "Failed to add genesis block to blockchain"); CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "Failed to add genesis block to blockchain");
} }

View File

@ -62,6 +62,7 @@
namespace nodetool namespace nodetool
{ {
inline bool append_net_address(std::vector<epee::net_utils::network_address> & seed_nodes, std::string const & addr, uint16_t default_port);
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
template<class t_payload_net_handler> template<class t_payload_net_handler>
void node_server<t_payload_net_handler>::init_options(boost::program_options::options_description& desc) void node_server<t_payload_net_handler>::init_options(boost::program_options::options_description& desc)
@ -273,10 +274,22 @@ namespace nodetool
{ {
nodetool::peerlist_entry pe = AUTO_VAL_INIT(pe); nodetool::peerlist_entry pe = AUTO_VAL_INIT(pe);
pe.id = crypto::rand<uint64_t>(); pe.id = crypto::rand<uint64_t>();
const uint16_t default_port = testnet ? ::config::testnet::P2P_DEFAULT_PORT : stagenet ? ::config::stagenet::P2P_DEFAULT_PORT : ::config::P2P_DEFAULT_PORT; const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT;
bool r = parse_peer_from_string(pe.adr, pr_str, default_port); bool r = parse_peer_from_string(pe.adr, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse address from string: " << pr_str); if (r)
{
m_command_line_peers.push_back(pe); m_command_line_peers.push_back(pe);
continue;
}
std::vector<epee::net_utils::network_address> resolved_addrs;
r = append_net_address(resolved_addrs, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse or resolve address from string: " << pr_str);
for (const epee::net_utils::network_address& addr : resolved_addrs)
{
pe.id = crypto::rand<uint64_t>();
pe.adr = addr;
m_command_line_peers.push_back(pe);
}
} }
} }
@ -327,24 +340,31 @@ namespace nodetool
return true; return true;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
inline void append_net_address( inline bool append_net_address(
std::vector<epee::net_utils::network_address> & seed_nodes std::vector<epee::net_utils::network_address> & seed_nodes
, std::string const & addr , std::string const & addr
, uint16_t default_port
) )
{ {
using namespace boost::asio; using namespace boost::asio;
std::string host = addr;
std::string port = std::to_string(default_port);
size_t pos = addr.find_last_of(':'); size_t pos = addr.find_last_of(':');
CHECK_AND_ASSERT_MES_NO_RET(std::string::npos != pos && addr.length() - 1 != pos && 0 != pos, "Failed to parse seed address from string: '" << addr << '\''); if (std::string::npos != pos)
std::string host = addr.substr(0, pos); {
std::string port = addr.substr(pos + 1); CHECK_AND_ASSERT_MES(addr.length() - 1 != pos && 0 != pos, false, "Failed to parse seed address from string: '" << addr << '\'');
host = addr.substr(0, pos);
port = addr.substr(pos + 1);
}
MINFO("Resolving node address: host=" << host << ", port=" << port);
io_service io_srv; io_service io_srv;
ip::tcp::resolver resolver(io_srv); ip::tcp::resolver resolver(io_srv);
ip::tcp::resolver::query query(host, port, boost::asio::ip::tcp::resolver::query::canonical_name); ip::tcp::resolver::query query(host, port, boost::asio::ip::tcp::resolver::query::canonical_name);
boost::system::error_code ec; boost::system::error_code ec;
ip::tcp::resolver::iterator i = resolver.resolve(query, ec); ip::tcp::resolver::iterator i = resolver.resolve(query, ec);
CHECK_AND_ASSERT_MES_NO_RET(!ec, "Failed to resolve host name '" << host << "': " << ec.message() << ':' << ec.value()); CHECK_AND_ASSERT_MES(!ec, false, "Failed to resolve host name '" << host << "': " << ec.message() << ':' << ec.value());
ip::tcp::resolver::iterator iend; ip::tcp::resolver::iterator iend;
for (; i != iend; ++i) for (; i != iend; ++i)
@ -354,14 +374,14 @@ namespace nodetool
{ {
epee::net_utils::network_address na{epee::net_utils::ipv4_network_address{boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()), endpoint.port()}}; epee::net_utils::network_address na{epee::net_utils::ipv4_network_address{boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()), endpoint.port()}};
seed_nodes.push_back(na); seed_nodes.push_back(na);
MINFO("Added seed node: " << na.str()); MINFO("Added node: " << na.str());
} }
else else
{ {
MWARNING("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec)); MWARNING("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec));
throw std::runtime_error("IPv6 unsupported");
} }
} }
return true;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
@ -484,7 +504,7 @@ namespace nodetool
if (result.size()) if (result.size())
{ {
for (const auto& addr_string : result) for (const auto& addr_string : result)
full_addrs.insert(addr_string + ":" + std::to_string(m_nettype == cryptonote::TESTNET ? ::config::testnet::P2P_DEFAULT_PORT : m_nettype == cryptonote::STAGENET ? ::config::stagenet::P2P_DEFAULT_PORT : ::config::P2P_DEFAULT_PORT)); full_addrs.insert(addr_string + ":" + std::to_string(cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT));
} }
++i; ++i;
} }
@ -507,7 +527,7 @@ namespace nodetool
for (const auto& full_addr : full_addrs) for (const auto& full_addr : full_addrs)
{ {
MDEBUG("Seed node: " << full_addr); MDEBUG("Seed node: " << full_addr);
append_net_address(m_seed_nodes, full_addr); append_net_address(m_seed_nodes, full_addr, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
} }
MDEBUG("Number of seed nodes: " << m_seed_nodes.size()); MDEBUG("Number of seed nodes: " << m_seed_nodes.size());
@ -1152,7 +1172,7 @@ namespace nodetool
for (const auto &peer: get_seed_nodes(m_nettype)) for (const auto &peer: get_seed_nodes(m_nettype))
{ {
MDEBUG("Fallback seed node: " << peer); MDEBUG("Fallback seed node: " << peer);
append_net_address(m_seed_nodes, peer); append_net_address(m_seed_nodes, peer, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
} }
m_fallback_seed_nodes_added = true; m_fallback_seed_nodes_added = true;
if (current_index == m_seed_nodes.size()) if (current_index == m_seed_nodes.size())
@ -1828,10 +1848,20 @@ namespace nodetool
for(const std::string& pr_str: perrs) for(const std::string& pr_str: perrs)
{ {
epee::net_utils::network_address na = AUTO_VAL_INIT(na); epee::net_utils::network_address na = AUTO_VAL_INIT(na);
const uint16_t default_port = m_nettype == cryptonote::TESTNET ? ::config::testnet::P2P_DEFAULT_PORT : m_nettype == cryptonote::STAGENET ? ::config::stagenet::P2P_DEFAULT_PORT : ::config::P2P_DEFAULT_PORT; const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT;
bool r = parse_peer_from_string(na, pr_str, default_port); bool r = parse_peer_from_string(na, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse address from string: " << pr_str); if (r)
{
container.push_back(na); container.push_back(na);
continue;
}
std::vector<epee::net_utils::network_address> resolved_addrs;
r = append_net_address(resolved_addrs, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse or resolve address from string: " << pr_str);
for (const epee::net_utils::network_address& addr : resolved_addrs)
{
container.push_back(addr);
}
} }
return true; return true;

View File

@ -3880,7 +3880,7 @@ bool simple_wallet::set_daemon(const std::vector<std::string>& args)
// If no port has been provided, use the default from config // If no port has been provided, use the default from config
if (!match[3].length()) if (!match[3].length())
{ {
int daemon_port = m_wallet->nettype() == cryptonote::TESTNET ? config::testnet::RPC_DEFAULT_PORT : m_wallet->nettype() == cryptonote::STAGENET ? config::stagenet::RPC_DEFAULT_PORT : config::RPC_DEFAULT_PORT; int daemon_port = get_config(m_wallet->nettype()).RPC_DEFAULT_PORT;
daemon_url = match[1] + match[2] + std::string(":") + std::to_string(daemon_port); daemon_url = match[1] + match[2] + std::string(":") + std::to_string(daemon_port);
} else { } else {
daemon_url = args[0]; daemon_url = args[0];

View File

@ -199,6 +199,7 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
{ {
const bool testnet = command_line::get_arg(vm, opts.testnet); const bool testnet = command_line::get_arg(vm, opts.testnet);
const bool stagenet = command_line::get_arg(vm, opts.stagenet); const bool stagenet = command_line::get_arg(vm, opts.stagenet);
const network_type nettype = testnet ? TESTNET : stagenet ? STAGENET : MAINNET;
const bool restricted = command_line::get_arg(vm, opts.restricted); const bool restricted = command_line::get_arg(vm, opts.restricted);
auto daemon_address = command_line::get_arg(vm, opts.daemon_address); auto daemon_address = command_line::get_arg(vm, opts.daemon_address);
@ -227,13 +228,13 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
if (!daemon_port) if (!daemon_port)
{ {
daemon_port = testnet ? config::testnet::RPC_DEFAULT_PORT : stagenet ? config::stagenet::RPC_DEFAULT_PORT : config::RPC_DEFAULT_PORT; daemon_port = get_config(nettype).RPC_DEFAULT_PORT;
} }
if (daemon_address.empty()) if (daemon_address.empty())
daemon_address = std::string("http://") + daemon_host + ":" + std::to_string(daemon_port); daemon_address = std::string("http://") + daemon_host + ":" + std::to_string(daemon_port);
std::unique_ptr<tools::wallet2> wallet(new tools::wallet2(testnet ? TESTNET : stagenet ? STAGENET : MAINNET, restricted)); std::unique_ptr<tools::wallet2> wallet(new tools::wallet2(nettype, restricted));
wallet->init(std::move(daemon_address), std::move(login)); wallet->init(std::move(daemon_address), std::move(login));
boost::filesystem::path ringdb_path = command_line::get_arg(vm, opts.shared_ringdb_dir); boost::filesystem::path ringdb_path = command_line::get_arg(vm, opts.shared_ringdb_dir);
wallet->set_ring_database(ringdb_path.string()); wallet->set_ring_database(ringdb_path.string());
@ -10832,17 +10833,6 @@ uint64_t wallet2::get_segregation_fork_height() const
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void wallet2::generate_genesis(cryptonote::block& b) const { void wallet2::generate_genesis(cryptonote::block& b) const {
if (m_nettype == TESTNET) cryptonote::generate_genesis_block(b, get_config(m_nettype).GENESIS_TX, get_config(m_nettype).GENESIS_NONCE);
{
cryptonote::generate_genesis_block(b, config::testnet::GENESIS_TX, config::testnet::GENESIS_NONCE);
}
else if (m_nettype == STAGENET)
{
cryptonote::generate_genesis_block(b, config::stagenet::GENESIS_TX, config::stagenet::GENESIS_NONCE);
}
else
{
cryptonote::generate_genesis_block(b, config::GENESIS_TX, config::GENESIS_NONCE);
}
} }
} }