70e4f2292e
Tuesdays idea of adding some code into the SyncSPVAction didn't feel right. A second look made clear that bloom filter updates make much more sense to go hand in hand with sending a mempool message. Especially since they depend on each other on the server side. To-rehash: the wallet may decide at any time that a new bloom filter is needed. It then uses the superclass (code in p2plib) PrivacySegment, to build that filter. As part of that we get a lock object which, when going out of scope, makes the peers that are subscribed to the privacySegment send out the filter. This separation of concerns means that the subclass wallet in the app doens't know about peers or messages, only its superclass PrivacySegment does. What we did in this change is make the PrivacySegment class decide to combine a bloom update with a mempool call. Typicall only once per connection. This means I can remove hacks in the SyncSPVAction which forced the sending of the mempool message separately.
240 lines
6.7 KiB
C++
240 lines
6.7 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2020-2023 Tom Zander <tom@flowee.org>
|
|
*
|
|
* 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 "PrivacySegment.h"
|
|
#include "PrivacySegmentListener.h"
|
|
#include "DataListenerInterface.h"
|
|
|
|
#include <crypto/common.h>
|
|
#include <streaming/P2PBuilder.h>
|
|
#include <streaming/P2PParser.h>
|
|
#include <primitives/Tx.h>
|
|
#include <primitives/pubkey.h>
|
|
#include <cashaddr.h>
|
|
#include <base58.h>
|
|
#include <random.h>
|
|
|
|
PrivacySegment::PrivacySegment(uint16_t id, DataListenerInterface *parent)
|
|
: m_segmentId(id),
|
|
m_bloom(10000, 0.001, GetRandInt(INT_MAX), BLOOM_UPDATE_ALL),
|
|
m_parent(parent)
|
|
{
|
|
assert(m_segmentId > 0); // zero is not allowed, that is the 'unset' value elsewhere
|
|
}
|
|
|
|
uint16_t PrivacySegment::segmentId() const
|
|
{
|
|
return m_segmentId;
|
|
}
|
|
|
|
PrivacySegment::FilterLock PrivacySegment::clearFilter()
|
|
{
|
|
FilterLock lock(this);
|
|
m_bloom.clear();
|
|
lock.setFirst(m_firstBlock == -1);
|
|
return lock;
|
|
}
|
|
|
|
void PrivacySegment::addToFilter(const uint256 &prevHash, int outIndex)
|
|
{
|
|
assert(outIndex >= 0);
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
std::vector<unsigned char> data;
|
|
data.resize(36);
|
|
memcpy(data.data(), prevHash.begin(), 32);
|
|
WriteLE32(data.data() + 32, outIndex);
|
|
m_bloom.insert(data);
|
|
}
|
|
|
|
void PrivacySegment::addToFilter(const std::string &address, int blockHeight)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
CashAddress::Content c = CashAddress::decodeCashAddrContent(address, "bitcoincash");
|
|
if (c.hash.empty()) {
|
|
CBase58Data old; // legacy address encoding
|
|
if (old.SetString(address)) {
|
|
c.hash = old.data();
|
|
if (!old.isMainnetPkh() && !old.isMainnetSh()) {
|
|
logCritical() << "PrivacySegment::addToFilter: Address could not be parsed";
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
m_bloom.insert(c.hash);
|
|
|
|
if (blockHeight > 0) {
|
|
if (m_firstBlock == -1)
|
|
m_firstBlock = blockHeight;
|
|
else
|
|
m_firstBlock = std::min(m_firstBlock, blockHeight);
|
|
}
|
|
m_filterChangedHeight = m_merkleBlockHeight;
|
|
}
|
|
|
|
void PrivacySegment::addKeyToFilter(const KeyId &address, int blockHeight)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
m_bloom.insert(std::vector<uint8_t>(address.begin(), address.end()));
|
|
|
|
if (blockHeight > 0) {
|
|
if (m_firstBlock == -1)
|
|
m_firstBlock = blockHeight;
|
|
else
|
|
m_firstBlock = std::min(m_firstBlock, blockHeight);
|
|
}
|
|
m_filterChangedHeight = m_merkleBlockHeight;
|
|
}
|
|
|
|
Streaming::ConstBuffer PrivacySegment::writeFilter(Streaming::BufferPool &pool) const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
pool.reserve(m_bloom.GetSerializeSize(0, 0));
|
|
Streaming::P2PBuilder builder(pool);
|
|
m_bloom.store(builder);
|
|
return builder.buffer();
|
|
}
|
|
|
|
int PrivacySegment::firstBlock() const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
return m_firstBlock;
|
|
}
|
|
|
|
void PrivacySegment::blockSynched(int height)
|
|
{
|
|
bool forward = false;
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
if (height <= m_merkleBlockHeight) {
|
|
m_softMerkleBlockHeight = height;
|
|
} else {
|
|
m_merkleBlockHeight = height;
|
|
forward = true;
|
|
}
|
|
}
|
|
// avoid deadlocks in wallet / privacysegment interaction and do this outside of our locks.
|
|
if (forward) {
|
|
assert(m_parent);
|
|
m_parent->setLastSynchedBlockHeight(height);
|
|
}
|
|
}
|
|
|
|
int PrivacySegment::lastBlockSynched() const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
if (m_merkleBlockHeight == -1)
|
|
return m_firstBlock - 1;
|
|
return m_merkleBlockHeight;
|
|
}
|
|
|
|
int PrivacySegment::backupSyncHeight() const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
if (m_softMerkleBlockHeight == -1)
|
|
return m_firstBlock - 1;
|
|
return m_softMerkleBlockHeight;
|
|
}
|
|
|
|
void PrivacySegment::newTransactions(const BlockHeader &header, int blockHeight, const std::deque<Tx> &blockTransactions)
|
|
{
|
|
/*
|
|
* Notice that the transactions match hit our filter, that doesn't mean it actually matched the
|
|
* address or output that the wallet owns.
|
|
* The wallet should thus test this and make sure that our filter is updated continuesly
|
|
* with new outputs and replaced with a new filter when many outputs are already spent (which
|
|
* we then want to push to peers to avoid them sending us some false-positives).
|
|
*/
|
|
m_parent->newTransactions(header, blockHeight, blockTransactions);
|
|
}
|
|
|
|
void PrivacySegment::newTransaction(const Tx &tx)
|
|
{
|
|
m_parent->newTransaction(tx);
|
|
}
|
|
|
|
int PrivacySegment::filterChangedHeight() const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
return m_filterChangedHeight;
|
|
}
|
|
|
|
const CBloomFilter &PrivacySegment::bloomFilter() const
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
return m_bloom;
|
|
}
|
|
|
|
void PrivacySegment::addListener(PrivacySegmentListener *listener)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
m_listeners.push_back(listener);
|
|
}
|
|
|
|
void PrivacySegment::removeListener(PrivacySegmentListener *listener)
|
|
{
|
|
std::unique_lock<std::recursive_mutex> lock(m_lock);
|
|
auto iter = m_listeners.begin();
|
|
while (iter != m_listeners.end()) {
|
|
if (*iter == listener)
|
|
iter = m_listeners.erase(iter);
|
|
else
|
|
++iter;
|
|
}
|
|
}
|
|
|
|
PrivacySegment::Priority PrivacySegment::priority() const
|
|
{
|
|
return m_priority;
|
|
}
|
|
|
|
void PrivacySegment::setPriority(Priority priority)
|
|
{
|
|
m_priority = priority;
|
|
}
|
|
|
|
bool PrivacySegment::enabled() const
|
|
{
|
|
return m_enabled;
|
|
}
|
|
|
|
void PrivacySegment::setEnabled(bool newEnabled)
|
|
{
|
|
m_enabled = newEnabled;
|
|
}
|
|
|
|
|
|
// ///////////////////////////////////////////////////////////////////
|
|
|
|
PrivacySegment::FilterLock::FilterLock(PrivacySegment *parent)
|
|
: parent(parent)
|
|
{
|
|
parent->m_lock.lock();
|
|
}
|
|
|
|
PrivacySegment::FilterLock::FilterLock(PrivacySegment::FilterLock && other)
|
|
: parent(other.parent)
|
|
{
|
|
}
|
|
|
|
PrivacySegment::FilterLock::~FilterLock()
|
|
{
|
|
parent->m_lock.unlock();
|
|
for (auto l : parent->m_listeners) {
|
|
l->filterUpdated(first);
|
|
}
|
|
}
|