Skip to content

Commit 6d63bd6

Browse files
authored
Merge pull request #620 from tronprotocol/p2p_m
p2p: add p2p exception and perfect log
2 parents 236a17c + 12f59e0 commit 6d63bd6

File tree

7 files changed

+47
-65
lines changed

7 files changed

+47
-65
lines changed

src/main/java/org/tron/common/overlay/discover/message/Message.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.lang3.ArrayUtils;
44
import org.tron.common.utils.Sha256Hash;
5+
import org.tron.core.exception.P2pException;
56

67
public abstract class Message {
78

@@ -46,7 +47,7 @@ public int hashCode() {
4647
return getMessageId().hashCode();
4748
}
4849

49-
public static Message parse(byte[] encode) {
50+
public static Message parse(byte[] encode) throws Exception{
5051
byte type = encode[0];
5152
byte[] data = ArrayUtils.subarray(encode, 1, encode.length);
5253
switch (type) {
@@ -59,7 +60,7 @@ public static Message parse(byte[] encode) {
5960
case 4:
6061
return new NeighborsMessage(data);
6162
default:
62-
throw new IllegalArgumentException("No such message");
63+
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type=" + encode[0]);
6364
}
6465
}
6566
}

src/main/java/org/tron/common/overlay/message/MessageCodec.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
/*
2-
* Copyright (c) [2016] [ <ether.camp> ]
3-
* This file is part of the ethereumJ library.
4-
*
5-
* The ethereumJ library is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU Lesser General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* The ethereumJ library is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU Lesser General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU Lesser General Public License
16-
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
17-
*/
181
package org.tron.common.overlay.message;
192

203
import io.netty.buffer.ByteBuf;
@@ -31,13 +14,10 @@
3114
import org.springframework.context.annotation.Scope;
3215
import org.springframework.stereotype.Component;
3316
import org.tron.common.overlay.server.Channel;
17+
import org.tron.core.exception.P2pException;
3418
import org.tron.core.net.message.MessageTypes;
3519
import org.tron.core.net.message.TronMessageFactory;
3620

37-
38-
/**
39-
* The Netty codec which encodes/decodes RPLx frames to subprotocol Messages
40-
*/
4121
@Component
4222
@Scope("prototype")
4323
public class MessageCodec extends ByteToMessageDecoder {
@@ -63,15 +43,15 @@ public void setChannel(Channel channel) {
6343
this.channel = channel;
6444
}
6545

66-
private Message createMessage(byte[] encoded) {
46+
private Message createMessage(byte[] encoded) throws Exception{
6747
byte type = encoded[0];
6848
if (MessageTypes.inP2pRange(type)) {
6949
return p2pMessageFactory.create(encoded);
7050
}
7151
if (MessageTypes.inTronRange(type)) {
7252
return tronMessageFactory.create(encoded);
7353
}
74-
throw new Error(MessageFactory.ERR_NO_SUCH_MSG + ", type=" + type + ", len=" + encoded.length);
54+
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type=" + encoded[0]);
7555
}
7656

7757
}

src/main/java/org/tron/common/overlay/message/MessageFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public abstract class MessageFactory {
1919
public static String ERR_NO_SUCH_MSG = "No such message";
2020
public static String ERR_PARSE_FAILED = "parse message failed";
2121

22-
protected abstract Message create(byte[] data);
22+
protected abstract Message create(byte[] data) throws Exception;
2323

2424
}

src/main/java/org/tron/common/overlay/message/P2pMessageFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,30 @@
1919
package org.tron.common.overlay.message;
2020

2121
import org.apache.commons.lang3.ArrayUtils;
22+
import org.tron.core.exception.P2pException;
2223
import org.tron.core.net.message.MessageTypes;
2324

2425
public class P2pMessageFactory extends MessageFactory {
2526

2627
@Override
27-
public P2pMessage create(byte[] data) {
28+
public P2pMessage create(byte[] data) throws Exception{
2829
try {
2930
byte type = data[0];
3031
byte[] rawData = ArrayUtils.subarray(data, 1, data.length);
3132
return create(type, rawData);
3233
} catch (Exception e) {
33-
if (e.getMessage() != null && e.getMessage().contains(MessageFactory.ERR_NO_SUCH_MSG)){
34+
if (e instanceof P2pException){
3435
throw e;
3536
}else {
36-
throw new Error(MessageFactory.ERR_PARSE_FAILED + ", type=" + data[0] + ", len=" + data.length);
37+
throw new P2pException(P2pException.TypeEnum.PARSE_MESSAGE_FAILED, "type=" + data[0] + ", len=" + data.length);
3738
}
3839
}
3940
}
4041

41-
private P2pMessage create(byte type, byte[] rawData) {
42+
private P2pMessage create(byte type, byte[] rawData) throws Exception{
4243
MessageTypes messageType = MessageTypes.fromByte(type);
4344
if (messageType == null){
44-
throw new RuntimeException(MessageFactory.ERR_NO_SUCH_MSG + ", type=" + type);
45+
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type=" + type);
4546
}
4647
switch (messageType) {
4748
case P2P_HELLO:
@@ -53,7 +54,7 @@ private P2pMessage create(byte type, byte[] rawData) {
5354
case P2P_PONG:
5455
return new PongMessage(type, rawData);
5556
default:
56-
throw new Error(MessageFactory.ERR_NO_SUCH_MSG + ", " + messageType);
57+
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, messageType.toString());
5758
}
5859
}
5960
}

src/main/java/org/tron/common/overlay/server/Channel.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.netty.handler.timeout.ReadTimeoutHandler;
2626
import java.io.IOException;
2727
import java.net.InetSocketAddress;
28+
import java.net.SocketAddress;
2829
import java.util.concurrent.TimeUnit;
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
@@ -36,6 +37,7 @@
3637
import org.tron.common.overlay.discover.NodeStatistics;
3738
import org.tron.common.overlay.message.*;
3839
import org.tron.core.db.ByteArrayWrapper;
40+
import org.tron.core.exception.P2pException;
3941
import org.tron.core.net.peer.PeerConnectionDelegate;
4042
import org.tron.core.net.peer.TronHandler;
4143

@@ -152,14 +154,15 @@ public void disconnect(ReasonCode reason) {
152154

153155
public void processException(Throwable throwable){
154156
String errMsg = throwable.getMessage();
157+
SocketAddress address = ctx.channel().remoteAddress();
155158
if (throwable instanceof ReadTimeoutException){
156-
logger.error("Read timeout, {}", ctx.channel().remoteAddress());
157-
}else if (errMsg != null && (errMsg.contains(MessageFactory.ERR_NO_SUCH_MSG) ||
158-
errMsg.contains(MessageFactory.ERR_PARSE_FAILED) ||
159-
errMsg.contains("Connection reset by peer"))){
160-
logger.error("{}, {}", errMsg, ctx.channel().remoteAddress());
159+
logger.error("Read timeout, {}", address);
160+
}else if (errMsg != null && errMsg.contains("Connection reset by peer")){
161+
logger.error("{}, {}", errMsg, address);
162+
}else if(throwable instanceof P2pException){
163+
logger.error("type: {}, info: {}, {}", ((P2pException) throwable).getType(), errMsg, address);
161164
}else {
162-
logger.error("exception caught, {}", ctx.channel().remoteAddress(), throwable);
165+
logger.error("exception caught, {}", address, throwable);
163166
}
164167
close();
165168
}

src/main/java/org/tron/core/exception/P2pException.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,53 @@
22

33
public class P2pException extends Exception {
44

5-
private P2pExceptionTypeEnum type;
5+
private TypeEnum type;
66

7-
public P2pException(P2pExceptionTypeEnum type){
7+
public P2pException(TypeEnum type){
88
super(type.getDesc());
99
this.type = type;
1010
}
11-
public P2pException(P2pExceptionTypeEnum type, String errMsg){
11+
public P2pException(TypeEnum type, String errMsg){
1212
super(errMsg);
1313
this.type = type;
1414
}
15-
public P2pException(P2pExceptionTypeEnum type, Throwable throwable){
15+
public P2pException(TypeEnum type, Throwable throwable){
1616
super(type.getDesc(), throwable);
1717
this.type = type;
1818
}
19-
public P2pException(P2pExceptionTypeEnum type, String errMsg, Throwable throwable){
19+
public P2pException(TypeEnum type, String errMsg, Throwable throwable){
2020
super(errMsg, throwable);
2121
this.type = type;
2222
}
2323

24-
public P2pExceptionTypeEnum getType() {
24+
public TypeEnum getType() {
2525
return type;
2626
}
2727

28-
public enum P2pExceptionTypeEnum {
29-
28+
public enum TypeEnum {
3029
NO_SUCH_MESSAGE (1, "No such message"),
3130
PARSE_MESSAGE_FAILED (2, "Parse message failed"),
3231
DEFAULT (100, "default exception");
3332

34-
private Integer type;
35-
private String description;
36-
37-
P2pExceptionTypeEnum(Integer type, String description) {
38-
this.type = type;
39-
this.description = description;
40-
}
33+
private Integer value;
34+
private String desc;
4135

42-
public Integer getType() {
43-
return type;
36+
TypeEnum(Integer value, String desc) {
37+
this.value = value;
38+
this.desc = desc;
4439
}
4540

46-
public void setType(Integer type) {
47-
this.type = type;
41+
public Integer getValue() {
42+
return value;
4843
}
4944

5045
public String getDesc() {
51-
return description;
46+
return desc;
5247
}
5348

54-
public void setDesc(String description) {
55-
this.description = description;
49+
@Override
50+
public String toString(){
51+
return value + ", " + desc;
5652
}
5753
}
5854

src/main/java/org/tron/core/net/message/TronMessageFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@
22

33
import org.apache.commons.lang3.ArrayUtils;
44
import org.tron.common.overlay.message.MessageFactory;
5+
import org.tron.core.exception.P2pException;
56

67
/**
78
* msg factory.
89
*/
910
public class TronMessageFactory extends MessageFactory {
1011

1112
@Override
12-
public TronMessage create(byte[] data) {
13+
public TronMessage create(byte[] data) throws Exception{
1314
try {
1415
byte type = data[0];
1516
byte[] rawData = ArrayUtils.subarray(data, 1, data.length);
1617
return create(type, rawData);
1718
} catch (Exception e) {
18-
if (e.getMessage() != null && e.getMessage().contains(MessageFactory.ERR_NO_SUCH_MSG)){
19+
if (e instanceof P2pException){
1920
throw e;
2021
}else {
21-
throw new Error(MessageFactory.ERR_PARSE_FAILED + ", type=" + data[0] + ", len=" + data.length);
22+
throw new P2pException(P2pException.TypeEnum.PARSE_MESSAGE_FAILED, "type=" + data[0] + ", len=" + data.length);
2223
}
2324
}
2425
}
2526

26-
private TronMessage create(byte type, byte[] packed) {
27+
private TronMessage create(byte type, byte[] packed) throws Exception{
2728
MessageTypes receivedTypes = MessageTypes.fromByte(type);
2829
if (receivedTypes == null){
2930
throw new RuntimeException(MessageFactory.ERR_NO_SUCH_MSG + ", type=" + type);
@@ -56,7 +57,7 @@ private TronMessage create(byte type, byte[] packed) {
5657
case TRX_INVENTORY:
5758
return new TransactionInventoryMessage(packed);
5859
default:
59-
throw new Error(MessageFactory.ERR_NO_SUCH_MSG + ", " + receivedTypes);
60+
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, receivedTypes.toString());
6061
}
6162
}
6263
}

0 commit comments

Comments
 (0)