Skip to content

Commit

Permalink
removed the flag for enabling the object graph use for AVM
Browse files Browse the repository at this point in the history
 - bug fixing storage retrieval
 - added AVM jars using the object graph
  • Loading branch information
AlexandraRoatis committed Apr 10, 2019
1 parent 86b23ec commit bf4c3e4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 42 deletions.
Binary file modified lib/org-aion-avm-api.jar
Binary file not shown.
Binary file modified lib/org-aion-avm-core.jar
Binary file not shown.
Binary file modified lib/org-aion-avm-rt.jar
Binary file not shown.
Binary file modified lib/org-aion-avm-userlib.jar
Binary file not shown.
116 changes: 74 additions & 42 deletions modAionImpl/src/org/aion/zero/impl/db/AionContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public void setCode(byte[] code) {
public byte[] getObjectGraph() {
if (objectGraph == null) {
// the object graph was not stored yet
if (java.util.Arrays.equals(objectGraphHash, EMPTY_DATA_HASH)) {
if (Arrays.equals(objectGraphHash, EMPTY_DATA_HASH)) {
return EMPTY_BYTE_ARRAY;
} else {
} else if (objectGraphSource != null) {
// note: the enforced use of optional is rather cumbersome here
Optional<byte[]> dbVal = getContractObjectGraphSource().get(objectGraphHash);
objectGraph = dbVal.isPresent() ? dbVal.get() : null;
Expand All @@ -164,17 +164,14 @@ public void setObjectGraph(byte[] graph) {
this.rlpEncoded = null;
}

// TODO: use until the AVM impl is added
private final boolean enableObjectGraph = false;

/**
* Returns the storage hash.
*
* @return the storage hash.
*/
@Override
public byte[] getStorageHash() {
if (vmType == TransactionTypes.AVM_CREATE_CODE && enableObjectGraph) {
if (vmType == TransactionTypes.AVM_CREATE_CODE) {
return computeAvmStorageHash();
} else {
return storageTrie.getRootHash();
Expand Down Expand Up @@ -293,24 +290,32 @@ public boolean decodeEncodingWithoutVmType(RLPList rlpList, boolean fastCheck) {
public void decodeStorage(RLPElement root, RLPElement storage, boolean keepStorageInMem) {
// different values based on the VM used
byte[] storageRootHash;
if (vmType == TransactionTypes.AVM_CREATE_CODE && enableObjectGraph) {
if (vmType == TransactionTypes.AVM_CREATE_CODE) {
// points to the storage hash and the object graph hash
concatenatedStorageHash = root.getRLPData();

RLPList data =
RLP.decode2(getContractObjectGraphSource().get(concatenatedStorageHash).get());
if (!(data.get(0) instanceof RLPList)) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}
RLPList pair = (RLPList) data.get(0);
if (pair.size() != 2) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
Optional<byte[]> concatenatedData =
objectGraphSource == null
? Optional.empty()
: getContractObjectGraphSource().get(concatenatedStorageHash);
if (concatenatedData.isPresent()) {
RLPList data = RLP.decode2(concatenatedData.get());
if (!(data.get(0) instanceof RLPList)) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}
RLPList pair = (RLPList) data.get(0);
if (pair.size() != 2) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}

storageRootHash = pair.get(0).getRLPData();
objectGraphHash = pair.get(1).getRLPData();
} else {
storageRootHash = EMPTY_TRIE_HASH;
objectGraphHash = EMPTY_DATA_HASH;
}

storageRootHash = pair.get(0).getRLPData();
objectGraphHash = pair.get(1).getRLPData();
} else {
storageRootHash = root.getRLPData();
}
Expand Down Expand Up @@ -410,12 +415,21 @@ public void setAddress(Address address) {
/** Syncs the storage trie. */
@Override
public void syncStorage() {
if (vmType == TransactionTypes.AVM_CREATE_CODE && enableObjectGraph) {
if (objectGraph == null || Arrays.equals(objectGraphHash, EMPTY_DATA_HASH)) {
throw new IllegalStateException(
"The AVM object graph must be set before pushing data to disk.");
if (vmType == TransactionTypes.AVM_CREATE_CODE) {
// if (objectGraph == null || Arrays.equals(objectGraphHash, EMPTY_DATA_HASH)) {
// throw new IllegalStateException(
// "The AVM object graph must be set before pushing data to disk.");
// }

if (objectGraphSource == null) {
throw new NullPointerException(
"The contract object graph source was not initialized.");
}

byte[] graph = getObjectGraph();
if (!Arrays.equals(graph, EMPTY_BYTE_ARRAY)) {
getContractObjectGraphSource().put(objectGraphHash, graph);
}
getContractObjectGraphSource().put(objectGraphHash, objectGraph);
getContractObjectGraphSource()
.put(
computeAvmStorageHash(),
Expand Down Expand Up @@ -464,10 +478,15 @@ private ByteArrayKeyValueStore getExternalStorageDataSource() {
*/
private ByteArrayKeyValueStore getContractObjectGraphSource() {
if (contractObjectGraphSource == null) {
contractObjectGraphSource =
new XorDataSource(
objectGraphSource,
h256(("details-graph/" + address.toString()).getBytes()));
if (objectGraphSource == null) {
throw new NullPointerException(
"The contract object graph source was not initialized.");
} else {
contractObjectGraphSource =
new XorDataSource(
objectGraphSource,
h256(("details-graph/" + address.toString()).getBytes()));
}
}
return contractObjectGraphSource;
}
Expand Down Expand Up @@ -499,30 +518,43 @@ public ContractDetails getSnapshotTo(byte[] hash) {

SecureTrie snapStorage;
AionContractDetailsImpl details;
if (vmType == TransactionTypes.AVM_CREATE_CODE && enableObjectGraph) {
if (vmType == TransactionTypes.AVM_CREATE_CODE) {
byte[] storageRootHash, graphHash;
// get the concatenated storage hash from storage
RLPList data = RLP.decode2(getContractObjectGraphSource().get(hash).get());
if (!(data.get(0) instanceof RLPList)) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}
RLPList pair = (RLPList) data.get(0);
if (pair.size() != 2) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
Optional<byte[]> concatenatedData =
objectGraphSource == null
? Optional.empty()
: getContractObjectGraphSource().get(hash);
if (concatenatedData.isPresent()) {
RLPList data = RLP.decode2(concatenatedData.get());
if (!(data.get(0) instanceof RLPList)) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}
RLPList pair = (RLPList) data.get(0);
if (pair.size() != 2) {
throw new IllegalArgumentException(
"rlp decode error: invalid concatenated storage for AVM");
}

storageRootHash = pair.get(0).getRLPData();
graphHash = pair.get(1).getRLPData();
} else {
storageRootHash = EMPTY_TRIE_HASH;
graphHash = EMPTY_DATA_HASH;
}

snapStorage =
wrap(hash).equals(wrap(EMPTY_TRIE_HASH))
wrap(storageRootHash).equals(wrap(EMPTY_TRIE_HASH))
? new SecureTrie(storageTrie.getCache(), "".getBytes())
: new SecureTrie(storageTrie.getCache(), pair.get(0).getRLPData());
: new SecureTrie(storageTrie.getCache(), storageRootHash);
snapStorage.withPruningEnabled(storageTrie.isPruningEnabled());

details = new AionContractDetailsImpl(this.address, snapStorage, getCodes());

// object graph information
details.objectGraphSource = this.objectGraphSource;
details.objectGraphHash = pair.get(1).getRLPData();
details.objectGraphHash = graphHash;
details.concatenatedStorageHash = hash;
} else {
snapStorage =
Expand Down
1 change: 1 addition & 0 deletions modMcf/src/org/aion/mcf/db/DetailsDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public synchronized ContractDetails get(byte[] key) {
public synchronized void update(Address key, ContractDetails contractDetails) {

contractDetails.setAddress(key);
contractDetails.setObjectGraphSource(graphSrc);
ByteArrayWrapper wrappedKey = wrap(key.toBytes());

// Put into cache.
Expand Down

0 comments on commit bf4c3e4

Please sign in to comment.