-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlockChain.java
More file actions
149 lines (116 loc) · 3.41 KB
/
BlockChain.java
File metadata and controls
149 lines (116 loc) · 3.41 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
/**
* The BlockChain class, contains a linked
* data structure of blocks.
* The Node class contains the block as well
* as a reference to the next node.
* The BlockChain itself keeps references to
* the first and the last nodes.
*/
public class BlockChain {
//fields------------------------------------------------------------
public Node first;
public Node last;
public static int blockNum = -1;
//Node--------------------------------------------------------------
public static class Node {
public Block value;
public Node next;
public Node(Block value, Node next) {
this.value = value;
this.next = next;
}
}
//constructor------------------------------------------------------
public BlockChain(int initial) throws NoSuchAlgorithmException{
blockNum++;
this.first = new Node(new Block(BlockChain.blockNum, initial, null), null);
this.last = this.first;
}
//methods---------------------------------------------------------
public Block mine(int amount) throws NoSuchAlgorithmException {
long nonce = -1;
boolean nonceFound = false;
while (!nonceFound) {
nonce++;
Hash hash = new Hash(Block.calculateHash(BlockChain.blockNum + 1, amount, this.last.value.getHash(), nonce));
if (hash.isValid()) {
nonceFound = true;
}
}
return new Block(BlockChain.blockNum + 1, amount, this.last.value.getHash(), nonce);
}
public int getSize() {
return BlockChain.blockNum + 1;
}
public void append(Block blk) {
Node node = new Node(blk, null);
this.last.next = node;
this.last = node;
BlockChain.blockNum++;
}
public boolean removeLast() {
if (this.getSize() < 2) {
return false;
}
Node temp = this.first;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
this.last = temp;
BlockChain.blockNum--;
return true;
}
public Hash getHash() {
return this.last.value.getHash();
}
//Checks if hashes match up
//if the hashes are valid, and
//whether the total remains positive
public boolean isValidBlockChain() {
Node temp = this.first;
int total = temp.value.getAmount();
Hash prevHash;
Hash currentHash = temp.value.getHash();
if (!currentHash.isValid()) {
return false;
}
while (temp.next != null) {
temp = temp.next;
total += temp.value.getAmount();
prevHash = currentHash;
currentHash = temp.value.getHash();
if (total < 0 || !temp.value.getPrevHash().equals(prevHash) || !currentHash.isValid()) {
return false;
}
}
return true;
}
public void printBalances() {
PrintWriter pen = new PrintWriter(System.out, true);
Node temp = this.first;
int total = temp.value.getAmount();
int initial = total;
while (temp.next != null) {
temp = temp.next;
total += temp.value.getAmount();
}
pen.println("Alice: " + total + " Bob: " + (initial - total));
}
public String toString() {
//Implement
String retString = new String();
PrintWriter pen = new PrintWriter(System.out, true);
Node temp = this.first;
while (temp != null) {
retString += '\n';
retString += temp.value.toString();
temp = temp.next;
}
return retString;
}
}