Skip to content

Commit

Permalink
Fix: Instances of context and app_state_manager no longer singleton-l…
Browse files Browse the repository at this point in the history
…ike [ci-skip]
  • Loading branch information
henriqueaklein committed Mar 6, 2024
1 parent 061cc5b commit 5441835
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 37 deletions.
3 changes: 2 additions & 1 deletion app/integration/ApiServiceFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace sgns
{
throw std::runtime_error( "Initialize AppStateManager first" );
}
auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "RpcThreadPool", boost::none );
if ( !result )
Expand Down
3 changes: 2 additions & 1 deletion app/integration/AuthorityManagerFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace sgns
{
throw std::runtime_error( "Initialize AppStateManager first" );
}
auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "ProductionConfiguration", boost::none );
if ( !result )
Expand Down
3 changes: 2 additions & 1 deletion app/integration/AuthorityUpdateObserverFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class AuthorityUpdateObserverFactory
throw std::runtime_error( "Initialize AppStateManager first" );
}

auto app_state_manager = std::dynamic_pointer_cast<sgns::application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "ProductionConfiguration", boost::none );
if ( !result )
Expand Down
3 changes: 2 additions & 1 deletion app/integration/FinalityFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace sgns
{
throw std::runtime_error( "Initialize AppStateManager first" );
}
auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "Environment", boost::none );

Expand Down
8 changes: 6 additions & 2 deletions app/integration/ListenerFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "api/transport/impl/http/http_listener_impl.hpp"
#include "api/transport/impl/ws/ws_listener_impl.hpp"
#include "integration/RpcContextFactory.hpp"

class CComponentFactory;
namespace sgns
Expand All @@ -25,14 +26,17 @@ namespace sgns
{
throw std::runtime_error( "Initialize AppStateManager first" );
}
auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "RpcContext", boost::none );
/*result = component_factory->GetComponent( "RpcContext", boost::none );
if ( !result )
{
throw std::runtime_error( "Initialize RpcContext first" );
}
auto rpc_context = std::dynamic_pointer_cast<api::RpcContext>( result.value() );
*/
auto rpc_context = RpcContextFactory{}.create();

api::Listener::Configuration listener_config;
listener_config.endpoint = endpoint;
Expand Down
5 changes: 3 additions & 2 deletions app/integration/ProductionFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ProductionFactory
{
throw std::runtime_error( "Initialize AppStateManager first" );
}
auto app_st_manager = std::dynamic_pointer_cast<sgns::application::AppStateManager>( result.value() );
//auto app_state_manager = std::dynamic_pointer_cast<application::AppStateManager>( result.value() );
auto app_state_manager = AppStateManagerFactory::create();

result = component_factory->GetComponent( "ProductionLottery", boost::none );
if ( !result )
Expand Down Expand Up @@ -132,7 +133,7 @@ class ProductionFactory
auto auth_updt_observer = std::dynamic_pointer_cast<sgns::authority::AuthorityUpdateObserver>( result.value() );

return std::make_shared<sgns::verification::ProductionImpl>( //
app_st_manager, //
app_state_manager, //
prod_lottery, //
block_executor, //
trie_storage, //
Expand Down
5 changes: 4 additions & 1 deletion app/integration/RpcThreadPoolFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "api/transport/rpc_thread_pool.hpp"
#include "api/transport/rpc_io_context.hpp"
#include "integration/RpcContextFactory.hpp"

class CComponentFactory;
namespace sgns
Expand All @@ -20,12 +21,14 @@ namespace sgns
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );

auto result = component_factory->GetComponent("RpcContext", boost::none);
/*auto result = component_factory->GetComponent("RpcContext", boost::none);
if (!result)
{
throw std::runtime_error("Initialize RpcContext first");
}
auto rpc_context = std::dynamic_pointer_cast<api::RpcContext>(result.value());
*/
auto rpc_context = RpcContextFactory{}.create();

return std::make_shared<api::RpcThreadPool>(rpc_context, api::RpcThreadPool::Configuration{});
}
Expand Down
62 changes: 34 additions & 28 deletions src/application/impl/validating_node_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ namespace sgns::application
production_execution_strategy_ = boost::filesystem::exists( app_config->rocksdb_path() ) ? Production::ExecutionStrategy::SYNC_FIRST :
Production::ExecutionStrategy::GENESIS;

io_context_ = std::make_shared<boost::asio::io_context>();
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
component_factory->Register( sgns::RpcContextFactory{}.create(), "RpcContext", boost::none );

