Skip to content

Commit

Permalink
Fix: Authority list updated to some hardcoded values to initialize co…
Browse files Browse the repository at this point in the history
…rrectly
  • Loading branch information
henriqueaklein committed Mar 6, 2024
1 parent 9e655d9 commit 061cc5b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 124 deletions.
58 changes: 0 additions & 58 deletions app/integration/ApiServiceFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,62 +114,4 @@ namespace sgns
};
}

/*
ApiService(
const std::shared_ptr<application::AppStateManager> &app_state_manager,
std::shared_ptr<api::RpcThreadPool> thread_pool,
std::vector<std::shared_ptr<Listener>> listeners,
std::shared_ptr<JRpcServer> server,
const std::vector<std::shared_ptr<JRpcProcessor>> &processors,
SubscriptionEnginePtr subscription_engine);
template <typename Injector>
sptr<api::ApiService> get_jrpc_api_service(const Injector &injector) {
static auto initialized =
boost::optional<sptr<api::ApiService>>(boost::none);
if (initialized) {
return initialized.value();
}
using SubscriptionEnginePtr = std::shared_ptr<
subscription::SubscriptionEngine<base::Buffer,
std::shared_ptr<api::Session>,
base::Buffer,
primitives::BlockHash>>;
auto subscription_engine =
injector.template create<SubscriptionEnginePtr>();
auto app_state_manager =
injector
.template create<std::shared_ptr<application::AppStateManager>>();
auto rpc_thread_pool =
injector.template create<std::shared_ptr<api::RpcThreadPool>>();
std::vector<std::shared_ptr<api::Listener>> listeners{
injector.template create<std::shared_ptr<api::HttpListenerImpl>>(),
injector.template create<std::shared_ptr<api::WsListenerImpl>>(),
};
auto server = injector.template create<std::shared_ptr<api::JRpcServer>>();
std::vector<std::shared_ptr<api::JRpcProcessor>> processors{
injector
.template create<std::shared_ptr<api::state::StateJrpcProcessor>>(),
injector.template create<
std::shared_ptr<api::author::AuthorJRpcProcessor>>(),
injector
.template create<std::shared_ptr<api::chain::ChainJrpcProcessor>>(),
injector.template create<
std::shared_ptr<api::system::SystemJrpcProcessor>>()};
initialized =
std::make_shared<api::ApiService>(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<std::shared_ptr<api::StateApi>>();
state_api->setApiService(initialized.value());
return initialized.value();
}
*/

#endif
145 changes: 79 additions & 66 deletions app/integration/BlockStorageFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sgns::blockchain::BlockStorage> create()
class BlockStorageFactory
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
auto maybe_hasher = component_factory->GetComponent( "Hasher", boost::none );

if ( !maybe_hasher )
public:
std::shared_ptr<blockchain::BlockStorage> create()
{
throw std::runtime_error( "Initialize Hasher first" );
}
auto hasher = std::dynamic_pointer_cast<sgns::crypto::Hasher>(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<sgns::storage::BufferStorage>(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<crypto::Hasher>( maybe_hasher.value() );

if ( !maybe_trie_storage )
{
throw std::runtime_error( "Initialize TrieStorage first" );
}
auto trie_storage = std::dynamic_pointer_cast<sgns::storage::trie::TrieStorage>(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<sptr<runtime::FinalityApi>>();
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<storage::BufferStorage>( 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<storage::trie::TrieStorage>( 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<application::KeyStorage>( 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<sptr<runtime::FinalityApi>>();
//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

0 comments on commit 061cc5b

Please sign in to comment.