Code formatting

This commit is contained in:
serkixenos 2022-02-16 18:01:29 -04:00
parent 6e2fb6fac5
commit 5860002d0a
3 changed files with 104 additions and 126 deletions

View file

@ -3,3 +3,4 @@
find ./libraries/app -regex ".*[c|h]pp" | xargs clang-format -i find ./libraries/app -regex ".*[c|h]pp" | xargs clang-format -i
find ./libraries/chain/hardfork.d -regex ".*hf" | xargs clang-format -i find ./libraries/chain/hardfork.d -regex ".*hf" | xargs clang-format -i
find ./libraries/plugins/peerplays_sidechain -regex ".*[c|h]pp" | xargs clang-format -i find ./libraries/plugins/peerplays_sidechain -regex ".*[c|h]pp" | xargs clang-format -i
find ./programs/cli_wallet -regex ".*[c|h]pp" | xargs clang-format -i

View file

@ -874,8 +874,8 @@ std::vector<zmq::message_t> zmq_listener::receive_multipart() {
auto res = zmq::recv_multipart(socket, std::back_inserter(msgs)); auto res = zmq::recv_multipart(socket, std::back_inserter(msgs));
FC_ASSERT(res); FC_ASSERT(res);
if (3 != *res) { if (3 != *res) {
elog("zmq::recv_multipart returned: ${res}", ("res", *res)); elog("zmq::recv_multipart returned: ${res}", ("res", *res));
throw zmq::error_t(); throw zmq::error_t();
} }
return msgs; return msgs;
@ -897,7 +897,7 @@ void zmq_listener::handle_zmq() {
const auto block_hash = boost::algorithm::hex(std::string(static_cast<char *>(msg[1].data()), msg[1].size())); const auto block_hash = boost::algorithm::hex(std::string(static_cast<char *>(msg[1].data()), msg[1].size()));
event_received(block_hash); event_received(block_hash);
} catch (zmq::error_t &e) { } catch (zmq::error_t &e) {
elog("handle_zmq recv_multipart exception ${str}", ("str", e.what())); elog("handle_zmq recv_multipart exception ${str}", ("str", e.what()));
} }
} }
} }

View file

