Search in sources :

Example 16 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase 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)

Example 17 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class BlockchainDataRecoveryTest method testRecoverIndexWithoutGenesis.

/**
 * Test the index recovery when the index database is empty.
 *
 * <p>Under these circumstances the recovery process will fail.
 */
@Test
public void testRecoverIndexWithoutGenesis() {
    // build a blockchain with a few blocks
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    BlockContext context;
    List<AionTransaction> txs;
    long time = System.currentTimeMillis();
    List<MiningBlock> blocksToImport = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
        blocksToImport.add(context.block);
    }
    Block bestBlock = chain.getBestBlock();
    assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
    ByteArrayKeyValueDatabase indexDatabase = repo.getIndexDatabase();
    // 1: direct recovery call
    repo.flush();
    // deleting the entire index database
    indexDatabase.drop();
    // ensure that the index was corrupted
    assertThat(repo.isIndexed(bestBlock.getHash(), bestBlock.getNumber())).isFalse();
    // call the recovery functionality
    boolean worked = chain.recoverIndexEntry(repo, bestBlock);
    // ensure that the best block is unchanged
    assertThat(chain.getBestBlockHash()).isEqualTo(bestBlock.getHash());
    // check that the index recovery failed
    assertThat(worked).isFalse();
    assertThat(repo.isIndexed(bestBlock.getHash(), bestBlock.getNumber())).isFalse();
    // 2: recovery at import
    repo.flush();
    // deleting the entire index database
    indexDatabase.drop();
    // ensure that the index was corrupted
    assertThat(repo.isIndexed(bestBlock.getHash(), bestBlock.getNumber())).isFalse();
    // call the recovery functionality indirectly
    for (MiningBlock block : blocksToImport) {
        // index missing before import
        assertThat(repo.isIndexed(block.getHash(), block.getNumber())).isFalse();
        assertThat(chain.tryToConnect(block)).isEqualTo(ImportResult.EXIST);
        // index missing after import
        assertThat(repo.isIndexed(block.getHash(), block.getNumber())).isFalse();
    }
    // ensure that the best block is unchanged
    assertThat(chain.getBestBlockHash()).isEqualTo(bestBlock.getHash());
    // check that the index recovery failed
    assertThat(repo.isIndexed(bestBlock.getHash(), bestBlock.getNumber())).isFalse();
}
Also used : BlockContext(org.aion.zero.impl.types.BlockContext) ArrayList(java.util.ArrayList) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 18 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class RocksDBDriverTest method testDriverReturnDatabase.

// It should return an instance of the DB given the correct properties
@Test
public void testDriverReturnDatabase() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, dbVendor);
    props.setProperty(Props.DB_NAME, dbName);
    props.setProperty(Props.DB_PATH, dbPath);
    ByteArrayKeyValueDatabase db = DatabaseFactory.connect(props, log);
    assertNotNull(db);
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Properties(java.util.Properties) Test(org.junit.Test)

Example 19 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class LevelDBDriverTest method testDriverReturnNull.

// It should return null if given bad vendor
@Test
public void testDriverReturnNull() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, "BAD VENDOR");
    props.setProperty(Props.DB_NAME, dbName);
    props.setProperty(Props.DB_PATH, dbPath);
    ByteArrayKeyValueDatabase db = DatabaseFactory.connect(props, log);
    assertNull(db);
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Properties(java.util.Properties) Test(org.junit.Test)

Example 20 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class LevelDBDriverTest method testDriverReturnDatabase.

// It should return an instance of the DB given the correct properties
@Test
public void testDriverReturnDatabase() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, dbVendor);
    props.setProperty(Props.DB_NAME, dbName);
    props.setProperty(Props.DB_PATH, dbPath);
    ByteArrayKeyValueDatabase db = DatabaseFactory.connect(props, log);
    assertNotNull(db);
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)20 Test (org.junit.Test)15 Properties (java.util.Properties)6 MockDB (org.aion.db.impl.mockdb.MockDB)6 AionAddress (org.aion.types.AionAddress)5 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)5 SecureTrie (org.aion.zero.impl.trie.SecureTrie)5 HashMap (java.util.HashMap)4 RLPElement (org.aion.rlp.RLPElement)4 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)4 Logger (org.slf4j.Logger)3 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 Block (org.aion.zero.impl.types.Block)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AionTransaction (org.aion.base.AionTransaction)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