forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeerManager.h
155 lines (126 loc) · 3.8 KB
/
PeerManager.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
#pragma once
// Copyright 2015 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/PeerBareAddress.h"
#include "util/Timer.h"
#include <functional>
namespace soci
{
class statement;
}
namespace stellar
{
class Database;
class RandomPeerSource;
enum class PeerType
{
INBOUND,
OUTBOUND,
PREFERRED
};
enum class PeerTypeFilter
{
INBOUND_ONLY,
OUTBOUND_ONLY,
PREFERRED_ONLY,
ANY_OUTBOUND
};
/**
* Raw database record of peer data. Its key is PeerBareAddress.
*/
struct PeerRecord
{
std::tm mNextAttempt;
int mNumFailures{0};
int mType{0};
};
bool operator==(PeerRecord const& x, PeerRecord const& y);
struct PeerQuery
{
bool mUseNextAttempt;
int mMaxNumFailures;
PeerTypeFilter mTypeFilter;
};
PeerAddress toXdr(PeerBareAddress const& address);
/**
* Maintain list of know peers in database.
*/
class PeerManager
{
public:
enum class TypeUpdate
{
SET_OUTBOUND,
SET_PREFERRED,
REMOVE_PREFERRED,
UPDATE_TO_OUTBOUND
};
enum class BackOffUpdate
{
HARD_RESET,
RESET,
INCREASE
};
static void dropAll(Database& db);
explicit PeerManager(Application& app);
/**
* Ensure that given peer is stored in database.
*/
void ensureExists(PeerBareAddress const& address);
/**
* Update type of peer associated with given address.
*/
void update(PeerBareAddress const& address, TypeUpdate type);
/**
* Update "next try" of peer associated with given address - can reset
* it to now or back off even further in future.
*/
void update(PeerBareAddress const& address, BackOffUpdate backOff);
/**
* Update both type and "next try" of peer associated with given address.
*/
void update(PeerBareAddress const& address, TypeUpdate type,
BackOffUpdate backOff);
/**
* Load PeerRecord data for peer with given address. If not available in
* database, create default one. Second value in pair is true when data
* was loaded from database, false otherwise.
*/
std::pair<PeerRecord, bool> load(PeerBareAddress const& address);
/**
* Store PeerRecord data into database. If inDatabase is true, uses UPDATE
* query, uses INSERT otherwise.
*/
void store(PeerBareAddress const& address, PeerRecord const& PeerRecord,
bool inDatabase);
/**
* Load size random peers matching query from database.
*/
std::vector<PeerBareAddress> loadRandomPeers(PeerQuery const& query,
int size);
/**
* Remove peers that have at least minNumFailures. Can only remove peer with
* given address.
*/
void removePeersWithManyFailures(int minNumFailures,
PeerBareAddress const* address = nullptr);
/**
* Get list of peers to send to peer with given address.
*/
std::vector<PeerBareAddress> getPeersToSend(int size,
PeerBareAddress const& address);
private:
static const char* kSQLCreateStatement;
Application& mApp;
std::unique_ptr<RandomPeerSource> mOutboundPeersToSend;
std::unique_ptr<RandomPeerSource> mInboundPeersToSend;
int countPeers(std::string const& where,
std::function<void(soci::statement&)> const& bind);
std::vector<PeerBareAddress>
loadPeers(int limit, int offset, std::string const& where,
std::function<void(soci::statement&)> const& bind);
void update(PeerRecord& peer, TypeUpdate type);
void update(PeerRecord& peer, BackOffUpdate backOff, Application& app);
};
}