diff --git a/library.cpp b/library.cpp index 438b51e..dfb4fe0 100644 --- a/library.cpp +++ b/library.cpp @@ -1,14 +1,9 @@ #include "library.h" #include +#include +#include -#ifdef __cplusplus -extern "C" { -#endif -#include -#ifdef __cplusplus -} -#endif class LogosBlockchainModule : public LogosBlockchainModuleAPI { Q_OBJECT @@ -24,46 +19,94 @@ public: if (node) stop(); } - QString name() const override { return "liblogos-blockchain-module"; } - QString version() const override { return "1.0.0"; } + [[nodiscard]] QString name() const override { return "liblogos-blockchain-module"; } + [[nodiscard]] QString version() const override { return "1.0.0"; } - void initLogos(LogosAPI* logosAPIInstance) override { + Q_INVOKABLE void initLogos(LogosAPI* logosAPIInstance) override { logosAPI = logosAPIInstance; } Q_INVOKABLE int start(const QString& config_path) override { if (node) { - qWarning() << "Node already started"; + qWarning() << "Could not execute the operation: The node is already running."; return 1; } - const QByteArray path = config_path.toUtf8(); - InitializedLogosBlockchainNodeResult result = start_lb_node(path.constData()); + // TODO: Ensure proper cleanup on SIGINT - if (!is_ok(&result.error)) { - qCritical() << "Failed to start Nomos node. Error code:" << result.error; + const QByteArray path = config_path.toUtf8(); + auto [value, error] = start_lb_node(path.constData()); + + if (!is_ok(&error)) { + qCritical() << "Failed to start the node. Error:" << error; return 2; } - node = result.value; - qInfo() << "Nomos node started successfully"; + node = value; + qInfo() << "The node was started successfully."; return 0; } - Q_INVOKABLE void stop() override { + Q_INVOKABLE int stop() override { if (!node) { - qWarning() << "Node not running"; - return; + qWarning() << "Could not execute the operation: The node is not running."; + return 1; } const OperationStatus status = stop_node(node); if (is_ok(&status)) { - qInfo() << "Nomos node stopped successfully"; + qInfo() << "The node was stopped successfully."; } else { - qCritical() << "Failed to stop Nomos node. Error code:" << status; + 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; } }; diff --git a/library.h b/library.h index 21a2023..ff89e14 100644 --- a/library.h +++ b/library.h @@ -3,6 +3,14 @@ #include +#ifdef __cplusplus +extern "C" { +#endif +#include +#ifdef __cplusplus +} +#endif + class LogosBlockchainModuleAPI : public QObject, public PluginInterface { Q_OBJECT Q_INTERFACES(PluginInterface) @@ -11,10 +19,16 @@ public: using QObject::QObject; ~LogosBlockchainModuleAPI() override = default; - Q_INVOKABLE virtual int start(const QString& config_path) = 0; - Q_INVOKABLE virtual void stop() = 0; + // Logos Core Q_INVOKABLE virtual void initLogos(LogosAPI* logosAPIInstance) = 0; + // Node + Q_INVOKABLE virtual int start(const QString& config_path) = 0; + Q_INVOKABLE virtual int stop() = 0; + Q_INVOKABLE virtual int subscribe() = 0; + Q_INVOKABLE virtual int wallet_get_balance(const uint8_t* wallet_address, const HeaderId* optional_tip, BalanceResult* output_balance) = 0; + Q_INVOKABLE virtual int wallet_transfer_funds(const TransferFundsArguments* transfer_funds_arguments, Hash* output_hash) = 0; + signals: void eventResponse(const QString& eventName, const QVariantList& data); };