Skip to content

Commit 91c0c50

Browse files
authored
Merge pull request #5109 from chengtx01/optimize_intranet
fix(net):fix the issue of block synchronization
2 parents 354e1af + 7c1bd7d commit 91c0c50

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

framework/src/main/java/org/tron/core/net/service/sync/SyncService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private void processSyncBlock(BlockCapsule block) {
292292
if (peer.getSyncBlockInProcess().remove(blockId)) {
293293
if (flag) {
294294
peer.setBlockBothHave(blockId);
295-
if (peer.getSyncBlockToFetch().isEmpty() && !peer.isFetchAble()) {
295+
if (peer.getSyncBlockToFetch().isEmpty() && peer.isFetchAble()) {
296296
syncNext(peer);
297297
}
298298
} else {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.tron.core.net.services;
2+
3+
import java.io.File;
4+
import java.lang.reflect.Field;
5+
import java.net.InetSocketAddress;
6+
import org.junit.After;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.tron.common.application.TronApplicationContext;
11+
import org.tron.common.utils.FileUtil;
12+
import org.tron.common.utils.ReflectUtils;
13+
import org.tron.core.Constant;
14+
import org.tron.core.config.DefaultConfig;
15+
import org.tron.core.config.args.Args;
16+
import org.tron.core.net.P2pEventHandlerImpl;
17+
import org.tron.core.net.peer.PeerConnection;
18+
import org.tron.core.net.peer.TronState;
19+
import org.tron.core.net.service.sync.SyncService;
20+
import org.tron.p2p.connection.Channel;
21+
22+
23+
public class SyncServiceTest {
24+
protected TronApplicationContext context;
25+
private SyncService service;
26+
private PeerConnection peer;
27+
private P2pEventHandlerImpl p2pEventHandler;
28+
private String dbPath = "output-sync-service-test";
29+
30+
/**
31+
* init context.
32+
*/
33+
@Before
34+
public void init() {
35+
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"},
36+
Constant.TEST_CONF);
37+
context = new TronApplicationContext(DefaultConfig.class);
38+
service = context.getBean(SyncService.class);
39+
}
40+
41+
/**
42+
* destroy.
43+
*/
44+
@After
45+
public void destroy() {
46+
Args.clearParam();
47+
FileUtil.deleteDir(new File(dbPath));
48+
}
49+
50+
@Test
51+
public void test() {
52+
try {
53+
ReflectUtils.setFieldValue(service, "fetchFlag", true);
54+
ReflectUtils.setFieldValue(service, "handleFlag", true);
55+
service.init();
56+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "fetchFlag"));
57+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "handleFlag"));
58+
peer = context.getBean(PeerConnection.class);
59+
Assert.assertNull(peer.getSyncChainRequested());
60+
Channel c1 = new Channel();
61+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
62+
Field field = c1.getClass().getDeclaredField("inetSocketAddress");
63+
field.setAccessible(true);
64+
field.set(c1, a1.getAddress());
65+
peer.setChannel(c1);
66+
service.startSync(peer);
67+
ReflectUtils.setFieldValue(peer, "tronState", TronState.SYNCING);
68+
service.startSync(peer);
69+
} catch (Exception e) {
70+
// no need to deal with
71+
}
72+
service.close();
73+
}
74+
}

0 commit comments

Comments
 (0)