Skip to content

Commit 7e88b13

Browse files
BarbatosBarbatos
authored andcommitted
style(p2p): fix checkstyle violations and enable checkstyle
Apply google-java-format for bulk formatting (indentation, imports, whitespace), then fix remaining violations manually: - Lambda indentation adjustments for checkstyle 8.7 compatibility - Line length wrapping for long strings and license headers - Empty catch blocks: add // expected comments - Star import replacement, multiple variable declaration splits Enable checkstyle plugin in p2p/build.gradle with proto-generated code excluded. Both checkstyleMain and checkstyleTest now pass.
1 parent 813ee97 commit 7e88b13

103 files changed

Lines changed: 2382 additions & 2033 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

p2p/build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
apply plugin: 'com.google.protobuf'
2+
apply plugin: 'checkstyle'
23

3-
// TODO: enable checkstyle after code style alignment
4-
// apply plugin: 'checkstyle'
4+
checkstyle {
5+
toolVersion = '8.7'
6+
configFile = file("${rootDir}/config/checkstyle/checkStyleAll.xml")
7+
maxWarnings = 0
8+
}
9+
10+
checkstyleMain {
11+
source = 'src/main/java'
12+
exclude '**/protos/**'
13+
}
514

615
def protobufVersion = '3.25.8'
716
def grpcVersion = '1.75.0'

p2p/src/main/java/org/tron/p2p/P2pConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public class P2pConfig {
2929
private boolean disconnectionPolicyEnable = false;
3030
private boolean nodeDetectEnable = false;
3131

32-
//dns read config
32+
// dns read config
3333
private List<String> treeUrls = new ArrayList<>();
3434

35-
//dns publish config
35+
// dns publish config
3636
private PublishConfig publishConfig = new PublishConfig();
3737
}

p2p/src/main/java/org/tron/p2p/P2pEventHandler.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66

77
public abstract class P2pEventHandler {
88

9-
@Getter
10-
protected Set<Byte> messageTypes;
9+
@Getter protected Set<Byte> messageTypes;
1110

12-
public void onConnect(Channel channel) {
13-
}
11+
public void onConnect(Channel channel) {}
1412

15-
public void onDisconnect(Channel channel) {
16-
}
13+
public void onDisconnect(Channel channel) {}
1714

18-
public void onMessage(Channel channel, byte[] data) {
19-
}
15+
public void onMessage(Channel channel, byte[] data) {}
2016
}

p2p/src/main/java/org/tron/p2p/base/Constant.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
public class Constant {
77

88
public static final int NODE_ID_LEN = 64;
9-
public static final List<String> ipV4Urls = Arrays.asList(
10-
"http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/");
11-
public static final List<String> ipV6Urls = Arrays.asList(
12-
"https://v6.ident.me", "http://6.ipw.cn/", "https://api6.ipify.org",
13-
"https://ipv6.icanhazip.com");
14-
public static final String ipV4Hex = "00000000"; //32 bit
15-
public static final String ipV6Hex = "00000000000000000000000000000000"; //128 bit
9+
public static final List<String> ipV4Urls =
10+
Arrays.asList("http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/");
11+
public static final List<String> ipV6Urls =
12+
Arrays.asList(
13+
"https://v6.ident.me",
14+
"http://6.ipw.cn/",
15+
"https://api6.ipify.org",
16+
"https://ipv6.icanhazip.com");
17+
public static final String ipV4Hex = "00000000"; // 32 bit
18+
public static final String ipV6Hex = "00000000000000000000000000000000"; // 128 bit
1619
}

p2p/src/main/java/org/tron/p2p/base/Parameter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.tron.p2p.base;
22

3+
import com.google.protobuf.ByteString;
34
import java.util.ArrayList;
45
import java.util.HashMap;
56
import java.util.List;
67
import java.util.Map;
7-
8-
import com.google.protobuf.ByteString;
98
import lombok.Data;
109
import org.apache.commons.lang3.StringUtils;
1110
import org.tron.p2p.P2pConfig;
@@ -59,16 +58,16 @@ public static void addP2pEventHandle(P2pEventHandler p2PEventHandler) throws P2p
5958
}
6059

6160
public static Discover.Endpoint getHomeNode() {
62-
Discover.Endpoint.Builder builder = Discover.Endpoint.newBuilder()
63-
.setNodeId(ByteString.copyFrom(Parameter.p2pConfig.getNodeID()))
64-
.setPort(Parameter.p2pConfig.getPort());
61+
Discover.Endpoint.Builder builder =
62+
Discover.Endpoint.newBuilder()
63+
.setNodeId(ByteString.copyFrom(Parameter.p2pConfig.getNodeID()))
64+
.setPort(Parameter.p2pConfig.getPort());
6565
if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) {
66-
builder.setAddress(ByteString.copyFrom(
67-
ByteArray.fromString(Parameter.p2pConfig.getIp())));
66+
builder.setAddress(ByteString.copyFrom(ByteArray.fromString(Parameter.p2pConfig.getIp())));
6867
}
6968
if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) {
70-
builder.setAddressIpv6(ByteString.copyFrom(
71-
ByteArray.fromString(Parameter.p2pConfig.getIpv6())));
69+
builder.setAddressIpv6(
70+
ByteString.copyFrom(ByteArray.fromString(Parameter.p2pConfig.getIpv6())));
7271
}
7372
return builder.build();
7473
}

