Search in sources :

Example 26 with IByteArrayKeyValueDatabase

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

the class BlockchainDataRecoveryTest method testRecoverWorldStateWithoutGenesis.

/**
 * Test the recovery of the world state in the case where it is missing from the database.
 */
@Test
public void testRecoverWorldStateWithoutGenesis() {
    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;
    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);
    }
    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();
    List<byte[]> statesToDelete = new ArrayList<>();
    statesToDelete.addAll(database.keys());
    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 was not recovered
    assertThat(worked).isFalse();
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isFalse();
}
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