forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracker.cpp
202 lines (178 loc) · 5.16 KB
/
Tracker.cpp
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
// Copyright 2016 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 "Tracker.h"
#include "crypto/Hex.h"
#include "crypto/SHA.h"
#include "herder/Herder.h"
#include "main/Application.h"
#include "medida/medida.h"
#include "overlay/OverlayManager.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "xdrpp/marshal.h"
namespace stellar
{
static std::chrono::milliseconds const MS_TO_WAIT_FOR_FETCH_REPLY{1500};
static int const MAX_REBUILD_FETCH_LIST = 1000;
Tracker::Tracker(Application& app, Hash const& hash, AskPeer& askPeer)
: mAskPeer(askPeer)
, mApp(app)
, mNumListRebuild(0)
, mTimer(app)
, mItemHash(hash)
, mTryNextPeer(app.getMetrics().NewMeter(
{"overlay", "item-fetcher", "next-peer"}, "item-fetcher"))
{
assert(mAskPeer);
}
Tracker::~Tracker()
{
cancel();
}
SCPEnvelope
Tracker::pop()
{
auto env = mWaitingEnvelopes.back().second;
mWaitingEnvelopes.pop_back();
return env;
}
// returns false if no one cares about this guy anymore
bool
Tracker::clearEnvelopesBelow(uint64 slotIndex)
{
for (auto iter = mWaitingEnvelopes.begin();
iter != mWaitingEnvelopes.end();)
{
if (iter->second.statement.slotIndex < slotIndex)
{
iter = mWaitingEnvelopes.erase(iter);
}
else
{
iter++;
}
}
if (!mWaitingEnvelopes.empty())
{
return true;
}
mTimer.cancel();
mLastAskedPeer = nullptr;
return false;
}
void
Tracker::doesntHave(Peer::pointer peer)
{
if (mLastAskedPeer == peer)
{
CLOG(TRACE, "Overlay") << "Does not have " << hexAbbrev(mItemHash);
tryNextPeer();
}
}
void
Tracker::tryNextPeer()
{
// will be called by some timer or when we get a
// response saying they don't have it
Peer::pointer peer;
CLOG(TRACE, "Overlay") << "tryNextPeer " << hexAbbrev(mItemHash)
<< " last: "
<< (mLastAskedPeer ? mLastAskedPeer->toString()
: "<none>");
// if we don't have a list of peers to ask and we're not
// currently asking peers, build a new list
if (mPeersToAsk.empty() && !mLastAskedPeer)
{
std::set<std::shared_ptr<Peer>> peersWithEnvelope;
for (auto const& e : mWaitingEnvelopes)
{
auto const& s = mApp.getOverlayManager().getPeersKnows(e.first);
peersWithEnvelope.insert(s.begin(), s.end());
}
// move the peers that have the envelope to the back,
// to be processed first
for (auto const& p :
mApp.getOverlayManager().getRandomAuthenticatedPeers())
{
if (peersWithEnvelope.find(p) != peersWithEnvelope.end())
{
mPeersToAsk.emplace_back(p);
}
else
{
mPeersToAsk.emplace_front(p);
}
}
mNumListRebuild++;
CLOG(TRACE, "Overlay")
<< "tryNextPeer " << hexAbbrev(mItemHash) << " attempt "
<< mNumListRebuild << " reset to #" << mPeersToAsk.size();
}
while (!peer && !mPeersToAsk.empty())
{
peer = mPeersToAsk.back();
if (!peer->isAuthenticated())
{
peer.reset();
}
mPeersToAsk.pop_back();
}
std::chrono::milliseconds nextTry;
if (!peer)
{ // we have asked all our peers
// clear mLastAskedPeer so that we rebuild a new list
mLastAskedPeer.reset();
if (mNumListRebuild > MAX_REBUILD_FETCH_LIST)
{
nextTry = MS_TO_WAIT_FOR_FETCH_REPLY * MAX_REBUILD_FETCH_LIST;
}
else
{
nextTry = MS_TO_WAIT_FOR_FETCH_REPLY * mNumListRebuild;
}
}
else
{
if (mLastAskedPeer)
{
mTryNextPeer.Mark();
}
mLastAskedPeer = peer;
CLOG(TRACE, "Overlay") << "Asking for " << hexAbbrev(mItemHash)
<< " to " << peer->toString();
mAskPeer(peer, mItemHash);
nextTry = MS_TO_WAIT_FOR_FETCH_REPLY;
}
mTimer.expires_from_now(nextTry);
mTimer.async_wait([this]() { this->tryNextPeer(); },
VirtualTimer::onFailureNoop);
}
void
Tracker::listen(const SCPEnvelope& env)
{
mLastSeenSlotIndex = std::max(env.statement.slotIndex, mLastSeenSlotIndex);
StellarMessage m;
m.type(SCP_MESSAGE);
m.envelope() = env;
mWaitingEnvelopes.push_back(
std::make_pair(sha256(xdr::xdr_to_opaque(m)), env));
}
void
Tracker::discard(const SCPEnvelope& env)
{
auto matchEnvelope = [&env](std::pair<Hash, SCPEnvelope> const& x) {
return x.second == env;
};
mWaitingEnvelopes.erase(std::remove_if(std::begin(mWaitingEnvelopes),
std::end(mWaitingEnvelopes),
matchEnvelope),
std::end(mWaitingEnvelopes));
}
void
Tracker::cancel()
{
mTimer.cancel();
mLastSeenSlotIndex = 0;
}
}