-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCollectAddress.java
More file actions
286 lines (258 loc) · 10.3 KB
/
Copy pathCollectAddress.java
File metadata and controls
286 lines (258 loc) · 10.3 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package org.tron;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Callable;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.RocksDBException;
import org.tron.plugins.utils.db.DBInterface;
import org.tron.plugins.utils.db.DBIterator;
import org.tron.plugins.utils.db.DbTool;
import org.tron.protos.contract.BalanceContract.TransferContract;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.proto.Chain.Transaction.Contract.ContractType;
import org.tron.trident.proto.Contract.TransferAssetContract;
import org.tron.trident.proto.Contract.TriggerSmartContract;
import org.tron.trident.proto.Response.BlockExtention;
import org.tron.trident.proto.Response.TransactionExtention;
import org.tron.trident.utils.Base58Check;
import org.tron.trxs.TxConfig;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Slf4j(topic = "collect")
@Command(name = "collect",
description = "Collect the address list from the account database.",
exitCodeListHeading = "Exit Codes:%n",
exitCodeList = {
"0:Successful",
"n:Internal error: exception occurred, please check logs/stress_test.log"})
public class CollectAddress implements Callable<Integer> {
@CommandLine.Spec
public static CommandLine.Model.CommandSpec spec;
@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;
@Option(names = {"-h", "--help"})
private boolean help;
@Option(names = {"-o", "--output"},
defaultValue = "address-list.csv",
description = "store the collected address list."
+ " Default: ${DEFAULT-VALUE}")
private String output;
private ApiWrapper apiWrapper;
@Override
public Integer call() throws Exception {
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();
// if (config.getGetAddressStartNumber() < 0 || config.getGetAddressStartNumber() >= config
// .getGetAddressEndNumber()) {
// logger.error("invalid start Block: {}, end Block: {}", config.getGetAddressStartNumber(),
// config.getGetAddressEndNumber());
// spec.commandLine().getErr()
// .format("invalid start Block: %d, end Block: %d", config.getGetAddressStartNumber(),
// config.getGetAddressEndNumber())
// .println();
// System.exit(1);
// }
if (config.getAddressTotal() <= 0) {
logger.error("invalid target number: {}", config.getAddressTotal());
spec.commandLine().getErr()
.format("invalid target number: %d", config.getAddressTotal())
.println();
System.exit(1);
}
// if (config.getGetAddressUrl().isEmpty()) {
// logger.error("no available get address url found.");
// spec.commandLine().getErr().println("no available get address url found.");
// System.exit(1);
// }
//
// apiWrapper = new ApiWrapper(config.getGetAddressUrl(), config.getGetAddressUrl(),
// config.getPrivateKey());
// Set<ByteString> addressList = getAddressList(config.getGetAddressStartNumber(),
// config.getGetAddressEndNumber(),
// config.getGetAddressTotalNumber());
Set<ByteString> addressList = getAddressListFromDB(config.getAddressDbPath(),
config.getAddressTotal());
writeToFile(addressList, output);
return 0;
}
private Set<ByteString> getAddressListFromDB(String dbPath, int totalNumber)
throws IOException, RocksDBException {
Set<ByteString> addressList = new HashSet<>();
String srcDir = dbPath + File.separator + "database";
DBInterface accountStore = DbTool.getDB(srcDir, "account");
DBIterator iterator = accountStore.iterator();
for (iterator.seekToFirst(); iterator.valid(); iterator.next()) {
addressList.add(ByteString.copyFrom(iterator.getKey()));
if (addressList.size() % 10000 == 0) {
logger.info("collecting address list, current size: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list, current size: %d, target: %d", addressList.size(),
totalNumber)
.println();
}
if (addressList.size() >= totalNumber) {
logger
.info("collecting address list: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list: %d, target: %d",
addressList.size(), totalNumber)
.println();
return addressList;
}
}
logger
.info("collecting address list: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list: %d, target: %d",
addressList.size(), totalNumber).println();
DbTool.close();
return addressList;
}
private Set<ByteString> getAddressList(long startBlockNumber, long endBlockNumber,
int totalNumber) throws InterruptedException {
if (startBlockNumber < endBlockNumber) {
logger.info("startNumber: {}, endNumber: {}", startBlockNumber, endBlockNumber);
} else {
logger.error("invalid startNumber: {}, endNumber: {}", startBlockNumber, endBlockNumber);
throw new IllegalArgumentException();
}
Set<ByteString> addressList = new HashSet<>();
BlockExtention block;
for (long i = startBlockNumber; i < endBlockNumber; i++) {
if (i % 100 == 0) {
logger.info("current block number: {}", i);
}
try {
block = apiWrapper.getBlockByNum(i);
} catch (Exception e) {
Thread.sleep(500);
continue;
}
if (block.getTransactionsCount() <= 0) {
continue;
}
for (TransactionExtention tx : block.getTransactionsList()) {
int cnt = 0;
ContractType type = tx.getTransaction().getRawData().getContract(0).getType();
try {
switch (type) {
case TransferContract:
TransferContract transferContract = tx.getTransaction().getRawData()
.getContract(0)
.getParameter().unpack(TransferContract.class);
if (addressList.add(transferContract.getOwnerAddress())) {
cnt++;
}
if (addressList.add(transferContract.getToAddress())) {
cnt++;
}
break;
case TransferAssetContract:
TransferAssetContract transferAssetContract = tx.getTransaction().getRawData()
.getContract(0)
.getParameter().unpack(TransferAssetContract.class);
if (addressList.add(transferAssetContract.getOwnerAddress())) {
cnt++;
}
if (addressList.add(transferAssetContract.getToAddress())) {
cnt++;
}
break;
case TriggerSmartContract:
TriggerSmartContract triggerSmartContract = tx.getTransaction().getRawData()
.getContract(0)
.getParameter().unpack(TriggerSmartContract.class);
if (addressList.add(triggerSmartContract.getOwnerAddress())) {
cnt++;
}
break;
default:
}
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
if (cnt > 0 && addressList.size() % 10000 == 0) {
logger.info("collecting address list, current size: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list, current size: %d, target: %d", addressList.size(),
totalNumber)
.println();
}
if (addressList.size() > totalNumber) {
logger
.info("collecting address list: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list: %d, target: %d",
addressList.size(), totalNumber)
.println();
return addressList;
}
}
}
logger
.info("collecting address list: {}, target: {}", addressList.size(),
totalNumber);
spec.commandLine().getOut()
.format("collecting address list: %d, target: %d",
addressList.size(), totalNumber).println();
return addressList;
}
public static void writeToFile(Set<ByteString> data, String filePath) {
logger.info("begin to write to file: " + filePath + ", please wait...");
spec.commandLine().getOut().println("begin to write to file: " + filePath + ", please wait...");
try (BufferedWriter writer = new BufferedWriter(
new FileWriter(filePath))) {
Iterator<ByteString> iterator = data.iterator();
while (iterator.hasNext()) {
String address = Base58Check.bytesToBase58(iterator.next().toByteArray()).trim();
writer.write(address);
if (iterator.hasNext()) {
writer.newLine();
}
}
writer.flush();
logger.info("finishing writing to file.");
spec.commandLine().getOut().println("finishing writing to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}