Search in sources :

Example 41 with AccountState

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

the class AionRepositoryImpl method getCode.

@Override
public byte[] getCode(AionAddress address) {
    AccountState accountState = getAccountState(address);
    if (accountState == null) {
        return EMPTY_BYTE_ARRAY;
    }
    byte[] codeHash = accountState.getCodeHash();
    ContractDetails details = getContractDetails(address);
    return (details == null) ? EMPTY_BYTE_ARRAY : details.getCode(codeHash);
}
Also used : AccountState(org.aion.base.AccountState)

Example 42 with AccountState

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

the class AionImpl method getAccountState.

// assumes a correctly formatted block number
@Override
public Optional<AccountState> getAccountState(AionAddress address, long blockNumber) {
    try {
        byte[] stateRoot = this.aionHub.getBlockchain().getBlockByNumber(blockNumber).getStateRoot();
        AccountState account = this.aionHub.getRepository().getSnapshotTo(stateRoot).getAccountState(address);
        if (account == null)
            return Optional.empty();
        return Optional.of(account);
    } catch (Exception e) {
        LOG_GEN.debug("query request failed", e);
        return Optional.empty();
    }
}
Also used : AccountState(org.aion.base.AccountState) VmFatalException(org.aion.zero.impl.vm.common.VmFatalException)

Example 43 with AccountState

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

the class DevCLI method printAccountDetails.

public static Cli.ReturnType printAccountDetails(String strAddress) {
    // TODO AKI-681: more parameters would be useful, e.g. get account X at block Y
    // read database configuration
    CfgAion.inst().dbFromXML();
    AionLoggerFactory.initAll(Map.of(LogEnum.GEN, LogLevel.INFO));
    final Logger log = AionLoggerFactory.getLogger(LogEnum.GEN.name());
    AionAddress address;
    try {
        address = new AionAddress(ByteUtil.hexStringToBytes(strAddress));
    } catch (NumberFormatException e) {
        log.error("The given argument «" + strAddress + "» cannot be converted to a valid account address.");
        return Cli.ReturnType.ERROR;
    }
    // get the current repository
    AionRepositoryImpl repository = AionRepositoryImpl.inst();
    try {
        Block bestBlock = repository.getBestBlock();
        Repository snapshot = repository.getSnapshotTo(bestBlock.getStateRoot()).startTracking();
        AccountState account = snapshot.getAccountState(address);
        log.info(account.toString());
        log.info(snapshot.getContractDetails(address).toString());
        return Cli.ReturnType.EXIT;
    } catch (Exception e) {
        log.error("Error encountered while attempting to retrieve the account data.", e);
        return Cli.ReturnType.ERROR;
    } finally {
        repository.close();
    }
}
Also used : Repository(org.aion.base.db.Repository) AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AccountState(org.aion.base.AccountState) Logger(org.slf4j.Logger) IOException(java.io.IOException)

Example 44 with AccountState

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

the class AvmBulkTransactionTest method importBlockWithContractAndCallsForFvmOnTopOfAddressWithBalanceAfterFork040.

@Test
public void importBlockWithContractAndCallsForFvmOnTopOfAddressWithBalanceAfterFork040() {
    // Enable Fork040 to be able to deploy the contract on FVM.
    blockchain.forkUtility.enable040Fork(1L);
    BigInteger initialNonce = getNonce(deployerKey);
    // One transaction will be made to add balance to the expected contract address before the contract deployment.
    BigInteger expectedNonce = initialNonce.add(BigInteger.ONE);
    BigInteger initialBalance = getBalance(deployerKey);
    List<AionTransaction> transactions = new ArrayList<>();
    // Deploy FVM contract.
    String contractCode = "0x605060405234156100105760006000fd5b5b600a600060005081909090555060006000505460016000506000600060005054815260100190815260100160002090506000508190909055506064600260005060000160005081909090555060c8600260005060010160005081909090555060026000506001016000505460016000506000600260005060000160005054815260100190815260100160002090506000508190909055505b6100ae565b610184806100bd6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680631677b0ff14610049578063209652551461007657806362eb702a146100a057610043565b60006000fd5b34156100555760006000fd5b61007460048080359060100190919080359060100190919050506100c4565b005b34156100825760006000fd5b61008a610111565b6040518082815260100191505060405180910390f35b34156100ac5760006000fd5b6100c26004808035906010019091905050610123565b005b8160026000506000016000508190909055508060026000506001016000508190909055508082016001600050600084815260100190815260100160002090506000508190909055505b5050565b60006000600050549050610120565b90565b806000600050819090905550600181016001600050600083815260100190815260100160002090506000508190909055505b505600a165627a7a723058205b6e690d70d3703337452467437dc7c4e863ee4ad34b24cc516e2afa71e334700029";
    AionTransaction deployTxFVM = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), ByteUtil.hexStringToBytes(contractCode), 5_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionAddress fvmContract = TxUtil.calculateContractAddress(deployTxFVM);
    transactions.add(deployTxFVM);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Call FVM contract.
    AionTransaction contractCallTx = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), fvmContract, BigInteger.ZERO.toByteArray(), Hex.decode("62eb702a00000000000000000000000000000006"), 2_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    transactions.add(contractCallTx);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // First send balance to the future contract.
    AionTransaction balanceTransferToFVM = AionTransaction.create(deployerKey, initialNonce.toByteArray(), fvmContract, BigInteger.TEN.toByteArray(), new byte[0], 2_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(List.of(balanceTransferToFVM));
    // Verify that the transaction was successful.
    assertThat(blockSummary.getSummaries().size()).isEqualTo(1);
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertThat(receipt.isSuccessful()).isTrue();
    BigInteger expectedBalance = initialBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(energyPrice)));
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(fvmContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the 2 transactions in a single block.
    blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // Verify that all transactions were successful.
    assertThat(blockSummary.getSummaries().size()).isEqualTo(2);
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        receipt = transactionSummary.getReceipt();
        assertThat(receipt.isSuccessful()).isTrue();
        // Compute the expected balance.
        expectedBalance = expectedBalance.subtract(BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(energyPrice)));
    }
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(fvmContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
    assertThat(getBalance(deployerKey)).isEqualTo(expectedBalance);
    assertThat(getNonce(deployerKey)).isEqualTo(expectedNonce);
}
Also used : AionAddress(org.aion.types.AionAddress) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Example 45 with AccountState

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

