19constexpr int MAX_TXHASHES = 16;
20constexpr int MAX_PEERS = 16;
26std::chrono::microseconds DELAYS[256];
32 for (uint8_t txhash = 0; txhash < MAX_TXHASHES; txhash += 1) {
38 DELAYS[i] = std::chrono::microseconds{i};
42 for (; i < 128; ++i) {
43 int diff_bits = ((i - 10) * 2) / 9;
45 DELAYS[i] = DELAYS[i - 1] + std::chrono::microseconds{diff};
48 for (; i < 256; ++i) {
49 DELAYS[i] = -DELAYS[255 - i];
84 uint64_t m_current_sequence{0};
87 std::priority_queue<std::chrono::microseconds, std::vector<std::chrono::microseconds>,
88 std::greater<std::chrono::microseconds>> m_events;
93 std::chrono::microseconds m_time;
95 State m_state{State::NOTHING};
102 Announcement m_announcements[MAX_TXHASHES][MAX_PEERS];
105 std::chrono::microseconds m_now{244466666};
108 void Cleanup(
int txhash)
110 bool all_nothing =
true;
111 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
112 const Announcement& ann = m_announcements[txhash][peer];
113 if (ann.m_state != State::NOTHING) {
114 if (ann.m_state != State::COMPLETED)
return;
118 if (all_nothing)
return;
119 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
120 m_announcements[txhash][peer].m_state = State::NOTHING;
125 int GetSelected(
int txhash)
const
128 uint64_t ret_priority = 0;
129 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
130 const Announcement& ann = m_announcements[txhash][peer];
132 if (ann.m_state == State::REQUESTED)
return -1;
134 if (ann.m_state == State::CANDIDATE && ann.m_time <= m_now) {
135 if (
ret == -1 || ann.m_priority > ret_priority) {
136 std::tie(
ret, ret_priority) = std::tie(peer, ann.m_priority);
144 Tester() : m_tracker(true) {}
146 std::chrono::microseconds
Now()
const {
return m_now; }
148 void AdvanceTime(std::chrono::microseconds offset)
151 while (!m_events.empty() && m_events.top() <= m_now) m_events.pop();
154 void AdvanceToEvent()
156 while (!m_events.empty() && m_events.top() <= m_now) m_events.pop();
157 if (!m_events.empty()) {
158 m_now = m_events.top();
163 void DisconnectedPeer(
int peer)
166 for (
int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
167 if (m_announcements[txhash][peer].m_state != State::NOTHING) {
168 m_announcements[txhash][peer].m_state = State::NOTHING;
177 void ForgetTxHash(
int txhash)
180 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
181 m_announcements[txhash][peer].m_state = State::NOTHING;
189 void ReceivedInv(
int peer,
int txhash,
bool is_wtxid,
bool preferred, std::chrono::microseconds reqtime)
193 Announcement& ann = m_announcements[txhash][peer];
194 if (ann.m_state == State::NOTHING) {
195 ann.m_preferred = preferred;
196 ann.m_state = State::CANDIDATE;
197 ann.m_time = reqtime;
198 ann.m_is_wtxid = is_wtxid;
199 ann.m_sequence = m_current_sequence++;
200 ann.m_priority = m_tracker.
ComputePriority(TXHASHES[txhash], peer, ann.m_preferred);
203 if (reqtime > m_now) m_events.push(reqtime);
208 m_tracker.
ReceivedInv(peer, gtxid, preferred, reqtime);
211 void RequestedTx(
int peer,
int txhash, std::chrono::microseconds exptime)
215 if (m_announcements[txhash][peer].m_state == State::CANDIDATE) {
216 for (
int peer2 = 0; peer2 < MAX_PEERS; ++peer2) {
217 if (m_announcements[txhash][peer2].m_state == State::REQUESTED) {
218 m_announcements[txhash][peer2].m_state = State::COMPLETED;
221 m_announcements[txhash][peer].m_state = State::REQUESTED;
222 m_announcements[txhash][peer].m_time = exptime;
226 if (exptime > m_now) m_events.push(exptime);
229 m_tracker.
RequestedTx(peer, TXHASHES[txhash], exptime);
232 void ReceivedResponse(
int peer,
int txhash)
235 if (m_announcements[txhash][peer].m_state != State::NOTHING) {
236 m_announcements[txhash][peer].m_state = State::COMPLETED;
244 void GetRequestable(
int peer)
249 std::vector<std::tuple<uint64_t, int, bool>> result;
250 std::vector<std::pair<NodeId, GenTxid>> expected_expired;
251 for (
int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
253 for (
int peer2 = 0; peer2 < MAX_PEERS; ++peer2) {
254 Announcement& ann2 = m_announcements[txhash][peer2];
255 if (ann2.m_state == State::REQUESTED && ann2.m_time <= m_now) {
257 expected_expired.emplace_back(peer2, gtxid);
258 ann2.m_state = State::COMPLETED;
265 const Announcement& ann = m_announcements[txhash][peer];
266 if (ann.m_state == State::CANDIDATE && GetSelected(txhash) == peer) {
267 result.emplace_back(ann.m_sequence, txhash, ann.m_is_wtxid);
271 std::sort(result.begin(), result.end());
272 std::sort(expected_expired.begin(), expected_expired.end());
275 std::vector<std::pair<NodeId, GenTxid>> expired;
276 const auto actual = m_tracker.
GetRequestable(peer, m_now, &expired);
277 std::sort(expired.begin(), expired.end());
278 assert(expired == expected_expired);
281 assert(result.size() == actual.size());
282 for (
size_t pos = 0; pos < actual.size(); ++pos) {
283 assert(TXHASHES[std::get<1>(result[pos])] == actual[pos].ToUint256());
284 assert(std::get<2>(result[pos]) == actual[pos].IsWtxid());
292 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
295 size_t candidates = 0;
296 for (
int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
297 tracked += m_announcements[txhash][peer].m_state != State::NOTHING;
298 inflight += m_announcements[txhash][peer].m_state == State::REQUESTED;
299 candidates += m_announcements[txhash][peer].m_state == State::CANDIDATE;
301 std::bitset<MAX_PEERS> expected_announcers;
302 for (
int peer = 0; peer < MAX_PEERS; ++peer) {
303 if (m_announcements[txhash][peer].m_state == State::CANDIDATE || m_announcements[txhash][peer].m_state == State::REQUESTED) {
304 expected_announcers[peer] =
true;
307 std::vector<NodeId> candidate_peers;
309 assert(expected_announcers.count() == candidate_peers.size());
310 for (
const auto& peer : candidate_peers) {
311 assert(expected_announcers[peer]);
334 auto it = buffer.begin();
335 while (it != buffer.end()) {
336 int cmd = *(it++) % 11;
337 int peer, txidnum, delaynum;
340 tester.AdvanceToEvent();
343 delaynum = it == buffer.end() ? 0 : *(it++);
344 tester.AdvanceTime(DELAYS[delaynum]);
347 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
348 tester.GetRequestable(peer);
351 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
352 tester.DisconnectedPeer(peer);
355 txidnum = it == buffer.end() ? 0 : *(it++);
356 tester.ForgetTxHash(txidnum % MAX_TXHASHES);
360 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
361 txidnum = it == buffer.end() ? 0 : *(it++);
362 tester.ReceivedInv(peer, txidnum % MAX_TXHASHES, (txidnum / MAX_TXHASHES) & 1,
cmd & 1,
363 std::chrono::microseconds::min());
367 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
368 txidnum = it == buffer.end() ? 0 : *(it++);
369 delaynum = it == buffer.end() ? 0 : *(it++);
370 tester.ReceivedInv(peer, txidnum % MAX_TXHASHES, (txidnum / MAX_TXHASHES) & 1,
cmd & 1,
371 tester.Now() + DELAYS[delaynum]);
374 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
375 txidnum = it == buffer.end() ? 0 : *(it++);
376 delaynum = it == buffer.end() ? 0 : *(it++);
377 tester.RequestedTx(peer, txidnum % MAX_TXHASHES, tester.Now() + DELAYS[delaynum]);
380 peer = it == buffer.end() ? 0 : *(it++) % MAX_PEERS;
381 txidnum = it == buffer.end() ? 0 : *(it++);
382 tester.ReceivedResponse(peer, txidnum % MAX_TXHASHES);
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Data structure to keep track of, and schedule, transaction downloads from peers.
void ReceivedInv(NodeId peer, const GenTxid >xid, bool preferred, std::chrono::microseconds reqtime)
Adds a new CANDIDATE announcement.
void SanityCheck() const
Run internal consistency check (testing only).
size_t CountInFlight(NodeId peer) const
Count how many REQUESTED announcements a peer has.
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...
size_t CountCandidates(NodeId peer) const
Count how many CANDIDATE announcements a peer has.
void DisconnectedPeer(NodeId peer)
Deletes all announcements for a given peer.
void ReceivedResponse(NodeId peer, const uint256 &txhash)
Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one.
uint64_t ComputePriority(const uint256 &txhash, NodeId peer, bool preferred) const
Access to the internal priority computation (testing only)
void PostGetRequestableSanityCheck(std::chrono::microseconds now) const
Run a time-dependent internal consistency check (testing only).
void RequestedTx(NodeId peer, const uint256 &txhash, std::chrono::microseconds expiry)
Marks a transaction as requested, with a specified expiry.
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
size_t Size() const
Count how many announcements are being tracked in total across all peers and transaction hashes.
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.
void ForgetTxHash(const uint256 &txhash)
Deletes all announcements for a given txhash (both txid and wtxid ones).
static transaction_identifier FromUint256(const uint256 &id)
T Now()
Return the current time point cast to the given precision.