Split into header and source files.

This commit is contained in:
Alejandro Cabeza Romero 2026-01-30 00:43:09 +01:00
parent 34b80a33c6
commit 5da7a006f7
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
5 changed files with 148 additions and 124 deletions

View File

@ -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

View File

@ -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 <core/interface.h>
@ -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

View File

@ -1,116 +0,0 @@
#include "library.h"
#include <QtCore/QDebug>
#include <iostream>
#include <QCoreApplication>
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"

View File

@ -0,0 +1,113 @@
#include "logos_blockchain_module.h"
#include <QtCore/QDebug>
#include <iostream>
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;
}

View File

@ -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;
};