Search in sources :

Example 31 with DataWord

use of org.aion.util.types.DataWord in project aion by aionnetwork.

the class AionTransactionTest method testTransactionCost.

@Test
public void testTransactionCost() {
    byte[] nonce = BigInteger.ONE.toByteArray();
    byte[] to = RandomUtils.nextBytes(AionAddress.LENGTH);
    byte[] value = BigInteger.ONE.toByteArray();
    byte[] data = RandomUtils.nextBytes(128);
    long nrg = new DataWord(1000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    AionTransaction tx = AionTransaction.create(key, nonce, new AionAddress(to), value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    long expected = 21000;
    for (byte b : data) {
        expected += (b == 0) ? 4 : 64;
    }
    assertEquals(expected, TxUtil.calculateTransactionCost(tx));
}
Also used : AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 32 with DataWord

use of org.aion.util.types.DataWord 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)

Example 33 with DataWord

use of org.aion.util.types.DataWord 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 34 with DataWord

use of org.aion.util.types.DataWord 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 35 with DataWord

use of org.aion.util.types.DataWord in project aion by aionnetwork.

the class AionRepositoryImplTest method testAccountStateUpdateStorageRowFlush.

@Test
public void testAccountStateUpdateStorageRowFlush() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    RepositoryCache track = repository.startTracking();
    AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    // Consider the original root the one after an account has been added
    byte[] key = HashUtil.blake128("hello".getBytes());
    byte[] value = HashUtil.blake128("world".getBytes());
    track.addStorageRow(defaultAccount, new DataWord(key).toWrapper(), new DataWord(value).toWrapper());
    track.saveVmType(defaultAccount, InternalVmType.FVM);
    // does not call parent's flush
    track.flushTo(repository, true);
    repository.flush();
    /**
     * Verify that the account has been flushed
     */
    StoredContractDetails snapshot = repository.getContractDetails(defaultAccount);
    assertThat(snapshot instanceof FvmContractDetails).isTrue();
    FvmContractDetails details = (FvmContractDetails) snapshot;
    assertThat(details.get(new DataWord(key).toWrapper())).isEqualTo(new DataWord(value).toWrapper());
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Aggregations

DataWord (org.aion.util.types.DataWord)49 Test (org.junit.Test)37 AionAddress (org.aion.types.AionAddress)36 AionTransaction (org.aion.base.AionTransaction)29 BigInteger (java.math.BigInteger)23 RepositoryCache (org.aion.base.db.RepositoryCache)23 AionTxExecSummary (org.aion.base.AionTxExecSummary)18 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)12 BlockContext (org.aion.zero.impl.types.BlockContext)12 ECKey (org.aion.crypto.ECKey)7 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)7 Log (org.aion.types.Log)4 HashMap (java.util.HashMap)3 AionTxReceipt (org.aion.base.AionTxReceipt)3 ImportResult (org.aion.zero.impl.core.ImportResult)3 Map (java.util.Map)2 InternalTransaction (org.aion.types.InternalTransaction)2 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)2