Search in sources :

Example 26 with MiningBlock

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

the class ContractIntegTest method testRecursiveStackoverflow.

@Test
public void testRecursiveStackoverflow() throws Exception {
    String contractName = "Recursive";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    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;
    }
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    // First recurse 1 time less than the max and verify this is ok.
    // Note that 128 == FvmConstants.MAX_CALL_DEPTH
    int numRecurses = 127;
    byte[] input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
    input = ByteUtil.merge(input, new DataWord(numRecurses + 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("", 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));
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    repo.flushTo(blockchain.getRepository(), true);
    repo = blockchain.getRepository().startTracking();
    // Now recurse the max amount of times and ensure we fail.
    // Note that 128 == FvmConstants.MAX_CALL_DEPTH
    numRecurses = 128;
    input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
    input = ByteUtil.merge(input, new DataWord(numRecurses + 1).getData());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("reverted", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    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 27 with MiningBlock

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

the class AlternatingVmBlockTest method getAvmContractDeploymentCost.

private long getAvmContractDeploymentCost(AvmVersion version, BigInteger nonce) {
    AionTransaction avmDeploy = makeAvmContractCreateTransaction(version, deployerKey, nonce);
    Block parentBlock = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parentBlock, Collections.singletonList(avmDeploy), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    return connectResult.getRight().getReceipts().get(0).getEnergyUsed();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 28 with MiningBlock

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

the class AlternatingVmBlockTest method testOverflowBlockWithAlternatingVms2.

/**
 * Tests a special case of the alternating transactions: the first 14 don't overflow the limit,
 * the 15th does overflow it, but the 16th can fit into it.
 *
 * <p>The problem: the nonce no longer makes any sense because the 15th transaction is kicked
 * out.
 */
@Test
public void testOverflowBlockWithAlternatingVms2() throws Exception {
    List<AionTransaction> alternatingTransactions = makeAlternatingAvmFvmContractCreateTransactions(AvmVersion.VERSION_1, 16, BigInteger.ZERO);
    Block parentBlock = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parentBlock, alternatingTransactions, false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    // A correct block is produced but it does not contain all of the transactions. The second
    // last transaction is rejected
    // because it would cause the block energy limit to be exceeded, and the last transaction
    // now has an invalid nonce since
    // the previous transaction was rejected, so neither of these are included in the block.
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    assertEquals(14, connectResult.getRight().getReceipts().size());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 29 with MiningBlock

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

the class AvmHelloWorldTest method testDeployAndCallContractInTheSameBlock.

@Test
public void testDeployAndCallContractInTheSameBlock() {
    TransactionTypeRule.allowAVMContractTransaction();
    // Deploy the contract.
    byte[] jar = getJarBytes(AvmVersion.VERSION_1);
    AionTransaction transaction = AionTransaction.create(deployerKey, new byte[0], null, new byte[0], jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
    List<AionTransaction> ls = new ArrayList<>();
    ls.add(transaction);
    byte[] call = getCallArguments(AvmVersion.VERSION_1);
    AionTransaction transaction2 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), TxUtil.calculateContractAddress(transaction), new byte[0], call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    ls.add(transaction2);
    MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), ls, false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(connectResult.getRight().getReceipts().size() == 2);
    // Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    // Check the call success
    receipt = connectResult.getRight().getReceipts().get(1);
    assertThat(receipt.isSuccessful()).isTrue();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 30 with MiningBlock

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

the class SolidityTypeTest method testStaticArray2.

@Test
public void testStaticArray2() throws Exception {
    List<byte[]> x = new ArrayList<>();
    x.add(Hex.decode("1122334455667788112233445566778811223344"));
    x.add(Hex.decode("2122334455667788112233445566778811223344"));
    x.add(Hex.decode("3122334455667788112233445566778811223344"));
    SolidityType type = new StaticArrayType("bytes20[3]");
    byte[] params = type.encode(x);
    System.out.println(Hex.toHexString(params));
    AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("e4bef5c9"), params));
    MiningBlock block = createDummyBlock();
    RepositoryCache repo = createRepository(tx);
    AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(params, receipt.getTransactionOutput());
    Object[] decoded = (Object[]) type.decode(receipt.getTransactionOutput());
    for (Object d : decoded) {
        System.out.println(Hex.toHexString((byte[]) d));
    }
}
Also used : ArrayList(java.util.ArrayList) RepositoryCache(org.aion.base.db.RepositoryCache) StaticArrayType(org.aion.solidity.SolidityType.StaticArrayType) AionTransaction(org.aion.base.AionTransaction) SolidityType(org.aion.solidity.SolidityType) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11