Skip to content

Commit 75bf992

Browse files
committed
add unittest 1. doNotSwith when no common block between the current main chain with the fork chain, 2. switchBack when switch fork failed
1 parent 7443157 commit 75bf992

File tree

2 files changed

+119
-7
lines changed

2 files changed

+119
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ private void switchFork(BlockCapsule newHead)
618618
throw e;
619619
} finally {
620620
if (exception != null) {
621+
logger.warn("switch back because exception thrown while switching forks. " + exception.getMessage(),
622+
exception);
621623
first.forEach(khaosBlock -> khaosDb.removeBlk(khaosBlock.getBlk().getBlockId()));
622624
khaosDb.setHead(binaryTree.getValue().peekFirst());
623625

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

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import java.util.stream.Collectors;
88
import java.util.stream.IntStream;
99
import lombok.extern.slf4j.Slf4j;
10+
import org.junit.After;
1011
import org.junit.AfterClass;
1112
import org.junit.Assert;
13+
import org.junit.Before;
1214
import org.junit.BeforeClass;
1315
import org.junit.Test;
1416
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -53,13 +55,10 @@ public class ManagerTest {
5355
private static BlockCapsule blockCapsule2;
5456
private static String dbPath = "output_manager_test";
5557

56-
static {
58+
@Before
59+
public void init() {
5760
Args.setParam(new String[]{"-d", dbPath, "-w"}, Constant.TEST_CONF);
5861
context = new AnnotationConfigApplicationContext(DefaultConfig.class);
59-
}
60-
61-
@BeforeClass
62-
public static void init() {
6362

6463
dbManager = context.getBean(Manager.class);
6564

@@ -80,8 +79,8 @@ public static void init() {
8079
ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()));
8180
}
8281

83-
@AfterClass
84-
public static void removeDb() {
82+
@After
83+
public void removeDb() {
8584
Args.clearParam();
8685
FileUtil.deleteDir(new File(dbPath));
8786
context.destroy();
@@ -275,6 +274,104 @@ public void fork()
275274
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
276275
}
277276

277+
@Test
278+
public void doNotSwitch()
279+
throws ValidateSignatureException, ContractValidateException, ContractExeException,
280+
UnLinkedBlockException, ValidateScheduleException, BadItemException,
281+
ItemNotFoundException, HeaderNotFound, AccountResourceInsufficientException,
282+
TransactionExpirationException, TooBigTransactionException,
283+
DupTransactionException, BadBlockException,
284+
TaposException, BadNumberBlockException, NonCommonBlockException {
285+
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
286+
long size = dbManager.getBlockStore().dbSource.allKeys().size();
287+
System.out.print("block store size:" + size + "\n");
288+
String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
289+
byte[] privateKey = ByteArray.fromHexString(key);
290+
final ECKey ecKey = ECKey.fromPrivate(privateKey);
291+
byte[] address = ecKey.getAddress();
292+
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
293+
dbManager.addWitness(ByteString.copyFrom(address));
294+
dbManager.generateBlock(witnessCapsule, System.currentTimeMillis(), privateKey);
295+
296+
Map<ByteString, String> addressToProvateKeys = addTestWitnessAndAccount();
297+
298+
long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
299+
BlockCapsule blockCapsule0 =
300+
createTestBlockCapsule(
301+
num + 1,
302+
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
303+
addressToProvateKeys);
304+
305+
BlockCapsule blockCapsule1 =
306+
createTestBlockCapsule(
307+
num + 1,
308+
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
309+
addressToProvateKeys);
310+
311+
BlockCapsule blockCapsule2 =
312+
createTestBlockCapsule(
313+
num + 2, blockCapsule1.getBlockId().getByteString(), addressToProvateKeys);
314+
315+
dbManager.pushBlock(blockCapsule0);
316+
dbManager.pushBlock(blockCapsule1);
317+
context.getBean(KhaosDatabase.class).removeBlk(dbManager.getBlockIdByNum(num));
318+
try {
319+
dbManager.pushBlock(blockCapsule2);
320+
} catch (NonCommonBlockException e) {
321+
logger.info("do not switch fork");
322+
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()));
323+
}
324+
}
325+
326+
@Test
327+
public void switchBack()
328+
throws ValidateSignatureException, ContractValidateException, ContractExeException,
329+
UnLinkedBlockException, ValidateScheduleException, BadItemException,
330+
ItemNotFoundException, HeaderNotFound, AccountResourceInsufficientException,
331+
TransactionExpirationException, TooBigTransactionException,
332+
DupTransactionException, BadBlockException,
333+
TaposException, BadNumberBlockException, NonCommonBlockException {
334+
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
335+
long size = dbManager.getBlockStore().dbSource.allKeys().size();
336+
System.out.print("block store size:" + size + "\n");
337+
String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
338+
byte[] privateKey = ByteArray.fromHexString(key);
339+
final ECKey ecKey = ECKey.fromPrivate(privateKey);
340+
byte[] address = ecKey.getAddress();
341+
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
342+
dbManager.addWitness(ByteString.copyFrom(address));
343+
dbManager.generateBlock(witnessCapsule, System.currentTimeMillis(), privateKey);
344+
345+
Map<ByteString, String> addressToProvateKeys = addTestWitnessAndAccount();
346+
347+
long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
348+
BlockCapsule blockCapsule0 =
349+
createTestBlockCapsule(
350+
num + 1,
351+
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
352+
addressToProvateKeys);
353+
354+
BlockCapsule blockCapsule1 =
355+
createTestBlockCapsule(
356+
num + 1,
357+
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
358+
addressToProvateKeys);
359+
360+
BlockCapsule blockCapsule2 =
361+
createTestBlockCapsuleError(
362+
num + 2, blockCapsule1.getBlockId().getByteString(), addressToProvateKeys);
363+
364+
dbManager.pushBlock(blockCapsule0);
365+
dbManager.pushBlock(blockCapsule1);
366+
try {
367+
dbManager.pushBlock(blockCapsule2);
368+
} catch (Exception e) {
369+
logger.info("the fork chain has error block");
370+
}
371+
372+
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule0.getBlockId().getBytes()));
373+
}
374+
278375
private Map<ByteString, String> addTestWitnessAndAccount() {
279376
dbManager.getWitnesses().clear();
280377
return IntStream.range(0, 2)
@@ -309,4 +406,17 @@ private BlockCapsule createTestBlockCapsule(
309406
blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress)));
310407
return blockCapsule;
311408
}
409+
410+
private BlockCapsule createTestBlockCapsuleError(
411+
long number, ByteString hash, Map<ByteString, String> addressToProvateKeys) {
412+
long time = System.currentTimeMillis();
413+
WitnessController witnessController = dbManager.getWitnessController();
414+
ByteString witnessAddress =
415+
witnessController.getScheduledWitness(witnessController.getSlotAtTime(time));
416+
BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(hash), time, ByteString.copyFromUtf8("onlyTest"));
417+
blockCapsule.generatedByMyself = true;
418+
blockCapsule.setMerkleRoot();
419+
blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress)));
420+
return blockCapsule;
421+
}
312422
}

0 commit comments

Comments
 (0)