Search in sources :

Example 71 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class AvmBulkTransactionTest method sendValueTransferTransactionsInBulkTest.

@Test
public void sendValueTransferTransactionsInBulkTest() {
    int numTransactions = 50;
    // Create the accounts.
    List<ECKey> accounts = getRandomAccounts(numTransactions);
    // Grab the initial data we need to track.
    BigInteger expectedDeployerNonce = getNonce(this.deployerKey);
    BigInteger initialBalanceDeployer = getBalance(this.deployerKey);
    // Declare the various transfer amounts.
    List<BigInteger> transferAmounts = getRandomValues(numTransactions, 500, 5_000_000);
    printValuesUsed(transferAmounts);
    // Make the transactions, then bundle them up together.
    List<AionTransaction> transactions = new ArrayList<>();
    for (int i = 0; i < numTransactions; i++) {
        transactions.add(makeValueTransferTransaction(this.deployerKey, accounts.get(i), transferAmounts.get(i), expectedDeployerNonce));
        expectedDeployerNonce = expectedDeployerNonce.add(BigInteger.ONE);
    }
    // Process the transactions in bulk.
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // Verify all transactions were successful.
    assertEquals(numTransactions, blockSummary.getSummaries().size());
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        assertTrue(transactionSummary.getReceipt().isSuccessful());
    }
    BigInteger expectedDeployerBalance = initialBalanceDeployer;
    for (int i = 0; i < numTransactions; i++) {
        BigInteger energyUsed = BigInteger.valueOf(blockSummary.getSummaries().get(i).getReceipt().getEnergyUsed());
        expectedDeployerBalance = expectedDeployerBalance.subtract(energyUsed.multiply(BigInteger.valueOf(energyPrice))).subtract(transferAmounts.get(i));
    }
    // Verify account states after the transactions have been processed.
    for (int i = 0; i < numTransactions; i++) {
        assertEquals(transferAmounts.get(i), getBalance(accounts.get(i)));
        assertEquals(BigInteger.ZERO, getNonce(accounts.get(i)));
    }
    assertEquals(expectedDeployerBalance, getBalance(this.deployerKey));
    assertEquals(expectedDeployerNonce, getNonce(this.deployerKey));
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 72 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class AvmBulkTransactionTest method importBlockWithContractDeploysAndSubsequentCalls.

/**
 * Ensures that AVM contracts can be deployed and called within the same block.
 */
@Test
public void importBlockWithContractDeploysAndSubsequentCalls() {
    BigInteger expectedNonce = getNonce(deployerKey);
    BigInteger initialBalance = getBalance(deployerKey);
    // note: 3 contract deployments would pass the block energy limit
    int nbCreateTransactions = 2;
    int nbCallTransactions = 2;
    int nbTransactions = nbCreateTransactions + nbCreateTransactions * nbCallTransactions;
    List<AionTransaction> transactions = new ArrayList<>();
    for (int j = 0; j < nbCreateTransactions; j++) {
        // create contract transaction
        AionTransaction deployTx = makeAvmContractCreateTransaction(deployerKey, expectedNonce);
        expectedNonce = expectedNonce.add(BigInteger.ONE);
        AionAddress deployedContract = TxUtil.calculateContractAddress(deployTx);
        transactions.add(deployTx);
        // subsequent call transactions
        for (int i = 0; i < nbCallTransactions; i++) {
            transactions.add(makeAvmContractCallTransaction(deployerKey, expectedNonce, deployedContract));
            expectedNonce = expectedNonce.add(BigInteger.ONE);
        }
    }
    // process the transactions in bulk
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // verify all transactions were successful
    assertEquals(nbTransactions, blockSummary.getSummaries().size());
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        System.out.println(transactionSummary.getReceipt());
        assertTrue(transactionSummary.getReceipt().isSuccessful());
    }
    BigInteger expectedBalance = initialBalance;
    for (int i = 0; i < nbTransactions; i++) {
        BigInteger energyUsed = BigInteger.valueOf(blockSummary.getSummaries().get(i).getReceipt().getEnergyUsed());
        expectedBalance = expectedBalance.subtract(energyUsed.multiply(BigInteger.valueOf(energyPrice)));
    }
    assertEquals(expectedBalance, getBalance(deployerKey));
    assertEquals(expectedNonce, getNonce(deployerKey));
}
Also used : AionAddress(org.aion.types.AionAddress) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 73 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class AvmInternalTxTest method makeCall.

