Search in sources :

Example 6 with AccountState

use of org.aion.mcf.core.AccountState in project aion by aionnetwork.

the class AionRepositoryDummy method loadAccount.

public void loadAccount(Address addr, HashMap<ByteArrayWrapper, AccountState> cacheAccounts, HashMap<ByteArrayWrapper, IContractDetails<DataWord>> cacheDetails) {
    AccountState account = getAccountState(addr);
    IContractDetails<DataWord> details = getContractDetails(addr);
    if (account == null) {
        account = new AccountState();
    } else {
        account = new AccountState(account);
    }
    if (details == null) {
        details = this.cfg.contractDetailsImpl();
    } else {
        details = details.clone();
    }
    cacheAccounts.put(addr.toByteArrayWrapper(), account);
    cacheDetails.put(addr.toByteArrayWrapper(), details);
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 7 with AccountState

use of org.aion.mcf.core.AccountState in project aion by aionnetwork.

the class AionRepositoryDummy method increaseNonce.

public BigInteger increaseNonce(Address addr) {
    AccountState account = getAccountState(addr);
    if (account == null) {
        account = createAccount(addr);
    }
    account.incrementNonce();
    worldState.put(addr.toByteArrayWrapper(), account);
    return account.getNonce();
}
Also used : AccountState(org.aion.mcf.core.AccountState)

Example 8 with AccountState

use of org.aion.mcf.core.AccountState in project aion by aionnetwork.

the class AionRepositoryImpl method updateBatch.

@Override
public synchronized void updateBatch(Map<Address, AccountState> stateCache, Map<Address, IContractDetails<DataWord>> detailsCache) {
    for (Map.Entry<Address, AccountState> entry : stateCache.entrySet()) {
        Address address = entry.getKey();
        AccountState accountState = entry.getValue();
        IContractDetails<DataWord> contractDetails = detailsCache.get(address);
        if (accountState.isDeleted()) {
            // TODO-A: batch operations here
            rwLock.readLock().lock();
            try {
                worldState.delete(address.toBytes());
            } catch (Exception e) {
                LOG.error("key deleted exception [{}]", e.toString());
            } finally {
                rwLock.readLock().unlock();
            }
            LOG.debug("key deleted <key={}>", Hex.toHexString(address.toBytes()));
        } else {
            if (!contractDetails.isDirty()) {
                // ContractState class
                if (accountState.isDirty()) {
                    updateAccountState(address, accountState);
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("update: [{}],nonce: [{}] balance: [{}] [{}]", Hex.toHexString(address.toBytes()), accountState.getNonce(), accountState.getBalance(), contractDetails.getStorage());
                    }
                }
                continue;
            }
            ContractDetailsCacheImpl contractDetailsCache = (ContractDetailsCacheImpl) contractDetails;
            if (contractDetailsCache.origContract == null) {
                contractDetailsCache.origContract = this.cfg.contractDetailsImpl();
                try {
                    contractDetailsCache.origContract.setAddress(address);
                } catch (Exception e) {
                    e.printStackTrace();
                    LOG.error("contractDetailsCache setAddress exception [{}]", e.toString());
                }
                contractDetailsCache.commit();
            }
            contractDetails = contractDetailsCache.origContract;
            updateContractDetails(address, contractDetails);
            if (!Arrays.equals(accountState.getCodeHash(), EMPTY_TRIE_HASH)) {
                accountState.setStateRoot(contractDetails.getStorageHash());
            }
            updateAccountState(address, accountState);
            if (LOG.isTraceEnabled()) {
                LOG.trace("update: [{}],nonce: [{}] balance: [{}] [{}]", Hex.toHexString(address.toBytes()), accountState.getNonce(), accountState.getBalance(), contractDetails.getStorage());
            }
        }
    }
    LOG.trace("updated: detailsCache.size: {}", detailsCache.size());
    stateCache.clear();
    detailsCache.clear();
}
Also used : Address(org.aion.base.type.Address) ContractDetailsCacheImpl(org.aion.mcf.db.ContractDetailsCacheImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 9 with AccountState

use of org.aion.mcf.core.AccountState in project aion by aionnetwork.

the class AionRepositoryImpl method getCode.

@Override
public synchronized byte[] getCode(Address address) {
    AccountState accountState = getAccountState(address);
    if (accountState == null) {
        return EMPTY_BYTE_ARRAY;
    }
    byte[] codeHash = accountState.getCodeHash();
    IContractDetails<DataWord> details = getContractDetails(address);
    return (details == null) ? EMPTY_BYTE_ARRAY : details.getCode(codeHash);
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 10 with AccountState

use of org.aion.mcf.core.AccountState in project aion by aionnetwork.

the class AbstractRepositoryCache method loadAccountState.

/**
 * @implNote The loaded objects are fresh copies of the locally cached
 * account state and contract details.
 */
@Override
public synchronized void loadAccountState(Address address, Map<Address, AccountState> accounts, Map<Address, IContractDetails<DataWord>> details) {
    // check if the account is cached locally
    AccountState accountState = this.cachedAccounts.get(address);
    IContractDetails<DataWord> contractDetails = this.cachedDetails.get(address);
    // when account not cached load from repository
    if (accountState == null) {
        // load directly to the caches given as parameters
        repository.loadAccountState(address, accounts, details);
    } else {
        // copy the objects if they were cached locally
        accounts.put(address, new AccountState(accountState));
        details.put(address, new ContractDetailsCacheImpl(contractDetails));
    }
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Aggregations

AccountState (org.aion.mcf.core.AccountState)21 DataWord (org.aion.mcf.vm.types.DataWord)10 BigInteger (java.math.BigInteger)4 Address (org.aion.base.type.Address)4 ContractDetailsCacheImpl (org.aion.mcf.db.ContractDetailsCacheImpl)3 AionGenesis (org.aion.zero.impl.AionGenesis)2 Test (org.junit.Test)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IContractDetails (org.aion.base.db.IContractDetails)1 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1