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