Repository navigation
Expand file tree
/
Copy pathBaseNetTest.java
More file actions
172 lines (157 loc) · 6.22 KB
/
Copy pathBaseNetTest.java
File metadata and controls
172 lines (157 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package org.tron.core.net.node;
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.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.After;
import org.junit.Before;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.overlay.client.PeerClient;
import org.tron.common.overlay.server.ChannelManager;
import org.tron.common.overlay.server.SyncPool;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.ReflectUtils;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.services.RpcApiService;
import org.tron.core.services.WitnessService;
@Slf4j
public abstract class BaseNetTest {
protected static TronApplicationContext context;
protected NodeImpl node;
protected RpcApiService rpcApiService;
protected PeerClient peerClient;
protected ChannelManager channelManager;
protected SyncPool pool;
protected Manager manager;
private Application appT;
private String dbPath;
private String dbDirectory;
private String indexDirectory;
private int port;
private ExecutorService executorService = Executors.newFixedThreadPool(1);
public BaseNetTest(String dbPath, String dbDirectory, String indexDirectory, int port) {
this.dbPath = dbPath;
this.dbDirectory = dbDirectory;
this.indexDirectory = indexDirectory;
this.port = port;
}
@Before
public void init() {
executorService.execute(new Runnable() {
@Override
public void run() {
logger.info("Full node running.");
Args.setParam(
new String[]{
"--output-directory", dbPath,
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory
},
"config.conf"
);
Args cfgArgs = Args.getInstance();
cfgArgs.setNodeListenPort(port);
cfgArgs.setNodeDiscoveryEnable(false);
cfgArgs.getSeedNode().getIpList().clear();
cfgArgs.setNeedSyncCheck(false);
cfgArgs.setNodeExternalIp("127.0.0.1");
context = new TronApplicationContext(DefaultConfig.class);
if (cfgArgs.isHelp()) {
logger.info("Here is the help message.");
return;
}
appT = ApplicationFactory.create(context);
rpcApiService = context.getBean(RpcApiService.class);
appT.addService(rpcApiService);
if (cfgArgs.isWitness()) {
appT.addService(new WitnessService(appT, context));
}
appT.initServices(cfgArgs);
appT.startServices();
node = context.getBean(NodeImpl.class);
peerClient = context.getBean(PeerClient.class);
channelManager = context.getBean(ChannelManager.class);
pool = context.getBean(SyncPool.class);
manager = context.getBean(Manager.class);
NodeDelegate nodeDelegate = new NodeDelegateImpl(manager);
node.setNodeDelegate(nodeDelegate);
pool.init(node);
appT.startup();
rpcApiService.blockUntilShutdown();
}
});
int tryTimes = 1;
while (tryTimes <= 30 && (node == null || peerClient == null
|| channelManager == null || pool == null)) {
try {
logger.info("node:{},peerClient:{},channelManager:{},pool:{}", node, peerClient,
channelManager, pool);
Thread.sleep(1000 * tryTimes);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
++tryTimes;
}
}
}
protected Channel createClient(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 {
// limit the size of receiving buffer to 1024
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);
// be aware of channel closing
ch.closeFuture();
}
}).option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000)
.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
return b.connect("127.0.0.1", port).sync().channel();
}
@After
public void destroy() {
executorService.shutdownNow();
Args.clearParam();
FileUtil.deleteDir(new File(dbPath));
Collection<PeerConnection> peerConnections = ReflectUtils.invokeMethod(node, "getActivePeer");
for (PeerConnection peer : peerConnections) {
peer.close();
}
peerClient.close();
node.shutDown();
appT.shutdownServices();
appT.shutdown();
}
}