Search in sources :

Example 1 with BlockInfo

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);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionBlockStore(org.aion.zero.impl.db.AionBlockStore) MiningBlock(org.aion.zero.impl.types.MiningBlock) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) BlockInfo(org.aion.zero.impl.db.AionBlockStore.BlockInfo) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) List(java.util.List) Test(org.junit.Test)

Aggregations

List (java.util.List)1 ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)1 ImportResult (org.aion.zero.impl.core.ImportResult)1 AionBlockStore (org.aion.zero.impl.db.AionBlockStore)1 BlockInfo (org.aion.zero.impl.db.AionBlockStore.BlockInfo)1 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)1 Block (org.aion.zero.impl.types.Block)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)1 Test (org.junit.Test)1