forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeer.h
237 lines (192 loc) · 5.69 KB
/
Peer.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#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 "util/asio.h"
#include "database/Database.h"
#include "overlay/PeerBareAddress.h"
#include "overlay/StellarXDR.h"
#include "util/NonCopyable.h"
#include "util/Timer.h"
#include "xdrpp/message.h"
namespace medida
{
class Timer;
class Meter;
}
namespace stellar
{
typedef std::shared_ptr<SCPQuorumSet> SCPQuorumSetPtr;
class Application;
class LoopbackPeer;
struct OverlayMetrics;
/*
* Another peer out there that we are connected to
*/
class Peer : public std::enable_shared_from_this<Peer>,
public NonMovableOrCopyable
{
public:
typedef std::shared_ptr<Peer> pointer;
enum PeerState
{
CONNECTING = 0,
CONNECTED = 1,
GOT_HELLO = 2,
GOT_AUTH = 3,
CLOSING = 4
};
enum PeerRole
{
REMOTE_CALLED_US,
WE_CALLED_REMOTE
};
enum class DropMode
{
FLUSH_WRITE_QUEUE,
IGNORE_WRITE_QUEUE
};
enum class DropDirection
{
REMOTE_DROPPED_US,
WE_DROPPED_REMOTE
};
protected:
Application& mApp;
PeerRole mRole;
PeerState mState;
NodeID mPeerID;
uint256 mSendNonce;
uint256 mRecvNonce;
HmacSha256Key mSendMacKey;
HmacSha256Key mRecvMacKey;
uint64_t mSendMacSeq{0};
uint64_t mRecvMacSeq{0};
std::string mRemoteVersion;
uint32_t mRemoteOverlayMinVersion;
uint32_t mRemoteOverlayVersion;
PeerBareAddress mAddress;
VirtualTimer mIdleTimer;
VirtualClock::time_point mLastRead;
VirtualClock::time_point mLastWrite;
VirtualClock::time_point mLastEmpty;
OverlayMetrics& getOverlayMetrics();
bool shouldAbort() const;
void recvMessage(StellarMessage const& msg);
void recvMessage(AuthenticatedMessage const& msg);
void recvMessage(xdr::msg_ptr const& xdrBytes);
virtual void recvError(StellarMessage const& msg);
void updatePeerRecordAfterEcho();
void updatePeerRecordAfterAuthentication();
void recvAuth(StellarMessage const& msg);
void recvDontHave(StellarMessage const& msg);
void recvGetPeers(StellarMessage const& msg);
void recvHello(Hello const& elo);
void recvPeers(StellarMessage const& msg);
void recvGetTxSet(StellarMessage const& msg);
void recvTxSet(StellarMessage const& msg);
void recvTransaction(StellarMessage const& msg);
void recvGetSCPQuorumSet(StellarMessage const& msg);
void recvSCPQuorumSet(StellarMessage const& msg);
void recvSCPMessage(StellarMessage const& msg);
void recvGetSCPState(StellarMessage const& msg);
void sendHello();
void sendAuth();
void sendSCPQuorumSet(SCPQuorumSetPtr qSet);
void sendDontHave(MessageType type, uint256 const& itemID);
void sendPeers();
void sendError(ErrorCode error, std::string const& message);
// NB: This is a move-argument because the write-buffer has to travel
// with the write-request through the async IO system, and we might have
// several queued at once. We have carefully arranged this to not copy
// data more than the once necessary into this buffer, but it can't be
// put in a reused/non-owned buffer without having to buffer/queue
// messages somewhere else. The async write request will point _into_
// this owned buffer. This is really the best we can do.
virtual void sendMessage(xdr::msg_ptr&& xdrBytes) = 0;
virtual void
connected()
{
}
virtual AuthCert getAuthCert();
void startIdleTimer();
void idleTimerExpired(asio::error_code const& error);
std::chrono::seconds getIOTimeout() const;
// helper method to acknownledge that some bytes were received
void receivedBytes(size_t byteCount, bool gotFullMessage);
public:
Peer(Application& app, PeerRole role);
Application&
getApp()
{
return mApp;
}
void sendGetTxSet(uint256 const& setID);
void sendGetQuorumSet(uint256 const& setID);
void sendGetPeers();
void sendGetScpState(uint32 ledgerSeq);
void sendErrorAndDrop(ErrorCode error, std::string const& message,
DropMode dropMode);
void sendMessage(StellarMessage const& msg);
PeerRole
getRole() const
{
return mRole;
}
bool isConnected() const;
bool isAuthenticated() const;
PeerState
getState() const
{
return mState;
}
std::string const&
getRemoteVersion() const
{
return mRemoteVersion;
}
uint32_t
getRemoteOverlayMinVersion() const
{
return mRemoteOverlayMinVersion;
}
uint32_t
getRemoteOverlayVersion() const
{
return mRemoteOverlayVersion;
}
PeerBareAddress const&
getAddress()
{
return mAddress;
}
NodeID
getPeerID()
{
return mPeerID;
}
std::string toString();
virtual std::string getIP() const = 0;
// These exist mostly to be overridden in TCPPeer and callable via
// shared_ptr<Peer> as a captured shared_from_this().
virtual void connectHandler(asio::error_code const& ec);
virtual void
writeHandler(asio::error_code const& error, size_t bytes_transferred)
{
}
virtual void
readHeaderHandler(asio::error_code const& error, size_t bytes_transferred)
{
}
virtual void
readBodyHandler(asio::error_code const& error, size_t bytes_transferred)
{
}
virtual void drop(std::string const& reason, DropDirection dropDirection,
DropMode dropMode) = 0;
virtual ~Peer()
{
}
friend class LoopbackPeer;
};
}