From 4a47722938d0016b7430bc1f7325ddd6612b17db Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Tue, 5 Mar 2024 17:52:41 -0300 Subject: [PATCH] Feat: Finished JRpc stuff. Need to check now finality dependency --- app/integration/ChainApiFactory.hpp | 43 +++++++++++++++ app/integration/StateApiFactory.hpp | 52 +++++++++++++++++++ app/integration/SystemApiFactory.hpp | 38 ++++++++++++++ src/api/service/chain/chain_api.hpp | 3 +- src/api/service/chain/impl/chain_api_impl.hpp | 5 ++ src/api/service/state/impl/state_api_impl.cpp | 14 ++--- src/api/service/state/impl/state_api_impl.hpp | 11 ++-- src/api/service/state/state_api.hpp | 3 +- .../service/system/impl/system_api_impl.hpp | 5 ++ src/api/service/system/system_api.hpp | 3 +- .../impl/validating_node_application.cpp | 10 +++- src/injector/application_injector.hpp | 8 +-- 12 files changed, 177 insertions(+), 18 deletions(-) create mode 100644 app/integration/ChainApiFactory.hpp create mode 100644 app/integration/StateApiFactory.hpp create mode 100644 app/integration/SystemApiFactory.hpp diff --git a/app/integration/ChainApiFactory.hpp b/app/integration/ChainApiFactory.hpp new file mode 100644 index 00000000..16c5559d --- /dev/null +++ b/app/integration/ChainApiFactory.hpp @@ -0,0 +1,43 @@ +/** + * @file ChainApiFactory.hpp + * @brief + * @date 2024-03-05 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _CHAIN_API_FACTORY_HPP_ +#define _CHAIN_API_FACTORY_HPP_ + +#include "api/service/chain/impl/chain_api_impl.hpp" +#include "blockchain/block_header_repository.hpp" +#include "blockchain/block_tree.hpp" + +class CComponentFactory; +namespace sgns +{ + class ChainApiFactory + { + public: + std::shared_ptr create() + { + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); + auto result = component_factory->GetComponent( "BlockHeaderRepository", boost::none ); + + if ( !result ) + { + throw std::runtime_error( "Initialize BlockHeaderRepository first" ); + } + auto block_header_repo = std::dynamic_pointer_cast( result.value() ); + + result = component_factory->GetComponent( "BlockTree", boost::none ); + if ( !result ) + { + throw std::runtime_error( "Initialize BlockTree first" ); + } + auto block_tree = std::dynamic_pointer_cast( result.value() ); + + return std::make_shared(block_header_repo, block_tree); + } + }; +} + +#endif \ No newline at end of file diff --git a/app/integration/StateApiFactory.hpp b/app/integration/StateApiFactory.hpp new file mode 100644 index 00000000..8bbb4112 --- /dev/null +++ b/app/integration/StateApiFactory.hpp @@ -0,0 +1,52 @@ +/** + * @file StateApiFactory.hpp + * @brief + * @date 2024-03-05 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _STATE_API_FACTORY_HPP_ +#define _STATE_API_FACTORY_HPP_ + +#include "api/service/state/impl/state_api_impl.hpp" +#include "blockchain/block_header_repository.hpp" +#include "storage/trie/trie_storage.hpp" +#include "blockchain/block_tree.hpp" + +class CComponentFactory; +namespace sgns +{ + class StateApiFactory + { + //TODO - Check removed runtime::Core from binaryen + public: + std::shared_ptr create() + { + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); + auto result = component_factory->GetComponent( "BlockHeaderRepository", boost::none ); + + if ( !result ) + { + throw std::runtime_error( "Initialize BlockHeaderRepository first" ); + } + auto block_header_repo = std::dynamic_pointer_cast( result.value() ); + + result = component_factory->GetComponent( "TrieStorage", boost::none ); + if ( !result ) + { + throw std::runtime_error( "Initialize TrieStorage first" ); + } + auto trie_storage = std::dynamic_pointer_cast( result.value() ); + + result = component_factory->GetComponent( "BlockTree", boost::none ); + if ( !result ) + { + throw std::runtime_error( "Initialize BlockTree first" ); + } + auto block_tree = std::dynamic_pointer_cast( result.value() ); + + return std::make_shared( block_header_repo, trie_storage, block_tree ); + } + }; +} + +#endif diff --git a/app/integration/SystemApiFactory.hpp b/app/integration/SystemApiFactory.hpp new file mode 100644 index 00000000..174ebdad --- /dev/null +++ b/app/integration/SystemApiFactory.hpp @@ -0,0 +1,38 @@ +/** + * @file SystemApiFactory.hpp + * @brief + * @date 2024-03-05 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _SYSTEM_API_FACTORY_HPP_ +#define _SYSTEM_API_FACTORY_HPP_ + +#include "api/service/system/impl/system_api_impl.hpp" +#include "blockchain/block_header_repository.hpp" +#include "storage/trie/trie_storage.hpp" +#include "blockchain/block_tree.hpp" + +class CComponentFactory; +namespace sgns +{ + class SystemApiFactory + { + //TODO - Check removed runtime::Core from binaryen + public: + std::shared_ptr create() + { + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); + auto result = component_factory->GetComponent( "ConfigurationStorage", boost::none ); + + if ( !result ) + { + throw std::runtime_error( "Initialize ConfigurationStorage first" ); + } + auto config_storage = std::dynamic_pointer_cast( result.value() ); + + return std::make_shared( config_storage ); + } + }; +} + +#endif \ No newline at end of file diff --git a/src/api/service/chain/chain_api.hpp b/src/api/service/chain/chain_api.hpp index 01af067d..7c8cee38 100644 --- a/src/api/service/chain/chain_api.hpp +++ b/src/api/service/chain/chain_api.hpp @@ -6,12 +6,13 @@ #include "base/buffer.hpp" #include "outcome/outcome.hpp" #include "primitives/common.hpp" +#include "integration/IComponent.hpp" namespace sgns::api { /** * @class ChainApi privides interface for blockchain api */ - class ChainApi { + class ChainApi : public IComponent { public: virtual ~ChainApi() = default; using BlockNumber = primitives::BlockNumber; diff --git a/src/api/service/chain/impl/chain_api_impl.hpp b/src/api/service/chain/impl/chain_api_impl.hpp index 8e8a99b3..4cb93447 100644 --- a/src/api/service/chain/impl/chain_api_impl.hpp +++ b/src/api/service/chain/impl/chain_api_impl.hpp @@ -27,6 +27,11 @@ namespace sgns::api { outcome::result> getBlockHash( gsl::span values) const override; + std::string GetName() override + { + return "ChainApiImpl"; + } + private: std::shared_ptr block_repo_; std::shared_ptr block_tree_; diff --git a/src/api/service/state/impl/state_api_impl.cpp b/src/api/service/state/impl/state_api_impl.cpp index cd21b61f..51d34dc9 100644 --- a/src/api/service/state/impl/state_api_impl.cpp +++ b/src/api/service/state/impl/state_api_impl.cpp @@ -8,16 +8,16 @@ namespace sgns::api { StateApiImpl::StateApiImpl( std::shared_ptr block_repo, std::shared_ptr trie_storage, - std::shared_ptr block_tree, - std::shared_ptr runtime_core) + std::shared_ptr block_tree/*, + std::shared_ptr runtime_core*/) : block_repo_{std::move(block_repo)}, storage_{std::move(trie_storage)}, - block_tree_{std::move(block_tree)}, - runtime_core_{std::move(runtime_core)} { + block_tree_{std::move(block_tree)}/*, + runtime_core_{std::move(runtime_core)}*/ { BOOST_ASSERT(nullptr != block_repo_); BOOST_ASSERT(nullptr != storage_); BOOST_ASSERT(nullptr != block_tree_); - BOOST_ASSERT(nullptr != runtime_core_); + //BOOST_ASSERT(nullptr != runtime_core_); } outcome::result StateApiImpl::getStorage( @@ -35,7 +35,9 @@ namespace sgns::api { outcome::result StateApiImpl::getRuntimeVersion( const boost::optional &at) const { - return runtime_core_->version(at); + //return runtime_core_->version(at); + + return primitives::Version{}; } void StateApiImpl::setApiService( diff --git a/src/api/service/state/impl/state_api_impl.hpp b/src/api/service/state/impl/state_api_impl.hpp index 4549fd44..cb0767ff 100644 --- a/src/api/service/state/impl/state_api_impl.hpp +++ b/src/api/service/state/impl/state_api_impl.hpp @@ -14,8 +14,8 @@ namespace sgns::api { public: StateApiImpl(std::shared_ptr block_repo, std::shared_ptr trie_storage, - std::shared_ptr block_tree, - std::shared_ptr runtime_core); + std::shared_ptr block_tree/*, + std::shared_ptr runtime_core*/); void setApiService( std::shared_ptr const &api_service) override; @@ -32,11 +32,16 @@ namespace sgns::api { outcome::result unsubscribeStorage( const std::vector &subscription_id) override; + std::string GetName() override + { + return "StateApiImpl"; + } + private: std::shared_ptr block_repo_; std::shared_ptr storage_; std::shared_ptr block_tree_; - std::shared_ptr runtime_core_; + //std::shared_ptr runtime_core_; std::weak_ptr api_service_; }; diff --git a/src/api/service/state/state_api.hpp b/src/api/service/state/state_api.hpp index e1ba948e..a6e31ae4 100644 --- a/src/api/service/state/state_api.hpp +++ b/src/api/service/state/state_api.hpp @@ -9,10 +9,11 @@ #include "outcome/outcome.hpp" #include "primitives/common.hpp" #include "primitives/version.hpp" +#include "integration/IComponent.hpp" namespace sgns::api { - class StateApi { + class StateApi : public IComponent { public: virtual ~StateApi() = default; diff --git a/src/api/service/system/impl/system_api_impl.hpp b/src/api/service/system/impl/system_api_impl.hpp index c4e156ca..f0621c5d 100644 --- a/src/api/service/system/impl/system_api_impl.hpp +++ b/src/api/service/system/impl/system_api_impl.hpp @@ -15,6 +15,11 @@ namespace sgns::api { std::shared_ptr getConfig() const override; + std::string GetName() override + { + return "SystemApiImpl"; + } + private: std::shared_ptr config_; }; diff --git a/src/api/service/system/system_api.hpp b/src/api/service/system/system_api.hpp index 65631f19..f98a1afa 100644 --- a/src/api/service/system/system_api.hpp +++ b/src/api/service/system/system_api.hpp @@ -2,11 +2,12 @@ #define SUPERGENIUS_API_SYSTEMAPI #include "application/configuration_storage.hpp" +#include "integration/IComponent.hpp" namespace sgns::api { /// Auxiliary class that providing access for some app's parts over RPC - class SystemApi { + class SystemApi : public IComponent { public: virtual ~SystemApi() = default; diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 4ec15e77..cfede50a 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -45,6 +45,9 @@ #include "integration/ListenerFactory.hpp" #include "integration/JRpcServerFactory.hpp" #include "integration/JRpcProcessorFactory.hpp" +#include "integration/ChainApiFactory.hpp" +#include "integration/StateApiFactory.hpp" +#include "integration/SystemApiFactory.hpp" #include "storage/trie/supergenius_trie/supergenius_trie_factory_impl.hpp" #include "storage/trie/serialization/supergenius_codec.hpp" @@ -110,7 +113,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( ProductionFactory::create( *io_context_ ), "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 ); @@ -124,12 +127,15 @@ namespace sgns::application 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_ws_endpoint() ), "Listener", + 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::ChainApiFactory{}.create(), "ChainApi", boost::none ); 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::SystemApiFactory{}.create(), "SystemApi", boost::none ); component_factory->Register( sgns::JRpcProcessorFactory{}.create("System"), "JRpcProcessor", boost::make_optional( std::string( "System" ) ) ); component_factory->Register( sgns::ApiServiceFactory{}.create(), "ApiService", boost::none ); diff --git a/src/injector/application_injector.hpp b/src/injector/application_injector.hpp index adb41882..9e6308b5 100644 --- a/src/injector/application_injector.hpp +++ b/src/injector/application_injector.hpp @@ -167,19 +167,19 @@ namespace sgns::injector { auto server = injector.template create>(); std::vector> processors{ injector - .template create>(), + .template create>(), injector.template create< std::shared_ptr>(), injector - .template create>(), + .template create>(), injector.template create< - std::shared_ptr>()}; + std::shared_ptr>()}; initialized = std::make_shared(std::move(app_state_manager), std::move(rpc_thread_pool), std::move(listeners), - std::move(server), + std::move(sevrver), processors, std::move(subscription_engine));