From 8c54e65e43eb2ba59ab7daac19d2512ddd1cf16d Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 21 May 2014 11:15:25 +0200 Subject: [PATCH 1/2] Add support for Travis CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..f5c99a7f --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: java \ No newline at end of file From 6b5b592595e8231b3d16eee5006d9d93fcc05cb2 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 21 May 2014 15:33:51 +0200 Subject: [PATCH 2/2] Fix RLP decode long list, TransactionTest and cleaning Utils/ByteUtil --- .../main/java/org/ethereum/core/Block.java | 38 ++--- .../java/org/ethereum/core/Transaction.java | 33 ++--- .../org/ethereum/net/client/ClientPeer.java | 5 +- .../net/client/EthereumPeerTasterHandler.java | 7 +- .../net/client/EthereumProtocolHandler.java | 7 +- .../ethereum/net/message/GetChainMessage.java | 3 +- .../net/message/NotInChainMessage.java | 4 +- .../main/java/org/ethereum/util/ByteUtil.java | 62 +++++++- .../src/main/java/org/ethereum/util/RLP.java | 9 +- .../main/java/org/ethereum/util/RLPList.java | 4 +- .../main/java/org/ethereum/util/Utils.java | 79 +--------- .../java/org/ethereum/core/BlockTest.java | 17 +-- .../org/ethereum/core/TransactionTest.java | 49 +++---- .../java/org/ethereum/crypto/CryptoTest.java | 4 +- .../java/org/ethereum/net/MessagesTest.java | 129 ++++++++-------- .../test/java/org/ethereum/util/RLPTest.java | 138 ++++++++---------- 16 files changed, 260 insertions(+), 328 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Block.java b/ethereumj-core/src/main/java/org/ethereum/core/Block.java index a2835ab1..f861f5c7 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Block.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Block.java @@ -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()){ diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java b/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java index a02a6a56..df1ef371 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java @@ -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); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/ClientPeer.java b/ethereumj-core/src/main/java/org/ethereum/net/client/ClientPeer.java index d813be4b..75e14d30 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/ClientPeer.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/ClientPeer.java @@ -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: [ " + diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumPeerTasterHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumPeerTasterHandler.java index 606d7969..180d9558 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumPeerTasterHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumPeerTasterHandler.java @@ -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); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java index 30c3827c..6f202abd 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java @@ -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 @@ -173,7 +172,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { answerBytes = Hex.decode(answer); buffer = ctx.alloc().buffer(answerBytes.length); - answerBytes = Utils.hexStringToByteArr(answer); + answerBytes = ByteUtil.hexStringToByteArr(answer); buffer = ctx.alloc().buffer(answerBytes.length); buffer.writeBytes(answerBytes); ctx.writeAndFlush(buffer); @@ -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); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/message/GetChainMessage.java b/ethereumj-core/src/main/java/org/ethereum/net/message/GetChainMessage.java index d2f765da..feba034c 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/message/GetChainMessage.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/message/GetChainMessage.java @@ -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); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/message/NotInChainMessage.java b/ethereumj-core/src/main/java/org/ethereum/net/message/NotInChainMessage.java index b0e34736..0f7f2cc0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/message/NotInChainMessage.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/message/NotInChainMessage.java @@ -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) + "]"; } } diff --git a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java index 66af424b..1e7a3ad0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java @@ -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 @@ -65,7 +125,7 @@ public class ByteUtil { return 0; return new BigInteger(1, b).intValue(); } - + public static byte[] encodeInt(int value) { if (isLessThanUnsigned(value, 253)) { return new byte[]{(byte) value}; diff --git a/ethereumj-core/src/main/java/org/ethereum/util/RLP.java b/ethereumj-core/src/main/java/org/ethereum/util/RLP.java index fe5569c5..985cdc82 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/RLP.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/RLP.java @@ -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 { diff --git a/ethereumj-core/src/main/java/org/ethereum/util/RLPList.java b/ethereumj-core/src/main/java/org/ethereum/util/RLPList.java index 29f3a1fa..5b692834 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/RLPList.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/RLPList.java @@ -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 implements RLPElement { } System.out.print("]"); } else { - String hex = Utils.toHexString(((RLPItem) element).getRLPData()); + String hex = ByteUtil.toHexString(((RLPItem) element).getRLPData()); System.out.print(hex + ", "); } } diff --git a/ethereumj-core/src/main/java/org/ethereum/util/Utils.java b/ethereumj-core/src/main/java/org/ethereum/util/Utils.java index 8bd5bece..9b3bbc3b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/Utils.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/Utils.java @@ -9,45 +9,9 @@ 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....' @@ -61,63 +25,22 @@ public class Utils { byte[] numberBytes = Hex.decode(hexNum.substring(2)); 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 thousand = new BigInteger("1000"); public static String getValueShortString(BigInteger number){ BigInteger result = number; int pow = 0; while (result.compareTo(thousand) == 1){ - result = result.divide(thousand); pow += 3; } - - return result.toString() + " (" + "10^" + pow + ")"; } diff --git a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java index e33edfd6..b870d7d3 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java @@ -6,16 +6,11 @@ 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 @@ -53,14 +48,12 @@ 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())); } - @Test /* create BlockData from part of real RLP BLOCKS message */ - public void test3(){ + @Test /* create BlockData from part of real RLP BLOCKS message */ + public void test3() { String blocksMsg = "F8C8F8C4A07B2536237CBF114A043B0F9B27C76F84AC160EA5B87B53E42C7E76148964D450A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347943854AAF203BA5F8D49B1EC221329C7AEBCF050D3A07A3BE0EE10ECE4B03097BF74AABAC628AA0FAE617377D30AB1B97376EE31F41AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347833FBFE884533F1CE880A0000000000000000000000000000000000000000000000000F3DEEA84969B6E95C0C0"; @@ -69,8 +62,8 @@ public class BlockTest { System.out.println(blockData.toString()); } - @Test /* create BlockData from part of real RLP BLOCKS message POC-5 */ - public void test4(){ + @Test /* create BlockData from part of real RLP BLOCKS message POC-5 */ + public void test4() { String blocksMsg = "F8D1A0085F6A51A63D1FBA43D6E5FE166A47BED64A8B93A99012537D50F3279D4CEA52A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934794D8758B101609A9F2A881A017BA86CBE6B7F0581DA068472689EA736CFC6B18FCAE9BA7454BADF9C65333A0317DFEFAE1D4AFFF6F90A000000000000000000000000000000000000000000000000000000000000000008401EDF1A18222778609184E72A0008080845373B0B180A0000000000000000000000000000000000000000000000000D1C0D8BC6D744943C0C0"; @@ -80,7 +73,7 @@ public class BlockTest { } @Test /* todo: that message kills the traffic somehow need to check it good chance it's not our fault */ - public void test5(){ + public void test5() { String blockMsg = "F91CC213F8D7F8D3A05A9C3A0F42EA90E45E1977D1215E64A92DB3B26F39C19081A957CF25CC4938E4A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A03382B0B1446914CF7F0CFBD2595FCF6760637446C25705675252A6FF35B49AD3A000000000000000000000000000000000000000000000000000000000000000008360E6398201B38609184E72A0008309F91480845376CF4280A000000000000000000000000000000000000000000000000075B7C5E93C227F24C0C0F901AFF8D3A0D63157442969541A039BC16B6D9E48AF25ED124ED04F8B75C09404EBF312EBC2A00F3083A396728B8D2FE28293AC0BB7E4F129B473CB5840F916415D73A6603AD29486B0F83E05CFBD8A695F6D4623ED330AB79F5407A0709F217DF5C9EE697800FC29ABE3E4887C433850F97D7F9FB17B8905ED156E36A000000000000000000000000000000000000000000000000000000000000000008360CE068201B28609184E72A0008309FB9380845376CF4280A0000000000000000000000000000000000000000000000000894E32ED89D9A937C0F8D7F8D5A047FDB11571BDD91DB9AFDAEBDC6538F13B2B0F18BFB56E37C6F7EABD952BDC47A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0A8D82B4BA4ECDE43A19F3BDE008D99F97E15694569CDD2F0308CB7CB4EAC15C6A0C12CB7054BE29658C73D0E02F93D7C6ADCBE348B3617FF7E41692CE23D3417268360B5D98201B18609184E72A0008309FE12820313845376CF2A80A0000000000000000000000000000000000000000000000000237953ADF0651ACEF9019EF8D5A047FDB11571BDD91DB9AFDAEBDC6538F13B2B0F18BFB56E37C6F7EABD952BDC47A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A02FF1AAE0D0A7FBF14E187946EB6B028D8F1716270DBD1F26200297F40748DEAFA077987FAF5286BE98D27BD462562D4E1A81B59B7B9A30A2D7C05AD1CFC48844038360B5D98201B18609184E72A0008309FE12820313845376CF2A80A00000000000000000000000000000000000000000000000001DCD333CA4D0CB0BF8C4F8C2F89C808609184E72A00082271094000000000000000000000000000000000000000080B7604060140260200A0F60FF60000B10630000001659006080600056600056036000576007603060003960076000F20000600060206001561CA0641A7E400D51FF7135A34F3E9D7BF16C8FB784A2ED00D844B025B80DD51D77C8A00828B02C77BA7A3AD6EB739854F0333D2DB18D756095EB997FB7BD2F920DFB82A0ED9DD9541C7962F7633646CC1D44444FC800EB78C2E16EC291FBD61368206794820313C0F8D7F8D3A020282FE2850D6FCC50A18D0A68FECF052281F01FC54EF2D806D112B44FED72A4A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A092E3F9A5D62A31073A21EE0AA19F37C6D513CFDDF742241B047CDA89076BC9C3A0000000000000000000000000000000000000000000000000000000000000000083609DB28201B08609184E72A000830A009380845376CF2980A000000000000000000000000000000000000000000000000076CB5317F46E4E1EC0C0F8D7F8D3A00A41F765D5D6A09F12DE756012791C1173532A52F0727FED75B3790C1E48F62CA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A0703FB6288C3B9463380F2BAA9EDA70FC6260A30B06DE531DAA37BAC6BCB51F96A00000000000000000000000000000000000000000000000000000000000000000836085918201AF8609184E72A000830A031480845376CF2080A000000000000000000000000000000000000000000000000012B0608A249E985CC0C0F8D7F8D3A0F61C52941C66B1DA732DCE69B9327CC903B77B426A09C1AD7AAC7D8BC642DC3EA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA06C8CB0D52DF07942B9B9492783FC7B0F563EEF5DD9FF28317130A6E339DDFF41A0000000000000000000000000000000000000000000000000000000000000000083606D768201AE8609184E72A000830A059680845376CF1E80A0000000000000000000000000000000000000000000000000781E922736EE69C0C0C0F8D7F8D3A0D7A5C737A911EEB3B525569746C6F863A7C11DB69703B537606FF8C75DC31E00A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA02E645EAB8B20125278167B643D68E45CC506BA3998909E10DB936EAE1E0BB45CA00000000000000000000000000000000000000000000000000000000000000000836055618201AD8609184E72A000830A081980845376CF1780A00000000000000000000000000000000000000000000000009F03B3443ED7CDB1C0C0F8D7F8D3A0C89E63C6CF2961B73F2D84D1F75E5EF298ECD7A7C9B27E53F5628CC066474254A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA029147B8FACD35B2D7D852BE539D472CB735B2D92196A6EA7EC866CD7B6DD84D3A0000000000000000000000000000000000000000000000000000000000000000083603D528201AC8609184E72A000830A0A9C80845376CF0080A0000000000000000000000000000000000000000000000000F3CBD47433EC51F2C0C0F8D7F8D3A05B6CEC1B8878B85B6332725CA026DE0C35E284D5E4FFC779FECEE16FF6C86F21A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A070D894DAF3EE7D1EA97A81F69E23E7632B4D09A158E5344AEDB97F2A2F93BCC2A00000000000000000000000000000000000000000000000000000000000000000836025498201AB8609184E72A000830A0D2080845376CEF180A0000000000000000000000000000000000000000000000000166728DB9360E580C0C0F8D7F8D3A091BEF98E5DFC6E3A65F4B482A10AF0613F947E55F736A9303F577ADCDD8B7F3CA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0923A44EED76F1705643B5C6EA44F69EFEF9D45D8DB7527375B8C629EB05269BEA0000000000000000000000000000000000000000000000000000000000000000083600D468201AA8609184E72A000830A0FA480845376CEEC80A00000000000000000000000000000000000000000000000001AB7440167369CAFC0C0F8D7F8D3A063CE861DA91055E0821754442F65DAAE8D3CB297A6250867E0CFCC28B61B68D8A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0EE8D924E5FE7CDB99C1122AD170263C632E105C12A94CF7CDCCF37E8497C81B9A00000000000000000000000000000000000000000000000000000000000000000835FF5498201A98609184E72A000830A122980845376CED980A0000000000000000000000000000000000000000000000000EFC942DD6FF7616DC0C0F8D7F8D3A06A5E905ED31F6392BA9DCFA7890A1F52CA25DD98FBDD75F3754845B6F120BEB8A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA0FFCA836C8F25754698505572C52403DE0B33D357487B07948FE430968762A270A00000000000000000000000000000000000000000000000000000000000000000835FDD528201A88609184E72A000830A14AF80845376CED680A00000000000000000000000000000000000000000000000002122F19CDFFFBAEBC0C0F8D7F8D3A060A93B480AB9BE58A0A6EDC95828F1579D6214D8374DD76629866DEEDD2A89C9A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A06C419EAA73ADB5117F4D3063C0C19E2CE7E7A6BC6A39D4B310DC10068FE1B247A00000000000000000000000000000000000000000000000000000000000000000835FC5618201A78609184E72A000830A173580845376CECE80A00000000000000000000000000000000000000000000000005FEE3C618DB72604C0C0F8D7F8D3A0D5AAC64688E132C60532521DD8CAF3176D20BE18205C5054E8BD2733856ACE3DA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A04E8EAAB0B801C0AFBEF8B8544C052775E1B1294A88BC2C379CD0FAD0876E1E64A00000000000000000000000000000000000000000000000000000000000000000835FAD768201A68609184E72A000830A19BC80845376CECA80A000000000000000000000000000000000000000000000000074A7E922D5F2C424C0C0F8D7F8D3A0DE1F20341C8438C9DBAFB761225F13EB8B0A4948F7A5F70CAAA141422A181C23A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A02A502AF08E85CCC2B6F25FAF8AF84602ECBEC67462E060412176A9BED03AE301A00000000000000000000000000000000000000000000000000000000000000000835F95918201A58609184E72A000830A1C4480845376CEB580A00000000000000000000000000000000000000000000000004EB1A2330EBEB25EC0C0F8D7F8D3A08576EF59DE78E1A0D71129F312089B5B9489D2C7BA860F79BD3C38F0B6181792A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0E3C122E389221F00AB2D7E184673C3C3BF067293A148EBBC2297D9955316FE4BA00000000000000000000000000000000000000000000000000000000000000000835F7DB28201A48609184E72A000830A1ECC80845376CEA280A00000000000000000000000000000000000000000000000001AB4403D3339F87CC0C0F8D7F8D3A07CE7DBFDCAA4671ABE04B9079EE4651B2855B91A1A2B478C2D6DC03AEA6E90EFA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0A4F79A0690598043C90732468FF94C319B8255AC105E994F35721F6EC5E94720A00000000000000000000000000000000000000000000000000000000000000000835F65D98201A38609184E72A000830A215580845376CE9F80A0000000000000000000000000000000000000000000000000531B2012C60B98C4C0C0F8D7F8D3A018E99074D2DBAAB4229C47AD0D43A50FB1D509DA7E2C8355CE8EE939A70231CBA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0E281A1C194C5E8481D6290FCBCED6900160B4B99A12C61C1D26007BB009EEF89A00000000000000000000000000000000000000000000000000000000000000000835F4E068201A28609184E72A000830A23DE80845376CE8680A000000000000000000000000000000000000000000000000034A019980E2816D8C0C0F8D7F8D3A0B8D73FA39ABAA55983BF25ABD8C437EAC1B5EFCAB296AE91F178DE82B0572CEDA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0076BF20A295EC2BBF28EEFA6D03B29263057D4B8CB4B9E5C4526689985192344A00000000000000000000000000000000000000000000000000000000000000000835F36398201A18609184E72A000830A266880845376CE7E80A0000000000000000000000000000000000000000000000000C2A29FAA134E6CE7C0C0F8D7F8D3A0483DDC1F805A88E5167EEDFD81551C0EDCAE05465B96A12B09FF5F3ACC943A4CA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA0C81C8A62625CBC5903BDC4507790F5A08AFA0F533962C38919205865BB7F7012A00000000000000000000000000000000000000000000000000000000000000000835F1E728201A08609184E72A000830A28F380845376CE7280A000000000000000000000000000000000000000000000000044EC79C1ACE56782C0C0F8D7F8D3A0BCBC4648BEA672F8E97FEECFF0DDAA52BEA25C69DD5AC6778064AEBD22558AB1A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0329645D0662AFE926B69310D5AB62B5F46B468C5EA868BD8383EF01E0F4EC017A00000000000000000000000000000000000000000000000000000000000000000835F06B182019F8609184E72A000830A2B7E80845376CE6880A000000000000000000000000000000000000000000000000078E192C00B5C9A55C0C0F8D7F8D3A030367C2249B99C9B64E5A54B04D2AB889E741E6163D705A551BFBAE1CAB48656A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A053BEFC6C748B901FD0BC1ECC266887FC8AF2562857412A2B4BC2D436C13CAA07A00000000000000000000000000000000000000000000000000000000000000000835EEEF682019E8609184E72A000830A2E0A80845376CE6780A0000000000000000000000000000000000000000000000000F72A22F3893684BEC0C0F8D7F8D3A040C61152DF8987E6384FE20D9997665305B9DBDCB10E31BF9C478FEBA9BA1555A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0C628CE6F140F1CA6BB999209B04F7C12AC75527548C23296DF278D95250B2FCAA00000000000000000000000000000000000000000000000000000000000000000835ED74182019D8609184E72A000830A309780845376CE6680A0000000000000000000000000000000000000000000000000FB7592504BA17F5FC0C0F8D7F8D3A0CD12EBD76977846C8873BE9234BB52178C94BC58A2E835368F1BB496DDDA3A65A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A0753FDE5CC9114048064959B14CDC5283C932E5D12A44DA80E1E815A652F13CA6A00000000000000000000000000000000000000000000000000000000000000000835EBF9282019C8609184E72A000830A332480845376CE6480A0000000000000000000000000000000000000000000000000D2FE571C3183D2C5C0C0F8D7F8D3A06C5ABE47363B7BD9FA8D0EB59B47024FA67490E42235A2EAD855C1BAA19363AEA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347942BE5CFECA826E623BE23BB36C6433E1DD8BBF75AA081F74A210499AFB3D5B49FC07B81D80A9D84D3CABDF0B47878838BE11DBD0FE4A00000000000000000000000000000000000000000000000000000000000000000835EA7E982019B8609184E72A000830A35B280845376CE5E80A00000000000000000000000000000000000000000000000002C2EE02B36ECEA58C0C0F8D7F8D3A00C52A67A2E9F0D67F95FFB34BEB48DF1307407C2B7EC3B325FD6228698D879C4A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479486B0F83E05CFBD8A695F6D4623ED330AB79F5407A047527C3AAFAA10FC113E965DD96C5795F5A4FC6BD7F3F19AF2CA5B5E8CBEF50AA00000000000000000000000000000000000000000000000000000000000000000835E904582019A8609184E72A000830A384180845376CE5B80A0000000000000000000000000000000000000000000000000BD4C3D2D8EAD07DCC0C0F8D7F8D3A0B72DF0564F00C843F439C383E178BD5D14C36DD28E0BA25DA4C743C188117470A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0E507344C0E52E46CC4AE7BFA7EB44F5D1694196D320CBC0A1C72F5B665CFD543A00000000000000000000000000000000000000000000000000000000000000000835E78A78201998609184E72A000830A3AD080845376CE5580A0000000000000000000000000000000000000000000000000F8DE5C5A5BAB5B93C0C0F8D7F8D3A0C7BEF299EDC81D1E93810F7F2EE200E29F292E3AE289A8FD083819BBCB0C0B17A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0B3918F7DED608484B5AF55332795D4D9B78E66AA7BD255148895556F0E888920A00000000000000000000000000000000000000000000000000000000000000000835E610F8201988609184E72A000830A3D6080845376CE4680A000000000000000000000000000000000000000000000000019A734F9E48CA7C8C0C0F8D7F8D3A0D5CB93F1E08E8A42E13BC200AA97C19189778E36BD010BCECBF513FC55267C88A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A00EA85CC320772CA355D74748716EC623A381473BB34B98271F33B24D90F674BCA00000000000000000000000000000000000000000000000000000000000000000835E497D8201978609184E72A000830A3FF080845376CE4680A00000000000000000000000000000000000000000000000005A7AF036D1551C24C0C0F8D7F8D3A058BA4DEB3E947B951053B7402FF400B3663B9B0184D86A915C5383C132D71B5CA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A02A5E467771A657D6E9FF64B1AB7BB2DEFB706F207A601AFF7E9AA9E6E93B9C69A00000000000000000000000000000000000000000000000000000000000000000835E31F18201968609184E72A000830A428180845376CE4080A0000000000000000000000000000000000000000000000000B8420BC2D2543481C0C0F8D7F8D3A00F54DE3B125BAD46E29A3CE8D3F634645C662A7F5717C922310FB5B3B280D90FA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A059BBF472F4B3EC6A82D9645250AB99A45E7488EAE98B25177149854B81763F46A00000000000000000000000000000000000000000000000000000000000000000835E1A6B8201958609184E72A000830A451380845376CE3D80A00000000000000000000000000000000000000000000000002185AD9F49F845C0C0C0F8D7F8D3A0C1938EBFBEAAC1A63E92FB44C4CCDB93F35DA9BBF9CB5EC5CCC7DE05F1278DE9A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D493479485000814DFDBCBD96DE0465675CA089D0CC711A1A0B2343359124953AB815B560053FA8EEE1A75A2FB67CE5971ACA2B56C047ADCA8A00000000000000000000000000000000000000000000000000000000000000000835E02EB8201948609184E72A000830A47A580845376CE3480A0000000000000000000000000000000000000000000000000EBFC6D9834899A53C0C0"; diff --git a/ethereumj-core/src/test/java/org/ethereum/core/TransactionTest.java b/ethereumj-core/src/test/java/org/ethereum/core/TransactionTest.java index 18492553..27329de2 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/TransactionTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/TransactionTest.java @@ -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); - + String encodedSigned = Hex.toHexString(tx.getEncodedSigned()); assertEquals(RLP_TX_SIGNED, encodedSigned); - assertEquals(HASH_RAW_TX, Hex.toHexString(tx.getHash())); + assertEquals(HASH_TX_UNSIGNED, Hex.toHexString(tx.getHash())); } } diff --git a/ethereumj-core/src/test/java/org/ethereum/crypto/CryptoTest.java b/ethereumj-core/src/test/java/org/ethereum/crypto/CryptoTest.java index 5b8da269..6c4463b1 100644 --- a/ethereumj-core/src/test/java/org/ethereum/crypto/CryptoTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/crypto/CryptoTest.java @@ -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); } diff --git a/ethereumj-core/src/test/java/org/ethereum/net/MessagesTest.java b/ethereumj-core/src/test/java/org/ethereum/net/MessagesTest.java index 08094131..a7a531ba 100644 --- a/ethereumj-core/src/test/java/org/ethereum/net/MessagesTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/net/MessagesTest.java @@ -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 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 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() )); diff --git a/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java b/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java index ae85f959..a03758ae 100644 --- a/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java @@ -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;