Search in sources :

Example 61 with AionTxReceipt

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

the class AvmHelloWorldTest method testDeployAndCallContract.

@Test
public void testDeployAndCallContract() {
    TransactionTypeRule.allowAVMContractTransaction();
    // Deploy the contract.
    byte[] jar = getJarBytes(AvmVersion.VERSION_1);
    AionTransaction transaction = AionTransaction.create(deployerKey, new byte[0], null, new byte[0], jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, 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, the contract has the Avm prefix, and deployment succeeded.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    AionAddress contract = new AionAddress(receipt.getTransactionOutput());
    // verify that the output is indeed the contract address
    assertThat(TxUtil.calculateContractAddress(transaction)).isEqualTo(contract);
    byte[] call = getCallArguments(AvmVersion.VERSION_1);
    transaction = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), contract, new byte[0], call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false);
    connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    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();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 62 with AionTxReceipt

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

the class AvmHelloWorldTest method testDeployContract.

@Test
public void testDeployContract() {
    TransactionTypeRule.allowAVMContractTransaction();
    byte[] jar = getJarBytes(AvmVersion.VERSION_1);
    AionTransaction transaction = AionTransaction.create(deployerKey, new byte[0], null, new byte[0], jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, 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, the contract has the Avm prefix, and deployment succeeded.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    // verify that the output is indeed the contract address
    AionAddress contractAddress = TxUtil.calculateContractAddress(transaction);
    assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 63 with AionTxReceipt

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

the class AvmLogAndInternalTransactionTest method deployContract.

public AionAddress deployContract(AvmVersion version, BigInteger nonce) {
    TransactionTypeRule.allowAVMContractTransaction();
    byte[] jar = getJarBytes(version);
    AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), null, new byte[0], jar, 5_000_000, minEnergyPrice, TransactionTypes.AVM_CREATE_CODE, null);
    MiningBlock block = this.blockchain.createBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false, this.blockchain.getBestBlock().getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    // Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.isSuccessful()).isTrue();
    return new AionAddress(receipt.getTransactionOutput());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 64 with AionTxReceipt

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

the class StatefulnessTest method testStateOfActorsAfterDeployment.

@Test
public void testStateOfActorsAfterDeployment() {
    BigInteger deployerBalance = getBalance(this.deployer);
    BigInteger deployerNonce = getNonce(this.deployer);
    AionTxReceipt receipt = deployContract(AvmVersion.VERSION_1);
    // Check the contract has the Avm prefix, and deployment succeeded, and grab the address.
    BigInteger contractBalance = BigInteger.ZERO;
    BigInteger contractNonce = BigInteger.ZERO;
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertEquals(AddressSpecs.A0_IDENTIFIER, receipt.getTransactionOutput()[0]);
        AionAddress contract = new AionAddress(receipt.getTransactionOutput());
        contractBalance = getBalance(contract);
        contractNonce = this.blockchain.getRepository().getNonce(contract);
    }
    assertTrue(receipt.isSuccessful());
    BigInteger deployerBalanceAfterDeployment = getBalance(this.deployer);
    BigInteger deployerNonceAfterDeployment = getNonce(this.deployer);
    BigInteger deploymentEnergyCost = BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(this.energyPrice));
    // Check that balances and nonce are in agreement after the deployment.
    assertEquals(deployerBalance.subtract(deploymentEnergyCost), deployerBalanceAfterDeployment);
    assertEquals(deployerNonce.add(BigInteger.ONE), deployerNonceAfterDeployment);
    assertEquals(BigInteger.ZERO, contractBalance);
    assertEquals(BigInteger.ZERO, contractNonce);
}
Also used : AionAddress(org.aion.types.AionAddress) BigInteger(java.math.BigInteger) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 65 with AionTxReceipt

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

the class AvmBulkTransactionTest method importBlockWithContractAndCallsForFvmOnTopOfAddressWithBalanceAfterFork040.

