Search in sources :

Example 66 with AccountState

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

the class AionRepositoryCacheTest method testLoadAccountState_withExistingAccountFromCache.

@Test
public void testLoadAccountState_withExistingAccountFromCache() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] code = RandomUtils.nextBytes(100);
    byte[] codeHash = h256(code);
    // initialize contract in the repository
    RepositoryCache tempCache = repository.startTracking();
    tempCache.createAccount(address);
    tempCache.saveCode(address, code);
    tempCache.saveVmType(address, InternalVmType.FVM);
    tempCache.addBalance(address, BigInteger.ONE);
    tempCache.flushTo(repository, true);
    // update the contract in the cache without flushing
    cache.addBalance(address, BigInteger.ONE);
    ByteArrayWrapper store = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
    cache.addStorageRow(address, store, store);
    AionRepositoryCache tracker = (AionRepositoryCache) cache.startTracking();
    assertThat(tracker.hasAccountState(address)).isTrue();
    assertThat(tracker.hasContractDetails(address)).isTrue();
    assertThat(tracker.cachedAccounts.containsKey(address)).isFalse();
    assertThat(tracker.cachedDetails.containsKey(address)).isFalse();
    // load account state for new address
    AccountState account = tracker.getAccountState(address);
    InnerContractDetails details = (InnerContractDetails) tracker.getContractDetails(address);
    // 1. Ensure new account and details were created
    assertThat(tracker.cachedAccounts.containsKey(address)).isTrue();
    assertThat(tracker.cachedDetails.containsKey(address)).isTrue();
    assertThat(account.getBalance()).isEqualTo(BigInteger.TWO);
    assertThat(account.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(account.getCodeHash()).isEqualTo(codeHash);
    assertThat(account.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(details.origContract).isNotNull();
    assertThat(details.getCode(codeHash)).isEqualTo(code);
    assertThat(details.get(store)).isEqualTo(store);
    assertThat(details.getVmType()).isEqualTo(InternalVmType.FVM);
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.isDirty()).isFalse();
    // 2. Ensure that the cache contains the loaded account and details
    assertThat(cache.cachedAccounts.containsKey(address)).isTrue();
    assertThat(cache.cachedDetails.containsKey(address)).isTrue();
    AccountState accountCache = cache.getAccountState(address);
    InnerContractDetails detailsCache = (InnerContractDetails) cache.getContractDetails(address);
    assertThat(accountCache.getBalance()).isEqualTo(BigInteger.TWO);
    assertThat(detailsCache.get(store)).isEqualTo(store);
    // 3. Ensure that the repository contains the old account and details
    assertThat(repository.hasAccountState(address)).isTrue();
    assertThat(repository.hasContractDetails(address)).isTrue();
    AccountState accountRepo = repository.getAccountState(address);
    ContractDetails detailsRepo = repository.getContractDetails(address);
    assertThat(accountRepo.getBalance()).isEqualTo(BigInteger.ONE);
    assertThat(detailsRepo.get(store)).isNull();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RepositoryCache(org.aion.base.db.RepositoryCache) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Example 67 with AccountState

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

the class AionRepositoryCacheTest method testLoadAccountState_withNewAccount.

@Test
public void testLoadAccountState_withNewAccount() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    AionRepositoryCache tracker = (AionRepositoryCache) cache.startTracking();
    assertThat(tracker.hasAccountState(address)).isFalse();
    assertThat(tracker.hasContractDetails(address)).isFalse();
    assertThat(tracker.cachedAccounts.containsKey(address)).isFalse();
    assertThat(tracker.cachedDetails.containsKey(address)).isFalse();
    // load account state for new address
    AccountState account = tracker.getAccountState(address);
    InnerContractDetails details = (InnerContractDetails) tracker.getContractDetails(address);
    // 1. Ensure new account and details were created
    assertThat(tracker.cachedAccounts.containsKey(address)).isTrue();
    assertThat(tracker.cachedDetails.containsKey(address)).isTrue();
    assertThat(account.isEmpty()).isTrue();
    assertThat(account.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(details.origContract).isNull();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.isDirty()).isFalse();
    // 2. Ensure that the cache does not contain the new account and details
    assertThat(cache.cachedAccounts.containsKey(address)).isFalse();
    assertThat(cache.cachedDetails.containsKey(address)).isFalse();
    // 3. Ensure that the repository does not contain the new account and details
    assertThat(repository.hasAccountState(address)).isFalse();
    assertThat(repository.hasContractDetails(address)).isFalse();
}
Also used : AionAddress(org.aion.types.AionAddress) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Example 68 with AccountState

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

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion1.

@Test
public void deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion1() throws VmFatalException {
    AvmTestConfig.clearConfigurations();
    AvmTestConfig.supportOnlyAvmVersion1();
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version without required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default balance value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.createAccount(internalContract);
    cache.addBalance(internalContract, BigInteger.TEN);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 69 with AccountState

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

the class GenesisSpecificationTest method defaultGenesisBlockTest.

/**
 * Test that the default genesis block built from the builder produces the correct genesis specs
 */
@Test
public void defaultGenesisBlockTest() {
    AionGenesis.Builder genesisBuilder = new AionGenesis.Builder();
    AionGenesis genesis = genesisBuilder.buildForTest();
    assertThat(genesis.getParentHash()).isEqualTo(AionGenesis.GENESIS_PARENT_HASH);
    assertThat(genesis.getCoinbase()).isEqualTo(AionGenesis.GENESIS_COINBASE);
    assertThat(genesis.getDifficulty()).isEqualTo(AionGenesis.GENESIS_DIFFICULTY);
    assertThat(genesis.getDifficultyBI()).isEqualTo(new BigInteger(1, AionGenesis.GENESIS_DIFFICULTY));
    assertThat(genesis.getLogBloom()).isEqualTo(AionGenesis.GENESIS_LOGSBLOOM);
    assertThat(genesis.getTimestamp()).isEqualTo(AionGenesis.GENESIS_TIMESTAMP);
    assertThat(genesis.getNrgConsumed()).isEqualTo(0);
    assertThat(genesis.getNrgLimit()).isEqualTo(AionGenesis.GENESIS_ENERGY_LIMIT);
    assertThat(genesis.getTxTrieRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getReceiptsRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getDifficultyBI()).isEqualTo(new BigInteger(1, AionGenesis.GENESIS_DIFFICULTY));
    assertThat(genesis.getTransactionsList().isEmpty()).isEqualTo(true);
    Map<AionAddress, AccountState> premined = genesis.getPremine();
    Set<AionAddress> keySet = premined.keySet();
    // default set
    Set<AionAddress> defaultKeySet = AionGenesis.GENESIS_PREMINE.keySet();
    assertThat(defaultKeySet.equals(keySet)).isEqualTo(true);
}
Also used : AionAddress(org.aion.types.AionAddress) BigInteger(java.math.BigInteger) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Example 70 with AccountState

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

the class ExternalStateForAvm method getVmType.

private InternalVmType getVmType(AionAddress destination) {
    // will load contract into memory otherwise leading to consensus issues
    RepositoryCache track = repositoryCache.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 = repositoryCache.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)

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