Search in sources :

Example 81 with AionTxExecSummary

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

the class ContractIntegTest method testTransferValueToPayableConstructorInsufficientFunds.

@Test
public void testTransferValueToPayableConstructorInsufficientFunds() throws Exception {
    String contractName = "PayableConstructor";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    // send too much value.
    BigInteger value = Builder.DEFAULT_BALANCE.add(BigInteger.ONE);
    BigInteger nonce = BigInteger.ZERO;
    // to == null  signals that this is contract creation.
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    MiningBlock block = makeBlock(tx);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    if (txType == TransactionTypes.DEFAULT) {
        assertEquals("INSUFFICIENT_BALANCE", summary.getReceipt().getError());
        assertEquals(0, summary.getReceipt().getEnergyUsed());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.INSUFFICIENT_BALANCE, BigInteger.ZERO);
        checkStateOfDeployerOnBadDeploy(repo);
    } else if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertEquals("Rejected: insufficient balance", summary.getReceipt().getError());
        assertEquals(tx.getEnergyLimit(), summary.getReceipt().getEnergyUsed());
        checkStateOfDeployerOnBadDeploy(repo);
    }
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 82 with AionTxExecSummary

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

the class FvmBulkTransactionTest method sendContractCreationAndCallTransactionsInBulkTest.

@Test
public void sendContractCreationAndCallTransactionsInBulkTest() throws IOException {
    BigInteger expectedDeployerNonce = getNonce(this.deployerKey);
    // First, deploy a contract that we can call into.
    AionBlockSummary initialSummary = sendTransactionsInBulkInSingleBlock(Collections.singletonList(makeFvmContractCreateTransaction(this.deployerKey, expectedDeployerNonce)));
    expectedDeployerNonce = expectedDeployerNonce.add(BigInteger.ONE);
    // Grab the address of the newly deployed contract.
    AionTransaction tx = initialSummary.getReceipts().get(0).getTransaction();
    AionAddress deployedContract = TxUtil.calculateContractAddress(tx);
    int numFvmCreateTransactions = 10;
    int numFvmCallTransactions = 10;
    int numTransactions = numFvmCreateTransactions + numFvmCallTransactions;
    // Grab the initial data we need to track.
    BigInteger initialBalanceDeployer = getBalance(this.deployerKey);
    // Make the create transactions.
    List<AionTransaction> transactions = new ArrayList<>();
    for (int i = 0; i < numFvmCreateTransactions; i++) {
        transactions.add(makeFvmContractCreateTransaction(this.deployerKey, expectedDeployerNonce));
        expectedDeployerNonce = expectedDeployerNonce.add(BigInteger.ONE);
    }
    // Make the call transactions.
    for (int i = 0; i < numFvmCallTransactions; i++) {
        transactions.add(makeFvmContractCallTransaction(this.deployerKey, expectedDeployerNonce, deployedContract));
        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());
    }
    List<AionAddress> contracts = new ArrayList<>();
    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)));
        // The first batch are creates, so grab the new contract addresses.
        if (i < numFvmCreateTransactions) {
            AionTransaction transaction = blockSummary.getSummaries().get(i).getReceipt().getTransaction();
            contracts.add(TxUtil.calculateContractAddress(transaction));
        }
    }
    // Verify account states after the transactions have been processed.
    for (int i = 0; i < numFvmCreateTransactions; i++) {
        // Check that these contracts have code.
        assertTrue(this.blockchain.getRepository().getCode(contracts.get(i)).length > 0);
        assertEquals(BigInteger.ZERO, getBalance(contracts.get(i)));
        assertEquals(BigInteger.ZERO, getNonce(contracts.get(i)));
    }
    assertEquals(expectedDeployerBalance, getBalance(this.deployerKey));
    assertEquals(expectedDeployerNonce, getNonce(this.deployerKey));
    // Call into the contract to get its current 'count' to verify its state is correct.
    int count = getDeployedTickerCountValue(this.deployerKey, expectedDeployerNonce, deployedContract);
    assertEquals(numFvmCallTransactions, count);
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 83 with AionTxExecSummary

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

the class TransactionExecutorTest method testExecutor.

