Search in sources :

Example 21 with AccountState

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

the class AionRepositoryCache method flush.

/**
 * @implNote To maintain intended functionality this method does not call
 *           the parent's {@code flush()} method. The changes are propagated
 *           to the parent through calling the parent's
 *           {@code updateBatch()} method.
 */
@Override
public synchronized void flush() {
    // determine which accounts should get stored
    HashMap<Address, AccountState> cleanedCacheAccounts = new HashMap<>();
    for (Map.Entry<Address, AccountState> entry : cachedAccounts.entrySet()) {
        AccountState account = entry.getValue();
        if (account != null && account.isDirty() && account.isEmpty()) {
            // ignore contract state for empty accounts at storage
            cachedDetails.remove(entry.getKey());
        } else {
            cleanedCacheAccounts.put(entry.getKey(), entry.getValue());
        }
    }
    // determine which contracts should get stored
    for (Map.Entry<Address, IContractDetails<DataWord>> entry : cachedDetails.entrySet()) {
        IContractDetails<DataWord> ctd = entry.getValue();
        // different ContractDetails implementation
        if (ctd != null && ctd instanceof ContractDetailsCacheImpl) {
            ContractDetailsCacheImpl contractDetailsCache = (ContractDetailsCacheImpl) ctd;
            contractDetailsCache.commit();
            if (contractDetailsCache.origContract == null && repository.hasContractDetails(entry.getKey())) {
                // in forked block the contract account might not exist thus
                // it is created without
                // origin, but on the main chain details can contain data
                // which should be merged
                // into a single storage trie so both branches with
                // different stateRoots are valid
                contractDetailsCache.origContract = repository.getContractDetails(entry.getKey());
                contractDetailsCache.commit();
            }
        }
    }
    repository.updateBatch(cleanedCacheAccounts, cachedDetails);
    cachedAccounts.clear();
    cachedDetails.clear();
}
Also used : IContractDetails(org.aion.base.db.IContractDetails) Address(org.aion.base.type.Address) HashMap(java.util.HashMap) ContractDetailsCacheImpl(org.aion.mcf.db.ContractDetailsCacheImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState) Map(java.util.Map) HashMap(java.util.HashMap)

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