Search in sources :

Example 21 with IByteArrayKeyValueDatabase

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();
    }
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase)

Example 22 with IByteArrayKeyValueDatabase

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;
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase)

Example 23 with IByteArrayKeyValueDatabase

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));
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 24 with IByteArrayKeyValueDatabase

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));
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 25 with IByteArrayKeyValueDatabase

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();
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) ArrayList(java.util.ArrayList) TrieImpl(org.aion.mcf.trie.TrieImpl) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Aggregations

IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)26 Test (org.junit.Test)24 Parameters (junitparams.Parameters)18 ArrayList (java.util.ArrayList)3 ImportResult (org.aion.mcf.core.ImportResult)3 TrieImpl (org.aion.mcf.trie.TrieImpl)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 HashMap (java.util.HashMap)2 Address (org.aion.base.type.Address)2 DataWord (org.aion.mcf.vm.types.DataWord)2 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)2 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 Properties (java.util.Properties)1 IContractDetails (org.aion.base.db.IContractDetails)1 MockDBDriver (org.aion.db.impl.mockdb.MockDBDriver)1