114 lines
3.2 KiB
C++
Raw Normal View History

2025-12-17 10:55:13 +01:00
#include "library.h"
#include <QtCore/QDebug>
2026-01-22 23:36:47 +01:00
#include <iostream>
#include <QCoreApplication>
2025-12-17 10:55:13 +01:00
2025-12-19 17:31:26 +01:00
2025-12-19 15:53:41 +01:00
class LogosBlockchainModule : public LogosBlockchainModuleAPI {
2025-12-17 10:55:13 +01:00
Q_OBJECT
Q_PLUGIN_METADATA(IID LogosBlockchainModuleInterface_iid FILE "metadata.json")
2025-12-19 15:53:41 +01:00
private:
LogosBlockchainNode* node = nullptr;
2025-12-19 15:53:41 +01:00
2025-12-17 10:55:13 +01:00
public:
2025-12-19 15:53:41 +01:00
LogosBlockchainModule() = default;
2025-12-17 10:55:13 +01:00
2025-12-19 15:53:41 +01:00
~LogosBlockchainModule() override {
if (node) stop();
2025-12-17 10:55:13 +01:00
}
2026-01-22 23:36:47 +01:00
[[nodiscard]] QString name() const override { return "liblogos-blockchain-module"; }
[[nodiscard]] QString version() const override { return "1.0.0"; }
2025-12-17 10:55:13 +01:00
2026-01-22 23:36:47 +01:00
Q_INVOKABLE void initLogos(LogosAPI* logosAPIInstance) override {
2025-12-17 10:55:13 +01:00
logosAPI = logosAPIInstance;
}
2025-12-19 17:31:26 +01:00
Q_INVOKABLE int start(const QString& config_path) override {
2025-12-19 15:53:41 +01:00
if (node) {
2026-01-22 23:36:47 +01:00
qWarning() << "Could not execute the operation: The node is already running.";
2025-12-19 15:53:41 +01:00
return 1;
2025-12-17 10:55:13 +01:00
}
2026-01-22 23:36:47 +01:00
// TODO: Ensure proper cleanup on SIGINT
2025-12-19 15:53:41 +01:00
const QByteArray path = config_path.toUtf8();
2026-01-22 23:36:47 +01:00
auto [value, error] = start_lb_node(path.constData());
2025-12-17 10:55:13 +01:00
2026-01-22 23:36:47 +01:00
if (!is_ok(&error)) {
qCritical() << "Failed to start the node. Error:" << error;
2025-12-19 15:53:41 +01:00
return 2;
2025-12-17 10:55:13 +01:00
}
2026-01-22 23:36:47 +01:00
node = value;
qInfo() << "The node was started successfully.";
2025-12-19 15:53:41 +01:00
return 0;
2025-12-17 10:55:13 +01:00
}
2026-01-22 23:36:47 +01:00
Q_INVOKABLE int stop() override {
2025-12-19 15:53:41 +01:00
if (!node) {
2026-01-22 23:36:47 +01:00
qWarning() << "Could not execute the operation: The node is not running.";
return 1;
2025-12-17 10:55:13 +01:00
}
2025-12-19 15:53:41 +01:00
const OperationStatus status = stop_node(node);
2025-12-18 09:14:40 +01:00
if (is_ok(&status)) {
2026-01-22 23:36:47 +01:00
qInfo() << "The node was stopped successfully.";
2025-12-18 09:14:40 +01:00
} else {
2026-01-22 23:36:47 +01:00
qCritical() << "Could not stop the node. Error:" << status;
2025-12-17 10:55:13 +01:00
}
node = nullptr;
2026-01-22 23:36:47 +01:00
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;
2025-12-17 10:55:13 +01:00
}
};
2025-12-18 11:47:59 +01:00
2025-12-19 15:53:41 +01:00
#include "library.moc"