From 8138f2f3f376e2b3100ee2e1d962d722af42a506 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Wed, 21 Feb 2024 19:03:31 -0300 Subject: [PATCH 01/14] Cleanup: Trying to remove injector --- app/CMakeLists.txt | 23 ++++ app/SuperGeniusDemoApp.cpp | 38 ++++++ app/SuperGeniusDemoApp.hpp | 29 ++++ app/runner.cpp | 26 ++++ build/CommonBuildParameters.cmake | 5 +- src/CMakeLists.txt | 2 +- src/application/CMakeLists.txt | 41 +++--- src/application/app_config.hpp | 71 +++++----- .../impl/validating_node_application.cpp | 126 ++++++++++-------- .../impl/validating_node_application.hpp | 65 ++++----- 10 files changed, 279 insertions(+), 147 deletions(-) create mode 100644 app/CMakeLists.txt create mode 100644 app/SuperGeniusDemoApp.cpp create mode 100644 app/SuperGeniusDemoApp.hpp create mode 100644 app/runner.cpp diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt new file mode 100644 index 00000000..2a94db39 --- /dev/null +++ b/app/CMakeLists.txt @@ -0,0 +1,23 @@ +add_executable (sgns_demo + runner.cpp + SuperGeniusDemoApp.cpp + ) + + target_link_libraries(sgns_demo + logger + Boost::system + Boost::thread + Boost::filesystem + ipfs_blockservice + #block_producing_node_application + validating_node_application + #app_config_impl + #protobuf::libprotobuf + #${USER_ENV_LIBRARY} # On windows platform , sr25519 library needs extra library userenv, so folllowing code is added + #${WIN_CRYPT_LIBRARY} + ) + if(FORCE_MULTILE) + set_target_properties(sgns_demo PROPERTIES LINK_FLAGS ${MULTIPLE_OPTION}) + endif() + + supergenius_install(sgns_demo) diff --git a/app/SuperGeniusDemoApp.cpp b/app/SuperGeniusDemoApp.cpp new file mode 100644 index 00000000..cfb06cb1 --- /dev/null +++ b/app/SuperGeniusDemoApp.cpp @@ -0,0 +1,38 @@ +/** + * @file SuperGeniusDemoApp.cpp + * @brief SuperGenius demo source file + * @date 2024-02-21 + * @author Henrique A. Klein (henryaklein@gmail.com) + */ +#include "SuperGeniusDemoApp.hpp" +#include "base/logger.hpp" +//#include "impl/app_config_factory.hpp" + +SuperGeniusDemoApp::SuperGeniusDemoApp() +{ + std::cout << "SuperGeniusDemoApp Constructed" << std::endl; +} +SuperGeniusDemoApp::~SuperGeniusDemoApp() +{ + std::cout << "SuperGeniusDemoApp Destructed" << std::endl; +} + +void SuperGeniusDemoApp::init( int argc, char **argv ) +{ + std::cout << "SuperGeniusDemoApp Init" << std::endl; + + auto logger = sgns::base::createLogger( "SuperGenius block node: " ); + //cfg = std::shared_ptr( std::move( AppConfigurationFactory::create( logger ) ) ); + cfg = std::shared_ptr( AppConfigurationFactory::create( logger ) ); + + cfg->initialize_from_args( sgns::application::AppConfiguration::LoadScheme::kValidating, argc, argv ); + + validation_node = std::make_shared( std::move( cfg ) ); +} +void SuperGeniusDemoApp::run( void ) +{ + validation_node->run(); +} +void SuperGeniusDemoApp::exit( void ) +{ +} \ No newline at end of file diff --git a/app/SuperGeniusDemoApp.hpp b/app/SuperGeniusDemoApp.hpp new file mode 100644 index 00000000..6541d536 --- /dev/null +++ b/app/SuperGeniusDemoApp.hpp @@ -0,0 +1,29 @@ +/** + * @file SuperGeniusDemoApp.hpp + * @brief SuperGenius App demo header file + * @date 2024-02-21 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _SUPERGENIUS_DEMO_APP_HPP_ +#define _SUPERGENIUS_DEMO_APP_HPP_ +#include + +#include "application/app_config.hpp" +#include "application/impl/validating_node_application.hpp" + +class SuperGeniusDemoApp +{ +public: + SuperGeniusDemoApp(); + ~SuperGeniusDemoApp(); + void init( int argc, char **argv ); + void run( void ); + void exit( void ); + +private: + std::shared_ptr cfg; + std::shared_ptr validation_node; +}; + +#endif \ No newline at end of file diff --git a/app/runner.cpp b/app/runner.cpp new file mode 100644 index 00000000..1c35fc79 --- /dev/null +++ b/app/runner.cpp @@ -0,0 +1,26 @@ +/** + * @file runner.cpp + * @brief + * @date 2024-02-21 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#include +#include "SuperGeniusDemoApp.hpp" + +SuperGeniusDemoApp app; +/** + * @brief Just the entry point to calling sgns app + * @param[in] argc + * @param[in] argv + * @return A @ref int + */ +int main( int argc, char **argv ) +{ + std::cout << "Starting SuperGenius demo app " << std::endl; + + app.init( argc, argv ); + app.run(); + app.exit(); + return 0; +} \ No newline at end of file diff --git a/build/CommonBuildParameters.cmake b/build/CommonBuildParameters.cmake index 496ef03c..794d8eed 100644 --- a/build/CommonBuildParameters.cmake +++ b/build/CommonBuildParameters.cmake @@ -267,6 +267,9 @@ include_directories(${xxhash_INCLUDE_DIR}) include_directories( ${PROJECT_ROOT}/src ) +include_directories( + ${PROJECT_ROOT}/app +) ADD_DEFINITIONS(-D_HAS_AUTO_PTR_ETC=1) @@ -288,7 +291,7 @@ link_directories( ) add_subdirectory(${PROJECT_ROOT}/src ${CMAKE_BINARY_DIR}/src) -#add_subdirectory(${PROJECT_ROOT}/node ${CMAKE_BINARY_DIR}/node) +add_subdirectory(${PROJECT_ROOT}/app ${CMAKE_BINARY_DIR}/app) if (TESTING) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 208e73d6..92672f41 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -#add_subdirectory(application) +add_subdirectory(application) add_subdirectory(api) add_subdirectory(base) add_subdirectory(blockchain) diff --git a/src/application/CMakeLists.txt b/src/application/CMakeLists.txt index 9265eae9..107a7533 100644 --- a/src/application/CMakeLists.txt +++ b/src/application/CMakeLists.txt @@ -39,35 +39,34 @@ target_link_libraries(configuration_storage sr25519_types config_reader_error ) -add_library(block_producing_node_application - impl/block_producing_node_application.cpp - ) -target_link_libraries(block_producing_node_application - block_producing_node_injector - blob - ) -if(FORCE_MULTILE) - set_target_properties(block_producing_node_application PROPERTIES LINK_FLAGS "${MULTIPLE_OPTION}") -endif() +#add_library(block_producing_node_application +# impl/block_producing_node_application.cpp +# ) +#target_link_libraries(block_producing_node_application +# block_producing_node_injector +# blob +# ) +#if(FORCE_MULTILE) +# set_target_properties(block_producing_node_application PROPERTIES LINK_FLAGS "${MULTIPLE_OPTION}") +#endif() add_library(validating_node_application impl/validating_node_application.cpp ) target_link_libraries(validating_node_application - validating_node_injector blob ) if(FORCE_MULTILE) set_target_properties(validating_node_application PROPERTIES LINK_FLAGS "${MULTIPLE_OPTION}") endif() -add_library(syncing_node_application - impl/syncing_node_application.cpp - ) -target_link_libraries(syncing_node_application - syncing_node_injector - blob - ) -if(FORCE_MULTILE) - set_target_properties(syncing_node_application PROPERTIES LINK_FLAGS "${MULTIPLE_OPTION}") -endif() +#add_library(syncing_node_application +# impl/syncing_node_application.cpp +# ) +#target_link_libraries(syncing_node_application +# syncing_node_injector +# blob +# ) +#if(FORCE_MULTILE) +# set_target_properties(syncing_node_application PROPERTIES LINK_FLAGS "${MULTIPLE_OPTION}") +#endif() diff --git a/src/application/app_config.hpp b/src/application/app_config.hpp index f7a1cd75..6b22e4c1 100644 --- a/src/application/app_config.hpp +++ b/src/application/app_config.hpp @@ -7,65 +7,70 @@ #include #include -namespace sgns::application { +namespace sgns::application +{ - /** + /** * Parse and store application config. */ - class AppConfiguration { - public: - enum struct LoadScheme { - kBlockProducing, - kValidating, - kFullSyncing, - }; - - public: - virtual ~AppConfiguration() = default; - - /** + class AppConfiguration + { + public: + enum struct LoadScheme + { + kBlockProducing, + kValidating, + kFullSyncing, + }; + + public: + virtual ~AppConfiguration() = default; + + /** * @return file path with genesis configuration. */ - virtual const std::string &genesis_path() const = 0; + virtual const std::string &genesis_path() const = 0; - /** + /** * @return keystore directory path. */ - virtual const std::string &keystore_path() const = 0; + virtual const std::string &keystore_path() const = 0; - /** + /** * @return rocksdb directory path. */ - virtual const std::string & rocksdb_path() const = 0; + virtual const std::string &rocksdb_path() const = 0; - /** + /** * @return port for peer to peer interactions. */ - virtual uint16_t p2p_port() const = 0; + virtual uint16_t p2p_port() const = 0; - /** + /** * @return endpoint for RPC over HTTP. */ - virtual const boost::asio::ip::tcp::endpoint &rpc_http_endpoint() const = 0; + virtual const boost::asio::ip::tcp::endpoint &rpc_http_endpoint() const = 0; - /** + /** * @return endpoint for RPC over Websocket protocol. */ - virtual const boost::asio::ip::tcp::endpoint &rpc_ws_endpoint() const = 0; + virtual const boost::asio::ip::tcp::endpoint &rpc_ws_endpoint() const = 0; - /** + /** * @return log level (0-trace, 5-only critical, 6-no logs). */ - virtual spdlog::level::level_enum verbosity() const = 0; + virtual spdlog::level::level_enum verbosity() const = 0; - /** + /** * @return true if node in only finalizing mode, otherwise false. */ - virtual bool is_only_finalizing() const = 0; - }; + virtual bool is_only_finalizing() const = 0; + + virtual bool initialize_from_args( LoadScheme scheme, int argc, char **argv ) = 0; + }; - using AppConfigPtr = std::shared_ptr; + using AppConfigPtr = std::shared_ptr; -} // namespace sgns::application +} // namespace sgns::application -#endif // SUPERGENIUS_APP_CONFIG_HPP +#endif // SUPERGENIUS_APP_CONFIG_HPP diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 26da0ed5..2cabb19e 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -1,69 +1,85 @@ #include "application/impl/validating_node_application.hpp" - +#include "application/impl/app_state_manager_impl.hpp" +#include "application/impl/configuration_storage_impl.hpp" +#include "verification/production/impl/production_impl.hpp" +#include "verification/finality/impl/finality_impl.hpp" +#include "network/impl/router_libp2p.hpp" #include +#include +#include "clock/impl/clock_impl.hpp" -namespace sgns::application { - - ValidatingNodeApplication::ValidatingNodeApplication( - const AppConfigPtr &app_config) - : injector_{injector::makeFullNodeInjector(app_config)}, - logger_(base::createLogger("Application")) { - spdlog::set_level(app_config->verbosity()); +namespace sgns::application +{ - // genesis launch if database does not exist - production_execution_strategy_ = boost::filesystem::exists(app_config->rocksdb_path()) - ? Production::ExecutionStrategy::SYNC_FIRST - : Production::ExecutionStrategy::GENESIS; + ValidatingNodeApplication::ValidatingNodeApplication( const std::shared_ptr &app_config ) : + logger_( base::createLogger( "Application" ) ) + { + spdlog::set_level( app_config->verbosity() ); - // keep important instances, the must exist when injector destroyed - // some of them are requested by reference and hence not copied - app_state_manager_ = injector_.create>(); + // genesis launch if database does not exist + production_execution_strategy_ = boost::filesystem::exists( app_config->rocksdb_path() ) ? Production::ExecutionStrategy::SYNC_FIRST : + Production::ExecutionStrategy::GENESIS; - io_context_ = injector_.create>(); - config_storage_ = injector_.create>(); - key_storage_ = injector_.create>(); - clock_ = injector_.create>(); - production_ = injector_.create>(); - finality_ = injector_.create>(); - router_ = injector_.create>(); + // keep important instances, the must exist when injector destroyed + // some of them are requested by reference and hence not copied + app_state_manager_ = std::make_shared(); + io_context_ = std::make_shared(); + config_storage_ = ( ConfigurationStorageImpl::create( app_config->genesis_path() ) ).value(); + key_storage_ = ( LocalKeyStorage::create( app_config->keystore_path() ) ).value(); + clock_ = std::make_shared(); + production_ = std::make_shared(); + finality_ = std::make_shared(); + router_ = std::make_shared(); - jrpc_api_service_ = injector_.create>(); - } + jrpc_api_service_ = std::make_shared(); + } - void ValidatingNodeApplication::run() { - logger_->info("Start as {} with PID {}", typeid(*this).name(), getpid()); + void ValidatingNodeApplication::run() + { + logger_->info( "Start as {} with PID {}", typeid( *this ).name(), getpid() ); - production_->setExecutionStrategy(production_execution_strategy_); + production_->setExecutionStrategy( production_execution_strategy_ ); - app_state_manager_->atLaunch([this] { - // execute listeners - io_context_->post([this] { - const auto ¤t_peer_info = - injector_.template create(); - auto &host = injector_.template create(); - 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; - }); + app_state_manager_->atLaunch( + [this] + { + // execute listeners + io_context_->post( + [this] + { + //di::bind.to( [p2p_port{ app_config->p2p_port() }]( const auto &injector ) + // { return get_peer_info( injector, p2p_port ); } ), + auto p2p_injector = libp2p::injector::makeHostInjector(); + //auto &key_marshaller = p2p_injector.template create(); + //libp2p::peer::PeerId peer_id = libp2p::peer::PeerId::fromPublicKey( key_marshaller.marshal( public_key ).value() ).value(); + //auto p2p_info = std::shared_ptr(); + //const network::OwnPeerInfo ¤t_peer_info(); + libp2p::Host &host = p2p_injector.template create(); + //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; + } ); - app_state_manager_->atLaunch([ctx{io_context_}] { - std::thread asio_runner([ctx{ctx}] { ctx->run(); }); - asio_runner.detach(); - return true; - }); + app_state_manager_->atLaunch( + [ctx{ io_context_ }] + { + std::thread asio_runner( [ctx{ ctx }] { ctx->run(); } ); + asio_runner.detach(); + return true; + } ); - app_state_manager_->atShutdown([ctx{io_context_}] { ctx->stop(); }); + app_state_manager_->atShutdown( [ctx{ io_context_ }] { ctx->stop(); } ); - app_state_manager_->run(); - } + app_state_manager_->run(); + } -} // namespace sgns::application +} // namespace sgns::application diff --git a/src/application/impl/validating_node_application.hpp b/src/application/impl/validating_node_application.hpp index f8519c0b..662cf3cf 100644 --- a/src/application/impl/validating_node_application.hpp +++ b/src/application/impl/validating_node_application.hpp @@ -8,27 +8,21 @@ #include "application/app_config.hpp" #include "application/configuration_storage.hpp" #include "application/impl/local_key_storage.hpp" -#include "injector/validating_node_injector.hpp" -#include "runtime/dummy/finality_api_dummy.hpp" #include "verification/finality/finality.hpp" -namespace sgns::application { +#include "verification/production.hpp" +#include "network/router.hpp" +namespace sgns::application +{ - class ValidatingNodeApplication : public SgnsApplication { - using Production = verification::Production; - using Finality = verification::finality::Finality; - using InjectorType = - decltype(injector::makeFullNodeInjector(AppConfigPtr{})); + class ValidatingNodeApplication : public SgnsApplication + { + using Production = verification::Production; + using Finality = verification::finality::Finality; - template - using sptr = std::shared_ptr; + public: + ~ValidatingNodeApplication() override = default; - template - using uptr = std::unique_ptr; - - public: - ~ValidatingNodeApplication() override = default; - - /** + /** * @param config_path genesis configs path * @param keystore_path local peer's keys * @param rocksdb_path storage path @@ -39,32 +33,31 @@ namespace sgns::application { * node * @param verbosity level of logging */ - ValidatingNodeApplication(const AppConfigPtr &config); + ValidatingNodeApplication( const std::shared_ptr &config ); - void run() override; + void run() override; - private: - // need to keep all of these instances, since injector itself is destroyed - InjectorType injector_; + private: + // need to keep all of these instances, since injector itself is destroyed - std::shared_ptr app_state_manager_; + std::shared_ptr app_state_manager_; - std::shared_ptr io_context_; + std::shared_ptr io_context_; - sptr config_storage_; - sptr key_storage_; - sptr clock_; - sptr production_; - sptr finality_; - sptr router_; + std::shared_ptr config_storage_; + std::shared_ptr key_storage_; + std::shared_ptr clock_; + std::shared_ptr production_; + std::shared_ptr finality_; + std::shared_ptr router_; - sptr jrpc_api_service_; + std::shared_ptr jrpc_api_service_; - Production::ExecutionStrategy production_execution_strategy_; + Production::ExecutionStrategy production_execution_strategy_; - base::Logger logger_; - }; + base::Logger logger_; + }; -} // namespace sgns::application +} // namespace sgns::application -#endif // SUPERGENIUS_SRC_APPLICATION_IMPL_VALIDATING_NODE_APPLICATION_HPP +#endif // SUPERGENIUS_SRC_APPLICATION_IMPL_VALIDATING_NODE_APPLICATION_HPP From 30b22c4c0f630a3cd5c801a9585927fd8f0161d6 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Thu, 22 Feb 2024 18:36:15 -0300 Subject: [PATCH 02/14] Add: Factories of stuff. Need to decouple them and finish it --- app/SuperGeniusDemoApp.cpp | 2 +- app/integration/AppConfigurationFactory.hpp | 22 ++++++++++ app/integration/AppStateManagerFactory.hpp | 22 ++++++++++ app/integration/BlockTreeFactory.hpp | 33 +++++++++++++++ app/integration/BufferStorageFactory.hpp | 36 ++++++++++++++++ .../ConfigurationStorageFactory.hpp | 32 +++++++++++++++ app/integration/HasherFactory.hpp | 20 +++++++++ app/integration/KeyStorageFactory.hpp | 32 +++++++++++++++ app/integration/ProductionFactory.hpp | 41 +++++++++++++++++++ app/integration/ProductionLotteryFactory.hpp | 23 +++++++++++ app/integration/SystemClockFactory.hpp | 22 ++++++++++ app/integration/VRFProviderFactory.hpp | 26 ++++++++++++ .../impl/validating_node_application.cpp | 16 ++++---- 13 files changed, 319 insertions(+), 8 deletions(-) create mode 100644 app/integration/AppConfigurationFactory.hpp create mode 100644 app/integration/AppStateManagerFactory.hpp create mode 100644 app/integration/BlockTreeFactory.hpp create mode 100644 app/integration/BufferStorageFactory.hpp create mode 100644 app/integration/ConfigurationStorageFactory.hpp create mode 100644 app/integration/HasherFactory.hpp create mode 100644 app/integration/KeyStorageFactory.hpp create mode 100644 app/integration/ProductionFactory.hpp create mode 100644 app/integration/ProductionLotteryFactory.hpp create mode 100644 app/integration/SystemClockFactory.hpp create mode 100644 app/integration/VRFProviderFactory.hpp diff --git a/app/SuperGeniusDemoApp.cpp b/app/SuperGeniusDemoApp.cpp index cfb06cb1..cddc1ede 100644 --- a/app/SuperGeniusDemoApp.cpp +++ b/app/SuperGeniusDemoApp.cpp @@ -23,7 +23,7 @@ void SuperGeniusDemoApp::init( int argc, char **argv ) auto logger = sgns::base::createLogger( "SuperGenius block node: " ); //cfg = std::shared_ptr( std::move( AppConfigurationFactory::create( logger ) ) ); - cfg = std::shared_ptr( AppConfigurationFactory::create( logger ) ); + cfg = AppConfigurationFactory::create( logger ); cfg->initialize_from_args( sgns::application::AppConfiguration::LoadScheme::kValidating, argc, argv ); diff --git a/app/integration/AppConfigurationFactory.hpp b/app/integration/AppConfigurationFactory.hpp new file mode 100644 index 00000000..0c431f3e --- /dev/null +++ b/app/integration/AppConfigurationFactory.hpp @@ -0,0 +1,22 @@ +/** + * @file AppConfigurationFactory.hpp + * @brief Factory to create AppConfiguration derived classes + * @date 2024-02-21 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _APP_CONFIGURATION_FACTORY_HPP_ +#define _APP_CONFIGURATION_FACTORY_HPP_ + +#include "application/impl/app_config_impl.hpp" + +class AppConfigurationFactory +{ +public: + static std::shared_ptr create( sgns::base::Logger logger ) + { + return std::make_shared( std::move( logger ) ); + } +}; + +#endif diff --git a/app/integration/AppStateManagerFactory.hpp b/app/integration/AppStateManagerFactory.hpp new file mode 100644 index 00000000..72df31d3 --- /dev/null +++ b/app/integration/AppStateManagerFactory.hpp @@ -0,0 +1,22 @@ +/** + * @file AppStateManagerFactory.hpp + * @brief Factory to create AppStateManager derived classes + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _APP_STATE_MANAGER_FACTORY_HPP_ +#define _APP_STATE_MANAGER_FACTORY_HPP_ + +#include "application/impl/app_state_manager_impl.hpp" + +class AppStateManagerFactory +{ +public: + static std::shared_ptr create() + { + return std::make_shared(); + } +}; + +#endif diff --git a/app/integration/BlockTreeFactory.hpp b/app/integration/BlockTreeFactory.hpp new file mode 100644 index 00000000..924b4c4a --- /dev/null +++ b/app/integration/BlockTreeFactory.hpp @@ -0,0 +1,33 @@ +/** + * @file BlockTreeFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _BLOCK_TREE_FACTORY_HPP_ +#define _BLOCK_TREE_FACTORY_HPP_ + +class BlockTreeFactory +{ +public: + static std::shared_ptr create( const std::string &db_path ) + { + auto header_repo_ = std::make_shared( BufferStorageFactory::create( "rocksdb", db_path ), + HasherFactory::create() ); + + auto result = sgns::blockchain::BlockTreeImpl::create( // + header_repo_, // + ); + + if ( result ) + { + return result.value(); + } + else + { + throw std::runtime_error( "BlockTree not created" ); + } + } +} + +#endif diff --git a/app/integration/BufferStorageFactory.hpp b/app/integration/BufferStorageFactory.hpp new file mode 100644 index 00000000..4c9759c3 --- /dev/null +++ b/app/integration/BufferStorageFactory.hpp @@ -0,0 +1,36 @@ +/** + * @file BufferStorageFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + #ifndef _BUFFER_STORAGE_HPP_ + #define _BUFFER_STORAGE_HPP_ + +class BufferStorageFactory +{ + public: + static std::shared_ptr create(const std::string &type, const std::string &path) + { + if (type == "rocksdb") + { + auto result = sgns::storage::rocksdb::create(path); + if (result) + { + return result.value(); + } + else + { + throw std::runtime_error("rocksdb not created"); + } + + } + else + { + + } + } +} + + #endif + diff --git a/app/integration/ConfigurationStorageFactory.hpp b/app/integration/ConfigurationStorageFactory.hpp new file mode 100644 index 00000000..d535e020 --- /dev/null +++ b/app/integration/ConfigurationStorageFactory.hpp @@ -0,0 +1,32 @@ +/** + * @file ConfigurationStorageFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _CONFIGURATION_STORAGE_FACTORY_HPP_ +#define _CONFIGURATION_STORAGE_FACTORY_HPP_ + +#include "application/impl/configuration_storage_impl.hpp" + +class ConfigurationStorageFactory +{ +public: + static std::shared_ptr create( const std::string &path ) + { + + auto result = sgns::application::ConfigurationStorageImpl::create( path ); + + if ( result ) + { + return result.value(); + } + else + { + throw std::runtime_error( "Configuration storage not found on Genesis path" ); + } + } +}; + +#endif diff --git a/app/integration/HasherFactory.hpp b/app/integration/HasherFactory.hpp new file mode 100644 index 00000000..84e8bc5c --- /dev/null +++ b/app/integration/HasherFactory.hpp @@ -0,0 +1,20 @@ +/** + * @file HasherFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _HASHER_FACTORY_HPP_ +#define _HASHER_FACTORY_HPP_ + +#include "crrpto/hasher/hasher_impl.hpp" + +class HasherFactory +{ +public: + static std::shared_ptr create() + { + return std::make_shared(); + } +} +#endif diff --git a/app/integration/KeyStorageFactory.hpp b/app/integration/KeyStorageFactory.hpp new file mode 100644 index 00000000..28a99c65 --- /dev/null +++ b/app/integration/KeyStorageFactory.hpp @@ -0,0 +1,32 @@ +/** + * @file KeyStorageFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _KEY_STORAGE_FACTORY_HPP_ +#define _KEY_STORAGE_FACTORY_HPP_ + +#include "application/impl/local_key_storage.hpp" + +class KeyStorageFactory +{ +public: + static std::shared_ptr create( const std::string &path ) + { + + auto result = sgns::application::LocalKeyStorage::create( path ); + + if ( result ) + { + return result.value(); + } + else + { + throw std::runtime_error( "Key storage not found on keystore path" ); + } + } +}; + +#endif \ No newline at end of file diff --git a/app/integration/ProductionFactory.hpp b/app/integration/ProductionFactory.hpp new file mode 100644 index 00000000..c3fbf75d --- /dev/null +++ b/app/integration/ProductionFactory.hpp @@ -0,0 +1,41 @@ +/** + * @file ProductionFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _PRODUCTION_FACTORY_HPP_ +#define _PRODUCTION_FACTORY_HPP_ + +class ProductionFactory +{ +public: + static std::shared_ptr create() + { + + BlockExecutor + return std::make_shared( // + AppStateManagerFactory::create(),// + ProductionLotteryFactory::create(),// + + ); + + /** + * std::shared_ptr block_executor, + std::shared_ptr trie_db, + std::shared_ptr epoch_storage, + std::shared_ptr configuration, + std::shared_ptr proposer, + std::shared_ptr block_tree, + std::shared_ptr gossiper, + crypto::SR25519Keypair keypair, + std::shared_ptr clock, + std::shared_ptr hasher, + std::unique_ptr timer, + std::shared_ptr + */ + } +}; + +#endif diff --git a/app/integration/ProductionLotteryFactory.hpp b/app/integration/ProductionLotteryFactory.hpp new file mode 100644 index 00000000..7a307e0b --- /dev/null +++ b/app/integration/ProductionLotteryFactory.hpp @@ -0,0 +1,23 @@ +/** + * @file ProductionLotteryFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#include "verification/production/impl/production_lottery_impl.hpp" +#include "integration/VRFProviderFactory.hpp" +#include "integration/HasherFactory.hpp" + +class ProductionLotteryFactory +{ +public: + static std::shared_ptr create() + { + return std::make_shared( // + VRFProviderFactory::create(), // + HasherFactory::create(), + + ); + } +} \ No newline at end of file diff --git a/app/integration/SystemClockFactory.hpp b/app/integration/SystemClockFactory.hpp new file mode 100644 index 00000000..4888df4b --- /dev/null +++ b/app/integration/SystemClockFactory.hpp @@ -0,0 +1,22 @@ +/** + * @file SystemClockFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _SYSTEM_CLOCK_FACTORY_HPP_ +#define _SYSTEM_CLOCK_FACTORY_HPP_ + +#include "application/impl/local_key_storage.hpp" + +class SystemClockFactory +{ +public: + static std::shared_ptr create() + { + return std::make_shared(); + } +}; + +#endif \ No newline at end of file diff --git a/app/integration/VRFProviderFactory.hpp b/app/integration/VRFProviderFactory.hpp new file mode 100644 index 00000000..51574677 --- /dev/null +++ b/app/integration/VRFProviderFactory.hpp @@ -0,0 +1,26 @@ +/** + * @file VRFProviderFactory.hpp + * @brief + * @date 2024-02-22 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#ifndef _VRF_PROVIDER_FACTORY_HPP_ +#ifndef _VRF_PROVIDER_FACTORY_HPP_ + +#include "crypto/vrf/vrf_provider_impl.hpp" + +class VRFProviderFactory +{ +public: + static std::shared_ptr create() + { + + //TODO - Remove CSPRNG. Don't like libp2p injector stuff. Probably use crypto3 here + + auto p2p_injector = libp2p::injector::makeHostInjector(); + auto random_generator = p2p_injector.template create>(); + + return std::make_shared( random_generator ); + } +}; \ No newline at end of file diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 2cabb19e..4266ec78 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -1,6 +1,8 @@ #include "application/impl/validating_node_application.hpp" -#include "application/impl/app_state_manager_impl.hpp" -#include "application/impl/configuration_storage_impl.hpp" +#include "integration/AppStateManagerFactory.hpp" +#include "integration/ConfigurationStorageFactory.hpp" +#include "integration/KeyStorageFactory.hpp" + #include "verification/production/impl/production_impl.hpp" #include "verification/finality/impl/finality_impl.hpp" #include "network/impl/router_libp2p.hpp" @@ -22,16 +24,16 @@ namespace sgns::application // keep important instances, the must exist when injector destroyed // some of them are requested by reference and hence not copied - app_state_manager_ = std::make_shared(); - io_context_ = std::make_shared(); - config_storage_ = ( ConfigurationStorageImpl::create( app_config->genesis_path() ) ).value(); - key_storage_ = ( LocalKeyStorage::create( app_config->keystore_path() ) ).value(); - clock_ = std::make_shared(); + app_state_manager_ = AppStateManagerFactory::create(); + config_storage_ = ConfigurationStorageFactory::create( app_config->genesis_path() ); + key_storage_ = KeyStorageFactory::create( app_config->keystore_path() ); + clock_ = SystemClockFactory::create(); production_ = std::make_shared(); finality_ = std::make_shared(); router_ = std::make_shared(); jrpc_api_service_ = std::make_shared(); + io_context_ = std::make_shared(); } void ValidatingNodeApplication::run() From 421bb4d3586107fd76e92d1a226fdb4f2ac955e3 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 09:28:53 -0300 Subject: [PATCH 03/14] Add: Singleton class --- app/Singleton.hpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 app/Singleton.hpp diff --git a/app/Singleton.hpp b/app/Singleton.hpp new file mode 100644 index 00000000..f85df79e --- /dev/null +++ b/app/Singleton.hpp @@ -0,0 +1,80 @@ +//--------------------------------------------------------------------- +// File: Singleton.hpp +// Description: template for Singleton objects +// Created: 11/30/03 +// Author: Kenneth L. Hurley +/// \note Elemental Engine +/// Copyright (C) 2013 Social Systems Technology, Inc. +/// +/// This code is redistributable under the terms of the EE License. +/// +/// This code is distributed without warranty or implied warranty of +/// merchantability or fitness for a particular purpose. See the +/// EE License for more details. +/// +/// You should have received a copy of the EE License along with this +/// code; If not, write to Social Systems Technology, Inc., +/// 109 East 17th Street Suite 4210 Cheyenne, WY 82001 USA +//--------------------------------------------------------------------- + +#ifndef SINGLETON_H +#define SINGLETON_H + +// +// this code is so that object can register themselves with +// the system. +// + +template +class CSingleton +{ +private: + static bool isInitialized; + static T *_instance; // first initialize memory without constructor +public: + static T *Instance() + { + // start lock for multithreading here + if (_instance == NULL) + { + _instance = new T(_instance); + } + + if (!isInitialized) + { + isInitialized = true; + new (_instance) T(); + } + return _instance; + }; + +private: + CSingleton() {}; + ~CSingleton() {}; + // disable copy & assignment + CSingleton( CSingleton const&); + CSingleton& operator=( CSingleton const&); +}; + +template +bool CSingleton::isInitialized = false; + +template +T *CSingleton::_instance = NULL; + +#define SINGLETONINSTANCE(T) \ + CSingleton< T >::Instance() + +#define SINGLETON(T) \ + protected: \ + friend class CSingleton< T >; \ + T(T *) {} \ + T(){} + +#define SINGLETONCONSTRUCTOROVERRIDE(T) \ + protected: \ + friend class CSingleton< T >; \ + T(T *) {} + + +#endif // #ifndef SINGLETON_H \ No newline at end of file From 6495774d3c6a6ae47cf2dcb00785bcbd11f86ddb Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 11:06:45 -0300 Subject: [PATCH 04/14] Add: Singleton factory class --- app/CMakeLists.txt | 19 ++++- app/SuperGeniusDemoApp.cpp | 2 +- app/integration/BufferStorageFactory.hpp | 22 +++-- app/integration/CComponentFactory.cpp | 40 +++++++++ app/integration/CComponentFactory.hpp | 22 +++++ app/integration/IComponent.hpp | 18 ++++ app/integration/IComponentFactory.hpp | 18 ++++ .../impl/app_state_manager_impl.hpp | 83 ++++++++++--------- 8 files changed, 172 insertions(+), 52 deletions(-) create mode 100644 app/integration/CComponentFactory.cpp create mode 100644 app/integration/CComponentFactory.hpp create mode 100644 app/integration/IComponent.hpp create mode 100644 app/integration/IComponentFactory.hpp diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 2a94db39..799fc9b2 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable (sgns_demo runner.cpp SuperGeniusDemoApp.cpp + integration/CComponentFactory.cpp ) target_link_libraries(sgns_demo @@ -9,9 +10,23 @@ add_executable (sgns_demo Boost::thread Boost::filesystem ipfs_blockservice - #block_producing_node_application validating_node_application - #app_config_impl + app_state_manager + app_config_impl + local_key_storage + configuration_storage + p2p::p2p_basic_host + p2p::p2p_default_network + p2p::p2p_peer_repository + p2p::p2p_inmem_address_repository + p2p::p2p_inmem_key_repository + p2p::p2p_inmem_protocol_repository + p2p::p2p_literals + p2p::p2p_kademlia + p2p::p2p_identify + p2p::p2p_ping + p2p::p2p_peer_address + p2p::p2p_protocol_echo #protobuf::libprotobuf #${USER_ENV_LIBRARY} # On windows platform , sr25519 library needs extra library userenv, so folllowing code is added #${WIN_CRYPT_LIBRARY} diff --git a/app/SuperGeniusDemoApp.cpp b/app/SuperGeniusDemoApp.cpp index cddc1ede..03630d39 100644 --- a/app/SuperGeniusDemoApp.cpp +++ b/app/SuperGeniusDemoApp.cpp @@ -6,7 +6,7 @@ */ #include "SuperGeniusDemoApp.hpp" #include "base/logger.hpp" -//#include "impl/app_config_factory.hpp" +#include "integration/AppConfigurationFactory.hpp" SuperGeniusDemoApp::SuperGeniusDemoApp() { diff --git a/app/integration/BufferStorageFactory.hpp b/app/integration/BufferStorageFactory.hpp index 4c9759c3..6e30c40e 100644 --- a/app/integration/BufferStorageFactory.hpp +++ b/app/integration/BufferStorageFactory.hpp @@ -4,33 +4,31 @@ * @date 2024-02-22 * @author Henrique A. Klein (hklein@gnus.ai) */ - #ifndef _BUFFER_STORAGE_HPP_ - #define _BUFFER_STORAGE_HPP_ +#ifndef _BUFFER_STORAGE_HPP_ +#define _BUFFER_STORAGE_HPP_ class BufferStorageFactory { - public: - static std::shared_ptr create(const std::string &type, const std::string &path) +public: + static std::shared_ptr create( const std::string &type, const std::string &path ) { - if (type == "rocksdb") + if ( type == "rocksdb" ) { - auto result = sgns::storage::rocksdb::create(path); - if (result) + auto result = sgns::storage::rocksdb::create( path ); + if ( result ) { return result.value(); } else { - throw std::runtime_error("rocksdb not created"); + throw std::runtime_error( "rocksdb not created" ); } - } else { - + //TrieStorageBackend } } } - #endif - +#endif diff --git a/app/integration/CComponentFactory.cpp b/app/integration/CComponentFactory.cpp new file mode 100644 index 00000000..7c4e080d --- /dev/null +++ b/app/integration/CComponentFactory.cpp @@ -0,0 +1,40 @@ +/** + * @file CComponentFactory.cpp + * @brief + * @date 2024-02-23 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#include "CComponentFactory.hpp" + +void CComponentFactory::Register( std::shared_ptr component, const std::string &type, const boost::optional &variant ) +{ + + std::string key = type; + + if ( variant ) + { + key.append( variant.value() ); + } + if ( ComponentTable.count( key ) == 0 ) + { + ComponentTable[key] = component; + } + else + { + ComponentTable[key] = component; // Overwrites existing value + } +} + +boost::optional> CComponentFactory::GetComponent( const std::string &type, const boost::optional &variant ) +{ + + std::string key = type; + + if ( variant ) + { + key.append( variant.value() ); + } + + return ComponentTable[key]; +} \ No newline at end of file diff --git a/app/integration/CComponentFactory.hpp b/app/integration/CComponentFactory.hpp new file mode 100644 index 00000000..40c78030 --- /dev/null +++ b/app/integration/CComponentFactory.hpp @@ -0,0 +1,22 @@ +/** + * @file CComponentFactory.hpp + * @brief + * @date 2024-02-23 + * @author Henrique A. Klein (hklein@gnus.ai) + */ + +#include "IComponentFactory.hpp" +#include "Singleton.hpp" + +class CComponentFactory : public IComponentFactory +{ + SINGLETON( CComponentFactory ); + +private: + std::unordered_map> ComponentTable; + +public: + void Register( std::shared_ptr component, const std::string &type, const boost::optional &variant ) override; + + boost::optional> GetComponent( const std::string &type, const boost::optional &variant ) override; +}; \ No newline at end of file diff --git a/app/integration/IComponent.hpp b/app/integration/IComponent.hpp new file mode 100644 index 00000000..943f45f4 --- /dev/null +++ b/app/integration/IComponent.hpp @@ -0,0 +1,18 @@ +/** + * @file IComponent.hpp + * @brief Component interface class + * @date 2024-02-23 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _ICOMPONENT_HPP_ +#define _ICOMPONENT_HPP_ +#include +class IComponent +{ +public: + virtual ~IComponent() = default; + virtual std::string GetName() = 0; + +}; + +#endif \ No newline at end of file diff --git a/app/integration/IComponentFactory.hpp b/app/integration/IComponentFactory.hpp new file mode 100644 index 00000000..1b282a15 --- /dev/null +++ b/app/integration/IComponentFactory.hpp @@ -0,0 +1,18 @@ +/** + * @file IComponentFactory.hpp + * @brief + * @date 2024-02-23 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#include "IComponent.hpp" +#include + +class IComponentFactory +{ +public: + virtual ~IComponentFactory() = default; + + virtual void Register( std::shared_ptr component, const std::string &type, const boost::optional &variant ) = 0; + + virtual boost::optional> GetComponent( const std::string &type, const boost::optional &variant ) = 0; +}; \ No newline at end of file diff --git a/src/application/impl/app_state_manager_impl.hpp b/src/application/impl/app_state_manager_impl.hpp index 8d3cef3c..97fae335 100644 --- a/src/application/impl/app_state_manager_impl.hpp +++ b/src/application/impl/app_state_manager_impl.hpp @@ -10,58 +10,67 @@ #include #include "base/logger.hpp" +#include "integration/IComponent.hpp" -namespace sgns::application { +namespace sgns::application +{ - class AppStateManagerImpl : public AppStateManager { - public: - AppStateManagerImpl(); - AppStateManagerImpl(const AppStateManagerImpl &) = delete; - AppStateManagerImpl(AppStateManagerImpl &&) noexcept = delete; + class AppStateManagerImpl : public AppStateManager, public IComponent + { + public: + AppStateManagerImpl(); + AppStateManagerImpl( const AppStateManagerImpl & ) = delete; + AppStateManagerImpl( AppStateManagerImpl && ) noexcept = delete; - ~AppStateManagerImpl() override; + ~AppStateManagerImpl() override; - AppStateManagerImpl &operator=(AppStateManagerImpl const &) = delete; - AppStateManagerImpl &operator=(AppStateManagerImpl &&) noexcept = delete; + AppStateManagerImpl &operator=( AppStateManagerImpl const & ) = delete; + AppStateManagerImpl &operator=( AppStateManagerImpl && ) noexcept = delete; - void atPrepare(OnPrepare &&cb) override; - void atLaunch(OnLaunch &&cb) override; - void atShutdown(OnShutdown &&cb) override; + void atPrepare( OnPrepare &&cb ) override; + void atLaunch( OnLaunch &&cb ) override; + void atShutdown( OnShutdown &&cb ) override; - void run() override; - void shutdown() override; + void run() override; + void shutdown() override; - State state() const override { - return state_; - } + State state() const override + { + return state_; + } + std::string GetName() override + { + return "AppStateManagerImpl"; + } - protected: - void reset(); - void doPrepare() override; - void doLaunch() override; - void doShutdown() override; + protected: + void reset(); - private: - static std::weak_ptr wp_to_myself; - static void shuttingDownSignalsHandler(int); + void doPrepare() override; + void doLaunch() override; + void doShutdown() override; - base::Logger logger_; + private: + static std::weak_ptr wp_to_myself; + static void shuttingDownSignalsHandler( int ); - State state_ = State::Init; + base::Logger logger_; - std::recursive_mutex mutex_; + State state_ = State::Init; - std::mutex cv_mutex_; - std::condition_variable cv_; + std::recursive_mutex mutex_; - std::queue prepare_; - std::queue launch_; - std::queue shutdown_; + std::mutex cv_mutex_; + std::condition_variable cv_; - std::atomic_bool shutdown_requested_{false}; - }; + std::queue prepare_; + std::queue launch_; + std::queue shutdown_; -} // namespace sgns::application + std::atomic_bool shutdown_requested_{ false }; + }; -#endif // SUPERGENIUS_SRC_APP_STATE_MANAGER +} // namespace sgns::application + +#endif // SUPERGENIUS_SRC_APP_STATE_MANAGER From b23304ba2c48ed39dad21a808c3f5532302956c3 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 11:07:39 -0300 Subject: [PATCH 05/14] Cleanup: Removing runtime core from block executor --- src/verification/production/impl/block_executor.cpp | 6 ++---- src/verification/production/impl/block_executor.hpp | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/verification/production/impl/block_executor.cpp b/src/verification/production/impl/block_executor.cpp index a17932b5..6d9463dc 100644 --- a/src/verification/production/impl/block_executor.cpp +++ b/src/verification/production/impl/block_executor.cpp @@ -12,7 +12,6 @@ namespace sgns::verification { BlockExecutor::BlockExecutor( std::shared_ptr block_tree, - std::shared_ptr core, std::shared_ptr configuration, std::shared_ptr production_synchronizer, std::shared_ptr block_validator, @@ -22,7 +21,6 @@ namespace sgns::verification { std::shared_ptr authority_update_observer) : block_tree_{std::move(block_tree)}, - core_{std::move(core)}, genesis_configuration_{std::move(configuration)}, production_synchronizer_{std::move(production_synchronizer)}, block_validator_{std::move(block_validator)}, @@ -32,7 +30,6 @@ namespace sgns::verification { authority_update_observer_{std::move(authority_update_observer)}, logger_{base::createLogger("BlockExecutor")} { BOOST_ASSERT(block_tree_ != nullptr); - BOOST_ASSERT(core_ != nullptr); BOOST_ASSERT(genesis_configuration_ != nullptr); BOOST_ASSERT(production_synchronizer_ != nullptr); BOOST_ASSERT(block_validator_ != nullptr); @@ -188,7 +185,8 @@ namespace sgns::verification { // block should be applied without last digest which contains the seal block_without_seal_digest.header.digest.pop_back(); // apply block - BOOST_OUTCOME_TRYV2(auto &&, core_->execute_block(block_without_seal_digest)); + // TODO - Add something instead of binaryen block executor stuff + //BOOST_OUTCOME_TRYV2(auto &&, core_->execute_block(block_without_seal_digest)); // add block header if it does not exist BOOST_OUTCOME_TRYV2(auto &&, block_tree_->addBlock(block)); diff --git a/src/verification/production/impl/block_executor.hpp b/src/verification/production/impl/block_executor.hpp index aae6e023..a5b4fb4a 100644 --- a/src/verification/production/impl/block_executor.hpp +++ b/src/verification/production/impl/block_executor.hpp @@ -12,7 +12,6 @@ #include "crypto/hasher.hpp" #include "primitives/production_configuration.hpp" #include "primitives/block_header.hpp" -#include "runtime/core.hpp" #include "transaction_pool/transaction_pool.hpp" namespace sgns::verification { @@ -20,7 +19,6 @@ namespace sgns::verification { class BlockExecutor : public std::enable_shared_from_this { public: BlockExecutor(std::shared_ptr block_tree, - std::shared_ptr core, std::shared_ptr configuration, std::shared_ptr production_synchronizer, std::shared_ptr block_validator, @@ -67,7 +65,7 @@ namespace sgns::verification { outcome::result applyBlock(const primitives::Block &block); std::shared_ptr block_tree_; - std::shared_ptr core_; + //std::shared_ptr core_; std::shared_ptr genesis_configuration_; std::shared_ptr production_synchronizer_; std::shared_ptr block_validator_; From d4fb6e33d6cea63549b1a63fd9f2ae4d06fa1543 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 11:25:13 -0300 Subject: [PATCH 06/14] Fix: App compilating, trimmed a lot of stuff --- src/application/app_state_manager.hpp | 3 ++- src/application/impl/app_state_manager_impl.hpp | 3 +-- .../impl/validating_node_application.cpp | 17 ++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/application/app_state_manager.hpp b/src/application/app_state_manager.hpp index ec8387c8..3fc9f8f4 100644 --- a/src/application/app_state_manager.hpp +++ b/src/application/app_state_manager.hpp @@ -3,10 +3,11 @@ #define SUPERGENIUS_APPLICATION_DISPATCHER #include "base/logger.hpp" +#include "integration/IComponent.hpp" namespace sgns::application { - class AppStateManager : public std::enable_shared_from_this { + class AppStateManager : public std::enable_shared_from_this, public IComponent { public: using OnPrepare = std::function; using OnLaunch = std::function; diff --git a/src/application/impl/app_state_manager_impl.hpp b/src/application/impl/app_state_manager_impl.hpp index 97fae335..34ecf240 100644 --- a/src/application/impl/app_state_manager_impl.hpp +++ b/src/application/impl/app_state_manager_impl.hpp @@ -10,12 +10,11 @@ #include #include "base/logger.hpp" -#include "integration/IComponent.hpp" namespace sgns::application { - class AppStateManagerImpl : public AppStateManager, public IComponent + class AppStateManagerImpl : public AppStateManager { public: AppStateManagerImpl(); diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 4266ec78..3c2afe2e 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -9,6 +9,7 @@ #include #include #include "clock/impl/clock_impl.hpp" +#include "integration/CComponentFactory.hpp" namespace sgns::application { @@ -24,16 +25,18 @@ namespace sgns::application // keep important instances, the must exist when injector destroyed // some of them are requested by reference and hence not copied - app_state_manager_ = AppStateManagerFactory::create(); + SINGLETONINSTANCE( CComponentFactory ) + ->Register( std::make_shared(), "AppStateManager", boost::none ); + app_state_manager_ = std::dynamic_pointer_cast((SINGLETONINSTANCE( CComponentFactory )->GetComponent( "AppStateManager", boost::none )).value()); config_storage_ = ConfigurationStorageFactory::create( app_config->genesis_path() ); key_storage_ = KeyStorageFactory::create( app_config->keystore_path() ); - clock_ = SystemClockFactory::create(); - production_ = std::make_shared(); - finality_ = std::make_shared(); - router_ = std::make_shared(); + //clock_ = SystemClockFactory::create(); + //production_ = std::make_shared(); + //finality_ = std::make_shared(); + //router_ = std::make_shared(); - jrpc_api_service_ = std::make_shared(); - io_context_ = std::make_shared(); + //jrpc_api_service_ = std::make_shared(); + //io_context_ = std::make_shared(); } void ValidatingNodeApplication::run() From 93d70185483ca4335f7b1880cd5dbbd4d75addd9 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 13:10:18 -0300 Subject: [PATCH 07/14] Fix: Removing things that don't work yet --- src/application/impl/validating_node_application.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 3c2afe2e..df4ebb31 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -36,19 +36,20 @@ namespace sgns::application //router_ = std::make_shared(); //jrpc_api_service_ = std::make_shared(); - //io_context_ = std::make_shared(); + io_context_ = std::make_shared(); } void ValidatingNodeApplication::run() { logger_->info( "Start as {} with PID {}", typeid( *this ).name(), getpid() ); - production_->setExecutionStrategy( production_execution_strategy_ ); + //production_->setExecutionStrategy( production_execution_strategy_ ); app_state_manager_->atLaunch( [this] { // execute listeners + io_context_->post( [this] { @@ -69,7 +70,7 @@ namespace sgns::application // std::exit( 1 ); // } //} - this->router_->init(); + //this->router_->init(); } ); return true; } ); @@ -83,6 +84,7 @@ namespace sgns::application } ); app_state_manager_->atShutdown( [ctx{ io_context_ }] { ctx->stop(); } ); + app_state_manager_->run(); } From a56a6b806688b3f7beb88237bbb3f2614f44df68 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 15:17:17 -0300 Subject: [PATCH 08/14] Fix: Adding more components into factory --- app/CMakeLists.txt | 21 +- app/integration/CComponentFactory.cpp | 9 +- app/integration/SystemClockFactory.hpp | 2 +- src/application/configuration_storage.hpp | 3 +- .../impl/configuration_storage_impl.hpp | 199 ++++++++++-------- src/application/impl/local_key_storage.hpp | 5 + .../impl/validating_node_application.cpp | 45 ++-- src/application/key_storage.hpp | 3 +- src/clock/clock.hpp | 3 +- src/clock/impl/clock_impl.hpp | 5 + 10 files changed, 175 insertions(+), 120 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 799fc9b2..1b67ce9a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -15,16 +15,17 @@ add_executable (sgns_demo app_config_impl local_key_storage configuration_storage - p2p::p2p_basic_host - p2p::p2p_default_network - p2p::p2p_peer_repository - p2p::p2p_inmem_address_repository - p2p::p2p_inmem_key_repository - p2p::p2p_inmem_protocol_repository - p2p::p2p_literals - p2p::p2p_kademlia - p2p::p2p_identify - p2p::p2p_ping + clock + p2p::p2p_basic_host + p2p::p2p_default_network + p2p::p2p_peer_repository + p2p::p2p_inmem_address_repository + p2p::p2p_inmem_key_repository + p2p::p2p_inmem_protocol_repository + p2p::p2p_literals + p2p::p2p_kademlia + p2p::p2p_identify + p2p::p2p_ping p2p::p2p_peer_address p2p::p2p_protocol_echo #protobuf::libprotobuf diff --git a/app/integration/CComponentFactory.cpp b/app/integration/CComponentFactory.cpp index 7c4e080d..a0020691 100644 --- a/app/integration/CComponentFactory.cpp +++ b/app/integration/CComponentFactory.cpp @@ -36,5 +36,12 @@ boost::optional> CComponentFactory::GetComponent( co key.append( variant.value() ); } - return ComponentTable[key]; + if ( ComponentTable.count( key ) == 0 ) + { + return boost::none; + } + else + { + return ComponentTable[key]; + } } \ No newline at end of file diff --git a/app/integration/SystemClockFactory.hpp b/app/integration/SystemClockFactory.hpp index 4888df4b..143978e3 100644 --- a/app/integration/SystemClockFactory.hpp +++ b/app/integration/SystemClockFactory.hpp @@ -8,7 +8,7 @@ #ifndef _SYSTEM_CLOCK_FACTORY_HPP_ #define _SYSTEM_CLOCK_FACTORY_HPP_ -#include "application/impl/local_key_storage.hpp" +#include "clock/impl/clock_impl.hpp" class SystemClockFactory { diff --git a/src/application/configuration_storage.hpp b/src/application/configuration_storage.hpp index 856fc0fd..55717c26 100644 --- a/src/application/configuration_storage.hpp +++ b/src/application/configuration_storage.hpp @@ -8,13 +8,14 @@ #include "network/types/peer_list.hpp" #include "primitives/block.hpp" #include +#include "integration/IComponent.hpp" namespace sgns::application { /** * Stores configuration of a sgns application and provides convenience * methods for accessing config parameters */ - class ConfigurationStorage { + class ConfigurationStorage : public IComponent { public: virtual ~ConfigurationStorage() = default; diff --git a/src/application/impl/configuration_storage_impl.hpp b/src/application/impl/configuration_storage_impl.hpp index cf804a08..7167d2b3 100644 --- a/src/application/impl/configuration_storage_impl.hpp +++ b/src/application/impl/configuration_storage_impl.hpp @@ -8,95 +8,110 @@ #include -namespace sgns::application { - - class ConfigurationStorageImpl : public ConfigurationStorage { - public: - static outcome::result> create( - const std::string &config_path); - - ~ConfigurationStorageImpl() override = default; - - const std::string &name() const override { - return name_; - } - - const std::string &id() const override { - return id_; - } - - const std::string &chainType() const override { - return chain_type_; - } - - network::PeerList getBootNodes() const override { - return boot_nodes_; - } - - const std::vector> &telemetryEndpoints() - const override { - return telemetry_endpoints_; - } - - const std::string &protocolId() const override { - return protocol_id_; - } - - const std::map &properties() const override { - return properties_; - } - - boost::optional> getProperty( - const std::string &property) const override { - auto it = properties_.find(property); - if (it != properties_.end()) { - return {{it->second}}; - } - return boost::none; - } - - const std::set &forkBlocks() const override { - return fork_blocks_; - } - - const std::set &badBlocks() const override { - return bad_blocks_; - } - - boost::optional verificationEngine() const override { - return verification_engine_; - } - - GenesisRawConfig getGenesis() const override { - return genesis_; - } - - private: - outcome::result loadFromJson(const std::string &file_path); - outcome::result loadFields(const boost::property_tree::ptree &tree); - outcome::result loadGenesis(const boost::property_tree::ptree &tree); - outcome::result loadBootNodes( - const boost::property_tree::ptree &tree); - outcome::result loadSessionKeys( - const boost::property_tree::ptree &tree); - - ConfigurationStorageImpl() = default; - - std::string name_; - std::string id_; - std::string chain_type_; - network::PeerList boot_nodes_; - std::vector> telemetry_endpoints_; - std::string protocol_id_{"sup"}; - std::map properties_; - std::set fork_blocks_; - std::set bad_blocks_; - boost::optional verification_engine_; - GenesisRawConfig genesis_; - - base::Logger logger_ = base::createLogger("ConfigurationStorageImpl"); - }; - -} // namespace sgns::application - -#endif // SUPERGENIUS_SRC_CONFIGURATION_STORAGE_IMPL_HPP +namespace sgns::application +{ + + class ConfigurationStorageImpl : public ConfigurationStorage + { + public: + static outcome::result> create( const std::string &config_path ); + + ~ConfigurationStorageImpl() override = default; + + const std::string &name() const override + { + return name_; + } + + const std::string &id() const override + { + return id_; + } + + const std::string &chainType() const override + { + return chain_type_; + } + + network::PeerList getBootNodes() const override + { + return boot_nodes_; + } + + const std::vector> &telemetryEndpoints() const override + { + return telemetry_endpoints_; + } + + const std::string &protocolId() const override + { + return protocol_id_; + } + + const std::map &properties() const override + { + return properties_; + } + + boost::optional> getProperty( const std::string &property ) const override + { + auto it = properties_.find( property ); + if ( it != properties_.end() ) + { + return { { it->second } }; + } + return boost::none; + } + + const std::set &forkBlocks() const override + { + return fork_blocks_; + } + + const std::set &badBlocks() const override + { + return bad_blocks_; + } + + boost::optional verificationEngine() const override + { + return verification_engine_; + } + + GenesisRawConfig getGenesis() const override + { + return genesis_; + } + + std::string GetName() override + { + return "ConfigurationStorageImpl"; + } + + private: + outcome::result loadFromJson( const std::string &file_path ); + outcome::result loadFields( const boost::property_tree::ptree &tree ); + outcome::result loadGenesis( const boost::property_tree::ptree &tree ); + outcome::result loadBootNodes( const boost::property_tree::ptree &tree ); + outcome::result loadSessionKeys( const boost::property_tree::ptree &tree ); + + ConfigurationStorageImpl() = default; + + std::string name_; + std::string id_; + std::string chain_type_; + network::PeerList boot_nodes_; + std::vector> telemetry_endpoints_; + std::string protocol_id_{ "sup" }; + std::map properties_; + std::set fork_blocks_; + std::set bad_blocks_; + boost::optional verification_engine_; + GenesisRawConfig genesis_; + + base::Logger logger_ = base::createLogger( "ConfigurationStorageImpl" ); + }; + +} // namespace sgns::application + +#endif // SUPERGENIUS_SRC_CONFIGURATION_STORAGE_IMPL_HPP diff --git a/src/application/impl/local_key_storage.hpp b/src/application/impl/local_key_storage.hpp index 8d8228b8..679898bf 100644 --- a/src/application/impl/local_key_storage.hpp +++ b/src/application/impl/local_key_storage.hpp @@ -22,6 +22,11 @@ namespace sgns::application { crypto::ED25519Keypair getLocalEd25519Keypair() const override; libp2p::crypto::KeyPair getP2PKeypair() const override; + std::string GetName() override + { + return "LocalKeyStorage"; + } + private: LocalKeyStorage() = default; diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index df4ebb31..5a6c9cda 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -2,6 +2,7 @@ #include "integration/AppStateManagerFactory.hpp" #include "integration/ConfigurationStorageFactory.hpp" #include "integration/KeyStorageFactory.hpp" +#include "integration/SystemClockFactory.hpp" #include "verification/production/impl/production_impl.hpp" #include "verification/finality/impl/finality_impl.hpp" @@ -22,21 +23,40 @@ namespace sgns::application // genesis launch if database does not exist production_execution_strategy_ = boost::filesystem::exists( app_config->rocksdb_path() ) ? Production::ExecutionStrategy::SYNC_FIRST : Production::ExecutionStrategy::GENESIS; + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); - // keep important instances, the must exist when injector destroyed - // some of them are requested by reference and hence not copied - SINGLETONINSTANCE( CComponentFactory ) - ->Register( std::make_shared(), "AppStateManager", boost::none ); - app_state_manager_ = std::dynamic_pointer_cast((SINGLETONINSTANCE( CComponentFactory )->GetComponent( "AppStateManager", boost::none )).value()); - config_storage_ = ConfigurationStorageFactory::create( app_config->genesis_path() ); - key_storage_ = KeyStorageFactory::create( app_config->keystore_path() ); - //clock_ = SystemClockFactory::create(); + component_factory->Register( std::make_shared(), "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 ); + + + auto result = component_factory->GetComponent( "AppStateManager", boost::none ); + if ( result ) + { + app_state_manager_ = std::dynamic_pointer_cast( result.value() ); + } + result = component_factory->GetComponent( "ConfigurationStorage", boost::none ); + if ( result ) + { + config_storage_ = std::dynamic_pointer_cast( result.value() ); + } + result = component_factory->GetComponent( "KeyStorage", boost::none ); + if ( result ) + { + key_storage_ = std::dynamic_pointer_cast( result.value() ); + } + result = component_factory->GetComponent( "SystemClock", boost::none ); + if ( result ) + { + clock_ = std::dynamic_pointer_cast( result.value() ); + } //production_ = std::make_shared(); //finality_ = std::make_shared(); //router_ = std::make_shared(); //jrpc_api_service_ = std::make_shared(); - io_context_ = std::make_shared(); + io_context_ = std::make_shared(); } void ValidatingNodeApplication::run() @@ -49,18 +69,18 @@ namespace sgns::application [this] { // execute listeners - + io_context_->post( [this] { //di::bind.to( [p2p_port{ app_config->p2p_port() }]( const auto &injector ) // { return get_peer_info( injector, p2p_port ); } ), - auto p2p_injector = libp2p::injector::makeHostInjector(); + //auto p2p_injector = libp2p::injector::makeHostInjector(); //auto &key_marshaller = p2p_injector.template create(); //libp2p::peer::PeerId peer_id = libp2p::peer::PeerId::fromPublicKey( key_marshaller.marshal( public_key ).value() ).value(); //auto p2p_info = std::shared_ptr(); //const network::OwnPeerInfo ¤t_peer_info(); - libp2p::Host &host = p2p_injector.template create(); + //libp2p::Host &host = p2p_injector.template create(); //for ( const auto &ma : current_peer_info.addresses ) //{ // auto listen = host.listen( ma ); @@ -84,7 +104,6 @@ namespace sgns::application } ); app_state_manager_->atShutdown( [ctx{ io_context_ }] { ctx->stop(); } ); - app_state_manager_->run(); } diff --git a/src/application/key_storage.hpp b/src/application/key_storage.hpp index 0a69e15a..06b64751 100644 --- a/src/application/key_storage.hpp +++ b/src/application/key_storage.hpp @@ -5,13 +5,14 @@ #include #include "crypto/ed25519_types.hpp" #include "crypto/sr25519_types.hpp" +#include "integration/IComponent.hpp" namespace sgns::application { /** * Stores crypto keys of the current node */ - class KeyStorage { + class KeyStorage : public IComponent { public: virtual ~KeyStorage() = default; diff --git a/src/clock/clock.hpp b/src/clock/clock.hpp index 4ba4ef2c..9034d53f 100644 --- a/src/clock/clock.hpp +++ b/src/clock/clock.hpp @@ -4,6 +4,7 @@ #define SUPERGENIUS_CLOCK_HPP #include +#include "integration/IComponent.hpp" namespace sgns::clock { @@ -12,7 +13,7 @@ namespace sgns::clock { * @tparam clock type is an underlying clock type, such as std::steady_clock */ template - class Clock { + class Clock: public IComponent { public: /** * Difference between two time points diff --git a/src/clock/impl/clock_impl.hpp b/src/clock/impl/clock_impl.hpp index 41f0bbf7..8bb49d00 100644 --- a/src/clock/impl/clock_impl.hpp +++ b/src/clock/impl/clock_impl.hpp @@ -12,6 +12,11 @@ namespace sgns::clock { public: typename Clock::TimePoint now() const override; uint64_t nowUint64() const override; + + std::string GetName() override + { + return "ClockImpl"; + } }; // aliases for implementations From e70b7586e0cbbfc0dff24a80704cf01ca9a264d6 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:37:52 -0300 Subject: [PATCH 09/14] Add: Block header factory [ci skip] --- .../BlockHeaderRepositoryFactory.hpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/integration/BlockHeaderRepositoryFactory.hpp diff --git a/app/integration/BlockHeaderRepositoryFactory.hpp b/app/integration/BlockHeaderRepositoryFactory.hpp new file mode 100644 index 00000000..cd490a6a --- /dev/null +++ b/app/integration/BlockHeaderRepositoryFactory.hpp @@ -0,0 +1,41 @@ + +/** + * @file BlockHeaderRepositoryFactory.hpp + * @brief + * @date 2024-02-23 + * @author Henrique A. Klein (hklein@gnus.ai) + */ +#ifndef _BLOCK_HEADER_REPOSITORY_FACTORY_HPP_ +#define _BLOCK_HEADER_REPOSITORY_FACTORY_HPP_ + +class BlockHeaderRepositoryFactory +{ +public: + static std::shared_ptr create( const std::string &type ) + { + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); + + auto buf_storage = component_factory->GetComponent( "BufferStorage", type ); + + if ( !buf_storage ) + { + throw std::runtime_error( "Initialize BufferStorage first" ); + } + auto hasher = component_factory->GetComponent( "Hasher", boost::none ); + if ( !hasher ) + { + throw std::runtime_error( "Initialize Hasher first" ); + } + auto result = std::make_shared( buf_storage.value(), hasher.value() ); + if ( result ) + { + return result.value(); + } + else + { + throw std::runtime_error( "BlockHeaderRepository not created" ); + } + } +} + +#endif \ No newline at end of file From 87fd9aa488eb5e4e0b7f414525e6634778c2df90 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:39:36 -0300 Subject: [PATCH 10/14] Feat: Adding more components --- app/CMakeLists.txt | 3 +++ .../BlockHeaderRepositoryFactory.hpp | 26 +++++++++---------- app/integration/BufferStorageFactory.hpp | 9 +++++-- app/integration/CComponentFactory.hpp | 6 ++++- app/integration/HasherFactory.hpp | 4 +-- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 1b67ce9a..93cfbb6a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -15,7 +15,10 @@ add_executable (sgns_demo app_config_impl local_key_storage configuration_storage + block_header_repository clock + rocksdb + hasher p2p::p2p_basic_host p2p::p2p_default_network p2p::p2p_peer_repository diff --git a/app/integration/BlockHeaderRepositoryFactory.hpp b/app/integration/BlockHeaderRepositoryFactory.hpp index cd490a6a..b41f44d6 100644 --- a/app/integration/BlockHeaderRepositoryFactory.hpp +++ b/app/integration/BlockHeaderRepositoryFactory.hpp @@ -8,6 +8,9 @@ #ifndef _BLOCK_HEADER_REPOSITORY_FACTORY_HPP_ #define _BLOCK_HEADER_REPOSITORY_FACTORY_HPP_ +#include "blockchain/impl/key_value_block_header_repository.hpp" +#include "integration/CComponentFactory.hpp" + class BlockHeaderRepositoryFactory { public: @@ -15,27 +18,22 @@ class BlockHeaderRepositoryFactory { auto component_factory = SINGLETONINSTANCE( CComponentFactory ); - auto buf_storage = component_factory->GetComponent( "BufferStorage", type ); + auto retval = component_factory->GetComponent( "BufferStorage", type ); - if ( !buf_storage ) + if ( !retval ) { throw std::runtime_error( "Initialize BufferStorage first" ); } - auto hasher = component_factory->GetComponent( "Hasher", boost::none ); - if ( !hasher ) + auto buf_storage = std::dynamic_pointer_cast( retval.value() ); + retval = component_factory->GetComponent( "Hasher", boost::none ); + if ( !retval ) { throw std::runtime_error( "Initialize Hasher first" ); } - auto result = std::make_shared( buf_storage.value(), hasher.value() ); - if ( result ) - { - return result.value(); - } - else - { - throw std::runtime_error( "BlockHeaderRepository not created" ); - } + auto hasher = std::dynamic_pointer_cast( retval.value() ); + + return std::make_shared( buf_storage, hasher ); } -} +}; #endif \ No newline at end of file diff --git a/app/integration/BufferStorageFactory.hpp b/app/integration/BufferStorageFactory.hpp index 6e30c40e..ab0e8b97 100644 --- a/app/integration/BufferStorageFactory.hpp +++ b/app/integration/BufferStorageFactory.hpp @@ -7,6 +7,8 @@ #ifndef _BUFFER_STORAGE_HPP_ #define _BUFFER_STORAGE_HPP_ +#include "storage/rocksdb/rocksdb.hpp" + class BufferStorageFactory { public: @@ -14,7 +16,9 @@ class BufferStorageFactory { if ( type == "rocksdb" ) { - auto result = sgns::storage::rocksdb::create( path ); + auto options = sgns::storage::rocksdb::Options{}; + options.create_if_missing = true; + auto result = sgns::storage::rocksdb::create( path, options ); if ( result ) { return result.value(); @@ -28,7 +32,8 @@ class BufferStorageFactory { //TrieStorageBackend } + throw std::runtime_error( "Invalid BufferStorage type" ); } -} +}; #endif diff --git a/app/integration/CComponentFactory.hpp b/app/integration/CComponentFactory.hpp index 40c78030..665ab430 100644 --- a/app/integration/CComponentFactory.hpp +++ b/app/integration/CComponentFactory.hpp @@ -4,6 +4,8 @@ * @date 2024-02-23 * @author Henrique A. Klein (hklein@gnus.ai) */ +#ifndef _CCOMPONENT_FACTORY_HPP_ +#define _CCOMPONENT_FACTORY_HPP_ #include "IComponentFactory.hpp" #include "Singleton.hpp" @@ -19,4 +21,6 @@ class CComponentFactory : public IComponentFactory void Register( std::shared_ptr component, const std::string &type, const boost::optional &variant ) override; boost::optional> GetComponent( const std::string &type, const boost::optional &variant ) override; -}; \ No newline at end of file +}; + +#endif diff --git a/app/integration/HasherFactory.hpp b/app/integration/HasherFactory.hpp index 84e8bc5c..4dd36bdf 100644 --- a/app/integration/HasherFactory.hpp +++ b/app/integration/HasherFactory.hpp @@ -7,7 +7,7 @@ #ifndef _HASHER_FACTORY_HPP_ #define _HASHER_FACTORY_HPP_ -#include "crrpto/hasher/hasher_impl.hpp" +#include "crypto/hasher/hasher_impl.hpp" class HasherFactory { @@ -16,5 +16,5 @@ class HasherFactory { return std::make_shared(); } -} +}; #endif From 82ce8c23eb59683fba1a4a24b9a16ee96d4fe37f Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:42:05 -0300 Subject: [PATCH 11/14] Fix: Storage base and derived classes inherit component --- src/storage/face/generic_storage.hpp | 4 +++- src/storage/in_memory/in_memory_storage.hpp | 5 +++++ src/storage/rocksdb/rocksdb.hpp | 5 +++++ src/storage/trie/impl/trie_storage_backend_impl.hpp | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/storage/face/generic_storage.hpp b/src/storage/face/generic_storage.hpp index 37e9f455..5c336cc9 100644 --- a/src/storage/face/generic_storage.hpp +++ b/src/storage/face/generic_storage.hpp @@ -4,6 +4,7 @@ #define SUPERGENIUS_GENERIC_STORAGE_HPP #include "storage/face/generic_maps.hpp" +#include "integration/IComponent.hpp" namespace sgns::storage::face { @@ -15,7 +16,8 @@ namespace sgns::storage::face { */ template struct GenericStorage : public ReadOnlyMap, - public BatchWriteMap {}; + public BatchWriteMap, + public IComponent {}; } // namespace sgns::storage::face diff --git a/src/storage/in_memory/in_memory_storage.hpp b/src/storage/in_memory/in_memory_storage.hpp index d4616e03..f9165afe 100644 --- a/src/storage/in_memory/in_memory_storage.hpp +++ b/src/storage/in_memory/in_memory_storage.hpp @@ -45,6 +45,11 @@ namespace sgns::storage { sgns::storage::face::MapCursor> cursor() override; + std::string GetName() override + { + return "InMemoryStorage"; + } + private: std::map storage; }; diff --git a/src/storage/rocksdb/rocksdb.hpp b/src/storage/rocksdb/rocksdb.hpp index 611bf5b2..5dc419f9 100644 --- a/src/storage/rocksdb/rocksdb.hpp +++ b/src/storage/rocksdb/rocksdb.hpp @@ -81,6 +81,11 @@ namespace sgns::storage outcome::result remove(const Buffer &key) override; + std::string GetName() override + { + return "rocksdb"; + } + inline std::shared_ptr getDB() const { return db_; } private: diff --git a/src/storage/trie/impl/trie_storage_backend_impl.hpp b/src/storage/trie/impl/trie_storage_backend_impl.hpp index 9d193c69..6f11a46d 100644 --- a/src/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/src/storage/trie/impl/trie_storage_backend_impl.hpp @@ -27,6 +27,11 @@ namespace sgns::storage::trie { outcome::result put(const Buffer &key, Buffer &&value) override; outcome::result remove(const Buffer &key) override; + std::string GetName() override + { + return "TrieStorageBackendImpl"; + } + private: base::Buffer prefixKey(const base::Buffer &key) const; From c3f66070672d51c875bbc2c8e38fa46377a564e0 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:50:23 -0300 Subject: [PATCH 12/14] Fix: Blockchain header base and derived classes inherit component --- src/blockchain/block_header_repository.hpp | 3 ++- src/blockchain/impl/key_value_block_header_repository.hpp | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/blockchain/block_header_repository.hpp b/src/blockchain/block_header_repository.hpp index 1da72531..0e4a080a 100644 --- a/src/blockchain/block_header_repository.hpp +++ b/src/blockchain/block_header_repository.hpp @@ -9,6 +9,7 @@ #include "base/blob.hpp" #include "primitives/block_header.hpp" #include "primitives/block_id.hpp" +#include "integration/IComponent.hpp" namespace sgns::blockchain { @@ -22,7 +23,7 @@ namespace sgns::blockchain { * convenience methods, such as getting bloch number by its hash and vice * versa or getting a block status */ - class BlockHeaderRepository { + class BlockHeaderRepository : public IComponent { public: virtual ~BlockHeaderRepository() = default; diff --git a/src/blockchain/impl/key_value_block_header_repository.hpp b/src/blockchain/impl/key_value_block_header_repository.hpp index 55931b38..7e5cfe78 100644 --- a/src/blockchain/impl/key_value_block_header_repository.hpp +++ b/src/blockchain/impl/key_value_block_header_repository.hpp @@ -28,7 +28,11 @@ namespace sgns::blockchain { auto getBlockStatus(const primitives::BlockId &id) const -> outcome::result override; - + + std::string GetName() override + { + return "KeyValueBlockHeaderRepository"; + } private: std::shared_ptr map_; std::shared_ptr hasher_; From 0e71fdb054bf7a19df4983adfae87902b267c9d5 Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:52:40 -0300 Subject: [PATCH 13/14] Fix: Hasher base and derived classes inherit component --- src/crypto/hasher.hpp | 3 ++- src/crypto/hasher/hasher_impl.hpp | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/crypto/hasher.hpp b/src/crypto/hasher.hpp index 02ac3469..514cee45 100644 --- a/src/crypto/hasher.hpp +++ b/src/crypto/hasher.hpp @@ -5,9 +5,10 @@ #include "base/blob.hpp" #include "base/buffer.hpp" +#include "integration/IComponent.hpp" namespace sgns::crypto { - class Hasher { + class Hasher : public IComponent { protected: using Hash64 = base::Hash64; using Hash128 = base::Hash128; diff --git a/src/crypto/hasher/hasher_impl.hpp b/src/crypto/hasher/hasher_impl.hpp index 0da4e26e..1f3eb2e4 100644 --- a/src/crypto/hasher/hasher_impl.hpp +++ b/src/crypto/hasher/hasher_impl.hpp @@ -27,6 +27,11 @@ namespace sgns::crypto { Hash256 blake2s_256(gsl::span buffer) const override; Hash256 sha2_256(gsl::span buffer) const override; + + std::string GetName() override + { + return "HasherImpl"; + } }; } // namespace sgns::hash From 6e4cd2d25d03748f02bb4e87b8a12dc3b55da37d Mon Sep 17 00:00:00 2001 From: Henrique A Klein Date: Sat, 24 Feb 2024 17:57:22 -0300 Subject: [PATCH 14/14] Fix: Mocks that are being used inherit component --- src/application/impl/validating_node_application.cpp | 9 ++++++++- .../mock/src/blockchain/block_header_repository_mock.hpp | 2 ++ test/mock/src/crypto/hasher_mock.hpp | 2 ++ test/mock/src/storage/persistent_map_mock.hpp | 2 ++ test/src/storage/trie/trie_storage/trie_batch_test.cpp | 2 ++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/application/impl/validating_node_application.cpp b/src/application/impl/validating_node_application.cpp index 5a6c9cda..521deb48 100644 --- a/src/application/impl/validating_node_application.cpp +++ b/src/application/impl/validating_node_application.cpp @@ -3,6 +3,10 @@ #include "integration/ConfigurationStorageFactory.hpp" #include "integration/KeyStorageFactory.hpp" #include "integration/SystemClockFactory.hpp" +#include "integration/BufferStorageFactory.hpp" +#include "integration/HasherFactory.hpp" +#include "integration/BlockHeaderRepositoryFactory.hpp" + #include "verification/production/impl/production_impl.hpp" #include "verification/finality/impl/finality_impl.hpp" @@ -29,7 +33,10 @@ namespace sgns::application 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 ); - + + component_factory->Register( BufferStorageFactory::create( "rocksdb", app_config->rocksdb_path() ), "BufferStorage", boost::make_optional(std::string("rocksdb")) ); + component_factory->Register( HasherFactory::create(), "Hasher", boost::none ); + component_factory->Register( BlockHeaderRepositoryFactory::create( "rocksdb" ), "BlockHeaderRepository", boost::none ); auto result = component_factory->GetComponent( "AppStateManager", boost::none ); if ( result ) diff --git a/test/mock/src/blockchain/block_header_repository_mock.hpp b/test/mock/src/blockchain/block_header_repository_mock.hpp index 4875cb76..4edfee4c 100644 --- a/test/mock/src/blockchain/block_header_repository_mock.hpp +++ b/test/mock/src/blockchain/block_header_repository_mock.hpp @@ -21,6 +21,8 @@ namespace sgns::blockchain { const primitives::BlockId &id)); MOCK_CONST_METHOD1(getNumberById, outcome::result ( const primitives::BlockId &id)); + + MOCK_METHOD0(GetName, std::string()); }; } // namespace sgns::blockchain diff --git a/test/mock/src/crypto/hasher_mock.hpp b/test/mock/src/crypto/hasher_mock.hpp index 61317161..d283a98e 100644 --- a/test/mock/src/crypto/hasher_mock.hpp +++ b/test/mock/src/crypto/hasher_mock.hpp @@ -27,6 +27,8 @@ namespace sgns::crypto { MOCK_CONST_METHOD1(keccak_256, Hash256(gsl::span)); MOCK_CONST_METHOD1(sha2_256, Hash256(gsl::span)); + + MOCK_METHOD0(GetName, std::string()); }; } // namespace sgns::crypto diff --git a/test/mock/src/storage/persistent_map_mock.hpp b/test/mock/src/storage/persistent_map_mock.hpp index 8555ce04..40dd7d5f 100644 --- a/test/mock/src/storage/persistent_map_mock.hpp +++ b/test/mock/src/storage/persistent_map_mock.hpp @@ -29,6 +29,8 @@ namespace sgns::storage::face { MOCK_METHOD2_T(put_rv, outcome::result(const K &, V)); MOCK_METHOD1_T(remove, outcome::result(const K &)); + + MOCK_METHOD0(GetName, std::string()); //-------------------- // friend std::ostream &operator<<(std::ostream &out, const GenericStorageMock &test_struct) // { diff --git a/test/src/storage/trie/trie_storage/trie_batch_test.cpp b/test/src/storage/trie/trie_storage/trie_batch_test.cpp index 0c56867e..5cdc3c90 100644 --- a/test/src/storage/trie/trie_storage/trie_batch_test.cpp +++ b/test/src/storage/trie/trie_storage/trie_batch_test.cpp @@ -75,6 +75,8 @@ class MockDb : public sgns::storage::InMemoryStorage { public: MOCK_METHOD2(put, outcome::result(const Buffer &, const Buffer &)); + MOCK_METHOD0(GetName, std::string()); + // to retain the ability to call the actual implementation of put from the // superclass outcome::result true_put(const Buffer &key, const Buffer &value) {