Skip to content

Commit

Permalink
Feat: Finished JRpc stuff. Need to check now finality dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueaklein committed Mar 5, 2024
1 parent b601b74 commit 4a47722
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 18 deletions.
43 changes: 43 additions & 0 deletions app/integration/ChainApiFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file ChainApiFactory.hpp
* @brief
* @date 2024-03-05
* @author Henrique A. Klein ([email protected])
*/
#ifndef _CHAIN_API_FACTORY_HPP_
#define _CHAIN_API_FACTORY_HPP_

#include "api/service/chain/impl/chain_api_impl.hpp"
#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_tree.hpp"

class CComponentFactory;
namespace sgns
{
class ChainApiFactory
{
public:
std::shared_ptr<api::ChainApi> create()
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
auto result = component_factory->GetComponent( "BlockHeaderRepository", boost::none );

if ( !result )
{
throw std::runtime_error( "Initialize BlockHeaderRepository first" );
}
auto block_header_repo = std::dynamic_pointer_cast<blockchain::BlockHeaderRepository>( result.value() );

result = component_factory->GetComponent( "BlockTree", boost::none );
if ( !result )
{
throw std::runtime_error( "Initialize BlockTree first" );
}
auto block_tree = std::dynamic_pointer_cast<blockchain::BlockTree>( result.value() );

return std::make_shared<api::ChainApiImpl>(block_header_repo, block_tree);
}
};
}

#endif
52 changes: 52 additions & 0 deletions app/integration/StateApiFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @file StateApiFactory.hpp
* @brief
* @date 2024-03-05
* @author Henrique A. Klein ([email protected])
*/
#ifndef _STATE_API_FACTORY_HPP_
#define _STATE_API_FACTORY_HPP_

#include "api/service/state/impl/state_api_impl.hpp"
#include "blockchain/block_header_repository.hpp"
#include "storage/trie/trie_storage.hpp"
#include "blockchain/block_tree.hpp"

class CComponentFactory;
namespace sgns
{
class StateApiFactory
{
//TODO - Check removed runtime::Core from binaryen
public:
std::shared_ptr<api::StateApi> create()
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
auto result = component_factory->GetComponent( "BlockHeaderRepository", boost::none );

if ( !result )
{
throw std::runtime_error( "Initialize BlockHeaderRepository first" );
}
auto block_header_repo = std::dynamic_pointer_cast<blockchain::BlockHeaderRepository>( result.value() );

result = component_factory->GetComponent( "TrieStorage", boost::none );
if ( !result )
{
throw std::runtime_error( "Initialize TrieStorage first" );
}
auto trie_storage = std::dynamic_pointer_cast<storage::trie::TrieStorage>( result.value() );

result = component_factory->GetComponent( "BlockTree", boost::none );
if ( !result )
{
throw std::runtime_error( "Initialize BlockTree first" );
}
auto block_tree = std::dynamic_pointer_cast<blockchain::BlockTree>( result.value() );

return std::make_shared<api::StateApiImpl>( block_header_repo, trie_storage, block_tree );
}
};
}

#endif
38 changes: 38 additions & 0 deletions app/integration/SystemApiFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file SystemApiFactory.hpp
* @brief
* @date 2024-03-05
* @author Henrique A. Klein ([email protected])
*/
#ifndef _SYSTEM_API_FACTORY_HPP_
#define _SYSTEM_API_FACTORY_HPP_

#include "api/service/system/impl/system_api_impl.hpp"
#include "blockchain/block_header_repository.hpp"
#include "storage/trie/trie_storage.hpp"
#include "blockchain/block_tree.hpp"

class CComponentFactory;
namespace sgns
{
class SystemApiFactory
{
//TODO - Check removed runtime::Core from binaryen
public:
std::shared_ptr<api::SystemApi> create()
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
auto result = component_factory->GetComponent( "ConfigurationStorage", boost::none );

if ( !result )
{
throw std::runtime_error( "Initialize ConfigurationStorage first" );
}
auto config_storage = std::dynamic_pointer_cast<application::ConfigurationStorage>( result.value() );

return std::make_shared<api::SystemApiImpl>( config_storage );
}
};
}

