Skip to content

Commit

Permalink
feature: adding the graphDatabase for storing the object graph
Browse files Browse the repository at this point in the history
bug fix: closing the contractIndexDatabase
  • Loading branch information
AlexandraRoatis committed Apr 4, 2019
1 parent a143239 commit e9396fa
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public abstract class AbstractContractDetails implements ContractDetails {
protected int detailsInMemoryStorageLimit;

private Map<ByteArrayWrapper, byte[]> codes = new HashMap<>();
// classes extending this rely on this value starting off as null
protected byte[] objectGraph = null;

// using the default transaction type to specify undefined VM
protected byte vmType = TransactionTypes.DEFAULT;
Expand Down
299 changes: 189 additions & 110 deletions modAionImpl/src/org/aion/zero/impl/db/AionContractDetailsImpl.java

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,17 @@ public void close() {
LOGGEN.error("Exception occurred while closing the details data source.", e);
}

try {
if (contractIndexDatabase != null) {
contractIndexDatabase.close();
LOGGEN.info("contractIndexDatabase store closed.");
contractIndexDatabase = null;
}
} catch (Exception e) {
LOGGEN.error(
"Exception occurred while closing the pendingTxCacheDatabase store.", e);
}

try {
if (stateDatabase != null) {
stateDatabase.close();
Expand Down
62 changes: 44 additions & 18 deletions modAionImpl/src/org/aion/zero/impl/db/ContractDetailsCacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public ContractDetailsCacheImpl(ContractDetails origContract) {
public static ContractDetailsCacheImpl copy(ContractDetailsCacheImpl cache) {
ContractDetailsCacheImpl copy = new ContractDetailsCacheImpl(cache.origContract);
copy.setCodes(new HashMap<>(cache.getCodes()));
copy.vmType = cache.vmType;
if (cache.objectGraph != null) {
copy.objectGraph = Arrays.copyOf(cache.objectGraph, cache.objectGraph.length);
}
copy.storage = new HashMap<>(cache.storage);
copy.setDirty(cache.isDirty());
copy.setDeleted(cache.isDeleted());
Expand Down Expand Up @@ -106,15 +110,22 @@ public byte getVmType() {

@Override
public byte[] getObjectGraph() {
return new byte[0];
if (objectGraph == null) {
if (origContract == null) {
return null;
} else {
objectGraph = origContract.getObjectGraph();
}
}
return objectGraph;
}

@Override
public void setObjectGraph(byte[] graph) {}
public void setObjectGraph(byte[] graph) {
Objects.requireNonNull(graph);

@Override
public void setObjectGraphSource(ByteArrayKeyValueStore objectGraphSource) {
// TODO
objectGraph = graph;
setDirty(true);
}

/**
Expand All @@ -124,7 +135,6 @@ public void setObjectGraphSource(ByteArrayKeyValueStore objectGraphSource) {
*/
@Override
public byte[] getStorageHash() {
// TODO: apply changes resulting from AVM switch
return origContract == null ? null : origContract.getStorageHash();
}

Expand Down Expand Up @@ -177,16 +187,23 @@ public void syncStorage() {
}

/**
* Puts all of the key-value pairs in this ContractDetailsCacheImple into the original contract
* injected into this class' constructor, transfers over any code and sets the original contract
* to dirty only if it already is dirty or if this class is dirty, otherwise sets it as clean.
* Puts all of the key-value pairs and object graph from this ContractDetailsCacheImpl into the
* original contract injected into this class' constructor, transfers over any code and sets the
* original contract to dirty only if it already is dirty or if this class is dirty, otherwise
* sets it as clean.
*/
public void commit() {

if (origContract == null) {
return;
}

// passing on the object graph
if (objectGraph != null) {
origContract.setObjectGraph(objectGraph);
}

// passing on the storage keys
for (ByteArrayWrapper key : storage.keySet()) {
ByteArrayWrapper value = storage.get(key);
if (value != null) {
Expand Down Expand Up @@ -216,6 +233,12 @@ 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 Expand Up @@ -244,15 +267,18 @@ public ContractDetailsCacheImpl copy() {

ContractDetails originalContractCopy =
(this.origContract == null) ? null : this.origContract.copy();
ContractDetailsCacheImpl contractDetailsCacheCopy =
new ContractDetailsCacheImpl(originalContractCopy);
contractDetailsCacheCopy.storage = getDeepCopyOfStorage();
contractDetailsCacheCopy.prune = this.prune;
contractDetailsCacheCopy.detailsInMemoryStorageLimit = this.detailsInMemoryStorageLimit;
contractDetailsCacheCopy.setCodes(getDeepCopyOfCodes());
contractDetailsCacheCopy.setDirty(this.isDirty());
contractDetailsCacheCopy.setDeleted(this.isDeleted());
return contractDetailsCacheCopy;
ContractDetailsCacheImpl copy = new ContractDetailsCacheImpl(originalContractCopy);
copy.vmType = this.vmType;
if (this.objectGraph != null) {
copy.objectGraph = Arrays.copyOf(this.objectGraph, this.objectGraph.length);
}
copy.storage = getDeepCopyOfStorage();
copy.prune = this.prune;
copy.detailsInMemoryStorageLimit = this.detailsInMemoryStorageLimit;
copy.setCodes(getDeepCopyOfCodes());
copy.setDirty(this.isDirty());
copy.setDeleted(this.isDeleted());
return copy;
}

private Map<ByteArrayWrapper, byte[]> getDeepCopyOfCodes() {
Expand Down
1 change: 1 addition & 0 deletions modMcf/src/org/aion/mcf/config/CfgDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class Names {
public static final String CONTRACT_INDEX = "contractIndex";
public static final String DETAILS = "details";
public static final String STORAGE = "storage";
public static final String GRAPH = "graph";

public static final String STATE = "state";
public static final String STATE_ARCHIVE = "stateArchive";
Expand Down
20 changes: 18 additions & 2 deletions modMcf/src/org/aion/mcf/db/AbstractRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class AbstractRepository<
protected static final String CONTRACT_INDEX_DB = Names.CONTRACT_INDEX;
protected static final String DETAILS_DB = Names.DETAILS;
protected static final String STORAGE_DB = Names.STORAGE;
protected static final String GRAPH_DB = Names.GRAPH;
protected static final String STATE_DB = Names.STATE;
protected static final String STATE_ARCHIVE_DB = Names.STATE_ARCHIVE;
protected static final String PENDING_TX_POOL_DB = Names.TX_POOL;
Expand All @@ -70,6 +71,7 @@ public abstract class AbstractRepository<
protected ByteArrayKeyValueDatabase contractIndexDatabase;
protected ByteArrayKeyValueDatabase detailsDatabase;
protected ByteArrayKeyValueDatabase storageDatabase;
protected ByteArrayKeyValueDatabase graphDatabase;
protected ByteArrayKeyValueDatabase indexDatabase;
protected ByteArrayKeyValueDatabase blockDatabase;
protected ByteArrayKeyValueDatabase stateDatabase;
Expand Down Expand Up @@ -186,7 +188,8 @@ protected void initializeDatabasesAndCaches() throws InvalidFilePathException {
}
databaseGroup.add(transactionDatabase);

// getting details specific properties
// getting contract index specific properties
// this db will be used only for fast sync
sharedProps = cfg.getDatabaseConfig(CONTRACT_INDEX_DB);
sharedProps.setProperty(Props.ENABLE_LOCKING, "false");
sharedProps.setProperty(Props.DB_PATH, cfg.getDbPath());
Expand Down Expand Up @@ -219,6 +222,17 @@ protected void initializeDatabasesAndCaches() throws InvalidFilePathException {
}
databaseGroup.add(storageDatabase);

// getting graph specific properties
sharedProps = cfg.getDatabaseConfig(GRAPH_DB);
sharedProps.setProperty(Props.ENABLE_LOCKING, "false");
sharedProps.setProperty(Props.DB_PATH, cfg.getDbPath());
sharedProps.setProperty(Props.DB_NAME, GRAPH_DB);
this.graphDatabase = connectAndOpen(sharedProps, LOG);
if (graphDatabase == null || graphDatabase.isClosed()) {
throw newException(GRAPH_DB, sharedProps);
}
databaseGroup.add(graphDatabase);

// getting index specific properties
sharedProps = cfg.getDatabaseConfig(INDEX_DB);
sharedProps.setProperty(Props.ENABLE_LOCKING, "false");
Expand Down Expand Up @@ -268,7 +282,9 @@ protected void initializeDatabasesAndCaches() throws InvalidFilePathException {
databaseGroup.add(pendingTxCacheDatabase);

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

// pruning config
pruneEnabled = this.cfg.getPruneConfig().isEnabled();
Expand Down
12 changes: 10 additions & 2 deletions modMcf/src/org/aion/mcf/db/DetailsDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@ public class DetailsDataStore<

private ByteArrayKeyValueDatabase detailsSrc;
private ByteArrayKeyValueDatabase storageSrc;
private ByteArrayKeyValueDatabase graphSrc;
private Set<ByteArrayWrapper> removes = new HashSet<>();

public DetailsDataStore() {}

public DetailsDataStore(
ByteArrayKeyValueDatabase detailsCache,
ByteArrayKeyValueDatabase storageCache,
ByteArrayKeyValueDatabase graphCache,
RepositoryConfig repoConfig) {

this.repoConfig = repoConfig;
withDb(detailsCache, storageCache);
withDb(detailsCache, storageCache, graphCache);
}

public DetailsDataStore<BLK, BH> withDb(
ByteArrayKeyValueDatabase detailsSrc, ByteArrayKeyValueDatabase storageSrc) {
ByteArrayKeyValueDatabase detailsSrc,
ByteArrayKeyValueDatabase storageSrc,
ByteArrayKeyValueDatabase graphSrc) {
this.detailsSrc = detailsSrc;
this.storageSrc = storageSrc;
this.graphSrc = graphSrc;
this.storageDSPrune = new JournalPruneDataSource(storageSrc);
return this;
}
Expand Down Expand Up @@ -69,6 +74,7 @@ public synchronized ContractDetails get(byte[] key) {
// Found something from cache or database, return it by decoding it.
ContractDetails detailsImpl = repoConfig.contractDetailsImpl();
detailsImpl.setDataSource(storageDSPrune);
detailsImpl.setObjectGraphSource(graphSrc);
detailsImpl.decode(rawDetails.get()); // We can safely get as we checked
// if it is present.

Expand Down Expand Up @@ -141,6 +147,7 @@ public void syncLargeStorage() {
// Decode the details.
ContractDetails detailsImpl = repoConfig.contractDetailsImpl();
detailsImpl.setDataSource(storageDSPrune);
detailsImpl.setObjectGraphSource(graphSrc);
detailsImpl.decode(rawDetails.get(), true);
// We can safely get as we checked if it is present.

Expand All @@ -161,6 +168,7 @@ public synchronized void close() {
try {
detailsSrc.close();
storageSrc.close();
graphSrc.close();
} catch (Exception e) {
throw new RuntimeException("error closing db");
}
Expand Down
5 changes: 4 additions & 1 deletion modMcf/src/org/aion/mcf/ds/XorDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public void close() throws Exception {

@Override
public void deleteBatch(Collection<byte[]> keys) {
// TODO Auto-generated method stub
// NOTE: implementing this method will cause a break in consensus
// due to the fact that the serialized storage must have old roots
// to enable reverting to a different state for the account in case of a fork
// TODO remove this black magic :P
}

@Override
Expand Down

0 comments on commit e9396fa

Please sign in to comment.