From eb05274e202781eddaa1c8c46a7f4b598fcc382a Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Tue, 3 Apr 2012 20:49:29 -0700 Subject: [PATCH] Add skeleton for unl_default. --- src/RPCServer.cpp | 70 +++++++++++++++++++++++++++++++++++++++++- src/RPCServer.h | 1 + src/UniqueNodeList.cpp | 7 +++++ src/UniqueNodeList.h | 1 + 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index 17ae7ba35e..661ca3f629 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -1,4 +1,5 @@ +#include #include #include @@ -11,6 +12,7 @@ #include "RPCServer.h" #include "RequestParser.h" #include "HttpReply.h" +#include "HttpsClient.h" #include "Application.h" #include "RPC.h" #include "Wallet.h" @@ -19,6 +21,12 @@ #include "NewcoinAddress.h" #include "AccountState.h" +#define VALIDATORS_FETCH_SECONDS 30 +#define VALIDATORS_FILE_NAME "validators.txt" +#define VALIDATORS_FILE_PATH "/" VALIDATORS_FILE_NAME +#define VALIDATORS_FILE_BYTES_MAX (50 << 10) +#define VALIDATORS_SITE "redstem.com" + /* Just read from wire until the entire request is in. */ @@ -523,8 +531,68 @@ Json::Value RPCServer::doUnlAdd(Json::Value& params) { else return "invalid params"; } +void RPCServer::validatorsResponse(const boost::system::error_code& err, std::string strResponse) +{ + std::cerr << "Fetch '" VALIDATORS_FILE_NAME "' complete." << std::endl; + + if(!err) + { + theApp->getUNL().nodeDefault(strResponse); + } + else + { + std::cerr << "Error: " << err.message() << std::endl; + } +} + Json::Value RPCServer::doUnlDefault(Json::Value& params) { - return "not implemented"; + // Populate the UNL from a validators.txt file. + if(!params.size() || (1==params.size() && !params[0u].compare("network"))) + { + bool bNetwork = 1 == params.size(); + std::string strValidators; + + if (!bNetwork) + { + std::ifstream ifsDefault(VALIDATORS_FILE_NAME, std::ios::in); + + if (!ifsDefault) + { + std::cerr << "Failed to read '" VALIDATORS_FILE_NAME "'." << std::endl; + + bNetwork = true; + } + else + { + strValidators.assign((std::istreambuf_iterator(ifsDefault)), + std::istreambuf_iterator()); + } + } + + if (bNetwork) + { + boost::shared_ptr client(new HttpsClient( + theApp->getIOService(), + VALIDATORS_SITE, + VALIDATORS_FILE_PATH, + 443, + VALIDATORS_FILE_BYTES_MAX + )); + + client->httpsGet( + boost::posix_time::seconds(VALIDATORS_FETCH_SECONDS), + boost::bind(&RPCServer::validatorsResponse, this, _1, _2)); + + return "fetching " VALIDATORS_FILE_NAME; + } + else + { + theApp->getUNL().nodeDefault(strValidators); + + return "processing " VALIDATORS_FILE_NAME; + } + } + else return "invalid params"; } // unl_delete diff --git a/src/RPCServer.h b/src/RPCServer.h index 21cf90df39..44f0ae8d5a 100644 --- a/src/RPCServer.h +++ b/src/RPCServer.h @@ -55,6 +55,7 @@ class RPCServer : public boost::enable_shared_from_this // Parses a string account name into a local or remote NewcoinAddress. NewcoinAddress parseAccount(const std::string& account); + void validatorsResponse(const boost::system::error_code& err, std::string strResponse); public: typedef boost::shared_ptr pointer; diff --git a/src/UniqueNodeList.cpp b/src/UniqueNodeList.cpp index 9031f59e35..7bfe85e172 100644 --- a/src/UniqueNodeList.cpp +++ b/src/UniqueNodeList.cpp @@ -194,4 +194,11 @@ Json::Value UniqueNodeList::getUnlJson() return ret; } + +void UniqueNodeList::nodeDefault(std::string strValidators) { + std::cerr << "Validators>" << std::endl; + std::cerr << strValidators; + std::cerr << "Validators<" << std::endl; + +} // vim:ts=4 diff --git a/src/UniqueNodeList.h b/src/UniqueNodeList.h index ecc641f4bb..2300781da3 100644 --- a/src/UniqueNodeList.h +++ b/src/UniqueNodeList.h @@ -39,6 +39,7 @@ public: void fetchNode(std::string strDomain); void removeNode(NewcoinAddress naHanko); void reset(); + void nodeDefault(std::string strValidators); // 2- we don't care, 1- we care and is valid, 2-invalid signature // int checkValid(newcoin::Validation& valid);