Merge pull request #8 from nicksavers/master

Add support for Travis CI + Fix RLP decode for long lists and various TransactionTest fixes
This commit is contained in:
romanman 2014-05-21 19:41:09 +03:00
commit f352d3050e
18 changed files with 266 additions and 330 deletions

1
.travis.yml Normal file
View File

@ -0,0 +1 @@
language: java

View File

@ -1,11 +1,11 @@
package org.ethereum.core;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import java.math.BigInteger;
import java.util.ArrayList;
@ -259,20 +259,20 @@ public class Block {
toStringBuff.setLength(0);
toStringBuff.append("BlockData [\n");
toStringBuff.append(" hash=" + Utils.toHexString(hash)).append("\n");
toStringBuff.append(" parentHash=" + Utils.toHexString(parentHash)).append("\n");
toStringBuff.append(" unclesHash=" + Utils.toHexString(unclesHash)).append("\n");
toStringBuff.append(" coinbase=" + Utils.toHexString(coinbase)).append("\n");
toStringBuff.append(" stateHash=" + Utils.toHexString(stateRoot)).append("\n");
toStringBuff.append(" txTrieHash=" + Utils.toHexString(txTrieRoot)).append("\n");
toStringBuff.append(" difficulty=" + Utils.toHexString(difficulty)).append("\n");
toStringBuff.append(" hash=" + ByteUtil.toHexString(hash)).append("\n");
toStringBuff.append(" parentHash=" + ByteUtil.toHexString(parentHash)).append("\n");
toStringBuff.append(" unclesHash=" + ByteUtil.toHexString(unclesHash)).append("\n");
toStringBuff.append(" coinbase=" + ByteUtil.toHexString(coinbase)).append("\n");
toStringBuff.append(" stateHash=" + ByteUtil.toHexString(stateRoot)).append("\n");
toStringBuff.append(" txTrieHash=" + ByteUtil.toHexString(txTrieRoot)).append("\n");
toStringBuff.append(" difficulty=" + ByteUtil.toHexString(difficulty)).append("\n");
toStringBuff.append(" number=" + number).append("\n");
toStringBuff.append(" minGasPrice=" + minGasPrice).append("\n");
toStringBuff.append(" gasLimit=" + gasLimit).append("\n");
toStringBuff.append(" gasUsed=" + gasUsed).append("\n");
toStringBuff.append(" timestamp=" + timestamp).append("\n");
toStringBuff.append(" extraData=" + Utils.toHexString(extraData)).append("\n");
toStringBuff.append(" nonce=" + Utils.toHexString(nonce)).append("\n");
toStringBuff.append(" extraData=" + ByteUtil.toHexString(extraData)).append("\n");
toStringBuff.append(" nonce=" + ByteUtil.toHexString(nonce)).append("\n");
for (Transaction tx : getTransactionsList()){
@ -289,20 +289,20 @@ public class Block {
toStringBuff.setLength(0);
toStringBuff.append("BlockData [");
toStringBuff.append(" hash=" + Utils.toHexString(hash)).append("");
toStringBuff.append(" parentHash=" + Utils.toHexString(parentHash)).append("");
toStringBuff.append(" unclesHash=" + Utils.toHexString(unclesHash)).append("");
toStringBuff.append(" coinbase=" + Utils.toHexString(coinbase)).append("");
toStringBuff.append(" stateHash=" + Utils.toHexString(stateRoot)).append("");
toStringBuff.append(" txTrieHash=" + Utils.toHexString(txTrieRoot)).append("");
toStringBuff.append(" difficulty=" + Utils.toHexString(difficulty)).append("");
toStringBuff.append(" hash=" + ByteUtil.toHexString(hash)).append("");
toStringBuff.append(" parentHash=" + ByteUtil.toHexString(parentHash)).append("");
toStringBuff.append(" unclesHash=" + ByteUtil.toHexString(unclesHash)).append("");
toStringBuff.append(" coinbase=" + ByteUtil.toHexString(coinbase)).append("");
toStringBuff.append(" stateHash=" + ByteUtil.toHexString(stateRoot)).append("");
toStringBuff.append(" txTrieHash=" + ByteUtil.toHexString(txTrieRoot)).append("");
toStringBuff.append(" difficulty=" + ByteUtil.toHexString(difficulty)).append("");
toStringBuff.append(" number=" + number).append("");
toStringBuff.append(" minGasPrice=" + minGasPrice).append("");
toStringBuff.append(" gasLimit=" + gasLimit).append("");
toStringBuff.append(" gasUsed=" + gasUsed).append("");
toStringBuff.append(" timestamp=" + timestamp).append("");
toStringBuff.append(" extraData=" + Utils.toHexString(extraData)).append("");
toStringBuff.append(" nonce=" + Utils.toHexString(nonce)).append("");
toStringBuff.append(" extraData=" + ByteUtil.toHexString(extraData)).append("");
toStringBuff.append(" nonce=" + ByteUtil.toHexString(nonce)).append("");
for (Transaction tx : getTransactionsList()){

View File

@ -3,12 +3,11 @@ package org.ethereum.core;
import org.ethereum.crypto.ECKey.ECDSASignature;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.BigIntegers;
import org.spongycastle.util.encoders.Hex;
import java.util.Arrays;
@ -128,7 +127,6 @@ public class Transaction {
if (!parsed) rlpParse();
byte[] plainMsg = this.getEncoded();
return HashUtil.sha3(plainMsg);
}
@ -204,17 +202,17 @@ public class Transaction {
@Override
public String toString() {
if (!parsed) rlpParse();
return "TransactionData [" + " hash=" + Utils.toHexString(hash) +
" nonce=" + Utils.toHexString(nonce) +
", value=" + Utils.toHexString(value) +
", receiveAddress=" + Utils.toHexString(receiveAddress) +
", gasPrice=" + Utils.toHexString(gasPrice) +
", gas=" + Utils.toHexString(gasLimit) +
", data=" + Utils.toHexString(data) +
", init=" + Utils.toHexString(init) +
return "TransactionData [" + " hash=" + ByteUtil.toHexString(hash) +
" nonce=" + ByteUtil.toHexString(nonce) +
", value=" + ByteUtil.toHexString(value) +
", receiveAddress=" + ByteUtil.toHexString(receiveAddress) +
", gasPrice=" + ByteUtil.toHexString(gasPrice) +
", gas=" + ByteUtil.toHexString(gasLimit) +
", data=" + ByteUtil.toHexString(data) +
", init=" + ByteUtil.toHexString(init) +
", signatureV=" + signature.v +
", signatureR=" + Utils.toHexString(signature.r.toByteArray()) +
", signatureS=" + Utils.toHexString(signature.s.toByteArray()) +
", signatureR=" + ByteUtil.toHexString(signature.r.toByteArray()) +
", signatureS=" + ByteUtil.toHexString(signature.s.toByteArray()) +
']';
}
@ -241,7 +239,6 @@ public class Transaction {
this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value,
data);
}
return rlpEncoded;
}
@ -257,12 +254,8 @@ public class Transaction {
byte[] data = RLP.encodeElement(this.data);
byte[] v = RLP.encodeByte( signature.v );
byte[] rBytes = BigIntegers.asUnsignedByteArray(signature.r);
System.out.println(Hex.toHexString(rBytes));
byte[] r = RLP.encodeElement(rBytes);
byte[] sBytes = BigIntegers.asUnsignedByteArray(signature.s);
System.out.println(Hex.toHexString(sBytes));
byte[] s = RLP.encodeElement(sBytes);
byte[] r = RLP.encodeElement(BigIntegers.asUnsignedByteArray(signature.r));
byte[] s = RLP.encodeElement(BigIntegers.asUnsignedByteArray(signature.s));
if(Arrays.equals(this.receiveAddress, new byte[0])) {
byte[] init = RLP.encodeElement(this.init);

View File

@ -6,12 +6,13 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.core.Transaction;
import org.ethereum.gui.PeerListener;
import org.ethereum.manager.MainData;
import org.ethereum.net.message.StaticMessages;
import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.util.Utils;
import org.ethereum.util.ByteUtil;
import org.spongycastle.util.encoders.Hex;
import java.util.ArrayList;
@ -97,7 +98,7 @@ public class ClientPeer {
ByteBuf buffer = channel.alloc().buffer(payload.length + 8);
buffer.writeBytes(StaticMessages.MAGIC_PACKET);
buffer.writeBytes(Utils.calcPacketSize(payload));
buffer.writeBytes(ByteUtil.calcPacketSize(payload));
buffer.writeBytes(payload);
System.out.println("Send msg: [ " +

View File

@ -5,7 +5,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption;
import io.netty.channel.FixedRecvByteBufAllocator;
import org.ethereum.core.Block;
import org.ethereum.gui.PeerListener;
import org.ethereum.manager.MainData;
import org.ethereum.net.Command;
@ -13,10 +12,8 @@ import org.ethereum.net.message.*;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@ -86,7 +83,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
byte[] payload = (byte[]) msg;
System.out.print("msg: ");
Utils.printHexStringForByteArray(payload);
ByteUtil.printHexStringForByteArray(payload);
byte command = RLP.getCommandCode(payload);
@ -218,7 +215,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
ByteBuf buffer = ctx.alloc().buffer(chainMessage.getPayload().length + 8);
buffer.writeBytes(StaticMessages.MAGIC_PACKET);
buffer.writeBytes(Utils.calcPacketSize(chainMessage.getPayload()));
buffer.writeBytes(ByteUtil.calcPacketSize(chainMessage.getPayload()));
buffer.writeBytes(chainMessage.getPayload());
ctx.writeAndFlush(buffer);

View File

@ -27,7 +27,6 @@ import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
/**
@ -121,7 +120,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
byte[] payload = (byte[]) msg;
System.out.print("msg: ");
Utils.printHexStringForByteArray(payload);
ByteUtil.printHexStringForByteArray(payload);
byte command = RLP.getCommandCode(payload);
// got HELLO
@ -344,7 +343,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
ByteBuf buffer = ctx.alloc().buffer(chainMessage.getPayload().length + 8);
buffer.writeBytes(StaticMessages.MAGIC_PACKET);
buffer.writeBytes(Utils.calcPacketSize(chainMessage.getPayload()));
buffer.writeBytes(ByteUtil.calcPacketSize(chainMessage.getPayload()));
buffer.writeBytes(chainMessage.getPayload());
ctx.writeAndFlush(buffer);

View File

@ -10,7 +10,6 @@ import org.ethereum.net.Command;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
/**
@ -87,7 +86,7 @@ public class GetChainMessage extends Message {
StringBuffer sb = new StringBuffer();
for (byte[] blockHash : blockHashList){
sb.append("").append(Utils.toHexString(blockHash)).append(", ");
sb.append("").append(Hex.toHexString(blockHash)).append(", ");
}
sb.append(" blockNum=").append(blockNum);

View File

@ -6,7 +6,7 @@ import org.ethereum.net.Command;
import org.ethereum.net.message.Message;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
/**
* www.ethereumJ.com
@ -43,6 +43,6 @@ public class NotInChainMessage extends Message {
public String toString(){
if (!parsed)
parseRLP();
return "NotInChain Message [" + Utils.toHexString(hash) + "]";
return "NotInChain Message [" + Hex.toHexString(hash) + "]";
}
}

View File

@ -29,7 +29,7 @@ public class StaticMessages {
public static final byte[] DISCONNECT_00 = Hex.decode("2240089100000003C20100");
public static final byte[] GET_CHAIN = Hex.decode("2240089100000027F82514A069A7356A245F9DC5B865475ADA5EE4E89B18F93C06503A9DB3B3630E88E9FB4E820100");
public static final byte[] GENESIS_HASH = Hex.decode("f5232afe32aba6b366f8aa86a6939437c5e13d1fd71a0f51e77735d3456eb1a6");
public static final byte[] GENESIS_HASH = Hex.decode("c305511e7cb9b33767e50f5e94ecd7b1c51359a04f45183860ec6808d80b0d3f");
public static final byte[] MAGIC_PACKET = Hex.decode("22400891");
static {

View File

@ -3,6 +3,8 @@ package org.ethereum.util;
import java.math.BigInteger;
import java.util.Arrays;
import org.spongycastle.util.encoders.Hex;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLongs;
@ -37,6 +39,64 @@ public class ByteUtil {
return bytes;
}
public static byte[] hexStringToByteArr(String hexString){
String hexSymbols = "0123456789ABCDEF";
int arrSize = (int) (hexString.length() / 3);
byte[] result = new byte[arrSize];
for (int i = 0; i < arrSize; ++i){
int digit1 = hexSymbols.indexOf( hexString.charAt(i * 3) );
int digit2 = hexSymbols.indexOf( hexString.charAt(i * 3 + 1) );
result[i] = (byte) (digit1 * 16 + digit2);
}
return result;
}
public static String toHexString(byte[] data){
if (data == null) return "null";
else return Hex.toHexString(data);
}
public static void printHexStringForByte(byte data){
System.out.print("[");
String hexNum = Integer.toHexString ((int) data & 0xFF);
if (((int) data & 0xFF) < 16) {
hexNum = "0" + hexNum;
}
System.out.print( hexNum );
System.out.print("]");
System.out.println();
}
public static void printHexStringForByteArray(byte[] data){
System.out.print("[");
for (int i = 0; i < data.length; ++i){
String hexNum = Integer.toHexString ((int) data[i] & 0xFF);
if (((int) data[i] & 0xFF) < 16) {
hexNum = "0" + hexNum;
}
System.out.print( hexNum );
System.out.print(" ");
}
System.out.print("]");
System.out.println();
}
// The packet size should be 4 byte long
public static byte[] calcPacketSize(byte[] packet){
byte[] size = new byte[4];
size[3] = (byte)(packet.length >> 0 & 0xFF);
size[2] = (byte)(packet.length >> 8 & 0xFF);
size[1] = (byte)(packet.length >> 16 & 0xFF);
size[0] = (byte)(packet.length >> 24 & 0xFF);
return size;
}
/**
* Calculate packet length
* @param msg

View File

@ -644,9 +644,8 @@ public class RLP {
continue;
}
}
} catch (Throwable th) {
throw new RuntimeException("wire packet not parsed correctly",
th.fillInStackTrace());
} catch (Exception e) {
throw new RuntimeException("wire packet not parsed correctly", e);
}
}
@ -679,9 +678,9 @@ public class RLP {
int prevPos = pos; pos++;
return decodeList(data, pos, prevPos, len);
} else if (prefix < 0xFF) {
int lenlen = prefix - OFFSET_LONG_LIST + 1; // length of length the encoded list
int lenlen = prefix - OFFSET_LONG_LIST; // length of length the encoded list
int lenlist = byteArrayToInt(copyOfRange(data, pos+1, pos+1+lenlen)); // length of encoded bytes
pos = pos + lenlen + 1;
pos = pos + lenlen + 1; // start at position of first element in list
int prevPos = lenlist;
return decodeList(data, pos, prevPos, lenlist);
} else {

View File

@ -2,6 +2,8 @@ package org.ethereum.util;
import java.util.ArrayList;
import org.spongycastle.util.encoders.Hex;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
@ -32,7 +34,7 @@ public class RLPList extends ArrayList<RLPElement> implements RLPElement {
}
System.out.print("]");
} else {
String hex = Utils.toHexString(((RLPItem) element).getRLPData());
String hex = ByteUtil.toHexString(((RLPItem) element).getRLPData());
System.out.print(hex + ", ");
}
}

View File

@ -9,46 +9,10 @@ import javax.swing.ImageIcon;
import org.spongycastle.util.encoders.Hex;
/**
* www.ethereumj.com
* User: Roman Mandeleil
* Created on: 06/04/14 13:13
*/
public class Utils {
private static SecureRandom random = new SecureRandom();
public static byte[] asUnsignedByteArray(BigInteger bigInteger) {
byte[] array = bigInteger.toByteArray();
if (array[0] == 0) {
byte[] tmp = new byte[array.length - 1];
System.arraycopy(array, 1, tmp, 0, tmp.length);
array = tmp;
}
return array;
}
public static byte[] hexStringToByteArr(String hexString){
String hexSymbols = "0123456789ABCDEF";
int arrSize = (int) (hexString.length() / 3);
byte[] result = new byte[arrSize];
for (int i = 0; i < arrSize; ++i){
int digit1 = hexSymbols.indexOf( hexString.charAt(i * 3) );
int digit2 = hexSymbols.indexOf( hexString.charAt(i * 3 + 1) );
result[i] = (byte) (digit1 * 16 + digit2);
}
return result;
}
public static String toHexString(byte[] data){
if (data == null) return "null";
else return Hex.toHexString(data);
}
/**
* @param hexNum should be in form '0x34fabd34....'
* @return
@ -62,57 +26,18 @@ public class Utils {
return (new BigInteger(1, numberBytes)).toString();
}
public static void printHexStringForByte(byte data){
System.out.print("[");
String hexNum = Integer.toHexString ((int) data & 0xFF);
if (((int) data & 0xFF) < 16) {
hexNum = "0" + hexNum;
}
System.out.print( hexNum );
System.out.print("]");
System.out.println();
}
public static void printHexStringForByteArray(byte[] data){
System.out.print("[");
for (int i = 0; i < data.length; ++i){
String hexNum = Integer.toHexString ((int) data[i] & 0xFF);
if (((int) data[i] & 0xFF) < 16) {
hexNum = "0" + hexNum;
}
System.out.print( hexNum );
System.out.print(" ");
}
System.out.print("]");
System.out.println();
}
public static ImageIcon getImageIcon(String resource){
URL imageURL = ClassLoader.getSystemResource(resource);
ImageIcon image = new ImageIcon(imageURL);
return image;
}
// The packet size should be 4 byte long
public static byte[] calcPacketSize(byte[] packet){
byte[] size = new byte[4];
size[3] = (byte)(packet.length >> 0 & 0xFF);
size[2] = (byte)(packet.length >> 8 & 0xFF);
size[1] = (byte)(packet.length >> 16 & 0xFF);
size[0] = (byte)(packet.length >> 24 & 0xFF);
return size;
}
static BigInteger _1000_ = new BigInteger("1000");
public static String getValueShortString(BigInteger number){
BigInteger result = number;
int pow = 0;
while (result.compareTo(_1000_) == 1 || result.compareTo(_1000_) == 0){
result = result.divide(_1000_);
pow += 3;
}

View File

@ -6,20 +6,14 @@ import org.ethereum.util.RLPList;
import org.spongycastle.util.encoders.Hex;
import org.ethereum.core.Block;
import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.RLP;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
public class BlockTest {
// https://ethereum.etherpad.mozilla.org/12
private String CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH = "12582945fc5ad12c3e7b67c4fc37a68fc0d52d995bb7f7291ff41a2739a7ca16";
private String CPP_PoC5_GENESIS_HEX_RLP_ENCODED = "f8abf8a7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a012582945fc5ad12c3e7b67c4fc37a68fc0d52d995bb7f7291ff41a2739a7ca1680834000008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0";
private String CPP_PoC5_GENESIS_HEX_HASH = Hex.toHexString(StaticMessages.GENESIS_HASH);
@ -27,9 +21,11 @@ public class BlockTest {
public void testGenesisFromRLP(){
// from RLP encoding
byte[] genesisBytes = Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
Block genesis = new Block(genesisBytes);
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
assertEquals(CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH, Hex.toHexString(genesis.getStateRoot()));
Block genesisFromRLP = new Block(genesisBytes);
Genesis genesis = new Genesis();
assertEquals(Hex.toHexString(genesis.getHash()), Hex.toHexString(genesisFromRLP.getHash()));
assertEquals(Hex.toHexString(genesis.getParentHash()), Hex.toHexString(genesisFromRLP.getParentHash()));
assertEquals(Hex.toHexString(genesis.getStateRoot()), Hex.toHexString(genesisFromRLP.getStateRoot()));
}
@Test
@ -53,8 +49,6 @@ public class BlockTest {
)
*/
Block genesis = new Genesis();
System.out.println(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
System.out.println(Hex.toHexString(genesis.getEncoded()));
assertEquals(CPP_PoC5_GENESIS_HEX_RLP_ENCODED, Hex.toHexString(genesis.getEncoded()));
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
}

View File

@ -10,6 +10,7 @@ import java.security.NoSuchProviderException;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
import org.spongycastle.util.BigIntegers;
@ -17,9 +18,9 @@ import org.spongycastle.util.encoders.Hex;
public class TransactionTest {
private static String RLP_ENCODED_TX = "e88085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc1000080";
private static String HASH_RAW_TX = "";
private static String HASH_SIGNED_TX = "";
private static String RLP_ENCODED_RAW_TX = "ed808b00d3c21bcecceda10000009479b08ad8787060333663d19704909ee7b1903e588609184e72a00082425580";
private static String HASH_RAW_TX = "c957fce141839221403b51d26a5de186db2dabe0de4ac48f3f6718bfeb7c5f47";
private static String RLP_ENCODED_SIGNED_TX = "f870808b00d3c21bcecceda10000009479b08ad8787060333663d19704909ee7b1903e588609184e72a000824255801ca08e7dfa371b0acde61f894f1969f1f17696b86492a8572c60154e85d7801a4a08a0229807de94c4cfa63d978ff22f764cd9e6abd1bae1bcdba4aa4ae299ad0a8a9f";
@Test /* sign transaction https://tools.ietf.org/html/rfc6979 */
public void test1() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, IOException {
@ -104,29 +105,32 @@ public class TransactionTest {
@Test
public void testTransactionFromRLP() {
// from RLP encoding
byte[] encodedTxBytes = Hex.decode(RLP_ENCODED_TX);
fail("Double check the expected values, they don't seem to be parsed in the right order.");
byte[] encodedTxBytes = Hex.decode(RLP_ENCODED_SIGNED_TX);
Transaction tx = new Transaction(encodedTxBytes);
assertNull(Hex.toHexString(tx.getNonce()));
assertNull(Hex.toHexString(tx.getValue()));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getReceiveAddress()));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getGasPrice()));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getGasLimit()));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getData()));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getInit()));
assertNull(tx.getNonce());
assertEquals(BigInteger.valueOf(16981), new BigInteger(tx.getValue()));
assertEquals("09184e72a000", ByteUtil.toHexString(tx.getReceiveAddress()));
assertEquals("00d3c21bcecceda1000000", ByteUtil.toHexString(tx.getGasPrice()));
assertEquals("79b08ad8787060333663d19704909ee7b1903e58", ByteUtil.toHexString(tx.getGasLimit()));
assertNull(tx.getData());
assertNull(tx.getInit());
assertEquals(28, tx.getSignature().v);
assertEquals("c2604bd6eeca76afce4e7775d87960e3d4ed3b69235a3f94d6f1497c9831b50c", tx.getSignature().r);
assertEquals("664124a6b323350dd57a650434dc6bf8ddf37cd1a2686fee377e512aa12f1214", tx.getSignature().s);
assertEquals("8e7dfa371b0acde61f894f1969f1f17696b86492a8572c60154e85d7801a4a08", Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().r)));
assertEquals("229807de94c4cfa63d978ff22f764cd9e6abd1bae1bcdba4aa4ae299ad0a8a9f", Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().s)));
assertEquals(RLP_ENCODED_TX, Hex.toHexString(tx.getEncodedSigned()));
assertEquals(HASH_RAW_TX, ByteUtil.toHexString(tx.getHash()));
assertEquals(RLP_ENCODED_RAW_TX, ByteUtil.toHexString(tx.getEncoded()));
assertEquals(RLP_ENCODED_SIGNED_TX, ByteUtil.toHexString(tx.getEncodedSigned()));
}
@Test
public void testTransactionFromNew() throws Exception {
byte[] privKeyBytes = Hex.decode("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4");
String RLP_TX_UNSIGNED = "eb8085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc1000080";
String RLP_TX_UNSIGNED = "e88085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc1000080";
String RLP_TX_SIGNED = "f86b8085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc10000801ba0eab47c1a49bf2fe5d40e01d313900e19ca485867d462fe06e139e3a536c6d4f4a014a569d327dcda4b29f74f93c0e9729d2f49ad726e703f9cd90dbb0fbf6649f1";
String HASH_TX_UNSIGNED = "328ea6d24659dec48adea1aced9a136e5ebdf40258db30d1b1d97ed2b74be34e";
byte[] nonce = BigIntegers.asUnsignedByteArray(BigInteger.ZERO);
byte[] gasPrice = Hex.decode("e8d4a51000"); // 1000000000000
@ -139,20 +143,13 @@ public class TransactionTest {
// Testing unsigned
String encodedUnsigned = Hex.toHexString(tx.getEncoded());
System.out.println(RLP_TX_UNSIGNED);
System.out.println(encodedUnsigned);
assertEquals(RLP_TX_UNSIGNED, encodedUnsigned);
assertEquals(HASH_RAW_TX, Hex.toHexString(tx.getHash()));
assertEquals(HASH_TX_UNSIGNED, Hex.toHexString(tx.getHash()));
// Testing signed
tx.sign(privKeyBytes);
String encodedSigned = Hex.toHexString(tx.getEncodedSigned());
System.out.println(RLP_TX_SIGNED);
System.out.println(encodedSigned);
assertEquals(RLP_TX_SIGNED, encodedSigned);
assertEquals(HASH_RAW_TX, Hex.toHexString(tx.getHash()));
assertEquals(HASH_TX_UNSIGNED, Hex.toHexString(tx.getHash()));
}
}