component_factory->Register( std::make_shared<sgns::application::AppStateManagerImpl>(), "AppStateManager", boost::none );
io_context_ = std::make_shared<boost::asio::io_context>();
component_factory->Register( AppStateManagerFactory::create(), "AppStateManager", boost::none );
component_factory->Register( ConfigurationStorageFactory::create( app_config->genesis_path() ), "ConfigurationStorage", boost::none );
component_factory->Register( KeyStorageFactory::create( app_config->keystore_path() ), "KeyStorage", boost::none );
component_factory->Register( SystemClockFactory::create(), "SystemClock", boost::none );
Expand All @@ -92,7 +93,7 @@ namespace sgns::application
component_factory->Register( TrieStorageBackendFactory::create(), "TrieStorageBackend", boost::none );
component_factory->Register( TrieSerializerFactory::create(), "TrieSerializer", boost::none );
component_factory->Register( TrieStorageFactory::create(), "TrieStorage", boost::none );
component_factory->Register( BlockStorageFactory::create(), "BlockStorage", boost::none );
component_factory->Register( sgns::BlockStorageFactory{}.create(), "BlockStorage", boost::none );
component_factory->Register( ExtrinsicGossiperFactory::create(), "ExtrinsicGossiper", boost::none );
component_factory->Register( ExtrinsicGossiperFactory::create(), "Gossiper", boost::none );
component_factory->Register( PoolModeratorFactory::create(), "PoolModerator", boost::none );
Expand All @@ -113,7 +114,7 @@ namespace sgns::application
component_factory->Register( ExtrinsicGossiperFactory::create(), "ProductionGossiper", boost::none );
component_factory->Register( sgns::SR25519KeypairFactory{}.create(), "SR25519Keypair", boost::none );
component_factory->Register( ProductionFactory::create( *io_context_ ), "Production", boost::none );
component_factory->Register( (component_factory->GetComponent( "Production", boost::none )).value(), "ProductionObserver", boost::none );
component_factory->Register( ( component_factory->GetComponent( "Production", boost::none ) ).value(), "ProductionObserver", boost::none );
component_factory->Register( sgns::EnvironmentFactory{}.create(), "Environment", boost::none );
component_factory->Register( sgns::ED25519ProviderFactory{}.create(), "ED25519Provider", boost::none );
component_factory->Register( sgns::ED25519KeyPairFactory{}.create(), "ED25519Keypair", boost::none );
Expand All @@ -123,20 +124,24 @@ namespace sgns::application
component_factory->Register( sgns::FinalityFactory{}.create( io_context_ ), "RoundObserver", boost::none );
component_factory->Register( sgns::SyncProtocolObserverFactory{}.create(), "SyncProtocolObserver", boost::none );
component_factory->Register( sgns::RouterFactory{}.create(), "Router", boost::none );
component_factory->Register( sgns::RpcContextFactory{}.create(), "RpcContext", boost::none );

component_factory->Register( sgns::RpcThreadPoolFactory{}.create(), "RpcThreadPool", boost::none );
component_factory->Register( sgns::ListenerFactory{}.create( "ws", app_config->rpc_ws_endpoint() ), "Listener",
boost::make_optional( std::string( "ws" ) ) );
component_factory->Register( sgns::ListenerFactory{}.create( "http", app_config->rpc_http_endpoint() ), "Listener",
boost::make_optional( std::string( "http" ) ) );
component_factory->Register( sgns::JRpcServerFactory{}.create(), "JRpcServer", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("Author"), "JRpcProcessor", boost::make_optional( std::string( "Author" ) ) );
component_factory->Register( sgns::JRpcProcessorFactory{}.create( "Author" ), "JRpcProcessor",
boost::make_optional( std::string( "Author" ) ) );
component_factory->Register( sgns::ChainApiFactory{}.create(), "ChainApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("Chain"), "JRpcProcessor", boost::make_optional( std::string( "Chain" ) ) );
component_factory->Register( sgns::JRpcProcessorFactory{}.create( "Chain" ), "JRpcProcessor",
boost::make_optional( std::string( "Chain" ) ) );
component_factory->Register( sgns::StateApiFactory{}.create(), "StateApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("State"), "JRpcProcessor", boost::make_optional( std::string( "State" ) ) );
component_factory->Register( sgns::JRpcProcessorFactory{}.create( "State" ), "JRpcProcessor",
boost::make_optional( std::string( "State" ) ) );
component_factory->Register( sgns::SystemApiFactory{}.create(), "SystemApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("System"), "JRpcProcessor", boost::make_optional( std::string( "System" ) ) );
component_factory->Register( sgns::JRpcProcessorFactory{}.create( "System" ), "JRpcProcessor",
boost::make_optional( std::string( "System" ) ) );
component_factory->Register( sgns::ApiServiceFactory{}.create(), "ApiService", boost::none );

auto result = component_factory->GetComponent( "AppStateManager", boost::none );
Expand Down Expand Up @@ -205,28 +210,29 @@ namespace sgns::application
[this]
{
// execute listeners

io_context_->post(
[this]
{
//di::bind<network::OwnPeerInfo>.to( [p2p_port{ app_config->p2p_port() }]( const auto &injector )
// { return get_peer_info( injector, p2p_port ); } ),
//auto p2p_injector = libp2p::injector::makeHostInjector<BOOST_DI_CFG>();
//auto &key_marshaller = p2p_injector.template create<libp2p::crypto::marshaller::KeyMarshaller &>();
//libp2p::peer::PeerId peer_id = libp2p::peer::PeerId::fromPublicKey( key_marshaller.marshal( public_key ).value() ).value();
//auto p2p_info = std::shared_ptr<network::OwnPeerInfo>();
//const network::OwnPeerInfo &current_peer_info();
//libp2p::Host &host = p2p_injector.template create<libp2p::Host &>();
//for ( const auto &ma : current_peer_info.addresses )
//{
// auto listen = host.listen( ma );
// if ( !listen )
// {
// logger_->error( "Cannot listen address {}. Error: {}", ma.getStringAddress(), listen.error().message() );
// std::exit( 1 );
// }
//}
//this->router_->init();
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
auto result = component_factory->GetComponent( "OwnPeerInfo", boost::none );
if ( !result )
{
throw std::runtime_error( "OwnPeerInfo not registered " );
}
auto current_peer_info = std::dynamic_pointer_cast<sgns::network::OwnPeerInfo>( result.value() );

auto p2p_injector = libp2p::injector::makeHostInjector<BOOST_DI_CFG>();
auto &host = p2p_injector.template create<libp2p::Host &>();
for ( const auto &ma : current_peer_info->addresses )
{
auto listen = host.listen( ma );
if ( !listen )
{
logger_->error( "Cannot listen address {}. Error: {}", ma.getStringAddress(), listen.error().message() );
std::exit( 1 );
}
}
this->router_->init();
} );
return true;
} );
Expand Down

0 comments on commit 5441835

Please sign in to comment.