Search in sources :

Example 86 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class TransactionExecutorTest method testExecutor.

@Test
public void testExecutor() throws Exception {
    byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    BigInteger refund = summary.getRefund();
    // We expect that there is a new account created, the contract, with 0 balance and 0 nonce
    // and that its code is the contract body. We also expect that the deployer (sender) has
    // its nonce incremented and its balance is now equal to its old balance minus the
    // transaction
    // fee plus the refund
    byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
    assertEquals("", summary.getReceipt().getError());
    assertArrayEquals(body, summary.getResult());
    AionAddress contract = TxUtil.calculateContractAddress(summary.getTransaction());
    assertArrayEquals(body, repo.getCode(contract));
    assertEquals(BigInteger.ZERO, repo.getBalance(contract));
    assertEquals(BigInteger.ZERO, repo.getNonce(contract));
    BigInteger txFee = BigInteger.valueOf(nrg).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(Builder.DEFAULT_BALANCE.subtract(txFee).add(refund), repo.getBalance(deployer));
    assertEquals(BigInteger.ONE, repo.getNonce(deployer));
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 87 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class TransactionExecutorTest method testDeployedCodeFunctionality.

@Test
public void testDeployedCodeFunctionality() throws Exception {
    AionAddress contract = deployByteArrayContract();
    byte[] callingCode = Hex.decode(f_func);
    BigInteger nonce = blockchain.getRepository().getNonce(deployer);
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    assertEquals("", summary.getReceipt().getError());
    System.out.println(Hex.toHexString(summary.getResult()));
    // We called the function f() which returns nothing.
    assertEquals(0, summary.getReceipt().getTransactionOutput().length);
    byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
    assertArrayEquals(body, blockchain.getRepository().getCode(contract));
    // Now we call the g() function, which returns a byte array of 1024 bytes that starts with
    // 'a' and ends with 'b'
    callingCode = Hex.decode(g_func);
    nonce = repo.getNonce(deployer);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    summary = executeTransaction(repo, context, tx);
    System.out.println(Hex.toHexString(summary.getResult()));
    System.out.println(summary.getResult().length);
    // // I'm guessing: first data word is the number of bytes that follows. Then those
    // following
    // // bytes denote the size of the output, which follows these last bytes.
    // int len = new DataWordImpl(Arrays.copyOfRange(output, 0,
    // DataWordImpl.BYTES)).intValue();
    // byte[] outputLen = new byte[len];
    // System.arraycopy(output, DataWordImpl.BYTES, outputLen, 0, len);
    // int outputSize = new BigInteger(outputLen).intValue();
    // 
    // byte[] expected = new byte[1024];
    // expected[0] = 'a';
    // expected[1023] = 'b';
    // 
    // byte[] out = new byte[outputSize];
    // System.arraycopy(output, DataWordImpl.BYTES + len, out, 0, outputSize);
    byte[] expected = new byte[1024];
    expected[0] = 'a';
    expected[1023] = 'b';
    byte[] out = extractActualOutput(summary.getResult());
    assertArrayEquals(expected, out);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 88 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class TransactionExecutorTest method testGfunction.

@Test
public void testGfunction() throws Exception {
    AionAddress contract = deployByteArrayContract();
    byte[] callingCode = Hex.decode(g_func);
    BigInteger nonce = blockchain.getRepository().getNonce(deployer);
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(repo, context, tx);
    System.out.println(summary.getReceipt());
    // System.out.println(Hex.toHexString(res.getOutput()));
    // System.out.println(res.getOutput().length);
    byte[] out = extractActualOutput(summary.getResult());
    assertEquals(0, out.length);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 89 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class AvmProviderTest method testBalanceTransferTransactionVersion1.

@Test
public void testBalanceTransferTransactionVersion1() throws Exception {
    Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
    AvmProvider.enableAvmVersion(AvmVersion.VERSION_1, this.projectRootDir);
    AvmProvider.startAvm(AvmVersion.VERSION_1);
    // Set up the repo and give the sender account some balance.
    RepositoryCache repository = newRepository();
    AionAddress sender = randomAddress();
    AionAddress recipient = randomAddress();
    addBalance(repository, sender, BigInteger.valueOf(1_000_000));
    // Run the transaction.
    RepositoryCache repositoryChild = repository.startTracking();
    IAvmExternalState externalState = newExternalState(AvmVersion.VERSION_1, repositoryChild, newEnergyRules());
    Transaction transaction = newBalanceTransferTransaction(sender, recipient, BigInteger.TEN);
    IAionVirtualMachine avm = AvmProvider.getAvm(AvmVersion.VERSION_1);
    IAvmFutureResult[] futures = avm.run(externalState, new Transaction[] { transaction }, AvmExecutionType.MINING, 0);
    // Assert the result and state changes we expect.
    Assert.assertEquals(1, futures.length);
    TransactionResult result = futures[0].getResult();
    Assert.assertTrue(result.transactionStatus.isSuccess());
    Assert.assertEquals(BigInteger.TEN, repositoryChild.getBalance(recipient));
    AvmProvider.shutdownAvm(AvmVersion.VERSION_1);
    AvmProvider.disableAvmVersion(AvmVersion.VERSION_1);
    AvmProvider.releaseLock();
}
Also used : IAvmExternalState(org.aion.avm.stub.IAvmExternalState) TransactionResult(org.aion.types.TransactionResult) AionAddress(org.aion.types.AionAddress) Transaction(org.aion.types.Transaction) IAvmFutureResult(org.aion.avm.stub.IAvmFutureResult) RepositoryCache(org.aion.base.db.RepositoryCache) IAionVirtualMachine(org.aion.avm.stub.IAionVirtualMachine) Test(org.junit.Test)

Example 90 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ContractIntegTest method testFvmOverWithdrawFromContract.

@Test
public void testFvmOverWithdrawFromContract() throws Exception {
    String contractName = "MultiFeatureContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    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;
    }
    BigInteger deployerBalance = repo.getBalance(deployer);
    repo.flushTo(blockchain.getRepository(), true);
    repo = blockchain.getRepository().startTracking();
    // Contract has no funds, try to withdraw just 1 coin.
    byte[] input = ByteUtil.merge(Hex.decode("9424bba3"), new DataWord(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("reverted", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    System.out.println("DEP: " + deployerBalance);
    BigInteger 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)

Aggregations

RepositoryCache (org.aion.base.db.RepositoryCache)120 Test (org.junit.Test)102 AionAddress (org.aion.types.AionAddress)90 AionTransaction (org.aion.base.AionTransaction)89 AionTxExecSummary (org.aion.base.AionTxExecSummary)72 BigInteger (java.math.BigInteger)43 MiningBlock (org.aion.zero.impl.types.MiningBlock)41 AccountState (org.aion.base.AccountState)38 DataWord (org.aion.util.types.DataWord)23 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)23 ImportResult (org.aion.zero.impl.core.ImportResult)22 InternalTransaction (org.aion.types.InternalTransaction)18 Block (org.aion.zero.impl.types.Block)18 AionTxReceipt (org.aion.base.AionTxReceipt)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 BlockContext (org.aion.zero.impl.types.BlockContext)15 SolidityType (org.aion.solidity.SolidityType)10 ArrayList (java.util.ArrayList)8 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)6