diff --git a/ethereumj-core/pom.xml b/ethereumj-core/pom.xml
index 09d6fc30..111413d6 100644
--- a/ethereumj-core/pom.xml
+++ b/ethereumj-core/pom.xml
@@ -92,6 +92,18 @@
3.2.1
-
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.1
+
+ 1.7
+ 1.7
+
+
+
+
\ No newline at end of file
diff --git a/ethereumj-core/src/main/java/org/ethereum/geodb/IpGeoDB.java b/ethereumj-core/src/main/java/org/ethereum/geodb/IpGeoDB.java
index d294b0b2..52ac2874 100644
--- a/ethereumj-core/src/main/java/org/ethereum/geodb/IpGeoDB.java
+++ b/ethereumj-core/src/main/java/org/ethereum/geodb/IpGeoDB.java
@@ -1,16 +1,11 @@
package org.ethereum.geodb;
+import java.io.File;
+import java.net.InetAddress;
+import java.net.URL;
+
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
-import com.maxmind.geoip2.DatabaseReader;
-import com.maxmind.geoip2.exception.GeoIp2Exception;
-import com.maxmind.geoip2.model.CityResponse;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.URISyntaxException;
-import java.net.URL;
/**
* www.ethereumJ.com
diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java b/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java
index f63853d8..7fe11764 100644
--- a/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java
+++ b/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java
@@ -1,14 +1,19 @@
package org.ethereum.gui;
-import org.ethereum.net.client.ClientPeer;
-import org.fife.ui.rsyntaxtextarea.*;
-import org.fife.ui.rtextarea.RTextScrollPane;
+import java.awt.BorderLayout;
+import java.awt.Image;
+import java.awt.Toolkit;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
-import javax.swing.*;
-import java.awt.*;
-import java.awt.event.*;
-import java.io.IOException;
-import java.util.TimerTask;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+import org.ethereum.net.client.ClientPeer;
+import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
+import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
+import org.fife.ui.rtextarea.RTextScrollPane;
/**
* A simple example showing how to modify the fonts and colors used in an
@@ -27,7 +32,6 @@ public class ConnectionConsole extends JFrame implements PeerListener{
private RSyntaxTextArea textArea;
-
public ConnectionConsole() {
final ConnectionConsole thisConsole = this;
@@ -59,28 +63,21 @@ public class ConnectionConsole extends JFrame implements PeerListener{
Thread t = new Thread() {
public void run() {
-// new ClientPeer(thisConsole).connect("54.201.28.117", 30303);
- new ClientPeer(thisConsole).connect("82.217.72.169", 30303);
+ new ClientPeer(thisConsole).connect("54.204.10.41", 30303);
+// new ClientPeer(thisConsole).connect("54.201.28.117", 30303);
+// new ClientPeer(thisConsole).connect("82.217.72.169", 30303);
}
};
t.start();
-
-
-
}
-
});
-
}
@Override
public void console(final String output) {
-
- SwingUtilities.invokeLater(new Runnable()
- {
- public void run()
- {
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
textArea.append(output);
textArea.append("\n");
textArea.setCaretPosition(textArea.getText().length());
diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java
index 8f27038f..c6fc6d93 100644
--- a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java
+++ b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java
@@ -1,7 +1,6 @@
package org.ethereum.gui;
import com.maxmind.geoip.Location;
-import com.maxmind.geoip2.model.CityResponse;
import org.ethereum.geodb.IpGeoDB;
import org.ethereum.util.Utils;
@@ -28,8 +27,6 @@ public class PeersTableModel extends AbstractTableModel {
generateRandomData();
}
-
-
public String getColumnName(int column) {
if (column == 0) return "Location";
@@ -59,7 +56,6 @@ public class PeersTableModel extends AbstractTableModel {
URL flagURL = ClassLoader.getSystemResource("flags/" + countryCode + ".png");
ImageIcon flagIcon = new ImageIcon(flagURL);
-
return flagIcon;
}
@@ -78,7 +74,6 @@ public class PeersTableModel extends AbstractTableModel {
flagIcon = Utils.getImageIcon("disconnected.png");
}
-
return flagIcon;
}
diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java
index 40be180c..f168c0b0 100644
--- a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java
+++ b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java
@@ -11,7 +11,6 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-
/**
* www.ethereumJ.com
* User: Roman Mandeleil
diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java b/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java
index becd2806..66c068d7 100644
--- a/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java
+++ b/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java
@@ -33,7 +33,6 @@ public class ToolBar extends JFrame {
this.setContentPane(cp);
-
java.net.URL imageURL_1 = ClassLoader.getSystemResource("buttons/feedly.png");
ImageIcon image_1 = new ImageIcon(imageURL_1);
@@ -43,8 +42,6 @@ public class ToolBar extends JFrame {
java.net.URL imageURL_3 = ClassLoader.getSystemResource("buttons/browser.png");
ImageIcon image_3 = new ImageIcon(imageURL_3);
-
-
JToggleButton editorToggle = new JToggleButton("");
editorToggle.setIcon(image_1);
editorToggle.setContentAreaFilled(true);
@@ -97,20 +94,12 @@ public class ToolBar extends JFrame {
PeersTableMain mainFrame = new PeersTableMain();
mainFrame.setVisible( true );
// mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
}
});
-
cp.add(editorToggle);
cp.add(logToggle);
cp.add(peersToggle);
-
-
-
-
-
}
public static void main(String args[]){
@@ -120,7 +109,6 @@ public class ToolBar extends JFrame {
new ToolBar().setVisible(true);
}
});
-
}
}
diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java b/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java
index d66e8981..34994640 100644
--- a/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java
+++ b/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java
@@ -1,8 +1,6 @@
package org.ethereum.manager;
import com.maxmind.geoip.Location;
-import com.maxmind.geoip2.model.CityResponse;
-import com.maxmind.geoip2.record.Country;
import org.ethereum.geodb.IpGeoDB;
import org.ethereum.net.vo.PeerData;
diff --git a/ethereumj-core/src/main/java/org/ethereum/net/RLP.java b/ethereumj-core/src/main/java/org/ethereum/net/RLP.java
index 62c598c2..085b912b 100644
--- a/ethereumj-core/src/main/java/org/ethereum/net/RLP.java
+++ b/ethereumj-core/src/main/java/org/ethereum/net/RLP.java
@@ -1,1102 +1,1035 @@
package org.ethereum.net;
-import org.bouncycastle.util.encoders.Hex;
-import org.ethereum.net.rlp.RLPItem;
-import org.ethereum.net.rlp.RLPList;
-import org.ethereum.util.Utils;
-
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Queue;
+
+import org.ethereum.net.rlp.RLPItem;
+import org.ethereum.net.rlp.RLPList;
/**
*/
public class RLP {
+ public static byte decodeOneByteItem(byte[] data, int index) {
- public static byte decodeOneByteItem(byte[] data, int index){
+ // null item
+ if ((data[index] & 0xFF) == 0x80) {
+ return (byte) (data[index] - 0x80);
+ }
+ // single byte item
+ if ((data[index] & 0xFF) < 0x80) {
+ return (byte) (data[index]);
+ }
+ // single byte item
+ if ((data[index] & 0xFF) == 0x81) {
+ return (byte) (data[index + 1]);
+ }
+ return 0;
+ }
- // null item
- if ((data[index] & 0xFF) == 0x80){
- return (byte) (data[index] - 0x80);
- }
+ public static String decodeStringItem(byte[] data, int index) {
- // single byte item
- if ((data[index] & 0xFF) < 0x80){
- return (byte) (data[index]);
- }
+ String value = null;
- // single byte item
- if ((data[index] & 0xFF) == 0x81){
- return (byte) (data[index + 1]);
- }
+ if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ byte lenghtOfLenght = (byte) (data[index] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- return 0;
- }
+ int length = 0;
- public static String decodeStringItem(byte[] data, int index){
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- String value = null;
+ length += data[index + i] << (8 * pow);
+ pow--;
+ }
- if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ value = new String(data, index + lenghtOfLenght + 1, length);
- byte lenghtOfLenght = (byte) (data[index] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- int length = 0;
+ byte length = (byte) ((data[index] & 0xFF) - 0x80);
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ value = new String(data, index + 1, length);
- length += data[index+ i] << (8 * pow);
- pow--;
- }
+ } else {
+ throw new Error("wrong decode attempt");
+ }
- value = new String(data, index + lenghtOfLenght + 1, length);
+ return value;
+ }
- }else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ public static int decodeInt(byte[] data, int index) {
- byte length = (byte) ((data[index] & 0xFF) - 0x80);
+ int value = 0;
- value = new String(data, index + 1, length);
+ if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- } else{
- throw new Error("wrong decode attempt");
- }
+ byte length = (byte) (data[index] - 0x80);
+ byte pow = (byte) (length - 1);
- return value;
- }
+ for (int i = 1; i <= length; ++i) {
+ value += data[index + i] << (8 * pow);
+ pow--;
+ }
+ } else {
+ throw new Error("wrong decode attempt");
+ }
+ return value;
+ }
+ public static short decodeShort(byte[] data, int index) {
- public static int decodeInt(byte[] data, int index){
+ short value = 0;
- int value = 0;
+ if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ byte length = (byte) (data[index] - 0x80);
+ value = ByteBuffer.wrap(data, index, length).getShort();
+ } else {
+ value = data[index];
+ }
+ return value;
+ }
- if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ public static short decodeLong(byte[] data, int index) {
- byte length = (byte) (data[index] - 0x80);
- byte pow = (byte) (length - 1);
+ short value = 0;
+ if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- for (int i = 1; i <= length; ++i){
+ byte length = (byte) (data[index] - 0x80);
+ byte pow = (byte) (length - 1);
- value += data[index + i] << (8 * pow);
- pow--;
- }
- } else{
+ for (int i = 1; i <= length; ++i) {
- throw new Error("wrong decode attempt");
+ value += data[index + i] << (8 * pow);
+ pow--;
+ }
+ } else {
+ throw new Error("wrong decode attempt");
+ }
- }
+ return value;
+ }
- return value;
- }
+ public static byte[] decodeItemBytes(byte[] data, int index) {
- public static short decodeShort(byte[] data, int index){
+ byte[] value = null;
- short value = 0;
+ if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
- if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ byte lenghtOfLenght = (byte) (data[index] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- byte length = (byte) (data[index] - 0x80);
+ int length = 0;
- value = ByteBuffer.wrap(data, index, length).getShort();
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- } else{
+ length += data[index + i] << (8 * pow);
+ pow--;
+ }
- value = data[index];
- }
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- return value;
- }
+ value = valueBytes;
- public static short decodeLong(byte[] data, int index){
+ } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- short value = 0;
+ byte length = (byte) (data[index] - 0x80);
- if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- byte length = (byte) (data[index] - 0x80);
- byte pow = (byte) (length - 1);
+ value = valueBytes;
+ } else {
- for (int i = 1; i <= length; ++i){
+ throw new Error("wrong decode attempt");
- value += data[index + i] << (8 * pow);
- pow--;
- }
- } else{
+ }
- throw new Error("wrong decode attempt");
+ return value;
+ }
- }
+ public static BigInteger decodeBigInteger(byte[] data, int index) {
- return value;
- }
+ BigInteger value = null;
+ if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
- public static byte[] decodeItemBytes(byte[] data, int index){
+ byte lenghtOfLenght = (byte) (data[index] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- byte[] value = null;
+ int length = 0;
- if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- byte lenghtOfLenght = (byte) (data[index] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ length += data[index + i] << (8 * pow);
+ pow--;
+ }
- int length = 0;
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ value = new BigInteger(valueBytes);
- length += data[index+ i] << (8 * pow);
- pow--;
- }
+ } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ byte length = (byte) (data[index] - 0x80);
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- value = valueBytes;
+ value = new BigInteger(valueBytes);
- } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ } else {
- byte length = (byte) (data[index] - 0x80);
+ throw new Error("wrong decode attempt");
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ }
- value = valueBytes;
+ return value;
+ }
- } else {
+ public static byte[] decodeByteArray(byte[] data, int index) {
- throw new Error("wrong decode attempt");
+ byte[] value = null;
- }
+ if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
- return value;
- }
+ byte lenghtOfLenght = (byte) (data[index] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- public static BigInteger decodeBigInteger(byte[] data, int index){
+ int length = 0;
- BigInteger value = null;
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ length += data[index + i] << (8 * pow);
+ pow--;
+ }
- byte lenghtOfLenght = (byte) (data[index] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- int length = 0;
+ value = valueBytes;
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- length += data[index+ i] << (8 * pow);
- pow--;
- }
+ byte length = (byte) (data[index] - 0x80);
+ byte[] valueBytes = new byte[length];
+ System.arraycopy(data, index, valueBytes, 0, length);
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ value = valueBytes;
- value = new BigInteger(valueBytes);
+ } else {
- } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ throw new Error("wrong decode attempt");
- byte length = (byte) (data[index] - 0x80);
+ }
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ return value;
+ }
- value = new BigInteger(valueBytes);
+ public static int nextItemLength(byte[] data, int index) {
- } else {
+ if (index >= data.length)
+ return -1;
- throw new Error("wrong decode attempt");
+ if ((data[index] & 0xFF) >= 0xF7) {
+ byte lenghtOfLenght = (byte) (data[index] - 0xF7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- }
+ int length = 0;
- return value;
- }
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- public static byte[] decodeByteArray(byte[] data, int index){
+ length += (data[index + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- byte[] value = null;
+ return length;
- if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ }
- byte lenghtOfLenght = (byte) (data[index] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ if ((data[index] & 0xFF) >= 0xC0 && (data[index] & 0xFF) < 0xF7) {
- int length = 0;
+ byte length = (byte) ((data[index] & 0xFF) - 0xC0);
+ return length;
+ }
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
- length += data[index+ i] << (8 * pow);
- pow--;
- }
+ byte lenghtOfLenght = (byte) (data[index] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
+ int length = 0;
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- value = valueBytes;
+ length += (data[index + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- } else if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ return length;
- byte length = (byte) (data[index] - 0x80);
+ }
- byte[] valueBytes = new byte[length];
- System.arraycopy(data, index, valueBytes, 0, length);
+ if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
- value = valueBytes;
+ byte length = (byte) ((data[index] & 0xFF) - 0x80);
+ return length;
+ }
- } else {
+ if ((data[index] & 0xFF) == 0x80) {
- throw new Error("wrong decode attempt");
+ return 1;
+ }
- }
+ if ((data[index] & 0xFF) < 0x80) {
- return value;
- }
+ return 1;
+ }
+ return -1;
+ }
- public static int nextItemLength(byte[] data, int index){
+ public static byte[] decodeIP4Bytes(byte[] data, int index) {
+ int length = (data[index] & 0xFF) - 0xC0;
+ int offset = 1;
- if (index >= data.length) return -1;
+ byte aByte = decodeOneByteItem(data, index + offset);
- if ((data[index] & 0xFF) >= 0xF7){
- byte lenghtOfLenght = (byte) (data[index] - 0xF7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ if ((data[index + offset] & 0xFF) > 0x80)
+ offset = offset + 2;
+ else
+ offset = offset + 1;
+ byte bByte = decodeOneByteItem(data, index + offset);
- int length = 0;
+ if ((data[index + offset] & 0xFF) > 0x80)
+ offset = offset + 2;
+ else
+ offset = offset + 1;
+ byte cByte = decodeOneByteItem(data, index + offset);
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ if ((data[index + offset] & 0xFF) > 0x80)
+ offset = offset + 2;
+ else
+ offset = offset + 1;
+ byte dByte = decodeOneByteItem(data, index + offset);
- length += (data[index + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ byte[] ip = new byte[4];
+ ip[0] = aByte;
+ ip[1] = bByte;
+ ip[2] = cByte;
+ ip[3] = dByte;
- return length;
+ return ip;
+ }
+ public static int getFirstListElement(byte[] payload, int pos) {
- }
+ if (pos >= payload.length)
+ return -1;
- if ((data[index] & 0xFF) >= 0xC0 && (data[index] & 0xFF) < 0xF7){
+ if ((payload[pos] & 0xFF) >= 0xF7) {
- byte length = (byte) ((data[index] & 0xFF) - 0xC0);
- return length;
- }
+ byte lenghtOfLenght = (byte) (payload[pos] - 0xF7);
+ return pos + lenghtOfLenght + 1;
+ }
- if ((data[index] & 0xFF) >= 0xB7 && (data[index] & 0xFF) < 0xC0) {
+ if ((payload[pos] & 0xFF) >= 0xC0 && (payload[pos] & 0xFF) < 0xF7) {
- byte lenghtOfLenght = (byte) (data[index] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ byte length = (byte) ((payload[pos] & 0xFF) - 0xC0);
+ return pos + 1;
+ }
- int length = 0;
+ if ((payload[pos] & 0xFF) >= 0xB7 && (payload[pos] & 0xFF) < 0xC0) {
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ byte lenghtOfLenght = (byte) (payload[pos] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- length += (data[index + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ int length = 0;
- return length ;
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
+ length += (payload[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- }
+ return pos + lenghtOfLenght + 1;
+ }
- if ((data[index] & 0xFF) > 0x80 && (data[index] & 0xFF) < 0xB7) {
+ return -1;
+ }
- byte length = (byte) ((data[index] & 0xFF) - 0x80);
- return length;
- }
+ public static int getNextElementIndex(byte[] payload, int pos) {
+ if (pos >= payload.length)
+ return -1;
- if ((data[index] & 0xFF) == 0x80){
+ if ((payload[pos] & 0xFF) >= 0xF7) {
+ byte lenghtOfLength = (byte) (payload[pos] - 0xF7);
+ byte pow = (byte) (lenghtOfLength - 1);
- return 1;
- }
+ int length = 0;
- if ((data[index] & 0xFF) < 0x80) {
+ for (int i = 1; i <= lenghtOfLength; ++i) {
- return 1;
- }
+ length += (payload[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- return -1;
- }
+ return pos + lenghtOfLength + length + 1;
+ }
+ if ((payload[pos] & 0xFF) >= 0xC0 && (payload[pos] & 0xFF) < 0xF7) {
- public static byte[] decodeIP4Bytes(byte[] data, int index){
+ byte length = (byte) ((payload[pos] & 0xFF) - 0xC0);
+ return pos + 1 + length;
+ }
- int length = (data[index] & 0xFF) - 0xC0;
- int offset = 1;
+ if ((payload[pos] & 0xFF) >= 0xB7 && (payload[pos] & 0xFF) < 0xC0) {
- byte aByte = decodeOneByteItem(data, index + offset);
+ byte lenghtOfLenght = (byte) (payload[pos] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- if ((data[index + offset] & 0xFF) > 0x80) offset = offset + 2; else offset = offset + 1;
- byte bByte = decodeOneByteItem(data, index + offset);
+ int length = 0;
- if ((data[index + offset] & 0xFF) > 0x80) offset = offset + 2; else offset = offset + 1;
- byte cByte = decodeOneByteItem(data, index + offset);
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- if ((data[index + offset] & 0xFF) > 0x80) offset = offset + 2; else offset = offset + 1;
- byte dByte = decodeOneByteItem(data, index + offset);
+ length += (payload[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- byte[] ip = new byte[4];
- ip[0] = aByte;
- ip[1] = bByte;
- ip[2] = cByte;
- ip[3] = dByte;
+ return pos + lenghtOfLenght + length + 1;
+ }
- return ip;
- }
+ if ((payload[pos] & 0xFF) > 0x80 && (payload[pos] & 0xFF) < 0xB7) {
- public static int getFirstListElement(byte[] payload, int pos) {
+ byte length = (byte) ((payload[pos] & 0xFF) - 0x80);
+ return pos + 1 + length;
+ }
- if (pos >= payload.length) return -1;
+ if ((payload[pos] & 0xFF) == 0x80) {
- if ((payload[pos] & 0xFF) >= 0xF7){
+ return pos + 1;
+ }
- byte lenghtOfLenght = (byte) (payload[pos] - 0xF7);
+ if ((payload[pos] & 0xFF) < 0x80) {
- return pos + lenghtOfLenght + 1 ;
- }
+ return pos + 1;
+ }
- if ((payload[pos] & 0xFF) >= 0xC0 && (payload[pos] & 0xFF) < 0xF7){
+ return -1;
+ }
- byte length = (byte) ((payload[pos] & 0xFF) - 0xC0);
- return pos + 1;
- }
+ /**
+ * Get exactly one message payload
+ */
+ public static void fullTraverse(byte[] msgData, int level, int startPos,
+ int endPos, int levelToIndex, Queue index) {
+ try {
- if ((payload[pos] & 0xFF) >= 0xB7 && (payload[pos] & 0xFF) < 0xC0) {
+ if (msgData == null || msgData.length == 0)
+ return;
+ int pos = startPos;
- byte lenghtOfLenght = (byte) (payload[pos] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ while (pos < endPos) {
- int length = 0;
+ if (level == levelToIndex)
+ index.add(new Integer(pos));
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ // It's a list with a payload more than 55 bytes
+ // data[0] - 0xF7 = how many next bytes allocated
+ // for the length of the list
+ if ((msgData[pos] & 0xFF) >= 0xF7) {
- length += (payload[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ byte lenghtOfLenght = (byte) (msgData[pos] - 0xF7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- return pos + lenghtOfLenght + 1 ;
- }
+ int length = 0;
- return -1;
- }
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
+ length += (msgData[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
+ // now we can parse an item for data[1]..data[length]
+ System.out.println("-- level: [" + level
+ + "] Found big list length: " + length);
+ fullTraverse(msgData, level + 1, pos + lenghtOfLenght + 1,
+ pos + lenghtOfLenght + length, levelToIndex, index);
+ pos += lenghtOfLenght + length + 1;
+ continue;
+ }
+ // It's a list with a payload less than 55 bytes
+ if ((msgData[pos] & 0xFF) >= 0xC0
+ && (msgData[pos] & 0xFF) < 0xF7) {
- public static int getNextElementIndex(byte[] payload, int pos) {
+ byte length = (byte) ((msgData[pos] & 0xFF) - 0xC0);
- if (pos >= payload.length) return -1;
+ System.out.println("-- level: [" + level
+ + "] Found small list length: " + length);
- if ((payload[pos] & 0xFF) >= 0xF7){
- byte lenghtOfLenght = (byte) (payload[pos] - 0xF7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ fullTraverse(msgData, level + 1, pos + 1, pos + length + 1,
+ levelToIndex, index);
- int length = 0;
+ pos += 1 + length;
+ continue;
+ }
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ // It's an item with a payload more than 55 bytes
+ // data[0] - 0xB7 = how much next bytes allocated for
+ // the length of the string
+ if ((msgData[pos] & 0xFF) >= 0xB7
+ && (msgData[pos] & 0xFF) < 0xC0) {
- length += (payload[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ byte lenghtOfLenght = (byte) (msgData[pos] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- return pos + lenghtOfLenght + length + 1 ;
+ int length = 0;
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- }
+ length += (msgData[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- if ((payload[pos] & 0xFF) >= 0xC0 && (payload[pos] & 0xFF) < 0xF7){
+ // now we can parse an item for data[1]..data[length]
+ System.out.println("-- level: [" + level
+ + "] Found big item length: " + length);
+ pos += lenghtOfLenght + length + 1;
- byte length = (byte) ((payload[pos] & 0xFF) - 0xC0);
- return pos + 1 + length;
- }
+ continue;
+ }
+ // It's an item less than 55 bytes long,
+ // data[0] - 0x80 == lenght of the item
+ if ((msgData[pos] & 0xFF) > 0x80
+ && (msgData[pos] & 0xFF) < 0xB7) {
- if ((payload[pos] & 0xFF) >= 0xB7 && (payload[pos] & 0xFF) < 0xC0) {
+ byte length = (byte) ((msgData[pos] & 0xFF) - 0x80);
- byte lenghtOfLenght = (byte) (payload[pos] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ System.out.println("-- level: [" + level
+ + "] Found small item length: " + length);
+ pos += 1 + length;
- int length = 0;
+ continue;
+ }
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ // null item
+ if ((msgData[pos] & 0xFF) == 0x80) {
+ System.out.println("-- level: [" + level
+ + "] Found null item: ");
+ pos += 1;
+ continue;
+ }
- length += (payload[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ // single byte item
+ if ((msgData[pos] & 0xFF) < 0x80) {
+ System.out.println("-- level: [" + level
+ + "] Found single item: ");
+ pos += 1;
+ continue;
+ }
+ }
+ } catch (Throwable th) {
+ throw new Error("wire packet not parsed correctly",
+ th.fillInStackTrace());
+ }
+ }
- return pos + lenghtOfLenght + length + 1 ;
- }
+ /**
+ * Parse wire byte[] message into RLP elements
+ *
+ * @param msgData
+ * - raw RLP data
+ * @param rlpList
+ * - outcome of recursive RLP structure
+ */
+ public static void parseObjects(byte[] msgData, RLPList rlpList) {
+ RLP.fullTraverse2(msgData, 0, 0, msgData.length, 1, rlpList);
+ }
- if ((payload[pos] & 0xFF) > 0x80 && (payload[pos] & 0xFF) < 0xB7) {
+ /**
+ * Get exactly one message payload
+ */
+ public static void fullTraverse2(byte[] msgData, int level, int startPos,
+ int endPos, int levelToIndex, RLPList rlpList) {
- byte length = (byte) ((payload[pos] & 0xFF) - 0x80);
- return pos + 1 + length;
- }
+ try {
+
+ if (msgData == null || msgData.length == 0)
+ return;
+ int pos = startPos;
+
+ while (pos < endPos) {
+ // It's a list with a payload more than 55 bytes
+ // data[0] - 0xF7 = how many next bytes allocated
+ // for the length of the list
+ if ((msgData[pos] & 0xFF) >= 0xF7) {
- if ((payload[pos] & 0xFF) == 0x80){
+ byte lenghtOfLenght = (byte) (msgData[pos] - 0xF7);
+ byte pow = (byte) (lenghtOfLenght - 1);
+
+ int length = 0;
+
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- return pos + 1;
- }
+ length += (msgData[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
- if ((payload[pos] & 0xFF) < 0x80) {
+ byte[] rlpData = new byte[lenghtOfLenght + length + 1];
+ System.arraycopy(msgData, pos, rlpData, 0, lenghtOfLenght
+ + length + 1);
+
+ RLPList newLevelList = new RLPList();
+ newLevelList.setRLPData(rlpData);
+
+ // todo: this done to get some data for testing should be
+ // delete
+ // byte[] subList = Arrays.copyOfRange(msgData, pos, pos +
+ // lenghtOfLenght + length + 1);
+ // System.out.println(Utils.toHexString(subList));
+
+ fullTraverse2(msgData, level + 1, pos + lenghtOfLenght + 1,
+ pos + lenghtOfLenght + length + 1, levelToIndex,
+ newLevelList);
+ rlpList.addItem(newLevelList);
+
+ pos += lenghtOfLenght + length + 1;
+ continue;
+ }
+
+ // It's a list with a payload less than 55 bytes
+ if ((msgData[pos] & 0xFF) >= 0xC0
+ && (msgData[pos] & 0xFF) < 0xF7) {
+
+ byte length = (byte) ((msgData[pos] & 0xFF) - 0xC0);
+
+ byte[] rlpData = new byte[length + 1];
+ System.arraycopy(msgData, pos, rlpData, 0, length + 1);
+
+ RLPList newLevelList = new RLPList();
+ newLevelList.setRLPData(rlpData);
+
+ if (length > 0)
+ fullTraverse2(msgData, level + 1, pos + 1, pos + length
+ + 1, levelToIndex, newLevelList);
+ rlpList.addItem(newLevelList);
+
+ pos += 1 + length;
+ continue;
+ }
+
+ // It's an item with a payload more than 55 bytes
+ // data[0] - 0xB7 = how much next bytes allocated for
+ // the length of the string
+ if ((msgData[pos] & 0xFF) >= 0xB7
+ && (msgData[pos] & 0xFF) < 0xC0) {
+
+ byte lenghtOfLenght = (byte) (msgData[pos] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
+
+ int length = 0;
+
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
+
+ length += (msgData[pos + i] & 0xFF) << (8 * pow);
+ pow--;
+ }
+
+ // now we can parse an item for data[1]..data[length]
+ byte[] item = new byte[length];
+ System.arraycopy(msgData, pos + lenghtOfLenght + 1, item,
+ 0, length);
+
+ byte[] rlpPrefix = new byte[lenghtOfLenght + 1];
+ System.arraycopy(msgData, pos, rlpPrefix, 0,
+ lenghtOfLenght + 1);
- return pos + 1;
- }
+ RLPItem rlpItem = new RLPItem(item);
+ rlpList.addItem(rlpItem);
+ pos += lenghtOfLenght + length + 1;
+ continue;
+ }
+ // It's an item less than 55 bytes long,
+ // data[0] - 0x80 == length of the item
+ if ((msgData[pos] & 0xFF) > 0x80
+ && (msgData[pos] & 0xFF) < 0xB7) {
- return -1;
- }
+ byte length = (byte) ((msgData[pos] & 0xFF) - 0x80);
+ byte[] item = new byte[length];
+ System.arraycopy(msgData, pos + 1, item, 0, length);
- /**
- * Get exactly one message payload
- */
- public static void fullTraverse(byte [] msgData, int level, int startPos, int endPos,
- int levelToIndex, Queue index) {
+ byte[] rlpPrefix = new byte[2];
+ System.arraycopy(msgData, pos, rlpPrefix, 0, 2);
- try{
+ RLPItem rlpItem = new RLPItem(item);
+ rlpList.addItem(rlpItem);
+ pos += 1 + length;
- if (msgData == null || msgData.length == 0) return ;
- int pos = startPos;
+ continue;
+ }
- while(pos < endPos){
+ // null item
+ if ((msgData[pos] & 0xFF) == 0x80) {
- if (level == levelToIndex) index.add(new Integer(pos));
+ byte[] item = new byte[0];
+ RLPItem rlpItem = new RLPItem(item);
+ rlpList.addItem(rlpItem);
+ pos += 1;
+ continue;
+ }
- // It's a list with a payload more than 55 bytes
- // data[0] - 0xF7 = how many next bytes allocated
- // for the length of the list
- if ((msgData[pos] & 0xFF) >= 0xF7){
+ // single byte item
+ if ((msgData[pos] & 0xFF) < 0x80) {
- byte lenghtOfLenght = (byte) (msgData[pos] - 0xF7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ byte[] item = { (byte) (msgData[pos] & 0xFF) };
- int length = 0;
+ RLPItem rlpItem = new RLPItem(item);
+ rlpList.addItem(rlpItem);
+ pos += 1;
+ continue;
+ }
+ }
+ } catch (Throwable th) {
+ throw new Error("wire packet not parsed correctly",
+ th.fillInStackTrace());
+ }
+ }
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ /*
+ * def rlp_encode(input): if isinstance(input,str): if len(input) == 1 and
+ * chr(input) < 128: return input else: return encode_length(len(input),128)
+ * + input elif isinstance(input,list): output = '' for item in input:
+ * output += rlp_encode(item) return encode_length(len(output),192) + output
+ *
+ * def encode_length(L,offset): if L < 56: return chr(L + offset) elif L <
+ * 256**8: BL = to_binary(L) return chr(len(BL) + offset + 55) + BL else:
+ * raise Exception("input too long")
+ *
+ * def to_binary(x): return '' if x == 0 else to_binary(int(x / 256)) +
+ * chr(x % 256)
+ */
- length += (msgData[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ private static String rlpEncode(Object item) {
- // now we can parse an item for data[1]..data[length]
- System.out.println("-- level: [" + level + "] Found big list length: " + length);
+ if (item instanceof String) {
- fullTraverse(msgData, level + 1, pos + lenghtOfLenght + 1, pos + lenghtOfLenght + length,
- levelToIndex, index);
+ String str = ((String) item);
+ int length = str.length();
+ if (length == 1 && str.charAt(0) < 128)
+ return str;
+ else
+ return encodeLenght(str.length(), 128) + str;
+ } else if (item instanceof List) {
- pos += lenghtOfLenght + length + 1 ;
- continue;
- }
+ List itemList = (List) item;
+ StringBuilder output = new StringBuilder();
+ for (Object oneItem : itemList)
+ output.append(rlpEncode(oneItem));
+ return encodeLenght(output.toString().length(), 192)
+ + output.toString();
+ }
- // It's a list with a payload less than 55 bytes
- if ((msgData[pos] & 0xFF) >= 0xC0 && (msgData[pos] & 0xFF) < 0xF7){
+ throw new Error("unsupported type" + item.getClass());
+ }
- byte length = (byte) ((msgData[pos] & 0xFF) - 0xC0);
+ private static String encodeLenght(int L, int offset) {
- System.out.println("-- level: [" + level + "] Found small list length: " + length);
+ if (L < 56)
+ return "" + (char) (L + offset);
+ else if (L < (256 ^ 8)) {
- fullTraverse(msgData, level + 1, pos + 1, pos + length + 1, levelToIndex, index);
+ String BL = toBinary(L);
+ return "" + (char) (BL.length() + offset + 55) + BL;
+ } else
+ throw new Error("input too long");
- pos += 1 + length;
- continue;
- }
+ }
+ public static byte getCommandCode(byte[] data) {
- // It's an item with a payload more than 55 bytes
- // data[0] - 0xB7 = how much next bytes allocated for
- // the length of the string
- if ((msgData[pos] & 0xFF) >= 0xB7 && (msgData[pos] & 0xFF) < 0xC0) {
+ byte command = 0;
- byte lenghtOfLenght = (byte) (msgData[pos] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ int index = getFirstListElement(data, 0);
- int length = 0;
+ command = data[index];
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ command = ((int) (command & 0xFF) == 0x80) ? 0 : command;
- length += (msgData[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ return command;
+ }
+ public static Object decode(char[] data) {
- // now we can parse an item for data[1]..data[length]
- System.out.println("-- level: [" + level + "] Found big item length: " + length);
- pos += lenghtOfLenght + length + 1 ;
+ if (data == null || data.length == 0)
+ return null;
- continue;
- }
+ if (data[0] >= 0xF7) { /*
+ * It's a list with a payload more than 55 bytes
+ * data[0] - 0xF7 = how many next bytes
+ * allocated for the length of the list
+ */
+ ;
+ byte lenghtOfLenght = (byte) (data[0] - 0xF7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- // It's an item less than 55 bytes long,
- // data[0] - 0x80 == lenght of the item
- if ((msgData[pos] & 0xFF) > 0x80 && (msgData[pos] & 0xFF) < 0xB7) {
+ long length = 0;
- byte length = (byte) ((msgData[pos] & 0xFF) - 0x80);
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
- System.out.println("-- level: [" + level + "] Found small item length: " + length);
- pos += 1 + length;
+ length += data[i] << (8 * pow);
+ pow--;
+ }
- continue;
- }
+ System.out.println(length);
- // null item
- if ((msgData[pos] & 0xFF) == 0x80){
- System.out.println("-- level: [" + level + "] Found null item: ");
- pos += 1;
- continue;
- }
+ // now we can parse an item for data[1]..data[length]
+ }
- // single byte item
- if ((msgData[pos] & 0xFF) < 0x80) {
- System.out.println("-- level: [" + level + "] Found single item: ");
- pos += 1;
- continue;
- }
- }
+ if (data[0] >= 0xC0 && data[0] < 0xF7) /*
+ * It's a list with a payload
+ * less than 55 bytes
+ */
+ ;
+ if (data[0] >= 0xB7 && data[0] < 0xC0) {/*
+ * It's an item with a payload
+ * more than 55 bytes data[0] -
+ * 0xB7 = how much next bytes
+ * allocated for the length of
+ * the string
+ */
+ ;
- } catch(Throwable th){
+ byte lenghtOfLenght = (byte) (data[0] - 0xB7);
+ byte pow = (byte) (lenghtOfLenght - 1);
- throw new Error("wire packet not parsed correctly", th.fillInStackTrace());
+ long length = 0;
- }
+ for (int i = 1; i <= lenghtOfLenght; ++i) {
+ length += data[i] << (8 * pow);
+ pow--;
+ }
+ // now we can parse an item for data[1]..data[length]
+ }
+ if (data[0] >= 0x80 && data[0] < 0xB7) {/*
+ * It's an item less than 55
+ * bytes long, data[0] - 0x80 ==
+ * lenght of the item
+ */
+ ;
+ }
- }
+ if (data[0] == 0x80) /* null item */
+ ;
+ if (data[0] < 0x80) /* single byte item */
+ ;
+ return null;
+ }
+ private static String toBinary(int x) {
+ if (x == 0)
+ return "";
+ else
+ return toBinary(x >> 8) + ((char) (x & 0x00FF));
+ }
- /**
- * Parse wire byte[] message into RLP elements
- *
- * @param msgData - raw RLP data
- * @param rlpList - outcome of recursive RLP structure
- */
- public static void parseObjects(byte [] msgData, RLPList rlpList){
+ public static byte[] encodeByte(byte singleByte) {
- RLP.fullTraverse2(msgData, 0, 0, msgData.length, 1, rlpList);
- }
+ if ((singleByte & 0xFF) == 0) {
- /**
- * Get exactly one message payload
- */
- public static void fullTraverse2(byte [] msgData, int level, int startPos, int endPos,
- int levelToIndex, RLPList rlpList) {
+ return new byte[] { (byte) 0x80 };
+ } else if ((singleByte & 0xFF) < 0x7F) {
- try{
+ return new byte[] { singleByte };
+ } else {
- if (msgData == null || msgData.length == 0) return ;
- int pos = startPos;
+ return new byte[] { (byte) 0x81, singleByte };
+ }
+ }
- while(pos < endPos){
+ public static byte[] encodeShort(short singleShort) {
- // It's a list with a payload more than 55 bytes
- // data[0] - 0xF7 = how many next bytes allocated
- // for the length of the list
- if ((msgData[pos] & 0xFF) >= 0xF7){
+ if (singleShort <= 0xFF)
- byte lenghtOfLenght = (byte) (msgData[pos] - 0xF7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ return encodeByte((byte) singleShort);
+ else {
- int length = 0;
+ return new byte[] { (byte) 0x82, (byte) (singleShort >> 8 & 0xFF),
+ (byte) (singleShort >> 0 & 0xFF) };
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ }
+ }
- length += (msgData[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ public static byte[] encodeString(String srcString) {
+ return encodeElement(srcString.getBytes());
+ }
- byte[] rlpData = new byte[lenghtOfLenght + length + 1];
- System.arraycopy(msgData, pos, rlpData, 0, lenghtOfLenght + length + 1);
+ public static byte[] encodeBigInteger(BigInteger srcBigInteger) {
- RLPList newLevelList = new RLPList();
- newLevelList.setRLPData(rlpData);
+ return encodeElement(srcBigInteger.toByteArray());
+ }
+ public static byte[] encodeElement(byte[] srcData) {
-// todo: this done to get some data for testing should be delete
-// byte[] subList = Arrays.copyOfRange(msgData, pos, pos + lenghtOfLenght + length + 1);
-// System.out.println(Utils.toHexString(subList));
+ if (srcData.length <= 0x37) {
+ // length = 8X
+ byte length = (byte) (0x80 + srcData.length);
- fullTraverse2(msgData, level + 1, pos + lenghtOfLenght + 1, pos + lenghtOfLenght + length + 1,
- levelToIndex, newLevelList);
- rlpList.addItem(newLevelList);
+ byte[] data = Arrays.copyOf(srcData, srcData.length + 1);
+ System.arraycopy(data, 0, data, 1, srcData.length);
+ data[0] = length;
- pos += lenghtOfLenght + length + 1;
- continue;
- }
+ return data;
+ } else {
- // It's a list with a payload less than 55 bytes
- if ((msgData[pos] & 0xFF) >= 0xC0 && (msgData[pos] & 0xFF) < 0xF7){
+ // length of length = BX
+ // prefix = [BX, [length]]
+ int tmpLenght = srcData.length;
+ byte byteNum = 0;
+ while (tmpLenght != 0) {
- byte length = (byte) ((msgData[pos] & 0xFF) - 0xC0);
+ ++byteNum;
+ tmpLenght = tmpLenght >> 8;
+ }
- byte[] rlpData = new byte[length + 1];
- System.arraycopy(msgData, pos, rlpData, 0, length + 1);
+ byte[] lenBytes = new byte[byteNum];
+ for (int i = 0; i < byteNum; ++i) {
+ lenBytes[0] = (byte) ((srcData.length >> (8 * i)) & 0xFF);
+ }
- RLPList newLevelList = new RLPList();
- newLevelList.setRLPData(rlpData);
+ // first byte = F7 + bytes.length
+ byte[] data = Arrays.copyOf(srcData, srcData.length + 1 + byteNum);
+ System.arraycopy(data, 0, data, 1 + byteNum, srcData.length);
+ data[0] = (byte) (0xB7 + byteNum);
+ System.arraycopy(lenBytes, 0, data, 1, lenBytes.length);
- if (length > 0)
- fullTraverse2(msgData, level + 1, pos + 1, pos + length + 1, levelToIndex, newLevelList);
- rlpList.addItem(newLevelList);
+ return data;
+ }
+ }
- pos += 1 + length;
- continue;
- }
+ public static byte[] encodeList(byte[]... elements) {
+ int totalLength = 0;
+ for (int i = 0; i < elements.length; ++i) {
- // It's an item with a payload more than 55 bytes
- // data[0] - 0xB7 = how much next bytes allocated for
- // the length of the string
- if ((msgData[pos] & 0xFF) >= 0xB7 && (msgData[pos] & 0xFF) < 0xC0) {
+ totalLength += elements[i].length;
+ }
- byte lenghtOfLenght = (byte) (msgData[pos] - 0xB7);
- byte pow = (byte) (lenghtOfLenght - 1);
+ byte[] data;
+ int copyPos = 0;
+ if (totalLength <= 0x37) {
- int length = 0;
+ data = new byte[1 + totalLength];
+ data[0] = (byte) (0xC0 + totalLength);
+ copyPos = 1;
- for (int i = 1; i <= lenghtOfLenght; ++i){
+ } else {
- length += (msgData[pos + i] & 0xFF) << (8 * pow);
- pow--;
- }
+ // length of length = BX
+ // prefix = [BX, [length]]
+ int tmpLenght = totalLength;
+ byte byteNum = 0;
+ while (tmpLenght != 0) {
+ ++byteNum;
+ tmpLenght = tmpLenght >> 8;
+ }
- // now we can parse an item for data[1]..data[length]
- byte[] item = new byte[length];
- System.arraycopy(msgData, pos + lenghtOfLenght + 1, item, 0, length);
+ tmpLenght = totalLength;
+ byte[] lenBytes = new byte[byteNum];
+ for (int i = 0; i < byteNum; ++i) {
+ lenBytes[i] = (byte) ((tmpLenght >> (8 * i)) & 0xFF);
+ }
- byte[] rlpPrefix = new byte[lenghtOfLenght + 1];
- System.arraycopy(msgData, pos, rlpPrefix, 0, lenghtOfLenght + 1);
+ // first byte = F7 + bytes.length
+ data = new byte[1 + lenBytes.length + totalLength];
+ data[0] = (byte) (0xF7 + byteNum);
+ System.arraycopy(lenBytes, 0, data, 1, lenBytes.length);
- RLPItem rlpItem= new RLPItem(item);
- rlpList.addItem(rlpItem);
- pos += lenghtOfLenght + length + 1 ;
+ copyPos = lenBytes.length + 1;
+ }
+ for (int i = 0; i < elements.length; ++i) {
- continue;
- }
+ System.arraycopy(elements[i], 0, data, copyPos, elements[i].length);
+ copyPos += elements[i].length;
+ }
+ return data;
+ }
- // It's an item less than 55 bytes long,
- // data[0] - 0x80 == length of the item
- if ((msgData[pos] & 0xFF) > 0x80 && (msgData[pos] & 0xFF) < 0xB7) {
+ public static void main(String args[]) {
- byte length = (byte) ((msgData[pos] & 0xFF) - 0x80);
+ char[] data = { 0xF9, 20, 100 };
+ decode(data);
+ }
- byte[] item = new byte[length];
- System.arraycopy(msgData, pos + 1, item, 0, length);
+ public static void main2(String args[]) throws UnsupportedEncodingException {
- byte[] rlpPrefix = new byte[2];
- System.arraycopy(msgData, pos, rlpPrefix, 0, 2);
+ List