private void makeCall(BigInteger nonce, AionAddress contract, byte[] call) {
    AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, new byte[0], call, 2_000_000, minEnergyPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    // Check the block was imported and the transaction was successful.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.isSuccessful()).isTrue();
    System.out.println(block);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 74 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class BeaconHashValidatorTest method validateTxForPendingStateWhenBeaconHashNotOnMainchain.

@Test
public void validateTxForPendingStateWhenBeaconHashNotOnMainchain() {
    long unityForkNumber = 3;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    Block bestBlock = mock(Block.class);
    byte[] badBlockhash = ByteUtil.hexStringToBytes("0xbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbad0");
    when(blockchain.isMainChain(badBlockhash)).thenReturn(false);
    when(blockchain.getBestBlock()).thenReturn(bestBlock);
    AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
    new byte[] { 1 }, new AionAddress(// sender - any val
    ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
    ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
    new byte[] { 1 }, // data - any val
    new byte[] {}, // energyLimit - any val
    1l, // energyPrice - any val
    1l, // type - any val
    (byte) 1, // beacon hash - does not exist in blockstore
    badBlockhash);
    BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
    when(bestBlock.getNumber()).thenReturn(unityForkNumber - 2);
    assertWithMessage("any beacon hash should fail for pending state if best_block_number+1 < unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
    when(bestBlock.getNumber()).thenReturn(unityForkNumber - 1);
    assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 = unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
    when(bestBlock.getNumber()).thenReturn(unityForkNumber);
    assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 > unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 75 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnSideChain.

@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnSideChain() {
    /*
         * Visualization of this case (x is the block of beacon hash , * is the new block):
         *
         *   o--o--o---o--o     main chain
         *         |
         *          \--x--o--*  side chain
         */
    long unityForkNumber = 5;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    LinkedList<Block> sideChain = mockChain(unityForkNumber, 4, blockchain);
    byte[] beaconHash = new byte[32];
    System.arraycopy(sideChain.get(3).getHash(), 0, beaconHash, 0, 32);
    AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
    new byte[] { 1 }, new AionAddress(// sender - any val
    ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
    ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
    new byte[] { 1 }, // data - any val
    new byte[] {}, // energyLimit - any val
    1l, // energyPrice - any val
    1l, // type - any val
    (byte) 1, // beacon hash
    beaconHash);
    BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
    assertWithMessage("beacon hash on chain of new block should pass if block_number = unityFork_number").that(unit.validateTxForBlock(tx, sideChain.get(0))).isTrue();
// not doing all the "if new block > unityFork" and "new block < unityFork" cases
// for the side chain test cases because those paths already checked by the
// mainchain tests
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Aggregations

AionTransaction (org.aion.base.AionTransaction)437 Test (org.junit.Test)308 AionAddress (org.aion.types.AionAddress)273 BigInteger (java.math.BigInteger)174 MiningBlock (org.aion.zero.impl.types.MiningBlock)149 ArrayList (java.util.ArrayList)127 ImportResult (org.aion.zero.impl.core.ImportResult)115 AionTxExecSummary (org.aion.base.AionTxExecSummary)103 Block (org.aion.zero.impl.types.Block)102 RepositoryCache (org.aion.base.db.RepositoryCache)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)87 AionTxReceipt (org.aion.base.AionTxReceipt)75 ECKey (org.aion.crypto.ECKey)52 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)46 BlockContext (org.aion.zero.impl.types.BlockContext)43 PooledTransaction (org.aion.base.PooledTransaction)40 AccountState (org.aion.base.AccountState)39 Properties (java.util.Properties)35 HashMap (java.util.HashMap)33 DataWord (org.aion.util.types.DataWord)29