Search in sources :

Example 6 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class ContractIntegTest method deployAvmContract.

private AionAddress deployAvmContract(AvmVersion version, BigInteger nonce) {
    byte[] jar = getJarBytes(version);
    AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000L, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
    MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    // Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    // verify that the output is indeed the contract address
    AionAddress contractAddress = TxUtil.calculateContractAddress(transaction);
    assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
    return contractAddress;
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 7 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class ContractIntegTest method tellFvmContractCallBalanceTransfer.

@Test
public void tellFvmContractCallBalanceTransfer() throws Exception {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    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, true);
    assertNotNull(contract);
    repo = blockchain.getRepository().startTracking();
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
    // input = ByteUtil.merge(input, new DataWordImpl(numRecurses + 1).copyOfData());
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    BigInteger senderBalance = repo.getBalance(deployer);
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contract));
    assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
    input = Arrays.copyOfRange(HashUtil.keccak256("callBalanceTransfer(address)".getBytes()), 0, 4);
    AionAddress receiver = AddressUtils.wrapAddress("0x000000000000000000000000000000000000000000000000000000000000000a");
    input = ByteUtil.merge(input, receiver.toByteArray());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    repo = blockchain.getRepository().startTracking();
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertEquals(1, result.getRight().getSummaries().get(0).getInternalTransactions().size());
    assertEquals(BigInteger.TEN.subtract(BigInteger.ONE), blockchain.getRepository().getBalance(contract));
    assertEquals(BigInteger.ONE, blockchain.getRepository().getBalance(receiver));
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) 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) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 8 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class StatefulnessTest method sendTransactions.

private AionTxReceipt sendTransactions(AionTransaction... transactions) {
    Block parentBlock = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createBlock(parentBlock, Arrays.asList(transactions), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    return connectResult.getRight().getReceipts().get(0);
}
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) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 9 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class FvmBulkTransactionTest method sendTransactionsInBulkInSingleBlock.

private AionBlockSummary sendTransactionsInBulkInSingleBlock(List<AionTransaction> transactions) {
    Block parentBlock = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createBlock(parentBlock, transactions, false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    return connectResult.getRight();
}
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) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 10 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class FvmExploitsTest method testModifer.

@Ignore
@Test
public void testModifer() throws InterruptedException {
    String testerByteCode = "0x605060405234156100105760006000fd5b5b3360006000508282909180600101839055555050505b61002c565b61016e8061003b6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680630acf8c2e14610049578063421b2d8b14610073578063e61a60bb1461009f57610043565b60006000fd5b34156100555760006000fd5b61005d6100c9565b6040518082815260100191505060405180910390f35b341561007f5760006000fd5b61009d600480808060100135903590916020019091929050506100d7565b005b34156100ab5760006000fd5b6100b3610113565b6040518082815260100191505060405180910390f35b6000600a90506100d4565b90565b600060005080600101549054339091149190141615156100f75760006000fd5b818160026000508282909180600101839055555050505b5b5050565b6000600260005080600101549054339091149190141615156101355760006000fd5b600b905061013e565b5b905600a165627a7a723058201d8c8bf193120213679831363ac65fecb0dcb5be8b65e6c0a1c97f4a7d3d3ef20029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // =======================================================================
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(testerByteCode), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    assertThat(tx.isContractCreationTransaction()).isTrue();
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    Thread.sleep(1000L);
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], // getEleven()
    ByteUtil.hexStringToBytes("0xe61a60bb"), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context2 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx2), false);
    ImportResult result2 = bc.tryToConnect(context2.block);
    assertThat(result2).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info2 = bc.getTransactionInfo(context2.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("REVERT", info2.getReceipt().getError());
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], ByteUtil.hexStringToBytes("0x421b2d8b" + Hex.toHexString(// addUser(address)
    deployerAccount.getAddress())), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context3 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx3), false);
    ImportResult result3 = bc.tryToConnect(context3.block);
    assertThat(result3).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info3 = bc.getTransactionInfo(context3.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("", info3.getReceipt().getError());
    Thread.sleep(1000L);
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx4 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], // getEleven()
    ByteUtil.hexStringToBytes("0xe61a60bb"), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context4 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx4), false);
    ImportResult result4 = bc.tryToConnect(context4.block);
    assertThat(result4).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info4 = bc.getTransactionInfo(context4.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("", info4.getReceipt().getError());
    assertEquals(11, FvmDataWord.fromBytes(info4.getReceipt().getTransactionOutput()).toInt());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) BlockContext(org.aion.zero.impl.types.BlockContext) BigInteger(java.math.BigInteger) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ImportResult (org.aion.zero.impl.core.ImportResult)166 Test (org.junit.Test)127 AionTransaction (org.aion.base.AionTransaction)114 AionAddress (org.aion.types.AionAddress)106 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)95 MiningBlock (org.aion.zero.impl.types.MiningBlock)90 Block (org.aion.zero.impl.types.Block)80 BigInteger (java.math.BigInteger)75 AionTxReceipt (org.aion.base.AionTxReceipt)50 AionTxExecSummary (org.aion.base.AionTxExecSummary)31 ECKey (org.aion.crypto.ECKey)28 RepositoryCache (org.aion.base.db.RepositoryCache)22 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)22 InternalTransaction (org.aion.types.InternalTransaction)20 AccountState (org.aion.base.AccountState)18 ArrayList (java.util.ArrayList)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 BlockContext (org.aion.zero.impl.types.BlockContext)9