Search in sources :

Example 71 with AionTxExecSummary

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

the class OpcodeIntegTest method testSuicideRecipientNewlyCreated.

@Test
public void testSuicideRecipientNewlyCreated() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    BigInteger balance = new BigInteger("32522224");
    AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    AionAddress contract = deployContract(repo, "Suicide", "Suicide.sol", BigInteger.ZERO);
    repo.addBalance(contract, balance);
    BigInteger balanceDeployer = repo.getBalance(deployer);
    BigInteger balanceRecipient = BigInteger.ZERO;
    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 a new account was created and
    // all of the balance in the contract has been transferred to it. We expect that the
    // contract has been deleted.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
    assertEquals(balanceDeployer.subtract(txCost), repo.getBalance(deployer));
    assertTrue(repo.hasAccountState(recipient));
    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 72 with AionTxExecSummary

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

the class OpcodeIntegTest method testCallcodeStorage.

// ======================================= test CALLCODE =======================================
@Test
public void testCallcodeStorage() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    BigInteger n = new BigInteger("7638523");
    AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
    AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
    // Deployer calls contract D which performs CALLCODE to call contract E. We expect that the
    // storage in contract D is modified by the code that is called in contract E.
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger nonce = BigInteger.TWO;
    byte[] input = // use CALLCODE on E.
    ByteUtil.merge(Hex.decode("5cce9fc2"), E.toByteArray());
    // pass in 'n' also.
    input = ByteUtil.merge(input, new DataWord(n).getData());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.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());
    System.err.println("Found in D's storage for n: " + inStore);
    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 73 with AionTxExecSummary

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

the class ContractIntegTest method testWithdrawFromFvmContract.

@Test
public void testWithdrawFromFvmContract() throws Exception {
    String contractName = "MultiFeatureContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.TWO.pow(32);
    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;
    }
    BigInteger deployerBalance = repo.getBalance(deployer);
    // Contract has 2^32 coins, let's withdraw them.
    byte[] input = ByteUtil.merge(Hex.decode("9424bba3"), new DataWord(value).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());
    // Check that the deployer did get the requested value sent back.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(deployerBalance.subtract(txCost).add(value), 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 74 with AionTxExecSummary

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

the class ContractIntegTest method testSendContractFundsToOtherAddress.

@Test
public void testSendContractFundsToOtherAddress() throws Exception {
    String contractName = "MultiFeatureContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.TWO.pow(13);
    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;
    }
    BigInteger deployerBalance = repo.getBalance(deployer);
    // Create a new account to be our fund recipient.
    AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    repo.createAccount(recipient);
    // Contract has 2^13 coins, let's withdraw them.
    byte[] input = ByteUtil.merge(Hex.decode("8c50612c"), recipient.toByteArray());
    input = ByteUtil.merge(input, new DataWord(value).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));
    // Check that the recipient received the value.
    assertEquals(value, repo.getBalance(recipient));
}
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 75 with AionTxExecSummary

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

the class ContractIntegTest method testFvmEmptyContract.

@Test
public void testFvmEmptyContract() throws Exception {
    String contractName = "EmptyContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    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(), 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 = BulkExecutor.executeTransactionWithNoPostExecutionWork(block.getDifficulty(), block.getNumber(), block.getTimestamp(), block.getNrgLimit(), block.getCoinbase(), tx, repo, false, true, false, false, LOGGER_VM, BlockCachingContext.PENDING, block.getNumber() - 1, false, false);
    if (txType == TransactionTypes.DEFAULT) {
        // "" == SUCCESS
        assertEquals("", summary.getReceipt().getError());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
        nonce = nonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
        assertEquals(226186, summary.getReceipt().getEnergyUsed());
    } else if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertEquals("Failed: invalid data", summary.getReceipt().getError());
        nonce = nonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
        assertEquals(1000000, summary.getReceipt().getEnergyUsed());
    }
}
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)

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