Search in sources :

Example 46 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class AionImplTest method testAddNewBlockSameAsTheBestBlock.

@Test
public void testAddNewBlockSameAsTheBestBlock() {
    doReturn(blockHeight).when(newBlock).getNumber();
    doCallRealMethod().when(chainImpl).addNewBlock(newBlock);
    ImportResult result = chainImpl.addNewBlock(newBlock);
    assertThat(result.equals(ImportResult.INVALID_BLOCK)).isTrue();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Test(org.junit.Test)

Example 47 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class AionImplTest method testAddNewBlockLessThenTheBestBlock.

@Test
public void testAddNewBlockLessThenTheBestBlock() {
    doReturn(blockHeight - 1).when(newBlock).getNumber();
    doCallRealMethod().when(chainImpl).addNewBlock(newBlock);
    ImportResult result = chainImpl.addNewBlock(newBlock);
    assertThat(result.equals(ImportResult.INVALID_BLOCK)).isTrue();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Test(org.junit.Test)

Example 48 with ImportResult

use of org.aion.zero.impl.core.ImportResult 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();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) TestResourceProvider(org.aion.zero.impl.vm.TestResourceProvider) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with ImportResult

use of org.aion.zero.impl.core.ImportResult 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);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) TestResourceProvider(org.aion.zero.impl.vm.TestResourceProvider) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) Block(org.aion.zero.impl.types.Block) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 50 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class StandaloneBlockchainTest method testBlockChainShutdownhook.

@Test
public void testBlockChainShutdownhook() {
    exit.expectSystemExitWithStatus(SystemExitCodes.NORMAL);
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain sb = bundle.bc;
    AionBlockchainImpl.shutdownHook = 2;
    Block block1 = sb.createBlock(sb.genesis, Collections.EMPTY_LIST, false, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    ImportResult result = sb.tryToConnect(block1);
    assertThat(result.isBest()).isTrue();
    Block block2 = sb.createBlock(block1, Collections.EMPTY_LIST, false, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    // expect the system will be shutdown when import the block2
    sb.tryToConnect(new BlockWrapper(block2));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Aggregations

ImportResult (org.aion.zero.impl.core.ImportResult)166 Test (org.junit.Test)127 AionTransaction (org.aion.base.AionTransaction)114 AionAddress (org.aion.types.AionAddress)106 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)95 MiningBlock (org.aion.zero.impl.types.MiningBlock)90 Block (org.aion.zero.impl.types.Block)80 BigInteger (java.math.BigInteger)75 AionTxReceipt (org.aion.base.AionTxReceipt)50 AionTxExecSummary (org.aion.base.AionTxExecSummary)31 ECKey (org.aion.crypto.ECKey)28 RepositoryCache (org.aion.base.db.RepositoryCache)22 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)22 InternalTransaction (org.aion.types.InternalTransaction)20 AccountState (org.aion.base.AccountState)18 ArrayList (java.util.ArrayList)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 BlockContext (org.aion.zero.impl.types.BlockContext)9