diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 17846e3751..e096e51701 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -210,7 +210,13 @@ namespace steemit { namespace app { if( _app._read_only ) { - FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." ); + // If we are not connected, attempt to connect once and then fail + if( !_app._remote_net_api ) + { + _app.connect_to_write_node(); + FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." ); + } + (*_app._remote_net_api)->broadcast_transaction( trx ); } else @@ -225,7 +231,13 @@ namespace steemit { namespace app { { if( _app._read_only ) { - FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." ); + // If we are not connected, attempt to connect once and then fail + if( !_app._remote_net_api ) + { + _app.connect_to_write_node(); + FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." ); + } + return (*_app._remote_net_api)->broadcast_transaction_synchronous( trx ); } else @@ -242,7 +254,13 @@ namespace steemit { namespace app { { if( _app._read_only ) { - FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." ); + // If we are not connected, attempt to connect once and then fail + if( !_app._remote_net_api ) + { + _app.connect_to_write_node(); + FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." ); + } + (*_app._remote_net_api)->broadcast_block( b ); } else @@ -256,7 +274,13 @@ namespace steemit { namespace app { { if( _app._read_only ) { - FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." ); + // If we are not connected, attempt to connect once and then fail + if( !_app._remote_net_api ) + { + _app.connect_to_write_node(); + FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." ); + } + (*_app._remote_net_api)->broadcast_transaction_with_callback( cb, trx ); } else diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index b6a8869454..ab6b2b4c10 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -336,11 +336,7 @@ namespace detail { { try { - auto ws_ptr = _self->_client.connect( _options->at( "read-forward-rpc" ).as< string >() ); - auto apic = std::make_shared< fc::rpc::websocket_api_connection >( *ws_ptr ); - auto login = apic->get_remote_api< login_api >( 1 ); - FC_ASSERT( login->login( "", "" ) ); - _self->_remote_net_api = login->get_api_by_name( "network_broadcast_api" )->as< network_broadcast_api >(); + _self->_remote_endpoint = _options->at( "read-forward-rpc" ).as< string >(); } catch( fc::exception& e ) { @@ -1036,6 +1032,19 @@ void application::get_max_block_age( int32_t& result ) my->get_max_block_age( result ); } +void application::connect_to_write_node() +{ + if( _remote_endpoint ) + { + _remote_net_api.reset(); + auto ws_ptr = _client.connect( *_remote_endpoint ); + auto apic = std::make_shared< fc::rpc::websocket_api_connection >( *ws_ptr ); + auto login = apic->get_remote_api< login_api >( 1 ); + FC_ASSERT( login->login( "", "" ) ); + _remote_net_api = login->get_api_by_name( "network_broadcast_api" )->as< network_broadcast_api >(); + } +} + void application::shutdown_plugins() { for( auto& entry : my->_plugins_enabled ) diff --git a/libraries/app/include/steemit/app/application.hpp b/libraries/app/include/steemit/app/application.hpp index 2228fa6dd6..10c8b46084 100644 --- a/libraries/app/include/steemit/app/application.hpp +++ b/libraries/app/include/steemit/app/application.hpp @@ -121,7 +121,10 @@ namespace steemit { namespace app { void get_max_block_age( int32_t& result ); + void connect_to_write_node(); + bool _read_only = true; + fc::optional< string > _remote_endpoint; fc::optional< fc::api< network_broadcast_api > > _remote_net_api; fc::optional< fc::api< login_api > > _remote_login; fc::http::websocket_connection_ptr _ws_ptr;