Skip to content

Commit 115e9bd

Browse files
BarbatosBarbatos
authored andcommitted
style(p2p): replace log field with logger for java-tron convention
Replace all @Slf4j-generated 'log' references with 'logger' to match java-tron's lombok.config (lombok.log.fieldName=logger). Remove the temporary p2p/lombok.config override added in the previous commit. Checkstyle is not yet enabled for the p2p module due to ~1900 violations inherited from the original libp2p codebase. This will be addressed in a follow-up PR.
1 parent 735f85b commit 115e9bd

35 files changed

Lines changed: 171 additions & 170 deletions

p2p/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
apply plugin: 'com.google.protobuf'
22

3+
// TODO: enable checkstyle after code style alignment
4+
// apply plugin: 'checkstyle'
5+
36
def protobufVersion = '3.25.8'
47
def grpcVersion = '1.75.0'
58

p2p/lombok.config

Lines changed: 0 additions & 2 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void start(P2pConfig p2pConfig) {
2929
NodeManager.init();
3030
ChannelManager.init();
3131
DnsManager.init();
32-
log.info("P2p service started");
32+
logger.info("P2p service started");
3333

3434
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
3535
}
@@ -42,7 +42,7 @@ public void close() {
4242
DnsManager.close();
4343
NodeManager.close();
4444
ChannelManager.close();
45-
log.info("P2p service closed");
45+
logger.info("P2p service closed");
4646
}
4747

