Search in sources :

Example 6 with IByteArrayKeyValueDatabase

use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.

the class ConcurrencyTest method testConcurrentPut.

@Test
@Parameters(method = "databaseInstanceDefinitions")
public void testConcurrentPut(Properties dbDef) throws InterruptedException {
    dbDef.setProperty("db_name", DatabaseTestUtils.dbName + getNext());
    IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
    assertThat(db.open()).isTrue();
    // create distinct threads with
    List<Runnable> threads = new ArrayList<>();
    for (int i = 0; i < CONCURRENT_THREADS; i++) {
        addThread4Put(threads, db, "key-" + i);
    }
    // run threads
    assertConcurrent("Testing put(...) ", threads, TIME_OUT);
    // check that all values were added
    assertThat(db.keys().size()).isEqualTo(CONCURRENT_THREADS);
    // ensuring close
    db.close();
    assertThat(db.isClosed()).isTrue();
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 7 with IByteArrayKeyValueDatabase

use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AionContractDetailsTest method testExternalStorageSerialization.

@Test
public void testExternalStorageSerialization() {
    Address address = Address.wrap(RandomUtils.nextBytes(Address.ADDRESS_LEN));
    byte[] code = RandomUtils.nextBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IByteArrayKeyValueDatabase externalStorage = repository.getDetailsDatabase();
    AionContractDetailsImpl original = new AionContractDetailsImpl(0, 1000000);
    original.setExternalStorageDataSource(externalStorage);
    original.setAddress(address);
    original.setCode(code);
    original.externalStorage = true;
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT / 64 + 10; i++) {
        DataWord key = new DataWord(RandomUtils.nextBytes(16));
        DataWord value = new DataWord(RandomUtils.nextBytes(16));
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    byte[] rlp = original.getEncoded();
    AionContractDetailsImpl deserialized = new AionContractDetailsImpl();
    deserialized.setExternalStorageDataSource(externalStorage);
    deserialized.decode(rlp);
    assertEquals(deserialized.externalStorage, true);
    assertTrue(address.equals(deserialized.getAddress()));
    assertEquals(ByteUtil.toHexString(code), ByteUtil.toHexString(deserialized.getCode()));
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        assertEquals(elements.get(key), storage.get(key));
    }
    DataWord deletedKey = elements.keySet().iterator().next();
    deserialized.put(deletedKey, DataWord.ZERO);
    deserialized.put(new DataWord(RandomUtils.nextBytes(16)), DataWord.ZERO);
}
Also used : Address(org.aion.base.type.Address) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) HashMap(java.util.HashMap) DataWord(org.aion.mcf.vm.types.DataWord) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 8 with IByteArrayKeyValueDatabase

use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AionContractDetailsTest method testExternalStorageTransition.

@Test
public void testExternalStorageTransition() {
    Address address = Address.wrap(RandomUtils.nextBytes(Address.ADDRESS_LEN));
    byte[] code = RandomUtils.nextBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IByteArrayKeyValueDatabase externalStorage = repository.getDetailsDatabase();
    AionContractDetailsImpl original = new AionContractDetailsImpl(0, 1000000);
    original.setExternalStorageDataSource(externalStorage);
    original.setAddress(address);
    original.setCode(code);
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT / 64 + 10; i++) {
        DataWord key = new DataWord(RandomUtils.nextBytes(16));
        DataWord value = new DataWord(RandomUtils.nextBytes(16));
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    assertTrue(!externalStorage.isEmpty());
    IContractDetails deserialized = deserialize(original.getEncoded(), externalStorage);
    // adds keys for in-memory storage limit overflow
    for (int i = 0; i < 10; i++) {
        DataWord key = new DataWord(RandomUtils.nextBytes(16));
        DataWord value = new DataWord(RandomUtils.nextBytes(16));
        elements.put(key, value);
        deserialized.put(key, value);
    }
    deserialized.syncStorage();
    assertTrue(!externalStorage.isEmpty());
    deserialized = deserialize(deserialized.getEncoded(), externalStorage);
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        assertEquals(elements.get(key), storage.get(key));
    }
}
Also used : IContractDetails(org.aion.base.db.IContractDetails) Address(org.aion.base.type.Address) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) HashMap(java.util.HashMap) DataWord(org.aion.mcf.vm.types.DataWord) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 9 with IByteArrayKeyValueDatabase

use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.

the class BlockchainDataRecoveryTest method testRecoverWorldStateWithStartFromGenesis.

/**
 * Test the recovery of the world state in the case where it is missing from the database.
 */
@Test
public void testRecoverWorldStateWithStartFromGenesis() {
    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;
    // all blocks will be incorrect
    ImportResult result;
    List<byte[]> statesToDelete = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_BLOCKS; 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();
    // System.out.println(Hex.toHexString(chain.getRepository().getRoot()));
    // 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();
    }
    // System.out.println(Hex.toHexString(chain.getRepository().getRoot()));
    // 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)

Example 10 with IByteArrayKeyValueDatabase

use of org.aion.base.db.IByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AccessWithExceptionTest method testDeleteBatchWithClosedDatabase.

@Test(expected = RuntimeException.class)
@Parameters(method = "databaseInstanceDefinitions")
public void testDeleteBatchWithClosedDatabase(Properties dbDef) {
    // create database
    dbDef.setProperty("db_name", DatabaseTestUtils.dbName + DatabaseTestUtils.getNext());
    IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
    assertThat(db.isOpen()).isFalse();
    List<byte[]> list = new ArrayList<>();
    list.add(DatabaseTestUtils.randomBytes(32));
    list.add(DatabaseTestUtils.randomBytes(32));
    list.add(DatabaseTestUtils.randomBytes(32));
    // attempt deleteBatch on closed db
    db.deleteBatch(list);
}
Also used : IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) Parameters(junitparams.Parameters) 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