Skip to content

fix(net): fix the unclear reason codes returned in disconnection messages #6394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private void processException(PeerConnection peer, TronMessage msg, Exception ex
code = Protocol.ReasonCode.BAD_TX;
break;
case BAD_BLOCK:
case BLOCK_SIGN_ERROR:
code = Protocol.ReasonCode.BAD_BLOCK;
break;
case NO_SUCH_MESSAGE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.lang3.StringUtils;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.Sha256Hash;
import org.tron.common.utils.StringUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.capsule.BlockCapsule;
Expand Down Expand Up @@ -156,17 +157,17 @@ public Protocol.HelloMessage getInstance() {

public boolean valid() {
byte[] genesisBlockByte = this.helloMessage.getGenesisBlockId().getHash().toByteArray();
if (genesisBlockByte.length == 0) {
if (genesisBlockByte.length != Sha256Hash.LENGTH) {
return false;
}

byte[] solidBlockId = this.helloMessage.getSolidBlockId().getHash().toByteArray();
if (solidBlockId.length == 0) {
if (solidBlockId.length != Sha256Hash.LENGTH) {
return false;
}

byte[] headBlockId = this.helloMessage.getHeadBlockId().getHash().toByteArray();
if (headBlockId.length == 0) {
if (headBlockId.length != Sha256Hash.LENGTH) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) {
msg.getInstance().getAddress().toByteArray().length,
msg.getInstance().getSignature().toByteArray().length,
msg.getInstance().getCodeVersion().toByteArray().length);
peer.disconnect(ReasonCode.UNEXPECTED_IDENTITY);
peer.disconnect(ReasonCode.INCOMPATIBLE_PROTOCOL);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ReflectUtils;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.ChainBaseManager;
Expand All @@ -34,6 +33,7 @@
import org.tron.core.net.message.handshake.HelloMessage;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.net.peer.PeerManager;
import org.tron.core.net.service.handshake.HandshakeService;
import org.tron.p2p.P2pConfig;
import org.tron.p2p.base.Parameter;
import org.tron.p2p.connection.Channel;
Expand Down Expand Up @@ -126,13 +126,27 @@ public void testInvalidHelloMessage() {
//block hash is empty
try {
BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId();
Protocol.HelloMessage.BlockId hBlockId = Protocol.HelloMessage.BlockId.newBuilder()
.setHash(ByteString.copyFrom(new byte[0]))
Protocol.HelloMessage.BlockId okBlockId = Protocol.HelloMessage.BlockId.newBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using "validBLockId" instead of "okBlockId"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case adjustment only.

.setHash(ByteString.copyFrom(new byte[32]))
.setNumber(hid.getNum())
.build();
builder.setHeadBlockId(hBlockId);
Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder()
.setHash(ByteString.copyFrom(new byte[31]))
.setNumber(hid.getNum())
.build();
builder.setHeadBlockId(invalidBlockId);
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertTrue(!helloMessage.valid());
Assert.assertFalse(helloMessage.valid());

builder.setHeadBlockId(okBlockId);
builder.setGenesisBlockId(invalidBlockId);
HelloMessage helloMessage2 = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage2.valid());

builder.setGenesisBlockId(okBlockId);
builder.setSolidBlockId(invalidBlockId);
HelloMessage helloMessage3 = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage3.valid());
} catch (Exception e) {
Assert.fail();
}
Expand Down Expand Up @@ -264,6 +278,36 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {
}
}

@Test
public void testProcessHelloMessage() {
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Channel c1 = mock(Channel.class);
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
PeerManager.add(ctx, c1);
PeerConnection p = PeerManager.getPeers().get(0);

try {
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(),
null, a1.getPort());
Protocol.HelloMessage.Builder builder =
getHelloMessageBuilder(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId();
Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder()
.setHash(ByteString.copyFrom(new byte[31]))
.setNumber(hid.getNum())
.build();
builder.setHeadBlockId(invalidBlockId);

HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
HandshakeService handshakeService = new HandshakeService();
handshakeService.processHelloMessage(p, helloMessage);
} catch (Exception e) {
Assert.fail();
}
}

private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long timestamp,
ChainBaseManager chainBaseManager) {
Endpoint fromEndpoint = getEndpointFromNode(from);
Expand Down