From 5bf3329993b5ea3dc862f259215fd7d69e84ed88 Mon Sep 17 00:00:00 2001 From: romanman Date: Tue, 26 Aug 2014 11:19:50 +0300 Subject: [PATCH 1/2] Trie work + nice string for nibbles --- .../org/ethereum/util/CompactEncoder.java | 48 +++++++++++++++++-- .../org/ethereum/util/CompactEncoderTest.java | 34 ++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/util/CompactEncoder.java b/ethereumj-core/src/main/java/org/ethereum/util/CompactEncoder.java index 536144f0..7bffb62e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/CompactEncoder.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/CompactEncoder.java @@ -9,6 +9,8 @@ import static org.spongycastle.util.encoders.Hex.encode; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; /** * Compact encoding of hex sequence with optional terminator @@ -47,8 +49,27 @@ import java.nio.ByteBuffer; public class CompactEncoder { private final static byte TERMINATOR = 16; - private final static String hexBase = "0123456789abcdef"; - + private final static Map hexMap = new HashMap(); + static { + hexMap.put('0', (byte)0); + hexMap.put('1', (byte)1); + hexMap.put('2', (byte)2); + hexMap.put('3', (byte)3); + hexMap.put('4', (byte)4); + hexMap.put('5', (byte)5); + hexMap.put('6', (byte)6); + hexMap.put('7', (byte)7); + hexMap.put('8', (byte)8); + hexMap.put('9', (byte)9); + hexMap.put('a', (byte)10); + hexMap.put('b', (byte)11); + hexMap.put('c', (byte)12); + hexMap.put('d', (byte)13); + hexMap.put('e', (byte)14); + hexMap.put('f', (byte)15); + } + + /** * Pack nibbles to binary * @@ -107,9 +128,30 @@ public class CompactEncoder { byte[] hexEncoded = encode(str); ByteBuffer slice = ByteBuffer.allocate(hexEncoded.length + 1); for (byte b : hexEncoded) { - slice.put((byte)hexBase.indexOf(b)); + slice.put(hexMap.get((char)b)); } slice.put(TERMINATOR); return slice.array(); } + + + /** + * turn nibbles to a nice good looking output string + * + * @param nibbles - getting byte of data [ 04 ] and turning + * it to a '\x04' representation + * @return + */ + public static String nibblesToPrettyString(byte[] nibbles){ + + StringBuffer buffer = new StringBuffer(); + for (byte nibble : nibbles){ + + String nibleString = Utils.oneByteToHexString(nibble); + + buffer.append("\\x" + nibleString); + } + + return buffer.toString(); + } } diff --git a/ethereumj-core/src/test/java/org/ethereum/util/CompactEncoderTest.java b/ethereumj-core/src/test/java/org/ethereum/util/CompactEncoderTest.java index cf8a53d3..c8f7fa4a 100644 --- a/ethereumj-core/src/test/java/org/ethereum/util/CompactEncoderTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/util/CompactEncoderTest.java @@ -66,9 +66,41 @@ public class CompactEncoderTest { } @Test - public void testCompactHexDecode() { + public void testCompactHexEncode_1() { byte[] test = "stallion".getBytes(); byte[] result = new byte[] { 7, 3, 7, 4, 6, 1, 6, 12, 6, 12, 6, 9, 6, 15, 6, 14, T }; assertArrayEquals(result, CompactEncoder.binToNibbles(test)); } + + @Test + public void testCompactHexEncode_2() { + byte[] test = "verb".getBytes(); + byte[] result = new byte[] { 7, 6, 6, 5, 7, 2, 6, 2, T }; + assertArrayEquals(result, CompactEncoder.binToNibbles(test)); + } + + @Test + public void testCompactHexEncode_3() { + byte[] test = "puppy".getBytes(); + byte[] result = new byte[] { 7, 0, 7, 5, 7, 0, 7, 0, 7, 9, T }; + assertArrayEquals(result, CompactEncoder.binToNibbles(test)); + } + + + @Test + public void testNiceNiblesOutput_1(){ + byte[] test = {7, 0, 7, 5, 7, 0, 7, 0, 7, 9}; + String result = "\\x07\\x00\\x07\\x05\\x07\\x00\\x07\\x00\\x07\\x09"; + + assertEquals(result, CompactEncoder.nibblesToPrettyString(test)); + } + + @Test + public void testNiceNiblesOutput_2(){ + byte[] test = {7, 0, 7, 0xf, 7, 0, 0xa, 0, 7, 9}; + String result = "\\x07\\x00\\x07\\x0f\\x07\\x00\\x0a\\x00\\x07\\x09"; + + assertEquals(result, CompactEncoder.nibblesToPrettyString(test)); + } + } From 69ba68adf4260cf934ea9ea0d2d187477c910569 Mon Sep 17 00:00:00 2001 From: Nick Savers Date: Wed, 27 Aug 2014 11:08:53 +0200 Subject: [PATCH 2/2] Pad state dump block no. in filenames with zeroes --- ethereumj-core/src/main/java/org/ethereum/db/Repository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java index a437fd0c..b86f7b13 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java @@ -385,10 +385,10 @@ public class Repository { String fileName = ""; if (txHash != null) - fileName = String.format("%d_%d_%s.dmp", block.getNumber(), txNumber, + fileName = String.format("%05d_%d_%s.dmp", block.getNumber(), txNumber, Hex.toHexString(txHash).substring(0, 8)); else - fileName = String.format("%d_c.dmp", block.getNumber()); + fileName = String.format("%05d_c.dmp", block.getNumber()); File dumpFile = new File(System.getProperty("user.dir") + "/" + dir + fileName); FileWriter fw = null;