-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountTxs.java
executable file
·54 lines (45 loc) · 1.65 KB
/
CountTxs.java
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
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
public class CountTxs {
private static final String BASE_URL = "http://127.0.0.1:8081/api/";
// one instance, reuse
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
public static void main(String[] args) throws Exception {
int start = Integer.parseInt(args[0]);
int end = Integer.parseInt(args[1]);
System.err.println("Count total number of txs in blocks in range [" + start + ", " + end + ")");
int count = 0;
for (int i = start; i < end; i++) {
if (i % 100 == 0) {
System.err.println("Processing block " + i);
}
count += parseBlock(i);
}
System.out.println("Total number of non-coinbase txs in blocks in [" + start + ", " + end + "): "
+ count);
}
private static int parseBlock(int block) throws Exception {
String url = BASE_URL + "block/" + block;
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build();
String data = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
int txsStart = data.indexOf("txs\":[{");
String txs = data.substring(txsStart + 5, data.indexOf("]", txsStart) + 1);
int count = 0;
for (int i = 0; i < txs.length(); i++) {
if (txs.charAt(i) == '{') {
count++;
}
}
// ignore coinbase txn
return count - 1;
}
}