|
2 | 2 |
|
3 | 3 | import static java.lang.Math.min; |
4 | 4 |
|
5 | | -import java.util.ArrayList; |
6 | | -import java.util.List; |
7 | | -import org.tron.protos.Protocol.ReasonCode; |
8 | | - |
9 | 5 | public class Reputation { |
10 | 6 |
|
11 | | - private List<Score> scoreList = new ArrayList<>(); |
| 7 | + private NodeStatistics nodeStatistics; |
12 | 8 |
|
13 | 9 | public Reputation(NodeStatistics nodeStatistics) { |
14 | | - Score<MessageStatistics> discoverScore = new DiscoverScore(nodeStatistics.messageStatistics); |
15 | | - Score<NodeStatistics> otherScore = new OtherScore(nodeStatistics); |
16 | | - Score<NodeStatistics> tcpScore = new TcpScore(nodeStatistics); |
17 | | - Score<NodeStatistics> disconnectScore = new DisConnectScore(nodeStatistics); |
| 10 | + this.nodeStatistics = nodeStatistics; |
18 | 11 |
|
19 | | - scoreList.add(discoverScore); |
20 | | - scoreList.add(tcpScore); |
21 | | - scoreList.add(otherScore); |
22 | | - scoreList.add(disconnectScore); |
23 | 12 | } |
24 | 13 |
|
25 | | - public int calculate() { |
26 | | - int scoreNumber = 0; |
27 | | - for (Score score : scoreList) { |
28 | | - scoreNumber = score.calculate(scoreNumber); |
29 | | - if (!score.isContinue()) { |
30 | | - break; |
31 | | - } |
32 | | - } |
33 | | - return scoreNumber > 0 ? scoreNumber : 0; |
| 14 | + public int getScore() { |
| 15 | + return getNodeActiveScore() + getPacketLossRateScore() + getNetLatencyScore() |
| 16 | + + getHandshakeScore() + getTcpFlowScore() + getDisconnectionScore(); |
34 | 17 | } |
35 | 18 |
|
36 | | - public abstract class Score<T> implements Comparable<Score> { |
37 | | - |
38 | | - protected T t; |
39 | | - |
40 | | - public Score(T t) { |
41 | | - this.t = t; |
42 | | - } |
43 | | - |
44 | | - abstract int calculate(int baseScore); |
45 | | - |
46 | | - public boolean isContinue() { |
47 | | - return true; |
48 | | - } |
49 | | - |
50 | | - public int getOrder() { |
51 | | - return 0; |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public int compareTo(Score score) { |
56 | | - if (getOrder() > score.getOrder()) { |
57 | | - return 1; |
58 | | - } else if (getOrder() < score.getOrder()) { |
59 | | - return -1; |
60 | | - } |
61 | | - return 0; |
62 | | - } |
| 19 | + private int getNodeActiveScore() { |
| 20 | + long inPongTotalCount = nodeStatistics.messageStatistics.discoverInPong.getTotalCount(); |
| 21 | + return inPongTotalCount == 0 ? 0 : 100; |
63 | 22 | } |
64 | 23 |
|
65 | | - public class DiscoverScore extends Score<MessageStatistics> { |
66 | | - |
67 | | - public DiscoverScore(MessageStatistics messageStatistics) { |
68 | | - super(messageStatistics); |
69 | | - } |
70 | | - |
71 | | - @Override |
72 | | - int calculate(int baseScore) { |
73 | | - int discoverReput = baseScore; |
74 | | - discoverReput += |
75 | | - min(t.discoverInPong.getTotalCount(), 1) * (t.discoverOutPing.getTotalCount() |
76 | | - == t.discoverInPong.getTotalCount() ? 101 : 1); |
77 | | - discoverReput += |
78 | | - min(t.discoverInNeighbours.getTotalCount(), 1) * (t.discoverOutFindNode.getTotalCount() |
79 | | - == t.discoverInNeighbours.getTotalCount() ? 10 : 1); |
80 | | - return discoverReput; |
81 | | - } |
82 | | - |
83 | | - @Override |
84 | | - public boolean isContinue() { |
85 | | - return t.discoverOutPing.getTotalCount() == t.discoverInPong.getTotalCount() |
86 | | - && t.discoverInNeighbours.getTotalCount() <= t.discoverOutFindNode.getTotalCount(); |
87 | | - } |
| 24 | + private int getPacketLossRateScore() { |
| 25 | + MessageStatistics s = nodeStatistics.messageStatistics; |
| 26 | + long in = s.discoverInPong.getTotalCount() + s.discoverInNeighbours.getTotalCount(); |
| 27 | + long out = s.discoverOutPing.getTotalCount() + s.discoverOutFindNode.getTotalCount(); |
| 28 | + return out == 0 ? 0 : 100 - min((int) ((1 - (double) in / out) * 200), 100); |
88 | 29 | } |
89 | 30 |
|
90 | | - public class TcpScore extends Score<NodeStatistics> { |
91 | | - |
92 | | - public TcpScore(NodeStatistics nodeStatistics) { |
93 | | - super(nodeStatistics); |
94 | | - } |
95 | | - |
96 | | - @Override |
97 | | - int calculate(int baseScore) { |
98 | | - int reput = baseScore; |
99 | | - reput += t.p2pHandShake.getTotalCount() > 0 ? 10 : 0; |
100 | | - reput += min(t.tcpFlow.getTotalCount() / 10240, 20); |
101 | | - reput += t.messageStatistics.p2pOutPing.getTotalCount() == t.messageStatistics.p2pInPong |
102 | | - .getTotalCount() ? 10 : 0; |
103 | | - return reput; |
104 | | - } |
| 31 | + private int getNetLatencyScore() { |
| 32 | + return (int) (nodeStatistics.discoverMessageLatency.getAvrg() == 0 ? 0 |
| 33 | + : min(1000 / nodeStatistics.discoverMessageLatency.getAvrg(), 20)); |
105 | 34 | } |
106 | 35 |
|
107 | | - public class DisConnectScore extends Score<NodeStatistics> { |
108 | | - |
109 | | - public DisConnectScore(NodeStatistics nodeStatistics) { |
110 | | - super(nodeStatistics); |
111 | | - } |
112 | | - |
113 | | - @Override |
114 | | - int calculate(int baseScore) { |
115 | | - if (t.wasDisconnected()) { |
116 | | - if (t.getTronLastLocalDisconnectReason() == null |
117 | | - && t.getTronLastRemoteDisconnectReason() == null) { |
118 | | - // means connection was dropped without reporting any reason - bad |
119 | | - baseScore *= 0.8; |
120 | | - } else if (t.getTronLastLocalDisconnectReason() != ReasonCode.REQUESTED) { |
121 | | - // the disconnect was not initiated by discover mode |
122 | | - if (t.getTronLastRemoteDisconnectReason() == ReasonCode.TOO_MANY_PEERS |
123 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.TOO_MANY_PEERS |
124 | | - || t.getTronLastRemoteDisconnectReason() == ReasonCode.TOO_MANY_PEERS_WITH_SAME_IP |
125 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.TOO_MANY_PEERS_WITH_SAME_IP |
126 | | - || t.getTronLastRemoteDisconnectReason() == ReasonCode.DUPLICATE_PEER |
127 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.DUPLICATE_PEER |
128 | | - || t.getTronLastRemoteDisconnectReason() == ReasonCode.TIME_OUT |
129 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.TIME_OUT |
130 | | - || t.getTronLastRemoteDisconnectReason() == ReasonCode.PING_TIMEOUT |
131 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.PING_TIMEOUT |
132 | | - || t.getTronLastRemoteDisconnectReason() == ReasonCode.CONNECT_FAIL |
133 | | - || t.getTronLastLocalDisconnectReason() == ReasonCode.CONNECT_FAIL) { |
134 | | - // The peer is popular, but we were unlucky |
135 | | - baseScore *= 0.9; |
136 | | - } else if (t.getTronLastLocalDisconnectReason() == ReasonCode.RESET) { |
137 | | - baseScore *= 0.95; |
138 | | - } else if (t.getTronLastRemoteDisconnectReason() != ReasonCode.REQUESTED) { |
139 | | - // other disconnect reasons |
140 | | - baseScore *= 0.7; |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - if (t.getDisconnectTimes() > 20) { |
145 | | - return 0; |
146 | | - } |
147 | | - int score = baseScore - (int) Math.pow(2, t.getDisconnectTimes()) |
148 | | - * (t.getDisconnectTimes() > 0 ? 10 : 0); |
149 | | - return score; |
150 | | - } |
| 36 | + private int getHandshakeScore() { |
| 37 | + return nodeStatistics.p2pHandShake.getTotalCount() > 0 ? 20 : 0; |
151 | 38 | } |
152 | 39 |
|
153 | | - public class OtherScore extends Score<NodeStatistics> { |
154 | | - |
155 | | - public OtherScore(NodeStatistics nodeStatistics) { |
156 | | - super(nodeStatistics); |
157 | | - } |
| 40 | + private int getTcpFlowScore() { |
| 41 | + return (int) min(nodeStatistics.tcpFlow.getTotalCount() / 10240, 20); |
| 42 | + } |
158 | 43 |
|
159 | | - @Override |
160 | | - int calculate(int baseScore) { |
161 | | - baseScore += (int) t.discoverMessageLatency.getAvrg() == 0 ? 0 |
162 | | - : min(1000 / t.discoverMessageLatency.getAvrg(), 20); |
163 | | - return baseScore; |
164 | | - } |
| 44 | + private int getDisconnectionScore() { |
| 45 | + return -10 * nodeStatistics.getDisconnectTimes(); |
165 | 46 | } |
166 | 47 |
|
167 | 48 | } |
0 commit comments