-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBroadcastTx.java
More file actions
168 lines (147 loc) · 6.29 KB
/
Copy pathBroadcastTx.java
File metadata and controls
168 lines (147 loc) · 6.29 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
package org.tron;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.Callable;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.net.TronNetDelegate;
import org.tron.core.net.TronNetService;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.core.exceptions.IllegalException;
import org.tron.trident.proto.Chain.Block;
import org.tron.trxs.BroadcastGenerate;
import org.tron.trxs.BroadcastRelay;
import org.tron.trxs.TxConfig;
import org.tron.utils.PublicMethod;
import org.tron.utils.Statistic;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@Slf4j(topic = "broadcast")
@Command(name = "broadcast",
description = "Broadcast the transactions and compute the TPS.",
exitCodeListHeading = "Exit Codes:%n",
exitCodeList = {
"0:Successful",
"n:Internal error: exception occurred, please check logs/stress_test.log"})
public class BroadcastTx implements Callable<Integer> {
@CommandLine.Spec
public static CommandLine.Model.CommandSpec spec;
@CommandLine.Option(names = {"-c", "--config"},
defaultValue = "stress.conf",
description = "configure the parameters for broadcasting transactions."
+ " Default: ${DEFAULT-VALUE}")
private String config;
@CommandLine.Option(names = {"-d", "--database-directory"},
defaultValue = "output-directory",
description = "java-tron database directory path. Default: ${DEFAULT-VALUE}")
private String database;
@CommandLine.Option(names = {"--fn-config"},
defaultValue = "config.conf",
description = "configure the parameters for broadcasting transactions."
+ " Default: ${DEFAULT-VALUE}")
private String fnConfig;
@CommandLine.Option(names = {"-h", "--help"})
private boolean help;
private ApiWrapper apiWrapper;
private TronApplicationContext context;
private Application app;
private TronNetService tronNetService;
private TronNetDelegate tronNetDelegate;
@Override
public Integer call() throws IOException, InterruptedException, IllegalException {
if (help) {
spec.commandLine().usage(System.out);
return 0;
}
Config stressConfig = ConfigFactory.load();
File file = Paths.get(config).toFile();
if (file.exists() && file.isFile()) {
stressConfig = ConfigFactory.parseFile(Paths.get(config).toFile());
} else {
logger.error("Stress test config file [" + config + "] not exists!");
spec.commandLine().getErr()
.format("Stress test config file [%s] not exists!", config)
.println();
System.exit(1);
}
TxConfig.initParams(stressConfig);
TxConfig config = TxConfig.getInstance();
File fnConfigFile = Paths.get(fnConfig).toFile();
if (!fnConfigFile.exists() || fnConfigFile.isDirectory()) {
logger.error("full node config file [" + fnConfig + "] not exists!");
spec.commandLine().getErr()
.format("full node config file [%s] not exists!", fnConfig)
.println();
System.exit(1);
}
Args.setParam(new String[]{"-d", database}, fnConfig);
int rpcPort = PublicMethod.chooseRandomPort();
Args.getInstance().setRpcPort(rpcPort);
int nodeListenPort = PublicMethod.chooseRandomPort();
while (nodeListenPort == rpcPort) {
nodeListenPort = PublicMethod.chooseRandomPort();
}
Args.getInstance().setNodeListenPort(nodeListenPort);
logger.info("rpc.port: {}, node.listen.port {}", rpcPort, nodeListenPort);
Args.getInstance().setOpenHistoryQueryWhenLiteFN(true);
Args.getInstance().setRpcEnable(true);
Args.getInstance().setRpcSolidityEnable(false);
Args.getInstance().setRpcPBFTEnable(false);
Args.getInstance().setFullNodeHttpEnable(false);
Args.getInstance().setSolidityNodeHttpEnable(false);
Args.getInstance().setPBFTHttpEnable(false);
Args.getInstance().setJsonRpcHttpFullNodeEnable(false);
Args.getInstance().setJsonRpcHttpSolidityNodeEnable(false);
Args.getInstance().setJsonRpcHttpPBFTNodeEnable(false);
context = new TronApplicationContext(DefaultConfig.class);
app = ApplicationFactory.create(context);
app.startup();
String url = String.format("%s:%d", "127.0.0.1",
Args.getInstance().getRpcPort());
apiWrapper = new ApiWrapper(url, url, config.getPrivateKey());
Statistic.setApiWrapper(apiWrapper);
Statistic.setBroadcastTpsLimit(config.getBroadcastTpsLimit());
Statistic.setTotalGenerateTxCnt(config.getTotalTxCnt());
tronNetDelegate = context.getBean(TronNetDelegate.class);
while (getBroadCastPeerCount() <= 0) {
logger.warn("No available peers to broadcast, please wait");
Thread.sleep(1000);
}
tronNetService = context.getBean(TronNetService.class);
if (config.isBroadcastGenerate()) {
BroadcastGenerate broadcastGenerate = new BroadcastGenerate(config, tronNetService);
Block startBlock = apiWrapper.getNowBlock();
broadcastGenerate.broadcastTransactions();
Block endBlock = apiWrapper.getNowBlock();
long startNumber = startBlock.getBlockHeader().getRawData().getNumber();
long endNumber = endBlock.getBlockHeader().getRawData().getNumber();
Statistic.result(startNumber, endNumber, "stress-test-output/broadcast-generate-result");
}
if (config.isBroadcastRelay()) {
BroadcastRelay broadcastRelay = new BroadcastRelay(tronNetService);
Block startBlock = apiWrapper.getNowBlock();
broadcastRelay.broadcastTransactions();
Block endBlock = apiWrapper.getNowBlock();
long startNumber = startBlock.getBlockHeader().getRawData().getNumber();
long endNumber = endBlock.getBlockHeader().getRawData().getNumber();
Statistic.result(startNumber, endNumber, "stress-test-output/broadcast-relay-result");
}
shutdown();
return 0;
}
private int getBroadCastPeerCount() {
return (int) tronNetDelegate.getActivePeer().stream()
.filter(p -> !p.isNeedSyncFromPeer() && !p.isNeedSyncFromUs())
.count();
}
private void shutdown() {
context.close();
}
}