forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHerderImpl.h
222 lines (169 loc) · 6.6 KB
/
HerderImpl.h
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
#pragma once
// Copyright 2014 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 "herder/Herder.h"
#include "herder/HerderSCPDriver.h"
#include "herder/PendingEnvelopes.h"
#include "herder/TransactionQueue.h"
#include "herder/Upgrades.h"
#include "util/Timer.h"
#include "util/XDROperators.h"
#include <deque>
#include <memory>
#include <unordered_map>
#include <vector>
namespace medida
{
class Meter;
class Counter;
class Timer;
}
namespace stellar
{
class Application;
class LedgerManager;
class HerderSCPDriver;
/*
* Is in charge of receiving transactions from the network.
*/
class HerderImpl : public Herder
{
public:
HerderImpl(Application& app);
~HerderImpl();
State getState() const override;
std::string getStateHuman() const override;
void syncMetrics() override;
// Bootstraps the HerderImpl if we're creating a new Network
void bootstrap() override;
void restoreState() override;
SCP& getSCP();
HerderSCPDriver&
getHerderSCPDriver()
{
return mHerderSCPDriver;
}
void valueExternalized(uint64 slotIndex, StellarValue const& value);
void emitEnvelope(SCPEnvelope const& envelope);
TransactionQueue::AddResult
recvTransaction(TransactionFramePtr tx) override;
EnvelopeStatus recvSCPEnvelope(SCPEnvelope const& envelope) override;
EnvelopeStatus recvSCPEnvelope(SCPEnvelope const& envelope,
const SCPQuorumSet& qset,
TxSetFrame txset) override;
void sendSCPStateToPeer(uint32 ledgerSeq, Peer::pointer peer) override;
bool recvSCPQuorumSet(Hash const& hash, const SCPQuorumSet& qset) override;
bool recvTxSet(Hash const& hash, const TxSetFrame& txset) override;
void peerDoesntHave(MessageType type, uint256 const& itemID,
Peer::pointer peer) override;
TxSetFramePtr getTxSet(Hash const& hash) override;
SCPQuorumSetPtr getQSet(Hash const& qSetHash) override;
void processSCPQueue();
uint32_t getCurrentLedgerSeq() const override;
SequenceNumber getMaxSeqInPendingTxs(AccountID const&) override;
void triggerNextLedger(uint32_t ledgerSeqToTrigger) override;
void setUpgrades(Upgrades::UpgradeParameters const& upgrades) override;
std::string getUpgradesJson() override;
bool resolveNodeID(std::string const& s, PublicKey& retKey) override;
Json::Value getJsonInfo(size_t limit, bool fullKeys = false) override;
Json::Value getJsonQuorumInfo(NodeID const& id, bool summary, bool fullKeys,
uint64 index) override;
Json::Value getJsonTransitiveQuorumIntersectionInfo(bool fullKeys) const;
virtual Json::Value getJsonTransitiveQuorumInfo(NodeID const& id,
bool summary,
bool fullKeys) override;
QuorumTracker::QuorumMap const& getCurrentlyTrackedQuorum() const override;
#ifdef BUILD_TESTS
// used for testing
PendingEnvelopes& getPendingEnvelopes();
#endif
// helper function to verify envelopes are signed
bool verifyEnvelope(SCPEnvelope const& envelope);
// helper function to sign envelopes
void signEnvelope(SecretKey const& s, SCPEnvelope& envelope);
// helper function to verify SCPValues are signed
bool verifyStellarValueSignature(StellarValue const& sv);
// helper function to sign SCPValues
void signStellarValue(SecretKey const& s, StellarValue& sv);
private:
// return true if values referenced by envelope have a valid close time:
// * it's within the allowed range (using lcl if possible)
// * it's recent enough (if `enforceRecent` is set)
bool checkCloseTime(SCPEnvelope const& envelope, bool enforceRecent);
void ledgerClosed();
void startRebroadcastTimer();
void rebroadcast();
void broadcast(SCPEnvelope const& e);
void processSCPQueueUpToIndex(uint64 slotIndex);
TransactionQueue mTransactionQueue;
void
updateTransactionQueue(std::vector<TransactionFramePtr> const& applied);
PendingEnvelopes mPendingEnvelopes;
Upgrades mUpgrades;
HerderSCPDriver mHerderSCPDriver;
void herderOutOfSync();
// attempt to retrieve additional SCP messages from peers
void getMoreSCPState();
// last slot that was persisted into the database
// keep track of all messages for MAX_SLOTS_TO_REMEMBER slots
uint64 mLastSlotSaved;
// timer that detects that we're stuck on an SCP slot
VirtualTimer mTrackingTimer;
// tracks the last time externalize was called
VirtualClock::time_point mLastExternalize;
// saves the SCP messages that the instance sent out last
void persistSCPState(uint64 slot);
// restores SCP state based on the last messages saved on disk
void restoreSCPState();
// saves upgrade parameters
void persistUpgrades();
void restoreUpgrades();
// called every time we get ledger externalized
// ensures that if we don't hear from the network, we throw the herder into
// indeterminate mode
void trackingHeartBeat();
VirtualTimer mTriggerTimer;
VirtualTimer mRebroadcastTimer;
Application& mApp;
LedgerManager& mLedgerManager;
struct SCPMetrics
{
medida::Meter& mLostSync;
medida::Meter& mEnvelopeEmit;
medida::Meter& mEnvelopeReceive;
// Counters for things reached-through the
// SCP maps: Slots and Nodes
medida::Counter& mCumulativeStatements;
// envelope signature verification
medida::Meter& mEnvelopeValidSig;
medida::Meter& mEnvelopeInvalidSig;
SCPMetrics(Application& app);
};
SCPMetrics mSCPMetrics;
// Check that the quorum map intersection state is up to date, and if not
// run a background job that re-analyzes the current quorum map.
void checkAndMaybeReanalyzeQuorumMap();
struct QuorumMapIntersectionState
{
uint32_t mLastCheckLedger{0};
uint32_t mLastGoodLedger{0};
size_t mNumNodes{0};
Hash mLastCheckQuorumMapHash{};
bool mRecalculating{false};
std::pair<std::vector<PublicKey>, std::vector<PublicKey>>
mPotentialSplit{};
bool
hasAnyResults() const
{
return mLastGoodLedger != 0;
}
bool
enjoysQuorunIntersection() const
{
return mLastCheckLedger == mLastGoodLedger;
}
};
QuorumMapIntersectionState mLastQuorumMapIntersectionState;
};
}