Skip to content

Commit 6b61bc5

Browse files
authored
Merge pull request tronprotocol#1085 from tronprotocol/switch_back_when_switch_fork_failed
Switch back when switch fork failed
2 parents 91edda0 + 9afeb06 commit 6b61bc5

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/test/java/org/tron/core/db/ManagerTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,19 @@ public void doNotSwitch()
315315
dbManager.pushBlock(blockCapsule0);
316316
dbManager.pushBlock(blockCapsule1);
317317
context.getBean(KhaosDatabase.class).removeBlk(dbManager.getBlockIdByNum(num));
318+
Exception exception = null;
318319
try {
319320
dbManager.pushBlock(blockCapsule2);
320321
} catch (NonCommonBlockException e) {
321322
logger.info("do not switch fork");
322323
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()));
324+
Assert.assertEquals(blockCapsule0.getBlockId(),
325+
dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()).getBlockId());
326+
exception = e;
327+
}
328+
329+
if (exception == null) {
330+
throw new IllegalStateException();
323331
}
324332
}
325333

@@ -365,11 +373,13 @@ public void switchBack()
365373
dbManager.pushBlock(blockCapsule1);
366374
try {
367375
dbManager.pushBlock(blockCapsule2);
368-
} catch (Exception e) {
376+
} catch (ValidateScheduleException e) {
369377
logger.info("the fork chain has error block");
370378
}
371379

372380
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()));
381+
Assert.assertEquals(blockCapsule0.getBlockId(),
382+
dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()).getBlockId());
373383
}
374384

375385
private Map<ByteString, String> addTestWitnessAndAccount() {

src/test/java/org/tron/core/db/RevokingStoreTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,39 @@ public synchronized void testUndo() throws RevokingStoreIllegalStateException {
6464
Assert.assertTrue(revokingDatabase.getStack().isEmpty());
6565
Assert.assertTrue(tronDatabase.getDbSource().allKeys().isEmpty());
6666
Assert.assertEquals(revokingDatabase.getActiveDialog(), 0);
67+
68+
dialog = DialogOptional.instance().setValue(revokingDatabase.buildDialog());
69+
revokingDatabase.disable();
70+
TestProtoCapsule testProtoCapsule = new TestProtoCapsule("del".getBytes());
71+
tronDatabase.put(testProtoCapsule.getData(), testProtoCapsule);
72+
revokingDatabase.enable();
73+
74+
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
75+
tronDatabase.put(testProtoCapsule.getData(), new TestProtoCapsule("del2".getBytes()));
76+
tmpDialog.merge();
77+
}
78+
79+
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
80+
tronDatabase.put(testProtoCapsule.getData(), new TestProtoCapsule("del22".getBytes()));
81+
tmpDialog.merge();
82+
}
83+
84+
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
85+
tronDatabase.put(testProtoCapsule.getData(), new TestProtoCapsule("del222".getBytes()));
86+
tmpDialog.merge();
87+
}
88+
89+
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
90+
tronDatabase.delete(testProtoCapsule.getData());
91+
tmpDialog.merge();
92+
}
93+
94+
dialog.reset();
95+
96+
logger.info("**********testProtoCapsule:" + String.valueOf(tronDatabase.get(testProtoCapsule.getData())));
97+
Assert.assertArrayEquals("del".getBytes(), tronDatabase.get(testProtoCapsule.getData()).getData());
98+
Assert.assertEquals(testProtoCapsule, tronDatabase.get(testProtoCapsule.getData()));
99+
67100
tronDatabase.close();
68101
}
69102

0 commit comments

Comments
 (0)