Search in sources :

Example 66 with AionTxExecSummary

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

the class InternalTransactionTest method testRecursiveCall.

/*
    pragma solidity ^0.4.0;

    contract A {
        function f(int n) {
            if (n > 0) {
                this.f(n - 1);
            }
        }
    }
         */
@Test
public void testRecursiveCall() throws Exception {
    String contractA = "0x605060405234156100105760006000fd5b610015565b60e9806100236000396000f30060506040526000356c01000000000000000000000000900463ffffffff168063ec77996414603157602b565b60006000fd5b3415603c5760006000fd5b605060048080359060100190919050506052565b005b600081131560b9573063ec779964600184036040518263ffffffff166c01000000000000000000000000028152600401808281526010019150506000604051808303816000888881813b151560a75760006000fd5b5af1151560b45760006000fd5b505050505b5b505600a165627a7a7230582033f76d593b80b3468bfb0f873882bc00903a790a9b996cb8ca3bac51295994cd0029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // ======================
    // DEPLOY
    // ======================
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress addressA = TxUtil.calculateContractAddress(tx1);
    System.out.println("contract A = " + addressA);
    Thread.sleep(1000);
    // ======================
    // CALL
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0xec779964"), new DataWord(2).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx2), false);
    AionTxExecSummary summary = executeTransaction(bc, context, tx2);
    assertEquals(2, summary.getInternalTransactions().size());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Example 67 with AionTxExecSummary

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

the class OpcodeIntegTest method testSuicideRecipientExists.

// ======================================= test SUICIDE ========================================
@Test
public void testSuicideRecipientExists() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    BigInteger balance = new BigInteger("32522224");
    AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    repo.createAccount(recipient);
    AionAddress contract = deployContract(repo, "Suicide", "Suicide.sol", BigInteger.ZERO);
    repo.addBalance(contract, balance);
    BigInteger balanceDeployer = repo.getBalance(deployer);
    BigInteger balanceRecipient = repo.getBalance(recipient);
    assertEquals(balance, repo.getBalance(contract));
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger nonce = BigInteger.ONE;
    byte[] input = ByteUtil.merge(Hex.decode("fc68521a"), recipient.toByteArray());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
    // We expect that deployer paid the tx cost. We expect that all of the balance in the
    // contract has been transferred to recipient. We expect that the contract has been deleted.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
    assertEquals(balanceDeployer.subtract(txCost), repo.getBalance(deployer));
    assertEquals(balanceRecipient.add(balance), repo.getBalance(recipient));
    assertEquals(BigInteger.ZERO, repo.getBalance(contract));
    assertFalse(repo.hasAccountState(contract));
    assertEquals(1, summary.getDeletedAccounts().size());
    assertEquals(contract, summary.getDeletedAccounts().get(0));
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 68 with AionTxExecSummary

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

the class OpcodeIntegTest method testDelegateCallStorage.

// ===================================== test DELEGATECALL =====================================
@Test
public void testDelegateCallStorage() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
    AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
    BigInteger n = new BigInteger("23786523");
    // Deployer calls contract D which performs DELEGATECALL to call contract E.
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger value = new BigInteger("4364463");
    BigInteger nonce = BigInteger.TWO;
    byte[] input = // use DELEGATECALL on E.
    ByteUtil.merge(Hex.decode("32817e1d"), E.toByteArray());
    // pass in 'n' also.
    input = ByteUtil.merge(input, new DataWord(n).getData());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertEquals(deployer, tx.getSenderAddress());
    assertEquals(D, tx.getDestinationAddress());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
    nonce = nonce.add(BigInteger.ONE);
    // When we call into contract D we should find its storage is modified so that 'n' is set.
    input = Hex.decode("3e955225");
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertEquals(deployer, tx.getSenderAddress());
    assertEquals(D, tx.getDestinationAddress());
    context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    BigInteger inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
    assertEquals(n, inStore);
    nonce = nonce.add(BigInteger.ONE);
    // When we call into contract E we should find its storage is unmodified.
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), E, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertEquals(deployer, tx.getSenderAddress());
    assertEquals(E, tx.getDestinationAddress());
    context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
    assertEquals(BigInteger.ZERO, inStore);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 69 with AionTxExecSummary

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

the class OpcodeIntegTest method testDelegateCallValueTransfer.

@Test
public void testDelegateCallValueTransfer() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
    AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
    BigInteger n = new BigInteger("23786523");
    BigInteger balanceDeployer = repo.getBalance(deployer);
    BigInteger balanceD = repo.getBalance(D);
    BigInteger balanceE = repo.getBalance(E);
    // Deployer calls contract D which performs DELEGATECALL to call contract E.
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger value = new BigInteger("4364463");
    BigInteger nonce = BigInteger.TWO;
    byte[] input = // use DELEGATECALL on E.
    ByteUtil.merge(Hex.decode("32817e1d"), E.toByteArray());
    // pass in 'n' also.
    input = ByteUtil.merge(input, new DataWord(n).getData());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertEquals(deployer, tx.getSenderAddress());
    assertEquals(D, tx.getDestinationAddress());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
    // We expect that deployer paid the tx cost and sent value. We expect that D received value.
    // We expect that E received nothing.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
    assertEquals(balanceDeployer.subtract(value).subtract(txCost), repo.getBalance(deployer));
    assertEquals(balanceD.add(value), repo.getBalance(D));
    assertEquals(balanceE, repo.getBalance(E));
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 70 with AionTxExecSummary

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

the class OpcodeIntegTest method deployContract.

/**
 * Deploys a contract named contractName in a file named contractFilename and checks the state
 * of the deployed contract and the contract deployer.
 *
 * <p>Returns the address of the newly deployed contract.
 */
private AionAddress deployContract(RepositoryCache repo, AionTransaction tx, String contractName, String contractFilename, BigInteger value, long nrg, long nrgPrice, BigInteger expectedNonce) throws Exception {
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(expectedNonce, repo.getNonce(deployer));
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    AionAddress contract = TxUtil.calculateContractAddress(tx);
    checkStateOfNewContract(repo, contractName, contractFilename, contract, value);
    checkStateOfDeployer(repo, deployerBalance, summary.getNrgUsed().longValue(), nrgPrice, value, expectedNonce);
    return contract;
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary)

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