Files
thehub/hub/api/AddressMonitorService.h
tomFlowee 65cc857704 Refactor and cleanup AddressMonitorService
This renames lots of variables to be more "correct" (call it
output instead of transaction and similar things).
This removes duplication by moving repeated into methods.

This fixes the behavior of the dsproof calls to be back to
the old unit test, while adding the new fields to the unit
test for minimal change.
2025-02-13 13:46:12 +01:00

108 lines
3.4 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2018-2025 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/>.
*/
#ifndef ADDRESSMONITORSERVICE_H
#define ADDRESSMONITORSERVICE_H
#include <validationinterface.h>
#include <NetworkService.h>
#include <NetworkConnection.h>
#include <primitives/Tx.h>
#include <primitives/PublicKey.h>
#include <script/standard.h>
#include <streaming/MessageBuilder.h>
#include <set>
class CTxMemPool;
class AddressMonitorService : public ValidationInterface, public NetworkService
{
public:
AddressMonitorService();
~AddressMonitorService() override;
// the hub pushed a transaction into its mempool
void syncTx(const Tx &tx) override;
void syncAllTransactionsInBlock(const Block &block, CBlockIndex *index) override;
void doubleSpendFound(const Tx &first, const Tx &duplicate) override;
void doubleSpendFound(const Tx &txInMempool, const DoubleSpendProof &proof) override;
void onIncomingMessage(Remote *con, const Message &message, const EndPoint &ep) override;
inline void setMempool(CTxMemPool *mempool) {
m_mempool = mempool;
}
int maxAddressesPerConnection() const;
void setMaxAddressesPerConnection(int maxAddressesPerConnection);
protected:
class RemoteWithKeys : public Remote {
public:
std::set<uint256> hashes;
};
// NetworkService interface
Remote *createRemote() override {
return new RemoteWithKeys();
}
private:
struct MatchedOutput {
int index = 0; // output index in transaction
uint64_t amount = 0;
uint256 hashedOutScript;
bool tokenIsFT = false;
bool tokenIsNFT = false;
bool hasToken = false;
Streaming::ConstBuffer tokenCategory;
bool tokenIsImmutable = false;
bool tokenIsMutable = false;
bool tokenIsMinting = false;
Streaming::ConstBuffer tokenCommitment;
uint64_t tokenAmount = 0; // fungible tokens
int serializedSize() const;
void serialize(Streaming::MessageBuilder &builder) const;
};
struct Match {
std::deque<MatchedOutput> matches;
};
bool match(Tx::Iterator &iter, const std::deque<NetworkService::Remote *> &remotes, std::map<int, Match> &matchingRemotes) const;
void sendMatchesToRemote(Remote *remote, const Match &data, const uint256 &txid, int blockheight, int64_t offsetInBlock);
void updateBools();
/// Callback for just subscribed addresses to find if there is a hit in the mempool.
void findTxInMempool(int connectionId, const uint256 &hash);
std::mutex m_poolMutex;
std::shared_ptr<Streaming::BufferPool> m_pool;
// true if any remote added a watch
bool m_findByHash = false;
int m_maxAddressesPerConnection = -1;
CTxMemPool *m_mempool = nullptr;
};
#endif