Search in sources :

Example 31 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class SolidityTypeTest method testBytes2.

@Test
public void testBytes2() throws Exception {
    byte[] x = Hex.decode("11223344556677881122334455667788112233445566778811223344556677881122334455667788");
    SolidityType type = new BytesType();
    byte[] params = ByteUtil.merge(Hex.decode("00000000000000000000000000000010"), type.encode(x));
    System.out.println(Hex.toHexString(params));
    AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("61cb5a01"), params));
    MiningBlock block = createDummyBlock();
    RepositoryCache repo = createRepository(tx);
    AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(params, receipt.getTransactionOutput());
    assertArrayEquals(x, (byte[]) type.decode(receipt.getTransactionOutput(), 16));
}
Also used : RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) SolidityType(org.aion.solidity.SolidityType) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) BytesType(org.aion.solidity.SolidityType.BytesType) Test(org.junit.Test)

Example 32 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class BulkExecutor method getInternalVmType.

/**
 * Returns the InternalVmType recorded for the given address.
 */
private static InternalVmType getInternalVmType(RepositoryCache repository, AionAddress destination) {
    // will load contract into memory otherwise leading to consensus issues
    RepositoryCache track = repository.startTracking();
    AccountState accountState = track.getAccountState(destination);
    InternalVmType vm;
    if (accountState == null) {
        // the address doesn't exist yet, so it can be used by either vm
        vm = InternalVmType.EITHER;
    } else {
        vm = repository.getVMUsed(destination, accountState.getCodeHash());
        // UNKNOWN is returned when there was no contract information stored
        if (vm == InternalVmType.UNKNOWN) {
            // use the in-memory value
            vm = track.getVmType(destination);
        }
    }
    return vm;
}
Also used : InternalVmType(org.aion.base.InternalVmType) RepositoryCache(org.aion.base.db.RepositoryCache) AccountState(org.aion.base.AccountState)

Example 33 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class FvmTransactionExecutor method executeTransactions.

/**
 * Executes the specified array of transactions using the FVM and returns a list of transaction
 * summaries, such that the i'th summary pertains to the i'th transaction in the input.
 *
 * <p>If no post-execution work is specified then none will be run. Otherwise, the
 * post-execution work will be applied in such a way that it appears, from the caller's
 * perspective, as if it was run immediately after each transaction sequentially.
 *
 * @param repository The current snapshot of the kernel's repository layer.
 * @param blockDifficulty The current best block's difficulty.
 * @param blockNumber The current best block number.
 * @param blockTimestamp The current best block timestamp.
 * @param blockNrgLimit The current best block energy limit.
 * @param blockCoinbase The address of the miner.
 * @param transactions The transactions to execute.
 * @param postExecutionWork The post-execute work, if any, to be run immediately after each
 *     transaction completes.
 * @param logger A logger.
 * @param decrementBlockEnergyLimit Whether to decrement the block energy limit (ie. if no
 *     decrement implication is block overflow won't be checked)
 * @param allowNonceIncrement Whether to increment the sender nonce.
 * @param isLocalCall Whether this is a local call or not.
 * @param fork040enabled Whether or not the 0.4.0 fork is enabled.
 * @param initialBlockEnergyLimit The initial block energy limit at the time of running these
 *     transactions.
 * @return a list of transaction summaries pertaining to the transactions.
 */
