Skip to content

Commit

Permalink
Removed some implementation specific functionality from ContractDetails:
Browse files Browse the repository at this point in the history
 - Removed DetailsProvider from configuration (superfluous design).
 - ContractDetailsCacheImpl no longer pretends to implement functionality to fit the interface.
 - modMcf dependency on modDbImpl is no longer required.
  • Loading branch information
AlexandraRoatis committed Sep 5, 2019
1 parent c95e65b commit 3aa31bf
Show file tree
Hide file tree
Showing 25 changed files with 56 additions and 315 deletions.
3 changes: 1 addition & 2 deletions modAionImpl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ dependencies {
compile project(':modLogger')
compile project(':modRlp')
compile project(':modCrypto')


compile project(':modDbImpl')
compile project(':modMcf')
compile project(':modP2pImpl')
compile project(':modP2p')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.aion.mcf.blockchain.Block;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.mcf.config.CfgPrune;
import org.aion.mcf.db.ContractDetails;
import org.aion.mcf.db.InternalVmType;
import org.aion.mcf.config.PruneConfig;
import org.aion.mcf.db.RepositoryCache;
Expand All @@ -40,7 +39,6 @@
import org.aion.zero.impl.core.energy.TargetStrategy;
import org.aion.zero.impl.db.AionContractDetailsImpl;
import org.aion.zero.impl.db.AionRepositoryImpl;
import org.aion.zero.impl.db.ContractDetailsAion;
import org.aion.zero.impl.sync.DatabaseType;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.AionBlockSummary;
Expand Down Expand Up @@ -73,11 +71,6 @@ public PruneConfig getPruneConfig() {
return new CfgPrune(false);
}

@Override
public ContractDetails contractDetailsImpl() {
return ContractDetailsAion.createForTesting(0, 1000000).getDetails();
}

@Override
public Properties getDatabaseConfig(String db_name) {
Properties props = new Properties();
Expand Down Expand Up @@ -269,11 +262,6 @@ public PruneConfig getPruneConfig() {
return new CfgPrune(false);
}

@Override
public ContractDetails contractDetailsImpl() {
return ContractDetailsAion.createForTesting(0, 1000000).getDetails();
}

@Override
public Properties getDatabaseConfig(String db_name) {
Properties props = new Properties();
Expand Down
16 changes: 5 additions & 11 deletions modAionImpl/src/org/aion/zero/impl/db/AbstractContractDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public abstract class AbstractContractDetails implements ContractDetails {
private boolean dirty = false;
private boolean deleted = false;

protected int prune;
protected int detailsInMemoryStorageLimit;
// a value > 0 indicates that prune should be for that many blocks.
protected int prune = 0;
// indicates the maximum storage size before shifting to the storage database
// NOTE: updating this value can lead to incompatible data storage
protected int detailsInMemoryStorageLimit = 64 * 1024;

private Map<ByteArrayWrapper, byte[]> codes = new HashMap<>();
protected byte[] transformedCode;
Expand All @@ -31,15 +34,6 @@ public abstract class AbstractContractDetails implements ContractDetails {
// using the default transaction type to specify undefined VM
protected InternalVmType vmType = InternalVmType.EITHER;

protected AbstractContractDetails() {
this(0, 64 * 1024);
}

protected AbstractContractDetails(int prune, int memStorageLimit) {
this.prune = prune;
this.detailsInMemoryStorageLimit = memStorageLimit;
}

@Override
public byte[] getCode() {
return codes.size() == 0 ? EMPTY_BYTE_ARRAY : codes.values().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected void initializeDatabasesAndCaches() throws InvalidFilePathException {

// Setup the cache for transaction data source.
this.detailsDS =
new DetailsDataStore(detailsDatabase, storageDatabase, graphDatabase, LOG, cfg);
new DetailsDataStore(detailsDatabase, storageDatabase, graphDatabase, LOG);

// pruning config
pruneEnabled = this.cfg.getPruneConfig().isEnabled();
Expand Down
45 changes: 30 additions & 15 deletions modAionImpl/src/org/aion/zero/impl/db/AionContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Optional;
import org.aion.db.impl.ByteArrayKeyValueStore;
import org.aion.db.store.XorDataSource;
import org.aion.mcf.db.ContractDetails;
import org.aion.mcf.db.InternalVmType;
import org.aion.zero.impl.trie.SecureTrie;
import org.aion.precompiled.ContractInfo;
Expand Down Expand Up @@ -47,8 +46,22 @@ public class AionContractDetailsImpl extends AbstractContractDetails {

public AionContractDetailsImpl() {}

public AionContractDetailsImpl(int prune, int memStorageLimit) {
super(prune, memStorageLimit);
@VisibleForTesting
AionContractDetailsImpl(int prune, int memStorageLimit) {
this.prune = prune;
// NOTE: updating this value can lead to incompatible data storage
this.detailsInMemoryStorageLimit = memStorageLimit;
}

/**
* Creates a object with attached database access for the storage and object graph.
*
* @param storageSource
* @param objectGraphSource
*/
public AionContractDetailsImpl(ByteArrayKeyValueStore storageSource, ByteArrayKeyValueStore objectGraphSource) {
this.dataSource = storageSource;
this.objectGraphSource = objectGraphSource;
}

private AionContractDetailsImpl(
Expand Down Expand Up @@ -201,20 +214,18 @@ private byte[] computeAvmStorageHash() {
* data is interpreted during decoding differs for AVM contracts, therefore it will be
* decoded incorrectly if the VM type is not set before making this method call.
*/
@Override
public void decode(byte[] rlpCode) {
// TODO: remove vm type requirement when refactoring into separate AVM & FVM implementations
decode(rlpCode, false);
}

/**
* Decodes an AionContractDetailsImpl object from the RLP encoding rlpCode with fast check does
* the contractDetails needs external storage.
* Decodes an AionContractDetailsImpl object from the RLP encoding rlpCode. The fast check flag
* indicates whether the contractDetails needs to sync with external storage.
*
* @param rlpCode The encoding to decode.
* @param fastCheck set fastCheck option.
* @param fastCheck indicates whether the contractDetails needs to sync with external storage.
*/
@Override
public void decode(byte[] rlpCode, boolean fastCheck) {
RLPList data = RLP.decode2(rlpCode);

Expand Down Expand Up @@ -340,7 +351,6 @@ public void decodeStorage(RLPElement root, RLPElement storage, boolean keepStora
*
* @return an rlp encoding of this.
*/
@Override
public byte[] getEncoded() {
if (rlpEncoded == null) {

Expand Down Expand Up @@ -431,15 +441,19 @@ public void syncStorage() {
}

/**
* Sets the data source to dataSource.
* Sets the storage data source.
*
* @param dataSource The new dataSource.
*/
public void setDataSource(ByteArrayKeyValueStore dataSource) {
this.dataSource = dataSource;
}

@Override
/**
* Sets the data source for storing the AVM object graph.
*
* @param objectGraphSource the new data source used for storing the object graph
*/
public void setObjectGraphSource(ByteArrayKeyValueStore objectGraphSource) {
this.objectGraphSource = objectGraphSource;
}
Expand Down Expand Up @@ -495,13 +509,14 @@ void setExternalStorageDataSource(ByteArrayKeyValueStore dataSource) {

/**
* Returns an AionContractDetailsImpl object pertaining to a specific point in time given by the
* root hash hash.
* storage root hash.
*
* @param hash The root hash to search for.
* @param hash the storage root hash to search for
* @param vm used to direct the interpretation of the storage root hash, since AVM contracts
* also include the hash of the object graph.
* @return the specified AionContractDetailsImpl.
*/
@Override
public ContractDetails getSnapshotTo(byte[] hash, InternalVmType vm) {
public AionContractDetailsImpl getSnapshotTo(byte[] hash, InternalVmType vm) {
// set the VM type using the code hash
vmType = vm;

Expand Down
14 changes: 7 additions & 7 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void updateBatch(
ContractDetailsCacheImpl contractDetailsCache =
(ContractDetailsCacheImpl) contractDetails;
if (contractDetailsCache.origContract == null) {
contractDetailsCache.origContract = this.cfg.contractDetailsImpl();
contractDetailsCache.origContract = new AionContractDetailsImpl();

try {
contractDetailsCache.origContract.setAddress(address);
Expand All @@ -185,7 +185,8 @@ public void updateBatch(

contractDetails = contractDetailsCache.origContract;

updateContractDetails(address, contractDetails);
// this method requires the encoding functionality therefore can be applied only to AionContractDetailsImpl
updateContractDetails(address, (AionContractDetailsImpl) contractDetails);

// TODO: incorrect check codeHash != trie hash
if (!Arrays.equals(accountState.getCodeHash(), EMPTY_TRIE_HASH)) {
Expand Down Expand Up @@ -225,7 +226,7 @@ public void updateBatch(

/** @implNote The method calling this method must handle the locking. */
private void updateContractDetails(
final AionAddress address, final ContractDetails contractDetails) {
final AionAddress address, final AionContractDetailsImpl contractDetails) {
// locked by calling method
detailsDS.update(address, contractDetails);

Expand Down Expand Up @@ -451,16 +452,16 @@ private void updateAccountState(AionAddress address, AccountState accountState)
/**
* @inheritDoc
* @implNote Any other method calling this can rely on the fact that the contract details
* returned is a newly created object by {@link ContractDetails#getSnapshotTo(byte[],
* returned is a newly created object by {@link AionContractDetailsImpl#getSnapshotTo(byte[],
* InternalVmType)}. Since this querying method it locked, the methods calling it <b>may not
* need to be locked or synchronized</b>, depending on the specific use case.
*/
@Override
public ContractDetails getContractDetails(AionAddress address) {
public AionContractDetailsImpl getContractDetails(AionAddress address) {
rwLock.readLock().lock();

try {
ContractDetails details;
AionContractDetailsImpl details;

// That part is important cause if we have
// to sync details storage according the trie root
Expand Down Expand Up @@ -1214,7 +1215,6 @@ private static class AionRepositoryImplHolder {
new AionRepositoryImpl(
new RepositoryConfigImpl(
config.getDatabasePath(),
ContractDetailsAion.getInstance(),
config.getDb()),
10);
}
Expand Down
58 changes: 0 additions & 58 deletions modAionImpl/src/org/aion/zero/impl/db/ContractDetailsAion.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,6 @@ public byte[] getStorageHash() {
return origContract == null ? null : origContract.getStorageHash();
}

/** This method is not supported. */
@Override
public void decode(byte[] rlpCode) {
throw new RuntimeException("Not supported by this implementation.");
}

/** This method is not supported. */
@Override
public void decode(byte[] rlpCode, boolean fastCheck) {
throw new RuntimeException("Not supported by this implementation.");
}

/** This method is not supported. */
@Override
public byte[] getEncoded() {
throw new RuntimeException("Not supported by this implementation.");
}

/**
* Get the address associated with this ContractDetailsCacheImpl.
*
Expand Down Expand Up @@ -252,24 +234,6 @@ public void commit() {
origContract.setDirty(this.isDirty() || origContract.isDirty());
}

/** This method is not supported. */
@Override
public ContractDetails getSnapshotTo(byte[] hash, InternalVmType vm) {
throw new UnsupportedOperationException("No snapshot option during cache state");
}

/** This method is not supported. */
@Override
public void setDataSource(ByteArrayKeyValueStore dataSource) {
throw new UnsupportedOperationException("Can't set datasource in cache implementation.");
}

/** This method is not supported. */
@Override
public void setObjectGraphSource(ByteArrayKeyValueStore objectGraphSource) {
throw new UnsupportedOperationException("Can't set datasource in cache implementation.");
}

/**
* Returns a sufficiently deep copy of this contract details object.
*
Expand Down
3 changes: 1 addition & 2 deletions modAionImpl/src/org/aion/zero/impl/db/DBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.aion.mcf.config.CfgDb;
import org.aion.base.AccountState;
import org.aion.zero.impl.core.ImportResult;
import org.aion.mcf.db.ContractDetails;
import org.aion.mcf.db.IBlockStoreBase;
import org.aion.mcf.db.Repository;
import org.aion.mcf.db.RepositoryCache;
Expand Down Expand Up @@ -248,7 +247,7 @@ public static void dumpTestData(long blockNumber, String[] otherParameters) {
writer.append("Contract: " + AddressUtils.wrapAddress(otherParameters[i]));
writer.newLine();

ContractDetails details =
AionContractDetailsImpl details =
repository.getContractDetails(
AddressUtils.wrapAddress(otherParameters[i]));

Expand Down
Loading

0 comments on commit 3aa31bf

Please sign in to comment.