Skip to content

Commit 0d0ea17

Browse files
committed
optimization of node reputation
1 parent 85c19be commit 0d0ea17

3 files changed

Lines changed: 56 additions & 143 deletions

File tree

framework/src/main/java/org/tron/common/overlay/discover/node/statistics/NodeStatistics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public NodeStatistics() {
5656
public int getReputation() {
5757
int score = 0;
5858
if (!isReputationPenalized()) {
59-
score += persistedReputation / 5 + reputation.calculate();
59+
score += persistedReputation / 5 + reputation.getScore();
6060
}
6161
if (isPredefined) {
6262
score += REPUTATION_PREDEFINED;

framework/src/main/java/org/tron/common/overlay/discover/node/statistics/Reputation.java

Lines changed: 23 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,166 +2,47 @@
22

33
import static java.lang.Math.min;
44

5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import org.tron.protos.Protocol.ReasonCode;
8-
95
public class Reputation {
106

11-
private List<Score> scoreList = new ArrayList<>();
7+
private NodeStatistics nodeStatistics;
128

139
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;
1811

19-
scoreList.add(discoverScore);
20-
scoreList.add(tcpScore);
21-
scoreList.add(otherScore);
22-
scoreList.add(disconnectScore);
2312
}
2413

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();
3417
}
3518

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;
6322
}
6423

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);
8829
}
8930

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));
10534
}
10635

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;
15138
}
15239

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+
}
15843

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();
16546
}
16647

16748
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.tron.common.overlay.discover.node.statistics;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ReputationTest {
7+
NodeStatistics nodeStatistics = new NodeStatistics();
8+
Reputation reputation = new Reputation(nodeStatistics);
9+
10+
@Test
11+
public void testGetScore() {
12+
Assert.assertEquals(0, reputation.getScore());
13+
14+
nodeStatistics.messageStatistics.discoverInPong.add(3);
15+
Assert.assertEquals(100, reputation.getScore());
16+
17+
nodeStatistics.messageStatistics.discoverOutPing.add(3);
18+
Assert.assertEquals(200, reputation.getScore());
19+
20+
nodeStatistics.messageStatistics.discoverOutPing.add(1);
21+
Assert.assertEquals(150, reputation.getScore());
22+
23+
nodeStatistics.tcpFlow.add(10240 * 5);
24+
Assert.assertEquals(155, reputation.getScore());
25+
26+
nodeStatistics.discoverMessageLatency.add(100);
27+
Assert.assertEquals(165, reputation.getScore());
28+
29+
nodeStatistics.notifyDisconnect();
30+
Assert.assertEquals(155, reputation.getScore());
31+
}
32+
}

0 commit comments

Comments
 (0)