#endif
3 changes: 2 additions & 1 deletion src/api/service/chain/chain_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include "base/buffer.hpp"
#include "outcome/outcome.hpp"
#include "primitives/common.hpp"
#include "integration/IComponent.hpp"

namespace sgns::api {
/**
* @class ChainApi privides interface for blockchain api
*/
class ChainApi {
class ChainApi : public IComponent {
public:
virtual ~ChainApi() = default;
using BlockNumber = primitives::BlockNumber;
Expand Down
5 changes: 5 additions & 0 deletions src/api/service/chain/impl/chain_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace sgns::api {
outcome::result<std::vector<BlockHash>> getBlockHash(
gsl::span<const ValueType> values) const override;

std::string GetName() override
{
return "ChainApiImpl";
}

private:
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
Expand Down
14 changes: 8 additions & 6 deletions src/api/service/state/impl/state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace sgns::api {
StateApiImpl::StateApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core)
std::shared_ptr<blockchain::BlockTree> block_tree/*,
std::shared_ptr<runtime::Core> runtime_core*/)
: block_repo_{std::move(block_repo)},
storage_{std::move(trie_storage)},
block_tree_{std::move(block_tree)},
runtime_core_{std::move(runtime_core)} {
block_tree_{std::move(block_tree)}/*,
runtime_core_{std::move(runtime_core)}*/ {
BOOST_ASSERT(nullptr != block_repo_);
BOOST_ASSERT(nullptr != storage_);
BOOST_ASSERT(nullptr != block_tree_);
BOOST_ASSERT(nullptr != runtime_core_);
//BOOST_ASSERT(nullptr != runtime_core_);
}

outcome::result<base::Buffer> StateApiImpl::getStorage(
Expand All @@ -35,7 +35,9 @@ namespace sgns::api {

outcome::result<primitives::Version> StateApiImpl::getRuntimeVersion(
const boost::optional<primitives::BlockHash> &at) const {
return runtime_core_->version(at);
//return runtime_core_->version(at);

return primitives::Version{};
}

void StateApiImpl::setApiService(
Expand Down
11 changes: 8 additions & 3 deletions src/api/service/state/impl/state_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace sgns::api {
public:
StateApiImpl(std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core);
std::shared_ptr<blockchain::BlockTree> block_tree/*,
std::shared_ptr<runtime::Core> runtime_core*/);

void setApiService(
std::shared_ptr<api::ApiService> const &api_service) override;
Expand All @@ -32,11 +32,16 @@ namespace sgns::api {
outcome::result<void> unsubscribeStorage(
const std::vector<uint32_t> &subscription_id) override;

std::string GetName() override
{
return "StateApiImpl";
}

private:
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo_;
std::shared_ptr<const storage::trie::TrieStorage> storage_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::Core> runtime_core_;
//std::shared_ptr<runtime::Core> runtime_core_;

std::weak_ptr<api::ApiService> api_service_;
};
Expand Down
3 changes: 2 additions & 1 deletion src/api/service/state/state_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include "outcome/outcome.hpp"
#include "primitives/common.hpp"
#include "primitives/version.hpp"
#include "integration/IComponent.hpp"

namespace sgns::api {

class StateApi {
class StateApi : public IComponent {
public:
virtual ~StateApi() = default;

Expand Down
5 changes: 5 additions & 0 deletions src/api/service/system/impl/system_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace sgns::api {
std::shared_ptr<application::ConfigurationStorage> getConfig()
const override;

std::string GetName() override
{
return "SystemApiImpl";
}

private:
std::shared_ptr<application::ConfigurationStorage> config_;
};
Expand Down
3 changes: 2 additions & 1 deletion src/api/service/system/system_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define SUPERGENIUS_API_SYSTEMAPI

#include "application/configuration_storage.hpp"
#include "integration/IComponent.hpp"

namespace sgns::api {

/// Auxiliary class that providing access for some app's parts over RPC
class SystemApi {
class SystemApi : public IComponent {
public:
virtual ~SystemApi() = default;

Expand Down
10 changes: 8 additions & 2 deletions src/application/impl/validating_node_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#include "integration/ListenerFactory.hpp"
#include "integration/JRpcServerFactory.hpp"
#include "integration/JRpcProcessorFactory.hpp"
#include "integration/ChainApiFactory.hpp"
#include "integration/StateApiFactory.hpp"
#include "integration/SystemApiFactory.hpp"

#include "storage/trie/supergenius_trie/supergenius_trie_factory_impl.hpp"
#include "storage/trie/serialization/supergenius_codec.hpp"
Expand Down Expand Up @@ -110,7 +113,7 @@ namespace sgns::application
component_factory->Register( ExtrinsicGossiperFactory::create(), "ProductionGossiper", boost::none );
component_factory->Register( sgns::SR25519KeypairFactory{}.create(), "SR25519Keypair", boost::none );
component_factory->Register( ProductionFactory::create( *io_context_ ), "Production", boost::none );
component_factory->Register( ProductionFactory::create( *io_context_ ), "ProductionObserver", boost::none );
component_factory->Register( (component_factory->GetComponent( "Production", boost::none )).value(), "ProductionObserver", boost::none );
component_factory->Register( sgns::EnvironmentFactory{}.create(), "Environment", boost::none );
component_factory->Register( sgns::ED25519ProviderFactory{}.create(), "ED25519Provider", boost::none );
component_factory->Register( sgns::ED25519KeyPairFactory{}.create(), "ED25519Keypair", boost::none );
Expand All @@ -124,12 +127,15 @@ namespace sgns::application
component_factory->Register( sgns::RpcThreadPoolFactory{}.create(), "RpcThreadPool", boost::none );
component_factory->Register( sgns::ListenerFactory{}.create( "ws", app_config->rpc_ws_endpoint() ), "Listener",
boost::make_optional( std::string( "ws" ) ) );
component_factory->Register( sgns::ListenerFactory{}.create( "http", app_config->rpc_ws_endpoint() ), "Listener",
component_factory->Register( sgns::ListenerFactory{}.create( "http", app_config->rpc_http_endpoint() ), "Listener",
boost::make_optional( std::string( "http" ) ) );
component_factory->Register( sgns::JRpcServerFactory{}.create(), "JRpcServer", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("Author"), "JRpcProcessor", boost::make_optional( std::string( "Author" ) ) );
component_factory->Register( sgns::ChainApiFactory{}.create(), "ChainApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("Chain"), "JRpcProcessor", boost::make_optional( std::string( "Chain" ) ) );
component_factory->Register( sgns::StateApiFactory{}.create(), "StateApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("State"), "JRpcProcessor", boost::make_optional( std::string( "State" ) ) );
component_factory->Register( sgns::SystemApiFactory{}.create(), "SystemApi", boost::none );
component_factory->Register( sgns::JRpcProcessorFactory{}.create("System"), "JRpcProcessor", boost::make_optional( std::string( "System" ) ) );
component_factory->Register( sgns::ApiServiceFactory{}.create(), "ApiService", boost::none );

Expand Down
8 changes: 4 additions & 4 deletions src/injector/application_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ namespace sgns::injector {
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>>(),
.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>>(),
.template create<std::shared_ptr<api::chain::ChainJRpcProcessor>>(),
injector.template create<
std::shared_ptr<api::system::SystemJrpcProcessor>>()};
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),
std::move(sevrver),
processors,
std::move(subscription_engine));

Expand Down

0 comments on commit 4a47722

Please sign in to comment.