Search in sources :

Example 1 with AionTxReceipt

use of org.aion.base.AionTxReceipt in project aion by aionnetwork.

the class ContractIntegTest method testDeployAvmContractToAnExistedAccount.

@Test
public void testDeployAvmContractToAnExistedAccount() {
    AvmVersion version = AvmVersion.VERSION_1;
    if (txType == TransactionTypes.DEFAULT) {
        return;
    }
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ONE;
    AionAddress avmAddress = 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(), avmAddress, value.toByteArray(), new byte[0], nrg, nrgPrice, TransactionTypes.DEFAULT, 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(avmAddress));
    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);
    AionAddress avmDeployedAddress = deployAvmContract(version, deployerNonce);
    assertNotNull(avmAddress);
    assertEquals(avmAddress, avmDeployedAddress);
    repo = blockchain.getRepository().startTracking();
    IAvmResourceFactory factory = (version == AvmVersion.VERSION_1) ? this.resourceProvider.factoryForVersion1 : this.resourceProvider.factoryForVersion2;
    byte[] avmCode = factory.newContractFactory().getJarBytes(AvmContract.HELLO_WORLD);
    assertArrayEquals(avmCode, repo.getCode(avmAddress));
    assertEquals(BigInteger.ONE, repo.getBalance(avmAddress));
    byte[] call = getCallArguments(version);
    tx = AionTransaction.create(deployerKey, deployerNonce.add(BigInteger.ONE).toByteArray(), avmAddress, BigInteger.ZERO.toByteArray(), call, 2_000_000, nrgPrice, TransactionTypes.DEFAULT, null);
    block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(tx), false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    // Check the block was imported and the transaction was successful.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(receipt.isSuccessful()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTxReceipt(org.aion.base.AionTxReceipt) AvmVersion(org.aion.avm.stub.AvmVersion) Test(org.junit.Test)

Example 2 with AionTxReceipt

use of org.aion.base.AionTxReceipt 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 3 with AionTxReceipt

use of org.aion.base.AionTxReceipt in project aion by aionnetwork.

the class OldTxExecutorTest method testCallTransaction.

@Test
public void testCallTransaction() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    // deployer
    String deployer = cr.contracts.get("Ticker").bin;
    // contract
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode("c0004213");
    long nrg = new DataWord(100000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repo = blockchain.getRepository();
    RepositoryCache cache = repo.startTracking();
    cache.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    cache.createAccount(to);
    cache.saveCode(to, Hex.decode(contract));
    cache.saveVmType(to, InternalVmType.FVM);
    cache.flushTo(repo, true);
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode("00000000000000000000000000000000"), receipt.getTransactionOutput());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) RepositoryCache(org.aion.base.db.RepositoryCache) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 4 with AionTxReceipt

use of org.aion.base.AionTxReceipt in project aion by aionnetwork.

the class StatefulnessTest method testDeployContract.

@Test
public void testDeployContract() {
    AionTxReceipt receipt = deployContract(AvmVersion.VERSION_1);
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        // Check the contract has the Avm prefix, and deployment succeeded.
        assertEquals(AddressSpecs.A0_IDENTIFIER, receipt.getTransactionOutput()[0]);
        assertTrue(receipt.isSuccessful());
    } else if (txType == TransactionTypes.DEFAULT) {
        assertEquals(0, receipt.getTransactionOutput().length);
        // FIXME: is the FVM behavior correct?
        assertTrue(receipt.isSuccessful());
    }
}
Also used : AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 5 with AionTxReceipt

use of org.aion.base.AionTxReceipt in project aion by aionnetwork.

the class ApiAion method doCall.

protected byte[] doCall(ArgTxCall _params) {
    AionTransaction tx = AionTransaction.createWithoutKey(_params.getNonce().toByteArray(), _params.getFrom() == null ? AddressUtils.ZERO_ADDRESS : _params.getFrom(), _params.getTo(), _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice(), _params.getType(), null);
    AionTxReceipt rec = this.ac.callConstant(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
    return rec.getTransactionOutput();
}
Also used : AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt)

Aggregations

AionTxReceipt (org.aion.base.AionTxReceipt)111 Test (org.junit.Test)76 AionTransaction (org.aion.base.AionTransaction)74 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)61 AionAddress (org.aion.types.AionAddress)56 BigInteger (java.math.BigInteger)53 ImportResult (org.aion.zero.impl.core.ImportResult)50 MiningBlock (org.aion.zero.impl.types.MiningBlock)43 ArrayList (java.util.ArrayList)24 Block (org.aion.zero.impl.types.Block)24 AionTxExecSummary (org.aion.base.AionTxExecSummary)17 RepositoryCache (org.aion.base.db.RepositoryCache)17 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)15 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 ECKey (org.aion.crypto.ECKey)12 SolidityType (org.aion.solidity.SolidityType)10 AccountState (org.aion.base.AccountState)9 Log (org.aion.types.Log)9 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)7 StakingBlock (org.aion.zero.impl.types.StakingBlock)7