forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseNet.java
More file actions
120 lines (110 loc) · 4.57 KB
/
BaseNet.java
File metadata and controls
120 lines (110 loc) · 4.57 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
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.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.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
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.net.peer.PeerConnection;
import org.tron.core.services.RpcApiService;
@Slf4j
public abstract class BaseNet {
private static String dbPath = "output-net";
private static String dbDirectory = "net-database";
private static String indexDirectory = "net-index";
private static int port = 10000;
protected TronApplicationContext context;
private RpcApiService rpcApiService;
private Application appT;
private TronNetDelegate tronNetDelegate;
private ExecutorService executorService = Executors.newFixedThreadPool(1);
@Before
public void init() throws Exception {
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.getSeedNode().getIpList().clear();
cfgArgs.setNodeExternalIp("127.0.0.1");
context = new TronApplicationContext(DefaultConfig.class);
appT = ApplicationFactory.create(context);
rpcApiService = context.getBean(RpcApiService.class);
appT.addService(rpcApiService);
appT.initServices(cfgArgs);
appT.startServices();
appT.startup();
tronNetDelegate = context.getBean(TronNetDelegate.class);
rpcApiService.blockUntilShutdown();
}
});
int tryTimes = 0;
while (++tryTimes < 100 && tronNetDelegate == null) {
Thread.sleep(3000);
}
}
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("127.0.0.1", port).sync().channel();
}
@After
public void destroy() {
Collection<PeerConnection> peerConnections = ReflectUtils
.invokeMethod(tronNetDelegate, "getActivePeer");
for (PeerConnection peer : peerConnections) {
peer.close();
}
context.destroy();
FileUtil.deleteDir(new File(dbPath));
}
}