use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.
the class DriverBaseTest method testOpenSecondInstance.
@Test
public void testOpenSecondInstance() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (db.isPersistent()) {
// another connection to same DB should fail on open for all persistent KVDBs
IByteArrayKeyValueDatabase otherDatabase = this.constructor.newInstance(this.args);
assertThat(otherDatabase.open()).isFalse();
// ensuring that new connection did not somehow close old one
assertThat(db.isOpen()).isTrue();
assertThat(db.isLocked()).isFalse();
}
}
use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.
the class AbstractRepository method connectAndOpen.
private IByteArrayKeyValueDatabase connectAndOpen(Properties info) {
// get the database object
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(info);
// open the database connection
db.open();
// check object status
if (db == null) {
LOG.error("Database <{}> connection could not be established for <{}>.", info.getProperty("db_type"), info.getProperty("db_name"));
}
// check persistence status
if (!db.isCreatedOnDisk()) {
LOG.error("Database <{}> cannot be saved to disk for <{}>.", info.getProperty("db_type"), info.getProperty("db_name"));
}
return db;
}
use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.
the class AccessWithExceptionTest method testGetWithClosedDatabase.
@Test(expected = RuntimeException.class)
@Parameters(method = "databaseInstanceDefinitions")
public void testGetWithClosedDatabase(Properties dbDef) {
// create database
dbDef.setProperty("db_name", DatabaseTestUtils.dbName + DatabaseTestUtils.getNext());
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
assertThat(db.isOpen()).isFalse();
// attempt get on closed db
db.get(DatabaseTestUtils.randomBytes(32));
}
use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.
the class AccessWithExceptionTest method testPutWithClosedDatabase.
@Test(expected = RuntimeException.class)
@Parameters(method = "databaseInstanceDefinitions")
public void testPutWithClosedDatabase(Properties dbDef) {
// create database
dbDef.setProperty("db_name", DatabaseTestUtils.dbName + DatabaseTestUtils.getNext());
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
assertThat(db.isOpen()).isFalse();
// attempt put on closed db
db.put(DatabaseTestUtils.randomBytes(32), DatabaseTestUtils.randomBytes(32));
}
use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.
the class BlockchainDataRecoveryTest method testRecoverWorldStateWithPartialWorldState.
/**
* Test the recovery of the world state in the case where it is missing from the database.
*/
@Test
public void testRecoverWorldStateWithPartialWorldState() {
final int NUMBER_OF_BLOCKS = 10;
// build a blockchain with a few blocks
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
StandaloneBlockchain chain = bundle.bc;
// first half of blocks will be correct
ImportResult result;
for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
result = chain.tryToConnect(chain.createNewBlock(chain.getBestBlock(), Collections.emptyList(), true));
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
}
// second half of blocks will miss the state root
List<byte[]> statesToDelete = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
AionBlock next = chain.createNewBlock(chain.getBestBlock(), Collections.emptyList(), true);
result = chain.tryToConnect(next);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
statesToDelete.add(next.getStateRoot());
}
AionBlock bestBlock = chain.getBestBlock();
assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
chain.getRepository().flush();
// delete some world state root entries from the database
TrieImpl trie = (TrieImpl) ((AionRepositoryImpl) chain.getRepository()).getWorldState();
IByteArrayKeyValueDatabase database = (IByteArrayKeyValueDatabase) trie.getCache().getDb();
for (byte[] key : statesToDelete) {
database.delete(key);
assertThat(trie.isValidRoot(key)).isFalse();
}
// ensure that the world state was corrupted
assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isFalse();
// call the recovery functionality
boolean worked = chain.recoverWorldState(chain.getRepository(), bestBlock.getNumber());
// ensure that the blockchain is ok
assertThat(chain.getBestBlockHash()).isEqualTo(bestBlock.getHash());
// ensure that the world state is ok
assertThat(worked).isTrue();
assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isTrue();
}
Aggregations