-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
129 lines (100 loc) · 2.77 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
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
import java.util.*;
import java.lang.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
public class Block {
public String hash;
public String prevHash;
public String data;
public long timeStamp;
public int nonce;
public ArrayList<Transaction> transactions = new ArrayList<Transaction>();
//constructor
public Block( String data) {
this.data = data;
this.prevHash = null;
this.timeStamp = new Date().getTime();
this.nonce = 0;
this.hash = calculateBlockHash();
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public String getPrevHash() {
return prevHash;
}
public void setPrevHash(String prevHash) {
this.prevHash = prevHash;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getNonce() {
return nonce;
}
public void setNonce(int nonce) {
this.nonce = nonce;
}
//maps input data to output data of fixed size
public String calculateBlockHash() {
String dataToHash = prevHash + timeStamp + data + nonce;
MessageDigest digest = null;
byte[] bytes = null;
try{
digest = MessageDigest.getInstance("SHA-256");
bytes = digest.digest(dataToHash.getBytes(StandardCharsets.UTF_8));
} catch(NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
for (byte b : bytes) {
buffer.append(String.format("%02x", b));
}
this.hash = buffer.toString();
return getHash();
}
public String mineBlock(int difficulty) { //difficulty is the number of 0's they must solve for
String target = new String(new char[difficulty]).replace('\0', '0');
while(!hash.substring(0, difficulty).equals(target)) {
nonce++;
hash = calculateBlockHash();
}
System.out.println("Block mined. New hash: " + hash);
return hash;
}
public boolean isValid(int difficulty) {
if (hash.startsWith(new String(new char[difficulty])))
return true;
else
return false;
}
//Add transactions to this block
public boolean addTransaction(Transaction transaction) {
//process transaction and check if valid, unless block is genesis block then ignore.
if(transaction == null)
return false;
if((prevHash != null)) {
if((transaction.processTransaction() != true)) {
System.out.println("Transaction failed to process. Discarded.");
return false;
}
}
transactions.add(transaction);
data = transaction.transactionToData();
System.out.println("Transaction Successfully added to Block");
return true;
}
}