Merge pull request #70 from nicksavers/master
Fix for getting Trie from rootNode
This commit is contained in:
commit
f1f637b123
|
@ -50,11 +50,9 @@ public class Cache {
|
||||||
return this.nodes.get(keyObj).getValue();
|
return this.nodes.get(keyObj).getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db != null) return new Value(null);
|
|
||||||
|
|
||||||
// Get the key of the database instead and cache it
|
// Get the key of the database instead and cache it
|
||||||
byte[] data = this.db.get(key);
|
byte[] data = this.db.get(key);
|
||||||
Value value = new Value(data);
|
Value value = Value.fromRlpEncoded(data);
|
||||||
// Create caching node
|
// Create caching node
|
||||||
this.nodes.put(keyObj, new Node(value, false));
|
this.nodes.put(keyObj, new Node(value, false));
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ import org.ethereum.crypto.HashUtil;
|
||||||
import org.ethereum.db.ByteArrayWrapper;
|
import org.ethereum.db.ByteArrayWrapper;
|
||||||
import org.ethereum.util.Value;
|
import org.ethereum.util.Value;
|
||||||
import org.iq80.leveldb.DB;
|
import org.iq80.leveldb.DB;
|
||||||
import org.spongycastle.util.encoders.Hex;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The modified Merkle Patricia tree (trie) provides a persistent data structure
|
* The modified Merkle Patricia tree (trie) provides a persistent data structure
|
||||||
|
@ -68,7 +67,7 @@ public class Trie implements TrieFacade{
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRoot(Node root) {
|
public void setRoot(byte[] root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,10 @@ public class Value {
|
||||||
|
|
||||||
private Object value;
|
private Object value;
|
||||||
|
|
||||||
public void fromRlpEncoded(byte[] data) {
|
public static Value fromRlpEncoded(byte[] data) {
|
||||||
if (data.length != 0) {
|
if (data.length != 0) {
|
||||||
this.value = RLP.decode(data, 0).getDecoded();
|
return new Value(RLP.decode(data, 0).getDecoded());
|
||||||
}
|
} return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Value(Object obj) {
|
public Value(Object obj) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.ethereum.trie;
|
package org.ethereum.trie;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.iq80.leveldb.DB;
|
import org.iq80.leveldb.DB;
|
||||||
import org.iq80.leveldb.DBException;
|
import org.iq80.leveldb.DBException;
|
||||||
|
@ -13,11 +15,11 @@ import org.iq80.leveldb.WriteOptions;
|
||||||
|
|
||||||
public class MockDB implements DB {
|
public class MockDB implements DB {
|
||||||
|
|
||||||
private int addedItems;
|
Map<byte[], byte[]> storage = new HashMap<byte[], byte[]>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
// TODO Auto-generated method stub
|
storage.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -33,7 +35,7 @@ public class MockDB implements DB {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(byte[] arg0) throws DBException {
|
public void delete(byte[] arg0) throws DBException {
|
||||||
// TODO Auto-generated method stub
|
storage.remove(arg0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -45,8 +47,7 @@ public class MockDB implements DB {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] get(byte[] arg0) throws DBException {
|
public byte[] get(byte[] arg0) throws DBException {
|
||||||
// TODO Auto-generated method stub
|
return storage.get(arg0);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -86,9 +87,8 @@ public class MockDB implements DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(byte[] arg0, byte[] arg1) throws DBException {
|
public void put(byte[] key, byte[] value) throws DBException {
|
||||||
// TODO Auto-generated method stub
|
storage.put(key, value);
|
||||||
addedItems++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -126,6 +126,6 @@ public class MockDB implements DB {
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public int getAddedItems() {
|
public int getAddedItems() {
|
||||||
return addedItems;
|
return storage.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,7 +2,10 @@ package org.ethereum.trie;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.ethereum.trie.Trie;
|
import org.ethereum.trie.Trie;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.spongycastle.util.encoders.Hex;
|
import org.spongycastle.util.encoders.Hex;
|
||||||
|
|
||||||
|
@ -30,6 +33,11 @@ public class TrieTest {
|
||||||
// G: [ '\x35', 'coin' ]
|
// G: [ '\x35', 'coin' ]
|
||||||
// C: [ '\x20\x6f\x72\x73\x65', 'stallion' ]
|
// C: [ '\x20\x6f\x72\x73\x65', 'stallion' ]
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void closeMockDb() throws IOException {
|
||||||
|
mockDb.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyKey() {
|
public void testEmptyKey() {
|
||||||
Trie trie = new Trie(mockDb);
|
Trie trie = new Trie(mockDb);
|
||||||
|
@ -488,4 +496,14 @@ public class TrieTest {
|
||||||
trie.update("te", "testy");
|
trie.update("te", "testy");
|
||||||
assertEquals("8452568af70d8d140f58d941338542f645fcca50094b20f3c3d8c3df49337928", Hex.toHexString(trie.getRootHash()));
|
assertEquals("8452568af70d8d140f58d941338542f645fcca50094b20f3c3d8c3df49337928", Hex.toHexString(trie.getRootHash()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetFromRootNode() {
|
||||||
|
Trie trie1 = new Trie(mockDb);
|
||||||
|
trie1.update(cat, LONG_STRING);
|
||||||
|
trie1.sync();
|
||||||
|
Trie trie2 = new Trie(mockDb);
|
||||||
|
trie2.setRoot(trie1.getRootHash());
|
||||||
|
assertEquals(LONG_STRING, new String(trie2.get(cat)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue