Skip to content

Commit 6c29cb2

Browse files
authored
Merge pull request #6394 from jwrct/fix_reason_code
fix(net): fix the unclear reason codes returned in disconnection messages
2 parents ae6222c + 618b352 commit 6c29cb2

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ private void processException(PeerConnection peer, TronMessage msg, Exception ex
253253
code = Protocol.ReasonCode.BAD_TX;
254254
break;
255255
case BAD_BLOCK:
256+
case BLOCK_SIGN_ERROR:
256257
code = Protocol.ReasonCode.BAD_BLOCK;
257258
break;
258259
case NO_SUCH_MESSAGE:

framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.commons.lang3.StringUtils;
66
import org.tron.common.utils.ByteArray;
77
import org.tron.common.utils.DecodeUtil;
8+
import org.tron.common.utils.Sha256Hash;
89
import org.tron.common.utils.StringUtil;
910
import org.tron.core.ChainBaseManager;
1011
import org.tron.core.capsule.BlockCapsule;
@@ -156,17 +157,17 @@ public Protocol.HelloMessage getInstance() {
156157

157158
public boolean valid() {
158159
byte[] genesisBlockByte = this.helloMessage.getGenesisBlockId().getHash().toByteArray();
159-
if (genesisBlockByte.length == 0) {
160+
if (genesisBlockByte.length != Sha256Hash.LENGTH) {
160161
return false;
161162
}
162163

163164
byte[] solidBlockId = this.helloMessage.getSolidBlockId().getHash().toByteArray();
164-
if (solidBlockId.length == 0) {
165+
if (solidBlockId.length != Sha256Hash.LENGTH) {
165166
return false;
166167
}
167168

168169
byte[] headBlockId = this.helloMessage.getHeadBlockId().getHash().toByteArray();
169-
if (headBlockId.length == 0) {
170+
if (headBlockId.length != Sha256Hash.LENGTH) {
170171
return false;
171172
}
172173

framework/src/main/java/org/tron/core/net/service/handshake/HandshakeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) {
5757
msg.getInstance().getAddress().toByteArray().length,
5858
msg.getInstance().getSignature().toByteArray().length,
5959
msg.getInstance().getCodeVersion().toByteArray().length);
60-
peer.disconnect(ReasonCode.UNEXPECTED_IDENTITY);
60+
peer.disconnect(ReasonCode.INCOMPATIBLE_PROTOCOL);
6161
return;
6262
}
6363

framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.mockito.Mockito;
2222
import org.springframework.context.ApplicationContext;
2323
import org.tron.common.application.TronApplicationContext;
24-
import org.tron.common.utils.ByteArray;
2524
import org.tron.common.utils.ReflectUtils;
2625
import org.tron.common.utils.Sha256Hash;
2726
import org.tron.core.ChainBaseManager;
@@ -34,6 +33,7 @@
3433
import org.tron.core.net.message.handshake.HelloMessage;
3534
import org.tron.core.net.peer.PeerConnection;
3635
import org.tron.core.net.peer.PeerManager;
36+
import org.tron.core.net.service.handshake.HandshakeService;
3737
import org.tron.p2p.P2pConfig;
3838
import org.tron.p2p.base.Parameter;
3939
import org.tron.p2p.connection.Channel;
@@ -126,13 +126,27 @@ public void testInvalidHelloMessage() {
126126
//block hash is empty
127127
try {
128128
BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId();
129-
Protocol.HelloMessage.BlockId hBlockId = Protocol.HelloMessage.BlockId.newBuilder()
130-
.setHash(ByteString.copyFrom(new byte[0]))
129+
Protocol.HelloMessage.BlockId okBlockId = Protocol.HelloMessage.BlockId.newBuilder()
130+
.setHash(ByteString.copyFrom(new byte[32]))
131131
.setNumber(hid.getNum())
132132
.build();
133-
builder.setHeadBlockId(hBlockId);
133+
Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder()
134+
.setHash(ByteString.copyFrom(new byte[31]))
135+
.setNumber(hid.getNum())
136+
.build();
137+
builder.setHeadBlockId(invalidBlockId);
134138
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
135-
Assert.assertTrue(!helloMessage.valid());
139+
Assert.assertFalse(helloMessage.valid());
140+
141+
builder.setHeadBlockId(okBlockId);
142+
builder.setGenesisBlockId(invalidBlockId);
143+
HelloMessage helloMessage2 = new HelloMessage(builder.build().toByteArray());
144+
Assert.assertFalse(helloMessage2.valid());
145+
146+
builder.setGenesisBlockId(okBlockId);
147+
builder.setSolidBlockId(invalidBlockId);
148+
HelloMessage helloMessage3 = new HelloMessage(builder.build().toByteArray());
149+
Assert.assertFalse(helloMessage3.valid());
136150
} catch (Exception e) {
137151
Assert.fail();
138152
}
@@ -264,6 +278,36 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {
264278
}
265279
}
266280

281+
@Test
282+
public void testProcessHelloMessage() {
283+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
284+
Channel c1 = mock(Channel.class);
285+
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
286+
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
287+
PeerManager.add(ctx, c1);
288+
PeerConnection p = PeerManager.getPeers().get(0);
289+
290+
try {
291+
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(),
292+
null, a1.getPort());
293+
Protocol.HelloMessage.Builder builder =
294+
getHelloMessageBuilder(node, System.currentTimeMillis(),
295+
ChainBaseManager.getChainBaseManager());
296+
BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId();
297+
Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder()
298+
.setHash(ByteString.copyFrom(new byte[31]))
299+
.setNumber(hid.getNum())
300+
.build();
301+
builder.setHeadBlockId(invalidBlockId);
302+
303+
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
304+
HandshakeService handshakeService = new HandshakeService();
305+
handshakeService.processHelloMessage(p, helloMessage);
306+
} catch (Exception e) {
307+
Assert.fail();
308+
}
309+
}
310+
267311
private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long timestamp,
268312
ChainBaseManager chainBaseManager) {
269313
Endpoint fromEndpoint = getEndpointFromNode(from);

0 commit comments

Comments
 (0)