2026-02-11 20:39:32 +01:00
|
|
|
#include "BlockchainBackend.h"
|
2026-02-17 21:22:38 +01:00
|
|
|
#include <QByteArray>
|
2026-02-11 20:39:32 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDateTime>
|
2026-02-18 10:27:21 +01:00
|
|
|
#include <QSettings>
|
2026-02-17 21:22:38 +01:00
|
|
|
#include <QTimer>
|
2026-02-11 20:39:32 +01:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
2026-02-18 10:27:21 +01:00
|
|
|
namespace {
|
|
|
|
|
const char kSettingsOrg[] = "Logos";
|
|
|
|
|
const char kSettingsApp[] = "BlockchainUI";
|
|
|
|
|
const char kConfigPathKey[] = "configPath";
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 20:39:32 +01:00
|
|
|
BlockchainBackend::BlockchainBackend(LogosAPI* logosAPI, QObject* parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
m_status(NotStarted),
|
|
|
|
|
m_configPath(""),
|
|
|
|
|
m_logModel(new LogModel(this)),
|
|
|
|
|
m_logos(nullptr),
|
|
|
|
|
m_blockchainModule(nullptr)
|
|
|
|
|
{
|
2026-02-18 10:27:21 +01:00
|
|
|
QSettings s(kSettingsOrg, kSettingsApp);
|
|
|
|
|
QString saved = s.value(kConfigPathKey).toString();
|
|
|
|
|
if (!saved.isEmpty()) {
|
|
|
|
|
m_configPath = saved;
|
|
|
|
|
} else {
|
|
|
|
|
m_configPath = QString::fromUtf8(qgetenv("LB_CONFIG_PATH"));
|
|
|
|
|
}
|
2026-02-11 20:39:32 +01:00
|
|
|
|
|
|
|
|
if (!logosAPI) {
|
|
|
|
|
logosAPI = new LogosAPI("core", this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_logos = new LogosModules(logosAPI);
|
|
|
|
|
|
|
|
|
|
if (!m_logos) {
|
|
|
|
|
setStatus(ErrorNotInitialized);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_blockchainModule = &m_logos->liblogos_blockchain_module;
|
|
|
|
|
|
|
|
|
|
if (m_blockchainModule && !m_blockchainModule->on("newBlock", [this](const QVariantList& data) {
|
|
|
|
|
onNewBlock(data);
|
|
|
|
|
})) {
|
|
|
|
|
setStatus(ErrorSubscribeFailed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockchainBackend::~BlockchainBackend()
|
|
|
|
|
{
|
|
|
|
|
stopBlockchain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockchainBackend::setStatus(BlockchainStatus newStatus)
|
|
|
|
|
{
|
|
|
|
|
if (m_status != newStatus) {
|
|
|
|
|
m_status = newStatus;
|
|
|
|
|
emit statusChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockchainBackend::setConfigPath(const QString& path)
|
|
|
|
|
{
|
|
|
|
|
const QString localPath = QUrl::fromUserInput(path).toLocalFile();
|
|
|
|
|
if (m_configPath != localPath) {
|
|
|
|
|
m_configPath = localPath;
|
2026-02-18 10:27:21 +01:00
|
|
|
QSettings s(kSettingsOrg, kSettingsApp);
|
|
|
|
|
s.setValue(kConfigPathKey, m_configPath);
|
2026-02-11 20:39:32 +01:00
|
|
|
emit configPathChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockchainBackend::clearLogs()
|
|
|
|
|
{
|
|
|
|
|
m_logModel->clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BlockchainBackend::getBalance(const QString& addressHex)
|
|
|
|
|
{
|
|
|
|
|
if (!m_blockchainModule) {
|
|
|
|
|
return QStringLiteral("Error: Module not initialized.");
|
|
|
|
|
}
|
2026-02-17 21:22:38 +01:00
|
|
|
return m_blockchainModule->wallet_get_balance(addressHex);
|
2026-02-11 20:39:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BlockchainBackend::transferFunds(const QString& fromKeyHex, const QString& toKeyHex, const QString& amountStr)
|
|
|
|
|
{
|
|
|
|
|
if (!m_blockchainModule) {
|
|
|
|
|
return QStringLiteral("Error: Module not initialized.");
|
|
|
|
|
}
|
2026-02-17 21:22:38 +01:00
|
|
|
QStringList senderAddresses;
|
|
|
|
|
senderAddresses << fromKeyHex;
|
|
|
|
|
return m_blockchainModule->wallet_transfer_funds(fromKeyHex, senderAddresses, toKeyHex, amountStr, "");
|
2026-02-11 20:39:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockchainBackend::startBlockchain()
|
|
|
|
|
{
|
|
|
|
|
if (!m_blockchainModule) {
|
|
|
|
|
setStatus(ErrorNotInitialized);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setStatus(Starting);
|
|
|
|
|
|
|
|
|
|
int result = m_blockchainModule->start(m_configPath, QString());
|
|
|
|
|
|
|
|
|
|
if (result == 0 || result == 1) {
|
|
|
|
|
setStatus(Running);
|
2026-02-17 21:22:38 +01:00
|
|
|
QTimer::singleShot(500, this, [this]() { refreshKnownAddresses(); });
|
2026-02-11 20:39:32 +01:00
|
|
|
} else if (result == 2) {
|
|
|
|
|
setStatus(ErrorConfigMissing);
|
|
|
|
|
} else if (result == 3) {
|
|
|
|
|
setStatus(ErrorStartFailed);
|
|
|
|
|
} else {
|
|
|
|
|
setStatus(ErrorStartFailed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 21:22:38 +01:00
|
|
|
void BlockchainBackend::refreshKnownAddresses()
|
|
|
|
|
{
|
|
|
|
|
if (!m_blockchainModule) return;
|
|
|
|
|
QStringList list = m_blockchainModule->wallet_get_known_addresses();
|
|
|
|
|
qDebug() << "BlockchainBackend: received from blockchain lib: type=QStringList, count=" << list.size();
|
|
|
|
|
if (m_knownAddresses != list) {
|
|
|
|
|
m_knownAddresses = std::move(list);
|
|
|
|
|
emit knownAddressesChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 20:39:32 +01:00
|
|
|
void BlockchainBackend::stopBlockchain()
|
|
|
|
|
{
|
|
|
|
|
if (m_status != Running && m_status != Starting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_blockchainModule) {
|
|
|
|
|
setStatus(ErrorNotInitialized);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setStatus(Stopping);
|
|
|
|
|
|
|
|
|
|
int result = m_blockchainModule->stop();
|
|
|
|
|
|
|
|
|
|
if (result == 0 || result == 1) {
|
|
|
|
|
setStatus(Stopped);
|
|
|
|
|
} else {
|
|
|
|
|
setStatus(ErrorStopFailed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockchainBackend::onNewBlock(const QVariantList& data)
|
|
|
|
|
{
|
|
|
|
|
QString timestamp = QDateTime::currentDateTime().toString("HH:mm:ss");
|
|
|
|
|
QString line;
|
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
|
QString blockInfo = data.first().toString();
|
|
|
|
|
QString shortInfo = blockInfo.left(80);
|
|
|
|
|
if (blockInfo.length() > 80) {
|
|
|
|
|
shortInfo += "...";
|
|
|
|
|
}
|
|
|
|
|
line = QString("[%1] 📦 New block: %2").arg(timestamp, shortInfo);
|
|
|
|
|
} else {
|
|
|
|
|
line = QString("[%1] 📦 New block (no data)").arg(timestamp);
|
|
|
|
|
}
|
|
|
|
|
m_logModel->append(line);
|
|
|
|
|
}
|