CatapultServer  v0.5.0.1 (Elephant)
NodeInfo.h
Go to the documentation of this file.
1 
21 #pragma once
24 #include "catapult/utils/Hashers.h"
25 #include "catapult/types.h"
26 #include <unordered_set>
27 #include <vector>
28 
29 namespace catapult { namespace ionet {
30 
31 #define NODE_SOURCE_LIST \
32  /* Incoming connection from another node. */ \
33  ENUM_VALUE(Dynamic_Incoming) \
34  \
35  /* Forwarded from a peer node. */ \
36  ENUM_VALUE(Dynamic) \
37  \
38  /* Loaded from a local peers file. */ \
39  ENUM_VALUE(Static) \
40  \
41  /* Local node. */ \
42  ENUM_VALUE(Local)
43 
44 #define ENUM_VALUE(LABEL) LABEL,
45  enum class NodeSource : uint32_t {
49  };
50 #undef ENUM_VALUE
51 
53  std::ostream& operator<<(std::ostream& out, NodeSource value);
54 
57 
59  struct ConnectionState {
60  public:
63  : Age(0)
65  , BanAge(0)
66  {}
67 
68  public:
71  uint32_t Age;
72 
75 
78  uint32_t BanAge;
79  };
80 
82  struct NodeInfo {
83  public:
85  using ServiceIdentifiers = std::unordered_set<ServiceIdentifier, utils::BaseValueHasher<ServiceIdentifier>>;
86 
87  public:
89  explicit NodeInfo(NodeSource source);
90 
91  public:
93  NodeSource source() const;
94 
96  NodeInteractions interactions(Timestamp timestamp) const;
97 
99  size_t numConnectionStates() const;
100 
103 
105  bool hasActiveConnection() const;
106 
108  const ConnectionState* getConnectionState(ServiceIdentifier serviceId) const;
109 
110  public:
112  void source(NodeSource source);
113 
115  void incrementSuccesses(Timestamp timestamp);
116 
118  void incrementFailures(Timestamp timestamp);
119 
122 
124  void clearAge(ServiceIdentifier serviceId);
125 
128  void updateBan(ServiceIdentifier serviceId, uint32_t maxConnectionBanAge, uint32_t numConsecutiveFailuresBeforeBanning);
129 
130  private:
133  std::vector<std::pair<ServiceIdentifier, ConnectionState>> m_connectionStates;
134  };
135 }}
catapult::ionet::NodeInfo::m_source
NodeSource m_source
Definition: NodeInfo.h:131
catapult::ionet::NodeInfo::ServiceIdentifiers
std::unordered_set< ServiceIdentifier, utils::BaseValueHasher< ServiceIdentifier > > ServiceIdentifiers
A container of service identifiers.
Definition: NodeInfo.h:85
catapult::ionet::ConnectionState
Connection state unique to a node and connection identifier.
Definition: NodeInfo.h:59
catapult::ionet::ServiceIdentifier
utils::BaseValue< uint32_t, ServiceIdentifier_tag > ServiceIdentifier
Definition: NodeInfo.h:56
catapult::ionet::NodeInfo::incrementFailures
void incrementFailures(Timestamp timestamp)
Increments the number of failed interactions at timestamp.
Definition: NodeInfo.cpp:86
MacroBasedEnum.h
catapult::ionet::ConnectionState::NumConsecutiveFailures
uint32_t NumConsecutiveFailures
Number of consecutive failed connections.
Definition: NodeInfo.h:74
catapult::ionet::NodeInteractionsContainer
Node interactions container.
Definition: NodeInteractionsContainer.h:50
NodeInfo.h
catapult::ionet::NodeInfo::interactions
NodeInteractions interactions(Timestamp timestamp) const
Gets the node interactions at timestamp.
Definition: NodeInfo.cpp:51
catapult::ionet::ConnectionState::BanAge
uint32_t BanAge
Definition: NodeInfo.h:78
catapult::ionet::NodeInfo
Information about a node and its interactions.
Definition: NodeInfo.h:82
catapult::ionet::NodeInfo::m_connectionStates
std::vector< std::pair< ServiceIdentifier, ConnectionState > > m_connectionStates
Definition: NodeInfo.h:133
catapult::ionet::NodeInfo::incrementSuccesses
void incrementSuccesses(Timestamp timestamp)
Increments the number of successful interactions at timestamp.
Definition: NodeInfo.cpp:81
catapult::utils::BaseValue< uint64_t, Timestamp_tag >
catapult::ionet::NodeInfo::provisionConnectionState
ConnectionState & provisionConnectionState(ServiceIdentifier serviceId)
Gets connection state for the service identified by serviceId and creates zeroed state if no state ex...
Definition: NodeInfo.cpp:91
catapult::ionet::NodeInfo::source
NodeSource source() const
Gets the node source.
Definition: NodeInfo.cpp:47
catapult::ionet::NodeInfo::numConnectionStates
size_t numConnectionStates() const
Gets the number of connection states.
Definition: NodeInfo.cpp:55
NodeInteractionsContainer.h
catapult::ionet::NodeInfo::getConnectionState
const ConnectionState * getConnectionState(ServiceIdentifier serviceId) const
Gets connection state for the service identified by serviceId or nullptr if no state exists.
Definition: NodeInfo.cpp:73
NodeInteractionResultCode.h
catapult::ionet::NodeInteractionsContainer::pruneBuckets
void pruneBuckets(Timestamp timestamp)
Prunes buckets at timestamp.
Definition: NodeInteractionsContainer.cpp:64
catapult::ionet::NodeInfo::hasActiveConnection
bool hasActiveConnection() const
Returns true if at least one connection is active.
Definition: NodeInfo.cpp:67
MacroBasedEnumIncludes.h
catapult::ionet::NodeInteractionsContainer::incrementSuccesses
void incrementSuccesses(Timestamp timestamp)
Increments successful interactions at timestamp.
Definition: NodeInteractionsContainer.cpp:56
catapult::ionet::operator<<
std::ostream & operator<<(std::ostream &out, ConnectionSecurityMode value)
Insertion operator for outputting value to out.
catapult::ionet::NodeInfo::NodeInfo
NodeInfo(NodeSource source)
Creates a node info with source.
Definition: NodeInfo.cpp:44
catapult::ionet::NodeSource
NodeSource
Definition: NodeInfo.h:47
catapult::ionet::NodeInteractions
Node interactions.
Definition: NodeInteractionsContainer.h:29
catapult::ionet::NodeInfo::clearAge
void clearAge(ServiceIdentifier serviceId)
Clears the age of the connection for the service identified by serviceId.
Definition: NodeInfo.cpp:100
catapult::ionet::NodeSource::NODE_SOURCE_LIST
types.h
catapult::ionet::ConnectionState::ConnectionState
ConnectionState()
Creates zeroed state.
Definition: NodeInfo.h:62
catapult
Definition: AddressExtractionExtension.cpp:28
Hashers.h
catapult::ionet::NodeInfo::services
ServiceIdentifiers services() const
Gets the associated service ids.
Definition: NodeInfo.cpp:59
catapult::ionet::NodeInteractionsContainer::incrementFailures
void incrementFailures(Timestamp timestamp)
Increments failed interactions at timestamp.
Definition: NodeInteractionsContainer.cpp:60
catapult::ionet::ConnectionState::Age
uint32_t Age
Definition: NodeInfo.h:71
catapult::ionet::NodeInfo::updateBan
void updateBan(ServiceIdentifier serviceId, uint32_t maxConnectionBanAge, uint32_t numConsecutiveFailuresBeforeBanning)
Definition: NodeInfo.cpp:108
catapult::ionet::ServiceIdentifier_tag
Definition: NodeInfo.h:55
catapult::ionet::NodeInfo::m_interactions
NodeInteractionsContainer m_interactions
Definition: NodeInfo.h:132
catapult::ionet::NodeInteractionsContainer::interactions
NodeInteractions interactions(Timestamp timestamp) const
Gets the node interactions at timestamp..
Definition: NodeInteractionsContainer.cpp:44