diff --git a/app/integration/ApiServiceFactory.hpp b/app/integration/ApiServiceFactory.hpp index 5a4c3560..5147a423 100644 --- a/app/integration/ApiServiceFactory.hpp +++ b/app/integration/ApiServiceFactory.hpp @@ -114,62 +114,4 @@ namespace sgns }; } -/* - - ApiService( - const std::shared_ptr &app_state_manager, - std::shared_ptr thread_pool, - std::vector> listeners, - std::shared_ptr server, - const std::vector> &processors, - SubscriptionEnginePtr subscription_engine); - - template - sptr get_jrpc_api_service(const Injector &injector) { - static auto initialized = - boost::optional>(boost::none); - if (initialized) { - return initialized.value(); - } - using SubscriptionEnginePtr = std::shared_ptr< - subscription::SubscriptionEngine, - base::Buffer, - primitives::BlockHash>>; - auto subscription_engine = - injector.template create(); - auto app_state_manager = - injector - .template create>(); - auto rpc_thread_pool = - injector.template create>(); - std::vector> listeners{ - injector.template create>(), - injector.template create>(), - }; - auto server = injector.template create>(); - std::vector> processors{ - injector - .template create>(), - injector.template create< - std::shared_ptr>(), - injector - .template create>(), - injector.template create< - std::shared_ptr>()}; - - initialized = - std::make_shared(std::move(app_state_manager), - std::move(rpc_thread_pool), - std::move(listeners), - std::move(server), - processors, - std::move(subscription_engine)); - - auto state_api = injector.template create>(); - state_api->setApiService(initialized.value()); - return initialized.value(); - } -*/ - #endif diff --git a/app/integration/BlockStorageFactory.hpp b/app/integration/BlockStorageFactory.hpp index 2695a55a..28aa6824 100644 --- a/app/integration/BlockStorageFactory.hpp +++ b/app/integration/BlockStorageFactory.hpp @@ -11,84 +11,97 @@ #include "integration/CComponentFactory.hpp" #include "storage/trie/trie_storage.hpp" #include "blockchain/impl/key_value_block_storage.hpp" - -class BlockStorageFactory +#include "application/key_storage.hpp" +#include "verification/finality/voter_set.hpp" +#include "primitives/authority.hpp" +namespace sgns { - -public: - static std::shared_ptr create() + class BlockStorageFactory { - auto component_factory = SINGLETONINSTANCE( CComponentFactory ); - auto maybe_hasher = component_factory->GetComponent( "Hasher", boost::none ); - if ( !maybe_hasher ) + public: + std::shared_ptr create() { - throw std::runtime_error( "Initialize Hasher first" ); - } - auto hasher = std::dynamic_pointer_cast(maybe_hasher.value()); - - auto maybe_buff_storage = component_factory->GetComponent( "BufferStorage", boost::make_optional(std::string("rocksdb")) ); + auto component_factory = SINGLETONINSTANCE( CComponentFactory ); + auto maybe_hasher = component_factory->GetComponent( "Hasher", boost::none ); - if ( !maybe_buff_storage ) - { - throw std::runtime_error( "Initialize BufferStorage first" ); - } - auto buff_storage = std::dynamic_pointer_cast(maybe_buff_storage.value()); - - auto maybe_trie_storage = component_factory->GetComponent( "TrieStorage", boost::none ); + if ( !maybe_hasher ) + { + throw std::runtime_error( "Initialize Hasher first" ); + } + auto hasher = std::dynamic_pointer_cast( maybe_hasher.value() ); - if ( !maybe_trie_storage ) - { - throw std::runtime_error( "Initialize TrieStorage first" ); - } - auto trie_storage = std::dynamic_pointer_cast(maybe_trie_storage.value()); - + auto maybe_buff_storage = component_factory->GetComponent( "BufferStorage", boost::make_optional( std::string( "rocksdb" ) ) ); - //TODO - Fix finalityAPI dependence here! - auto maybe_storage = sgns::blockchain::KeyValueBlockStorage::create( // - trie_storage->getRootHash(), // - buff_storage, - hasher, // - []( const sgns::primitives::Block &genesis_block ){} - /**[buf_storage, &injector]( const primitives::Block &genesis_block ) + if ( !maybe_buff_storage ) { - // handle genesis initialization, which happens when there is not - // authorities and last completed round in the storage - if ( !buf_storage->get( storage::kAuthoritySetKey ) ) - { - // insert authorities - auto finality_api = injector.template create>(); - const auto &weighted_authorities_res = finality_api->authorities( primitives::BlockId( primitives::BlockNumber{ 0 } ) ); - BOOST_ASSERT_MSG( weighted_authorities_res, "finality_api_->authorities failed" ); - const auto &weighted_authorities = weighted_authorities_res.value(); + throw std::runtime_error( "Initialize BufferStorage first" ); + } + auto buff_storage = std::dynamic_pointer_cast( maybe_buff_storage.value() ); - for ( const auto authority : weighted_authorities ) - { - spdlog::info( "Finality authority: {}", authority.id.id.toHex() ); - } + auto maybe_trie_storage = component_factory->GetComponent( "TrieStorage", boost::none ); - verification::finality::VoterSet voters{ 0 }; - for ( const auto &weighted_authority : weighted_authorities ) - { - voters.insert( weighted_authority.id.id, weighted_authority.weight ); - spdlog::debug( "Added to finality authorities: {}, weight: {}", weighted_authority.id.id.toHex(), weighted_authority.weight ); - } - BOOST_ASSERT_MSG( voters.size() != 0, "Finality voters are empty" ); - auto authorities_put_res = db->put( storage::kAuthoritySetKey, base::Buffer( scale::encode( voters ).value() ) ); - if ( !authorities_put_res ) + if ( !maybe_trie_storage ) + { + throw std::runtime_error( "Initialize TrieStorage first" ); + } + auto trie_storage = std::dynamic_pointer_cast( maybe_trie_storage.value() ); + + auto result = component_factory->GetComponent( "KeyStorage", boost::none ); + if ( !result ) + { + throw std::runtime_error( "Initialize KeyStorage first" ); + } + auto key_storage = std::dynamic_pointer_cast( result.value() ); + + //TODO - Fix finalityAPI dependence here! + auto maybe_storage = blockchain::KeyValueBlockStorage::create( // + trie_storage->getRootHash(), // + buff_storage, + hasher, // + [buff_storage, key_storage]( const primitives::Block &genesis_block ) + { + // handle genesis initialization, which happens when there is not + // authorities and last completed round in the storage + if ( !buff_storage->get( storage::kAuthoritySetKey ) ) { - BOOST_ASSERT_MSG( false, "Could not insert authorities" ); - std::exit( 1 ); + // insert authorities + // TODO - Insert authorities here from finalityAPI + primitives::AuthorityList weighted_authorities{ { { key_storage->getLocalEd25519Keypair().public_key }, 1 } }; + //auto finality_api = injector.template create>(); + //const auto &weighted_authorities_res = finality_api->authorities( primitives::BlockId( primitives::BlockNumber{ 0 } ) ); + //BOOST_ASSERT_MSG( weighted_authorities_res, "finality_api_->authorities failed" ); + //const auto &weighted_authorities = weighted_authorities_res.value(); + + for ( const auto authority : weighted_authorities ) + { + spdlog::info( "Finality authority: {}", authority.id.id.toHex() ); + } + + verification::finality::VoterSet voters{ 0 }; + for ( const auto &weighted_authority : weighted_authorities ) + { + voters.insert( weighted_authority.id.id, weighted_authority.weight ); + spdlog::debug( "Added to finality authorities: {}, weight: {}", weighted_authority.id.id.toHex(), + weighted_authority.weight ); + } + BOOST_ASSERT_MSG( voters.size() != 0, "Finality voters are empty" ); + auto authorities_put_res = buff_storage->put( storage::kAuthoritySetKey, base::Buffer( scale::encode( voters ).value() ) ); + if ( !authorities_put_res ) + { + BOOST_ASSERT_MSG( false, "Could not insert authorities" ); + std::exit( 1 ); + } } - } - }*/ ); - if ( !maybe_storage ) - { - throw std::runtime_error( "BlockStorage failed to be created" ); - } + } ); + if ( !maybe_storage ) + { + throw std::runtime_error( "BlockStorage failed to be created" ); + } - return maybe_storage.value(); - } -}; + return maybe_storage.value(); + } + }; +} #endif