@Test
public void importBlockWithContractAndCallsForFvmOnTopOfAddressWithBalanceAfterFork040() {
    // Enable Fork040 to be able to deploy the contract on FVM.
    blockchain.forkUtility.enable040Fork(1L);
    BigInteger initialNonce = getNonce(deployerKey);
    // One transaction will be made to add balance to the expected contract address before the contract deployment.
    BigInteger expectedNonce = initialNonce.add(BigInteger.ONE);
    BigInteger initialBalance = getBalance(deployerKey);
    List<AionTransaction> transactions = new ArrayList<>();
    // Deploy FVM contract.
    String contractCode = "0x605060405234156100105760006000fd5b5b600a600060005081909090555060006000505460016000506000600060005054815260100190815260100160002090506000508190909055506064600260005060000160005081909090555060c8600260005060010160005081909090555060026000506001016000505460016000506000600260005060000160005054815260100190815260100160002090506000508190909055505b6100ae565b610184806100bd6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680631677b0ff14610049578063209652551461007657806362eb702a146100a057610043565b60006000fd5b34156100555760006000fd5b61007460048080359060100190919080359060100190919050506100c4565b005b34156100825760006000fd5b61008a610111565b6040518082815260100191505060405180910390f35b34156100ac5760006000fd5b6100c26004808035906010019091905050610123565b005b8160026000506000016000508190909055508060026000506001016000508190909055508082016001600050600084815260100190815260100160002090506000508190909055505b5050565b60006000600050549050610120565b90565b806000600050819090905550600181016001600050600083815260100190815260100160002090506000508190909055505b505600a165627a7a723058205b6e690d70d3703337452467437dc7c4e863ee4ad34b24cc516e2afa71e334700029";
    AionTransaction deployTxFVM = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), ByteUtil.hexStringToBytes(contractCode), 5_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionAddress fvmContract = TxUtil.calculateContractAddress(deployTxFVM);
    transactions.add(deployTxFVM);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Call FVM contract.
    AionTransaction contractCallTx = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), fvmContract, BigInteger.ZERO.toByteArray(), Hex.decode("62eb702a00000000000000000000000000000006"), 2_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    transactions.add(contractCallTx);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // First send balance to the future contract.
    AionTransaction balanceTransferToFVM = AionTransaction.create(deployerKey, initialNonce.toByteArray(), fvmContract, BigInteger.TEN.toByteArray(), new byte[0], 2_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(List.of(balanceTransferToFVM));
    // Verify that the transaction was successful.
    assertThat(blockSummary.getSummaries().size()).isEqualTo(1);
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertThat(receipt.isSuccessful()).isTrue();
    BigInteger expectedBalance = initialBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(energyPrice)));
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(fvmContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the 2 transactions in a single block.
    blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // Verify that all transactions were successful.
    assertThat(blockSummary.getSummaries().size()).isEqualTo(2);
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        receipt = transactionSummary.getReceipt();
        assertThat(receipt.isSuccessful()).isTrue();
        // Compute the expected balance.
        expectedBalance = expectedBalance.subtract(BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(energyPrice)));
    }
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(fvmContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
    assertThat(getBalance(deployerKey)).isEqualTo(expectedBalance);
    assertThat(getNonce(deployerKey)).isEqualTo(expectedNonce);
}
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) AionTxReceipt(org.aion.base.AionTxReceipt) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Aggregations

AionTxReceipt (org.aion.base.AionTxReceipt)111 Test (org.junit.Test)76 AionTransaction (org.aion.base.AionTransaction)74 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)61 AionAddress (org.aion.types.AionAddress)56 BigInteger (java.math.BigInteger)53 ImportResult (org.aion.zero.impl.core.ImportResult)50 MiningBlock (org.aion.zero.impl.types.MiningBlock)43 ArrayList (java.util.ArrayList)24 Block (org.aion.zero.impl.types.Block)24 AionTxExecSummary (org.aion.base.AionTxExecSummary)17 RepositoryCache (org.aion.base.db.RepositoryCache)17 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)15 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 ECKey (org.aion.crypto.ECKey)12 SolidityType (org.aion.solidity.SolidityType)10 AccountState (org.aion.base.AccountState)9 Log (org.aion.types.Log)9 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)7 StakingBlock (org.aion.zero.impl.types.StakingBlock)7