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