-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalibrator.java
More file actions
51 lines (44 loc) · 1.64 KB
/
Copy pathCalibrator.java
File metadata and controls
51 lines (44 loc) · 1.64 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
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import org.bitcoinj.base.Difficulty;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Transaction;
public class Calibrator {
public static void main(String [] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java Calibrator hex_target");
System.exit(-1);
} else {
System.out.println("Patience please, this may take a while");
}
long target = Long.parseLong(args[0], 16);
Random random = new Random();
int n = 100;
long startTime = System.currentTimeMillis();
for (int r = 0; r < n; r++) {
int version = 1;
byte[] bytes1 = new byte[32];
random.nextBytes(bytes1);
Sha256Hash prevBlockHash = Sha256Hash.wrap(bytes1);
byte[] bytes2 = new byte[32];
random.nextBytes(bytes2);
Sha256Hash merkleRootHash = Sha256Hash.wrap(bytes2);
Instant time = Instant.now().truncatedTo(ChronoUnit.SECONDS);
Difficulty difficulty = Difficulty.ofCompact(target);
long nonce = 0;
List<Transaction> txns = null;
Block b = new Block(version, prevBlockHash, merkleRootHash, time, difficulty, nonce, txns);
while (!difficulty.isMetByWork(b.getHash())) {
b.setNonce(++nonce);
}
System.out.println("" + (r+1) + "% complete");
}
long endTime = System.currentTimeMillis();
System.out.println("Throughput (blocks/s) for target " + args[0] + ": " + n*1000f/(endTime-startTime));
System.out.println("Latency (ms/block) for target " + args[0] + ": " + 1f*(endTime-startTime)/n);
}
}