Files
thehub/libs/p2p/PrivacySegment.h
T

151 lines
5.1 KiB
C++
Raw Permalink Normal View History

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 PRIVACYSEGMENT_H
#define PRIVACYSEGMENT_H
#include "BlockHeader.h"
#include <utils/PartialMerkleTree.h>
#include <utils/bloom.h>
#include <deque>
2020-05-18 09:35:51 +02:00
#include <mutex>
class CKeyID;
2020-04-17 19:33:06 +02:00
class Tx;
class Message;
class DataListenerInterface;
2020-05-18 09:35:51 +02:00
class PrivacySegmentListener;
2020-04-17 19:33:06 +02:00
/**
* A wallet can split its funds into different privacy segments.
* The effect is that backing resources will be allocated for each
* segment and details will be cordoned off.
*
* A bloom filter, for instance, is known to allow combining of addresses
* with higher probablity than we initially thought.
* The simple solution to this is to not use the same bloom filter for
* addresses that should be separated.
*
* The privacy segment is intended to be assigned to a certain set of
* addresses in the wallet and the P2PNet library makes sure that we never
* mix the segments when talking to the individual peers on the Bitcoin network.
*/
class PrivacySegment
{
public:
explicit PrivacySegment(uint16_t id, DataListenerInterface *parent = nullptr);
2020-10-19 14:04:57 +02:00
/// The priority of a segmment in the wider system.
/// This decides the order in which peers are assigned to privacy segments.
enum Priority {
First, ///< Highest priority
Normal,
2021-07-30 14:03:29 +02:00
Last,
2020-10-19 14:04:57 +02:00
OnlyManual ///< Never auto-connect, only when specifically asked.
};
2020-04-17 19:33:06 +02:00
uint16_t segmentId() const;
2020-05-18 09:35:51 +02:00
struct FilterLock {
FilterLock(FilterLock && other);
~FilterLock();
private:
friend class PrivacySegment;
FilterLock(PrivacySegment *parent);
PrivacySegment *parent;
};
/* clears the bloom filter, to allow adding addresses and outputs to it again.
* This returns a FilterLock that will keep the mutex locked for the duration
* of its scope.
*
* The safe way to update the filter is something like this:
* @code
* { // lock scope
* auto lock = segment->clearFilter(),
* segment->addToFilter(something);
* }
*
* Additionally, as the FilterLock destructor is called it will push out an update to any listeners.
*/
FilterLock clearFilter();
2020-06-07 13:56:55 +02:00
void addToFilter(const uint256 &prevHash, int outIndex);
2020-05-18 09:35:51 +02:00
2020-04-17 19:33:06 +02:00
/**
* @brief addToFilter allows you to get updates for a specific address.
* @param address The address to add.
* @param blockHeight the blockHeight the address was created at, first one we look at to get updates for data.
*/
void addToFilter(const std::string &address, int blockHeight);
2020-05-18 09:35:51 +02:00
/**
2020-06-07 13:56:55 +02:00
* Add public-key-hash directly instead of an address.
*/
2020-06-07 13:56:55 +02:00
void addKeyToFilter(const CKeyID &address, int blockHeight);
2020-04-17 19:33:06 +02:00
Streaming::ConstBuffer writeFilter(Streaming::BufferPool &pool) const;
int firstBlock() const;
/// set the block a peer just synchronized (received and verified)
void blockSynched(int height);
/// returns the last block that was synched
int lastBlockSynched() const;
/// a backup peer doing a second sync has reached this height
int backupSyncHeight() const;
/**
* @brief newTransactions announces a list of transactions pushed to us from a peer.
* @param header the block header these transactions appeared in.
* @param blockHeight the blockheight we know the header under.
* @param blockTransactions The actual transactions.
*/
void newTransactions(const BlockHeader &header, int blockHeight, const std::deque<Tx> &blockTransactions);
/// A single transaction that matches our filters, forwarded to us as it hits a mempool.
2020-04-17 19:33:06 +02:00
void newTransaction(const Tx &tx);
int filterChangedHeight() const;
const CBloomFilter &bloomFilter() const;
2020-04-17 19:33:06 +02:00
2020-05-18 09:35:51 +02:00
void addListener(PrivacySegmentListener *listener);
void removeListener(PrivacySegmentListener *listener);
2020-10-19 14:04:57 +02:00
/// The priority of a segmment in the wider system.
/// This decides the order in which peers are assigned to privacy segments.
Priority priority() const;
/// Return the priority
void setPriority(const Priority &priority);
2020-04-17 19:33:06 +02:00
private:
2020-05-18 09:35:51 +02:00
const uint16_t m_segmentId = 0;
mutable std::recursive_mutex m_lock;
std::deque<PrivacySegmentListener*> m_listeners;
2020-04-17 19:33:06 +02:00
int m_firstBlock = -1; ///< first block we need to investigate
CBloomFilter m_bloom;
DataListenerInterface *m_parent;
int m_merkleBlockHeight = -1;
int m_filterChangedHeight = 0;
int m_softMerkleBlockHeight = -1;
2020-10-19 14:04:57 +02:00
Priority m_priority = Normal;
2020-04-17 19:33:06 +02:00
};
#endif