Files

108 lines
3.4 KiB
C++
Raw Permalink Normal View History

2018-02-17 14:27:49 +01:00
/*
* This file is part of the Flowee project
2025-02-12 22:16:17 +01:00
* Copyright (C) 2018-2025 Tom Zander <tom@flowee.org>
2018-02-17 14:27:49 +01: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 ADDRESSMONITORSERVICE_H
#define ADDRESSMONITORSERVICE_H
#include <validationinterface.h>
2019-04-09 17:24:38 +02:00
#include <NetworkService.h>
2018-02-17 14:27:49 +01:00
#include <NetworkConnection.h>
#include <primitives/Tx.h>
2025-02-12 22:16:17 +01:00
#include <primitives/PublicKey.h>
2018-02-17 14:27:49 +01:00
#include <script/standard.h>
2025-02-12 22:16:17 +01:00
#include <streaming/MessageBuilder.h>
2018-02-17 14:27:49 +01:00
#include <set>
class CTxMemPool;
2019-04-09 17:24:38 +02:00
class AddressMonitorService : public ValidationInterface, public NetworkService
2018-02-17 14:27:49 +01:00
{
public:
AddressMonitorService();
~AddressMonitorService() override;
2018-02-17 14:27:49 +01:00
// the hub pushed a transaction into its mempool
2021-02-18 16:03:01 +01:00
void syncTx(const Tx &tx) override;
2021-11-02 10:18:24 +01:00
void syncAllTransactionsInBlock(const Block &block, CBlockIndex *index) override;
2021-02-18 16:03:01 +01:00
void doubleSpendFound(const Tx &first, const Tx &duplicate) override;
void doubleSpendFound(const Tx &txInMempool, const DoubleSpendProof &proof) override;
2018-02-17 14:27:49 +01:00
2019-04-09 17:24:38 +02:00
void onIncomingMessage(Remote *con, const Message &message, const EndPoint &ep) override;
inline void setMempool(CTxMemPool *mempool) {
m_mempool = mempool;
}
2021-02-26 15:14:14 +01:00
int maxAddressesPerConnection() const;
void setMaxAddressesPerConnection(int maxAddressesPerConnection);
protected:
class RemoteWithKeys : public Remote {
public:
std::set<uint256> hashes;
2018-02-17 14:27:49 +01:00
};
2019-04-09 17:24:38 +02:00
// NetworkService interface
Remote *createRemote() override {
return new RemoteWithKeys();
}
private:
2025-02-12 22:16:17 +01:00
struct MatchedOutput {
int index = 0; // output index in transaction
2025-02-12 13:35:34 +01:00
uint64_t amount = 0;
2025-02-12 22:16:17 +01:00
uint256 hashedOutScript;
bool tokenIsFT = false;
bool tokenIsNFT = false;
2025-02-12 13:35:34 +01:00
2025-02-12 22:16:17 +01:00
bool hasToken = false;
Streaming::ConstBuffer tokenCategory;
bool tokenIsImmutable = false;
bool tokenIsMutable = false;
bool tokenIsMinting = false;
Streaming::ConstBuffer tokenCommitment;
uint64_t tokenAmount = 0; // fungible tokens
2025-02-12 13:35:34 +01:00
2025-02-12 22:16:17 +01:00
int serializedSize() const;
void serialize(Streaming::MessageBuilder &builder) const;
2025-02-12 13:35:34 +01:00
};
struct Match {
2025-02-12 22:16:17 +01:00
std::deque<MatchedOutput> matches;
2018-02-17 14:27:49 +01:00
};
bool match(Tx::Iterator &iter, const std::deque<NetworkService::Remote *> &remotes, std::map<int, Match> &matchingRemotes) const;
2025-02-12 22:16:17 +01:00
void sendMatchesToRemote(Remote *remote, const Match &data, const uint256 &txid, int blockheight, int64_t offsetInBlock);
2018-02-17 14:27:49 +01:00
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;
2023-12-21 15:13:56 +01:00
std::shared_ptr<Streaming::BufferPool> m_pool;
2018-02-17 14:27:49 +01:00
// true if any remote added a watch
bool m_findByHash = false;
2021-02-26 15:14:14 +01:00
int m_maxAddressesPerConnection = -1;
CTxMemPool *m_mempool = nullptr;
2018-02-17 14:27:49 +01:00
};
#endif