-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerkleNode.java
More file actions
195 lines (177 loc) · 4.46 KB
/
MerkleNode.java
File metadata and controls
195 lines (177 loc) · 4.46 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package modules.ads.merkletree;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import com.google.gson.Gson;
import crypto.Sha3256Hasher;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import model.Entity;
import model.crypto.Sha3256Hash;
/**
* A node in the Merkle tree.
*/
public class MerkleNode implements Serializable {
private static final Sha3256Hasher hasher = new Sha3256Hasher();
private MerkleNode left;
private MerkleNode right;
private MerkleNode parent;
private boolean isLeft;
private Sha3256Hash hash;
/**
* Default constructor.
*/
public MerkleNode() {
this.left = null;
this.right = null;
this.parent = null;
this.isLeft = false;
this.hash = null;
}
/**
* Constructor with entity and isLeft.
*
* @param e input entity
* @param isLeft boolean that specifies if the node is left child or not
*/
public MerkleNode(Entity e, boolean isLeft) {
this.left = null;
this.right = null;
this.parent = null;
this.isLeft = isLeft;
this.hash = hasher.computeHash(e.id());
}
/**
* Constructor with hash of the entity.
*
* @param hash input hash of the entity corresponding to that node
*/
public MerkleNode(Sha3256Hash hash) {
this.left = null;
this.right = null;
this.parent = null;
this.isLeft = false;
this.hash = hash;
}
/**
* Constructor of a parent node.
*
* @param hash input hash of the entity corresponding to that node
* @param left left child of the node
* @param right right child of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "left and right are intentionally mutable externally")
public MerkleNode(Sha3256Hash hash, MerkleNode left, MerkleNode right) {
this.left = left;
this.right = right;
this.parent = null;
this.isLeft = false;
this.hash = hash;
}
/**
* Returns the left child of the node.
*
* @return the left child of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getLeft() {
return left;
}
/**
* Returns the right child of the node.
*
* @return the right child of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getRight() {
return right;
}
/**
* Returns the parent node of the node.
*
* @return the parent node of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getParent() {
return parent;
}
/**
* Sets the parent node of the node.
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "parent is intentionally mutable externally")
public void setParent(MerkleNode parent) {
this.parent = parent;
}
/**
* Returns the hash of the node.
*
* @return the hash of the node
*/
public Sha3256Hash getHash() {
return hash;
}
/**
* Returns the isLeft boolean of the node.
*
* @return the isLeft boolean of the node
*/
public boolean isLeft() {
return isLeft;
}
/**
* Sets the isLeft of the node.
*
* @param isLeft isLeft boolean of the node
*/
public void setLeft(boolean isLeft) {
this.isLeft = isLeft;
}
/**
* Returns the sibling of the node.
*
* @return the sibling of the node
*/
public MerkleNode getSibling() {
if (isLeft()) {
return parent.getRight();
} else {
return parent.getLeft();
}
}
/**
* Returns if o is equal to this node.
*
* @param o object to compare
*
* @return true if o is equal to this node
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MerkleNode that = (MerkleNode) o;
return hash.equals(that.hash);
}
/**
* Returns the hash code of the node.
*
* @return the hash code of the node
*/
@Override
public int hashCode() {
return Objects.hash(left, right, parent, isLeft, hash);
}
/**
* Returns the byte array representation of the node.
*
* @return the byte array representation of the node
*/
public byte[] getBytes() {
Gson gson = new Gson();
byte[] bytes = gson.toJson(this).getBytes(StandardCharsets.UTF_8);
return bytes;
}
}