Search in sources :

Example 31 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class VMTxStateAlignTest method testDeployAndCallContractStatesAlign.

@Test
public void testDeployAndCallContractStatesAlign() throws IOException {
    List<AionTransaction> txList = new ArrayList<>();
    List<AionAddress> deployAddr = new ArrayList<>();
    for (int i = 0; i < this.deployerKeys.size() && i < deployerNum; i++) {
        ECKey senderKey = this.deployerKeys.get(i);
        txList.add(makeFvmContractCreateTransaction(senderKey, BigInteger.ZERO));
        deployAddr.add(TxUtil.calculateContractAddress(txList.get(i)));
    }
    MiningBlock deplayBlock = genNewBlock(txList, blockchainWoAVM);
    tryImportNewBlock(blockchainWoAVM, deplayBlock);
    txList.clear();
    ECKey sender = this.deployerKeys.get(deployerKeys.size() - 1);
    txList.add(makePrecompiledContractTransaction(sender, BigInteger.ZERO));
    for (int i = 0; i < this.deployerKeys.size() && i < deployerNum; i++) {
        ECKey senderKey = this.deployerKeys.get(i);
        txList.add(makeFvmContractCallTransaction(senderKey, BigInteger.ONE, deployAddr.get(i)));
    }
    MiningBlock callBlock = genNewBlock(txList, blockchainWoAVM);
    tryImportNewBlock(blockchainWoAVM, callBlock);
    // Does not initial in the setup call due to the avmenable in the StandaloneBlockchain is a
    // static variable.
    StandaloneBlockchain.Bundle bundleAVM = new StandaloneBlockchain.Builder().withDefaultAccounts(deployerKeys).withAvmEnabled().withValidatorConfiguration("simple").build();
    StandaloneBlockchain blockchainWithAVM = bundleAVM.bc;
    tryImportNewBlock(blockchainWithAVM, deplayBlock);
    tryImportNewBlock(blockchainWithAVM, callBlock);
}
Also used : AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 32 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class VMTxStateAlignTest method setup.

@Before
public void setup() {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    this.blockchainWoAVM = bundle.bc;
    deployerKeys = bundle.privateKeys;
    deployers = new ArrayList<>();
    for (int i = 0; i < deployerNum && i < deployerKeys.size(); i++) {
        AionAddress deployerAddr = new AionAddress(this.deployerKeys.get(i).getAddress());
        deployers.add(deployerAddr);
    }
}
Also used : AionAddress(org.aion.types.AionAddress) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) Before(org.junit.Before)

Example 33 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ApiAion method createContract.

