Files

199 lines
6.5 KiB
C++
Raw Permalink Normal View History

2021-02-14 19:44:28 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Engine.h"
#include "ContextData.h"
#include <streaming/MessageBuilder.h>
#include <streaming/MessageParser.h>
Engine::Engine(ContextData *cd)
: m_verbosity(Log::DebugLevel),
m_cd(cd)
{
assert(m_cd);
setLogLevel(Log::FatalLevel); // by default we are silent.
}
void Engine::hubSentMessage(const Message &message)
{
m_cd->messageFromHub(message);
}
void Engine::indexerSentMessage(const Message &message)
{
if (message.serviceId() == Api::IndexerService
&& message.messageId() == Api::Indexer::GetIndexerLastBlockReply) {
Streaming::MessageParser parser(message.body());
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::BlockHeight) {
logInfo() << "Indexer is at block-height:" << parser.intData();
return;
}
}
}
}
bool Engine::unsubscribe(Streaming::ConstBuffer addressHash)
{
std::lock_guard<std::mutex> lock(m_subscribeAddressesLock);
auto iter = m_subscribedAddresses.begin();
while (iter != m_subscribedAddresses.end()) {
if (iter->bitcoinScriptHashed == addressHash) {
m_subscribedAddresses.erase(iter);
// so, we have it, then the Hub has it too. Lets tell them to remove it.
if (isHubConnected()) {
2021-02-18 09:47:42 +01:00
Streaming::MessageBuilder builder(poolForThread(40));
2021-02-14 19:44:28 +01:00
builder.add(Api::AddressMonitor::BitcoinScriptHashed, addressHash);
sendMessage(builder.message(Api::AddressMonitorService, Api::AddressMonitor::Unsubscribe), Blockchain::TheHub);
}
return true;
}
++iter;
}
return false;
}
void Engine::subscribe(const std::vector<SubscribedAddress> &addresses)
{
if (addresses.empty())
return;
2021-04-03 20:34:51 +02:00
const bool sendNow = isHubConnected();
std::lock_guard<std::mutex> lock(m_subscribeAddressesLock);
Streaming::MessageBuilder builder(poolForThread(40 * addresses.size()));
for (const auto &ad : addresses) {
m_subscribedAddresses.push_back(ad);
2021-04-03 20:34:51 +02:00
if (sendNow)
builder.add(Api::AddressMonitor::BitcoinScriptHashed, ad.bitcoinScriptHashed);
}
2021-04-03 20:34:51 +02:00
if (sendNow) {
sendMessage(builder.message(Api::AddressMonitorService, Api::AddressMonitor::Subscribe),
Blockchain::TheHub);
}
}
2021-02-14 19:44:28 +01:00
std::string Engine::addressForScriptHash(const Streaming::ConstBuffer &buffer) const
{
std::lock_guard<std::mutex> lock(m_subscribeAddressesLock);
auto iter = m_subscribedAddresses.begin();
while (iter != m_subscribedAddresses.end()) {
if (iter->bitcoinScriptHashed == buffer) {
return iter->origRequest;
}
++iter;
}
return std::string();
}
void Engine::initializeHubConnection(NetworkConnection connection, const std::string &hubVersion)
{
// first re-subscribe the addresses to the AddressMonitorService
std::lock_guard<std::mutex> lock(m_subscribeAddressesLock);
for (auto i = m_subscribedAddresses.begin(); i != m_subscribedAddresses.end(); ++i) {
2021-02-18 09:47:42 +01:00
Streaming::MessageBuilder builder(poolForThread(40));
2021-02-14 19:44:28 +01:00
builder.add(Api::AddressMonitor::BitcoinScriptHashed, i->bitcoinScriptHashed);
connection.send(builder.message(Api::AddressMonitorService, Api::AddressMonitor::Subscribe));
}
if (m_listenToBlockUpdates) {
sendMessage(Message(Api::BlockNotificationService,
Api::BlockNotification::Subscribe), Blockchain::TheHub);
}
2021-02-14 19:44:28 +01:00
const bool sent = m_cd->hubConnected(hubVersion);
if (m_startedIndexerConnection && isIndexerConnected() || !sent && !m_startedIndexerConnection)
2021-02-14 19:44:28 +01:00
m_cd->startAllConnectedCallbacks();
}
void Engine::initializeIndexerConnection(NetworkConnection connection, const std::set<Blockchain::Service> &services)
{
Message message(Api::IndexerService, Api::Indexer::GetIndexerLastBlock);
connection.send(message);
bool sent = m_cd->indexerConnected(services);
if (m_startedHubConnection && isHubConnected() || !sent && !m_startedHubConnection)
2021-02-14 19:44:28 +01:00
m_cd->startAllConnectedCallbacks();
}
void Engine::hubDisconnected()
{
// TODO maybe notify JS?
}
void Engine::indexerDisconnected()
{
// TODO maybe notify JS?
}
void Engine::setLogLevel(Log::Verbosity verbosity)
{
if (verbosity == m_verbosity)
return;
m_verbosity = verbosity;
auto *logger = Log::Manager::instance();
logger->clearLogLevels(verbosity);
}
Log::Verbosity Engine::logLevel() const
{
return m_verbosity;
}
void Engine::connect(const std::string &hostname, Net net)
{
EndPoint ep(hostname, (net == Mainnet) ? 1235: 21235);
2021-02-16 17:31:04 +01:00
if (!m_startedHubConnection) {
m_startedHubConnection = true;
addHub(ep);
}
if (!m_startedIndexerConnection) {
ep.peerPort = ep.announcePort = (net == Mainnet) ? 1234 : 21234;
addIndexer(ep);
m_startedIndexerConnection = true;
}
2021-02-14 19:44:28 +01:00
}
void Engine::connectHub(const std::string &hostname, int port)
{
assert(port >= 0 && port <= 0xFFFF);
2021-02-16 17:31:04 +01:00
if (!m_startedHubConnection) {
addHub(EndPoint(hostname, static_cast<uint16_t>(port)));
m_startedHubConnection = true;
}
2021-02-14 19:44:28 +01:00
}
void Engine::connectIndexer(const std::string &hostname, int port)
{
assert(port >= 0 && port <= 0xFFFF);
2021-02-16 17:31:04 +01:00
if (!m_startedIndexerConnection) {
addIndexer(EndPoint(hostname, static_cast<uint16_t>(port)));
m_startedIndexerConnection = true;
}
2021-02-14 19:44:28 +01:00
}
void Engine::setListenToBlockUpdates(bool on)
{
if (on == m_listenToBlockUpdates)
return;
m_listenToBlockUpdates = on;
if (isHubConnected()) {
// subscribe if we need
sendMessage(Message(Api::BlockNotificationService,
m_listenToBlockUpdates ? Api::BlockNotification::Subscribe
: Api::BlockNotification::Unsubscribe), Blockchain::TheHub);
}
}