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): limit SR connections to fast forwarding nodes #5881

Closed
wants to merge 1 commit into from
Closed
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 @@ -71,6 +71,8 @@ public class RelayService {

private int maxFastForwardNum = Args.getInstance().getMaxFastForwardNum();

private static final int MAX_PEER_COUNT_PER_ADDRESS = 2;

public void init() {
manager = ctx.getBean(Manager.class);
witnessScheduleStore = ctx.getBean(WitnessScheduleStore.class);
Expand Down Expand Up @@ -139,6 +141,13 @@ public boolean checkHelloMessage(HelloMessage message, Channel channel) {
return false;
}

if (getPeerCountByAddress(msg.getAddress()) >= MAX_PEER_COUNT_PER_ADDRESS) {
logger.warn("HelloMessage from {}, the number of peers of {} exceeds 2.",
channel.getInetAddress(),
ByteArray.toHexString(msg.getAddress().toByteArray()));
return false;
}

boolean flag;
try {
Sha256Hash hash = Sha256Hash.of(CommonParameter
Expand All @@ -164,6 +173,12 @@ public boolean checkHelloMessage(HelloMessage message, Channel channel) {
}
}

private long getPeerCountByAddress(ByteString address) {
return tronNetDelegate.getActivePeer().stream()
.filter(peer -> peer.getAddress() != null && peer.getAddress().equals(address))
.count();
}

private boolean isActiveWitness() {
return parameter.isWitness()
&& keySize > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -13,19 +16,29 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.tron.common.BaseTest;
import org.tron.common.utils.ReflectUtils;
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.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.connection.Channel;
import org.tron.p2p.discover.Node;
import org.tron.p2p.utils.NetUtil;
import org.tron.protos.Protocol;

import static org.mockito.Mockito.mock;

public class RelayServiceTest extends BaseTest {

@Resource
Expand All @@ -49,6 +62,7 @@ public void test() throws Exception {
initWitness();
testGetNextWitnesses();
testBroadcast();
testCheckHelloMessage();
}

private void initWitness() {
Expand Down Expand Up @@ -119,4 +133,39 @@ private ByteString getFromHexString(String s) {
return ByteString.copyFrom(Hex.decode(s));
}

private void testCheckHelloMessage() {
ByteString address = getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F");
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(),
null, a1.getPort());
HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
helloMessage.setHelloMessage(helloMessage.getHelloMessage().toBuilder()
.setAddress(address).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");
PeerConnection peer1 = PeerManager.add(ctx, c1);
assert peer1 != null;
peer1.setAddress(address);
PeerConnection peer2 = PeerManager.add(ctx, c2);
assert peer2 != null;
peer2.setAddress(address);
try {
Field field = service.getClass().getDeclaredField("witnessScheduleStore");
field.setAccessible(true);
field.set(service, chainBaseManager.getWitnessScheduleStore());
boolean res = service.checkHelloMessage(helloMessage, c1);
Assert.assertFalse(res);
} catch (Exception e) {

}
}

}
Loading