Files
thehub/libs/p2p/DownloadManager.cpp
T

462 lines
16 KiB
C++
Raw Permalink Normal View History

2020-04-17 19:33:06 +02:00
/*
* This file is part of the Flowee project
2025-03-07 14:31:02 +01:00
* Copyright (C) 2020-2025 Tom Zander <tom@flowee.org>
2020-04-17 19:33:06 +02: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 "DownloadManager.h"
2023-01-31 16:46:30 +01:00
#include "DataListenerInterface.h"
2020-04-17 19:33:06 +02:00
#include "FillAddressDBAction.h"
2023-01-31 16:46:30 +01:00
#include "HeaderSyncInterface.h"
2020-04-17 19:33:06 +02:00
#include "InventoryItem.h"
#include "P2PNetInterface.h"
#include "Peer.h"
#include "SyncChainAction.h"
#include "SyncSPVAction.h"
2021-02-03 14:35:06 +01:00
#include "CleanPeersAction.h"
2020-04-17 19:33:06 +02:00
#include <primitives/Tx.h>
2020-04-17 19:33:06 +02:00
#include <streaming/P2PParser.h>
#include <streaming/P2PBuilder.h>
2022-01-24 12:06:37 +01:00
#include <streaming/BufferPools.h>
2020-04-17 19:33:06 +02:00
2025-02-08 19:05:26 +01:00
DownloadManager::DownloadManager(boost::asio::io_context &context, const boost::filesystem::path &basedir, P2PNet::Chain chain)
: m_strand(context),
2020-10-29 21:47:53 +01:00
m_chain(chain),
2026-01-13 15:55:08 +01:00
m_connectionManager(context, basedir, this),
2020-10-29 21:47:53 +01:00
m_blockchain(this, basedir, chain),
2020-04-17 19:33:06 +02:00
m_shuttingDown(false)
{
m_connectionManager.setBlockHeight(m_blockchain.height());
m_isBehind = !isChainUpToDate();
2021-01-13 12:00:15 +01:00
// create basedir, and fail-fast if we don't have writing rights to do that.
try {
boost::filesystem::create_directories(basedir);
} catch (const boost::filesystem::filesystem_error&) {
if (!boost::filesystem::exists(basedir) || !boost::filesystem::is_directory(basedir)) {
logFatal() << "Failed to create datadir" << basedir.string();
throw;
}
// errors like "already exists" are safe to ignore.
}
2020-04-17 19:33:06 +02:00
}
const ConnectionManager &DownloadManager::connectionManager() const
{
return m_connectionManager;
}
ConnectionManager &DownloadManager::connectionManager()
{
return m_connectionManager;
}
void DownloadManager::headersDownloadFinished(int newBlockHeight, int peerId)
{
if (m_shuttingDown)
return;
// assert(m_strand.running_in_this_thread()); // disabled to make usable in unit test
2020-11-13 20:11:06 +01:00
// The blockchain lets us know the result of a successful headers-download.
2020-04-17 19:33:06 +02:00
if (m_peerDownloadingHeaders == peerId)
m_peerDownloadingHeaders = -1;
2020-05-05 22:53:25 +02:00
auto peer = m_connectionManager.peer(peerId);
2020-11-13 20:11:06 +01:00
if (peer.get()) {
peer->peerAddress().gotGoodHeaders();
2020-11-13 20:11:06 +01:00
peer->updatePeerHeight(newBlockHeight);
}
2020-04-17 19:33:06 +02:00
m_connectionManager.setBlockHeight(newBlockHeight);
2020-10-29 22:18:10 +01:00
getMoreHeaders();
2023-01-31 16:46:30 +01:00
for (auto iface : m_headerSyncListeners) {
iface->setHeaderSyncHeight(newBlockHeight);
2020-04-17 19:33:06 +02:00
}
if (!m_isBehind) // don't flood the user during initial sync.
m_notifications.notifyNewBlock(newBlockHeight);
if (m_isBehind && isChainUpToDate()) {
m_isBehind = false;
2023-01-31 16:46:30 +01:00
for (auto iface : m_headerSyncListeners) {
iface->headerSyncComplete();
}
}
2020-04-17 19:33:06 +02:00
addAction<SyncSPVAction>();
}
2020-10-29 22:18:10 +01:00
void DownloadManager::getMoreHeaders()
{
if (m_peerDownloadingHeaders == -1) { // check if we need to download more of them.
// TODO use the fastest peer.
2021-02-05 17:29:01 +01:00
for (auto &p : m_connectionManager.connectedPeers()) {
2020-10-29 22:18:10 +01:00
if (p->startHeight() > blockHeight()) {
m_peerDownloadingHeaders = p->connectionId();
m_connectionManager.requestHeaders(p);
return;
}
}
}
}
2020-04-17 19:33:06 +02:00
void DownloadManager::parseInvMessage(Message message, int sourcePeerId)
{
if (m_shuttingDown)
return;
2020-11-13 20:11:06 +01:00
// this is called as a result of an INV received by a peer.
// We check this and insert into the m_downloadQueue a target to download.
2020-04-17 19:33:06 +02:00
try {
Streaming::P2PParser parser(message);
const size_t count = parser.readCompactInt();
2023-08-17 16:58:32 +02:00
logDebug() << "Received" << count << "Inv messages from" << sourcePeerId;
2020-04-17 19:33:06 +02:00
std::unique_lock<std::mutex> lock(m_downloadsLock);
for (size_t i = 0; i < count; ++i) {
uint32_t type = parser.readInt();
auto inv = InventoryItem(parser.readUint256(), type);
2021-02-04 17:40:06 +01:00
// if block type, check if we already know about it
2020-11-13 20:11:06 +01:00
if (type == InventoryItem::BlockType) {
auto height = m_blockchain.blockHeightFor(inv.hash());
2023-08-17 16:58:32 +02:00
logDebug() << " \\- inv is of 'block' type." << inv.hash();
2020-11-13 20:11:06 +01:00
if (height > 0) {
2023-08-17 16:58:32 +02:00
// a block-inv we already have seen and approved.
2020-11-13 20:11:06 +01:00
auto peer = m_connectionManager.peer(sourcePeerId);
if (peer)
peer->updatePeerHeight(height);
// no need for further action.
continue;
}
}
2021-02-04 17:40:06 +01:00
2023-08-17 16:58:32 +02:00
// we update the downloads queue
2021-02-04 17:40:06 +01:00
if (type == InventoryItem::TransactionType || type == InventoryItem::BlockType
|| type == InventoryItem::DoubleSpendType) {
2020-04-17 19:33:06 +02:00
auto findIter = m_downloadTargetIds.find(inv.hash());
if (findIter == m_downloadTargetIds.end()) {
2023-08-17 16:58:32 +02:00
#ifndef BCH_NO_DEBUG_OUTPUT
auto debug = logDebug() << " \\- inv is";
if (type ==InventoryItem::TransactionType)
debug << "Tx" << inv.hash();
else if (type ==InventoryItem::DoubleSpendType)
debug << "DSProof";
else
debug << "block";
#endif
2020-04-17 19:33:06 +02:00
// new download target.
DownloadTarget dlt(inv);
dlt.sourcePeers.push_back(sourcePeerId);
m_downloadQueue.insert(std::make_pair(m_nextDownloadTarget, dlt));
m_downloadTargetIds.insert(std::make_pair(inv.hash(), m_nextDownloadTarget++));
} else {
2023-08-17 16:58:32 +02:00
logDebug() << " \\- inv is duplicate";
2020-04-17 19:33:06 +02:00
// add source to existing one
auto targetIter = m_downloadQueue.find(findIter->second);
assert(targetIter != m_downloadQueue.end());
targetIter->second.sourcePeers.push_back(sourcePeerId);
}
}
}
} catch (const std::exception &e) {
logInfo() << "Inv messsage parsing failed" << e << "peer:" << sourcePeerId;
2023-05-08 11:34:47 +02:00
m_connectionManager.punish(sourcePeerId, 250);
2020-04-17 19:33:06 +02:00
}
logDebug() << " Queue size now" << m_downloadQueue.size();
// call runQueue in a next event.
2025-02-11 16:46:21 +01:00
m_strand.post(std::bind(&DownloadManager::runQueue, this), std::allocator<void>());
2020-04-17 19:33:06 +02:00
}
void DownloadManager::parseTransaction(Tx tx, int sourcePeerId)
{
2020-11-13 20:11:06 +01:00
// we get called by the peer about a transaction just received.
// Now we find the downloads data that requested it and update
// m_downloads and m_downloadTargetIds and m_downloadQueue.
2020-04-17 19:33:06 +02:00
auto hash = tx.createHash();
std::unique_lock<std::mutex> lock(m_downloadsLock);
const size_t downloadSlots = m_downloads.size();
bool found = false;
for (size_t i = 0; i < downloadSlots; ++i) {
if (m_downloads[i].primary == sourcePeerId
|| m_downloads[i].secondary == sourcePeerId) {
auto dlIter = m_downloadQueue.find(m_downloads[i].targetId);
assert(dlIter != m_downloadQueue.end());
if (dlIter->second.inv.type() == InventoryItem::TransactionType
&& dlIter->second.inv.hash() == hash) {
// mark download complete.
auto tIter = m_downloadTargetIds.find(dlIter->second.inv.hash());
if (m_downloadTargetIds.end() != tIter)
m_downloadTargetIds.erase(tIter);
m_downloads[i] = ActiveDownload();
m_downloadQueue.erase(dlIter);
found = true;
break;
}
}
}
if (!found) {
logWarning() << "Peer" << sourcePeerId << "sent unsolicited tx. This breaks protocol";
m_connectionManager.punish(sourcePeerId, 34);
}
try {
for (auto iface : m_dataListeners) {
iface->newTransaction(tx);
}
} catch (const std::exception &e) {
// assume that anything wrong happening in the interface is our fault for not checking the
// validity of the transaction.
// Then we just blame the source peer for providing us with bad data.
m_connectionManager.punish(sourcePeerId, 501);
}
}
void DownloadManager::peerDisconnected(int connectionId)
{
if (connectionId == m_peerDownloadingHeaders)
m_peerDownloadingHeaders = -1;
}
void DownloadManager::reportDataFailure(int connectionId)
{
m_connectionManager.punish(connectionId, 1001);
}
void DownloadManager::done(Action *action)
{
std::unique_lock<std::mutex> lock(m_lock);
for (auto iter = m_runningActions.begin(); iter != m_runningActions.end(); ++iter) {
if (*iter == action) {
m_runningActions.erase(iter);
delete action;
return;
}
}
}
void DownloadManager::addDataListener(DataListenerInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
for (auto iface : m_dataListeners) {
if (iface == listener)
return;
}
m_dataListeners.push_back(listener);
}
void DownloadManager::removeDataListener(DataListenerInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
for (auto iter = m_dataListeners.begin(); iter != m_dataListeners.end(); ++iter) {
if (*iter == listener) {
m_dataListeners.erase(iter);
return;
}
}
}
void DownloadManager::addP2PNetListener(P2PNetInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
2023-01-31 16:46:30 +01:00
for (auto iface : m_netListeners) {
2020-04-17 19:33:06 +02:00
if (iface == listener)
return;
}
2023-01-31 16:46:30 +01:00
m_netListeners.push_back(listener);
2020-04-17 19:33:06 +02:00
}
void DownloadManager::removeP2PNetListener(P2PNetInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
2023-01-31 16:46:30 +01:00
for (auto iter = m_netListeners.begin(); iter != m_netListeners.end(); ++iter) {
2020-04-17 19:33:06 +02:00
if (*iter == listener) {
2023-01-31 16:46:30 +01:00
m_netListeners.erase(iter);
2020-04-17 19:33:06 +02:00
return;
}
}
}
2023-01-31 16:46:30 +01:00
std::vector<P2PNetInterface *> DownloadManager::p2pNetListeners() const
2020-04-17 19:33:06 +02:00
{
2023-01-31 16:46:30 +01:00
std::unique_lock<std::mutex> lock(m_lock);
return m_netListeners;
}
void DownloadManager::addHeaderListener(HeaderSyncInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
for (auto iface : m_headerSyncListeners) {
if (iface == listener)
return;
}
m_headerSyncListeners.push_back(listener);
}
void DownloadManager::removeHeaderListener(HeaderSyncInterface *listener)
{
std::unique_lock<std::mutex> lock(m_lock);
for (auto iter = m_headerSyncListeners.begin(); iter != m_headerSyncListeners.end(); ++iter) {
if (*iter == listener) {
m_headerSyncListeners.erase(iter);
return;
}
}
2020-04-17 19:33:06 +02:00
}
void DownloadManager::shutdown()
{
std::unique_lock<std::mutex> lock(m_lock);
if (m_shuttingDown)
return;
m_shuttingDown = true;
for (auto a : m_runningActions) {
a->cancel();
}
m_connectionManager.shutdown();
2022-11-11 19:25:19 +01:00
m_blockchain.save();
2026-01-13 15:55:08 +01:00
// Post an action on the strand that when it is executed means the strand-queue is done
2025-02-11 16:46:21 +01:00
m_strand.post(std::bind(&DownloadManager::finishShutdown, this), std::allocator<void>());
2020-04-17 19:33:06 +02:00
m_waitVariable.wait(lock);
}
2022-11-11 19:25:19 +01:00
void DownloadManager::saveData()
{
m_connectionManager.saveData();
m_blockchain.save();
}
2020-04-17 19:33:06 +02:00
void DownloadManager::finishShutdown()
{
assert(m_shuttingDown);
std::unique_lock<std::mutex> lock(m_lock);
m_waitVariable.notify_all();
}
2020-10-29 21:47:53 +01:00
P2PNet::Chain DownloadManager::chain() const
{
return m_chain;
}
2020-04-17 19:33:06 +02:00
void DownloadManager::runQueue()
{
if (m_shuttingDown)
return;
std::unique_lock<std::mutex> lock(m_downloadsLock);
auto iter = m_downloadQueue.begin();
const size_t downloadSlots = m_downloads.size();
for (size_t i = 0; i < downloadSlots; ++i) {
if (m_downloads[i].targetId == 0) { // slot unoccupied.
2020-11-13 20:11:06 +01:00
// iterate through downloadqueue to find a new job
2020-04-17 19:33:06 +02:00
while (true) {
if (iter == m_downloadQueue.end())
return; // nothing left to download
const DownloadTarget &dt = iter->second;
bool alreadyRunning = false;
for (size_t x = 0; !alreadyRunning && x < downloadSlots; ++x) {
// first check if nobody is downloading this one yet.
alreadyRunning = m_downloads[x].targetId == iter->first;
}
if (alreadyRunning) {
++iter;
continue;
}
2023-03-27 15:19:05 +02:00
if (dt.inv.type() == InventoryItem::BlockType) { // remove duplicates
2020-11-13 20:11:06 +01:00
auto height = m_blockchain.blockHeightFor(dt.inv.hash());
if (height > 0) {
// hash already known.
// No need to download it.
2020-04-17 19:33:06 +02:00
auto tIter = m_downloadTargetIds.find(dt.inv.hash());
if (m_downloadTargetIds.end() != tIter)
m_downloadTargetIds.erase(tIter);
2020-11-13 20:11:06 +01:00
// let the peer know the height that is related to the INV
for (auto id : iter->second.sourcePeers) {
auto peer = m_connectionManager.peer(id);
if (peer)
peer->updatePeerHeight(height);
}
2020-04-17 19:33:06 +02:00
iter = m_downloadQueue.erase(iter);
continue;
}
}
assert(!dt.sourcePeers.empty());
int preferredDownload = -1;
for (auto peerId : dt.sourcePeers) {
// find a peer we assign the download to.
if (preferredDownload == -1) {
preferredDownload = peerId;
} else {
bool inUse = false;
for (size_t x = 0; !inUse && x < downloadSlots; ++x) {
if (m_downloads[x].primary == peerId || m_downloads[x].secondary == peerId) {
inUse = true;
break;
}
}
if (!inUse) {
break;
preferredDownload = peerId;
}
}
}
auto peer = m_connectionManager.peer(preferredDownload);
assert(peer);
if (peer) {
logInfo() << "Requesting DL for inv from peer:" << preferredDownload;
2023-08-17 16:58:32 +02:00
logDebug() << " + inv:" << dt.inv.hash();
2020-04-17 19:33:06 +02:00
m_downloads[i].targetId = iter->first;
m_downloads[i].downloadStartTime = time(nullptr);
m_downloads[i].primary = preferredDownload;
2021-02-04 17:40:06 +01:00
if (dt.inv.type() == InventoryItem::TransactionType || dt.inv.type() == InventoryItem::DoubleSpendType) {
2022-01-24 12:06:37 +01:00
Streaming::P2PBuilder builder(Streaming::pool(40));
2020-04-17 19:33:06 +02:00
builder.writeCompactSize(1);
builder.writeInt(dt.inv.type());
builder.writeByteArray(dt.inv.hash(), Streaming::RawBytes);
peer->sendMessage(builder.message(Api::P2P::GetData));
}
2020-11-13 20:11:06 +01:00
else if (dt.inv.type() == InventoryItem::BlockType) {
2020-04-17 19:33:06 +02:00
m_connectionManager.requestHeaders(peer);
}
}
break;
}
}
}
}
2025-03-07 14:31:02 +01:00
P2PNet::PowerMode DownloadManager::powerMode() const
{
return m_powerMode;
}
void DownloadManager::setPowerMode(P2PNet::PowerMode newPowerMode)
{
m_powerMode = newPowerMode;
m_connectionManager.onPowerModeChanged();
}
2020-04-17 19:33:06 +02:00
void DownloadManager::start()
{
2025-03-07 14:31:02 +01:00
if (m_powerMode == P2PNet::NormalPower) {
addAction<SyncSPVAction>();
addAction<CleanPeersAction>();
// do these last, so they reuse connections started by the syncSPV one.
addAction<FillAddressDBAction>();
addAction<SyncChainAction>();
}
2020-04-17 19:33:06 +02:00
}