Files
thehub/hub/api/BlockNotificationService.cpp
T

92 lines
3.3 KiB
C++
Raw Permalink Normal View History

2019-03-10 14:00:36 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2018-2021 Tom Zander <tom@flowee.org>
2019-03-10 14:00:36 +01:00
*
* 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 "BlockNotificationService.h"
#include <APIProtocol.h>
2019-03-10 14:00:36 +01:00
#include <Logger.h>
#include <Message.h>
#include <chain.h>
#include <primitives/Block.h>
2019-03-10 14:00:36 +01:00
#include <streaming/MessageBuilder.h>
BlockNotificationService::BlockNotificationService()
2019-04-09 17:24:38 +02:00
: NetworkService(Api::BlockNotificationService)
2019-03-10 14:00:36 +01:00
{
ValidationNotifier().addListener(this);
}
BlockNotificationService::~BlockNotificationService()
{
ValidationNotifier().removeListener(this);
}
2021-11-02 10:18:24 +01:00
void BlockNotificationService::syncAllTransactionsInBlock(const Block &, CBlockIndex *index)
2019-03-10 14:00:36 +01:00
{
const auto list = remotes<RemoteWithBool>(&NetworkService::filterRemoteWithBool);
if (list.empty())
2019-03-10 14:00:36 +01:00
return;
m_pool.reserve(45);
Streaming::MessageBuilder builder(m_pool);
builder.add(Api::BlockNotification::BlockHash, index->GetBlockHash());
2019-03-27 18:51:19 +01:00
builder.add(Api::BlockNotification::BlockHeight, index->nHeight);
2019-03-10 14:00:36 +01:00
Message message(builder.message(Api::BlockNotificationService, Api::BlockNotification::NewBlockOnChain));
for (auto &subinfo : list) {
subinfo->connection.send(message);
}
}
2021-11-02 10:18:24 +01:00
void BlockNotificationService::chainReorged(CBlockIndex *oldTip, const std::vector<Block> &revertedBlocks)
{
const auto list = remotes<RemoteWithBool>(&NetworkService::filterRemoteWithBool);
if (list.empty())
return;
/*
* since the service already sends out which block is the new one in a separate message,
* all we will do in this one is notify them which blocks have been removed.
*/
m_pool.reserve(revertedBlocks.size() * 42);
CBlockIndex *index = oldTip;
Streaming::MessageBuilder builder(m_pool);
for (size_t i = 0; i < revertedBlocks.size(); ++i) {
assert(index);
builder.add(Api::BlockNotification::BlockHash, index->GetBlockHash());
builder.add(Api::BlockNotification::BlockHeight, index->nHeight);
index = index->pprev;
}
Message message(builder.message(Api::BlockNotificationService, Api::BlockNotification::BlocksRemoved));
for (auto &subinfo : list) {
subinfo->connection.send(message);
2019-03-10 14:00:36 +01:00
}
}
2019-04-09 17:24:38 +02:00
void BlockNotificationService::onIncomingMessage(Remote *remote_, const Message &message, const EndPoint &ep)
2019-03-10 14:00:36 +01:00
{
assert(dynamic_cast<RemoteWithBool*>(remote_));
RemoteWithBool *remote = static_cast<RemoteWithBool*>(remote_);
2019-03-10 14:00:36 +01:00
if (message.messageId() == Api::BlockNotification::Subscribe) {
logInfo(Log::BlockNotifactionService) << "Remote" << ep.connectionId << "Wants to hear about blocks";
remote->enabled = true;
2019-03-10 14:00:36 +01:00
}
else if (message.messageId() == Api::BlockNotification::Unsubscribe)
remote->enabled = false;
2019-03-10 14:00:36 +01:00
}