Merge pull request #70 from nicksavers/master

Fix for getting Trie from rootNode
This commit is contained in:
romanman 2014-07-17 00:37:15 +03:00
commit f1f637b123
5 changed files with 33 additions and 18 deletions

View File

@ -50,11 +50,9 @@ public class Cache {
return this.nodes.get(keyObj).getValue();
}
if (db != null) return new Value(null);
// Get the key of the database instead and cache it
byte[] data = this.db.get(key);
Value value = new Value(data);
Value value = Value.fromRlpEncoded(data);
// Create caching node
this.nodes.put(keyObj, new Node(value, false));

View File

@ -10,7 +10,6 @@ import org.ethereum.crypto.HashUtil;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.Value;
import org.iq80.leveldb.DB;
import org.spongycastle.util.encoders.Hex;
/**
* The modified Merkle Patricia tree (trie) provides a persistent data structure
@ -68,7 +67,7 @@ public class Trie implements TrieFacade{
return root;
}
public void setRoot(Node root) {
public void setRoot(byte[] root) {
this.root = root;
}

View File

@ -13,10 +13,10 @@ public class Value {
private Object value;
public void fromRlpEncoded(byte[] data) {
public static Value fromRlpEncoded(byte[] data) {
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) {

View File

@ -1,6 +1,8 @@
package org.ethereum.trie;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
@ -13,11 +15,11 @@ import org.iq80.leveldb.WriteOptions;
public class MockDB implements DB {
private int addedItems;
Map<byte[], byte[]> storage = new HashMap<byte[], byte[]>();
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
storage.clear();
}
@Override
@ -33,7 +35,7 @@ public class MockDB implements DB {
@Override
public void delete(byte[] arg0) throws DBException {
// TODO Auto-generated method stub
storage.remove(arg0);
}
@Override
@ -45,8 +47,7 @@ public class MockDB implements DB {
@Override
public byte[] get(byte[] arg0) throws DBException {
// TODO Auto-generated method stub
return null;
return storage.get(arg0);
}
@Override
@ -86,9 +87,8 @@ public class MockDB implements DB {
}
@Override
public void put(byte[] arg0, byte[] arg1) throws DBException {
// TODO Auto-generated method stub
addedItems++;
public void put(byte[] key, byte[] value) throws DBException {
storage.put(key, value);
}
@Override
@ -126,6 +126,6 @@ public class MockDB implements DB {
* @return int
*/
public int getAddedItems() {
return addedItems;
return storage.size();
}
}

View File

@ -2,7 +2,10 @@ package org.ethereum.trie;
import static org.junit.Assert.*;
import java.io.IOException;
import org.ethereum.trie.Trie;
import org.junit.After;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@ -30,6 +33,11 @@ public class TrieTest {
// G: [ '\x35', 'coin' ]
// C: [ '\x20\x6f\x72\x73\x65', 'stallion' ]
@After
public void closeMockDb() throws IOException {
mockDb.close();
}
@Test
public void testEmptyKey() {
Trie trie = new Trie(mockDb);
@ -488,4 +496,14 @@ public class TrieTest {
trie.update("te", "testy");
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)));
}
}