Search in sources :

Example 31 with BlockContext

use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.

the class TransactionExecutorTest method testExecutorBlind.

@Test
public void testExecutorBlind() throws IOException {
    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);
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(context.block);
    AionBlockSummary summary = result.getRight();
    assertEquals(ImportResult.IMPORTED_BEST, result.getLeft());
    // 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");
    AionAddress contract = TxUtil.calculateContractAddress(tx);
    assertArrayEquals(body, blockchain.getRepository().getCode(contract));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getBalance(contract));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(contract));
    assertEquals(BigInteger.ONE, blockchain.getRepository().getNonce(deployer));
    assertEquals(Builder.DEFAULT_BALANCE.subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 32 with BlockContext

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

use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.

the class TransactionExecutorTest method deployByteArrayContract.

// <-----------------------------------------HELPERS------------------------------------------->
private AionAddress deployByteArrayContract() throws IOException {
    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);
    blockchain.tryToConnect(context.block);
    return TxUtil.calculateContractAddress(tx);
}
Also used : BlockContext(org.aion.zero.impl.types.BlockContext) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction)

Example 34 with BlockContext

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

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

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