use of org.aion.zero.impl.db.AionBlockStore.BlockInfo in project aion by aionnetwork.
the class BlockchainIndexIntegrityTest method testIndexIntegrityWithRecovery.
/**
* Test the index integrity check and recovery when the index database is incorrect.
*/
@Test
public void testIndexIntegrityWithRecovery() {
final int NUMBER_OF_BLOCKS = 5;
// build a blockchain with a few blocks
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
StandaloneBlockchain chain = bundle.bc;
Block bestBlock;
ImportResult result;
for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
bestBlock = chain.getBestBlock();
MiningBlock next = chain.createNewMiningBlock(bestBlock, Collections.emptyList(), true);
result = chain.tryToConnect(next);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// adding side chain
next = chain.createNewMiningBlock(bestBlock, Collections.emptyList(), true);
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(next.getHeader()).withExtraData("other".getBytes()).build();
next.updateHeader(newBlockHeader);
result = chain.tryToConnect(next);
assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
}
bestBlock = chain.getBestBlock();
assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
chain.getRepository().flush();
AionRepositoryImpl repo = chain.getRepository();
ByteArrayKeyValueDatabase indexDatabase = repo.getIndexDatabase();
// corrupting the index at level 2
ArrayStore<List<BlockInfo>> index = Stores.newArrayStore(indexDatabase, BLOCK_INFO_SERIALIZER);
List<BlockInfo> infos = index.get(2);
assertThat(infos.size()).isEqualTo(2);
for (BlockInfo bi : infos) {
bi.setTotalDifficulty(bi.getTotalDifficulty().add(BigInteger.TEN));
}
index.set(2, infos);
AionBlockStore blockStore = repo.getBlockStore();
// check that the index recovery succeeded
assertThat(blockStore.indexIntegrityCheck()).isEqualTo(AionBlockStore.IntegrityCheckResult.FIXED);
}
Aggregations