-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
48 lines (38 loc) · 1.26 KB
/
Block.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
package healthchain;
import java.util.Date;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Block {
public String hash; //Hash generated from the mined block
private String data; //All the Patient data
private String timeStamp; //The timestamp at which transcation takes place
public String previousHash; //The hash of previous block
private int nonce; //To find a integer that satisfies the given difficulty in concensus algo
public Block(String data,String previousHash )
{
//THe Block constructor
this.data = data;
this.timeStamp = HealthChain.time;
this.previousHash = previousHash;
this.hash = calHash(); //fuction that calculate hash.
}
public String calHash() {
//Hash Calculation for new block
String calhash = StrOpr.applySha256(
previousHash +
timeStamp +
Integer.toString(nonce) +
data
);
return calhash;
}
public void mineBlock(int difficulty) {
//Mine operation, find the value that matches the req. hash output
String difstr = StrOpr.getDificultyString(difficulty); //Generate a diff. string
while(!hash.substring( 0, difficulty).equals(difstr)) {
nonce ++;
hash = calHash();
}
System.out.println("Block Mined Successfully! Hash: " + hash);
}
}