forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKhaosDatabase.java
More file actions
364 lines (305 loc) · 9.6 KB
/
KhaosDatabase.java
File metadata and controls
364 lines (305 loc) · 9.6 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package org.tron.core.db;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.util.Pair;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.core.exception.BadNumberBlockException;
import org.tron.core.exception.NonCommonBlockException;
import org.tron.core.exception.UnLinkedBlockException;
@Component
public class KhaosDatabase extends TronDatabase {
public static class KhaosBlock {
public Sha256Hash getParentHash() {
return this.blk.getParentHash();
}
public KhaosBlock(BlockCapsule blk) {
this.blk = blk;
this.id = blk.getBlockId();
this.num = blk.getNum();
}
@Getter
private BlockCapsule blk;
private Reference<KhaosBlock> parent = new WeakReference<>(null);
private BlockId id;
private Boolean invalid;
private long num;
public KhaosBlock getParent() {
return parent == null ? null : parent.get();
}
public void setParent(KhaosBlock parent) {
this.parent = new WeakReference<>(parent);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
KhaosBlock that = (KhaosBlock) o;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
public class KhaosStore {
private HashMap<BlockId, KhaosBlock> hashKblkMap = new HashMap<>();
// private HashMap<Sha256Hash, KhaosBlock> parentHashKblkMap = new HashMap<>();
private int maxCapcity = 1024;
@Getter
private LinkedHashMap<Long, ArrayList<KhaosBlock>> numKblkMap =
new LinkedHashMap<Long, ArrayList<KhaosBlock>>() {
@Override
protected boolean removeEldestEntry(Map.Entry<Long, ArrayList<KhaosBlock>> entry) {
long minNum = Long.max(0L, head.num - maxCapcity);
Map<Long, ArrayList<KhaosBlock>> minNumMap = numKblkMap.entrySet().stream()
.filter(e -> e.getKey() < minNum)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
minNumMap.forEach((k, v) -> {
numKblkMap.remove(k);
v.forEach(b -> hashKblkMap.remove(b.id));
});
return false;
}
};
public void setMaxCapcity(int maxCapcity) {
this.maxCapcity = maxCapcity;
}
public void insert(KhaosBlock block) {
hashKblkMap.put(block.id, block);
numKblkMap.computeIfAbsent(block.num, listBlk -> new ArrayList<>()).add(block);
}
public boolean remove(Sha256Hash hash) {
KhaosBlock block = this.hashKblkMap.get(hash);
// Sha256Hash parentHash = Sha256Hash.ZERO_HASH;
if (block != null) {
long num = block.num;
// parentHash = block.getParentHash();
ArrayList<KhaosBlock> listBlk = numKblkMap.get(num);
if (listBlk != null) {
listBlk.removeIf(b -> b.id.equals(hash));
}
if (CollectionUtils.isEmpty(listBlk)) {
numKblkMap.remove(num);
}
this.hashKblkMap.remove(hash);
return true;
}
return false;
}
public List<KhaosBlock> getBlockByNum(Long num) {
return numKblkMap.get(num);
}
public KhaosBlock getByHash(Sha256Hash hash) {
return hashKblkMap.get(hash);
}
public int size() {
return hashKblkMap.size();
}
}
private KhaosBlock head;
@Getter
private KhaosStore miniStore = new KhaosStore();
@Getter
private KhaosStore miniUnlinkedStore = new KhaosStore();
@Autowired
protected KhaosDatabase(@Value("block_KDB") String dbName) {
super(dbName);
}
@Override
public void put(byte[] key, Object item) {
}
@Override
public void delete(byte[] key) {
}
@Override
public Object get(byte[] key) {
return null;
}
@Override
public boolean has(byte[] key) {
return false;
}
void start(BlockCapsule blk) {
this.head = new KhaosBlock(blk);
miniStore.insert(this.head);
}
void setHead(KhaosBlock blk) {
this.head = blk;
}
void removeBlk(Sha256Hash hash) {
if (!miniStore.remove(hash)) {
miniUnlinkedStore.remove(hash);
}
head = miniStore.numKblkMap.entrySet().stream()
.max(Comparator.comparingLong(Map.Entry::getKey))
.map(Map.Entry::getValue)
.map(list -> list.get(0))
.orElseThrow(() -> new RuntimeException("khaosDB head should not be null."));
}
/**
* check if the id is contained in the KhoasDB.
*/
public Boolean containBlock(Sha256Hash hash) {
return miniStore.getByHash(hash) != null || miniUnlinkedStore.getByHash(hash) != null;
}
public Boolean containBlockInMiniStore(Sha256Hash hash) {
return miniStore.getByHash(hash) != null;
}
/**
* Get the Block form KhoasDB, if it doesn't exist ,return null.
*/
public BlockCapsule getBlock(Sha256Hash hash) {
return Stream.of(miniStore.getByHash(hash), miniUnlinkedStore.getByHash(hash))
.filter(Objects::nonNull)
.map(block -> block.blk)
.findFirst()
.orElse(null);
}
/**
* Push the block in the KhoasDB.
*/
public BlockCapsule push(BlockCapsule blk)
throws UnLinkedBlockException, BadNumberBlockException {
KhaosBlock block = new KhaosBlock(blk);
if (head != null && block.getParentHash() != Sha256Hash.ZERO_HASH) {
KhaosBlock kblock = miniStore.getByHash(block.getParentHash());
if (kblock != null) {
if (blk.getNum() != kblock.num + 1) {
throw new BadNumberBlockException(
"parent number :" + kblock.num + ",block number :" + blk.getNum());
}
block.setParent(kblock);
} else {
miniUnlinkedStore.insert(block);
throw new UnLinkedBlockException();
}
}
miniStore.insert(block);
if (head == null || block.num > head.num) {
head = block;
}
return head.blk;
}
public BlockCapsule getHead() {
return head.blk;
}
/**
* pop the head block then remove it.
*/
public boolean pop() {
KhaosBlock prev = head.getParent();
if (prev != null) {
head = prev;
return true;
}
return false;
}
public void setMaxSize(int maxSize) {
miniUnlinkedStore.setMaxCapcity(maxSize);
miniStore.setMaxCapcity(maxSize);
}
/**
* Find two block's most recent common parent block.
*/
public Pair<LinkedList<KhaosBlock>, LinkedList<KhaosBlock>> getBranch(Sha256Hash block1,
Sha256Hash block2)
throws NonCommonBlockException {
LinkedList<KhaosBlock> list1 = new LinkedList<>();
LinkedList<KhaosBlock> list2 = new LinkedList<>();
KhaosBlock kblk1 = miniStore.getByHash(block1);
checkNull(kblk1);
KhaosBlock kblk2 = miniStore.getByHash(block2);
checkNull(kblk2);
while (kblk1.num > kblk2.num) {
list1.add(kblk1);
kblk1 = kblk1.getParent();
checkNull(kblk1);
checkNull(miniStore.getByHash(kblk1.id));
}
while (kblk2.num > kblk1.num) {
list2.add(kblk2);
kblk2 = kblk2.getParent();
checkNull(kblk2);
checkNull(miniStore.getByHash(kblk2.id));
}
while (!Objects.equals(kblk1, kblk2)) {
list1.add(kblk1);
list2.add(kblk2);
kblk1 = kblk1.getParent();
checkNull(kblk1);
checkNull(miniStore.getByHash(kblk1.id));
kblk2 = kblk2.getParent();
checkNull(kblk2);
checkNull(miniStore.getByHash(kblk2.id));
}
return new Pair<>(list1, list2);
}
private void checkNull(Object o) throws NonCommonBlockException {
if (o == null) {
throw new NonCommonBlockException();
}
}
/**
* Find two block's most recent common parent block.
*/
@Deprecated
public Pair<LinkedList<BlockCapsule>, LinkedList<BlockCapsule>> getBranch(
BlockId block1, BlockId block2) {
LinkedList<BlockCapsule> list1 = new LinkedList<>();
LinkedList<BlockCapsule> list2 = new LinkedList<>();
KhaosBlock kblk1 = miniStore.getByHash(block1);
KhaosBlock kblk2 = miniStore.getByHash(block2);
if (kblk1 != null && kblk2 != null) {
while (!Objects.equals(kblk1, kblk2)) {
if (kblk1.num > kblk2.num) {
list1.add(kblk1.blk);
kblk1 = kblk1.getParent();
} else if (kblk1.num < kblk2.num) {
list2.add(kblk2.blk);
kblk2 = kblk2.getParent();
} else {
list1.add(kblk1.blk);
list2.add(kblk2.blk);
kblk1 = kblk1.getParent();
kblk2 = kblk2.getParent();
}
}
}
return new Pair<>(list1, list2);
}
// only for unittest
public BlockCapsule getParentBlock(Sha256Hash hash) {
return Stream.of(miniStore.getByHash(hash), miniUnlinkedStore.getByHash(hash))
.filter(Objects::nonNull)
.map(KhaosBlock::getParent)
.map(khaosBlock -> khaosBlock == null ? null : khaosBlock.blk)
.filter(Objects::nonNull)
.filter(b -> containBlock(b.getBlockId()))
.findFirst()
.orElse(null);
}
public boolean hasData() {
return !this.miniStore.hashKblkMap.isEmpty();
}
}