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

Cleaning up some trie functionality #813

Merged
merged 2 commits into from
Feb 7, 2019
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
8 changes: 2 additions & 6 deletions modAion/src/org/aion/zero/db/AionContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,12 @@ public void setExternalStorageDataSource(IByteArrayKeyValueStore dataSource) {
@Override
public IContractDetails getSnapshotTo(byte[] hash) {

IByteArrayKeyValueStore keyValueDataSource = this.storageTrie.getCache().getDb();

SecureTrie snapStorage =
wrap(hash).equals(wrap(EMPTY_TRIE_HASH))
? new SecureTrie(keyValueDataSource, "".getBytes())
: new SecureTrie(keyValueDataSource, hash);
? new SecureTrie(storageTrie.getCache(), "".getBytes())
: new SecureTrie(storageTrie.getCache(), hash);
snapStorage.withPruningEnabled(storageTrie.isPruningEnabled());

snapStorage.setCache(this.storageTrie.getCache());

AionContractDetailsImpl details =
new AionContractDetailsImpl(this.address, snapStorage, getCodes());
details.externalStorage = this.externalStorage;
Expand Down
59 changes: 24 additions & 35 deletions modMcf/src/org/aion/mcf/trie/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public synchronized Value get(byte[] key) {
return node.getValue();
}
if (this.dataSource != null) {
Optional<byte[]> data =
(this.dataSource == null) ? Optional.empty() : this.dataSource.get(key);
Optional<byte[]> data = this.dataSource.get(key);
if (data.isPresent()) {
// dbhits++;
Value val = fromRlpEncoded(data.get());
Expand All @@ -94,10 +93,6 @@ public synchronized void delete(byte[] key) {
}
}

public synchronized void commit() {
commit(true);
}

public synchronized void commit(boolean flushCache) {
// Don't try to commit if it isn't dirty
if ((dataSource == null) || !this.isDirty) {
Expand Down Expand Up @@ -143,24 +138,16 @@ public synchronized void commit(boolean flushCache) {
this.removedNodes.clear();
}

public synchronized void undo() {
Iterator<Map.Entry<ByteArrayWrapper, Node>> iter = this.nodes.entrySet().iterator();
while (iter.hasNext()) {
if (iter.next().getValue().isDirty()) {
iter.remove();
}
}
this.isDirty = false;
}
// not used
// public synchronized void undo() {
// this.nodes.entrySet().removeIf(entry -> entry.getValue().isDirty());
// this.isDirty = false;
// }

public synchronized boolean isDirty() {
return isDirty;
}

public synchronized void setDirty(boolean isDirty) {
this.isDirty = isDirty;
}

public synchronized Map<ByteArrayWrapper, Node> getNodes() {
return nodes;
}
Expand All @@ -169,22 +156,24 @@ public synchronized IByteArrayKeyValueStore getDb() {
return dataSource;
}

public String cacheDump() {
StringBuffer cacheDump = new StringBuffer();
for (ByteArrayWrapper key : nodes.keySet()) {
Node node = nodes.get(key);
if (node.getValue() != null) {
cacheDump
.append(key.toString())
.append(" : ")
.append(node.getValue().toString())
.append("\n");
}
}

return cacheDump.toString();
}

// not used
// public String cacheDump() {
// StringBuilder cacheDump = new StringBuilder();
// for (ByteArrayWrapper key : nodes.keySet()) {
// Node node = nodes.get(key);
// if (node.getValue() != null) {
// cacheDump
// .append(key.toString())
// .append(" : ")
// .append(node.getValue().toString())
// .append("\n");
// }
// }
//
// return cacheDump.toString();
// }

@SuppressWarnings("OptionalGetWithoutIsPresent")
public synchronized void setDB(IByteArrayKeyValueStore kvds) {
if (this.dataSource == kvds) {
return;
Expand Down
52 changes: 22 additions & 30 deletions modMcf/src/org/aion/mcf/trie/SecureTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@

public class SecureTrie extends TrieImpl implements Trie {

/**
* A private constructor that initializes the db and root to null.
*
* Used by this class's copy() method.
*/
private SecureTrie() {
super(null, null);
}

public SecureTrie(IByteArrayKeyValueStore db) {
this(db, "");
}
Expand All @@ -24,6 +15,10 @@ public SecureTrie(IByteArrayKeyValueStore db, Object root) {
super(db, root);
}

public SecureTrie(final Cache cache, Object root) {
super(cache, root);
}

@Override
public byte[] get(byte[] key) {
return super.get(h256(key));
Expand All @@ -42,37 +37,35 @@ public void delete(byte[] key) {
/**
* Returns a copy of this trie.
*
* The {@link Cache} object returned will be a copy of this object's cache, but the two will
* <p>The {@link Cache} object returned will be a copy of this object's cache, but the two will
* share the same references to their data sources ({@link IByteArrayKeyValueStore} objects) and
* each {@link Node} will retain the same references as the original node's
* {@link org.aion.rlp.Value} objects.
* each {@link Node} will retain the same references as the original node's {@link
* org.aion.rlp.Value} objects.
*
* The previous root and current root of this trie will return deep copies of these objects only
* if they are of type {@code byte[]}. Otherwise, the original references will be passed on. It
* is expected that they will indeed be byte arrays.
* <p>The previous root and current root of this trie will return deep copies of these objects
* only if they are of type {@code byte[]}. Otherwise, the original references will be passed
* on. It is expected that they will indeed be byte arrays.
*
* @return A copy of this trie.
*/
public SecureTrie copy() {
synchronized (super.getCache()) {
SecureTrie secureTrieCopy = new SecureTrie();

Object originalPreviousRoot = super.getPrevRoot();
// Object originalPreviousRoot = super.getPrevRoot();
Object originalRoot = super.getRoot();

Object previousRootCopy = null;
// Object previousRootCopy = null;
Object rootCopy = null;

// We should only be dealing in terms of byte arrays for the previous & current roots.
// But if we do not get a byte[] here, then we cannot make a deep copy of the object.
if (originalPreviousRoot instanceof byte[]) {
byte[] originalPreviousRootAsBytes = (byte[]) originalPreviousRoot;
previousRootCopy =
Arrays.copyOf(
originalPreviousRootAsBytes, originalPreviousRootAsBytes.length);
} else if (originalPreviousRoot != null) {
previousRootCopy = originalPreviousRoot;
}
// if (originalPreviousRoot instanceof byte[]) {
// byte[] originalPreviousRootAsBytes = (byte[]) originalPreviousRoot;
// previousRootCopy =
// Arrays.copyOf(
// originalPreviousRootAsBytes, originalPreviousRootAsBytes.length);
// } else if (originalPreviousRoot != null) {
// previousRootCopy = originalPreviousRoot;
// }

if (originalRoot instanceof byte[]) {
byte[] originalRootAsBytes = (byte[]) originalRoot;
Expand All @@ -81,9 +74,8 @@ public SecureTrie copy() {
rootCopy = originalRoot;
}

secureTrieCopy.setPrevRoot(previousRootCopy);
secureTrieCopy.setRoot(rootCopy);
secureTrieCopy.setCache(super.getCache().copy());
SecureTrie secureTrieCopy = new SecureTrie(super.getCache().copy(), rootCopy);
// secureTrieCopy.setPrevRoot(previousRootCopy);
secureTrieCopy.setPruningEnabled(super.isPruningEnabled());
return secureTrieCopy;
}
Expand Down
10 changes: 6 additions & 4 deletions modMcf/src/org/aion/mcf/trie/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ public interface Trie {

void sync(boolean flushCache);

/** Discard all the changes until now */
@Deprecated
void undo();
// never used
// /** Discard all the changes until now */
// @Deprecated
// void undo();

String getTrieDump();

String getTrieDump(byte[] stateRoot);

int getTrieSize(byte[] stateRoot);

boolean validate();
// never used
// boolean validate();

long saveFullStateToDatabase(byte[] stateRoot, IByteArrayKeyValueDatabase db);

Expand Down
Loading