Search in sources :

Example 26 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AionRepositoryImpl method loadAccountState.

/**
 * @implNote The loaded objects are fresh copies of the original account
 *           state and contract details.
 */
@Override
public synchronized void loadAccountState(Address address, Map<Address, AccountState> cacheAccounts, Map<Address, IContractDetails<DataWord>> cacheDetails) {
    AccountState account = getAccountState(address);
    IContractDetails<DataWord> details = getContractDetails(address);
    account = (account == null) ? new AccountState() : new AccountState(account);
    details = new ContractDetailsCacheImpl(details);
    // details.setAddress(addr);
    cacheAccounts.put(address, account);
    cacheDetails.put(address, details);
}
Also used : ContractDetailsCacheImpl(org.aion.mcf.db.ContractDetailsCacheImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 27 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AionRepositoryImpl method getContractDetails.

@Override
public synchronized IContractDetails<DataWord> getContractDetails(Address address) {
    rwLock.readLock().lock();
    try {
        // That part is important cause if we have
        // to sync details storage according the trie root
        // saved in the account
        AccountState accountState = getAccountState(address);
        byte[] storageRoot = EMPTY_TRIE_HASH;
        if (accountState != null) {
            storageRoot = getAccountState(address).getStateRoot();
        }
        IContractDetails<DataWord> details = detailsDS.get(address.toBytes());
        if (details != null) {
            details = details.getSnapshotTo(storageRoot);
        }
        return details;
    } finally {
        rwLock.readLock().unlock();
    }
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 28 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AionContractDetailsImpl method get.

@Override
public DataWord get(DataWord key) {
    DataWord result = DataWord.ZERO;
    byte[] data = storageTrie.get(key.getData());
    if (data.length > 0) {
        byte[] dataDecoded = RLP.decode2(data).get(0).getRLPData();
        result = new DataWord(dataDecoded);
    }
    return result;
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord)

Example 29 with DataWord

use of org.aion.mcf.vm.types.DataWord 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

DataWord (org.aion.mcf.vm.types.DataWord)29 Address (org.aion.base.type.Address)11 AccountState (org.aion.mcf.core.AccountState)11 Test (org.junit.Test)9 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)5 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)5 HashMap (java.util.HashMap)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepositoryCache (org.aion.base.db.IRepositoryCache)3 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)3 ContractDetailsCacheImpl (org.aion.mcf.db.ContractDetailsCacheImpl)3 IContractDetails (org.aion.base.db.IContractDetails)2 RLPElement (org.aion.rlp.RLPElement)2 RLPList (org.aion.rlp.RLPList)2 AionTransaction (org.aion.zero.types.AionTransaction)2 BigInteger (java.math.BigInteger)1 Map (java.util.Map)1 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)1 IEvent (org.aion.evtmgr.IEvent)1 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)1