Fix for getting Trie from rootNode

This commit is contained in:
nicksavers 2014-07-16 23:32:14 +02:00
parent 685fa48a6b
commit 6c90111ea0
4 changed files with 23 additions and 8 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

@ -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)));
}
}