Search in sources :

Example 1 with MockRepositoryConfig

use of org.aion.zero.impl.db.MockRepositoryConfig in project aion by aionnetwork.

the class BlockchainImplementationTest method testIsPruneRestricted_wSpreadState.

/**
 * In SPREAD mode the top K blocks and the blocks that are multiples of the archive rate have a
 * stored state. There are no restrictions due to pruning.
 */
@Test
public void testIsPruneRestricted_wSpreadState() {
    // number of blocks stored by the blockchain
    int stored = 150;
    // the maximum height considered by this test
    int height = 1200;
    // the interval at which blocks are indexed
    int index = 1000;
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withRepoConfig(new MockRepositoryConfig(new CfgPrune(stored, index))).withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    BlockContext context;
    List<AionTransaction> txs;
    // creating (height) blocks
    long time = System.currentTimeMillis();
    for (int i = 0; i < height; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 100000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
    }
    // testing restriction for unrestricted blocks
    for (int i = height; i >= 0; i--) {
        assertThat(chain.isPruneRestricted(i)).isFalse();
        if (i % index == 0 || i >= height - stored) {
            // ensure the state exists
            assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isTrue();
        } else {
            // ensure the state is missing
            assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isFalse();
        }
    }
}
Also used : MockRepositoryConfig(org.aion.zero.impl.db.MockRepositoryConfig) BlockContext(org.aion.zero.impl.types.BlockContext) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) CfgPrune(org.aion.zero.impl.config.CfgPrune) Test(org.junit.Test)

Example 2 with MockRepositoryConfig

use of org.aion.zero.impl.db.MockRepositoryConfig in project aion by aionnetwork.

the class BlockchainImplementationTest method testIsPruneRestricted_wFullState.

/**
 * In FULL mode the state is stored for all blocks. There are no restrictions due to pruning.
 */
@Test
public void testIsPruneRestricted_wFullState() {
    // the maximum height considered by this test
    int height = 200;
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withRepoConfig(new MockRepositoryConfig(new CfgPrune(false))).withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    BlockContext context;
    List<AionTransaction> txs;
    // creating (height) blocks
    long time = System.currentTimeMillis();
    for (int i = 0; i < height; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
    }
    // testing restriction for unrestricted blocks
    for (int i = height; i >= 0; i--) {
        assertThat(chain.isPruneRestricted(i)).isFalse();
        // ensure the state exists
        assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isTrue();
    }
}
Also used : MockRepositoryConfig(org.aion.zero.impl.db.MockRepositoryConfig) BlockContext(org.aion.zero.impl.types.BlockContext) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) CfgPrune(org.aion.zero.impl.config.CfgPrune) Test(org.junit.Test)

Example 3 with MockRepositoryConfig

use of org.aion.zero.impl.db.MockRepositoryConfig in project aion by aionnetwork.

the class BlockchainImplementationTest method testIsPruneRestricted_wTopState.

/**
 * In TOP mode only the top K blocks have a stored state. Blocks older than the top K are have
 * restrictions due to pruning.
 */
@Test
public void testIsPruneRestricted_wTopState() {
    // number of blocks stored by the blockchain
    // note that min is 128
    int stored = 130;
    // the maximum height considered by this test
    int height = 200;
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withRepoConfig(new MockRepositoryConfig(new CfgPrune(stored))).withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    MiningBlock importBlock, sidechainBlock;
    List<AionTransaction> txs;
    // creating (height) blocks
    long time = System.currentTimeMillis();
    for (int i = 0; i < stored; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        importBlock = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L).block;
        assertThat(chain.tryToConnect(importBlock)).isEqualTo(ImportResult.IMPORTED_BEST);
    }
    Block fork = chain.getBestBlock();
    for (int i = stored; i < height; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        importBlock = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L).block;
        assertThat(chain.tryToConnect(importBlock)).isEqualTo(ImportResult.IMPORTED_BEST);
        // create the sidechain block
        repo.syncToRoot(fork.getStateRoot());
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
        repo.syncToRoot(chain.getBestBlock().getStateRoot());
        sidechainBlock = chain.createNewMiningBlockInternal(fork, txs, true, (time - 10) / 10000L).block;
        assertThat(chain.tryToConnect(sidechainBlock)).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
        fork = sidechainBlock;
    }
    // testing restriction for unrestricted blocks: height to (height - stored + 1)
    for (int i = height; i >= height - stored + 1; i--) {
        assertThat(chain.isPruneRestricted(i)).isFalse();
        // ensure the state exists
        assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isTrue();
    }
    // testing restriction for restricted blocks: (height - stored) to 0
    for (int i = height - stored; i >= 0; i--) {
        assertThat(chain.isPruneRestricted(i)).isTrue();
        // ensure the state is missing
        if (i < height - stored) {
            assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isFalse();
        } else {
            // NOTE: state at (height - stored) exists, but is already restricted
            assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isTrue();
        }
    }
}
Also used : MockRepositoryConfig(org.aion.zero.impl.db.MockRepositoryConfig) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) CfgPrune(org.aion.zero.impl.config.CfgPrune) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

AionTransaction (org.aion.base.AionTransaction)3 CfgPrune (org.aion.zero.impl.config.CfgPrune)3 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)3 MockRepositoryConfig (org.aion.zero.impl.db.MockRepositoryConfig)3 Test (org.junit.Test)3 BlockContext (org.aion.zero.impl.types.BlockContext)2 Block (org.aion.zero.impl.types.Block)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1