Search in sources :

Example 46 with AionTransaction

use of org.aion.base.AionTransaction 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 47 with AionTransaction

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

the class ContractIntegTest method testDeployWithOutCode.

@Test
public void testDeployWithOutCode() throws Exception {
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    // attempt to transfer value to new contract.
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    // to == null  signals that this is contract creation.
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    if (txType == TransactionTypes.DEFAULT) {
        MiningBlock block = makeBlock(tx);
        RepositoryCache repo = blockchain.getRepository().startTracking();
        AionTxExecSummary summary = executeTransaction(tx, block, repo);
        assertEquals("", summary.getReceipt().getError());
        // all energy is not used up.
        assertNotEquals(nrg, summary.getNrgUsed().longValue());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, nonce.add(BigInteger.ONE));
        byte[] code = repo.getCode(contract);
        assertNotNull(code);
    } else {
        blockchain.forkUtility.enable040Fork(0);
        MiningBlock block = makeBlock(tx);
        RepositoryCache repo = blockchain.getRepository().startTracking();
        AionTxExecSummary summary = executeTransaction(tx, block, repo);
        assertEquals("Failed: invalid data", summary.getReceipt().getError());
    }
}
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 48 with AionTransaction

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

the class ContractIntegTest method testRecursiveStackoverflow.

@Test
public void testRecursiveStackoverflow() throws Exception {
    String contractName = "Recursive";
    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);
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertNull(contract);
        return;
    }
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    // First recurse 1 time less than the max and verify this is ok.
    // Note that 128 == FvmConstants.MAX_CALL_DEPTH
    int numRecurses = 127;
    byte[] input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
    input = ByteUtil.merge(input, new DataWord(numRecurses + 1).getData());
    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);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    repo.flushTo(blockchain.getRepository(), true);
    repo = blockchain.getRepository().startTracking();
    // Now recurse the max amount of times and ensure we fail.
    // Note that 128 == FvmConstants.MAX_CALL_DEPTH
    numRecurses = 128;
    input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
    input = ByteUtil.merge(input, new DataWord(numRecurses + 1).getData());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("reverted", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
}
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) DataWord(org.aion.util.types.DataWord) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 49 with AionTransaction

use of org.aion.base.AionTransaction 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 50 with AionTransaction

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

the class AlternatingVmBlockTest method makeAlternatingAvmFvmContractCreateTransactions.

private List<AionTransaction> makeAlternatingAvmFvmContractCreateTransactions(AvmVersion version, int totalNum, BigInteger initialNonce) throws IOException {
    List<AionTransaction> transactions = new ArrayList<>();
    BigInteger currentNonce = initialNonce;
    for (int i = 0; i < totalNum; i++) {
        if (i % 2 == 0) {
            transactions.add(makeAvmContractCreateTransaction(version, deployerKey, currentNonce));
        } else {
            transactions.add(makeFvmContractCreateTransaction(deployerKey, currentNonce));
        }
        currentNonce = currentNonce.add(BigInteger.ONE);
    }
    return transactions;
}
Also used : ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction)

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