Search in sources :

Example 6 with CfgPrune

use of org.aion.zero.impl.config.CfgPrune 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

CfgPrune (org.aion.zero.impl.config.CfgPrune)6 Properties (java.util.Properties)3 AionTransaction (org.aion.base.AionTransaction)3 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)3 MockRepositoryConfig (org.aion.zero.impl.db.MockRepositoryConfig)3 Test (org.junit.Test)3 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)2 RepositoryConfig (org.aion.zero.impl.db.RepositoryConfig)2 BlockContext (org.aion.zero.impl.types.BlockContext)2 Before (org.junit.Before)2 TokenBridgeContract (org.aion.precompiled.contracts.ATB.TokenBridgeContract)1 Block (org.aion.zero.impl.types.Block)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1