-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiner.java
More file actions
36 lines (31 loc) · 1.22 KB
/
Miner.java
File metadata and controls
36 lines (31 loc) · 1.22 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
package blockchain;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;
public class Miner implements Callable<Block> {
private final String prevHash;
private final String zeroString;
private final int id;
private final Instant time;
private final String mess;
public Miner(String prevHash, String zeroString, int id,
Instant startedMaking, String mess) {
this.mess = mess;
this.prevHash = prevHash;
this.zeroString = zeroString;
this.id = id;
this.time = startedMaking;
}
@Override
public Block call() {
long magicNumber = HashUtility.generateMagicNumber(this.zeroString);
String hash = HashUtility.applySha256(String.valueOf(magicNumber));
long minerId = Thread.currentThread().getId();
long created = Duration.between(this.time, Instant.now()).toSeconds();
String message = created < 14 ?
String.format("N was increased to %d",
this.zeroString.length() + 1) :
(created < 40 ? "N stays the same" : "N was decreased by 1");
return new Block(prevHash, hash, message, id, minerId, magicNumber, created, mess);
}
}