diff --git a/.clang-tidy b/.clang-tidy index d11ccf10..a64b6054 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -14,6 +14,7 @@ Checks: > cppcoreguidelines-*, -cppcoreguidelines-avoid-do-while, + -cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-non-private-member-variables-in-classes, -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-reinterpret-cast, @@ -39,6 +40,7 @@ Checks: > readability-*, -readability-identifier-length, + -readability-magic-numbers, -readability-named-parameter FormatStyle: file diff --git a/example/processing_dapp/processing_dapp.cpp b/example/processing_dapp/processing_dapp.cpp index e8a52f65..3d78141c 100644 --- a/example/processing_dapp/processing_dapp.cpp +++ b/example/processing_dapp/processing_dapp.cpp @@ -206,7 +206,7 @@ int main(int argc, char* argv[]) std::vector pubsubBootstrapPeers; if (options->remote) { - pubsubBootstrapPeers = std::move(std::vector({ *options->remote })); + pubsubBootstrapPeers = std::vector( { *options->remote } ); } pubs->Start(40001, pubsubBootstrapPeers); diff --git a/src/account/AccountManager.cpp b/src/account/AccountManager.cpp index 5a5afe4b..c21ed574 100644 --- a/src/account/AccountManager.cpp +++ b/src/account/AccountManager.cpp @@ -5,7 +5,6 @@ * @author Henrique A. Klein (hklein@gnus.ai) */ #include "account/AccountManager.hpp" -#include "processing/processing_imagesplit.hpp" namespace sgns { diff --git a/src/account/AccountManager.hpp b/src/account/AccountManager.hpp index 2e65840e..0732a07f 100644 --- a/src/account/AccountManager.hpp +++ b/src/account/AccountManager.hpp @@ -18,7 +18,6 @@ #include "ipfs_pubsub/gossip_pubsub.hpp" #include "crdt/globaldb/globaldb.hpp" #include "crypto/hasher/hasher_impl.hpp" -#include "blockchain/impl/common.hpp" #include "blockchain/impl/key_value_block_header_repository.hpp" #include "blockchain/impl/key_value_block_storage.hpp" #include "integration/IComponent.hpp" diff --git a/src/account/GeniusAccount.hpp b/src/account/GeniusAccount.hpp index 7ab31c75..d453ec65 100644 --- a/src/account/GeniusAccount.hpp +++ b/src/account/GeniusAccount.hpp @@ -7,7 +7,6 @@ #ifndef _GENIUS_ACCOUNT_HPP_ #define _GENIUS_ACCOUNT_HPP_ #include -#include #include #include @@ -29,28 +28,30 @@ namespace sgns template const T GetAddress() const; - template<> - const std::string GetAddress() const + template <> [[nodiscard]] const std::string GetAddress() const { std::ostringstream oss; oss << std::hex << address; return ( "0x" + oss.str() ); } - template<> - const uint256_t GetAddress() const + + template <> [[nodiscard]] const uint256_t GetAddress() const { return address; } - const std::string GetBalance() const + + [[nodiscard]] std::string GetBalance() const { return std::to_string(balance); } - const std::string GetToken() const + + [[nodiscard]] std::string GetToken() const { return "GNUS Token"; } - const std::string GetNonce() const + + [[nodiscard]] std::string GetNonce() const { return std::to_string(nonce); } diff --git a/src/account/IGeniusTransactions.hpp b/src/account/IGeniusTransactions.hpp index 8ddd7ede..ff2b7b2d 100644 --- a/src/account/IGeniusTransactions.hpp +++ b/src/account/IGeniusTransactions.hpp @@ -7,6 +7,7 @@ #ifndef _IGENIUS_TRANSACTIONS_HPP_ #define _IGENIUS_TRANSACTIONS_HPP_ +#include #include #include #include @@ -23,13 +24,14 @@ namespace sgns class IGeniusTransactions { public: - IGeniusTransactions( const std::string &type, const SGTransaction::DAGStruct &dag ) : - transaction_type( type ), // - dag_st( dag ) // + IGeniusTransactions( std::string type, const SGTransaction::DAGStruct &dag ) : + dag_st( dag ), transaction_type( std::move( type ) ) { } + virtual ~IGeniusTransactions() = default; - const std::string GetType() const + + [[nodiscard]] std::string GetType() const { return transaction_type; } @@ -66,21 +68,18 @@ namespace sgns return full_path.str(); } - template - const T GetSrcAddress() const; + template const T GetSrcAddress() const; - template <> - const std::string GetSrcAddress() const + template <> const std::string GetSrcAddress() const { - //std::string address(bytes_data.begin(), bytes_data.end()); //std::ostringstream oss; //oss << std::hex << src_address; return dag_st.source_addr(); } - template <> - const uint256_t GetSrcAddress() const + + template <> const uint256_t GetSrcAddress() const { return uint256_t{ dag_st.source_addr() }; } diff --git a/src/account/MintTransaction.cpp b/src/account/MintTransaction.cpp index b6873386..b4aae2a9 100644 --- a/src/account/MintTransaction.cpp +++ b/src/account/MintTransaction.cpp @@ -14,9 +14,10 @@ namespace sgns amount( new_amount ) // { auto hasher_ = std::make_shared(); - auto hash = hasher_->blake2b_256(SerializeByteVector()); - dag_st.set_data_hash(hash.toReadableString()); + auto hash = hasher_->blake2b_256( SerializeByteVector() ); + dag_st.set_data_hash( hash.toReadableString() ); } + std::vector MintTransaction::SerializeByteVector() { SGTransaction::MintTx tx_struct; @@ -28,9 +29,9 @@ namespace sgns tx_struct.SerializeToArray( serialized_proto.data(), serialized_proto.size() ); return serialized_proto; } + MintTransaction MintTransaction::DeSerializeByteVector( const std::vector &data ) { - SGTransaction::MintTx tx_struct; if ( !tx_struct.ParseFromArray( data.data(), data.size() ) ) { @@ -39,10 +40,11 @@ namespace sgns uint64_t v64 = tx_struct.amount(); //std::memcpy( &v64, &( *data.begin() ), sizeof( v64 ) ); - return MintTransaction( v64, tx_struct.dag_struct() ); // Return new instance + return { v64, tx_struct.dag_struct() }; // Return new instance } + uint64_t MintTransaction::GetAmount() const { return amount; } -} \ No newline at end of file +} diff --git a/src/account/MintTransaction.hpp b/src/account/MintTransaction.hpp index d6faa21e..e8ecd7ca 100644 --- a/src/account/MintTransaction.hpp +++ b/src/account/MintTransaction.hpp @@ -6,6 +6,7 @@ */ #ifndef _MINT_TRANSACTION_HPP_ #define _MINT_TRANSACTION_HPP_ + #include #include #include "account/IGeniusTransactions.hpp" diff --git a/src/account/ProcessingTransaction.cpp b/src/account/ProcessingTransaction.cpp index 0981542a..60c1eac0 100644 --- a/src/account/ProcessingTransaction.cpp +++ b/src/account/ProcessingTransaction.cpp @@ -6,13 +6,15 @@ */ #include "account/ProcessingTransaction.hpp" + +#include #include "crypto/hasher/hasher_impl.hpp" namespace sgns { - ProcessingTransaction::ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag) : - IGeniusTransactions( "processing", SetDAGWithType(dag,"processing")), // - hash_process_data( hash ) // + ProcessingTransaction::ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag ) : + IGeniusTransactions( "processing", SetDAGWithType( dag, "processing" ) ), // + hash_process_data( std::move( hash ) ) { auto hasher_ = std::make_shared(); auto hash_data = hasher_->blake2b_256(SerializeByteVector()); @@ -32,6 +34,6 @@ namespace sgns uint256_t hash; import_bits( hash, data.begin(), data.end() ); - return ProcessingTransaction( hash, {} ); // Return new instance + return { hash, {} }; // Return new instance } } \ No newline at end of file diff --git a/src/account/ProcessingTransaction.hpp b/src/account/ProcessingTransaction.hpp index 9b18e711..e7999c36 100644 --- a/src/account/ProcessingTransaction.hpp +++ b/src/account/ProcessingTransaction.hpp @@ -11,6 +11,7 @@ #include using namespace boost::multiprecision; + namespace sgns { @@ -18,7 +19,7 @@ namespace sgns { public: ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag ); - ~ProcessingTransaction() = default; + ~ProcessingTransaction() override = default; std::vector SerializeByteVector() override; static ProcessingTransaction DeSerializeByteVector( const std::vector &data ); diff --git a/src/account/TransactionManager.cpp b/src/account/TransactionManager.cpp index da9d1982..05b6de66 100644 --- a/src/account/TransactionManager.cpp +++ b/src/account/TransactionManager.cpp @@ -11,16 +11,17 @@ namespace sgns { - TransactionManager::TransactionManager( std::shared_ptr db, std::shared_ptr ctx, + TransactionManager::TransactionManager( std::shared_ptr db, + std::shared_ptr ctx, std::shared_ptr account, std::shared_ptr block_storage ) : db_m( std::move( db ) ), // ctx_m( std::move( ctx ) ), // account_m( std::move( account ) ), // - block_storage_m( std::move( block_storage ) ), // timer_m( std::make_shared( *ctx_m, boost::asio::chrono::milliseconds( 300 ) ) ), // last_block_id_m( 0 ), // - last_trans_on_block_id( 0 ) + last_trans_on_block_id( 0 ), // + block_storage_m( std::move( block_storage ) ) { m_logger->set_level( spdlog::level::debug ); @@ -39,7 +40,8 @@ namespace sgns this->Update(); this->timer_m->expires_after( boost::asio::chrono::milliseconds( 300 ) ); - this->timer_m->async_wait( [this, task]( const boost::system::error_code & ) { this->ctx_m->post( *task ); } ); + this->timer_m->async_wait( [this, task]( const boost::system::error_code & ) + { this->ctx_m->post( *task ); } ); }; ctx_m->post( *task ); } @@ -56,16 +58,19 @@ namespace sgns { return *account_m; } + void TransactionManager::TransferFunds( const uint256_t &amount, const uint256_t &destination ) { auto transfer_transaction = std::make_shared( amount, destination, FillDAGStruct() ); this->EnqueueTransaction( transfer_transaction ); } + void TransactionManager::MintFunds( const uint64_t &amount ) { auto mint_transaction = std::make_shared( amount, FillDAGStruct() ); this->EnqueueTransaction( mint_transaction ); } + void TransactionManager::HoldEscrow( const uint64_t &amount ) { auto mint_transaction = std::make_shared( amount, FillDAGStruct() ); @@ -77,11 +82,13 @@ namespace sgns SendTransaction(); CheckBlockchain(); } + void TransactionManager::EnqueueTransaction( std::shared_ptr element ) { std::lock_guard lock( mutex_m ); out_transactions.emplace_back( std::move( element ) ); } + //TODO - Fill hash stuff on DAGStruct SGTransaction::DAGStruct TransactionManager::FillDAGStruct() { @@ -102,7 +109,6 @@ namespace sgns std::unique_lock lock( mutex_m ); if ( !out_transactions.empty() ) { - auto elem = out_transactions.front(); out_transactions.pop_front(); boost::format tx_key{ std::string( TRANSACTION_BASE_FORMAT ) }; @@ -136,12 +142,14 @@ namespace sgns block_storage_m->putBlockData( header.number, block_data ); - m_logger->debug( "Putting on " + transaction_path + " the data: " + std::string( data_transaction.toString() ) ); + m_logger->debug( "Putting on " + transaction_path + + " the data: " + std::string( data_transaction.toString() ) ); m_logger->debug( "Recording Block with number " + std::to_string( header.number ) ); block_storage_m->setLastFinalizedBlockHash( new_hash.value() ); } lock.unlock(); // Manual unlock, no need to wait to run out of scope } + bool TransactionManager::GetTransactionsFromBlock( const primitives::BlockNumber &block_number ) { outcome::result retval = outcome::failure( boost::system::error_code{} ); @@ -179,6 +187,7 @@ namespace sgns //while ( !retval ); return ret; } + void TransactionManager::ParseTransaction( std::string transaction_key ) { auto maybe_transaction_data = db_m->Get( { transaction_key } ); @@ -196,7 +205,8 @@ namespace sgns if ( maybe_dag.value().type() == "transfer" ) { m_logger->info( "Transfer transaction" ); - TransferTransaction tx = TransferTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); + TransferTransaction tx = + TransferTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); if ( tx.GetDstAddress() == account_m->GetAddress() ) { account_m->balance += static_cast( tx.GetAmount() ); @@ -211,7 +221,8 @@ namespace sgns else if ( maybe_dag.value().type() == "mint" ) { m_logger->info( "Mint transaction" ); - MintTransaction tx = MintTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); + MintTransaction tx = + MintTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); //std::cout << tx.GetAddress() << std::endl; if ( tx.GetSrcAddress() == account_m->GetAddress() ) @@ -226,7 +237,8 @@ namespace sgns else if ( maybe_dag.value().type() == "process" ) { m_logger->info( "Process transaction" ); - ProcessingTransaction tx = ProcessingTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); + ProcessingTransaction tx = + ProcessingTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() ); //std::cout << tx.GetAddress() << std::endl; /*if ( tx.GetSrcAddress() == account_m->GetAddress() ) @@ -277,4 +289,4 @@ namespace sgns } while ( retval ); } -} \ No newline at end of file +} diff --git a/src/application/app_config.hpp b/src/application/app_config.hpp index 6b22e4c1..18212e34 100644 --- a/src/application/app_config.hpp +++ b/src/application/app_config.hpp @@ -27,44 +27,44 @@ namespace sgns::application virtual ~AppConfiguration() = default; /** - * @return file path with genesis configuration. - */ - virtual const std::string &genesis_path() const = 0; + * @return file path with genesis configuration. + */ + [[nodiscard]] virtual const std::string &genesis_path() const = 0; /** - * @return keystore directory path. - */ - virtual const std::string &keystore_path() const = 0; + * @return keystore directory path. + */ + [[nodiscard]] virtual const std::string &keystore_path() const = 0; /** - * @return rocksdb directory path. - */ - virtual const std::string &rocksdb_path() const = 0; + * @return rocksdb directory path. + */ + [[nodiscard]] virtual const std::string &rocksdb_path() const = 0; /** - * @return port for peer to peer interactions. - */ - virtual uint16_t p2p_port() const = 0; + * @return port for peer to peer interactions. + */ + [[nodiscard]] 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; + * @return endpoint for RPC over HTTP. + */ + [[nodiscard]] 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; + * @return endpoint for RPC over Websocket protocol. + */ + [[nodiscard]] 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; + * @return log level (0-trace, 5-only critical, 6-no logs). + */ + [[nodiscard]] 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; + * @return true if node in only finalizing mode, otherwise false. + */ + [[nodiscard]] virtual bool is_only_finalizing() const = 0; virtual bool initialize_from_args( LoadScheme scheme, int argc, char **argv ) = 0; }; diff --git a/src/application/app_state_manager.hpp b/src/application/app_state_manager.hpp index 3fc9f8f4..28c10808 100644 --- a/src/application/app_state_manager.hpp +++ b/src/application/app_state_manager.hpp @@ -1,8 +1,7 @@ - #ifndef SUPERGENIUS_APPLICATION_DISPATCHER #define SUPERGENIUS_APPLICATION_DISPATCHER -#include "base/logger.hpp" +#include #include "integration/IComponent.hpp" namespace sgns::application { @@ -76,9 +75,9 @@ namespace sgns::application { virtual void shutdown() = 0; /// Get current stage - virtual State state() const = 0; + [[nodiscard]] virtual State state() const = 0; - protected: +protected: virtual void doPrepare() = 0; virtual void doLaunch() = 0; virtual void doShutdown() = 0; diff --git a/src/application/configuration_storage.hpp b/src/application/configuration_storage.hpp index 55717c26..8f58281e 100644 --- a/src/application/configuration_storage.hpp +++ b/src/application/configuration_storage.hpp @@ -3,12 +3,10 @@ #include #include "application/genesis_raw_config.hpp" -#include "crypto/ed25519_types.hpp" -#include "crypto/sr25519_types.hpp" #include "network/types/peer_list.hpp" -#include "primitives/block.hpp" #include #include "integration/IComponent.hpp" +#include "primitives/common.hpp" namespace sgns::application { /** @@ -19,35 +17,34 @@ namespace sgns::application { public: virtual ~ConfigurationStorage() = default; - virtual const std::string &name() const = 0; + [[nodiscard]] virtual const std::string &name() const = 0; - virtual const std::string &id() const = 0; + [[nodiscard]] virtual const std::string &id() const = 0; - virtual const std::string &chainType() const = 0; + [[nodiscard]] virtual const std::string &chainType() const = 0; /// Return ids of peer nodes of the current node - virtual network::PeerList getBootNodes() const = 0; + [[nodiscard]] virtual network::PeerList getBootNodes() const = 0; - virtual const std::vector> - &telemetryEndpoints() const = 0; + [[nodiscard]] virtual const std::vector> &telemetryEndpoints() const = 0; - virtual const std::string &protocolId() const = 0; + [[nodiscard]] virtual const std::string &protocolId() const = 0; - virtual const std::map &properties() const = 0; + [[nodiscard]] virtual const std::map &properties() const = 0; - virtual boost::optional> - getProperty(const std::string &property) const = 0; + [[nodiscard]] virtual boost::optional> getProperty( + const std::string &property ) const = 0; - virtual const std::set &forkBlocks() const = 0; + [[nodiscard]] virtual const std::set &forkBlocks() const = 0; - virtual const std::set &badBlocks() const = 0; + [[nodiscard]] virtual const std::set &badBlocks() const = 0; - virtual boost::optional verificationEngine() const = 0; + [[nodiscard]] virtual boost::optional verificationEngine() const = 0; /** * @return genesis block of the chain */ - virtual GenesisRawConfig getGenesis() const = 0; + [[nodiscard]] virtual GenesisRawConfig getGenesis() const = 0; }; } // namespace sgns::application diff --git a/src/application/impl/app_config_impl.cpp b/src/application/impl/app_config_impl.cpp index 49fa2c1a..b2d0df36 100644 --- a/src/application/impl/app_config_impl.cpp +++ b/src/application/impl/app_config_impl.cpp @@ -14,7 +14,10 @@ namespace { Func &&f) { assert(nullptr != name); auto it = vm.find(name); - if (it != vm.end()) std::forward(f)(it->second.as()); + if ( it != vm.end() ) + { + std::forward( f )( it->second.as() ); + } } const std::string def_rpc_http_host = "0.0.0.0"; @@ -41,8 +44,7 @@ namespace sgns::application { AppConfigurationImpl::FilePtr AppConfigurationImpl::open_file( const std::string &filepath) { assert(!filepath.empty()); - return AppConfigurationImpl::FilePtr(std::fopen(filepath.c_str(), "r"), - &std::fclose); + return { std::fopen( filepath.c_str(), "r" ), &std::fclose }; } bool AppConfigurationImpl::load_str(const rapidjson::Value &val, @@ -277,8 +279,10 @@ namespace sgns::application { }); /// aggregate data from command line args - if (vm.end() != vm.find("single_finalizing_node")) - is_only_finalizing_ = true; + if ( vm.end() != vm.find( "single_finalizing_node" ) ) + { + is_only_finalizing_ = true; + } find_argument( vm, "genesis", [&](std::string const &val) { genesis_path_ = val; }); @@ -292,10 +296,14 @@ namespace sgns::application { find_argument( vm, "p2p_port", [&](uint16_t val) { p2p_port_ = val; }); - find_argument(vm, "verbosity", [&](int32_t val) { - if (val >= SPDLOG_LEVEL_TRACE && val <= SPDLOG_LEVEL_OFF) - verbosity_ = static_cast(val); - }); + find_argument( vm, "verbosity", + [&]( int32_t val ) + { + if ( val >= SPDLOG_LEVEL_TRACE && val <= SPDLOG_LEVEL_OFF ) + { + verbosity_ = static_cast( val ); + } + } ); find_argument( vm, "rpc_http_host", [&](std::string const &val) { diff --git a/src/application/impl/app_config_impl.hpp b/src/application/impl/app_config_impl.hpp index 9fd57a2f..dc18c526 100644 --- a/src/application/impl/app_config_impl.hpp +++ b/src/application/impl/app_config_impl.hpp @@ -1,5 +1,3 @@ - - #ifndef SUPERGENIUS_APP_CONFIG_IMPL_HPP #define SUPERGENIUS_APP_CONFIG_IMPL_HPP @@ -66,7 +64,6 @@ namespace sgns::application { }; // clang-format on - private: sgns::base::Logger logger_; std::string rpc_http_host_; std::string rpc_ws_host_; @@ -83,7 +80,6 @@ namespace sgns::application { */ // clang-format on - private: void validate_config(AppConfiguration::LoadScheme scheme); void read_config_from_file(const std::string &filepath); diff --git a/src/application/impl/app_state_manager_impl.cpp b/src/application/impl/app_state_manager_impl.cpp index 0a83ded3..669c4316 100644 --- a/src/application/impl/app_state_manager_impl.cpp +++ b/src/application/impl/app_state_manager_impl.cpp @@ -52,9 +52,18 @@ namespace sgns::application { void AppStateManagerImpl::reset() { std::lock_guard lg(mutex_); - while (!prepare_.empty()) prepare_.pop(); - while (!launch_.empty()) launch_.pop(); - while (!shutdown_.empty()) shutdown_.pop(); + while ( !prepare_.empty() ) + { + prepare_.pop(); + } + while ( !launch_.empty() ) + { + launch_.pop(); + } + while ( !shutdown_.empty() ) + { + shutdown_.pop(); + } state_ = State::Init; shutdown_requested_ = false; } @@ -139,9 +148,15 @@ namespace sgns::application { return; } - while (!prepare_.empty()) prepare_.pop(); + while ( !prepare_.empty() ) + { + prepare_.pop(); + } - while (!launch_.empty()) launch_.pop(); + while ( !launch_.empty() ) + { + launch_.pop(); + } state_ = State::ShuttingDown; diff --git a/src/application/impl/block_producing_node_application.cpp b/src/application/impl/block_producing_node_application.cpp index 9b222059..443f4702 100644 --- a/src/application/impl/block_producing_node_application.cpp +++ b/src/application/impl/block_producing_node_application.cpp @@ -1,62 +1,69 @@ #include "application/impl/block_producing_node_application.hpp" -namespace sgns::application { - - BlockProducingNodeApplication::BlockProducingNodeApplication( - const AppConfigPtr &app_config) - : injector_{injector::makeBlockProducingNodeInjector(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>(); - - io_context_ = injector_.create>(); - config_storage_ = injector_.create>(); - key_storage_ = injector_.create>(); - clock_ = injector_.create>(); - production_ = injector_.create>(); - router_ = injector_.create>(); - - jrpc_api_service_ = injector_.create>(); - } - - void BlockProducingNodeApplication::run() { - logger_->info("Start as {} with PID {}", "sgns_node.exe", getpid()); - - production_->setExecutionStrategy(Production::ExecutionStrategy::SYNC_FIRST); - - 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*/ libp2p::multi::Multiaddress &ma : current_peer_info.addresses) { - /*auto*/outcome::result 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_->atShutdown([ctx{io_context_}] { ctx->stop(); }); - - app_state_manager_->run(); - } - -} // namespace sgns::application +namespace sgns::application +{ + BlockProducingNodeApplication::BlockProducingNodeApplication( const AppConfigPtr &app_config ) : + injector_{ injector::makeBlockProducingNodeInjector( 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>(); + + io_context_ = injector_.create>(); + config_storage_ = injector_.create>(); + key_storage_ = injector_.create>(); + clock_ = injector_.create>(); + production_ = injector_.create>(); + router_ = injector_.create>(); + + jrpc_api_service_ = injector_.create>(); + } + + void BlockProducingNodeApplication::run() + { + logger_->info( "Start as {} with PID {}", "sgns_node.exe", getpid() ); + + production_->setExecutionStrategy( Production::ExecutionStrategy::SYNC_FIRST ); + + 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*/ libp2p::multi::Multiaddress &ma : current_peer_info.addresses ) + { + /*auto*/ outcome::result 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_->atShutdown( [ctx{ io_context_ }] { ctx->stop(); } ); + + app_state_manager_->run(); + } + +} // namespace sgns::application diff --git a/src/application/key_storage.hpp b/src/application/key_storage.hpp index 06b64751..2efa0e14 100644 --- a/src/application/key_storage.hpp +++ b/src/application/key_storage.hpp @@ -19,17 +19,17 @@ namespace sgns::application { /** * Get the node sr25519 keypair, which is used, for example, in PRODUCTION */ - virtual crypto::SR25519Keypair getLocalSr25519Keypair() const = 0; + [[nodiscard]] virtual crypto::SR25519Keypair getLocalSr25519Keypair() const = 0; /** * Get the node ed25519 keypair used in finality of verificaiton */ - virtual crypto::ED25519Keypair getLocalEd25519Keypair() const = 0; + [[nodiscard]] virtual crypto::ED25519Keypair getLocalEd25519Keypair() const = 0; /** * Get the node libp2p keypair, which is used by libp2p network library */ - virtual libp2p::crypto::KeyPair getP2PKeypair() const = 0; + [[nodiscard]] virtual libp2p::crypto::KeyPair getP2PKeypair() const = 0; }; } diff --git a/src/base/hexutil.hpp b/src/base/hexutil.hpp index 50aedb44..66aa1829 100644 --- a/src/base/hexutil.hpp +++ b/src/base/hexutil.hpp @@ -1,5 +1,3 @@ - - #ifndef SUPERGENIUS_HEXUTIL_HPP #define SUPERGENIUS_HEXUTIL_HPP @@ -80,10 +78,10 @@ namespace sgns::base { return UnhexError::VALUE_OUT_OF_RANGE; } - T result{0u}; + T result{ 0U }; for (auto b : bytes) { // check if `multiply by 10` will cause overflow - result <<= 8u; + result <<= 8U; result += b; } diff --git a/src/verification/authority/impl/authority_manager_impl.hpp b/src/verification/authority/impl/authority_manager_impl.hpp index 9f01fb22..48f86177 100644 --- a/src/verification/authority/impl/authority_manager_impl.hpp +++ b/src/verification/authority/impl/authority_manager_impl.hpp @@ -2,6 +2,7 @@ #ifndef SUPERGENIUS_VERIFICATION_AUTHORITIES_MANAGER_IMPL #define SUPERGENIUS_VERIFICATION_AUTHORITIES_MANAGER_IMPL +#include "base/logger.hpp" #include "verification/authority/authority_manager.hpp" #include "verification/authority/authority_update_observer.hpp" #include "verification/finality/finalization_observer.hpp"