Skip to content

Commit

Permalink
Merge pull request #2167 from bitshares/pr-844-rpc-logging
Browse files Browse the repository at this point in the history
Fix issue #844: Improve API logging
  • Loading branch information
abitmore authored May 7, 2020
2 parents c8ae77f + 3132de0 commit 26d0fc1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
16 changes: 14 additions & 2 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ void application_impl::reset_websocket_server()
if( !_options->count("rpc-endpoint") )
return;

_websocket_server = std::make_shared<fc::http::websocket_server>();
string proxy_forward_header;
if( _options->count("proxy-forwarded-for-header") )
proxy_forward_header = _options->at("proxy-forwarded-for-header").as<string>();

_websocket_server = std::make_shared<fc::http::websocket_server>( proxy_forward_header );
_websocket_server->on_connection( std::bind(&application_impl::new_connection, this, std::placeholders::_1) );

ilog("Configured websocket rpc to listen on ${ip}", ("ip",_options->at("rpc-endpoint").as<string>()));
Expand All @@ -210,8 +214,13 @@ void application_impl::reset_websocket_tls_server()
return;
}

string proxy_forward_header;
if( _options->count("proxy-forwarded-for-header") )
proxy_forward_header = _options->at("proxy-forwarded-for-header").as<string>();

string password = _options->count("server-pem-password") ? _options->at("server-pem-password").as<string>() : "";
_websocket_tls_server = std::make_shared<fc::http::websocket_tls_server>( _options->at("server-pem").as<string>(), password );
_websocket_tls_server = std::make_shared<fc::http::websocket_tls_server>(
_options->at("server-pem").as<string>(), password, proxy_forward_header );
_websocket_tls_server->on_connection( std::bind(&application_impl::new_connection, this, std::placeholders::_1) );

ilog("Configured websocket TLS rpc to listen on ${ip}", ("ip",_options->at("rpc-tls-endpoint").as<string>()));
Expand Down Expand Up @@ -974,6 +983,9 @@ void application::set_program_options(boost::program_options::options_descriptio
"Endpoint for TLS websocket RPC to listen on")
("server-pem,p", bpo::value<string>()->implicit_value("server.pem"), "The TLS certificate file for this server")
("server-pem-password,P", bpo::value<string>()->implicit_value(""), "Password for this certificate")
("proxy-forwarded-for-header", bpo::value<string>()->implicit_value("X-Forwarded-For-Client"),
"A HTTP header similar to X-Forwarded-For (XFF), used by the RPC server to extract clients' address info, "
"usually added by a trusted reverse proxy")
("genesis-json", bpo::value<boost::filesystem::path>(), "File to read Genesis State from")
("dbg-init-key", bpo::value<string>(), "Block signing key to use for init witnesses, overrides genesis file")
("api-access", bpo::value<boost::filesystem::path>(), "JSON file specifying API permissions")
Expand Down
6 changes: 3 additions & 3 deletions programs/build_helpers/run-node-test
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ NODE_PID=$!

echo "Waiting for cli_wallet start..." 1>&2 &
CLI_PID=$!
sleep 1
sleep 5
_START="`date +%s`"
while ! ps -p "$CLI_PID" >/dev/null && [ $((`date +%s` - _START)) -lt 120 ]; do
while ! ps -p "$CLI_PID" >/dev/null && [ $((`date +%s` - $_START)) -lt 120 ]; do
programs/cli_wallet/cli_wallet -sws://127.0.0.1:8090 -d -H127.0.0.1:8091 >cli.log 2>&1 &
CLI_PID=$!
sleep 2
sleep 10
done

