Kagome
Polkadot Runtime Engine in C++17
reputation_change.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_REPUTATIONCHANGE
7 #define KAGOME_NETWORK_REPUTATIONCHANGE
8 
9 #include <chrono>
10 
11 #include <libp2p/peer/peer_id.hpp>
12 
13 namespace kagome::network {
14 
15  using Reputation = std::int32_t;
16 
18  public:
20  const std::string_view reason;
21 
23  if (delta < 0) {
24  if (value < 0 and value + delta > 0) { // negative overflow
25  return {std::numeric_limits<Reputation>::min(), reason};
26  }
27  } else if (delta > 0) {
28  if (value > 0 and value + delta < 0) { // positive overflow
29  return {std::numeric_limits<Reputation>::max(), reason};
30  }
31  }
32  return {value + delta, reason};
33  }
34 
35  ReputationChange operator*(size_t times) const {
36  auto limit = value < 0 ? std::numeric_limits<Reputation>::min()
37  : std::numeric_limits<Reputation>::max();
38  if (static_cast<size_t>(limit / value) < times) { // overflow
39  return {limit, reason};
40  }
41  return {static_cast<Reputation>(value * times), reason};
42  }
43  };
44 
45  namespace reputation {
46 
47  // clang-format off
48  namespace cost {
49  const ReputationChange UNEXPECTED_DISCONNECT = {-1000, "Network: Unexpected disconnect"};
50  const ReputationChange DUPLICATE_BLOCK_REQUEST = {-50, "Sync: Duplicate block request"};
51 
52  const ReputationChange PAST_REJECTION = {-50, "Grandpa: Past message"};
53 
54  const ReputationChange BAD_SIGNATURE = {-100, "Grandpa: Bad signature"};
55  const ReputationChange MALFORMED_CATCH_UP = {-1000, "Grandpa: Malformed catch-up"};
56  const ReputationChange MALFORMED_COMMIT = {-1000, "Grandpa: Malformed commit"};
57 
58  // A message received that's from the future relative to our view. always misbehavior.
59  const ReputationChange FUTURE_MESSAGE = {-500, "Grandpa: Future message"};
60 
61  const ReputationChange UNKNOWN_VOTER = {-150, "Grandpa: Unknown voter"};
62 
63  // invalid neighbor message, considering the last one
64  const ReputationChange INVALID_VIEW_CHANGE = {-500, "Grandpa: Invalid view change"};
65 
66  // could not decode neighbor message. bytes-length of the packet.
67  // TODO need to catch scale-decoder error for that
68  const ReputationChange UNDECODABLE_NEIGHBOR_MESSAGE = {-5, "Grandpa: Bad packet"};
69 
71 
72  // bad signature in catch-up response (for each checked signature)
73  const ReputationChange BAD_CATCHUP_RESPONSE = {-25, "Grandpa: Bad catch-up message"};
74 
76 
78  const ReputationChange INVALID_CATCH_UP = {-5000, "Grandpa: Invalid catch-up"};
79  const ReputationChange INVALID_COMMIT = {-5000, "Grandpa: Invalid commit"};
80  const ReputationChange OUT_OF_SCOPE_MESSAGE = {-500, "Grandpa: Out-of-scope message"};
81  const ReputationChange CATCH_UP_REQUEST_TIMEOUT = {-200, "Grandpa: Catch-up request timeout"};
82 
83  // cost of answering a catch-up request
84  const ReputationChange CATCH_UP_REPLY = {-200, "Grandpa: Catch-up reply"};
85 
86  // A message received that cannot be evaluated relative to our view.
87  // This happens before we have a view and have sent out neighbor packets.
88  // always misbehavior.
89  const ReputationChange HONEST_OUT_OF_SCOPE_CATCH_UP = {-200, "Grandpa: Out-of-scope catch-up"};
90 
91  } // namespace cost
92 
93  namespace benefit {
94  const ReputationChange NEIGHBOR_MESSAGE = {100, "Grandpa: Neighbor message"};
95  const ReputationChange ROUND_MESSAGE = {100, "Grandpa: Round message"};
96  const ReputationChange BASIC_VALIDATED_CATCH_UP = {200, "Grandpa: Catch-up message"};
97  const ReputationChange BASIC_VALIDATED_COMMIT = {100, "Grandpa: Commit"};
99  } // namespace benefit
100  // clang-format on
101 
102  } // namespace reputation
103 
104 } // namespace kagome::network
105 
106 #endif // KAGOME_NETWORK_REPUTATIONCHANGE
const ReputationChange PER_SIGNATURE_CHECKED
const ReputationChange OUT_OF_SCOPE_MESSAGE
const ReputationChange CATCH_UP_REQUEST_TIMEOUT
const ReputationChange UNKNOWN_VOTER
const ReputationChange BAD_CATCHUP_RESPONSE
const ReputationChange PAST_REJECTION
const ReputationChange INVALID_VIEW_CHANGE
const ReputationChange UNEXPECTED_DISCONNECT
const ReputationChange MALFORMED_COMMIT
const ReputationChange INVALID_COMMIT
const ReputationChange BASIC_VALIDATED_COMMIT
const ReputationChange DUPLICATE_BLOCK_REQUEST
const ReputationChange BASIC_VALIDATED_CATCH_UP
const ReputationChange PER_BLOCK_LOADED
std::int32_t Reputation
const ReputationChange PER_UNDECODABLE_BYTE
const ReputationChange BAD_SIGNATURE
const ReputationChange UNDECODABLE_NEIGHBOR_MESSAGE
ReputationChange operator*(size_t times) const
const ReputationChange INVALID_CATCH_UP
const ReputationChange CATCH_UP_REPLY
const ReputationChange FUTURE_MESSAGE
ReputationChange operator+(Reputation delta) const
const ReputationChange MALFORMED_CATCH_UP
const ReputationChange HONEST_OUT_OF_SCOPE_CATCH_UP