4848
public void register(P2pEventHandler p2PEventHandler) throws P2pException {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ public void processException(Throwable throwable) {
9494
baseThrowable = Throwables.getRootCause(baseThrowable);
9595
} catch (IllegalArgumentException e) {
9696
baseThrowable = e.getCause();
97-
log.warn("Loop in causal chain detected");
97+
logger.warn("Loop in causal chain detected");
9898
}
9999
SocketAddress address = ctx.channel().remoteAddress();
100100
if (throwable instanceof ReadTimeoutException
101101
|| throwable instanceof IOException
102102
|| throwable instanceof CorruptedFrameException) {
103-
log.warn("Close peer {}, reason: {}", address, throwable.getMessage());
103+
logger.warn("Close peer {}, reason: {}", address, throwable.getMessage());
104104
} else if (baseThrowable instanceof P2pException) {
105-
log.warn("Close peer {}, type: ({}), info: {}",
105+
logger.warn("Close peer {}, type: ({}), info: {}",
106106
address, ((P2pException) baseThrowable).getType(), baseThrowable.getMessage());
107107
} else {
108-
log.error("Close peer {}, exception caught", address, throwable);
108+
logger.error("Close peer {}, exception caught", address, throwable);
109109
}
110110
close();
111111
}
@@ -137,9 +137,9 @@ public void close() {
137137

138138
public void send(Message message) {
139139
if (message.needToLog()) {
140-
log.info("Send message to channel {}, {}", inetSocketAddress, message);
140+
logger.info("Send message to channel {}, {}", inetSocketAddress, message);
141141
} else {
142-
log.debug("Send message to channel {}, {}", inetSocketAddress, message);
142+
logger.debug("Send message to channel {}, {}", inetSocketAddress, message);
143143
}
144144
send(message.getSendData());
145145
}
@@ -148,7 +148,7 @@ public void send(byte[] data) {
148148
try {
149149
byte type = data[0];
150150
if (isDisconnect) {
151-
log.warn("Send to {} failed as channel has closed, message-type:{} ",
151+
logger.warn("Send to {} failed as channel has closed, message-type:{} ",
152152
ctx.channel().remoteAddress(), type);
153153
return;
154154
}
@@ -160,14 +160,14 @@ public void send(byte[] data) {
160160
ByteBuf byteBuf = Unpooled.wrappedBuffer(data);
161161
ctx.writeAndFlush(byteBuf).addListener((ChannelFutureListener) future -> {
162162
if (!future.isSuccess() && !isDisconnect) {
163-
log.warn("Send to {} failed, message-type:{}, cause:{}",
163+
logger.warn("Send to {} failed, message-type:{}, cause:{}",
164164
ctx.channel().remoteAddress(), ByteArray.byte2int(type),
165165
future.cause().getMessage());
166166
}
167167
});
168168
setLastSendTime(System.currentTimeMillis());
169169
} catch (Exception e) {
170-
log.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage());
170+
logger.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage());
171171
ctx.channel().close();
172172
}
173173
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static ChannelFuture connect(Node node, ChannelFutureListener future) {
8787

8888
public static void notifyDisconnect(Channel channel) {
8989
if (channel.getInetSocketAddress() == null) {
90-
log.warn("Notify Disconnect peer has no address.");
90+
logger.warn("Notify Disconnect peer has no address.");
9191
return;
9292
}
9393
channels.remove(channel.getInetSocketAddress());
@@ -114,18 +114,18 @@ public static synchronized DisconnectCode processPeer(Channel channel) {
114114
InetAddress inetAddress = channel.getInetAddress();
115115
if (bannedNodes.getIfPresent(inetAddress) != null
116116
&& bannedNodes.getIfPresent(inetAddress) > System.currentTimeMillis()) {
117-
log.info("Peer {} recently disconnected", channel);
117+
logger.info("Peer {} recently disconnected", channel);
118118
return DisconnectCode.TIME_BANNED;
119119
}
120120

121121
if (channels.size() >= Parameter.p2pConfig.getMaxConnections()) {
122-
log.info("Too many peers, disconnected with {}", channel);
122+
logger.info("Too many peers, disconnected with {}", channel);
123123
return DisconnectCode.TOO_MANY_PEERS;
124124
}
125125

126126
int num = getConnectionNum(channel.getInetAddress());
127127
if (num >= Parameter.p2pConfig.getMaxConnectionsWithSameIp()) {
128-
log.info("Max connection with same ip {}", channel);
128+
logger.info("Max connection with same ip {}", channel);
129129
return DisconnectCode.MAX_CONNECTION_WITH_SAME_IP;
130130
}
131131
}
@@ -136,7 +136,7 @@ public static synchronized DisconnectCode processPeer(Channel channel) {
136136
if (c.getStartTime() > channel.getStartTime()) {
137137
c.close();
138138
} else {
139-
log.info("Duplicate peer {}, exist peer {}", channel, c);
139+
logger.info("Duplicate peer {}, exist peer {}", channel, c);
140140
return DisconnectCode.DUPLICATE_PEER;
141141
}
142142
}
@@ -145,7 +145,7 @@ public static synchronized DisconnectCode processPeer(Channel channel) {
145145

146146
channels.put(channel.getInetSocketAddress(), channel);
147147

148-
log.info("Add peer {}, total channels: {}", channel.getInetSocketAddress(), channels.size());
148+
logger.info("Add peer {}, total channels: {}", channel.getInetSocketAddress(), channels.size());
149149
return DisconnectCode.NORMAL;
150150
}
151151

@@ -175,7 +175,7 @@ public static DisconnectReason getDisconnectReason(DisconnectCode code) {
175175
}
176176

177177
public static void logDisconnectReason(Channel channel, DisconnectReason reason) {
178-
log.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name());
178+
logger.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name());
179179
}
180180

181181
public static void banNode(InetAddress inetAddress, Long banTime) {
@@ -211,9 +211,9 @@ public static void processMessage(Channel channel, byte[] data) throws P2pExcept
211211
Message message = Message.parse(data);
212212

213213
if (message.needToLog()) {
214-
log.info("Receive message from channel: {}, {}", channel.getInetSocketAddress(), message);
214+
logger.info("Receive message from channel: {}, {}", channel.getInetSocketAddress(), message);
215215
} else {
216-
log.debug("Receive message from channel {}, {}", channel.getInetSocketAddress(), message);
216+
logger.debug("Receive message from channel {}, {}", channel.getInetSocketAddress(), message);
217217
}
218218

219219
switch (message.getType()) {
@@ -264,7 +264,7 @@ private static void handMessage(Channel channel, byte[] data) throws P2pExceptio
264264
public static synchronized void updateNodeId(Channel channel, String nodeId) {
265265
channel.setNodeId(nodeId);
266266
if (nodeId.equals(Hex.toHexString(Parameter.p2pConfig.getNodeID()))) {
267-
log.warn("Channel {} is myself", channel.getInetSocketAddress());
267+
logger.warn("Channel {} is myself", channel.getInetSocketAddress());
268268
channel.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER));
269269
channel.close();
270270
return;
@@ -282,11 +282,11 @@ public static synchronized void updateNodeId(Channel channel, String nodeId) {
282282
Channel c1 = list.get(0);
283283
Channel c2 = list.get(1);
284284
if (c1.getStartTime() > c2.getStartTime()) {
285-
log.info("Close channel {}, other channel {} is earlier", c1, c2);
285+
logger.info("Close channel {}, other channel {} is earlier", c1, c2);
286286
c1.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER));
287287
c1.close();
288288
} else {
289-
log.info("Close channel {}, other channel {} is earlier", c2, c1);
289+
logger.info("Close channel {}, other channel {} is earlier", c2, c1);
290290
c2.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER));
291291
c2.close();
292292
}

p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void init(PeerClient peerClient) {
6464
try {
6565
work();
6666
} catch (Exception t) {
67-
log.warn("Exception in node detect worker, {}", t.getMessage());
67+
logger.warn("Exception in node detect worker, {}", t.getMessage());
6868
}
6969
}, 1, 5, TimeUnit.SECONDS);
7070
}
@@ -137,7 +137,7 @@ private void detect(NodeStat stat) {
137137
setLastDetectTime(stat);
138138
peerClient.connectAsync(stat.getNode(), true);
139139
} catch (Exception e) {
140-
log.warn("Detect node {} failed, {}",
140+
logger.warn("Detect node {} failed, {}",
141141
stat.getNode().getPreferInetSocketAddress(), e.getMessage());
142142
nodeStatMap.remove(stat.getSocketAddress());
143143
}

p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void processMessage(Channel channel, Message message) {
2727
HelloMessage msg = (HelloMessage) message;
2828

2929
if (channel.isFinishHandshake()) {
30-
log.warn("Close channel {}, handshake is finished", channel.getInetSocketAddress());
30+
logger.warn("Close channel {}, handshake is finished", channel.getInetSocketAddress());
3131
channel.send(new P2pDisconnectMessage(DisconnectReason.DUP_HANDSHAKE));
3232
channel.close();
3333
return;
@@ -55,7 +55,7 @@ public void processMessage(Channel channel, Message message) {
5555
|| (msg.getNetworkId() != networkId && msg.getVersion() != networkId)) {
5656
DisconnectCode disconnectCode = DisconnectCode.forNumber(msg.getCode());
5757
//v0.1 have version, v0.2 both have version and networkId
58-
log.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}",
58+
logger.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}",
5959
channel.getInetSocketAddress(),
6060
msg.getCode(),
6161
disconnectCode.name(),
@@ -68,7 +68,7 @@ public void processMessage(Channel channel, Message message) {
6868
} else {
6969

7070
if (msg.getNetworkId() != networkId) {
71-
log.info("Peer {} different network id, peer->{}, me->{}",
71+
logger.info("Peer {} different network id, peer->{}, me->{}",
7272
channel.getInetSocketAddress(), msg.getNetworkId(), networkId);
7373
sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION, msg.getTimestamp());
7474
logDisconnectReason(channel, DisconnectReason.DIFFERENT_VERSION);

p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void init() {
4444
}
4545
});
4646
} catch (Exception t) {
47-
log.error("Exception in keep alive task", t);
47+
logger.error("Exception in keep alive task", t);
4848
}
4949
}, 2, 2, TimeUnit.SECONDS);
5050
}