if ! ps -p "$CLI_PID" >/dev/null; then
Expand Down
6 changes: 3 additions & 3 deletions programs/cli_wallet/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ int main( int argc, char** argv )
std::shared_ptr<fc::http::websocket_server> _websocket_server;
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 ){
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS);
wsc->register_api(wapi);
Expand All @@ -263,7 +263,7 @@ int main( int argc, char** argv )
std::shared_ptr<fc::http::websocket_tls_server> _websocket_tls_server;
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([&wapi]( const fc::http::websocket_connection_ptr& c ){
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(c, GRAPHENE_MAX_NESTED_OBJECTS);
wsc->register_api(wapi);
Expand All @@ -278,7 +278,7 @@ int main( int argc, char** argv )
std::shared_ptr<fc::http::websocket_server> _http_ws_server;
if( options.count("rpc-http-endpoint" ) )
{
_http_ws_server = std::make_shared<fc::http::websocket_server>();
_http_ws_server = std::make_shared<fc::http::websocket_server>("");
ilog( "Listening for incoming HTTP and WS RPC requests on ${p}",
("p", options.at("rpc-http-endpoint").as<string>()) );
_http_ws_server->on_connection([&wapi]( const fc::http::websocket_connection_ptr& c ){
Expand Down
24 changes: 20 additions & 4 deletions tests/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ BOOST_AUTO_TEST_CASE( two_node_network )
app1.initialize(app_dir.path(), cfg);
BOOST_TEST_MESSAGE( "Starting app1 and waiting 500 ms" );
app1.startup();
fc::wait_for( fc::seconds(10), [&app1] () {
#ifdef NDEBUG
#define LISTEN_WAIT_TIME (fc::milliseconds(10000))
#else
#define LISTEN_WAIT_TIME (fc::milliseconds(30000))
#endif
fc::wait_for( LISTEN_WAIT_TIME, [&app1] () {
const auto status = app1.p2p_node()->network_get_info();
return status["listening_on"].as<fc::ip::endpoint>( 5 ).port() == 3939;
});
Expand All @@ -268,7 +273,12 @@ BOOST_AUTO_TEST_CASE( two_node_network )
BOOST_TEST_MESSAGE( "Starting app2 and waiting for connection" );
app2.startup();

fc::wait_for( fc::seconds(10), [&app1] () { return app1.p2p_node()->get_connection_count() > 0; } );
#ifdef NDEBUG
#define CONNECT_WAIT_TIME (fc::milliseconds(10000))
#else
#define CONNECT_WAIT_TIME (fc::milliseconds(30000))
#endif
fc::wait_for( CONNECT_WAIT_TIME, [&app1] () { return app1.p2p_node()->get_connection_count() > 0; } );

BOOST_REQUIRE_EQUAL(app1.p2p_node()->get_connection_count(), 1u);
BOOST_CHECK_EQUAL(std::string(app1.p2p_node()->get_connected_peers().front().host.get_address()), "127.0.0.1");
Expand Down Expand Up @@ -316,7 +326,12 @@ BOOST_AUTO_TEST_CASE( two_node_network )
BOOST_TEST_MESSAGE( "Broadcasting tx" );
app1.p2p_node()->broadcast(graphene::net::trx_message(trx));

fc::usleep(fc::milliseconds(3000));
#ifdef NDEBUG
#define BROADCAST_WAIT_TIME (fc::milliseconds(5000))
#else
#define BROADCAST_WAIT_TIME (fc::milliseconds(15000))
#endif
fc::usleep( BROADCAST_WAIT_TIME );

BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
Expand All @@ -333,7 +348,8 @@ BOOST_AUTO_TEST_CASE( two_node_network )
BOOST_TEST_MESSAGE( "Broadcasting block" );
app2.p2p_node()->broadcast(graphene::net::block_message( block_1 ));

fc::usleep(fc::milliseconds(3000));
fc::usleep( BROADCAST_WAIT_TIME );

BOOST_TEST_MESSAGE( "Verifying nodes are still connected" );
BOOST_CHECK_EQUAL(app1.p2p_node()->get_connection_count(), 1u);
BOOST_CHECK_EQUAL(app1.chain_database()->head_block_num(), 1u);
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
#include <cstdlib>
#include <iostream>
#include <boost/test/included/unit_test.hpp>
#include <chrono>

extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP;

boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
std::srand(time(NULL));
std::cout << "Random number generator seeded to " << time(NULL) << std::endl;
const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::srand( seed );
std::cout << "Random number generator seeded to " << seed << std::endl;
const char* genesis_timestamp_str = getenv("GRAPHENE_TESTING_GENESIS_TIMESTAMP");
if( genesis_timestamp_str != nullptr )
{
Expand Down

0 comments on commit 26d0fc1

Please sign in to comment.