- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIMerkleTree.java
More file actions
Latest commit
149 lines (126 loc) · 3.88 KB
/
Copy pathIMerkleTree.java
File metadata and controls
149 lines (126 loc) · 3.88 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
packagemerkle;
importjava.io.File;
importjava.io.Serializable;
/**
* This is the Merkle Tree
* DO NOT UPLOAD THIS FILE
*/
publicabstractclassIMerkleTreeimplementsSerializable {
/**
* NodeType signifies whether a node is a left child or right child
*/
publicenumNodeType {
left, right
}
/**
* Node contains data for every node in the tree
*/
publicstaticclassNodeimplementsSerializable {
privateStringhash;
privateNodeTypetype;
privateintindex;
publicNode(Stringhash, intindex) {
this.hash = hash;
this.index = index;
this.type = index % 2 == 0 ? NodeType.left : NodeType.right;
}
publicStringgetHash() {
returnhash;
}
publicintgetIndex() {
returnindex;
}
publicNodeTypegetType() {
returntype;
}
@Override
publicStringtoString() {
return"Node{" +
"hash='" + hash + '\'' +
", type=" + type +
", index=" + index +
'}';
}
/**
* This will mostly be required during verification
*/
publicvoidsetHash(Stringhash) {
this.hash = hash;
}
@Override
publicbooleanequals(Objecto) {
if (this == o) returntrue;
if (!(oinstanceofNode)) returnfalse;
Nodenode = (Node) o;
if (index != node.index) returnfalse;
if (!hash.equals(node.hash)) returnfalse;
returntype == node.type;
}
@Override
publicinthashCode() {
intresult = hash.hashCode();
result = 31 * result + type.hashCode();
result = 31 * result + index;
returnresult;
}
}
/**
* This is the tree represented as an array
*/
protectedNode[] tree;
/**
* padBytes is used to check whether the number of bytes are
* equal to the block size. If not the array is padded with space.
* The number of bytes read will be lesser than block size at the last
* block
*/
protectedStringpadBytes(byte[] array, intreadStatus) {
if (readStatus < Configuration.blockSize) {
for (inti = readStatus; i < Configuration.blockSize; i++) {
array[i] = ' ';
}
}
returnnewString(array);
}
/**
* Given an <i>inputFile</i> this function builds a Merkle Tree and return the <i>masterHash</i>
* <i>this.tree</i> is the array representation of the tree which you need to create
* You can use <i>Configuration.hashFunction</i>
* The basic code to read a file block wise is provided. You can choose to use it.
* The tree should be 1-indexed
*/
publicabstractStringbuild(FileinputFile) throwsException;
/**
* This returns the total number of nodes in the tree including the zeroth dummy node
*/
intgetNumberOfNodes() {
returntree.length;
}
/**
* This is used for printing the whole tree level wise with spacing
*/
publicvoidprint() {
for (inti = 1; i < tree.length; i++) {
Nodenode = tree[i];
for (intj = 0; j < (int) Math.floor(Math.log(i) / Math.log(2)); j++) {
Configuration.print(' ');
}
Configuration.println(node);
}
}
/**
* Given i getNode returns the ith node in the tree.
* Note that indexes start from 1.
* This will be helpful in detecting the path siblings
*/
publicNodegetNode(inti) {
returntree[i];
}
/**
* Returns the array representing the tree.
* Will be useful for collision generation
*/
publicNode[] getTree() {
returntree;
}
}