Skip to content

fix(net): optimize disconnect reason for light node #6375

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

Open
wants to merge 12 commits into
base: release_v4.8.1
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Component;
import org.tron.common.utils.ByteArray;
import org.tron.core.ChainBaseManager;
import org.tron.core.ChainBaseManager.NodeType;
import org.tron.core.config.args.Args;
import org.tron.core.net.TronNetService;
import org.tron.core.net.message.handshake.HelloMessage;
Expand Down Expand Up @@ -96,12 +97,17 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) {
}

if (chainBaseManager.getSolidBlockId().getNum() >= msg.getSolidBlockId().getNum()
&& !chainBaseManager.containBlockInMainChain(msg.getSolidBlockId())) {
logger.info("Peer {} different solid block, peer->{}, me->{}",
peer.getInetSocketAddress(),
msg.getSolidBlockId().getString(),
chainBaseManager.getSolidBlockId().getString());
peer.disconnect(ReasonCode.FORKED);
&& !chainBaseManager.containBlockInMainChain(msg.getSolidBlockId())) {
if (chainBaseManager.getLowestBlockNum() <= msg.getSolidBlockId().getNum()) {

Choose a reason for hiding this comment

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

Suggest add comments something like "It maybe light FullNode with part of database snapshot"

logger.info("Peer {} different solid block, fork with me, peer->{}, me->{}",
peer.getInetSocketAddress(),
msg.getSolidBlockId().getString(),
chainBaseManager.getSolidBlockId().getString());
peer.disconnect(ReasonCode.FORKED);
} else {
logger.info("Peer {} solid block is below than my lowest", peer.getInetSocketAddress());
peer.disconnect(ReasonCode.LIGHT_NODE_SYNC_FAIL);
}
return;
}

Expand Down
13 changes: 13 additions & 0 deletions framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import static org.tron.keystore.Wallet.generateRandomBytes;

import com.google.protobuf.ByteString;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tron.common.utils.ByteArray;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.services.jsonrpc.JsonRpcApiUtil;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.BlockHeader;
Expand All @@ -16,6 +19,16 @@

public class ApiUtilTest {

@BeforeClass
public static void init() {
Args.setParam(new String[]{}, "config-localtest.conf");
}

@AfterClass
public static void clear() {
Args.clearParam();
}

@Test
public void testGetBlockID() {
byte[] mockedHash = generateRandomBytes(128);
Expand Down
48 changes: 0 additions & 48 deletions framework/src/test/java/org/tron/core/net/BaseNet.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package org.tron.core.net;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultMessageSizeEstimator;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -29,13 +13,10 @@
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.PublicMethod;
import org.tron.common.utils.ReflectUtils;
import org.tron.core.Constant;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.net.peer.PeerConnection;

@Slf4j
public class BaseNet {
Expand All @@ -53,30 +34,6 @@ public class BaseNet {

private static ExecutorService executorService = Executors.newFixedThreadPool(1);

public static Channel connect(ByteToMessageDecoder decoder) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024));
ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024);
ch.config().setOption(ChannelOption.SO_BACKLOG, 1024);
ch.pipeline()
.addLast("readTimeoutHandler", new ReadTimeoutHandler(600, TimeUnit.SECONDS))
.addLast("writeTimeoutHandler", new WriteTimeoutHandler(600, TimeUnit.SECONDS));
ch.pipeline().addLast("protoPender", new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast("lengthDecode", new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast("handshakeHandler", decoder);
ch.closeFuture();
}
}).option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000)
.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
return b.connect(Constant.LOCAL_HOST, port).sync().channel();
}

@BeforeClass
public static void init() throws Exception {
executorService.execute(() -> {
Expand Down Expand Up @@ -123,11 +80,6 @@ public static void init() throws Exception {

@AfterClass
public static void destroy() {
Collection<PeerConnection> peerConnections = ReflectUtils
.invokeMethod(tronNetDelegate, "getActivePeer");
for (PeerConnection peer : peerConnections) {
peer.getChannel().close();
}
Args.clearParam();
context.destroy();
}
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 Down Expand Up @@ -101,6 +100,7 @@ public void testOkHelloMessage()
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, a1.getPort());
HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
Assert.assertNotNull(helloMessage.toString());

Assert.assertEquals(Version.getVersion(),
new String(helloMessage.getHelloMessage().getCodeVersion().toByteArray()));
Expand Down Expand Up @@ -214,7 +214,7 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {

Node node2 = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, 10002);

//lowestBlockNum > headBlockNum
//peer's lowestBlockNum > my headBlockNum => peer is light, LIGHT_NODE_SYNC_FAIL
Protocol.HelloMessage.Builder builder =
getHelloMessageBuilder(node2, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
Expand All @@ -226,7 +226,7 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {
Assert.fail();
}

//genesisBlock is not equal
//genesisBlock is not equal => INCOMPATIBLE_CHAIN
builder = getHelloMessageBuilder(node2, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
BlockCapsule.BlockId gid = ChainBaseManager.getChainBaseManager().getGenesisBlockId();
Expand All @@ -242,9 +242,11 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {
Assert.fail();
}

//solidityBlock <= us, but not contained
// peer's solidityBlock <= my solidityBlock, but not contained
// and my lowestBlockNum <= peer's solidityBlock => FORKED
builder = getHelloMessageBuilder(node2, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());

BlockCapsule.BlockId sid = ChainBaseManager.getChainBaseManager().getSolidBlockId();

Random gen = new Random();
Expand All @@ -262,6 +264,16 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException {
} catch (Exception e) {
Assert.fail();
}

// peer's solidityBlock <= my solidityBlock, but not contained
// and my lowestBlockNum > peer's solidityBlock => i am light, LIGHT_NODE_SYNC_FAIL
ChainBaseManager.getChainBaseManager().setLowestBlockNum(2);
try {
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
method.invoke(p2pEventHandler, peer, helloMessage.getSendBytes());
} catch (Exception e) {
Assert.fail();
}
}

private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long timestamp,
Expand Down
Loading