protected ApiTxResponse createContract(ArgTxCall _params) {
    if (_params == null) {
        return (new ApiTxResponse(TxResponse.INVALID_TX));
    }
    AionAddress from = _params.getFrom();
    if (from == null) {
        LOG.error("<create-contract msg=invalid-from-address>");
        return (new ApiTxResponse(TxResponse.INVALID_FROM));
    }
    ECKey key = this.getAccountKey(from.toString());
    if (key == null) {
        LOG.debug("ApiAion.createContract - null key");
        return (new ApiTxResponse(TxResponse.INVALID_ACCOUNT));
    }
    try {
        synchronized (pendingState) {
            byte[] nonce = !(_params.getNonce().equals(BigInteger.ZERO)) ? _params.getNonce().toByteArray() : pendingState.bestPendingStateNonce(new AionAddress(key.getAddress())).toByteArray();
            AionTransaction tx = AionTransaction.create(key, nonce, null, _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice(), _params.getType(), null);
            TxResponse rsp = pendingState.addTransactionFromApiServer(tx);
            AionAddress address = TxUtil.calculateContractAddress(tx);
            return new ApiTxResponse(rsp, tx.getTransactionHash(), address);
        }
    } catch (Exception ex) {
        LOG.error("ApiAion.createContract - exception: [{}]", ex.getMessage());
        return new ApiTxResponse(TxResponse.EXCEPTION, ex);
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) TxResponse(org.aion.zero.impl.types.TxResponse)

Example 34 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ApiAion method estimateNrg.

protected long estimateNrg(ArgTxCall params) {
    AionAddress fromAddr = (params.getFrom() == null) ? AddressUtils.ZERO_ADDRESS : params.getFrom();
    AionTransaction tx = AionTransaction.createWithoutKey(params.getNonce().toByteArray(), fromAddr, params.getTo(), params.getValue().toByteArray(), params.getData(), params.getNrg(), params.getNrgPrice(), params.getType(), null);
    AionTxReceipt receipt = this.ac.callConstant(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
    return receipt.getEnergyUsed();
}
Also used : AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt)

Example 35 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class AvmProviderTest method testBalanceTransferTransactionVersion2.

@Test
public void testBalanceTransferTransactionVersion2() throws Exception {
    Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
    AvmProvider.enableAvmVersion(AvmVersion.VERSION_2, this.projectRootDir);
    AvmProvider.startAvm(AvmVersion.VERSION_2);
    // Set up the repo and give the sender account some balance.
    RepositoryCache repository = newRepository();
    AionAddress sender = randomAddress();
    AionAddress recipient = randomAddress();
    addBalance(repository, sender, BigInteger.valueOf(1_000_000));
    // Run the transaction.
    RepositoryCache repositoryChild = repository.startTracking();
    IAvmExternalState externalState = newExternalState(AvmVersion.VERSION_2, repositoryChild, newEnergyRules());
    Transaction transaction = newBalanceTransferTransaction(sender, recipient, BigInteger.TEN);
    IAionVirtualMachine avm = AvmProvider.getAvm(AvmVersion.VERSION_2);
    IAvmFutureResult[] futures = avm.run(externalState, new Transaction[] { transaction }, AvmExecutionType.MINING, 0);
    // Assert the result and state changes we expect.
    Assert.assertEquals(1, futures.length);
    TransactionResult result = futures[0].getResult();
    Assert.assertTrue(result.transactionStatus.isSuccess());
    Assert.assertEquals(BigInteger.TEN, repositoryChild.getBalance(recipient));
    AvmProvider.shutdownAvm(AvmVersion.VERSION_2);
    AvmProvider.disableAvmVersion(AvmVersion.VERSION_2);
    AvmProvider.releaseLock();
}
Also used : IAvmExternalState(org.aion.avm.stub.IAvmExternalState) TransactionResult(org.aion.types.TransactionResult) AionAddress(org.aion.types.AionAddress) Transaction(org.aion.types.Transaction) IAvmFutureResult(org.aion.avm.stub.IAvmFutureResult) RepositoryCache(org.aion.base.db.RepositoryCache) IAionVirtualMachine(org.aion.avm.stub.IAionVirtualMachine) Test(org.junit.Test)

Aggregations

AionAddress (org.aion.types.AionAddress)491 Test (org.junit.Test)364 AionTransaction (org.aion.base.AionTransaction)275 BigInteger (java.math.BigInteger)194 ImportResult (org.aion.zero.impl.core.ImportResult)110 AionTxExecSummary (org.aion.base.AionTxExecSummary)97 RepositoryCache (org.aion.base.db.RepositoryCache)90 MiningBlock (org.aion.zero.impl.types.MiningBlock)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)80 ArrayList (java.util.ArrayList)76 ECKey (org.aion.crypto.ECKey)74 AionTxReceipt (org.aion.base.AionTxReceipt)60 Block (org.aion.zero.impl.types.Block)60 AccountState (org.aion.base.AccountState)56 HashMap (java.util.HashMap)42 DataWord (org.aion.util.types.DataWord)39 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)36 InternalTransaction (org.aion.types.InternalTransaction)32 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)29 BlockContext (org.aion.zero.impl.types.BlockContext)29