@ -27,8 +27,18 @@
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <boost/algorithm/string/replace.hpp>
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/version.hpp>
#include <fc/interprocess/signals.hpp>
#include <fc/io/json.hpp> #include <fc/io/json.hpp>
#include <fc/io/stdio.hpp> #include <fc/io/stdio.hpp>
#include <fc/log/console_appender.hpp>
#include <fc/log/file_appender.hpp>
#include <fc/log/logger.hpp>
#include <fc/log/logger_config.hpp>
#include <fc/network/http/server.hpp> #include <fc/network/http/server.hpp>
#include <fc/network/http/websocket.hpp> #include <fc/network/http/websocket.hpp>
#include <fc/rpc/cli.hpp> #include <fc/rpc/cli.hpp>
@ -40,26 +50,14 @@
#include <graphene/chain/config.hpp> #include <graphene/chain/config.hpp>
#include <graphene/chain/protocol/protocol.hpp> #include <graphene/chain/protocol/protocol.hpp>
#include <graphene/egenesis/egenesis.hpp> #include <graphene/egenesis/egenesis.hpp>
#include <graphene/utilities/git_revision.hpp>
#include <graphene/utilities/key_conversion.hpp> #include <graphene/utilities/key_conversion.hpp>
#include <graphene/wallet/wallet.hpp> #include <graphene/wallet/wallet.hpp>
#include <fc/interprocess/signals.hpp>
#include <boost/program_options.hpp>
#include <fc/log/console_appender.hpp>
#include <fc/log/file_appender.hpp>
#include <fc/log/logger.hpp>
#include <fc/log/logger_config.hpp>
#include <graphene/utilities/git_revision.hpp>
#include <boost/version.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/program_options/parsers.hpp>
#ifdef WIN32 #ifdef WIN32
# include <signal.h> #include <signal.h>
#else #else
# include <csignal> #include <csignal>
#endif #endif
using namespace graphene::app; using namespace graphene::app;
@ -69,24 +67,22 @@ using namespace graphene::wallet;
using namespace std; using namespace std;
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
int main( int argc, char** argv ) int main(int argc, char **argv) {
{
try { try {
boost::program_options::options_description opts; boost::program_options::options_description opts;
opts.add_options() opts.add_options()("help,h", "Print this help message and exit.");
("help,h", "Print this help message and exit.") opts.add_options()("version,v", "Display the version info and exit");
("version,v", "Display the version info and exit") opts.add_options()("server-rpc-endpoint,s", bpo::value<string>()->implicit_value("ws://127.0.0.1:8090"), "Server websocket RPC endpoint");
("server-rpc-endpoint,s", bpo::value<string>()->implicit_value("ws://127.0.0.1:8090"), "Server websocket RPC endpoint") opts.add_options()("server-rpc-user,u", bpo::value<string>(), "Server Username");
("server-rpc-user,u", bpo::value<string>(), "Server Username") opts.add_options()("server-rpc-password,p", bpo::value<string>(), "Server Password");
("server-rpc-password,p", bpo::value<string>(), "Server Password") opts.add_options()("rpc-endpoint,r", bpo::value<string>()->implicit_value("127.0.0.1:8091"), "Endpoint for wallet websocket RPC to listen on");
("rpc-endpoint,r", bpo::value<string>()->implicit_value("127.0.0.1:8091"), "Endpoint for wallet websocket RPC to listen on") opts.add_options()("rpc-tls-endpoint,t", bpo::value<string>()->implicit_value("127.0.0.1:8092"), "Endpoint for wallet websocket TLS RPC to listen on");
("rpc-tls-endpoint,t", bpo::value<string>()->implicit_value("127.0.0.1:8092"), "Endpoint for wallet websocket TLS RPC to listen on") opts.add_options()("rpc-tls-certificate,c", bpo::value<string>()->implicit_value("server.pem"), "PEM certificate for wallet websocket TLS RPC");
("rpc-tls-certificate,c", bpo::value<string>()->implicit_value("server.pem"), "PEM certificate for wallet websocket TLS RPC") opts.add_options()("rpc-http-endpoint,H", bpo::value<string>()->implicit_value("127.0.0.1:8093"), "Endpoint for wallet HTTP RPC to listen on");
("rpc-http-endpoint,H", bpo::value<string>()->implicit_value("127.0.0.1:8093"), "Endpoint for wallet HTTP RPC to listen on") opts.add_options()("daemon,d", "Run the wallet in daemon mode");
("daemon,d", "Run the wallet in daemon mode" ) opts.add_options()("wallet-file,w", bpo::value<string>()->implicit_value("wallet.json"), "wallet to load");
("wallet-file,w", bpo::value<string>()->implicit_value("wallet.json"), "wallet to load") opts.add_options()("chain-id", bpo::value<string>(), "chain ID to connect to");
("chain-id", bpo::value<string>(), "chain ID to connect to");
bpo::variables_map options; bpo::variables_map options;
@ -99,20 +95,18 @@ int main( int argc, char** argv )
} }
return 0; return 0;
} }
bpo::store( po, options ); bpo::store(po, options);
if( options.count("help") ) if (options.count("help")) {
{
std::cout << opts << "\n"; std::cout << opts << "\n";
return 0; return 0;
} }
if (options.count("version")) if (options.count("version")) {
{
std::string wallet_version(graphene::utilities::git_revision_description); std::string wallet_version(graphene::utilities::git_revision_description);
const size_t pos = wallet_version.find('/'); const size_t pos = wallet_version.find('/');
if( pos != std::string::npos && wallet_version.size() > pos ) if (pos != std::string::npos && wallet_version.size() > pos)
wallet_version = wallet_version.substr( pos + 1 ); wallet_version = wallet_version.substr(pos + 1);
std::cout << "Version: " << wallet_version << "\n"; std::cout << "Version: " << wallet_version << "\n";
std::cout << "Git Revision: " << graphene::utilities::git_revision_sha << "\n"; std::cout << "Git Revision: " << graphene::utilities::git_revision_sha << "\n";
std::cout << "Built: " << __DATE__ " at " __TIME__ << "\n"; std::cout << "Built: " << __DATE__ " at " __TIME__ << "\n";
@ -126,33 +120,33 @@ int main( int argc, char** argv )
fc::path log_dir = data_dir / "logs"; fc::path log_dir = data_dir / "logs";
fc::file_appender::config ac; fc::file_appender::config ac;
ac.filename = log_dir / "rpc" / "rpc.log"; ac.filename = log_dir / "rpc" / "rpc.log";
ac.flush = true; ac.flush = true;
ac.rotate = true; ac.rotate = true;
ac.rotation_interval = fc::hours( 1 ); ac.rotation_interval = fc::hours(1);
ac.rotation_limit = fc::days( 1 ); ac.rotation_limit = fc::days(1);
std::cout << "Logging RPC to file: " << (data_dir / ac.filename).preferred_string() << "\n"; std::cout << "Logging RPC to file: " << (data_dir / ac.filename).preferred_string() << "\n";
cfg.appenders.push_back(fc::appender_config( "default", "console", fc::variant(fc::console_appender::config(), 20))); cfg.appenders.push_back(fc::appender_config("default", "console", fc::variant(fc::console_appender::config(), 20)));
cfg.appenders.push_back(fc::appender_config( "rpc", "file", fc::variant(ac, 5))); cfg.appenders.push_back(fc::appender_config("rpc", "file", fc::variant(ac, 5)));
cfg.loggers = { fc::logger_config("default"), fc::logger_config( "rpc") }; cfg.loggers = {fc::logger_config("default"), fc::logger_config("rpc")};
cfg.loggers.front().level = fc::log_level::warn; cfg.loggers.front().level = fc::log_level::warn;
cfg.loggers.front().appenders = {"default"}; cfg.loggers.front().appenders = {"default"};
cfg.loggers.back().level = fc::log_level::info; cfg.loggers.back().level = fc::log_level::info;
cfg.loggers.back().appenders = {"rpc"}; cfg.loggers.back().appenders = {"rpc"};
fc::configure_logging( cfg ); fc::configure_logging(cfg);
fc::ecc::private_key committee_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key"))); fc::ecc::private_key committee_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")));
idump( (key_to_wif( committee_private_key ) ) ); idump((key_to_wif(committee_private_key)));
fc::ecc::private_key nathan_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan"))); fc::ecc::private_key nathan_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
public_key_type nathan_pub_key = nathan_private_key.get_public_key(); public_key_type nathan_pub_key = nathan_private_key.get_public_key();
idump( (nathan_pub_key) ); idump((nathan_pub_key));
idump( (key_to_wif( nathan_private_key ) ) ); idump((key_to_wif(nathan_private_key)));
// //
// TODO: We read wallet_data twice, once in main() to grab the // TODO: We read wallet_data twice, once in main() to grab the
@ -162,153 +156,136 @@ int main( int argc, char** argv )
// //
wallet_data wdata; wallet_data wdata;
fc::path wallet_file( options.count("wallet-file") ? options.at("wallet-file").as<string>() : "wallet.json"); fc::path wallet_file(options.count("wallet-file") ? options.at("wallet-file").as<string>() : "wallet.json");
if( fc::exists( wallet_file ) ) if (fc::exists(wallet_file)) {
{ wdata = fc::json::from_file(wallet_file).as<wallet_data>(GRAPHENE_MAX_NESTED_OBJECTS);
wdata = fc::json::from_file( wallet_file ).as<wallet_data>( GRAPHENE_MAX_NESTED_OBJECTS ); if (options.count("chain-id")) {
if( options.count("chain-id") )
{
// the --chain-id on the CLI must match the chain ID embedded in the wallet file // the --chain-id on the CLI must match the chain ID embedded in the wallet file
if( chain_id_type(options.at("chain-id").as<std::string>()) != wdata.chain_id ) if (chain_id_type(options.at("chain-id").as<std::string>()) != wdata.chain_id) {
{
std::cout << "Chain ID in wallet file does not match specified chain ID\n"; std::cout << "Chain ID in wallet file does not match specified chain ID\n";
return 1; return 1;
} }
} }
} } else {
else if (options.count("chain-id")) {
{
if( options.count("chain-id") )
{
wdata.chain_id = chain_id_type(options.at("chain-id").as<std::string>()); wdata.chain_id = chain_id_type(options.at("chain-id").as<std::string>());
std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from CLI)\n"; std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from CLI)\n";
} } else {
else
{
wdata.chain_id = graphene::egenesis::get_egenesis_chain_id(); wdata.chain_id = graphene::egenesis::get_egenesis_chain_id();
std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from egenesis)\n"; std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from egenesis)\n";
} }
} }
// but allow CLI to override // but allow CLI to override
if( options.count("server-rpc-endpoint") ) if (options.count("server-rpc-endpoint"))
wdata.ws_server = options.at("server-rpc-endpoint").as<std::string>(); wdata.ws_server = options.at("server-rpc-endpoint").as<std::string>();
if( options.count("server-rpc-user") ) if (options.count("server-rpc-user"))
wdata.ws_user = options.at("server-rpc-user").as<std::string>(); wdata.ws_user = options.at("server-rpc-user").as<std::string>();
if( options.count("server-rpc-password") ) if (options.count("server-rpc-password"))
wdata.ws_password = options.at("server-rpc-password").as<std::string>(); wdata.ws_password = options.at("server-rpc-password").as<std::string>();
fc::http::websocket_client client; fc::http::websocket_client client;
idump((wdata.ws_server)); idump((wdata.ws_server));
auto con = client.connect( wdata.ws_server ); auto con = client.connect(wdata.ws_server);
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(con, GRAPHENE_MAX_NESTED_OBJECTS); auto apic = std::make_shared<fc::rpc::websocket_api_connection>(con, GRAPHENE_MAX_NESTED_OBJECTS);
auto remote_api = apic->get_remote_api< login_api >(1); auto remote_api = apic->get_remote_api<login_api>(1);
edump((wdata.ws_user)(wdata.ws_password) ); edump((wdata.ws_user)(wdata.ws_password));
FC_ASSERT( remote_api->login( wdata.ws_user, wdata.ws_password ), "Failed to log in to API server" ); FC_ASSERT(remote_api->login(wdata.ws_user, wdata.ws_password), "Failed to log in to API server");
auto wapiptr = std::make_shared<wallet_api>( wdata, remote_api ); auto wapiptr = std::make_shared<wallet_api>(wdata, remote_api);
wapiptr->set_wallet_filename( wallet_file.generic_string() ); wapiptr->set_wallet_filename(wallet_file.generic_string());
wapiptr->load_wallet_file(); wapiptr->load_wallet_file();
fc::api<wallet_api> wapi(wapiptr); fc::api<wallet_api> wapi(wapiptr);
auto wallet_cli = std::make_shared<fc::rpc::cli>( GRAPHENE_MAX_NESTED_OBJECTS ); auto wallet_cli = std::make_shared<fc::rpc::cli>(GRAPHENE_MAX_NESTED_OBJECTS);
for( auto& name_formatter : wapiptr->get_result_formatters() ) for (auto &name_formatter : wapiptr->get_result_formatters())
wallet_cli->format_result( name_formatter.first, name_formatter.second ); wallet_cli->format_result(name_formatter.first, name_formatter.second);
boost::signals2::scoped_connection closed_connection(con->closed.connect([wallet_cli]{ boost::signals2::scoped_connection closed_connection(con->closed.connect([wallet_cli] {
cerr << "Server has disconnected us.\n"; cerr << "Server has disconnected us.\n";
wallet_cli->stop(); wallet_cli->stop();
})); }));
(void)(closed_connection); (void)(closed_connection);
if( wapiptr->is_new() ) if (wapiptr->is_new()) {
{
std::cout << "Please use the set_password method to initialize a new wallet before continuing\n"; std::cout << "Please use the set_password method to initialize a new wallet before continuing\n";
wallet_cli->set_prompt( "new >>> " ); wallet_cli->set_prompt("new >>> ");
} else } else
wallet_cli->set_prompt( "locked >>> " ); wallet_cli->set_prompt("locked >>> ");
boost::signals2::scoped_connection locked_connection(wapiptr->lock_changed.connect([&](bool locked) { boost::signals2::scoped_connection locked_connection(wapiptr->lock_changed.connect([&](bool locked) {
wallet_cli->set_prompt( locked ? "locked >>> " : "unlocked >>> " ); wallet_cli->set_prompt(locked ? "locked >>> " : "unlocked >>> ");
})); }));
std::shared_ptr<fc::http::websocket_server> _websocket_server; std::shared_ptr<fc::http::websocket_server> _websocket_server;
if( options.count("rpc-endpoint") ) if (options.count("rpc-endpoint")) {
{
_websocket_server = std::make_shared<fc::http::websocket_server>(); _websocket_server = std::make_shared<fc::http::websocket_server>();
_websocket_server->on_connection([&wapi]( const fc::http::websocket_connection_ptr& c ){ _websocket_server->on_connection([&wapi](const fc::http::websocket_connection_ptr &c) {
std::cout << "here... \n"; std::cout << "here... \n";
wlog("." ); wlog(".");
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS); auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS);
wsc->register_api(wapi); wsc->register_api(wapi);
c->set_session_data( wsc ); c->set_session_data(wsc);
}); });
ilog( "Listening for incoming RPC requests on ${p}", ("p", options.at("rpc-endpoint").as<string>() )); ilog("Listening for incoming RPC requests on ${p}", ("p", options.at("rpc-endpoint").as<string>()));
_websocket_server->listen( fc::ip::endpoint::from_string(options.at("rpc-endpoint").as<string>()) ); _websocket_server->listen(fc::ip::endpoint::from_string(options.at("rpc-endpoint").as<string>()));
_websocket_server->start_accept(); _websocket_server->start_accept();
} }
string cert_pem = "server.pem"; string cert_pem = "server.pem";
if( options.count( "rpc-tls-certificate" ) ) if (options.count("rpc-tls-certificate"))
cert_pem = options.at("rpc-tls-certificate").as<string>(); cert_pem = options.at("rpc-tls-certificate").as<string>();
std::shared_ptr<fc::http::websocket_tls_server> _websocket_tls_server; std::shared_ptr<fc::http::websocket_tls_server> _websocket_tls_server;
if( options.count("rpc-tls-endpoint") ) if (options.count("rpc-tls-endpoint")) {
{
_websocket_tls_server = std::make_shared<fc::http::websocket_tls_server>(cert_pem); _websocket_tls_server = std::make_shared<fc::http::websocket_tls_server>(cert_pem);
_websocket_tls_server->on_connection([&]( const fc::http::websocket_connection_ptr& c ){ _websocket_tls_server->on_connection([&](const fc::http::websocket_connection_ptr &c) {
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS); auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS);
wsc->register_api(wapi); wsc->register_api(wapi);
c->set_session_data( wsc ); c->set_session_data(wsc);
}); });
ilog( "Listening for incoming TLS RPC requests on ${p}", ("p", options.at("rpc-tls-endpoint").as<string>() )); ilog("Listening for incoming TLS RPC requests on ${p}", ("p", options.at("rpc-tls-endpoint").as<string>()));
_websocket_tls_server->listen( fc::ip::endpoint::from_string(options.at("rpc-tls-endpoint").as<string>()) ); _websocket_tls_server->listen(fc::ip::endpoint::from_string(options.at("rpc-tls-endpoint").as<string>()));
_websocket_tls_server->start_accept(); _websocket_tls_server->start_accept();
} }
auto _http_server = std::make_shared<fc::http::server>(); auto _http_server = std::make_shared<fc::http::server>();
if( options.count("rpc-http-endpoint" ) ) if (options.count("rpc-http-endpoint")) {
{ ilog("Listening for incoming HTTP RPC requests on ${p}", ("p", options.at("rpc-http-endpoint").as<string>()));
ilog( "Listening for incoming HTTP RPC requests on ${p}", ("p", options.at("rpc-http-endpoint").as<string>() ) ); _http_server->listen(fc::ip::endpoint::from_string(options.at("rpc-http-endpoint").as<string>()));
_http_server->listen( fc::ip::endpoint::from_string( options.at( "rpc-http-endpoint" ).as<string>() ) );
// //
// due to implementation, on_request() must come AFTER listen() // due to implementation, on_request() must come AFTER listen()
// //
_http_server->on_request( _http_server->on_request(
[&wapi]( const fc::http::request& req, const fc::http::server::response& resp ) [&wapi](const fc::http::request &req, const fc::http::server::response &resp) {
{ std::shared_ptr<fc::rpc::http_api_connection> conn =
std::shared_ptr< fc::rpc::http_api_connection > conn = std::make_shared<fc::rpc::http_api_connection>(GRAPHENE_MAX_NESTED_OBJECTS);
std::make_shared< fc::rpc::http_api_connection >( GRAPHENE_MAX_NESTED_OBJECTS ); conn->register_api(wapi);
conn->register_api( wapi ); conn->on_request(req, resp);
conn->on_request( req, resp ); });
} );
} }
if( !options.count( "daemon" ) ) if (!options.count("daemon")) {
{ wallet_cli->register_api(wapi);
wallet_cli->register_api( wapi );
wallet_cli->start(); wallet_cli->start();
wallet_cli->wait(); wallet_cli->wait();
} } else {
else fc::promise<int>::ptr exit_promise = new fc::promise<int>("UNIX Signal Handler");
{ fc::set_signal_handler([&exit_promise](int signal) {
fc::promise<int>::ptr exit_promise = new fc::promise<int>("UNIX Signal Handler"); exit_promise->set_value(signal);
fc::set_signal_handler([&exit_promise](int signal) { },
exit_promise->set_value(signal); SIGINT);
}, SIGINT);
ilog( "Entering Daemon Mode, ^C to exit" ); ilog("Entering Daemon Mode, ^C to exit");
exit_promise->wait(); exit_promise->wait();
} }
wapi->save_wallet_file(wallet_file.generic_string()); wapi->save_wallet_file(wallet_file.generic_string());
locked_connection.disconnect(); locked_connection.disconnect();
closed_connection.disconnect(); closed_connection.disconnect();
} } catch (const fc::exception &e) {
catch ( const fc::exception& e )
{
std::cout << e.to_detail_string() << "\n"; std::cout << e.to_detail_string() << "\n";
return -1; return -1;
} }