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 8 commits into
base: release_v4.8.1
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 @@ -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,21 @@ 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()) {
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 me and light node doesn't contain the block,"
+ " it's unuseful, peer->{}, me->{}",
peer.getInetSocketAddress(),
msg.getSolidBlockId().getString(),
chainBaseManager.getSolidBlockId().getString());
peer.disconnect(ReasonCode.LIGHT_NODE_SYNC_FAIL);
}
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 Down Expand Up @@ -214,7 +213,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 +225,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 +241,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 +263,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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.tron.core.net.services;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -12,7 +13,6 @@
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Assert;
Expand All @@ -21,19 +21,27 @@
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.tron.common.BaseTest;
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ReflectUtils;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.ChainBaseManager;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.net.P2pEventHandlerImpl;
import org.tron.core.net.TronNetDelegate;
import org.tron.core.net.TronNetService;
import org.tron.core.net.message.adv.BlockMessage;
import org.tron.core.net.message.handshake.HelloMessage;
import org.tron.core.net.peer.Item;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.net.peer.PeerManager;
import org.tron.core.net.service.relay.RelayService;
import org.tron.p2p.P2pConfig;
import org.tron.p2p.connection.Channel;
import org.tron.p2p.discover.Node;
import org.tron.p2p.utils.NetUtil;
Expand All @@ -44,11 +52,13 @@ public class RelayServiceTest extends BaseTest {

@Resource
private RelayService service;
@Resource
private PeerConnection peer;

@Resource
private P2pEventHandlerImpl p2pEventHandler;

@Resource
private TronNetService tronNetService;

/**
* init context.
*/
Expand All @@ -67,8 +77,12 @@ public void test() throws Exception {
}

private void initWitness() {
byte[] key = Hex.decode("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F");
// key: 0154435f065a57fec6af1e12eaa2fa600030639448d7809f4c65bdcf8baed7e5
// Hex: A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01
// Base58: 27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz
byte[] key = Hex.decode("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01");//exist already
WitnessCapsule witnessCapsule = chainBaseManager.getWitnessStore().get(key);
System.out.println(witnessCapsule.getInstance());
witnessCapsule.setVoteCount(1000);
chainBaseManager.getWitnessStore().put(key, witnessCapsule);
List<ByteString> list = new ArrayList<>();
Expand All @@ -83,7 +97,7 @@ public void testGetNextWitnesses() throws Exception {
"getNextWitnesses", ByteString.class, Integer.class);
method.setAccessible(true);
Set<ByteString> s1 = (Set<ByteString>) method.invoke(
service, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"), 3);
service, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"), 3);
Assert.assertEquals(3, s1.size());
assertContains(s1, "A0299F3DB80A24B20A254B89CE639D59132F157F13");
assertContains(s1, "A0807337F180B62A77576377C1D0C9C24DF5C0DD62");
Expand All @@ -93,35 +107,51 @@ public void testGetNextWitnesses() throws Exception {
service, getFromHexString("A0FAB5FBF6AFB681E4E37E9D33BDDB7E923D6132E5"), 3);
Assert.assertEquals(3, s2.size());
assertContains(s2, "A014EEBE4D30A6ACB505C8B00B218BDC4733433C68");
assertContains(s2, "A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F");
assertContains(s2, "A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01");
assertContains(s2, "A0299F3DB80A24B20A254B89CE639D59132F157F13");

Set<ByteString> s3 = (Set<ByteString>) method.invoke(
service, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"), 1);
service, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"), 1);
Assert.assertEquals(1, s3.size());
assertContains(s3, "A0299F3DB80A24B20A254B89CE639D59132F157F13");
}

private void testBroadcast() {
try {
PeerConnection peer = new PeerConnection();
InetSocketAddress a1 = new InetSocketAddress("127.0.0.2", 10001);
Channel c1 = mock(Channel.class);
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
doNothing().when(c1).send((byte[]) any());

peer.setChannel(c1);
peer.setAddress(getFromHexString("A0299F3DB80A24B20A254B89CE639D59132F157F13"));
peer.setNeedSyncFromPeer(false);
peer.setNeedSyncFromUs(false);

List<PeerConnection> peers = Lists.newArrayList();
List<PeerConnection> peers = new ArrayList<>();
peers.add(peer);
ReflectUtils.setFieldValue(p2pEventHandler, "activePeers", peers);

TronNetDelegate tronNetDelegate = Mockito.mock(TronNetDelegate.class);
Mockito.doReturn(peers).when(tronNetDelegate).getActivePeer();

Field field = service.getClass().getDeclaredField("tronNetDelegate");
field.setAccessible(true);
field.set(service, tronNetDelegate);

BlockCapsule blockCapsule = new BlockCapsule(chainBaseManager.getHeadBlockNum() + 1,
chainBaseManager.getHeadBlockId(),
0, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"));
0, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"));
BlockMessage msg = new BlockMessage(blockCapsule);
service.broadcast(msg);
Item item = new Item(blockCapsule.getBlockId(), Protocol.Inventory.InventoryType.BLOCK);
Assert.assertEquals(1, peer.getAdvInvSpread().size());
Assert.assertNotNull(peer.getAdvInvSpread().getIfPresent(item));
peer.getChannel().close();
} catch (NullPointerException e) {
System.out.println(e);
} catch (Exception e) {
logger.info("", e);
assert false;
}
}

Expand All @@ -135,20 +165,32 @@ private ByteString getFromHexString(String s) {
}

private void testCheckHelloMessage() {
ByteString address = getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F");
String key = "0154435f065a57fec6af1e12eaa2fa600030639448d7809f4c65bdcf8baed7e5";
ByteString address = getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01");
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(),
null, a1.getPort());

SignInterface cryptoEngine = SignUtils.fromPrivate(ByteArray.fromHexString(key),
Args.getInstance().isECKeyCryptoEngine());
HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
ByteString sig = ByteString.copyFrom(cryptoEngine.Base64toBytes(cryptoEngine
.signHash(Sha256Hash.of(CommonParameter.getInstance()
.isECKeyCryptoEngine(), ByteArray.fromLong(helloMessage
.getTimestamp())).getBytes())));
helloMessage.setHelloMessage(helloMessage.getHelloMessage().toBuilder()
.setAddress(address).build());
.setAddress(address)
.setSignature(sig)
.build());

Channel c1 = mock(Channel.class);
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
Channel c2 = mock(Channel.class);
Mockito.when(c2.getInetSocketAddress()).thenReturn(a1);
Mockito.when(c2.getInetAddress()).thenReturn(a1.getAddress());

Args.getInstance().fastForward = true;
ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler,
"ctx");
Expand All @@ -158,14 +200,23 @@ private void testCheckHelloMessage() {
PeerConnection peer2 = PeerManager.add(ctx, c2);
assert peer2 != null;
peer2.setAddress(address);

ReflectUtils.setFieldValue(tronNetService, "p2pConfig", new P2pConfig());

try {
Field field = service.getClass().getDeclaredField("witnessScheduleStore");
field.setAccessible(true);
field.set(service, chainBaseManager.getWitnessScheduleStore());

Field field2 = service.getClass().getDeclaredField("manager");
field2.setAccessible(true);
field2.set(service, dbManager);

boolean res = service.checkHelloMessage(helloMessage, c1);
Assert.assertFalse(res);
Assert.assertTrue(res);
} catch (Exception e) {
logger.info("{}", e.getMessage());
logger.info("", e);
assert false;
}
}
}
2 changes: 1 addition & 1 deletion framework/src/test/resources/config-test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ genesis.block = {
voteCount = 96
},
{
address: 27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh
address: 27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz
url = "http://AlphaLyrae.org",
voteCount = 95
}
Expand Down