2020-04-17 19:33:06 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2023-01-30 17:10:02 +01:00
|
|
|
* Copyright (C) 2020-2023 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>
|
|
|
|
|
|
2020-05-16 10:30:55 +02:00
|
|
|
#include <deque>
|
2020-05-18 09:35:51 +02:00
|
|
|
#include <mutex>
|
2020-05-16 10:30:55 +02:00
|
|
|
|
2022-07-06 21:52:47 +02:00
|
|
|
class KeyId;
|
2020-04-17 19:33:06 +02:00
|
|
|
class Tx;
|
|
|
|
|
class Message;
|
|
|
|
|
class DataListenerInterface;
|
2023-03-29 16:00:25 +02:00
|
|
|
class Peer;
|
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:
|
2023-01-31 16:06:25 +01:00
|
|
|
explicit PrivacySegment(uint16_t id, DataListenerInterface *parent);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2020-10-19 14:04:57 +02:00
|
|
|
/// The priority of a segmment in the wider system.
|
2023-03-27 15:19:23 +02:00
|
|
|
/// This decides the order in which peers are assigned to this privacy segments.
|
2020-10-19 14:04:57 +02:00
|
|
|
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();
|
2023-03-27 15:19:23 +02:00
|
|
|
|
|
|
|
|
void addMempoolRequest();
|
2020-05-18 09:35:51 +02:00
|
|
|
private:
|
|
|
|
|
friend class PrivacySegment;
|
|
|
|
|
FilterLock(PrivacySegment *parent);
|
|
|
|
|
PrivacySegment *parent;
|
2023-03-27 15:19:23 +02:00
|
|
|
bool shouldAddMempoolRequest = false;
|
2020-05-18 09:35:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* 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-05-15 17:18:42 +02:00
|
|
|
|
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-05-15 17:18:42 +02:00
|
|
|
/**
|
2020-06-07 13:56:55 +02:00
|
|
|
* Add public-key-hash directly instead of an address.
|
2020-05-15 17:18:42 +02:00
|
|
|
*/
|
2022-07-06 21:52:47 +02:00
|
|
|
void addKeyToFilter(const KeyId &address, int blockHeight);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
Streaming::ConstBuffer writeFilter(Streaming::BufferPool &pool) const;
|
|
|
|
|
|
2023-01-30 17:10:02 +01:00
|
|
|
/**
|
|
|
|
|
* Return the blockheight when this segment was born.
|
|
|
|
|
* This privacy segment gets details in a bloom filter which allows
|
|
|
|
|
* it to use merkleblocks and follow the blockchain. One main ingredient here
|
|
|
|
|
* is the first block height of this segment as we know that nothing can be found
|
|
|
|
|
* before that date.
|
|
|
|
|
*
|
|
|
|
|
* The returned integer is the blockheight this segment was created at,
|
|
|
|
|
* in the case that we don't have this information we'll return -1.
|
|
|
|
|
*
|
|
|
|
|
* Cases where we don't have this info are that the block headers are not synched
|
|
|
|
|
* yet and we don't actually know the height. There may also simply be no keys
|
|
|
|
|
* loaded or created at all yet, for instance when the loading is user initiated.
|
|
|
|
|
*/
|
2020-04-17 19:33:06 +02:00
|
|
|
int firstBlock() const;
|
|
|
|
|
|
|
|
|
|
/// set the block a peer just synchronized (received and verified)
|
|
|
|
|
void blockSynched(int height);
|
2023-01-30 17:10:02 +01:00
|
|
|
/**
|
|
|
|
|
* This returns the last block that was synched.
|
|
|
|
|
*
|
|
|
|
|
* We return the height of the last synchronized block for this segment.
|
|
|
|
|
* Special value is:
|
|
|
|
|
* * -2 for not initialized wallets. This may be an empty wallet, never seen a block, or it is encrypted.
|
|
|
|
|
*/
|
2020-04-17 19:33:06 +02:00
|
|
|
int lastBlockSynched() const;
|
|
|
|
|
/// a backup peer doing a second sync has reached this height
|
|
|
|
|
int backupSyncHeight() const;
|
|
|
|
|
|
2020-05-16 10:30:55 +02:00
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
|
2021-05-23 17:51:45 +02:00
|
|
|
const CBloomFilter &bloomFilter() const;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
2023-03-29 16:00:25 +02:00
|
|
|
/// add peer for callbacks about filter changes
|
|
|
|
|
void addPeer(Peer *peer);
|
|
|
|
|
/// forget about a certain peer
|
|
|
|
|
void removePeer(Peer *peer);
|
2020-05-18 09:35:51 +02:00
|
|
|
|
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;
|
2022-08-17 19:28:42 +02:00
|
|
|
/// Sets the sync priority
|
2022-04-05 21:10:24 +02:00
|
|
|
void setPriority(Priority priority);
|
2020-10-19 14:04:57 +02:00
|
|
|
|
2023-03-27 15:19:23 +02:00
|
|
|
/// If true, this privacy segment is open for bloom / merkleblock updates.
|
2022-06-23 23:17:05 +02:00
|
|
|
bool enabled() const;
|
|
|
|
|
void setEnabled(bool newEnabled);
|
|
|
|
|
|
2023-03-29 16:00:25 +02:00
|
|
|
enum IsAtTip {
|
|
|
|
|
NotAtTip,
|
|
|
|
|
FilterAtTIp
|
|
|
|
|
};
|
|
|
|
|
void rebuildFilter(IsAtTip tip);
|
2023-03-27 15:19:23 +02:00
|
|
|
|
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;
|
2023-03-29 16:00:25 +02:00
|
|
|
std::deque<Peer*> m_peers;
|
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;
|
2022-06-23 23:17:05 +02:00
|
|
|
bool m_enabled = true;
|
2023-03-29 16:00:25 +02:00
|
|
|
bool m_filterShouldFollowWithMemoolCall = false;
|
2020-04-17 19:33:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|