p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void init(PeerClient peerClient) {
7777
try {
7878
connect(false);
7979
} catch (Exception t) {
80-
log.error("Exception in poolLoopExecutor worker", t);
80+
logger.error("Exception in poolLoopExecutor worker", t);
8181
}
8282
}, 200, 3600, TimeUnit.MILLISECONDS);
8383

@@ -86,7 +86,7 @@ public void init(PeerClient peerClient) {
8686
try {
8787
check();
8888
} catch (Exception t) {
89-
log.error("Exception in disconnectExecutor worker", t);
89+
logger.error("Exception in disconnectExecutor worker", t);
9090
}
9191
}, 30, 30, TimeUnit.SECONDS);
9292
}
@@ -189,12 +189,12 @@ private void connect(boolean isFilterActiveNodes) {
189189
connectNodes.addAll(newNodes);
190190
}
191191

192-
log.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}",
192+
logger.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}",
193193
size, connectNodes.size(), isFilterActiveNodes);
194194
//establish tcp connection with chose nodes by peerClient
195195
{
196196
connectNodes.forEach(n -> {
197-
log.info("Connect to peer {}", n.getPreferInetSocketAddress());
197+
logger.info("Connect to peer {}", n.getPreferInetSocketAddress());
198198
peerClient.connectAsync(n, false);
199199
peerClientCache.put(n.getPreferInetSocketAddress().getAddress(),
200200
System.currentTimeMillis());
@@ -254,14 +254,14 @@ private void check() {
254254
if (!peers.isEmpty()) {
255255
List<Channel> list = new ArrayList<>(peers);
256256
Channel peer = list.get(new Random().nextInt(peers.size()));
257-
log.info("Disconnect with peer randomly: {}", peer);
257+
logger.info("Disconnect with peer randomly: {}", peer);
258258
peer.send(new P2pDisconnectMessage(DisconnectReason.RANDOM_ELIMINATION));
259259
peer.close();
260260
}
261261
}
262262

263263
private synchronized void logActivePeers() {
264-
log.info("Peer stats: channels {}, activePeers {}, active {}, passive {}",
264+
logger.info("Peer stats: channels {}, activePeers {}, active {}, passive {}",
265265
ChannelManager.getChannels().size(), activePeers.size(), activePeersCount.get(),
266266
passivePeersCount.get());
267267
}
@@ -272,7 +272,7 @@ public void triggerConnect(InetSocketAddress address) {
272272
}
273273
connectingPeersCount.decrementAndGet();
274274
if (poolLoopExecutor.getQueue().size() >= Parameter.CONN_MAX_QUEUE_SIZE) {
275-
log.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE);
275+
logger.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE);
276276
return;
277277
}
278278
try {
@@ -281,12 +281,12 @@ public void triggerConnect(InetSocketAddress address) {
281281
try {
282282
connect(true);
283283
} catch (Exception t) {
284-
log.error("Exception in poolLoopExecutor worker", t);
284+
logger.error("Exception in poolLoopExecutor worker", t);
285285
}
286286
});
287287
}
288288
} catch (Exception e) {
289-
log.warn("Submit task failed, message:{}", e.getMessage());
289+
logger.warn("Submit task failed, message:{}", e.getMessage());
290290
}
291291
}
292292

@@ -333,7 +333,7 @@ public void close() {
333333
poolLoopExecutor.shutdownNow();
334334
disconnectExecutor.shutdownNow();
335335
} catch (Exception e) {
336-
log.warn("Problems shutting down executor", e);
336+
logger.warn("Problems shutting down executor", e);
337337
}
338338
}
339339
}

p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void handlerAdded(ChannelHandlerContext ctx) {
2929

3030
@Override
3131
public void channelActive(ChannelHandlerContext ctx) {
32-
log.debug("Channel active, {}", ctx.channel().remoteAddress());
32+
logger.debug("Channel active, {}", ctx.channel().remoteAddress());
3333
channel.setChannelHandlerContext(ctx);
3434
if (channel.isActive()) {
3535
if (channel.isDiscoveryMode()) {
@@ -76,7 +76,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
7676
}
7777
channel.processException(e);
7878
} catch (Throwable t) {
79-
log.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(),
79+
logger.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(),
8080
ByteArray.toHexString(data));
8181
throw t;
8282
}

0 commit comments

Comments
 (0)