View File

@ -71,7 +71,7 @@ public class CryptoTest {
String txRaw = "F89D80809400000000000000000000000000000000000000008609184E72A000822710B3606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C5884336069571CA07F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4A06D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC";
byte[] txHashB = HashUtil.sha3(Hex.decode(txRaw));
String txHash = Utils.toHexString(txHashB);
String txHash = Hex.toHexString(txHashB);
assertEquals("4b7d9670a92bf120d5b43400543b69304a14d767cf836a7f6abff4edde092895", txHash);
}
@ -81,7 +81,7 @@ public class CryptoTest {
String blockRaw = "F885F8818080A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347940000000000000000000000000000000000000000A0BCDDD284BF396739C224DBA0411566C891C32115FEB998A3E2B4E61F3F35582AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934783800000808080C0C0";
byte[] blockHashB = HashUtil.sha3(Hex.decode(blockRaw));
String blockHash = Utils.toHexString(blockHashB);
String blockHash = Hex.toHexString(blockHashB);
System.out.println(blockHash);
}

View File

@ -20,9 +20,9 @@ import org.ethereum.net.message.NotInChainMessage;
import org.ethereum.net.message.PeersMessage;
import org.ethereum.net.message.ReasonCode;
import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@ -56,7 +56,7 @@ public class MessagesTest {
assertEquals(30303, helloMessage.getPeerPort());
assertEquals(
"D8833B83560E0B12170E9169DC43784223A59842DE2359E6D03DB34C30A966C2DE3B4B2552FB0D7595A185D558F2E669B595674F5217C996EE148884828BE0FD",
Utils.toHexString(helloMessage.getPeerId()).toUpperCase() );
Hex.toHexString(helloMessage.getPeerId()).toUpperCase() );
}
@Test /* HelloMessage 2 */
@ -77,7 +77,7 @@ public class MessagesTest {
assertEquals(30303, helloMessage.getPeerPort());
assertEquals(
"E02B18FBA6B887FB9258469C3AF8E445CC9AE2B5386CAC5F60C4170F822086224E3876555C745A7EC8AC181C7F9701776D94A779604EA12651DE5F4A748D29E1",
Utils.toHexString(helloMessage.getPeerId()).toUpperCase() );
Hex.toHexString(helloMessage.getPeerId()).toUpperCase() );
}
/* DISCONNECT_MESSAGE */
@ -127,7 +127,7 @@ public class MessagesTest {
assertEquals("/85.65.126.45", peerData.getInetAddress().toString());
assertEquals(30303, peerData.getPort());
assertEquals("82A8A5831D3B4FB76CF130CDC8A2B162A85D005D82A1DCC9B73239035EADE6347EDE2FFC86571ABE348EA38699CE886AA3D425FE58182C433434AB4CFD7B5B88",
Utils.toHexString( peerData.getPeerId() ).toUpperCase());
Hex.toHexString( peerData.getPeerId() ).toUpperCase());
}
@Test /* PeersMessage 2 */
@ -147,14 +147,14 @@ public class MessagesTest {
assertEquals("/191.234.57.55", peerData.getInetAddress().toString());
assertEquals(30303, peerData.getPort());
assertEquals("21780C55B47DB4B11467B5F55B0B555E0887CE36FBD975E224B1C70EAC7AB8E8C2DB37F0A48B90FFDD5A379ADA99B6A0F6429C4A53C25558191A682636AEF4F2",
Utils.toHexString( peerData.getPeerId() ).toUpperCase());
Hex.toHexString( peerData.getPeerId() ).toUpperCase());
peerData = peersMessage.getPeers().get(75);
assertEquals("/86.124.82.254", peerData.getInetAddress().toString());
assertEquals(30303, peerData.getPort());
assertEquals("F6155F1A60143B7D9D5D1A440D7D52FE6809F69E0C6F1E0024457E0D71DD88ADE3B13AAA940C89AC0610952B48BD832C42E343A13E61FFDB06010CFFC345E053",
Utils.toHexString( peerData.getPeerId() ).toUpperCase());
Hex.toHexString( peerData.getPeerId() ).toUpperCase());
}
@Test /* Peers msg parsing performance*/
@ -195,37 +195,37 @@ public class MessagesTest {
transactionsMessage.getTransactions().get(0);
assertEquals("558A3797E0DD3FBFAF761F1ADD6749C7D5DB313FDAC5CBA59F40E28AF7BBACD1",
Utils.toHexString( tx.getHash() ).toUpperCase());
Hex.toHexString( tx.getHash() ).toUpperCase());
assertEquals("04",
Utils.toHexString( tx.getNonce() ).toUpperCase());
Hex.toHexString( tx.getNonce() ).toUpperCase());
assertEquals("1BC16D674EC80000",
Utils.toHexString( tx.getValue() ).toUpperCase());
Hex.toHexString( tx.getValue() ).toUpperCase());
assertEquals("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826",
Utils.toHexString( tx.getReceiveAddress() ).toUpperCase());
Hex.toHexString( tx.getReceiveAddress() ).toUpperCase());
assertEquals("09184E72A000",
Utils.toHexString( tx.getGasPrice() ).toUpperCase());
Hex.toHexString( tx.getGasPrice() ).toUpperCase());
assertEquals("64",
Utils.toHexString( tx.getGasLimit() ).toUpperCase());
Hex.toHexString( tx.getGasLimit() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getData() ).toUpperCase());
Hex.toHexString( tx.getData() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getInit() ).toUpperCase());
Hex.toHexString( tx.getInit() ).toUpperCase());
assertEquals("1B",
Utils.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
Hex.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
assertEquals("5E3868194605F1647593B842725818CCFA6A38651A728715133A8E97CDCFAC54",
Utils.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
Hex.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
assertEquals("0FF91628D04B215EBCCFD5F4FC34CC1B45DF32F6B4609FBB0DE42E8522264467",
Utils.toHexString( tx.getSignature().s.toByteArray() ).toUpperCase());
Hex.toHexString( tx.getSignature().s.toByteArray() ).toUpperCase());
}
@Test /* Transactions message 2 */
@ -245,72 +245,72 @@ public class MessagesTest {
transactionsMessage.getTransactions().get(0);
assertEquals("4B7D9670A92BF120D5B43400543B69304A14D767CF836A7F6ABFF4EDDE092895",
Utils.toHexString( tx.getHash() ).toUpperCase());
Hex.toHexString( tx.getHash() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getNonce() ).toUpperCase());
Hex.toHexString( tx.getNonce() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getValue() ).toUpperCase());
Hex.toHexString( tx.getValue() ).toUpperCase());
assertEquals("0000000000000000000000000000000000000000",
Utils.toHexString( tx.getReceiveAddress() ).toUpperCase());
Hex.toHexString( tx.getReceiveAddress() ).toUpperCase());
assertEquals("09184E72A000",
Utils.toHexString( tx.getGasPrice() ).toUpperCase());
Hex.toHexString( tx.getGasPrice() ).toUpperCase());
assertEquals("2710",
Utils.toHexString( tx.getGasLimit() ).toUpperCase());
Hex.toHexString( tx.getGasLimit() ).toUpperCase());
assertEquals("606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C58",
Utils.toHexString( tx.getData() ).toUpperCase());
Hex.toHexString( tx.getData() ).toUpperCase());
assertEquals("33606957",
Utils.toHexString( tx.getInit() ).toUpperCase());
Hex.toHexString( tx.getInit() ).toUpperCase());
assertEquals("1C",
Utils.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
Hex.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
assertEquals("7F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4",
Utils.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
Hex.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
assertEquals("6D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC",
Utils.toHexString( tx.getSignature().s.toByteArray() ).toUpperCase());
Hex.toHexString( tx.getSignature().s.toByteArray() ).toUpperCase());
tx = transactionsMessage.getTransactions().get(2);
assertEquals("B0251A1BB20B44459DB5B5444AB53EDD9E12C46D0BA07FA401A797BEB967D53C",
Utils.toHexString( tx.getHash() ).toUpperCase());
Hex.toHexString( tx.getHash() ).toUpperCase());
assertEquals("02",
Utils.toHexString( tx.getNonce() ).toUpperCase());
Hex.toHexString( tx.getNonce() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getValue() ).toUpperCase());
Hex.toHexString( tx.getValue() ).toUpperCase());
assertEquals("CCDEAC59D35627B7DE09332E819D5159E7BB7250",
Utils.toHexString( tx.getReceiveAddress() ).toUpperCase());
Hex.toHexString( tx.getReceiveAddress() ).toUpperCase());
assertEquals("09184E72A000",
Utils.toHexString( tx.getGasPrice() ).toUpperCase());
Hex.toHexString( tx.getGasPrice() ).toUpperCase());
assertEquals("2710",
Utils.toHexString( tx.getGasLimit() ).toUpperCase());
Hex.toHexString( tx.getGasLimit() ).toUpperCase());
assertEquals("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000002D0ACEEE7E5AB874E22CCF8D1A649F59106D74E8",
Utils.toHexString( tx.getData() ).toUpperCase());
Hex.toHexString( tx.getData() ).toUpperCase());
assertEquals("NULL",
Utils.toHexString( tx.getInit() ).toUpperCase());
Hex.toHexString( tx.getInit() ).toUpperCase());
assertEquals("1B",
Utils.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
Hex.toHexString( new byte[] {tx.getSignature().v} ).toUpperCase());
assertEquals("D05887574456C6DE8F7A0D172342C2CBDD4CF7AFE15D9DBB8B75B748BA6791C9",
Utils.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
Hex.toHexString( tx.getSignature().r.toByteArray() ).toUpperCase());
assertEquals("1E87172A861F6C37B5A9E3A5D0D7393152A7FBE41530E5BB8AC8F35433E5931B",
Utils.toHexString(tx.getSignature().s.toByteArray()).toUpperCase());
Hex.toHexString(tx.getSignature().s.toByteArray()).toUpperCase());
}
/* BLOCKS */
@ -354,30 +354,30 @@ public class MessagesTest {
Block block = list.get(0);
assertEquals("69a7356a245f9dc5b865475ada5ee4e89b18f93c06503a9db3b3630e88e9fb4e",
Utils.toHexString(block.getHash()));
Hex.toHexString(block.getHash()));
assertEquals("00000000000000000000000000000000000000000000000000000000000000000",
Utils.toHexString(block.getParentHash()));
Hex.toHexString(block.getParentHash()));
assertEquals("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
Utils.toHexString(block.getUnclesHash()));
Hex.toHexString(block.getUnclesHash()));
assertEquals("0000000000000000000000000000000000000000",
Utils.toHexString(block.getCoinbase()));
Hex.toHexString(block.getCoinbase()));
assertEquals("2f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d",
Utils.toHexString(block.getStateRoot()));
Hex.toHexString(block.getStateRoot()));
assertEquals("00000000000000000000000000000000000000000000000000000000000000000",
Utils.toHexString(block.getTxTrieRoot()));
Hex.toHexString(block.getTxTrieRoot()));
assertEquals("400000", Utils.toHexString(block.getDifficulty()));
assertEquals("400000", Hex.toHexString(block.getDifficulty()));
assertEquals(0, block.getTimestamp());
assertNull(block.getExtraData());
assertEquals("04994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829",
Utils.toHexString(block.getNonce()));
Hex.toHexString(block.getNonce()));
}
@Test /* BlocksMessage really big message parsing */
@ -397,30 +397,30 @@ public class MessagesTest {
Block block = list.get(31);
assertEquals("518916DFB79C390BD7BFF75712174512C2F96BEC42A3F573355507AD1588CE0C",
Utils.toHexString(block.getHash()).toUpperCase());
Hex.toHexString(block.getHash()).toUpperCase());
assertEquals("AB6B9A5613970FAA771B12D449B2E9BB925AB7A369F0A4B86B286E9D540099CF",
Utils.toHexString(block.getParentHash()).toUpperCase());
Hex.toHexString(block.getParentHash()).toUpperCase());
assertEquals("1DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347",
Utils.toHexString(block.getUnclesHash()).toUpperCase());
Hex.toHexString(block.getUnclesHash()).toUpperCase());
assertEquals("3854AAF203BA5F8D49B1EC221329C7AEBCF050D3",
Utils.toHexString(block.getCoinbase()).toUpperCase());
Hex.toHexString(block.getCoinbase()).toUpperCase());
assertEquals("990DC3B5ACBEE04124361D958FE51ACB582593613FC290683940A0769549D3ED",
Utils.toHexString(block.getStateRoot()).toUpperCase());
Hex.toHexString(block.getStateRoot()).toUpperCase());
assertEquals("9BFE4817D274EA3EB8672E9FE848C3885B53BBBD1D7C26E6039F90FB96B942B0",
Utils.toHexString(block.getTxTrieRoot()).toUpperCase());
Hex.toHexString(block.getTxTrieRoot()).toUpperCase());
assertEquals("3FF000", Utils.toHexString(block.getDifficulty()).toUpperCase());
assertEquals("3FF000", Hex.toHexString(block.getDifficulty()).toUpperCase());
assertEquals(1396643511, block.getTimestamp());
assertNull(block.getExtraData());
assertEquals("00000000000000000000000000000000000000000000000077377ADFF6C227DB",
Utils.toHexString(block.getNonce()).toUpperCase());
Hex.toHexString(block.getNonce()).toUpperCase());
System.out.println(blocksMessage);
}
@ -442,13 +442,13 @@ public class MessagesTest {
assertEquals(32, getChainMessage.getBlockHashList().size());
assertEquals("E5E441F0877116011CCDECE2501A50B40C40418377037E16D0282B2B5E347138",
Utils.toHexString( getChainMessage.getBlockHashList().get(5) ).toUpperCase());
Hex.toHexString( getChainMessage.getBlockHashList().get(5) ).toUpperCase());
assertEquals("6AA7A7D18B07BE0222DA47A8EE6923BAD522DBF5D0650D64CA0F2AD4C606D0CC",
Utils.toHexString( getChainMessage.getBlockHashList().get(19) ).toUpperCase());
Hex.toHexString( getChainMessage.getBlockHashList().get(19) ).toUpperCase());
assertEquals("03AF21F3939C29C231200B1F790F16421A8923254CBF2A90455B9B8F28BE4562",
Utils.toHexString( getChainMessage.getBlockHashList().get(25) ).toUpperCase());
Hex.toHexString( getChainMessage.getBlockHashList().get(25) ).toUpperCase());
}
/* NOT_IN_CHAIN */
@ -465,7 +465,7 @@ public class MessagesTest {
System.out.println(notInChainMessage);
assertEquals("E5E441F0877116011CCDECE2501A50B40C40418377037E16D0282B2B5E347138",
Utils.toHexString(notInChainMessage.getHash()).toUpperCase());
Hex.toHexString(notInChainMessage.getHash()).toUpperCase());
}
@Test /* BlocksMessage POC-5 real msg*/
@ -480,9 +480,7 @@ public class MessagesTest {
List<Block> list = blocksMessage.getBlockDataList();
System.out.println(blocksMessage);
Block block = list.get(0);
}
@Test /* BlocksMessage POC-5 real big msg*/
@ -497,12 +495,9 @@ public class MessagesTest {
List<Block> list = blocksMessage.getBlockDataList();
System.out.println(blocksMessage);
Block block = list.get(0);
}
@Test /* GetChain encode */
public void test14(){
@ -517,12 +512,11 @@ public class MessagesTest {
assertEquals(expected, Hex.toHexString(getChainMessage.getPayload()).toUpperCase());
byte[] size = Utils.calcPacketSize( getChainMessage.getPayload());
byte[] size = ByteUtil.calcPacketSize( getChainMessage.getPayload());
assertEquals("00000067", Utils.toHexString(size));
assertEquals("00000067", Hex.toHexString(size));
}
@Test /* Transactions msg encode */
public void test15() throws Exception {
@ -544,7 +538,6 @@ public class MessagesTest {
txList.add(tx);
TransactionsMessage transactionsMessage = new TransactionsMessage(txList);
//todo: add assertion to the test whenever you know what should be the result
System.out.println(Hex.toHexString( transactionsMessage.getPayload() ));

View File

@ -2,7 +2,6 @@ package org.ethereum.util;
import org.spongycastle.util.encoders.Hex;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.Utils;
import org.junit.Test;
import com.cedarsoftware.util.DeepEquals;
@ -34,7 +33,7 @@ public class RLPTest {
"E0 DE 49 98 33 4F 3B CF 73 FA 11 7E F2 13 F8 74 " +
"17 08 9F EA F8 4C 21 B0";
byte[] payload = Utils.hexStringToByteArr(peersPacket);
byte[] payload = ByteUtil.hexStringToByteArr(peersPacket);
byte[] ip = RLP.decodeIP4Bytes(payload, 5);
@ -51,7 +50,7 @@ public class RLPTest {
"E0 DE 49 98 33 4F 3B CF 73 FA 11 7E F2 13 F8 74 " +
"17 08 9F EA F8 4C 21 B0";
byte[] payload = Utils.hexStringToByteArr(peersPacket);
byte[] payload = ByteUtil.hexStringToByteArr(peersPacket);
int oneInt = RLP.decodeInt(payload, 11);
assertEquals(oneInt, 30303);
@ -72,7 +71,7 @@ public class RLPTest {
"98 33 4F 3B CF 73 FA 11 7E F2 13 F8 74 17 08 9F " +
"EA F8 4C 21 B0 ";
byte[] payload = Utils.hexStringToByteArr(peersPacket);
byte[] payload = ByteUtil.hexStringToByteArr(peersPacket);
int nextIndex = 5;
byte[] ip = RLP.decodeIP4Bytes(payload, nextIndex);
@ -218,11 +217,11 @@ public class RLPTest {
"40 7F 65 9C 78 F4 19 02 5D 78 57 27 ED 01 7B 6A " +
"DD 21 95 2D 7E 12 00 73 73 E3 21 DB C3 18 24 BA ";
byte[] byteArray = Utils.hexStringToByteArr(byteArr);
byte[] byteArray = ByteUtil.hexStringToByteArr(byteArr);
String expected = "B8 40 " + byteArr;
assertArrayEquals(Utils.hexStringToByteArr(expected),
assertArrayEquals(ByteUtil.hexStringToByteArr(expected),
RLP.encodeElement(byteArray));
}
@ -341,7 +340,7 @@ public class RLPTest {
String test = "";
String expected = "80";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(test, bytesToAscii(decodeResult));
@ -352,7 +351,7 @@ public class RLPTest {
String test = "dog";
String expected = "83646f67";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(test, bytesToAscii(decodeResult));
@ -363,7 +362,7 @@ public class RLPTest {
String test = "d";
String expected = "64";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(test, bytesToAscii(decodeResult));
@ -374,7 +373,7 @@ public class RLPTest {
String test = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; // length = 56
String expected = "b8384c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c6974";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(test, bytesToAscii(decodeResult));
@ -385,7 +384,7 @@ public class RLPTest {
Integer test = new Integer(0);
String expected = "80";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
int result = byteArrayToInt(decodeResult);
@ -397,7 +396,7 @@ public class RLPTest {
Integer test = new Integer(15);
String expected = "0f";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
int result = byteArrayToInt(decodeResult);
@ -409,7 +408,7 @@ public class RLPTest {
Integer test = new Integer(1000);
String expected = "8203e8";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
int result = byteArrayToInt(decodeResult);
@ -418,7 +417,7 @@ public class RLPTest {
test = new Integer(1024);
expected = "820400";
encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
result = byteArrayToInt(decodeResult);
@ -430,7 +429,7 @@ public class RLPTest {
BigInteger test = new BigInteger("100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 16);
String expected = "a0100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
byte[] decodeResult = (byte[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(test, new BigInteger(decodeResult));
@ -441,7 +440,7 @@ public class RLPTest {
Object[] test = new Object[0];
String expected = "c0";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertTrue(decodeResult.length == 0);
@ -452,7 +451,7 @@ public class RLPTest {
String[] test = new String[] { "cat", "dog" };
String expected = "c88363617483646f67";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals("cat", bytesToAscii((byte[]) decodeResult[0]));
@ -461,7 +460,7 @@ public class RLPTest {
test = new String[] { "dog", "god", "cat" };
expected = "cc83646f6783676f6483636174";
encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals("dog", bytesToAscii((byte[]) decodeResult[0]));
@ -476,7 +475,7 @@ public class RLPTest {
String[] test = new String[] { element1, element2 };
String expected = "f83e83636174b8384c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c6974";
byte[] encoderesult = (byte[]) RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(element1, bytesToAscii((byte[]) decodeResult[0]));
@ -493,7 +492,7 @@ public class RLPTest {
Object[] test = new Object[] { 1, new Object[] { "cat" }, "dog", new Object[] { 2 } };
String expected = "cc01c48363617483646f67c102";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals(1, byteArrayToInt( (byte[]) decodeResult[0] ));
@ -504,7 +503,7 @@ public class RLPTest {
test = new Object[] { new Object[] { "cat", "dog" }, new Object[] { 1, 2 }, new Object[] { } };
expected = "cdc88363617483646f67c20102c0";
encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertEquals("cat", bytesToAscii( ((byte[]) ((Object[]) decodeResult[0])[0] )));
@ -520,7 +519,7 @@ public class RLPTest {
Object[] test = new Object[] { new Object[] { new Object[] {}, new Object[] {} }, new Object[] {} };
String expected = "c4c2c0c0c0";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertTrue( decodeResult.length == 2 );
@ -537,7 +536,7 @@ public class RLPTest {
Object[] test = new Object[] { new Object[] { }, new Object[] { new Object[] {} }, new Object[] { new Object[] {}, new Object[] { new Object[] { } } } };
String expected = "c7c0c1c0c3c0c1c0";
byte[] encoderesult = RLP.encode(test);
assertEquals(expected, asHex(encoderesult));
assertEquals(expected, Hex.toHexString(encoderesult));
Object[] decodeResult = (Object[]) RLP.decode(encoderesult, 0).getDecoded();
assertTrue( decodeResult.length == 3 );
@ -553,22 +552,22 @@ public class RLPTest {
@Test
public void testRlpEncode() {
assertEquals(result01, asHex(RLP.encode(test01)));
assertEquals(result02, asHex(RLP.encode(test02)));
assertEquals(result03, asHex(RLP.encode(test03)));
assertEquals(result04, asHex(RLP.encode(test04)));
assertEquals(result05, asHex(RLP.encode(test05)));
assertEquals(result06, asHex(RLP.encode(test06)));
assertEquals(result07, asHex(RLP.encode(test07)));
assertEquals(result08, asHex(RLP.encode(test08)));
assertEquals(result09, asHex(RLP.encode(test09)));
assertEquals(result10, asHex(RLP.encode(test10)));
assertEquals(result11, asHex(RLP.encode(test11)));
assertEquals(result12, asHex(RLP.encode(test12)));
assertEquals(result13, asHex(RLP.encode(test13)));
assertEquals(result14, asHex(RLP.encode(test14)));
assertEquals(result15, asHex(RLP.encode(test15)));
assertEquals(result16, asHex(RLP.encode(test16)));
assertEquals(result01, Hex.toHexString(RLP.encode(test01)));
assertEquals(result02, Hex.toHexString(RLP.encode(test02)));
assertEquals(result03, Hex.toHexString(RLP.encode(test03)));
assertEquals(result04, Hex.toHexString(RLP.encode(test04)));
assertEquals(result05, Hex.toHexString(RLP.encode(test05)));
assertEquals(result06, Hex.toHexString(RLP.encode(test06)));
assertEquals(result07, Hex.toHexString(RLP.encode(test07)));
assertEquals(result08, Hex.toHexString(RLP.encode(test08)));
assertEquals(result09, Hex.toHexString(RLP.encode(test09)));
assertEquals(result10, Hex.toHexString(RLP.encode(test10)));
assertEquals(result11, Hex.toHexString(RLP.encode(test11)));
assertEquals(result12, Hex.toHexString(RLP.encode(test12)));
assertEquals(result13, Hex.toHexString(RLP.encode(test13)));
assertEquals(result14, Hex.toHexString(RLP.encode(test14)));
assertEquals(result15, Hex.toHexString(RLP.encode(test15)));
assertEquals(result16, Hex.toHexString(RLP.encode(test16)));
}
@Test
@ -578,61 +577,61 @@ public class RLPTest {
byte[] decodedData;
Object[] decodedList;
decodedByte = (byte[]) RLP.decode(fromHex(result01), pos).getDecoded();
decodedByte = (byte[]) RLP.decode(Hex.decode(result01), pos).getDecoded();
assertEquals(test01, byteArrayToInt(decodedByte));
decodedData = (byte[]) RLP.decode(fromHex(result02), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result02), pos).getDecoded();
assertEquals(test02, bytesToAscii(decodedData));
decodedData = (byte[]) RLP.decode(fromHex(result03), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result03), pos).getDecoded();
assertEquals(test03, bytesToAscii(decodedData));
decodedData = (byte[]) RLP.decode(fromHex(result04), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result04), pos).getDecoded();
assertEquals(test04, bytesToAscii(decodedData));
decodedData = (byte[]) RLP.decode(fromHex(result05), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result05), pos).getDecoded();
assertEquals(test05, bytesToAscii(decodedData));
decodedList = (Object[]) RLP.decode(fromHex(result06), pos).getDecoded();
decodedList = (Object[]) RLP.decode(Hex.decode(result06), pos).getDecoded();
assertEquals(test06[0], bytesToAscii((byte[]) decodedList[0]));
assertEquals(test06[1], bytesToAscii((byte[]) decodedList[1]));
decodedList = (Object[]) RLP.decode(fromHex(result07), pos).getDecoded();
decodedList = (Object[]) RLP.decode(Hex.decode(result07), pos).getDecoded();
assertEquals(test07[0], bytesToAscii((byte[]) decodedList[0]));
assertEquals(test07[1], bytesToAscii((byte[]) decodedList[1]));
assertEquals(test07[2], bytesToAscii((byte[]) decodedList[2]));
// 1
decodedData = (byte[]) RLP.decode(fromHex(result08), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result08), pos).getDecoded();
assertEquals(test08, byteArrayToInt(decodedData));
// 10
decodedData = (byte[]) RLP.decode(fromHex(result09), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result09), pos).getDecoded();
assertEquals(test09, byteArrayToInt(decodedData));
// 100
decodedData = (byte[]) RLP.decode(fromHex(result10), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result10), pos).getDecoded();
assertEquals(test10, byteArrayToInt(decodedData));
// 1000
decodedData = (byte[]) RLP.decode(fromHex(result11), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result11), pos).getDecoded();
assertEquals(test11, byteArrayToInt(decodedData));
decodedData = (byte[]) RLP.decode(fromHex(result12), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result12), pos).getDecoded();
assertTrue(test12.compareTo(new BigInteger(decodedData)) == 0);
decodedData = (byte[]) RLP.decode(fromHex(result13), pos).getDecoded();
decodedData = (byte[]) RLP.decode(Hex.decode(result13), pos).getDecoded();
assertTrue(test13.compareTo(new BigInteger(decodedData)) == 0);
// Need to test with different expected value, because decoding doesn't recognize types
Object testObject1 = RLP.decode(fromHex(result14), pos).getDecoded();
Object testObject1 = RLP.decode(Hex.decode(result14), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(expected14, testObject1));
Object testObject2 = RLP.decode(fromHex(result15), pos).getDecoded();
Object testObject2 = RLP.decode(Hex.decode(result15), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(test15, testObject2));
// Need to test with different expected value, because decoding doesn't recognize types
Object testObject3 = RLP.decode(fromHex(result16), pos).getDecoded();
Object testObject3 = RLP.decode(Hex.decode(result16), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(expected16, testObject3));
}
@ -647,13 +646,13 @@ public class RLPTest {
length = 1; offset = 128;
encodedLength = RLP.encodeLength(length, offset);
expected = "81";
assertEquals(expected, asHex(encodedLength));
assertEquals(expected, Hex.toHexString(encodedLength));
// 56 > length < 2^64
length = 56; offset = 192;
encodedLength = RLP.encodeLength(length, offset);
expected = "f838";
assertEquals(expected, asHex(encodedLength));
assertEquals(expected, Hex.toHexString(encodedLength));
// length > 2^64
// TODO: Fix this test - when casting double to int, information gets lost since 'int' is max (2^31)-1
@ -667,21 +666,9 @@ public class RLPTest {
}
}
// Code from: http://stackoverflow.com/a/9855338/459349
protected final static char[] hexArray = "0123456789abcdef".toCharArray();
private static String asHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
// Code from: http://stackoverflow.com/a/4785776/459349
private String bytesToAscii(byte[] b) {
String hex = asHex(b);
String hex = Hex.toHexString(b);
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
@ -690,17 +677,6 @@ public class RLPTest {
return output.toString();
}
// Code from: http://stackoverflow.com/a/140861/459349
public static byte[] fromHex(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
@Test
public void performanceDecode() throws IOException {
boolean performanceEnabled = false;