Skip to content

Commit 91edda0

Browse files
authored
Merge pull request tronprotocol#1064 from tronprotocol/switch_back_when_switch_fork_failed
Switch back when switch fork failed
2 parents 33d8a4e + 1a2a7d7 commit 91edda0

File tree

11 files changed

+323
-116
lines changed

11 files changed

+323
-116
lines changed

src/main/java/org/tron/core/db/AbstractRevokingStore.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public synchronized void onRemove(RevokingTuple tuple, byte[] value) {
109109
}
110110

111111
@Override
112-
public synchronized void merge() throws RevokingStoreIllegalStateException {
112+
public synchronized void merge() {
113113
if (activeDialog <= 0) {
114114
throw new RevokingStoreIllegalStateException("activeDialog has to be greater than 0");
115115
}
@@ -148,7 +148,7 @@ public synchronized void merge() throws RevokingStoreIllegalStateException {
148148
.filter(e -> {
149149
boolean has = prevState.oldValues.containsKey(e.getKey());
150150
if (has) {
151-
prevState.removed.put(e.getKey(), e.getValue());
151+
prevState.removed.put(e.getKey(), prevState.oldValues.get(e.getKey()));
152152
prevState.oldValues.remove(e.getKey());
153153
}
154154

@@ -161,7 +161,7 @@ public synchronized void merge() throws RevokingStoreIllegalStateException {
161161
}
162162

163163
@Override
164-
public synchronized void revoke() throws RevokingStoreIllegalStateException {
164+
public synchronized void revoke() {
165165
if (disabled) {
166166
return;
167167
}
@@ -189,7 +189,7 @@ public synchronized void revoke() throws RevokingStoreIllegalStateException {
189189
}
190190

191191
@Override
192-
public synchronized void commit() throws RevokingStoreIllegalStateException {
192+
public synchronized void commit() {
193193
if (activeDialog <= 0) {
194194
throw new RevokingStoreIllegalStateException("activeDialog has to be greater than 0");
195195
}
@@ -198,7 +198,7 @@ public synchronized void commit() throws RevokingStoreIllegalStateException {
198198
}
199199

200200
@Override
201-
public synchronized void pop() throws RevokingStoreIllegalStateException {
201+
public synchronized void pop() {
202202
if (activeDialog != 0) {
203203
throw new RevokingStoreIllegalStateException("activeDialog has to be equal 0");
204204
}
@@ -311,28 +311,28 @@ public Dialog(RevokingDatabase revokingDatabase, boolean disableOnExit) {
311311
this.disableOnExit = disableOnExit;
312312
}
313313

314-
void commit() throws RevokingStoreIllegalStateException {
314+
void commit() {
315315
applyRevoking = false;
316316
revokingDatabase.commit();
317317
}
318318

319-
void revoke() throws RevokingStoreIllegalStateException {
319+
void revoke() {
320320
if (applyRevoking) {
321321
revokingDatabase.revoke();
322322
}
323323

324324
applyRevoking = false;
325325
}
326326

327-
void merge() throws RevokingStoreIllegalStateException {
327+
void merge() {
328328
if (applyRevoking) {
329329
revokingDatabase.merge();
330330
}
331331

332332
applyRevoking = false;
333333
}
334334

335-
void copy(Dialog dialog) throws RevokingStoreIllegalStateException {
335+
void copy(Dialog dialog) {
336336
if (this.equals(dialog)) {
337337
return;
338338
}
@@ -358,7 +358,7 @@ public void destroy() {
358358
}
359359

360360
@Override
361-
public void close() throws RevokingStoreIllegalStateException {
361+
public void close() {
362362
try {
363363
if (applyRevoking) {
364364
revokingDatabase.revoke();

src/main/java/org/tron/core/db/KhaosDatabase.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212
import java.util.Objects;
13+
import java.util.stream.Collectors;
1314
import java.util.stream.Stream;
1415
import javafx.util.Pair;
1516
import lombok.Getter;
@@ -21,12 +22,13 @@
2122
import org.tron.core.capsule.BlockCapsule;
2223
import org.tron.core.capsule.BlockCapsule.BlockId;
2324
import org.tron.core.exception.BadNumberBlockException;
25+
import org.tron.core.exception.NonCommonBlockException;
2426
import org.tron.core.exception.UnLinkedBlockException;
2527

2628
@Component
2729
public class KhaosDatabase extends TronDatabase {
2830

29-
private class KhaosBlock {
31+
public static class KhaosBlock {
3032

3133
public Sha256Hash getParentHash() {
3234
return this.blk.getParentHash();
@@ -38,6 +40,7 @@ public KhaosBlock(BlockCapsule blk) {
3840
this.num = blk.getNum();
3941
}
4042

43+
@Getter
4144
BlockCapsule blk;
4245
Reference<KhaosBlock> parent = new WeakReference<>(null);
4346
BlockId id;
@@ -82,10 +85,16 @@ public class KhaosStore {
8285

8386
@Override
8487
protected boolean removeEldestEntry(Map.Entry<Long, ArrayList<KhaosBlock>> entry) {
85-
if (entry.getKey() < Long.max(0L, head.num - maxCapcity)) {
86-
entry.getValue().forEach(b -> hashKblkMap.remove(b.id));
87-
return true;
88-
}
88+
long minNum = Long.max(0L, head.num - maxCapcity);
89+
Map<Long, ArrayList<KhaosBlock>> minNumMap = numKblkMap.entrySet().stream()
90+
.filter(e -> e.getKey() < minNum)
91+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
92+
93+
minNumMap.forEach((k, v) -> {
94+
numKblkMap.remove(k);
95+
v.forEach(b -> hashKblkMap.remove(b.id));
96+
});
97+
8998
return false;
9099
}
91100
};
@@ -260,33 +269,48 @@ public void setMaxSize(int maxSize) {
260269
/**
261270
* Find two block's most recent common parent block.
262271
*/
263-
public Pair<LinkedList<BlockCapsule>, LinkedList<BlockCapsule>> getBranch(
264-
Sha256Hash block1, Sha256Hash block2) {
265-
LinkedList<BlockCapsule> list1 = new LinkedList<>();
266-
LinkedList<BlockCapsule> list2 = new LinkedList<>();
272+
public Pair<LinkedList<KhaosBlock>, LinkedList<KhaosBlock>> getBranch(Sha256Hash block1, Sha256Hash block2)
273+
throws NonCommonBlockException {
274+
LinkedList<KhaosBlock> list1 = new LinkedList<>();
275+
LinkedList<KhaosBlock> list2 = new LinkedList<>();
267276
KhaosBlock kblk1 = miniStore.getByHash(block1);
277+
checkNull(kblk1);
268278
KhaosBlock kblk2 = miniStore.getByHash(block2);
279+
checkNull(kblk2);
269280

270-
if (kblk1 != null && kblk2 != null) {
271-
while (!Objects.equals(kblk1, kblk2)) {
272-
if (kblk1.num > kblk2.num) {
273-
list1.add(kblk1.blk);
274-
kblk1 = kblk1.getParent();
275-
} else if (kblk1.num < kblk2.num) {
276-
list2.add(kblk2.blk);
277-
kblk2 = kblk2.getParent();
278-
} else {
279-
list1.add(kblk1.blk);
280-
list2.add(kblk2.blk);
281-
kblk1 = kblk1.getParent();
282-
kblk2 = kblk2.getParent();
283-
}
284-
}
281+
while (kblk1.num > kblk2.num) {
282+
list1.add(kblk1);
283+
kblk1 = kblk1.getParent();
284+
checkNull(kblk1);
285+
checkNull(miniStore.getByHash(kblk1.id));
286+
}
287+
288+
while (kblk2.num > kblk1.num) {
289+
list2.add(kblk2);
290+
kblk2 = kblk2.getParent();
291+
checkNull(kblk2);
292+
checkNull(miniStore.getByHash(kblk2.id));
293+
}
294+
295+
while (!Objects.equals(kblk1, kblk2)) {
296+
list1.add(kblk1);
297+
list2.add(kblk2);
298+
kblk1 = kblk1.getParent();
299+
checkNull(kblk1);
300+
checkNull(miniStore.getByHash(kblk1.id));
301+
kblk2 = kblk2.getParent();
302+
checkNull(kblk2);
303+
checkNull(miniStore.getByHash(kblk2.id));
285304
}
286305

287306
return new Pair<>(list1, list2);
288307
}
289308

309+
private void checkNull(Object o) throws NonCommonBlockException {
310+
if (o == null) {
311+
throw new NonCommonBlockException();
312+
}
313+
}
290314
/**
291315
* Find two block's most recent common parent block.
292316
*/

0 commit comments

Comments
 (0)