2019-03-10 14:00:36 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2018-2019 Tom Zander <tomz@freedommail.ch>
|
|
|
|
|
*
|
|
|
|
|
* 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"
|
2019-03-29 20:55:37 +01:00
|
|
|
#include <APIProtocol.h>
|
2019-03-10 14:00:36 +01:00
|
|
|
|
|
|
|
|
#include <Logger.h>
|
|
|
|
|
#include <Message.h>
|
|
|
|
|
#include <chain.h>
|
|
|
|
|
|
|
|
|
|
#include <streaming/MessageBuilder.h>
|
|
|
|
|
#include <streaming/MessageParser.h>
|
|
|
|
|
|
|
|
|
|
// #include "Application.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BlockNotificationService::SyncAllTransactionsInBlock(const FastBlock &, CBlockIndex *index)
|
|
|
|
|
{
|
2019-05-18 15:56:34 +02:00
|
|
|
const auto remotes_ = remotes();
|
|
|
|
|
if (remotes_.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));
|
|
|
|
|
|
2019-05-18 15:56:34 +02:00
|
|
|
for (auto remote : remotes_) {
|
2019-03-10 14:00:36 +01:00
|
|
|
RemoteSubscriptionInfo *subinfo = static_cast<RemoteSubscriptionInfo*>(remote);
|
|
|
|
|
if (subinfo->m_wantsNewBlockHashes)
|
|
|
|
|
subinfo->connection.send(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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<RemoteSubscriptionInfo*>(remote_));
|
|
|
|
|
RemoteSubscriptionInfo *remote = static_cast<RemoteSubscriptionInfo*>(remote_);
|
|
|
|
|
if (message.messageId() == Api::BlockNotification::Subscribe) {
|
|
|
|
|
logInfo(Log::BlockNotifactionService) << "Remote" << ep.connectionId << "Wants to hear about blocks";
|
|
|
|
|
remote->m_wantsNewBlockHashes = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (message.messageId() == Api::BlockNotification::Unsubscribe)
|
|
|
|
|
remote->m_wantsNewBlockHashes = false;
|
|
|
|
|
}
|