forked from csaguil/p2p-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.java
executable file
·112 lines (97 loc) · 3.6 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
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
public class Block implements Serializable {
private transient Block previousBlock;
private byte[] previousHash;
private byte[] currentHash;
private ArrayList<Transaction> transactions;
public Block() {
transactions = new ArrayList<>();
}
public byte[] getPreviousHash() {
return previousHash;
}
public void setPreviousHash(byte[] prevHash) {
this.previousHash = prevHash;
}
public byte[] getCurrentHash() {
return currentHash;
}
public void setCurrentHash(byte[] currentHash) {
this.currentHash = currentHash;
}
public Block getPreviousBlock() {
return previousBlock;
}
public void setPreviousBlock(Block previousBlock) {
this.previousBlock = previousBlock;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(ArrayList<Transaction> transactions) {
this.transactions = transactions;
}
public void addTransaction(Transaction transaction) {
this.transactions.add(transaction);
this.currentHash = calculateHash();
}
public byte[] calculateHash() {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baout);
out.write(previousHash);
for (Transaction tx : transactions) {
out.writeUTF("tx|" + tx.getSender() + "|" + tx.getContent());
}
byte[] bytes = baout.toByteArray();
return digest.digest(bytes);
} catch (NoSuchAlgorithmException e) {
return null;
} catch (IOException e) {
return null;
}
}
public byte[] calculateHashWithNonce(int nonce) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baout);
out.write(nonce);
out.write(previousHash);
for (Transaction tx : transactions) {
out.writeUTF("tx|" + tx.getSender() + "|" + tx.getContent());
}
byte[] bytes = baout.toByteArray();
return digest.digest(bytes);
} catch (NoSuchAlgorithmException e) {
return null;
} catch (IOException e) {
return null;
}
}
public String toString() {
String cutOffRule = new String(new char[81]).replace("\0", "-") + "\n";
String prevHashString = String.format("|PreviousHash:|%65s|\n", Base64.getEncoder().encodeToString(previousHash));
String hashString = String.format("|CurrentHash:|%66s|\n", Base64.getEncoder().encodeToString(calculateHash()));
String transactionsString = "";
for (Transaction tx : transactions) {
transactionsString += tx.toString();
}
return "Block:\n"
+ cutOffRule
+ hashString
+ cutOffRule
+ transactionsString
+ cutOffRule
+ prevHashString
+ cutOffRule;
}
}