From ec008fe76d025c151cd3f951b0c76f940f6e0b1e Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Mon, 18 Jun 2012 11:37:09 -0700 Subject: [PATCH] Improve configuration handling. - Make VALIDATORS_SITE configurable. - At bootstrap load validators.txt with out a unl_default entry. - Always merge in unl_default at start if available. --- newcoind.cfg | 6 ++++++ src/Config.cpp | 5 +++++ src/Config.h | 5 ++++- src/RPCServer.cpp | 2 +- src/UniqueNodeList.cpp | 46 +++++++++++++++++++++++++----------------- src/UniqueNodeList.h | 2 +- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/newcoind.cfg b/newcoind.cfg index 916275fee6..ea55a0a280 100644 --- a/newcoind.cfg +++ b/newcoind.cfg @@ -31,6 +31,12 @@ # Note: $XDG_CONFIG_HOME defaults to $HOME/.config # $XDG_DATA_HOME defaults to $HOME/.local/share # +# [validators_site]: +# Specifies where to find validators.txt for UNL boostrapping and RPC command unl_network. +# During alpha testing, this defaults to: redstem.com +# +# Example: newcoin.org +# # [unl_default]: # Specifies how to bootstrap the UNL list. The UNL list is based on a # validators.txt file and is maintained in the databases. When newcoind diff --git a/src/Config.cpp b/src/Config.cpp index 176bb2b522..6aaf809c6a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -28,6 +28,7 @@ #define SECTION_VALIDATION_SEED "validation_seed" #define SECTION_WEBSOCKET_IP "websocket_ip" #define SECTION_WEBSOCKET_PORT "websocket_port" +#define SECTION_VALIDATORS_SITE "validators_site" // Fees are in XNB. #define DEFAULT_FEE_ACCOUNT_CREATE 1000 @@ -143,6 +144,8 @@ void Config::setup(const std::string& strConf) ACCOUNT_PROBE_MAX = 10; + VALIDATORS_SITE = DEFAULT_VALIDATORS_SITE; + load(); } @@ -170,6 +173,8 @@ void Config::load() section secConfig = ParseSection(strConfigFile, true); std::string strTemp; + (void) sectionSingleB(secConfig, SECTION_VALIDATORS_SITE, VALIDATORS_SITE); + (void) sectionSingleB(secConfig, SECTION_PEER_IP, PEER_IP); if (sectionSingleB(secConfig, SECTION_PEER_PORT, strTemp)) diff --git a/src/Config.h b/src/Config.h index 10d1a1ce11..17f25dca21 100644 --- a/src/Config.h +++ b/src/Config.h @@ -9,7 +9,6 @@ #include #define SYSTEM_NAME "newcoin" -#define VALIDATORS_SITE "redstem.com" #define SYSTEM_CURRENCY_CODE "XNS" #define SYSTEM_CURRENCY_PRECISION 6 @@ -18,6 +17,7 @@ #define SYSTEM_CURRENCY_PARTS 1000000ull // 10^SYSTEM_CURRENCY_PRECISION #define SYSTEM_CURRENCY_START (SYSTEM_CURRENCY_GIFT*SYSTEM_CURRENCY_USERS*SYSTEM_CURRENCY_PARTS) +#define DEFAULT_VALIDATORS_SITE "redstem.com" #define VALIDATORS_FILE_NAME "validators.txt" const int SYSTEM_PEER_PORT = 6561; @@ -46,6 +46,9 @@ public: boost::filesystem::path DATA_DIR; boost::filesystem::path UNL_DEFAULT; + // Where to find validators.txt on the Internet. + std::string VALIDATORS_SITE; + // Network parameters int NETWORK_START_TIME; // The Unix time we start ledger 0 int TRANSACTION_FEE_BASE; diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index f663219ee0..0395d52a2a 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -1926,7 +1926,7 @@ Json::Value RPCServer::doUnlList(Json::Value& params) // Populate the UNL from a local validators.txt file. Json::Value RPCServer::doUnlLoad(Json::Value& params) { - if (!theApp->getUNL().nodeLoad()) + if (theConfig.UNL_DEFAULT.empty() || !theApp->getUNL().nodeLoad(theConfig.UNL_DEFAULT)) { return RPCError(rpcLOAD_FAILED); } diff --git a/src/UniqueNodeList.cpp b/src/UniqueNodeList.cpp index fbee423211..0e8fdc109e 100644 --- a/src/UniqueNodeList.cpp +++ b/src/UniqueNodeList.cpp @@ -1485,34 +1485,34 @@ Json::Value UniqueNodeList::getUnlJson() return ret; } -bool UniqueNodeList::nodeLoad() +bool UniqueNodeList::nodeLoad(boost::filesystem::path pConfig) { - if (theConfig.UNL_DEFAULT.empty()) + if (pConfig.empty()) { - std::cerr << "UNL_DEFAULT not specified." << std::endl; + std::cerr << VALIDATORS_FILE_NAME " path not specified." << std::endl; return false; } - if (!boost::filesystem::exists(theConfig.UNL_DEFAULT)) + if (!boost::filesystem::exists(pConfig)) { - std::cerr << str(boost::format("UNL_DEFAULT not found: %s") % theConfig.UNL_DEFAULT) << std::endl; + std::cerr << str(boost::format(VALIDATORS_FILE_NAME " not found: %s") % pConfig) << std::endl; return false; } - if (!boost::filesystem::is_regular_file(theConfig.UNL_DEFAULT)) + if (!boost::filesystem::is_regular_file(pConfig)) { - std::cerr << str(boost::format("UNL_DEFAULT not regular file: %s") % theConfig.UNL_DEFAULT) << std::endl; + std::cerr << str(boost::format(VALIDATORS_FILE_NAME " not regular file: %s") % pConfig) << std::endl; return false; } - std::ifstream ifsDefault(theConfig.UNL_DEFAULT.native().c_str(), std::ios::in); + std::ifstream ifsDefault(pConfig.native().c_str(), std::ios::in); if (!ifsDefault) { - std::cerr << str(boost::format("Failed to open: %s") % theConfig.UNL_DEFAULT) << std::endl; + std::cerr << str(boost::format(VALIDATORS_FILE_NAME " failed to open: %s") % pConfig) << std::endl; return false; } @@ -1524,14 +1524,14 @@ bool UniqueNodeList::nodeLoad() if (ifsDefault.bad()) { - std::cerr << str(boost::format("Failed to read: %s") % theConfig.UNL_DEFAULT) << std::endl; + std::cerr << str(boost::format("Failed to read: %s") % pConfig) << std::endl; return false; } - nodeDefault(strValidators, theConfig.UNL_DEFAULT.native()); + nodeDefault(strValidators, pConfig.native()); - std::cerr << str(boost::format("Processing: %s") % theConfig.UNL_DEFAULT) << std::endl; + std::cerr << str(boost::format("Processing: %s") % pConfig) << std::endl; return true; } @@ -1542,7 +1542,7 @@ void UniqueNodeList::validatorsResponse(const boost::system::error_code& err, st if (!err) { - nodeDefault(strResponse, VALIDATORS_SITE); + nodeDefault(strResponse, theConfig.VALIDATORS_SITE); } else { @@ -1554,7 +1554,7 @@ void UniqueNodeList::nodeNetwork() { HttpsClient::httpsGet( theApp->getIOService(), - VALIDATORS_SITE, + theConfig.VALIDATORS_SITE, 443, VALIDATORS_FILE_PATH, VALIDATORS_FILE_BYTES_MAX, @@ -1581,16 +1581,26 @@ void UniqueNodeList::nodeBootstrap() bool bLoaded = iDomains || iNodes; - if (!bLoaded && !theConfig.UNL_DEFAULT.empty()) + // Always merge in the file specified in the config. + if (!theConfig.UNL_DEFAULT.empty()) { - std::cerr << "Bootstrapping UNL: loading from file." << std::endl; + std::cerr << "Bootstrapping UNL: loading from unl_default." << std::endl; - bLoaded = nodeLoad(); + bLoaded = nodeLoad(theConfig.UNL_DEFAULT); + } + + // If never loaded anything try the current directory. + if (!bLoaded && theConfig.UNL_DEFAULT.empty()) + { + std::cerr << "Bootstrapping UNL: loading from '" VALIDATORS_FILE_NAME "'." << std::endl; + + bLoaded = nodeLoad(VALIDATORS_FILE_NAME); } if (!bLoaded) { - std::cerr << "Bootstrapping UNL: loading from " VALIDATORS_SITE "." << std::endl; + std::cerr << "Bootstrapping UNL: loading from " << theConfig.VALIDATORS_SITE << "." << std::endl; + nodeNetwork(); } } diff --git a/src/UniqueNodeList.h b/src/UniqueNodeList.h index 8ef9b88f0e..6b39f9fee5 100644 --- a/src/UniqueNodeList.h +++ b/src/UniqueNodeList.h @@ -151,7 +151,7 @@ public: bool nodeInUNL(const NewcoinAddress& naNodePublic); void nodeBootstrap(); - bool nodeLoad(); + bool nodeLoad(boost::filesystem::path pConfig); void nodeNetwork(); Json::Value getUnlJson();