Search in sources :

Example 96 with ImportResult

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

the class MultiVersionAvmTest method deployHelloWorldContract.

private AionAddress deployHelloWorldContract(AvmVersion version, BigInteger nonce) {
    AionTransaction transaction = makeHelloWorldDeployTransaction(version, nonce);
    Block parentBlock = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
    byte[] o = connectResult.getRight().getReceipts().get(0).getTransactionOutput();
    return new AionAddress(connectResult.getRight().getReceipts().get(0).getTransactionOutput());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) 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 97 with ImportResult

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

the class MultiVersionAvmTest method testDeployVersion2ContractWithOnlyVersion1Support.

/**
 * The transaction hash contract can only be deployed in version 2 because the functionality
 * it makes use of does not exist in avm version 1.
 */
@Test
public void testDeployVersion2ContractWithOnlyVersion1Support() {
    Assert.assertEquals(0, this.blockchain.getBestBlock().getNumber());
    // Ensure we are at a block height where avm version 1 is enabled, then deploy.
    buildBlockchainToHeight(BLOCK_VERSION1_ENABLED);
    AionTransaction transaction = makeTransactionHashContract(BigInteger.ZERO);
    Block parentBlock = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
    AionAddress contract = new AionAddress(connectResult.getRight().getReceipts().get(0).getTransactionOutput());
    transaction = makeTransactionHashCallTransaction(BigInteger.ONE, contract);
    parentBlock = this.blockchain.getBestBlock();
    block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
    connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    // We expect a failure here, because an exception will get thrown inside the contract!
    Assert.assertFalse(connectResult.getRight().getReceipts().get(0).isSuccessful());
    // Now we wait until avm version 2 is enabled before calling it again.
    buildBlockchainToHeight(BLOCK_VERSION2_ENABLED);
    transaction = makeTransactionHashCallTransaction(BigInteger.TWO, contract);
    parentBlock = this.blockchain.getBestBlock();
    block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
    connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
    // Verify that the contract does indeed return the transaction hash.
    Assert.assertArrayEquals(transaction.getTransactionHash(), connectResult.getRight().getReceipts().get(0).getTransactionOutput());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) 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 98 with ImportResult

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

the class MultiVersionAvmTest method buildBlockchainToHeight.

private void buildBlockchainToHeight(long height) {
    Block parentBlock = this.blockchain.getBestBlock();
    while (parentBlock.getNumber() < height) {
        MiningBlock block = this.blockchain.createBlock(parentBlock, new ArrayList<>(), false, parentBlock.getTimestamp());
        Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
        Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
        parentBlock = this.blockchain.getBestBlock();
    }
}
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 99 with ImportResult

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

the class ContractIntegTest method testDeployFvmContractToAnExistedAccountBefore040Fork.

@Test
public void testDeployFvmContractToAnExistedAccountBefore040Fork() throws IOException {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    blockchain.forkUtility.enable040Fork(1000);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ONE;
    AionAddress destinationAddr = TxUtil.calculateContractAddress(deployer.toByteArray(), deployerNonce);
    // create a tx the sender send some balance to the account the deployer will deploy in the
    // feature.
    AionTransaction tx = AionTransaction.create(senderKey, senderNonce.toByteArray(), destinationAddr, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    MiningBlock block = makeBlock(tx);
    assertEquals(1, block.getTransactionsList().size());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    RepositoryCache repo = blockchain.getRepository().startTracking();
    assertEquals(BigInteger.ONE, repo.getBalance(destinationAddr));
    BigInteger txCost = BigInteger.valueOf(nrgPrice).multiply(BigInteger.valueOf(result.getRight().getReceipts().get(0).getEnergyUsed()));
    assertEquals(senderBalance.subtract(BigInteger.ONE).subtract(txCost), repo.getBalance(sender));
    senderNonce = senderNonce.add(BigInteger.ONE);
    String contractName = "PayableConstructor";
    byte[] deployCode = getDeployCode(contractName);
    // to == null  signals that this is contract creation.
    tx = AionTransaction.create(deployerKey, deployerNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    block = makeBlock(tx);
    assertEquals(1, block.getTransactionsList().size());
    result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = result.getRight().getSummaries().get(0);
    if (txType == TransactionTypes.DEFAULT) {
        // "" == SUCCESS
        assertEquals("FAILURE", summary.getReceipt().getError());
        deployerNonce = deployerNonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, deployerNonce);
    }
    assertEquals(1000000, summary.getReceipt().getEnergyUsed());
}
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 100 with ImportResult

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

the class ContractIntegTest method testDeployFvmContractToAnExistedAccountWith040Fork.

@Test
public void testDeployFvmContractToAnExistedAccountWith040Fork() throws IOException {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    blockchain.forkUtility.enable040Fork(0);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ONE;
    AionAddress destinationAddr = TxUtil.calculateContractAddress(deployer.toByteArray(), deployerNonce);
    // create a tx the sender send some balance to the account the deployer will deploy in the
    // feature.
    AionTransaction tx = AionTransaction.create(senderKey, senderNonce.toByteArray(), destinationAddr, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    MiningBlock block = makeBlock(tx);
    assertEquals(1, block.getTransactionsList().size());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    RepositoryCache repo = blockchain.getRepository().startTracking();
    assertEquals(BigInteger.ONE, repo.getBalance(destinationAddr));
    BigInteger txCost = BigInteger.valueOf(nrgPrice).multiply(BigInteger.valueOf(result.getRight().getReceipts().get(0).getEnergyUsed()));
    assertEquals(senderBalance.subtract(BigInteger.ONE).subtract(txCost), repo.getBalance(sender));
    senderNonce = senderNonce.add(BigInteger.ONE);
    String contractName = "PayableConstructor";
    byte[] deployCode = getDeployCode(contractName);
    // to == null  signals that this is contract creation.
    tx = AionTransaction.create(deployerKey, deployerNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    block = makeBlock(tx);
    assertEquals(1, block.getTransactionsList().size());
    result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = result.getRight().getSummaries().get(0);
    if (txType == TransactionTypes.DEFAULT) {
        // "" == SUCCESS
        assertEquals("", summary.getReceipt().getError());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
        deployerNonce = deployerNonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, deployerNonce);
    }
    assertEquals(225787, summary.getReceipt().getEnergyUsed());
}
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)

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