Search in sources :

Example 16 with ImportResult

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

the class ContractIntegTest method tellFvmContractCallWithinDeployingBlock.

@Test
public void tellFvmContractCallWithinDeployingBlock() throws IOException {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(deployerNonce, repo.getNonce(deployer));
    List<AionTransaction> ls = new ArrayList<>();
    ls.add(tx);
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
    AionTransaction tx2 = AionTransaction.create(deployerKey, nonce.toByteArray(), TxUtil.calculateContractAddress(tx), BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx2.isContractCreationTransaction());
    ls.add(tx2);
    BigInteger senderBalance = repo.getBalance(deployer);
    Block parent = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parent, ls, false, parent.getTimestamp());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    AionBlockSummary summary = result.getRight();
    assertTrue(result.getLeft().isSuccessful());
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contractAddress));
    assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))).subtract(BigInteger.valueOf(summary.getReceipts().get(1).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
    repo = blockchain.getRepository().startTracking();
    assertEquals(BigInteger.TWO, repo.getNonce(deployer));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 17 with ImportResult

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

the class ContractIntegTest method tellFvmContractCallAvmContract.

@Test
public void tellFvmContractCallAvmContract() throws Exception {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce, true);
    assertNotNull(contract);
    repo = blockchain.getRepository().startTracking();
    AionAddress avmAddress = deployAvmContract(AvmVersion.VERSION_1, nonce);
    assertNotNull(avmAddress);
    nonce = nonce.add(BigInteger.ONE);
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("callAVM(address)".getBytes()), 0, 4);
    input = ByteUtil.merge(input, avmAddress.toByteArray());
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    // The evmjit only return the the transaction success or failed when performing the function
    // call.
    assertEquals("reverted", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertTrue(result.getRight().getSummaries().get(0).isFailed());
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) 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 18 with ImportResult

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

the class AlternatingVmBlockTest method getAvmContractDeploymentCost.

private long getAvmContractDeploymentCost(AvmVersion version, BigInteger nonce) {
    AionTransaction avmDeploy = makeAvmContractCreateTransaction(version, deployerKey, nonce);
    Block parentBlock = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parentBlock, Collections.singletonList(avmDeploy), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    return connectResult.getRight().getReceipts().get(0).getEnergyUsed();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 19 with ImportResult

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

the class AlternatingVmBlockTest method testOverflowBlockWithAlternatingVms2.

/**
 * Tests a special case of the alternating transactions: the first 14 don't overflow the limit,
 * the 15th does overflow it, but the 16th can fit into it.
 *
 * <p>The problem: the nonce no longer makes any sense because the 15th transaction is kicked
 * out.
 */
@Test
public void testOverflowBlockWithAlternatingVms2() throws Exception {
    List<AionTransaction> alternatingTransactions = makeAlternatingAvmFvmContractCreateTransactions(AvmVersion.VERSION_1, 16, BigInteger.ZERO);
    Block parentBlock = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parentBlock, alternatingTransactions, false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    // A correct block is produced but it does not contain all of the transactions. The second
    // last transaction is rejected
    // because it would cause the block energy limit to be exceeded, and the last transaction
    // now has an invalid nonce since
    // the previous transaction was rejected, so neither of these are included in the block.
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    assertEquals(14, connectResult.getRight().getReceipts().size());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 20 with ImportResult

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

the class AvmHelloWorldTest method testDeployAndCallContractInTheSameBlock.

@Test
public void testDeployAndCallContractInTheSameBlock() {
    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);
    List<AionTransaction> ls = new ArrayList<>();
    ls.add(transaction);
    byte[] call = getCallArguments(AvmVersion.VERSION_1);
    AionTransaction transaction2 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), TxUtil.calculateContractAddress(transaction), new byte[0], call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    ls.add(transaction2);
    MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), ls, false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(connectResult.getRight().getReceipts().size() == 2);
    // Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    // Check the call success
    receipt = connectResult.getRight().getReceipts().get(1);
    assertThat(receipt.isSuccessful()).isTrue();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) 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