@Test
public void testExecutor() throws Exception {
    byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    BigInteger refund = summary.getRefund();
    // We expect that there is a new account created, the contract, with 0 balance and 0 nonce
    // and that its code is the contract body. We also expect that the deployer (sender) has
    // its nonce incremented and its balance is now equal to its old balance minus the
    // transaction
    // fee plus the refund
    byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
    assertEquals("", summary.getReceipt().getError());
    assertArrayEquals(body, summary.getResult());
    AionAddress contract = TxUtil.calculateContractAddress(summary.getTransaction());
    assertArrayEquals(body, repo.getCode(contract));
    assertEquals(BigInteger.ZERO, repo.getBalance(contract));
    assertEquals(BigInteger.ZERO, repo.getNonce(contract));
    BigInteger txFee = BigInteger.valueOf(nrg).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(Builder.DEFAULT_BALANCE.subtract(txFee).add(refund), repo.getBalance(deployer));
    assertEquals(BigInteger.ONE, repo.getNonce(deployer));
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 84 with AionTxExecSummary

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

the class TransactionExecutorTest method testDeployedCodeFunctionality.

@Test
public void testDeployedCodeFunctionality() throws Exception {
    AionAddress contract = deployByteArrayContract();
    byte[] callingCode = Hex.decode(f_func);
    BigInteger nonce = blockchain.getRepository().getNonce(deployer);
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    assertEquals("", summary.getReceipt().getError());
    System.out.println(Hex.toHexString(summary.getResult()));
    // We called the function f() which returns nothing.
    assertEquals(0, summary.getReceipt().getTransactionOutput().length);
    byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
    assertArrayEquals(body, blockchain.getRepository().getCode(contract));
    // Now we call the g() function, which returns a byte array of 1024 bytes that starts with
    // 'a' and ends with 'b'
    callingCode = Hex.decode(g_func);
    nonce = repo.getNonce(deployer);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    summary = executeTransaction(repo, context, tx);
    System.out.println(Hex.toHexString(summary.getResult()));
    System.out.println(summary.getResult().length);
    // // I'm guessing: first data word is the number of bytes that follows. Then those
    // following
    // // bytes denote the size of the output, which follows these last bytes.
    // int len = new DataWordImpl(Arrays.copyOfRange(output, 0,
    // DataWordImpl.BYTES)).intValue();
    // byte[] outputLen = new byte[len];
    // System.arraycopy(output, DataWordImpl.BYTES, outputLen, 0, len);
    // int outputSize = new BigInteger(outputLen).intValue();
    // 
    // byte[] expected = new byte[1024];
    // expected[0] = 'a';
    // expected[1023] = 'b';
    // 
    // byte[] out = new byte[outputSize];
    // System.arraycopy(output, DataWordImpl.BYTES + len, out, 0, outputSize);
    byte[] expected = new byte[1024];
    expected[0] = 'a';
    expected[1023] = 'b';
    byte[] out = extractActualOutput(summary.getResult());
    assertArrayEquals(expected, out);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 85 with AionTxExecSummary

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

the class TransactionExecutorTest method testGfunction.

@Test
public void testGfunction() throws Exception {
    AionAddress contract = deployByteArrayContract();
    byte[] callingCode = Hex.decode(g_func);
    BigInteger nonce = blockchain.getRepository().getNonce(deployer);
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    System.out.println(summary.getReceipt());
    // System.out.println(Hex.toHexString(res.getOutput()));
    // System.out.println(res.getOutput().length);
    byte[] out = extractActualOutput(summary.getResult());
    assertEquals(0, out.length);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Aggregations

AionTxExecSummary (org.aion.base.AionTxExecSummary)121 AionTransaction (org.aion.base.AionTransaction)103 AionAddress (org.aion.types.AionAddress)98 Test (org.junit.Test)97 RepositoryCache (org.aion.base.db.RepositoryCache)72 BigInteger (java.math.BigInteger)66 AccountState (org.aion.base.AccountState)37 ImportResult (org.aion.zero.impl.core.ImportResult)31 MiningBlock (org.aion.zero.impl.types.MiningBlock)26 ArrayList (java.util.ArrayList)22 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)22 InternalTransaction (org.aion.types.InternalTransaction)20 Block (org.aion.zero.impl.types.Block)20 BlockContext (org.aion.zero.impl.types.BlockContext)19 DataWord (org.aion.util.types.DataWord)18 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)18 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)18 AionTxReceipt (org.aion.base.AionTxReceipt)17 Bloom (org.aion.base.Bloom)5 ECKey (org.aion.crypto.ECKey)5