Bitcoin Core 29.99.0
P2P Digital Currency
txrequest.h
Go to the documentation of this file.
1// Copyright (c) 2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_TXREQUEST_H
6#define BITCOIN_TXREQUEST_H
7
9#include <net.h> // For NodeId
10#include <uint256.h>
11
12#include <chrono>
13#include <vector>
14
15#include <stdint.h>
16
101 // Avoid littering this header file with implementation details.
102 class Impl;
103 const std::unique_ptr<Impl> m_impl;
104
105public:
107 explicit TxRequestTracker(bool deterministic = false);
109
110 // Conceptually, the data structure consists of a collection of "announcements", one for each peer/txhash
111 // combination:
112 //
113 // - CANDIDATE announcements represent transactions that were announced by a peer, and that become available for
114 // download after their reqtime has passed.
115 //
116 // - REQUESTED announcements represent transactions that have been requested, and which we're awaiting a
117 // response for from that peer. Their expiry value determines when the request times out.
118 //
119 // - COMPLETED announcements represent transactions that have been requested from a peer, and a NOTFOUND or a
120 // transaction was received in response (valid or not), or they timed out. They're only kept around to
121 // prevent requesting them again. If only COMPLETED announcements for a given txhash remain (so no CANDIDATE
122 // or REQUESTED ones), all of them are deleted (this is an invariant, and maintained by all operations below).
123 //
124 // The operations below manipulate the data structure.
125
135 void ReceivedInv(NodeId peer, const GenTxid& gtxid, bool preferred,
136 std::chrono::microseconds reqtime);
137
142 void DisconnectedPeer(NodeId peer);
143
149 void ForgetTxHash(const uint256& txhash);
150
167 std::vector<GenTxid> GetRequestable(NodeId peer, std::chrono::microseconds now,
168 std::vector<std::pair<NodeId, GenTxid>>* expired = nullptr);
169
178 void RequestedTx(NodeId peer, const uint256& txhash, std::chrono::microseconds expiry);
179
186 void ReceivedResponse(NodeId peer, const uint256& txhash);
187
188 // The operations below inspect the data structure.
189
191 size_t CountInFlight(NodeId peer) const;
192
194 size_t CountCandidates(NodeId peer) const;
195
197 size_t Count(NodeId peer) const;
198
200 size_t Size() const;
201
204 void GetCandidatePeers(const uint256& txhash, std::vector<NodeId>& result_peers) const;
205
207 uint64_t ComputePriority(const uint256& txhash, NodeId peer, bool preferred) const;
208
210 void SanityCheck() const;
211
216 void PostGetRequestableSanityCheck(std::chrono::microseconds now) const;
217};
218
219#endif // BITCOIN_TXREQUEST_H
A generic txid reference (txid or wtxid).
Definition: transaction.h:428
Actual implementation for TxRequestTracker's data structure.
Definition: txrequest.cpp:310
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:100
void ReceivedInv(NodeId peer, const GenTxid &gtxid, bool preferred, std::chrono::microseconds reqtime)
Adds a new CANDIDATE announcement.
Definition: txrequest.cpp:741
void SanityCheck() const
Run internal consistency check (testing only).
Definition: txrequest.cpp:734
size_t CountInFlight(NodeId peer) const
Count how many REQUESTED announcements a peer has.
Definition: txrequest.cpp:729
void GetCandidatePeers(const uint256 &txhash, std::vector< NodeId > &result_peers) const
For some txhash (txid or wtxid), finds all peers with non-COMPLETED announcements and appends them to...
Definition: txrequest.cpp:733
size_t CountCandidates(NodeId peer) const
Count how many CANDIDATE announcements a peer has.
Definition: txrequest.cpp:730
TxRequestTracker(bool deterministic=false)
Construct a TxRequestTracker.
Definition: txrequest.cpp:722
const std::unique_ptr< Impl > m_impl
Definition: txrequest.h:103
void DisconnectedPeer(NodeId peer)
Deletes all announcements for a given peer.
Definition: txrequest.cpp:728
void ReceivedResponse(NodeId peer, const uint256 &txhash)
Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one.
Definition: txrequest.cpp:752
uint64_t ComputePriority(const uint256 &txhash, NodeId peer, bool preferred) const
Access to the internal priority computation (testing only)
Definition: txrequest.cpp:763
void PostGetRequestableSanityCheck(std::chrono::microseconds now) const
Run a time-dependent internal consistency check (testing only).
Definition: txrequest.cpp:736
void RequestedTx(NodeId peer, const uint256 &txhash, std::chrono::microseconds expiry)
Marks a transaction as requested, with a specified expiry.
Definition: txrequest.cpp:747
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
Definition: txrequest.cpp:731
size_t Size() const
Count how many announcements are being tracked in total across all peers and transaction hashes.
Definition: txrequest.cpp:732
std::vector< GenTxid > GetRequestable(NodeId peer, std::chrono::microseconds now, std::vector< std::pair< NodeId, GenTxid > > *expired=nullptr)
Find the txids to request now from peer.
Definition: txrequest.cpp:757
void ForgetTxHash(const uint256 &txhash)
Deletes all announcements for a given txhash (both txid and wtxid ones).
Definition: txrequest.cpp:727
256-bit opaque blob.
Definition: uint256.h:201
int64_t NodeId
Definition: net.h:97