public static List<AionTxExecSummary> executeTransactions(RepositoryCache repository, byte[] blockDifficulty, long blockNumber, long blockTimestamp, long blockNrgLimit, AionAddress blockCoinbase, AionTransaction[] transactions, PostExecutionWork postExecutionWork, Logger logger, boolean decrementBlockEnergyLimit, boolean allowNonceIncrement, boolean isLocalCall, boolean fork040enabled, long initialBlockEnergyLimit, boolean unityForkEnabled, boolean signatureSwapForkEnabled) throws VmFatalException {
    List<AionTxExecSummary> transactionSummaries = new ArrayList<>();
    long blockRemainingEnergy = initialBlockEnergyLimit;
    // Run the transactions.
    IExternalStateForFvm externalState = new ExternalStateForFvm(repository, blockCoinbase, getDifficultyAsDataWord(blockDifficulty), isLocalCall, allowNonceIncrement, fork040enabled, blockNumber, blockTimestamp, blockNrgLimit, unityForkEnabled, signatureSwapForkEnabled);
    // Process the results of the transactions.
    for (AionTransaction transaction : transactions) {
        FvmWrappedTransactionResult wrappedResult = FastVirtualMachine.run(externalState, new ExternalCapabilitiesForFvm(), toAionTypesTransaction(transaction), fork040enabled);
        TransactionResult result = wrappedResult.result;
        List<AionAddress> deletedAddresses = wrappedResult.deletedAddresses;
        if (result.transactionStatus.isFatal()) {
            throw new VmFatalException(result.toString());
        }
        // Check the block energy limit & reject if necessary.
        if (result.energyUsed > blockRemainingEnergy) {
            TransactionStatus status = TransactionStatus.rejection("Invalid Energy Limit");
            result = new TransactionResult(status, result.logs, result.internalTransactions, 0, ByteUtil.EMPTY_BYTE_ARRAY);
        }
        // Build the transaction summary.
        AionTxExecSummary summary = buildTransactionSummary(transaction, result, deletedAddresses);
        // If the transaction was not rejected, then commit the state changes.
        if (!result.transactionStatus.isRejected()) {
            externalState.commit();
        }
        // For non-rejected non-local transactions, make some final repository updates.
        if (!isLocalCall && !summary.isRejected()) {
            RepositoryCache repositoryTracker = repository.startTracking();
            refundSender(repositoryTracker, summary, transaction, result);
            payMiner(repositoryTracker, blockCoinbase, summary);
            deleteAccountsMarkedForDeletion(repositoryTracker, summary.getDeletedAccounts(), result);
            repositoryTracker.flushTo(repository, true);
        }
        // Do any post execution work.
        if (postExecutionWork != null) {
            postExecutionWork.doWork(repository, summary, transaction);
        }
        // Update the remaining block energy.
        if (!result.transactionStatus.isRejected() && decrementBlockEnergyLimit) {
            blockRemainingEnergy -= summary.getReceipt().getEnergyUsed();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Transaction receipt: {}", summary.getReceipt());
            logger.debug("Transaction logs: {}", summary.getLogs());
        }
        transactionSummaries.add(summary);
    }
    return transactionSummaries;
}
Also used : IExternalStateForFvm(org.aion.fastvm.IExternalStateForFvm) FvmWrappedTransactionResult(org.aion.fastvm.FvmWrappedTransactionResult) TransactionResult(org.aion.types.TransactionResult) AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) TransactionStatus(org.aion.types.TransactionStatus) AionTransaction(org.aion.base.AionTransaction) FvmWrappedTransactionResult(org.aion.fastvm.FvmWrappedTransactionResult) IExternalStateForFvm(org.aion.fastvm.IExternalStateForFvm) VmFatalException(org.aion.zero.impl.vm.common.VmFatalException) RepositoryCache(org.aion.base.db.RepositoryCache)

Example 34 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ExternalStateForFvm method getVmType.

private InternalVmType getVmType(AionAddress destination) {
    // will load contract into memory otherwise leading to consensus issues
    RepositoryCache track = this.repository.startTracking();
    AccountState accountState = track.getAccountState(destination);
    InternalVmType vm;
    if (accountState == null) {
        // the address doesn't exist yet, so it can be used by either vm
        vm = InternalVmType.EITHER;
    } else {
        vm = this.repository.getVMUsed(destination, accountState.getCodeHash());
        // UNKNOWN is returned when there was no contract information stored
        if (vm == InternalVmType.UNKNOWN) {
            // use the in-memory value
            vm = track.getVmType(destination);
        }
    }
    return vm;
}
Also used : InternalVmType(org.aion.base.InternalVmType) RepositoryCache(org.aion.base.db.RepositoryCache) AccountState(org.aion.base.AccountState)

Example 35 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ExternalStateForAvm method getTransformedCode.

@Override
public byte[] getTransformedCode(AionAddress address) {
    // will load contract into memory otherwise leading to consensus issues
    RepositoryCache track = repositoryCache.startTracking();
    byte[] codeHash = track.getAccountState(address).getCodeHash();
    return this.repositoryCache.getTransformedCode(address, codeHash, 2);
}
Also used : RepositoryCache(org.aion.base.db.RepositoryCache)

Aggregations

RepositoryCache (org.aion.base.db.RepositoryCache)120 Test (org.junit.Test)102 AionAddress (org.aion.types.AionAddress)90 AionTransaction (org.aion.base.AionTransaction)89 AionTxExecSummary (org.aion.base.AionTxExecSummary)72 BigInteger (java.math.BigInteger)43 MiningBlock (org.aion.zero.impl.types.MiningBlock)41 AccountState (org.aion.base.AccountState)38 DataWord (org.aion.util.types.DataWord)23 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)23 ImportResult (org.aion.zero.impl.core.ImportResult)22 InternalTransaction (org.aion.types.InternalTransaction)18 Block (org.aion.zero.impl.types.Block)18 AionTxReceipt (org.aion.base.AionTxReceipt)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 BlockContext (org.aion.zero.impl.types.BlockContext)15 SolidityType (org.aion.solidity.SolidityType)10 ArrayList (java.util.ArrayList)8 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)6