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);
}
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();
}
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);
}
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);
}
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);
}
Aggregations