the class AvmBulkTransactionTest method importBlockWithContractAndCallsForBothVMs.

/**
 * Ensures that a block containing the transactions described below is processed correctly:
 *
 * <ol>
 *   <li>an FVM contract deployment,
 *   <li>an AVM contract deployment,
 *   <li>an FVM call to the first contract,
 *   <li>an AVM call to the second contract.
 * </ol>
 */
@Test
public void importBlockWithContractAndCallsForBothVMs() {
    BigInteger expectedNonce = getNonce(deployerKey);
    BigInteger initialBalance = getBalance(deployerKey);
    List<AionTransaction> transactions = new ArrayList<>();
    // Deploy FVM contract with code.
    String contractCode = "0x605060405234156100105760006000fd5b5b600a600060005081909090555060006000505460016000506000600060005054815260100190815260100160002090506000508190909055506064600260005060000160005081909090555060c8600260005060010160005081909090555060026000506001016000505460016000506000600260005060000160005054815260100190815260100160002090506000508190909055505b6100ae565b610184806100bd6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680631677b0ff14610049578063209652551461007657806362eb702a146100a057610043565b60006000fd5b34156100555760006000fd5b61007460048080359060100190919080359060100190919050506100c4565b005b34156100825760006000fd5b61008a610111565b6040518082815260100191505060405180910390f35b34156100ac5760006000fd5b6100c26004808035906010019091905050610123565b005b8160026000506000016000508190909055508060026000506001016000508190909055508082016001600050600084815260100190815260100160002090506000508190909055505b5050565b60006000600050549050610120565b90565b806000600050819090905550600181016001600050600083815260100190815260100160002090506000508190909055505b505600a165627a7a723058205b6e690d70d3703337452467437dc7c4e863ee4ad34b24cc516e2afa71e334700029";
    AionTransaction deployTxFVM = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), ByteUtil.hexStringToBytes(contractCode), 5_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionAddress fvmContract = TxUtil.calculateContractAddress(deployTxFVM);
    transactions.add(deployTxFVM);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Deploy AVM contract.
    AionTransaction deployTxAVM = makeAvmContractCreateTransaction(deployerKey, expectedNonce);
    AionAddress avmContract = TxUtil.calculateContractAddress(deployTxAVM);
    transactions.add(deployTxAVM);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Call FVM contract with code.
    AionTransaction contractCallTx = AionTransaction.create(deployerKey, expectedNonce.toByteArray(), fvmContract, BigInteger.ZERO.toByteArray(), Hex.decode("62eb702a00000000000000000000000000000006"), 2_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    transactions.add(contractCallTx);
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Call AVM contract.
    transactions.add(makeAvmContractCallTransaction(deployerKey, expectedNonce, avmContract));
    expectedNonce = expectedNonce.add(BigInteger.ONE);
    // Process the 4 transactions in a single block.
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // Verify that all transactions were successful.
    assertThat(blockSummary.getSummaries().size()).isEqualTo(4);
    BigInteger expectedBalance = initialBalance;
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        AionTxReceipt receipt = transactionSummary.getReceipt();
        assertThat(receipt.isSuccessful()).isTrue();
        // Compute the expected balance.
        expectedBalance = expectedBalance.subtract(BigInteger.valueOf(receipt.getEnergyUsed()).multiply(BigInteger.valueOf(energyPrice)));
    }
    // Verify that the code and storage of the FVM contract have changed.
    AccountState fvm = (AccountState) blockchain.getRepository().startTracking().getAccountState(fvmContract);
    assertThat(fvm.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(fvm.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
    assertEquals(expectedBalance, getBalance(deployerKey));
    assertEquals(expectedNonce, getNonce(deployerKey));
}
Also used : AionAddress(org.aion.types.AionAddress) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Aggregations

AccountState (org.aion.base.AccountState)71 AionAddress (org.aion.types.AionAddress)54 Test (org.junit.Test)46 AionTransaction (org.aion.base.AionTransaction)40 RepositoryCache (org.aion.base.db.RepositoryCache)38 AionTxExecSummary (org.aion.base.AionTxExecSummary)37 Block (org.aion.zero.impl.types.Block)21 ImportResult (org.aion.zero.impl.core.ImportResult)20 BigInteger (java.math.BigInteger)18 InternalTransaction (org.aion.types.InternalTransaction)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 AionTxReceipt (org.aion.base.AionTxReceipt)11 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)10 ArrayList (java.util.ArrayList)8 InternalVmType (org.aion.base.InternalVmType)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 JSONObject (org.json.JSONObject)4 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)3