Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repository and contract details clean up #1117

Merged
merged 18 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 136 additions & 130 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryCache.java

Large diffs are not rendered by default.

47 changes: 7 additions & 40 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,40 +172,31 @@ public void updateBatch(
continue;
}

ContractDetailsCacheImpl contractDetailsCache =
(ContractDetailsCacheImpl) contractDetails;
InnerContractDetails contractDetailsCache =
(InnerContractDetails) contractDetails;
if (contractDetailsCache.origContract == null) {
contractDetailsCache.origContract = detailsDS.newContractDetails(address, contractDetailsCache.getVmType());
contractDetailsCache.commit();
}

contractDetails = contractDetailsCache.origContract;
StoredContractDetails parentDetails = (StoredContractDetails) contractDetailsCache.origContract;

// this method requires the encoding functionality therefore can be applied only to StoredContractDetails
detailsDS.update(address, (StoredContractDetails) contractDetails);
detailsDS.update(address, parentDetails);

// TODO: incorrect check codeHash != trie hash
if (!Arrays.equals(accountState.getCodeHash(), ConstantUtil.EMPTY_TRIE_HASH)) {
accountState.setStateRoot(contractDetails.getStorageHash());
}
accountState.setStateRoot(parentDetails.getStorageHash());

updateAccountState(address, accountState);

if (contractDetails.getVmType().isContract()) {
cachedContractIndex.put(
contractDetails.getAddress(),
Pair.of(
ByteArrayWrapper.wrap(accountState.getCodeHash()),
contractDetails.getVmType()));
}
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(contractDetails.getStorageHash()));
Hex.toHexString(parentDetails.getStorageHash()));
}
}
}
Expand Down Expand Up @@ -531,30 +522,6 @@ public boolean hasAccountState(AionAddress address) {
return getAccountState(address) != null;
}

/**
* @implNote The loaded objects are fresh copies of the original account state and contract
* details.
*/
@Override
public void loadAccountState(
AionAddress address,
Map<AionAddress, AccountState> cacheAccounts,
Map<AionAddress, ContractDetails> cacheDetails) {

AccountState account = getAccountState(address);
ContractDetails details;
if (account != null) {
account = new AccountState(account);
details = new ContractDetailsCacheImpl(getContractDetails(address));
} else {
account = new AccountState();
details = new ContractDetailsCacheImpl(null);
}

cacheAccounts.put(address, account);
cacheDetails.put(address, details);
}

@Override
public byte[] getRoot() {
rwLock.readLock().lock();
Expand Down
49 changes: 1 addition & 48 deletions modAionImpl/src/org/aion/zero/impl/db/AvmContractDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class AvmContractDetails implements StoredContractDetails {
// identifies the contract
private final AionAddress address;
public final AionAddress address;

// external databases used for storage and the object graph
private final ByteArrayKeyValueStore externalStorageSource;
Expand Down Expand Up @@ -193,10 +193,6 @@ public ByteArrayWrapper get(ByteArrayWrapper key) {
: ByteArrayWrapper.wrap(RLP.decode2(data).get(0).getRLPData());
}

public void setVmType(InternalVmType vmType) {
// nothing to do, always AVM
}

public InternalVmType getVmType() {
return InternalVmType.AVM;
}
Expand Down Expand Up @@ -356,17 +352,6 @@ public byte[] getEncoded() {
return RLP.encodeList(rlpAddress, rlpStorageRoot, rlpCode);
}

/**
* Get the address associated with this AvmContractDetails.
*
* @return the associated address.
*/
@Override
public AionAddress getAddress() {
return address;
}

/** Syncs the storage trie. */
@Override
public void syncStorage() {
byte[] graph = getObjectGraph();
Expand All @@ -378,38 +363,6 @@ public void syncStorage() {
storageTrie.sync();
}

/**
* Returns a sufficiently deep copy of this contract details object.
*
* <p>The copy is not completely deep. The following object references will be passed on from
* this object to the copy:
*
* <p>- The external storage data source: the copy will back-end on this same source. - The
* previous root of the trie will pass its original object reference if this root is not of type
* {@code byte[]}. - The current root of the trie will pass its original object reference if
* this root is not of type {@code byte[]}. - Each {@link org.aion.rlp.Value} object reference
* held by each of the {@link Node} objects in the underlying cache.
*
* @return A copy of this object.
*/
@Override
public AvmContractDetails copy() {
AvmContractDetails aionContractDetailsCopy = new AvmContractDetails(this.address, this.externalStorageSource, this.objectGraphSource);

// object graph information
aionContractDetailsCopy.objectGraph = objectGraph; // can pass reference since the class is immutable
aionContractDetailsCopy.objectGraphHash =
Arrays.equals(objectGraphHash, EMPTY_DATA_HASH)
? EMPTY_DATA_HASH
: Arrays.copyOf(this.objectGraphHash, this.objectGraphHash.length);

aionContractDetailsCopy.codes = new HashMap<>(codes);
aionContractDetailsCopy.dirty = this.dirty;
aionContractDetailsCopy.deleted = this.deleted;
aionContractDetailsCopy.storageTrie = (this.storageTrie == null) ? null : this.storageTrie.copy();
return aionContractDetailsCopy;
}

@Override
public String toString() {
StringBuilder ret = new StringBuilder();
Expand Down
43 changes: 1 addition & 42 deletions modAionImpl/src/org/aion/zero/impl/db/FvmContractDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class FvmContractDetails implements StoredContractDetails {

private final ByteArrayKeyValueStore externalStorageSource;

private final AionAddress address;
public final AionAddress address;

private SecureTrie storageTrie;

Expand Down Expand Up @@ -172,10 +172,6 @@ public ByteArrayWrapper get(ByteArrayWrapper key) {
: ByteArrayWrapper.wrap(RLP.decode2(data).get(0).getRLPData());
}

public void setVmType(InternalVmType vmType) {
// nothing to do
}

public InternalVmType getVmType() {
return InternalVmType.FVM;
}
Expand Down Expand Up @@ -283,48 +279,11 @@ public byte[] getEncoded() {
return RLP.encodeList(rlpAddress, rlpStorageRoot, rlpCode);
}

/**
* Get the address associated with this FvmContractDetails.
*
* @return the associated address.
*/
@Override
public AionAddress getAddress() {
return address;
}

/** Syncs the storage trie. */
@Override
public void syncStorage() {
storageTrie.sync();
}

/**
* Returns a sufficiently deep copy of this contract details object.
*
* <p>The copy is not completely deep. The following object references will be passed on from
* this object to the copy:
*
* <p>- The external storage data source: the copy will back-end on this same source. - The
* previous root of the trie will pass its original object reference if this root is not of type
* {@code byte[]}. - The current root of the trie will pass its original object reference if
* this root is not of type {@code byte[]}. - Each {@link org.aion.rlp.Value} object reference
* held by each of the {@link Node} objects in the underlying cache.
*
* @return A copy of this object.
*/
@Override
public FvmContractDetails copy() {
FvmContractDetails aionContractDetailsCopy = new FvmContractDetails(this.address, this.externalStorageSource);

// storage information
aionContractDetailsCopy.codes = new HashMap<>(codes);
aionContractDetailsCopy.dirty = this.dirty;
aionContractDetailsCopy.deleted = this.deleted;
aionContractDetailsCopy.storageTrie = this.storageTrie.copy();
return aionContractDetailsCopy;
}

@Override
public String toString() {
StringBuilder ret = new StringBuilder();
Expand Down
Loading