Search in sources :

Example 1 with IAvmExternalState

use of org.aion.avm.stub.IAvmExternalState 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)

Example 2 with IAvmExternalState

use of org.aion.avm.stub.IAvmExternalState in project aion by aionnetwork.

the class AvmProviderTest method testBalanceTransferTransactionVersion2WithCoinbaseLock.

@Test
public void testBalanceTransferTransactionVersion2WithCoinbaseLock() throws Exception {
    Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
    AvmProvider.enableAvmVersion(AvmVersion.VERSION_2, this.projectRootDir);
    AvmProvider.startAvm(AvmVersion.VERSION_2, true);
    // 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)

Example 3 with IAvmExternalState

use of org.aion.avm.stub.IAvmExternalState in project aion by aionnetwork.

the class AvmTransactionExecutor method buildSummaryAndUpdateState.

/**
 * Builds a transaction summary and updates the current state of the world. If the transction
 * was rejected, or if this is a local call, then no state changes will be made. Otherwise,
 * the world state will be updated.
 *
 * Returns the built summary.
 *
 * @param future The future result.
 * @param transaction The transaction that was run.
 * @param result The result of running the transaction.
 * @param avmVersion The version of the avm that was used.
 * @param repository The current state of the world.
 * @param blockDifficulty The block difficulty.
 * @param blockNumber The current block number.
 * @param blockTimestamp The block timestamp.
 * @param blockEnergyLimit The energy limit of the block.
 * @param miner The miner address.
 * @param allowNonceIncrement Whether to increment the sender's nonce or not.
 * @param isLocalCall Whether this is a local call (ie. is to cause no state changes).
 * @return the execution summary.
 */
private static AionTxExecSummary buildSummaryAndUpdateState(IAvmFutureResult future, AionTransaction transaction, TransactionResult result, AvmVersion avmVersion, RepositoryCache repository, BigInteger blockDifficulty, long blockNumber, long blockTimestamp, long blockEnergyLimit, AionAddress miner, boolean allowNonceIncrement, boolean isLocalCall) {
    AionTxExecSummary summary = buildTransactionSummary(transaction, result);
    // Update the repository by committing any changes in the Avm so long as the transaction was not rejected.
    if (!result.transactionStatus.isRejected() && !isLocalCall) {
        // We have to build a new world state so things flush to repository.
        IAvmExternalState currentWorldState = AvmProvider.newExternalStateBuilder(avmVersion).withRepository(repository).withMiner(miner).withDifficulty(blockDifficulty).withBlockNumber(blockNumber).withBlockTimestamp(blockTimestamp).withBlockEnergyLimit(blockEnergyLimit).withEnergyRules(AvmConfigurations.getEnergyLimitRules()).allowNonceIncrement(allowNonceIncrement).isLocalCall(isLocalCall).build();
        future.commitStateChangesTo(currentWorldState);
    }
    return summary;
}
Also used : IAvmExternalState(org.aion.avm.stub.IAvmExternalState) AionTxExecSummary(org.aion.base.AionTxExecSummary)

Example 4 with IAvmExternalState

use of org.aion.avm.stub.IAvmExternalState in project aion by aionnetwork.

the class AvmProviderTest method testBalanceTransferTransactionVersion1.

@Test
public void testBalanceTransferTransactionVersion1() throws Exception {
    Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
    AvmProvider.enableAvmVersion(AvmVersion.VERSION_1, this.projectRootDir);
    AvmProvider.startAvm(AvmVersion.VERSION_1);
    // 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_1, repositoryChild, newEnergyRules());
    Transaction transaction = newBalanceTransferTransaction(sender, recipient, BigInteger.TEN);
    IAionVirtualMachine avm = AvmProvider.getAvm(AvmVersion.VERSION_1);
    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_1);
    AvmProvider.disableAvmVersion(AvmVersion.VERSION_1);
    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)

Example 5 with IAvmExternalState

use of org.aion.avm.stub.IAvmExternalState in project aion by aionnetwork.

the class AvmTransactionExecutor method invokeAvm.

/**
 * Invokes the avm, whichever is the specified versionToUse, to run the given transactions under
 * the given circumstances. Returns a list of future results pertaining to the transactions.
 *
 * @param versionToUse The version of the avm to use.
 * @param repository The current world state.
 * @param blockDifficulty The block difficulty.
 * @param blockNumber The current block number.
 * @param blockTimestamp The block timestamp.
 * @param blockEnergyLimit The energy limit of the block.
 * @param miner The miner address.
 * @param transactions The transactions to execute.
 * @param allowNonceIncrement Whether to increment the sender's nonce or not.
 * @param isLocalCall Whether this is a local call (ie. is to cause no state changes).
 * @param executionType The avm execution type.
 * @param cachedBlockNumber The cached block number.
 * @param unityForkEnabled The unityFork feature enabled/disabled.
 * @return the future execution results.
 */
private static IAvmFutureResult[] invokeAvm(AvmVersion versionToUse, RepositoryCache repository, BigInteger blockDifficulty, long blockNumber, long blockTimestamp, long blockEnergyLimit, AionAddress miner, AionTransaction[] transactions, boolean allowNonceIncrement, boolean isLocalCall, AvmExecutionType executionType, long cachedBlockNumber, boolean unityForkEnabled) {
    IAvmExternalState externalState = AvmProvider.newExternalStateBuilder(versionToUse).withRepository(repository.startTracking()).withMiner(miner).withDifficulty(blockDifficulty).withBlockNumber(blockNumber).withBlockTimestamp(blockTimestamp).withBlockEnergyLimit(blockEnergyLimit).withEnergyRules(unityForkEnabled ? AvmConfigurations.getEnergyLimitRulesAfterUnityFork() : AvmConfigurations.getEnergyLimitRules()).allowNonceIncrement(allowNonceIncrement).isLocalCall(isLocalCall).build();
    IAionVirtualMachine avm = AvmProvider.getAvm(versionToUse);
    return avm.run(externalState, toAionTypesTransactions(transactions), executionType, cachedBlockNumber);
}
Also used : IAvmExternalState(org.aion.avm.stub.IAvmExternalState) IAionVirtualMachine(org.aion.avm.stub.IAionVirtualMachine)

Aggregations

IAvmExternalState (org.aion.avm.stub.IAvmExternalState)5 IAionVirtualMachine (org.aion.avm.stub.IAionVirtualMachine)4 IAvmFutureResult (org.aion.avm.stub.IAvmFutureResult)3 RepositoryCache (org.aion.base.db.RepositoryCache)3 AionAddress (org.aion.types.AionAddress)3 Transaction (org.aion.types.Transaction)3 TransactionResult (org.aion.types.TransactionResult)3 Test (org.junit.Test)3 AionTxExecSummary (org.aion.base.AionTxExecSummary)1