-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathTxAdverts.cpp
More file actions
233 lines (201 loc) · 5.53 KB
/
TxAdverts.cpp
File metadata and controls
233 lines (201 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2022 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "overlay/TxAdverts.h"
#include "ledger/LedgerManager.h"
#include "main/Application.h"
#include "util/ProtocolVersion.h"
#include <algorithm>
namespace stellar
{
constexpr uint32 const ADVERT_CACHE_SIZE = 50000;
TxAdverts::TxAdverts(Application& app)
: mApp(app), mAdvertHistory(ADVERT_CACHE_SIZE), mAdvertTimer(app)
{
}
void
TxAdverts::flushAdvert()
{
if (mOutgoingTxHashes.size() > 0)
{
auto msg = std::make_shared<StellarMessage>();
msg->type(FLOOD_ADVERT);
msg->floodAdvert().txHashes = std::move(mOutgoingTxHashes);
mOutgoingTxHashes.clear();
mApp.postOnMainThread(
[send = mSendCb, msg = std::move(msg)]() {
releaseAssert(send);
send(msg);
},
"flushAdvert");
}
}
void
TxAdverts::shutdown()
{
mAdvertTimer.cancel();
}
void
TxAdverts::start(
std::function<void(std::shared_ptr<StellarMessage const>)> sendCb)
{
if (!sendCb)
{
throw std::invalid_argument("sendCb must be set");
}
mSendCb = sendCb;
}
void
TxAdverts::startAdvertTimer()
{
mAdvertTimer.expires_from_now(mApp.getConfig().FLOOD_ADVERT_PERIOD_MS);
mAdvertTimer.async_wait([this](asio::error_code const& error) {
if (!error)
{
flushAdvert();
}
});
}
size_t
TxAdverts::getTxLimit()
{
auto& lm = mApp.getLedgerManager();
size_t classic = lm.getLastMaxTxSetSizeOps();
if (protocolVersionStartsFrom(
lm.getLastClosedLedgerHeader().header.ledgerVersion,
SOROBAN_PROTOCOL_VERSION))
{
return classic +
lm.getLastClosedSorobanNetworkConfig().ledgerMaxTxCount();
}
return classic;
}
void
TxAdverts::queueOutgoingAdvert(Hash const& txHash)
{
if (mOutgoingTxHashes.empty())
{
startAdvertTimer();
}
mOutgoingTxHashes.emplace_back(txHash);
// Flush adverts at the earliest of the following two conditions:
// 1. The number of hashes reaches the threshold (see condition below).
// 2. The oldest tx hash hash been in the queue for FLOOD_TX_PERIOD_MS
// (managed via mAdvertTimer).
if (mOutgoingTxHashes.size() == getMaxAdvertSize())
{
flushAdvert();
}
}
bool
TxAdverts::seenAdvert(Hash const& hash)
{
return mAdvertHistory.exists(hash);
}
void
TxAdverts::rememberHash(Hash const& hash, uint32_t ledgerSeq)
{
mAdvertHistory.put(hash, ledgerSeq);
}
size_t
TxAdverts::size() const
{
return mIncomingTxHashes.size() + mTxHashesToRetry.size();
}
void
TxAdverts::retryIncomingAdvert(std::list<Hash>& list)
{
mTxHashesToRetry.splice(mTxHashesToRetry.end(), list);
size_t const limit = getTxLimit();
while (size() > limit)
{
popIncomingAdvert();
}
}
void
TxAdverts::queueIncomingAdvert(TxAdvertVector const& txHashes, uint32_t seq)
{
for (auto const& hash : txHashes)
{
rememberHash(hash, seq);
}
auto it = txHashes.begin();
size_t const limit = getTxLimit();
if (txHashes.size() > limit)
{
// If txHashes has more than limit txns, then
// the first (txHashes.size() - limit) txns will be
// popped in the while loop below. Therefore, we won't even bother
// pushing them.
it += txHashes.size() - limit;
}
while (it != txHashes.end())
{
mIncomingTxHashes.emplace_back(*it);
it++;
}
while (size() > limit)
{
popIncomingAdvert();
}
}
Hash
TxAdverts::popIncomingAdvert()
{
if (size() <= 0)
{
throw std::runtime_error("No advert to pop");
}
if (mTxHashesToRetry.size() > 0)
{
auto const h = mTxHashesToRetry.front();
mTxHashesToRetry.pop_front();
return h;
}
else
{
auto const h = mIncomingTxHashes.front();
mIncomingTxHashes.pop_front();
return h;
}
}
void
TxAdverts::clearBelow(uint32_t ledgerSeq)
{
mAdvertHistory.erase_if(
[&](uint32_t const& seq) { return seq < ledgerSeq; });
}
int64_t
TxAdverts::getOpsFloodLedger(size_t maxOps, double rate)
{
double opsToFloodPerLedgerDbl = rate * static_cast<double>(maxOps);
releaseAssertOrThrow(opsToFloodPerLedgerDbl >= 0.0);
return static_cast<int64_t>(opsToFloodPerLedgerDbl);
}
size_t
TxAdverts::getMaxAdvertSize() const
{
auto const& cfg = mApp.getConfig();
auto ledgerCloseTime =
mApp.getLedgerManager().getExpectedLedgerCloseTime().count();
int64_t opsToFloodPerLedger =
getOpsFloodLedger(mApp.getLedgerManager().getLastMaxTxSetSizeOps(),
cfg.FLOOD_OP_RATE_PER_LEDGER);
{
if (protocolVersionStartsFrom(mApp.getLedgerManager()
.getLastClosedLedgerHeader()
.header.ledgerVersion,
SOROBAN_PROTOCOL_VERSION))
{
auto limits =
mApp.getLedgerManager().getLastClosedSorobanNetworkConfig();
opsToFloodPerLedger += getOpsFloodLedger(
limits.ledgerMaxTxCount(), cfg.FLOOD_SOROBAN_RATE_PER_LEDGER);
}
}
size_t res = static_cast<size_t>(bigDivideOrThrow(
opsToFloodPerLedger, cfg.FLOOD_ADVERT_PERIOD_MS.count(),
ledgerCloseTime, Rounding::ROUND_UP));
return std::clamp<size_t>(res, 1, TX_ADVERT_VECTOR_MAX_SIZE);
}
}