Search in sources :

Example 26 with BlockContext

use of org.aion.zero.impl.types.BlockContext 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 27 with BlockContext

use of org.aion.zero.impl.types.BlockContext 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 28 with BlockContext

use of org.aion.zero.impl.types.BlockContext 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)

Example 29 with BlockContext

use of org.aion.zero.impl.types.BlockContext 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 30 with BlockContext

use of org.aion.zero.impl.types.BlockContext 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

BlockContext (org.aion.zero.impl.types.BlockContext)49 AionTransaction (org.aion.base.AionTransaction)42 Test (org.junit.Test)42 BigInteger (java.math.BigInteger)27 AionAddress (org.aion.types.AionAddress)25 AionTxExecSummary (org.aion.base.AionTxExecSummary)19 MiningBlock (org.aion.zero.impl.types.MiningBlock)18 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)16 RepositoryCache (org.aion.base.db.RepositoryCache)15 Block (org.aion.zero.impl.types.Block)15 ArrayList (java.util.ArrayList)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 DataWord (org.aion.util.types.DataWord)11 ImportResult (org.aion.zero.impl.core.ImportResult)9 ECKey (org.aion.crypto.ECKey)8 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)8 TrieImpl (org.aion.zero.impl.trie.TrieImpl)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Log (org.aion.types.Log)5