mirror of
https://github.com/cculianu/Fulcrum.git
synced 2026-08-16 13:01:07 +02:00
Playing with lambdas. Detects when bitcoind goes down. Initial code -- can put hooks in the detection for eg. stopping block processing or starting it, etc.
203 lines
6.4 KiB
C++
203 lines
6.4 KiB
C++
#include "BitcoinD.h"
|
|
|
|
BitcoinDMgr::BitcoinDMgr(const QHostAddress &host, quint16 port,
|
|
const QString &user, const QString &pass)
|
|
: Mgr(nullptr), IdMixin(newId()), host(host), port(port), user(user), pass(pass)
|
|
{
|
|
setObjectName("BitcoinDMgr");
|
|
_thread.setObjectName(objectName());
|
|
}
|
|
|
|
BitcoinDMgr::~BitcoinDMgr() { cleanup(); }
|
|
|
|
void BitcoinDMgr::startup() {
|
|
Log() << objectName() << ": starting " << N_CLIENTS << " bitcoin rpc clients ...";
|
|
|
|
for (auto & client : clients) {
|
|
constexpr int miniTimeout = 333;
|
|
|
|
client = std::make_unique<BitcoinD>(host, port, user, pass);
|
|
|
|
// connect client to us -- TODO: figure out workflow: how requests for work and results will get dispatched
|
|
connect(client.get(), &BitcoinD::gotMessage, this, &BitcoinDMgr::on_Message);
|
|
connect(client.get(), &BitcoinD::gotErrorMessage, this, &BitcoinDMgr::on_ErrorMessage);
|
|
connect(client.get(), &BitcoinD::authenticated, this, [this](BitcoinD *b){
|
|
// guard against stale/old signal
|
|
if (!Util::CallOnObjectWithTimeoutNoThrow<bool>(miniTimeout, b, &BitcoinD::isGood).value_or(false)) {
|
|
Debug() << "got authenticated for id:" << b->id << " but isGood() is false!";
|
|
return; // false/stale signal
|
|
}
|
|
const bool wasEmpty = goodBitcoinDs.empty();
|
|
goodBitcoinDs.insert(b->id);
|
|
if (wasEmpty)
|
|
emit gotFirstGoodConnection(b->id);
|
|
});
|
|
connect(client.get(), &BitcoinD::lostConnection, this, [this](AbstractConnection *c){
|
|
// guard against stale/old signal
|
|
if (Util::CallOnObjectWithTimeoutNoThrow<bool>(miniTimeout, c, &AbstractConnection::isGood).value_or(false)) {
|
|
Debug() << "got lostConnection for id:" << c->id << " but isGood() is true!";
|
|
return; // false/stale signal
|
|
}
|
|
goodBitcoinDs.erase(c->id);
|
|
auto constexpr chkTimer = "checkNoMoreBitcoinDs";
|
|
// we throttle the spamming of the allConnectionsLost signal via this mechanism
|
|
callOnTimerSoonNoRepeat(miniTimeout, chkTimer, [this]{
|
|
if (goodBitcoinDs.empty())
|
|
emit allConnectionsLost();
|
|
}, true);
|
|
});
|
|
|
|
client->start();
|
|
}
|
|
|
|
start();
|
|
|
|
Log() << objectName() << ": started ok";
|
|
}
|
|
|
|
void BitcoinDMgr::cleanup() {
|
|
stop();
|
|
|
|
for (auto & client : clients) {
|
|
client.reset(); /// implicitly calls client->stop()
|
|
}
|
|
goodBitcoinDs.clear();
|
|
|
|
Debug() << "BitcoinDMgr cleaned up";
|
|
}
|
|
|
|
void BitcoinDMgr::on_Message(quint64 bid, const RPC::Message &msg)
|
|
{
|
|
Debug() << "Msg from: " << bid << " method=" << msg.method;
|
|
}
|
|
void BitcoinDMgr::on_ErrorMessage(quint64 bid, const RPC::Message &msg)
|
|
{
|
|
Debug() << "ErrMsg from: " << bid << " error=" << msg.errorMessage();
|
|
}
|
|
|
|
|
|
auto BitcoinDMgr::stats() const -> Stats
|
|
{
|
|
Stats ret;
|
|
QVariantList l;
|
|
constexpr int timeout = kDefaultTimeout/qMax(N_CLIENTS,1);
|
|
for (const auto & client : clients) {
|
|
if (!client) continue;
|
|
auto map = client->statsSafe(timeout);
|
|
auto name = map.take("name").toString();
|
|
l += QVariantMap({{ name, map }});
|
|
}
|
|
ret["Bitcoin Daemon"] = l;
|
|
return ret;
|
|
}
|
|
|
|
auto BitcoinD::stats() const -> Stats
|
|
{
|
|
Stats m = RPC::HttpConnection::stats();
|
|
m["lastPeerError"] = badAuth ? "Auth Failure" : lastPeerError;
|
|
m.remove("nErrorsSent"); // should always be 0
|
|
m.remove("nNotificationsSent"); // again, 0
|
|
m.remove("nResultsSent"); // again, 0
|
|
return m;
|
|
}
|
|
|
|
BitcoinD::BitcoinD(const QHostAddress &host, quint16 port, const QString & user, const QString &pass)
|
|
: RPC::HttpConnection(RPC::MethodMap{}, newId(), nullptr), host(host), port(port)
|
|
{
|
|
static int N = 1;
|
|
setObjectName(QString("BitcoinD.%1").arg(N++));
|
|
_thread.setObjectName(objectName());
|
|
|
|
setAuth(user, pass);
|
|
setV1(true); // bitcoind uses jsonrpc v1
|
|
pingtime_ms = 10000;
|
|
stale_threshold = pingtime_ms * 2;
|
|
|
|
connectMiscSignals();
|
|
}
|
|
|
|
|
|
BitcoinD::~BitcoinD()
|
|
{
|
|
stop();
|
|
}
|
|
|
|
void BitcoinD::connectMiscSignals()
|
|
{
|
|
connect(this, &BitcoinD::gotMessage, this, [this]{
|
|
// this hook emits "authenticated" as soon as we get a good result message via 'do_ping' initiated from 'on_connected' below
|
|
if (needAuth || badAuth) {
|
|
needAuth = badAuth = false;
|
|
emit authenticated(this);
|
|
}
|
|
});
|
|
}
|
|
|
|
bool BitcoinD::isGood() const
|
|
{
|
|
return !badAuth && !needAuth && RPC::HttpConnection::isGood();
|
|
}
|
|
|
|
void BitcoinD::on_started()
|
|
{
|
|
ThreadObjectMixin::on_started();
|
|
|
|
{ // setup the "reconnect timer"
|
|
constexpr auto reconnectTimer = "reconnectTimer";
|
|
const auto SetTimer = [this] {
|
|
callOnTimerSoon(5000, reconnectTimer, [this]{
|
|
if (!isGood()) {
|
|
Debug() << prettyName() << " reconnecting...";
|
|
reconnect();
|
|
return true; // keep the timer alive
|
|
}
|
|
return false; // kill timer
|
|
});
|
|
};
|
|
conns += connect(this, &BitcoinD::lostConnection, this, [SetTimer]{
|
|
Log() << "Lost connection to bitcoind, will retry every 5 seconds ...";
|
|
SetTimer();
|
|
});
|
|
conns += connect(this, &BitcoinD::authFailure, this, [SetTimer, this] {
|
|
Error() << "Authentication to bitcoind rpc failed. Please check the rpcuser and rpcpass are correct and restart!";
|
|
badAuth = true;
|
|
SetTimer();
|
|
});
|
|
conns += connect(this, &BitcoinD::authenticated, this, [this] { stopTimer(reconnectTimer); });
|
|
|
|
SetTimer();
|
|
}
|
|
|
|
reconnect();
|
|
}
|
|
|
|
void BitcoinD::reconnect()
|
|
{
|
|
if (socket) delete socket;
|
|
socket = new QTcpSocket(this);
|
|
socketConnectSignals();
|
|
socket->connectToHost(host, port);
|
|
}
|
|
|
|
void BitcoinD::on_connected()
|
|
{
|
|
RPC::HttpConnection::on_connected();
|
|
lastGood = Util::getTime();
|
|
nSent = nReceived = 0;
|
|
lastPeerError.clear();
|
|
lastSocketError.clear();
|
|
badAuth = false;
|
|
needAuth = true;
|
|
emit connected(this);
|
|
// note that the 'authenticated' signal is only emitted after good auth is confirmed via the reply from the do_ping below
|
|
do_ping();
|
|
}
|
|
|
|
void BitcoinD::do_ping()
|
|
{
|
|
if (isStale()) {
|
|
Debug() << "Stale connection, reconnecting.";
|
|
reconnect();
|
|
} else
|
|
emit sendRequest(newId(), "getblockcount");
|
|
}
|