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