p2p/src/main/java/org/tron/p2p/connection/Channel.java

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,22 @@ public class Channel {
3737
public volatile boolean waitForPong = false;
3838
public volatile long pingSent = System.currentTimeMillis();
3939

40-
@Getter
41-
private HelloMessage helloMessage;
42-
@Getter
43-
private Node node;
44-
@Getter
45-
private int version;
46-
@Getter
47-
private ChannelHandlerContext ctx;
48-
@Getter
49-
private InetSocketAddress inetSocketAddress;
50-
@Getter
51-
private InetAddress inetAddress;
52-
@Getter
53-
private volatile long disconnectTime;
54-
@Getter
55-
@Setter
56-
private volatile boolean isDisconnect = false;
57-
@Getter
58-
@Setter
59-
private long lastSendTime = System.currentTimeMillis();
60-
@Getter
61-
private final long startTime = System.currentTimeMillis();
62-
@Getter
63-
private boolean isActive = false;
64-
@Getter
65-
private boolean isTrustPeer;
66-
@Getter
67-
@Setter
68-
private volatile boolean finishHandshake;
69-
@Getter
70-
@Setter
71-
private String nodeId;
72-
@Setter
73-
@Getter
74-
private boolean discoveryMode;
75-
@Getter
76-
private long avgLatency;
40+
@Getter private HelloMessage helloMessage;
41+
@Getter private Node node;
42+
@Getter private int version;
43+
@Getter private ChannelHandlerContext ctx;
44+
@Getter private InetSocketAddress inetSocketAddress;
45+
@Getter private InetAddress inetAddress;
46+
@Getter private volatile long disconnectTime;
47+
@Getter @Setter private volatile boolean isDisconnect = false;
48+
@Getter @Setter private long lastSendTime = System.currentTimeMillis();
49+
@Getter private final long startTime = System.currentTimeMillis();
50+
@Getter private boolean isActive = false;
51+
@Getter private boolean isTrustPeer;
52+
@Getter @Setter private volatile boolean finishHandshake;
53+
@Getter @Setter private String nodeId;
54+
@Setter @Getter private boolean discoveryMode;
55+
@Getter private long avgLatency;
7756
private long count;
7857

7958
public void init(ChannelPipeline pipeline, String nodeId, boolean discoveryMode) {
@@ -102,8 +81,11 @@ public void processException(Throwable throwable) {
10281
|| throwable instanceof CorruptedFrameException) {
10382
logger.warn("Close peer {}, reason: {}", address, throwable.getMessage());
10483
} else if (baseThrowable instanceof P2pException) {
105-
logger.warn("Close peer {}, type: ({}), info: {}",
106-
address, ((P2pException) baseThrowable).getType(), baseThrowable.getMessage());
84+
logger.warn(
85+
"Close peer {}, type: ({}), info: {}",
86+
address,
87+
((P2pException) baseThrowable).getType(),
88+
baseThrowable.getMessage());
10789
} else {
10890
logger.error("Close peer {}, exception caught", address, throwable);
10991
}
@@ -113,7 +95,7 @@ public void processException(Throwable throwable) {
11395
public void setHelloMessage(HelloMessage helloMessage) {
11496
this.helloMessage = helloMessage;
11597
this.node = helloMessage.getFrom();
116-
this.nodeId = node.getHexId(); //update node id from handshake
98+
this.nodeId = node.getHexId(); // update node id from handshake
11799
this.version = helloMessage.getVersion();
118100
}
119101

@@ -148,8 +130,10 @@ public void send(byte[] data) {
148130
try {
149131
byte type = data[0];
150132
if (isDisconnect) {
151-
logger.warn("Send to {} failed as channel has closed, message-type:{} ",
152-
ctx.channel().remoteAddress(), type);
133+
logger.warn(
134+
"Send to {} failed as channel has closed, message-type:{} ",
135+
ctx.channel().remoteAddress(),
136+
type);
153137
return;
154138
}
155139

@@ -158,13 +142,16 @@ public void send(byte[] data) {
158142
}
159143

160144
ByteBuf byteBuf = Unpooled.wrappedBuffer(data);
161-
ctx.writeAndFlush(byteBuf).addListener((ChannelFutureListener) future -> {
162-
if (!future.isSuccess() && !isDisconnect) {
163-
logger.warn("Send to {} failed, message-type:{}, cause:{}",
164-
ctx.channel().remoteAddress(), ByteArray.byte2int(type),
165-
future.cause().getMessage());
166-
}
167-
});
145+
ctx.writeAndFlush(byteBuf)
146+
.addListener((ChannelFutureListener) future -> {
147+
if (!future.isSuccess() && !isDisconnect) {
148+
logger.warn(
149+
"Send to {} failed, message-type:{}, cause:{}",
150+
ctx.channel().remoteAddress(),
151+
ByteArray.byte2int(type),
152+
future.cause().getMessage());
153+
}
154+
});
168155
setLastSendTime(System.currentTimeMillis());
169156
} catch (Exception e) {
170157
logger.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage());
@@ -197,8 +184,7 @@ public int hashCode() {
197184

198185
@Override
199186
public String toString() {
200-
return String.format("%s | %s", inetSocketAddress,
201-
StringUtils.isEmpty(nodeId) ? "<null>" : nodeId);
187+
return String.format(
188+
"%s | %s", inetSocketAddress, StringUtils.isEmpty(nodeId) ? "<null>" : nodeId);
202189
}
203-
204190
}

p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,23 @@
3535
@Slf4j(topic = "net")
3636
public class ChannelManager {
3737

38-
@Getter
39-
private static NodeDetectService nodeDetectService;
38+
@Getter private static NodeDetectService nodeDetectService;
4039

4140
private static PeerServer peerServer;
4241

43-
@Getter
44-
private static PeerClient peerClient;
42+
@Getter private static PeerClient peerClient;
4543

46-
@Getter
47-
private static ConnPoolService connPoolService;
44+
@Getter private static ConnPoolService connPoolService;
4845

4946
private static KeepAliveService keepAliveService;
5047

51-
@Getter
52-
private static HandshakeService handshakeService;
48+
@Getter private static HandshakeService handshakeService;
5349

54-
@Getter
55-
private static final Map<InetSocketAddress, Channel> channels = new ConcurrentHashMap<>();
50+
@Getter private static final Map<InetSocketAddress, Channel> channels = new ConcurrentHashMap<>();
5651

5752
@Getter
58-
private static final Cache<InetAddress, Long> bannedNodes = CacheBuilder
59-
.newBuilder().maximumSize(2000).build(); //ban timestamp
53+
private static final Cache<InetAddress, Long> bannedNodes =
54+
CacheBuilder.newBuilder().maximumSize(2000).build(); // ban timestamp
6055

6156
private static boolean isInit = false;
6257
public static volatile boolean isShutdown = false;
@@ -77,7 +72,9 @@ public static void init() {
7772
}
7873

7974
public static void connect(InetSocketAddress address) {
80-
peerClient.connect(address.getAddress().getHostAddress(), address.getPort(),
75+
peerClient.connect(
76+
address.getAddress().getHostAddress(),
77+
address.getPort(),
8178
ByteArray.toHexString(NetUtil.getNodeId()));
8279
}
8380

@@ -167,15 +164,15 @@ public static DisconnectReason getDisconnectReason(DisconnectCode code) {
167164
case MAX_CONNECTION_WITH_SAME_IP:
168165
disconnectReason = DisconnectReason.TOO_MANY_PEERS_WITH_SAME_IP;
169166
break;
170-
default: {
167+
default:
171168
disconnectReason = DisconnectReason.UNKNOWN;
172-
}
173169
}
174170
return disconnectReason;
175171
}
176172

177173
public static void logDisconnectReason(Channel channel, DisconnectReason reason) {
178-
logger.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name());
174+
logger.info(
175+
"Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name());
179176
}
180177

181178
public static void banNode(InetAddress inetAddress, Long banTime) {
@@ -198,7 +195,6 @@ public static void close() {
198195
nodeDetectService.close();
199196
}
200197

201-
202198
public static void processMessage(Channel channel, byte[] data) throws P2pException {
203199
if (data == null || data.length == 0) {
204200
throw new P2pException(TypeEnum.EMPTY_MESSAGE, "");
@@ -271,11 +267,14 @@ public static synchronized void updateNodeId(Channel channel, String nodeId) {
271267
}
272268

273269
List<Channel> list = new ArrayList<>();
274-
channels.values().forEach(c -> {
275-
if (nodeId.equals(c.getNodeId())) {
276-
list.add(c);
277-
}
278-
});
270+
channels
271+
.values()
272+
.forEach(
273+
c -> {
274+
if (nodeId.equals(c.getNodeId())) {
275+
list.add(c);
276+
}
277+
});
279278
if (list.size() <= 1) {
280279
return;
281280
}

0 commit comments

Comments
 (0)