Search in sources :

Example 71 with RepositoryCache

use of org.aion.base.db.RepositoryCache 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 72 with RepositoryCache

use of org.aion.base.db.RepositoryCache 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 73 with RepositoryCache

use of org.aion.base.db.RepositoryCache 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 74 with RepositoryCache

use of org.aion.base.db.RepositoryCache 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 75 with RepositoryCache

use of org.aion.base.db.RepositoryCache 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)

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