Search in sources :

Example 1 with TransformedCodeInfoInterface

use of org.aion.base.db.TransformedCodeInfoInterface in project aion by aionnetwork.

the class AionRepositoryImpl method updateBatch.

@Override
public void updateBatch(Map<AionAddress, AccountState> stateCache, Map<AionAddress, ContractDetail> detailsCache, Map<AionAddress, TransformedCodeInfoInterface> transformedCodeCache) {
    rwLock.writeLock().lock();
    try {
        for (Map.Entry<AionAddress, AccountState> entry : stateCache.entrySet()) {
            AionAddress address = entry.getKey();
            AccountState accountState = entry.getValue();
            ContractDetails contractDetails = (ContractDetails) detailsCache.get(address);
            if (accountState.isDeleted()) {
                // TODO-A: batch operations here
                try {
                    worldState.delete(address.toByteArray());
                } catch (Exception e) {
                    LOG.error("key deleted exception [{}]", e.toString());
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("key deleted <key={}>", Hex.toHexString(address.toByteArray()));
                }
            } else {
                if (!contractDetails.isDirty() || (contractDetails.getVmType() == InternalVmType.EITHER && !ContractInfo.isPrecompiledContract(address))) {
                    // ContractState class
                    if (accountState.isDirty()) {
                        updateAccountState(address, accountState);
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("update: [{}],nonce: [{}] balance: [{}] [{}]", Hex.toHexString(address.toByteArray()), accountState.getNonce(), accountState.getBalance(), Hex.toHexString(contractDetails.getStorageHash()));
                        }
                    }
                    continue;
                }
                InnerContractDetails contractDetailsCache = (InnerContractDetails) contractDetails;
                if (contractDetailsCache.origContract == null) {
                    contractDetailsCache.origContract = detailsDS.newContractDetails(address, contractDetailsCache.getVmType());
                    contractDetailsCache.commit();
                }
                StoredContractDetails parentDetails = (StoredContractDetails) contractDetailsCache.origContract;
                // this method requires the encoding functionality therefore can be applied only to StoredContractDetails
                detailsDS.update(address, parentDetails);
                accountState.setStateRoot(parentDetails.getStorageHash());
                updateAccountState(address, accountState);
                cachedContractIndex.put(address, Pair.of(ByteArrayWrapper.wrap(accountState.getCodeHash()), parentDetails.getVmType()));
                if (LOG.isTraceEnabled()) {
                    LOG.trace("update: [{}],nonce: [{}] balance: [{}] [{}]", Hex.toHexString(address.toByteArray()), accountState.getNonce(), accountState.getBalance(), Hex.toHexString(parentDetails.getStorageHash()));
                }
            }
        }
        for (Map.Entry<AionAddress, TransformedCodeInfoInterface> entry : transformedCodeCache.entrySet()) {
            for (Map.Entry<ByteArrayWrapper, Map<Integer, byte[]>> infoMap : ((TransformedCodeInfo) entry.getValue()).transformedCodeMap.entrySet()) {
                for (Map.Entry<Integer, byte[]> innerEntry : infoMap.getValue().entrySet()) {
                    setTransformedCode(entry.getKey(), infoMap.getKey().toBytes(), innerEntry.getKey(), innerEntry.getValue());
                }
            }
        }
        LOG.trace("updated: detailsCache.size: {}", detailsCache.size());
        stateCache.clear();
        detailsCache.clear();
        transformedCodeCache.clear();
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : TransformedCodeInfoInterface(org.aion.base.db.TransformedCodeInfoInterface) AionAddress(org.aion.types.AionAddress) AccountState(org.aion.base.AccountState) IOException(java.io.IOException) BigInteger(java.math.BigInteger) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AccountState (org.aion.base.AccountState)1 TransformedCodeInfoInterface (org.aion.base.db.TransformedCodeInfoInterface)1 AionAddress (org.aion.types.AionAddress)1 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)1