use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class BlockchainIndexIntegrityTest method testIndexIntegrityWithCorrectData.
/**
* Test the index integrity check and recovery when the index database is correct.
*/
@Test
public void testIndexIntegrityWithCorrectData() {
final int NUMBER_OF_BLOCKS = 5;
// build a blockchain with a few blocks
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
StandaloneBlockchain chain = bundle.bc;
Block bestBlock;
ImportResult result;
for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
bestBlock = chain.getBestBlock();
MiningBlock next = chain.createNewMiningBlock(bestBlock, Collections.emptyList(), true);
result = chain.tryToConnect(next);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// adding side chain
next = chain.createNewMiningBlock(bestBlock, Collections.emptyList(), true);
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(next.getHeader()).withExtraData("other".getBytes()).build();
next.updateHeader(newBlockHeader);
result = chain.tryToConnect(next);
assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
}
bestBlock = chain.getBestBlock();
assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
chain.getRepository().flush();
AionRepositoryImpl repo = chain.getRepository();
AionBlockStore blockStore = repo.getBlockStore();
// check that the index recovery succeeded
assertThat(blockStore.indexIntegrityCheck()).isEqualTo(AionBlockStore.IntegrityCheckResult.CORRECT);
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class BlockchainPruningTest method testTopPruningWithoutSideChains.
@Test
public void testTopPruningWithoutSideChains() throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException {
// Setup used accounts.
assertThat(accounts.size()).isAtLeast(12);
ECKey stakingRegistryOwner = accounts.get(0);
// Lists of stakers.
List<ECKey> allStakes = List.of(accounts.get(1), accounts.get(2), accounts.get(3), accounts.get(4), accounts.get(5), accounts.get(6));
List<ECKey> mainStakers = List.of(accounts.get(1), accounts.get(2), accounts.get(3));
List<ECKey> otherStakers = List.of(accounts.get(4), accounts.get(5), accounts.get(6));
// Lists of users.
List<ECKey> mainUsers = List.of(accounts.get(1), accounts.get(2), accounts.get(3), accounts.get(7), accounts.get(8), accounts.get(9));
List<ECKey> otherUsers = List.of(accounts.get(4), accounts.get(5), accounts.get(6), accounts.get(10), accounts.get(11), accounts.get(0));
// Setup the blockchain.
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain chain = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).withAvmEnabled().build().bc;
chain.forkUtility.enableUnityFork(unityForkBlock);
// Setup TOP pruning for the repository.
AionRepositoryImpl repository = chain.getRepository();
repository.setupTopPruning(1);
// Setup the first block in the chain with the staker registry deployment.
Block nextBlock = BlockchainTestUtils.generateNextMiningBlockWithStakerRegistry(chain, chain.getGenesis(), resourceProvider, stakingRegistryOwner);
Pair<ImportResult, AionBlockSummary> result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(result.getRight().getReceipts().get(0).isSuccessful()).isTrue();
assertThat(result.getRight().getReceipts().get(0).getLogInfoList()).isNotEmpty();
assertThat(result.getRight().getReceipts().get(0).getEnergyUsed()).isEqualTo(1_225_655L);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
// Set the staking contract address in the staking genesis.
AionTransaction deploy = nextBlock.getTransactionsList().get(0);
AionAddress contract = TxUtil.calculateContractAddress(deploy.getSenderAddress().toByteArray(), deploy.getNonceBI());
chain.getGenesis().setStakingContractAddress(contract);
// Create block to register all stakers.
nextBlock = BlockchainTestUtils.generateNextMiningBlockWithStakers(chain, chain.getBestBlock(), resourceProvider, allStakes, MIN_SELF_STAKE);
result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
// Verify that all stakers were registered.
verifyReceipts(result.getRight().getReceipts(), allStakes.size(), true);
verifyEffectiveSelfStake(otherStakers, chain, nextBlock, MIN_SELF_STAKE);
// Generate random transactions for all accounts to add them to the state.
List<AionTransaction> txs = BlockchainTestUtils.generateTransactions(1_000, accounts, chain.getRepository());
nextBlock = BlockchainTestUtils.generateNextStakingBlock(chain, nextBlock, txs, otherStakers.get(0));
result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
BigInteger expectedStake = MIN_SELF_STAKE;
for (int i = 0; i < 6; i++) {
// Add blocks with transactions for mainStakers and mainUsers.
for (int j = 0; j < 6; j++) {
// Add transactions for frequent users.
txs = BlockchainTestUtils.generateTransactions(1_000, mainUsers, chain.getRepository());
// Seal the block with a frequent staker.
ECKey staker = mainStakers.get((i + j) % mainStakers.size());
if (nextBlock instanceof MiningBlock) {
nextBlock = BlockchainTestUtils.generateNextStakingBlock(chain, nextBlock, txs, staker);
} else {
nextBlock = BlockchainTestUtils.generateNextMiningBlock(chain, nextBlock, txs);
}
result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
}
// Increase stake of mainStakes.
txs = BlockchainTestUtils.generateIncreaseStakeTransactions(chain, nextBlock, resourceProvider, mainStakers, MIN_SELF_STAKE);
assertThat(txs.size()).isEqualTo(mainStakers.size());
nextBlock = BlockchainTestUtils.generateNextMiningBlock(chain, nextBlock, txs);
result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
verifyReceipts(result.getRight().getReceipts(), mainStakers.size(), false);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
// Verify stakers effective stake update.
expectedStake = expectedStake.add(MIN_SELF_STAKE);
verifyEffectiveSelfStake(mainStakers, chain, nextBlock, expectedStake);
// Add transactions for infrequent users.
txs = BlockchainTestUtils.generateTransactions(10, otherUsers, chain.getRepository());
// Seal the block with an infrequent staker.
ECKey staker = otherStakers.get(i % otherStakers.size());
nextBlock = BlockchainTestUtils.generateNextStakingBlock(chain, nextBlock, txs, staker);
result = chain.tryToConnectAndFetchSummary(nextBlock);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Ensure the current state was not pruned after the import.
verifyFullState(repository, nextBlock);
}
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class BlockchainTestUtils method generateNextBlock.
/**
* @param chain blockchain implementation to be populated
* @param accounts existing accounts
* @param txCount maximum number of transactions per block
*/
public static Block generateNextBlock(StandaloneBlockchain chain, List<ECKey> accounts, int txCount) {
Block block, parent = chain.getBestBlock();
AionRepositoryImpl repo = chain.getRepository();
List<AionTransaction> txs = generateTransactions(txCount, accounts, repo);
long time = System.currentTimeMillis();
block = chain.createNewMiningBlockInternal(parent, txs, true, time / TEN_THOUSAND_MS).block;
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) block.getHeader()).withExtraData(String.valueOf(time).getBytes()).build();
block.updateHeader(newBlockHeader);
return block;
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class SignatureSchemeSwapForkTest method testRejectFvmContractDeploy.
@Test
// We ignore this test due to the FVM contract deploy is not disable in ver1.6
@Ignore
public void testRejectFvmContractDeploy() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
// setup Unity fork and AVM
long unityForkBlock = 2;
long signatureSwapForkBlockHeight = unityForkBlock + 3;
setupAVM(unityForkBlock);
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
// setup an identical blockchains
StandaloneBlockchain blockchain = setupIdenticalBlockchain(unityForkBlock, signatureSwapForkBlockHeight);
// create block with staker registry
Block blockWithRegistry = BlockchainTestUtils.generateNextMiningBlockWithStakerRegistry(blockchain, blockchain.getGenesis(), resourceProvider, stakingRegistryOwner);
// import block on firstChain
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(blockWithRegistry);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(result.getRight().getReceipts().get(0).isSuccessful()).isTrue();
assertThat(result.getRight().getReceipts().get(0).getLogInfoList()).isNotEmpty();
assertThat(result.getRight().getReceipts().get(0).getEnergyUsed()).isEqualTo(1_225_655L);
// set the staking contract address in the staking genesis
AionTransaction deploy = blockWithRegistry.getTransactionsList().get(0);
AionAddress contract = TxUtil.calculateContractAddress(deploy.getSenderAddress().toByteArray(), deploy.getNonceBI());
blockchain.getGenesis().setStakingContractAddress(contract);
// create Unity block with stakers
Block block2Unity = BlockchainTestUtils.generateNextMiningBlockWithStakers(blockchain, blockchain.getBestBlock(), resourceProvider, stakers, MIN_SELF_STAKE);
// import block2Unity on blockchain
result = blockchain.tryToConnectAndFetchSummary(block2Unity);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
verifyReceipts(result.getRight().getReceipts(), 3, true);
// create staking block
Block block3Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block3Staking).isNotNull();
// import block3Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block3Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next mining block
AionTransaction tx = BlockchainTestUtils.deployFvmTickerContractTransaction(accounts.get(4), BigInteger.ZERO);
Block block4Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx));
assertThat(block4Mining.getTransactionsList().size() == 1).isTrue();
// import block4Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block4Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create the first signatureSchemeSwap block
tx = BlockchainTestUtils.deployFvmTickerContractTransaction(accounts.get(5), BigInteger.ZERO);
Block block5SignatureSchemeSwapStaking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx), stakers.get(0));
assertThat(block5SignatureSchemeSwapStaking).isNotNull();
assertThat(block5SignatureSchemeSwapStaking.getTransactionsList().size() == 0).isTrue();
// import block5SignatureSchemeSwapStaking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block5SignatureSchemeSwapStaking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next mining block with rejected transaction
Block block6Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx));
assertThat(block6Mining.getTransactionsList().size() == 0).isTrue();
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class SignatureSchemeSwapForkTest method testSigatureSchemeSwapForkWithFallbackTransaction.
@Test
public void testSigatureSchemeSwapForkWithFallbackTransaction() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
// setup Unity fork and AVM
long unityForkBlock = 2;
long signatureSwapForkBlockHeight = unityForkBlock + 3;
setupAVM(unityForkBlock);
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
// setup an identical blockchains
StandaloneBlockchain blockchain = setupIdenticalBlockchain(unityForkBlock, signatureSwapForkBlockHeight);
// create block with staker registry
Block blockWithRegistry = BlockchainTestUtils.generateNextMiningBlockWithStakerRegistry(blockchain, blockchain.getGenesis(), resourceProvider, stakingRegistryOwner);
// import block on firstChain
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(blockWithRegistry);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(result.getRight().getReceipts().get(0).isSuccessful()).isTrue();
assertThat(result.getRight().getReceipts().get(0).getLogInfoList()).isNotEmpty();
assertThat(result.getRight().getReceipts().get(0).getEnergyUsed()).isEqualTo(1_225_655L);
// set the staking contract address in the staking genesis
AionTransaction deploy = blockWithRegistry.getTransactionsList().get(0);
AionAddress contract = TxUtil.calculateContractAddress(deploy.getSenderAddress().toByteArray(), deploy.getNonceBI());
blockchain.getGenesis().setStakingContractAddress(contract);
// create Unity block with stakers
Block block2Unity = BlockchainTestUtils.generateNextMiningBlockWithStakers(blockchain, blockchain.getBestBlock(), resourceProvider, stakers, MIN_SELF_STAKE);
// import block2Unity on blockchain
result = blockchain.tryToConnectAndFetchSummary(block2Unity);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
verifyReceipts(result.getRight().getReceipts(), 3, true);
// create staking block
Block block3Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block3Staking).isNotNull();
// import block3Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block3Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create an invalid transaction for the hardfork fallback.
List<AionTransaction> fallbackTx = BlockchainTestUtils.generateTransactions(1, accounts, blockchain.getRepository(), true);
List<byte[]> fallbackTxHash = new ArrayList<>();
for (AionTransaction a : fallbackTx) {
fallbackTxHash.add(a.getTransactionHash());
}
// cracking the config settings
CfgAion.inst().getFork().setRollbackTx(fallbackTxHash);
// create next mining block
Block block4Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), fallbackTx);
// import block4Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block4Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Check the transaction has been processed
assertThat(fallbackTx.get(0).getValueBI().equals(blockchain.getRepository().getBalance(fallbackTx.get(0).getDestinationAddress())));
BigInteger senderBalance = blockchain.getRepository().getBalance(fallbackTx.get(0).getSenderAddress());
// create the first signatureSchemeSwap block
Block block5SignatureSchemeSwapStaking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block5SignatureSchemeSwapStaking).isNotNull();
// import block5SignatureSchemeSwapStaking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block5SignatureSchemeSwapStaking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Check the invalid account has been fallbacked
assertThat(blockchain.getRepository().getAccountState(fallbackTx.get(0).getDestinationAddress()) == null);
BigInteger senderBalanceNew = blockchain.getRepository().getBalance(fallbackTx.get(0).getSenderAddress());
assertThat(senderBalanceNew.equals(senderBalance.add(fallbackTx.get(0).getValueBI())));
// create next mining block
Block block6Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList());
// import block6Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block6Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next staking block
Block block7Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
// import block7Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block7Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
}
Aggregations