2020-04-17 19:33:06 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-02-02 13:08:07 +01:00
|
|
|
* Copyright (C) 2020 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/>.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CONNECTIONMANAGER_h
|
|
|
|
|
#define CONNECTIONMANAGER_h
|
|
|
|
|
|
|
|
|
|
#include "PeerAddressDB.h"
|
|
|
|
|
|
|
|
|
|
#include <networkmanager/NetworkManager.h>
|
|
|
|
|
#include <primitives/FastTransaction.h>
|
|
|
|
|
|
|
|
|
|
#include <boost/asio/deadline_timer.hpp>
|
|
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
|
class DownloadManager;
|
|
|
|
|
class PrivacySegment;
|
|
|
|
|
class Peer;
|
|
|
|
|
class uint256;
|
2020-06-08 21:35:10 +02:00
|
|
|
class BroadcastTxData;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
namespace Streaming {
|
|
|
|
|
class BufferPool;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/**
|
|
|
|
|
* @brief The ConnectionManager class owns all the Peer objects and handles their lifespan.
|
|
|
|
|
*/
|
2020-04-17 19:33:06 +02:00
|
|
|
class ConnectionManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-05-11 12:49:10 +02:00
|
|
|
ConnectionManager(boost::asio::io_service &service, const boost::filesystem::path &basedir, DownloadManager *parent);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
// return a pool for the current thread;
|
|
|
|
|
Streaming::BufferPool &pool(int reserveSize);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// create a new Peer for address, if it isn't already connected.
|
2020-04-17 19:33:06 +02:00
|
|
|
void connect(PeerAddress &address);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// disconnect this peer.
|
2020-05-06 10:42:58 +02:00
|
|
|
void disconnect(const std::shared_ptr<Peer> &peer);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// the network services we support.
|
2020-04-17 19:33:06 +02:00
|
|
|
uint64_t servicesBitfield() const;
|
2020-05-11 17:42:44 +02:00
|
|
|
/// set the network services we support.
|
2020-04-17 19:33:06 +02:00
|
|
|
void setServicesBitfield(const uint64_t &servicesBitfield);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Sync-height, cached.
|
2020-04-17 19:33:06 +02:00
|
|
|
int blockHeight() const;
|
2020-05-11 17:42:44 +02:00
|
|
|
/// You probably should not call this. Its for the Blockchain
|
2020-04-17 19:33:06 +02:00
|
|
|
void setBlockHeight(int blockHeight);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Return the blockheight for the argument block-hash.
|
2020-04-17 19:33:06 +02:00
|
|
|
int blockHeightFor(const uint256 &blockId);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// return the blockhash for a certain block-height.
|
2020-04-17 19:33:06 +02:00
|
|
|
uint256 blockHashFor(int height);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// a randomly generated nonce, to avoid connecting to self.
|
2020-04-17 19:33:06 +02:00
|
|
|
uint64_t appNonce() const;
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// slot that peers call to notify us they connected and finished handshake.
|
2020-05-06 10:42:58 +02:00
|
|
|
void connectionEstablished(const std::shared_ptr<Peer> &peer);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// A peer sends us blockheaders it received.
|
2020-04-17 19:33:06 +02:00
|
|
|
void addBlockHeaders(const Message &message, int sourcePeerId);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// A peer sends up addresses it receved.
|
2020-04-17 19:33:06 +02:00
|
|
|
void addAddresses(const Message &message, int sourcePeerId);
|
2020-05-11 17:42:44 +02:00
|
|
|
// A peer sends us INV messages it received.
|
2020-04-17 19:33:06 +02:00
|
|
|
void addInvMessage(const Message &message, int sourcePeerId);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// A peer sends us a Transaction it received
|
2020-04-17 19:33:06 +02:00
|
|
|
void addTransaction(const Tx &message, int sourcePeerId);
|
|
|
|
|
|
|
|
|
|
inline boost::asio::io_service &service() {
|
|
|
|
|
return m_ioService;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Punish a peer after detecting misbehavior.
|
2020-05-07 15:38:54 +02:00
|
|
|
bool punish(const std::shared_ptr<Peer> &peer, int amount = 250);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// conveninience overload
|
2020-05-07 15:38:54 +02:00
|
|
|
bool punish(int connectionId, int amount = 250);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Send a request to peer for headers, to identify their chain.
|
2020-05-06 10:42:58 +02:00
|
|
|
void requestHeaders(const std::shared_ptr<Peer> &peer);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// returns a list of connected peers.
|
2020-05-05 22:53:25 +02:00
|
|
|
std::deque<std::shared_ptr<Peer> > connectedPeers() const;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-11-05 21:47:54 +01:00
|
|
|
int unconnectedPeerCount() const;
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Share the peer addresses DB
|
2020-04-17 19:33:06 +02:00
|
|
|
inline const PeerAddressDB &peerAddressDb() const {
|
|
|
|
|
return m_peerAddressDb;
|
|
|
|
|
}
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Share the peer addresses DB
|
2020-04-17 19:33:06 +02:00
|
|
|
inline PeerAddressDB &peerAddressDb() {
|
|
|
|
|
return m_peerAddressDb;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Return a peer by connection-id.
|
2020-05-05 22:53:25 +02:00
|
|
|
std::shared_ptr<Peer> peer(int connectionId) const;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// register a privacy segment to be assigned to peers.
|
2020-04-17 19:33:06 +02:00
|
|
|
void addPrivacySegment(PrivacySegment *ps);
|
2020-05-11 17:42:44 +02:00
|
|
|
/// remove a privacy segment from our list. No existing peers will be affected.
|
2020-04-17 19:33:06 +02:00
|
|
|
void removePrivacySegment(PrivacySegment *ps);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Note the network-identifying string we will announce ourself as.
|
2020-04-17 19:33:06 +02:00
|
|
|
void setUserAgent(const std::string &userAgent);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// returns our network-identifying string we will announce ourself as.
|
2020-04-17 19:33:06 +02:00
|
|
|
inline const std::string &userAgent() const {
|
|
|
|
|
return m_userAgent;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 16:56:14 +01:00
|
|
|
/**
|
|
|
|
|
* Allow apps to broadcast a transaction to peers.
|
|
|
|
|
*
|
|
|
|
|
* This method takes a BroadcastTxData which has several reasons:
|
|
|
|
|
* - It combines the actual transaction and the private segment.
|
|
|
|
|
* - It gives the ConnectionManager some call-backs to report
|
|
|
|
|
* success or failure.
|
|
|
|
|
* - It allows the caller of this method to set the lifetime
|
|
|
|
|
* of the broadcast order by simply deleting the txOwner when
|
|
|
|
|
* it wants to stop the broadcast.
|
|
|
|
|
*
|
|
|
|
|
* This class will keep a weak-pointer to the txOwner only, so lifetime
|
|
|
|
|
* management lies with the caller.
|
|
|
|
|
*/
|
2020-06-08 21:35:10 +02:00
|
|
|
void broadcastTransaction(const std::shared_ptr<BroadcastTxData> &txOwner);
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// returns the amount of peers we currently have. Even unconnected ones.
|
2020-04-26 16:20:45 +02:00
|
|
|
int peerCount() const;
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// returns a copy of the segments list we hold.
|
2020-04-17 19:33:06 +02:00
|
|
|
std::deque<PrivacySegment *> segments() const;
|
|
|
|
|
|
2020-05-11 17:42:44 +02:00
|
|
|
/// Shut down this connection manager, and the peers as well as connections.
|
|
|
|
|
/// It is required to call this prior to calling the destructor in order to
|
|
|
|
|
/// cleanly shut down the system and stop all tasks in all threads.
|
2020-04-17 19:33:06 +02:00
|
|
|
void shutdown();
|
|
|
|
|
|
2020-05-09 19:58:44 +02:00
|
|
|
/**
|
|
|
|
|
* @brief setMessageQueueSize allows a configuration of how many buffers a connection should have.
|
|
|
|
|
* @param size the amount of messages we queue. The variable should be positive and fit in a `short` integer.
|
|
|
|
|
*
|
|
|
|
|
* @see NetworkConnection::setMessageQueueSizes
|
|
|
|
|
*
|
|
|
|
|
* Notice that this only affects newly created connections.
|
|
|
|
|
*/
|
|
|
|
|
void setMessageQueueSize(int size);
|
|
|
|
|
|
2021-02-03 14:38:05 +01:00
|
|
|
/**
|
|
|
|
|
* Assign a privacy-segment to /a peer.
|
|
|
|
|
*/
|
|
|
|
|
void assignSegmentToPeer(const std::shared_ptr<Peer> &peer);
|
|
|
|
|
|
2021-07-30 14:04:58 +02:00
|
|
|
const std::deque<std::weak_ptr<BroadcastTxData> > &transactionsToBroadcast() const;
|
|
|
|
|
|
2020-04-17 19:33:06 +02:00
|
|
|
private:
|
|
|
|
|
void cron(const boost::system::error_code &error);
|
|
|
|
|
void handleError(int remoteId, const boost::system::error_code &error);
|
2020-04-26 16:20:45 +02:00
|
|
|
void handleError_impl(int remoteId, const boost::system::error_code &error);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
// m_lock should already be taken by caller
|
2020-05-06 10:42:58 +02:00
|
|
|
void removePeer(const std::shared_ptr<Peer> &peer);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
uint64_t m_appNonce;
|
|
|
|
|
uint64_t m_servicesBitfield = 0;
|
|
|
|
|
int m_blockHeight = 0;
|
|
|
|
|
|
2020-05-09 19:58:44 +02:00
|
|
|
short m_queueSize = 200; // config setting for the NetworkConnection buffer-size
|
|
|
|
|
|
2020-04-17 19:33:06 +02:00
|
|
|
mutable std::mutex m_lock;
|
|
|
|
|
std::atomic<bool> m_shuttingDown;
|
2020-05-05 22:53:25 +02:00
|
|
|
std::map<int, std::shared_ptr<Peer>> m_peers;
|
2020-04-17 19:33:06 +02:00
|
|
|
std::set<int> m_connectedPeers;
|
|
|
|
|
|
|
|
|
|
boost::asio::io_service &m_ioService;
|
|
|
|
|
boost::asio::deadline_timer m_cronTimer;
|
|
|
|
|
PeerAddressDB m_peerAddressDb;
|
2020-05-09 19:58:44 +02:00
|
|
|
NetworkManager m_network;
|
2020-04-17 19:33:06 +02:00
|
|
|
DownloadManager *m_dlManager; // parent
|
|
|
|
|
std::string m_userAgent;
|
2020-05-11 12:49:10 +02:00
|
|
|
boost::filesystem::path m_basedir;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
std::deque<PrivacySegment*> m_segments;
|
2020-06-08 21:35:10 +02:00
|
|
|
std::deque<std::weak_ptr<BroadcastTxData> > m_transactionsToBroadcast;
|
2020-04-17 19:33:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|