Skip to content
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

feat(net): optimize blockid hash validation in HelloMessage #5906

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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 @@ -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 @@ -127,7 +127,7 @@ private void sendHelloMessage(PeerConnection peer, long time) {
TronNetService.getP2pConfig().getIp(),
TronNetService.getP2pConfig().getIpv6(),
TronNetService.getP2pConfig().getPort());
HelloMessage message = new HelloMessage(node, time, ChainBaseManager.getChainBaseManager());
HelloMessage message = new HelloMessage(node, time, chainBaseManager);
relayService.fillHelloMessage(message, peer.getChannel());
peer.sendMessage(message);
peer.setHelloMessageSend(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void testInvalidHelloMessage() {
Protocol.HelloMessage.Builder builder =
getHelloMessageBuilder(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
//block hash is empty
//block hash length is not valid
try {
BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId();
Protocol.HelloMessage.BlockId hBlockId = Protocol.HelloMessage.BlockId.newBuilder()
Expand All @@ -132,7 +132,15 @@ public void testInvalidHelloMessage() {
.build();
builder.setHeadBlockId(hBlockId);
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertTrue(!helloMessage.valid());
Assert.assertFalse(helloMessage.valid());

hBlockId = Protocol.HelloMessage.BlockId.newBuilder()
.setHash(ByteString.copyFrom(new byte[1]))
.setNumber(hid.getNum())
.build();
builder.setHeadBlockId(hBlockId);
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage.valid());
} catch (Exception e) {
Assert.fail();
}
Expand Down
Loading