diff --git a/CMakeLists.txt b/CMakeLists.txt index 6691a28..77ce4d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -162,12 +162,13 @@ set_target_properties(${PLUGIN_TARGET} PROPERTIES ) target_sources(${PLUGIN_TARGET} PRIVATE - src/library.cpp + src/logos_blockchain_module.cpp + src/logos_blockchain_module.h metadata.json ) set_target_properties(${PLUGIN_TARGET} PROPERTIES - PUBLIC_HEADER "src/library.h" + PUBLIC_HEADER "src/i_logos_blockchain_module.h" ) target_include_directories(${PLUGIN_TARGET} PRIVATE diff --git a/src/library.h b/src/i_logos_blockchain_module.h similarity index 69% rename from src/library.h rename to src/i_logos_blockchain_module.h index a2fd62a..01ca94d 100644 --- a/src/library.h +++ b/src/i_logos_blockchain_module.h @@ -1,5 +1,5 @@ -#ifndef LOGOS_BLOCKCHAIN_MODULE_API_H -#define LOGOS_BLOCKCHAIN_MODULE_API_H +#ifndef I_LOGOS_BLOCKCHAIN_MODULE_API_H +#define I_LOGOS_BLOCKCHAIN_MODULE_API_H #include @@ -11,12 +11,13 @@ extern "C" { } #endif -class LogosBlockchainModuleAPI : public QObject, public PluginInterface { +class ILogosBlockchainModule : public QObject, public PluginInterface { + Q_OBJECT Q_INTERFACES(PluginInterface) public: using QObject::QObject; - ~LogosBlockchainModuleAPI() override = default; + ~ILogosBlockchainModule() override = default; // Logos Core Q_INVOKABLE virtual void initLogos(LogosAPI* logosAPIInstance) = 0; @@ -32,7 +33,7 @@ public: void eventResponse(const QString& eventName, const QVariantList& data); }; -#define LogosBlockchainModuleInterface_iid "org.logos.blockchaininterface" -Q_DECLARE_INTERFACE(LogosBlockchainModuleAPI, LogosBlockchainModuleInterface_iid) +#define ILogosBlockchainModule_iid "org.logos.ilogosblockchainmodule" +Q_DECLARE_INTERFACE(ILogosBlockchainModule, ILogosBlockchainModule_iid) #endif diff --git a/src/library.cpp b/src/library.cpp deleted file mode 100644 index 79be8c4..0000000 --- a/src/library.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "library.h" - -#include -#include -#include - - -class LogosBlockchainModule : public LogosBlockchainModuleAPI { - Q_OBJECT - Q_PLUGIN_METADATA(IID LogosBlockchainModuleInterface_iid FILE "../metadata.json") - -private: - LogosBlockchainNode* node = nullptr; - -public: - LogosBlockchainModule() = default; - - ~LogosBlockchainModule() override { - if (node) stop(); - } - - [[nodiscard]] QString name() const override { return "liblogos-blockchain-module"; } - [[nodiscard]] QString version() const override { return "1.0.0"; } - - Q_INVOKABLE void initLogos(LogosAPI* logosAPIInstance) override { - logosAPI = logosAPIInstance; - } - - Q_INVOKABLE int start(const QString& config_path) override { - if (node) { - qWarning() << "Could not execute the operation: The node is already running."; - return 1; - } - - // TODO: Default configuration file - - // TODO: Ensure proper cleanup on SIGINT - - const QByteArray path = config_path.toUtf8(); - const char* deployment = nullptr; - auto [value, error] = start_lb_node(path.constData(), deployment); - - if (!is_ok(&error)) { - qCritical() << "Failed to start the node. Error:" << error; - return 2; - } - - node = value; - qInfo() << "The node was started successfully."; - return 0; - } - - Q_INVOKABLE int stop() override { - if (!node) { - qWarning() << "Could not execute the operation: The node is not running."; - return 1; - } - - const OperationStatus status = stop_node(node); - if (is_ok(&status)) { - qInfo() << "The node was stopped successfully."; - } else { - qCritical() << "Could not stop the node. Error:" << status; - } - - node = nullptr; - return 0; - } - - Q_INVOKABLE int subscribe() override { - if (!node) { - qWarning() << "Could not execute the operation: The node is not running."; - return 1; - } - - subscribe_to_new_blocks(node, [](const char* block) { - std::cout << "Received new block: " << block << std::endl; - }); - - return 0; - } - - Q_INVOKABLE int wallet_get_balance(const uint8_t* wallet_address, const HeaderId* optional_tip, BalanceResult* output_balance) override { - if (!node) { - qWarning() << "Could not execute the operation: The node is not running."; - return 1; - } - - auto [value, error] = get_balance(node, wallet_address, optional_tip); - if (!is_ok(&error)) { - qCritical() << "Failed to get balance. Error:" << error; - return 1; - } - - output_balance->value = value; - return 0; - } - - Q_INVOKABLE int wallet_transfer_funds(const TransferFundsArguments* transfer_funds_arguments, Hash* output_hash) override { - if (!node) { - qWarning() << "Could not execute the operation: The node is not running."; - return 1; - } - - auto [value, error] = transfer_funds(node, transfer_funds_arguments); - if (!is_ok(&error)) { - qCritical() << "Failed to transfer funds. Error:" << error; - return 1; - } - - std::ranges::copy(value, *output_hash); - return 0; - } -}; - -#include "library.moc" diff --git a/src/logos_blockchain_module.cpp b/src/logos_blockchain_module.cpp new file mode 100644 index 0000000..d87e1fe --- /dev/null +++ b/src/logos_blockchain_module.cpp @@ -0,0 +1,113 @@ +#include "logos_blockchain_module.h" + +#include +#include + +LogosBlockchainModule::LogosBlockchainModule() = default; + +LogosBlockchainModule::~LogosBlockchainModule() { + if (node) { + stop(); + } +} + +QString LogosBlockchainModule::name() const { + return "liblogos-blockchain-module"; +} + +QString LogosBlockchainModule::version() const { + return "1.0.0"; +} + +void LogosBlockchainModule::initLogos(LogosAPI* logosAPIInstance) { + logosAPI = logosAPIInstance; +} + +int LogosBlockchainModule::start(const QString& config_path) { + if (node) { + qWarning() << "Could not execute the operation: The node is already running."; + return 1; + } + + const QByteArray path = config_path.toUtf8(); + const char* deployment = nullptr; + + auto [value, error] = start_lb_node(path.constData(), deployment); + if (!is_ok(&error)) { + qCritical() << "Failed to start the node. Error:" << error; + return 2; + } + + node = value; + qInfo() << "The node was started successfully."; + return 0; +} + +int LogosBlockchainModule::stop() { + if (!node) { + qWarning() << "Could not execute the operation: The node is not running."; + return 1; + } + + const OperationStatus status = stop_node(node); + if (is_ok(&status)) { + qInfo() << "The node was stopped successfully."; + } else { + qCritical() << "Could not stop the node. Error:" << status; + } + + node = nullptr; + return 0; +} + +int LogosBlockchainModule::subscribe() { + if (!node) { + qWarning() << "Could not execute the operation: The node is not running."; + return 1; + } + + subscribe_to_new_blocks(node, [](const char* block) { + std::cout << "Received new block: " << block << std::endl; + }); + + return 0; +} + +int LogosBlockchainModule::wallet_get_balance( + const uint8_t* wallet_address, + const HeaderId* optional_tip, + BalanceResult* output_balance +) { + if (!node) { + qWarning() << "Could not execute the operation: The node is not running."; + return 1; + } + + auto [value, error] = get_balance(node, wallet_address, optional_tip); + if (!is_ok(&error)) { + qCritical() << "Failed to get balance. Error:" << error; + return 1; + } + + output_balance->value = value; + return 0; +} + +int LogosBlockchainModule::wallet_transfer_funds( + const TransferFundsArguments* transfer_funds_arguments, + Hash* output_hash +) { + if (!node) { + qWarning() << "Could not execute the operation: The node is not running."; + return 1; + } + + auto [value, error] = transfer_funds(node, transfer_funds_arguments); + if (!is_ok(&error)) { + qCritical() << "Failed to transfer funds. Error:" << error; + return 1; + } + + std::ranges::copy(value, *output_hash); + return 0; +} diff --git a/src/logos_blockchain_module.h b/src/logos_blockchain_module.h new file mode 100644 index 0000000..c6a0ce8 --- /dev/null +++ b/src/logos_blockchain_module.h @@ -0,0 +1,25 @@ +#pragma once + +#include "i_logos_blockchain_module.h" + +class LogosBlockchainModule final : public ILogosBlockchainModule { + Q_OBJECT + Q_PLUGIN_METADATA(IID ILogosBlockchainModule_iid FILE "../metadata.json") + +public: + LogosBlockchainModule(); + ~LogosBlockchainModule() override; + + [[nodiscard]] QString name() const override; + [[nodiscard]] QString version() const override; + + Q_INVOKABLE void initLogos(LogosAPI*) override; + Q_INVOKABLE int start(const QString&) override; + Q_INVOKABLE int stop() override; + Q_INVOKABLE int subscribe() override; + Q_INVOKABLE int wallet_get_balance(const uint8_t*, const HeaderId*, BalanceResult*) override; + Q_INVOKABLE int wallet_transfer_funds(const TransferFundsArguments*, Hash*) override; + +private: + LogosBlockchainNode* node = nullptr; +};