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);
}
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();
}
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();
}
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);
}
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));
}
}
Aggregations