Cleanup code

This commit is contained in:
nicksavers 2014-05-05 02:53:56 +02:00
parent f4ae933ab3
commit 96773417ff
16 changed files with 93 additions and 177 deletions

View File

@ -27,7 +27,6 @@ import java.util.Arrays;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.ethereum.util.ByteUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.asn1.sec.SECNamedCurves; import org.spongycastle.asn1.sec.SECNamedCurves;
@ -297,6 +296,12 @@ public class ECKey implements Serializable {
public static ECDSASignature fromComponents(byte[] r, byte[] s) { public static ECDSASignature fromComponents(byte[] r, byte[] s) {
return new ECDSASignature(new BigInteger(r), new BigInteger(s)); return new ECDSASignature(new BigInteger(r), new BigInteger(s));
} }
public static ECDSASignature fromComponents(byte[] r, byte[] s, byte v) {
ECDSASignature signature = new ECDSASignature(new BigInteger(r), new BigInteger(s));
signature.v = v;
return signature;
}
/** /**
* Will automatically adjust the S component to be less than or equal to half the curve order, if necessary. * Will automatically adjust the S component to be less than or equal to half the curve order, if necessary.

View File

@ -16,11 +16,9 @@ public class IpGeoDB { // change
static{ static{
try { try {
URL geiIpDBFile = ClassLoader.getSystemResource("GeoLiteCity.dat"); URL geiIpDBFile = ClassLoader.getSystemResource("GeoLiteCity.dat");
File file = new File(geiIpDBFile.toURI()); File file = new File(geiIpDBFile.toURI());
cl = new LookupService(file); cl = new LookupService(file);
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -28,17 +26,14 @@ public class IpGeoDB { // change
private static LookupService cl; private static LookupService cl;
public static Location getLocationForIp(InetAddress ip){ public static Location getLocationForIp(InetAddress ip){
try { try {
return cl.getLocation(ip); return cl.getLocation(ip);
} catch (Throwable e) { } catch (Throwable e) {
// todo: think about this exception, maybe you can do something more reasonable // todo: think about this exception, maybe you can do something more reasonable
System.out.println(e.getMessage()); System.out.println(e.getMessage());
// e.printStackTrace(); // e.printStackTrace();
} }
return null; return null;
} }
} }

View File

@ -3,9 +3,9 @@ package org.ethereum.manager;
import com.maxmind.geoip.Location; import com.maxmind.geoip.Location;
import org.ethereum.geodb.IpGeoDB; import org.ethereum.geodb.IpGeoDB;
import org.ethereum.net.vo.BlockData; import org.ethereum.net.vo.Block;
import org.ethereum.net.vo.PeerData; import org.ethereum.net.vo.PeerData;
import org.ethereum.net.vo.TransactionData; import org.ethereum.net.vo.Transaction;
import java.util.*; import java.util.*;
@ -30,6 +30,6 @@ public class MainData {
} }
} }
public void addBlocks(List<BlockData> blocks) {} public void addBlocks(List<Block> blocks) {}
public void addTransactions(List<TransactionData> transactions) {} public void addTransactions(List<Transaction> transactions) {}
} }

View File

@ -15,75 +15,51 @@ public class MessageDeserializer {
int pos = startPos; int pos = startPos;
while(pos < endPos){ while(pos < endPos){
// It's a list with a payload more than 55 bytes // It's a list with a payload more than 55 bytes
// data[0] - 0xF7 = how many next bytes allocated // data[0] - 0xF7 = how many next bytes allocated
// for the length of the list // for the length of the list
if ((msgData[pos] & 0xFF) >= 0xF7){ if ((msgData[pos] & 0xFF) >= 0xF7){
byte lengthOfLength = (byte) (msgData[pos] - 0xF7);
byte lenghtOfLenght = (byte) (msgData[pos] - 0xF7); int length = calcLength(lengthOfLength, msgData, pos);
byte pow = (byte) (lenghtOfLenght - 1);
int length = 0;
for (int i = 1; i <= lenghtOfLenght; ++i){
length += msgData[pos + i] << (8 * pow);
pow--;
}
// now we can parse an item for data[1]..data[length] // now we can parse an item for data[1]..data[length]
System.out.println("-- level: [" + level + "] Found big list length: " + length); System.out.println("-- level: [" + level + "] Found big list length: " + length);
deserialize(msgData, level + 1, pos + lenghtOfLenght + 1, pos + lenghtOfLenght + length); deserialize(msgData, level + 1, pos + lengthOfLength + 1, pos + lengthOfLength + length);
pos += lenghtOfLenght + length + 1 ; pos += lengthOfLength + length + 1 ;
continue; continue;
} }
// It's a list with a payload less than 55 bytes // It's a list with a payload less than 55 bytes
if ((msgData[pos] & 0xFF) >= 0xC0 && (msgData[pos] & 0xFF) < 0xF7){ if ((msgData[pos] & 0xFF) >= 0xC0 && (msgData[pos] & 0xFF) < 0xF7){
byte length = (byte) (msgData[pos] - 0xC0); byte length = (byte) (msgData[pos] - 0xC0);
System.out.println("-- level: [" + level + "] Found small list length: " + length); System.out.println("-- level: [" + level + "] Found small list length: " + length);
deserialize(msgData, level + 1, pos + 1, pos + length + 1); deserialize(msgData, level + 1, pos + 1, pos + length + 1);
pos += 1 + length; pos += 1 + length;
continue; continue;
} }
// It's an item with a payload more than 55 bytes // It's an item with a payload more than 55 bytes
// data[0] - 0xB7 = how much next bytes allocated for // data[0] - 0xB7 = how much next bytes allocated for
// the length of the string // the length of the string
if ((msgData[pos] & 0xFF) >= 0xB7 && (msgData[pos] & 0xFF) < 0xC0) { if ((msgData[pos] & 0xFF) >= 0xB7 && (msgData[pos] & 0xFF) < 0xC0) {
byte lengthOfLength = (byte) (msgData[pos] - 0xB7);
byte lenghtOfLenght = (byte) (msgData[pos] - 0xB7); int length = calcLength(lengthOfLength, msgData, pos);
byte pow = (byte) (lenghtOfLenght - 1);
int length = 0;
for (int i = 1; i <= lenghtOfLenght; ++i){
length += msgData[pos + i] << (8 * pow);
pow--;
}
// now we can parse an item for data[1]..data[length] // now we can parse an item for data[1]..data[length]
System.out.println("-- level: [" + level + "] Found big item length: " + length); System.out.println("-- level: [" + level + "] Found big item length: " + length);
pos += lenghtOfLenght + length + 1 ; pos += lengthOfLength + length + 1 ;
continue; continue;
} }
// It's an item less than 55 bytes long, // It's an item less than 55 bytes long,
// data[0] - 0x80 == lenght of the item // data[0] - 0x80 == lenght of the item
if ((msgData[pos] & 0xFF) > 0x80 && (msgData[pos] & 0xFF) < 0xB7) { if ((msgData[pos] & 0xFF) > 0x80 && (msgData[pos] & 0xFF) < 0xB7) {
byte length = (byte) (msgData[pos] - 0x80); byte length = (byte) (msgData[pos] - 0x80);
System.out.println("-- level: [" + level + "] Found small item length: " + length); System.out.println("-- level: [" + level + "] Found small item length: " + length);
pos += 1 + length; pos += 1 + length;
continue; continue;
} }
// null item // null item
if ((msgData[pos] & 0xFF) == 0x80){ if ((msgData[pos] & 0xFF) == 0x80){
System.out.println("-- level: [" + level + "] Found null item: "); System.out.println("-- level: [" + level + "] Found null item: ");
pos += 1; pos += 1;
continue; continue;
} }
// single byte item // single byte item
if ((msgData[pos] & 0xFF) < 0x80) { if ((msgData[pos] & 0xFF) < 0x80) {
System.out.println("-- level: [" + level + "] Found single item: "); System.out.println("-- level: [" + level + "] Found single item: ");
@ -92,4 +68,14 @@ public class MessageDeserializer {
} }
} }
} }
private static int calcLength(int lengthOfLength, byte[] msgData, int pos) {
byte pow = (byte) (lengthOfLength - 1);
int length = 0;
for (int i = 1; i <= lengthOfLength; ++i){
length += msgData[pos + i] << (8 * pow);
pow--;
}
return length;
}
} }

View File

@ -35,7 +35,7 @@ import org.ethereum.net.message.PeersMessage;
import org.ethereum.net.message.StaticMessages; import org.ethereum.net.message.StaticMessages;
import org.ethereum.net.message.TransactionsMessage; import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.net.rlp.RLPList; import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.vo.BlockData; import org.ethereum.net.vo.Block;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -266,7 +266,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
RLP.parseObjects(payload, rlpList); RLP.parseObjects(payload, rlpList);
BlocksMessage blocksMessage = new BlocksMessage(rlpList); BlocksMessage blocksMessage = new BlocksMessage(rlpList);
List<BlockData> blockList = blocksMessage.getBlockDataList(); List<Block> blockList = blocksMessage.getBlockDataList();
MainData.instance.addBlocks(blockList); MainData.instance.addBlocks(blockList);
System.out.println(blocksMessage); System.out.println(blocksMessage);

View File

@ -8,8 +8,8 @@ import static org.ethereum.net.Command.BLOCKS;
import org.ethereum.net.Command; import org.ethereum.net.Command;
import org.ethereum.net.rlp.RLPItem; import org.ethereum.net.rlp.RLPItem;
import org.ethereum.net.rlp.RLPList; import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.vo.BlockData; import org.ethereum.net.vo.Block;
import org.ethereum.net.vo.TransactionData; import org.ethereum.net.vo.Transaction;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
@ -18,7 +18,7 @@ import org.ethereum.net.vo.TransactionData;
*/ */
public class BlocksMessage extends Message { public class BlocksMessage extends Message {
private List<BlockData> blockDataList = new ArrayList<BlockData>(); private List<Block> blockDataList = new ArrayList<Block>();
public BlocksMessage(RLPList rawData) { public BlocksMessage(RLPList rawData) {
super(rawData); super(rawData);
@ -34,7 +34,7 @@ public class BlocksMessage extends Message {
for (int i = 1; i < paramsList.size(); ++i){ for (int i = 1; i < paramsList.size(); ++i){
RLPList rlpData = ((RLPList)paramsList.getElement(i)); RLPList rlpData = ((RLPList)paramsList.getElement(i));
BlockData blockData = new BlockData(rlpData); Block blockData = new Block(rlpData);
this.blockDataList.add(blockData); this.blockDataList.add(blockData);
} }
parsed = true; parsed = true;
@ -45,7 +45,7 @@ public class BlocksMessage extends Message {
return null; return null;
} }
public List<BlockData> getBlockDataList() { public List<Block> getBlockDataList() {
if (!parsed) parseRLP(); if (!parsed) parseRLP();
return blockDataList; return blockDataList;
} }
@ -53,11 +53,11 @@ public class BlocksMessage extends Message {
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (BlockData blockData : this.getBlockDataList()){ for (Block blockData : this.getBlockDataList()){
sb.append(" ").append( blockData.toString() ).append("\n"); sb.append(" ").append( blockData.toString() ).append("\n");
List<TransactionData> transactions = blockData.getTransactionsList(); List<Transaction> transactions = blockData.getTransactionsList();
for (TransactionData transactionData : transactions){ for (Transaction transactionData : transactions){
sb.append("[").append(transactionData).append("]\n"); sb.append("[").append(transactionData).append("]\n");
} }
} }

View File

@ -46,9 +46,7 @@ public class StaticMessages {
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03,
(byte) 0xC2, (byte) 0x01, (byte) 0x08}; (byte) 0xC2, (byte) 0x01, (byte) 0x08};
public static final byte[] GET_CHAIN = { public static final byte[] GET_CHAIN = {
(byte) 0x22, (byte) 0x40, (byte) 0x08, (byte) 0x91, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x27, (byte) 0x22, (byte) 0x40, (byte) 0x08, (byte) 0x91, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x27,
(byte) 0xF8, (byte) 0x25, (byte) 0x14, (byte) 0xA0, (byte) 0xAB, (byte) 0x6B, (byte) 0x9A, (byte) 0x56, (byte) 0x13, (byte) 0xF8, (byte) 0x25, (byte) 0x14, (byte) 0xA0, (byte) 0xAB, (byte) 0x6B, (byte) 0x9A, (byte) 0x56, (byte) 0x13,
(byte) 0x97, (byte) 0x0F, (byte) 0xAA, (byte) 0x77, (byte) 0x1B, (byte) 0x12, (byte) 0xD4, (byte) 0x49, (byte) 0x97, (byte) 0x0F, (byte) 0xAA, (byte) 0x77, (byte) 0x1B, (byte) 0x12, (byte) 0xD4, (byte) 0x49,

View File

@ -4,7 +4,7 @@ import static org.ethereum.net.Command.TRANSACTIONS;
import org.ethereum.net.Command; import org.ethereum.net.Command;
import org.ethereum.net.rlp.RLPItem; import org.ethereum.net.rlp.RLPItem;
import org.ethereum.net.rlp.RLPList; import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.vo.TransactionData; import org.ethereum.net.vo.Transaction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -16,7 +16,7 @@ import java.util.List;
*/ */
public class TransactionsMessage extends Message { public class TransactionsMessage extends Message {
private List<TransactionData> transactions = new ArrayList<TransactionData>(); private List<Transaction> transactions = new ArrayList<Transaction>();
public TransactionsMessage() { public TransactionsMessage() {
} }
@ -33,17 +33,17 @@ public class TransactionsMessage extends Message {
throw new Error("TransactionMessage: parsing for mal data"); throw new Error("TransactionMessage: parsing for mal data");
} }
transactions = new ArrayList<TransactionData>(); transactions = new ArrayList<Transaction>();
int size = paramsList.getList().size(); int size = paramsList.getList().size();
for (int i = 1; i < size; ++i){ for (int i = 1; i < size; ++i){
RLPList rlpTxData = (RLPList) paramsList.getElement(i); RLPList rlpTxData = (RLPList) paramsList.getElement(i);
TransactionData tx = new TransactionData(rlpTxData); Transaction tx = new Transaction(rlpTxData);
transactions.add(tx); transactions.add(tx);
} }
parsed = true; parsed = true;
} }
public List<TransactionData> getTransactions() { public List<Transaction> getTransactions() {
if (!parsed) parseRLP(); if (!parsed) parseRLP();
return transactions; return transactions;
} }
@ -56,7 +56,7 @@ public class TransactionsMessage extends Message {
public String toString(){ public String toString(){
if(!parsed) parseRLP(); if(!parsed) parseRLP();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (TransactionData transactionData : transactions){ for (Transaction transactionData : transactions){
sb.append(" ").append(transactionData).append("\n"); sb.append(" ").append(transactionData).append("\n");
} }
return "Transactions Message [\n" + sb.toString() + " ]"; return "Transactions Message [\n" + sb.toString() + " ]";

View File

@ -7,7 +7,6 @@ import org.ethereum.net.rlp.RLPList;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -16,10 +15,10 @@ import java.util.List;
* User: Roman Mandeleil * User: Roman Mandeleil
* Created on: 13/04/14 19:34 * Created on: 13/04/14 19:34
*/ */
public class BlockData { public class Block {
RLPList rawData; private RLPList rawData;
boolean parsed = false; private boolean parsed = false;
private byte[] hash; private byte[] hash;
private byte[] parentHash; private byte[] parentHash;
@ -33,15 +32,15 @@ public class BlockData {
private byte[] extraData; private byte[] extraData;
private byte[] nonce; private byte[] nonce;
List<TransactionData> transactionsList = new ArrayList<TransactionData>(); List<Transaction> transactionsList = new ArrayList<Transaction>();
List<BlockData> uncleList = new ArrayList<BlockData>(); List<Block> uncleList = new ArrayList<Block>();
public BlockData(RLPList rawData) { public Block(RLPList rawData) {
this.rawData = rawData; this.rawData = rawData;
this.parsed = false; this.parsed = false;
} }
public BlockData(byte[] parentHash, byte[] unclesHash, byte[] coinbase, byte[] stateHash, byte[] txListHash, byte[] difficulty, long timestamp, byte[] extraData, byte[] nonce, List<TransactionData> transactionsList, List uncleList) { public Block(byte[] parentHash, byte[] unclesHash, byte[] coinbase, byte[] stateHash, byte[] txListHash, byte[] difficulty, long timestamp, byte[] extraData, byte[] nonce, List<Transaction> transactionsList, List<Block> uncleList) {
this.parentHash = parentHash; this.parentHash = parentHash;
this.unclesHash = unclesHash; this.unclesHash = unclesHash;
this.coinbase = coinbase; this.coinbase = coinbase;
@ -54,7 +53,6 @@ public class BlockData {
this.transactionsList = transactionsList; this.transactionsList = transactionsList;
this.uncleList = uncleList; this.uncleList = uncleList;
this.parsed = true; this.parsed = true;
} }
// [parent_hash, uncles_hash, coinbase, state_root, tx_list_hash, difficulty, timestamp, extradata, nonce] // [parent_hash, uncles_hash, coinbase, state_root, tx_list_hash, difficulty, timestamp, extradata, nonce]
@ -76,24 +74,18 @@ public class BlockData {
this.extraData = ((RLPItem) params.get(7)).getData(); this.extraData = ((RLPItem) params.get(7)).getData();
this.nonce = ((RLPItem) params.get(8)).getData(); this.nonce = ((RLPItem) params.get(8)).getData();
// parse transactions // parse transactions
List<RLPElement> transactions = ((RLPList) rawData.getElement(1)).getList(); List<RLPElement> transactions = ((RLPList) rawData.getElement(1)).getList();
for (RLPElement rlpTx : transactions){ for (RLPElement rlpTx : transactions){
Transaction tx = new Transaction((RLPList)rlpTx);
TransactionData tx = new TransactionData((RLPList)rlpTx);
this.transactionsList.add(tx); this.transactionsList.add(tx);
} }
// parse uncles // parse uncles
List<RLPElement> uncleBlocks = ((RLPList) rawData.getElement(2)).getList(); List<RLPElement> uncleBlocks = ((RLPList) rawData.getElement(2)).getList();
for (RLPElement rawUncle : uncleBlocks){ for (RLPElement rawUncle : uncleBlocks){
Block blockData = new Block((RLPList)rawUncle);
BlockData blockData = new BlockData((RLPList)rawUncle);
this.uncleList.add(blockData); this.uncleList.add(blockData);
} }
this.parsed = true; this.parsed = true;
} }
@ -148,12 +140,12 @@ public class BlockData {
return nonce; return nonce;
} }
public List<TransactionData> getTransactionsList() { public List<Transaction> getTransactionsList() {
if (!parsed) parseRLP(); if (!parsed) parseRLP();
return transactionsList; return transactionsList;
} }
public List<BlockData> getUncleList() { public List<Block> getUncleList() {
if (!parsed) parseRLP(); if (!parsed) parseRLP();
return uncleList; return uncleList;
} }

View File

@ -1,7 +1,6 @@
package org.ethereum.net.vo; package org.ethereum.net.vo;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import org.ethereum.net.rlp.RLPList;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -13,39 +12,27 @@ import java.net.UnknownHostException;
*/ */
public class PeerData { public class PeerData {
RLPList rawData; private byte[] ip;
boolean parsed = false; private short port;
private byte[] peerId;
byte[] ip; private transient boolean isOnline = false;
short port; private transient long lastCheckTime = 0;
byte[] peerId;
transient boolean isOnline = false;
transient long lastCheckTime = 0;
public PeerData(RLPList rlpList){
rawData = rlpList;
parsed = false;
}
public PeerData(byte[] ip, short port, byte[] peerId) { public PeerData(byte[] ip, short port, byte[] peerId) {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
this.peerId = peerId; this.peerId = peerId;
parsed = true;
} }
public InetAddress getInetAddress(){ public InetAddress getInetAddress(){
InetAddress addr = null; InetAddress addr = null;
try { try {
addr = InetAddress.getByAddress(ip); addr = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
throw new Error("malformed ip"); throw new Error("malformed ip");
} }
return addr; return addr;
} }
@ -77,7 +64,6 @@ public class PeerData {
this.lastCheckTime = lastCheckTime; this.lastCheckTime = lastCheckTime;
} }
@Override @Override
public String toString() { public String toString() {
return "Peer: [ ip=" + getInetAddress()+ ", port=" + getPort() + ", peerId=" + Hex.toHexString( getPeerId() ) + "]"; return "Peer: [ ip=" + getInetAddress()+ ", port=" + getPort() + ", peerId=" + Hex.toHexString( getPeerId() ) + "]";

View File

@ -10,44 +10,41 @@ import org.ethereum.util.Utils;
* User: Roman Mandeleil * User: Roman Mandeleil
* Created on: 21/04/14 09:19 * Created on: 21/04/14 09:19
*/ */
public class TransactionData { public class Transaction {
RLPList rawData;
boolean parsed = false;
private RLPList rawData;
private boolean parsed = false;
// creation contract tx or simple send tx // creation contract tx or simple send tx
// [ nonce, value, receiveAddress, gasPrice, gasDeposit, data, signatureV, signatureR, signatureS ] // [ nonce, value, receiveAddress, gasPrice, gasDeposit, data, signatureV, signatureR, signatureS ]
// or // or
// [ nonce, endowment, 0, gasPrice, gasDeposit (for init), body, init, signatureV, signatureR, signatureS ] // [ nonce, endowment, 0, gasPrice, gasDeposit (for init), body, init, signatureV, signatureR, signatureS ]
byte[] hash; private byte[] hash;
byte[] nonce; private byte[] nonce;
byte[] value; private byte[] value;
// In creation transaction the receive address is - 0 // In creation transaction the receive address is - 0
byte[] receiveAddress; private byte[] receiveAddress;
byte[] gasPrice; private byte[] gasPrice;
byte[] gas; private byte[] gas;
// Contract creation [data] will hold the contract // Contract creation [data] will hold the contract
// for other transaction [data] can hold data // for other transaction [data] can hold data
byte[] data; private byte[] data;
byte[] init; private byte[] init;
// Signature // Signature
byte signatureV; private byte signatureV;
byte[] signatureR; private byte[] signatureR;
byte[] signatureS; private byte[] signatureS;
public Transaction(RLPList rawData) {
public TransactionData(RLPList rawData) {
this.rawData = rawData; this.rawData = rawData;
parsed = false; parsed = false;
} }
public TransactionData(byte[] nonce, byte[] value, byte[] recieveAddress, byte[] gasPrice, byte[] gas, byte[] data, byte signatureV, byte[] signatureR, byte[] signatureS) { public Transaction(byte[] nonce, byte[] value, byte[] recieveAddress, byte[] gasPrice, byte[] gas, byte[] data, byte signatureV, byte[] signatureR, byte[] signatureS) {
this.nonce = nonce; this.nonce = nonce;
this.value = value; this.value = value;
this.receiveAddress = recieveAddress; this.receiveAddress = recieveAddress;
@ -60,31 +57,22 @@ public class TransactionData {
parsed = true; parsed = true;
} }
public void rlpParse(){ public void rlpParse(){
if (rawData.size() == 9){ // Simple transaction this.hash = HashUtil.sha3(rawData.getRLPData());
this.nonce = ((RLPItem) rawData.getElement(0)).getData();
this.value = ((RLPItem) rawData.getElement(1)).getData();
this.receiveAddress = ((RLPItem) rawData.getElement(2)).getData();
this.gasPrice = ((RLPItem) rawData.getElement(3)).getData();
this.gas = ((RLPItem) rawData.getElement(4)).getData();
this.data = ((RLPItem) rawData.getElement(5)).getData();
this.hash = HashUtil.sha3(rawData.getRLPData()); if (rawData.size() == 9){ // Simple transaction
this.nonce = ((RLPItem) rawData.getElement(0)).getData();
this.value = ((RLPItem) rawData.getElement(1)).getData();
this.receiveAddress = ((RLPItem) rawData.getElement(2)).getData();
this.gasPrice = ((RLPItem) rawData.getElement(3)).getData();
this.gas = ((RLPItem) rawData.getElement(4)).getData();
this.data = ((RLPItem) rawData.getElement(5)).getData();
this.signatureV = ((RLPItem) rawData.getElement(6)).getData()[0]; this.signatureV = ((RLPItem) rawData.getElement(6)).getData()[0];
this.signatureR = ((RLPItem) rawData.getElement(7)).getData(); this.signatureR = ((RLPItem) rawData.getElement(7)).getData();
this.signatureS = ((RLPItem) rawData.getElement(8)).getData(); this.signatureS = ((RLPItem) rawData.getElement(8)).getData();
} else if (rawData.size() == 10){ // Contract creation transaction } else if (rawData.size() == 10){ // Contract creation transaction
this.hash = HashUtil.sha3(rawData.getRLPData());
this.nonce = ((RLPItem) rawData.getElement(0)).getData();
this.value = ((RLPItem) rawData.getElement(1)).getData();
this.receiveAddress = ((RLPItem) rawData.getElement(2)).getData();
this.gasPrice = ((RLPItem) rawData.getElement(3)).getData();
this.gas = ((RLPItem) rawData.getElement(4)).getData();
this.data = ((RLPItem) rawData.getElement(5)).getData();
this.init = ((RLPItem) rawData.getElement(6)).getData(); this.init = ((RLPItem) rawData.getElement(6)).getData();
this.signatureV = ((RLPItem) rawData.getElement(7)).getData()[0]; this.signatureV = ((RLPItem) rawData.getElement(7)).getData()[0];
this.signatureR = ((RLPItem) rawData.getElement(8)).getData(); this.signatureR = ((RLPItem) rawData.getElement(8)).getData();
@ -95,7 +83,6 @@ public class TransactionData {
this.parsed = true; this.parsed = true;
} }
public RLPList getRawData() { public RLPList getRawData() {
return rawData; return rawData;
} }
@ -110,61 +97,51 @@ public class TransactionData {
} }
public byte[] getNonce() { public byte[] getNonce() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return nonce; return nonce;
} }
public byte[] getValue() { public byte[] getValue() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return value; return value;
} }
public byte[] getReceiveAddress() { public byte[] getReceiveAddress() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return receiveAddress; return receiveAddress;
} }
public byte[] getGasPrice() { public byte[] getGasPrice() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return gasPrice; return gasPrice;
} }
public byte[] getGas() { public byte[] getGas() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return gas; return gas;
} }
public byte[] getData() { public byte[] getData() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return data; return data;
} }
public byte[] getInit() { public byte[] getInit() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return init; return init;
} }
public byte getSignatureV() { public byte getSignatureV() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return signatureV; return signatureV;
} }
public byte[] getSignatureR() { public byte[] getSignatureR() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return signatureR; return signatureR;
} }
public byte[] getSignatureS() { public byte[] getSignatureS() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return signatureS; return signatureS;
} }

View File

@ -49,9 +49,7 @@ public class Utils {
} }
public static void printHexStringForByte(byte data){ public static void printHexStringForByte(byte data){
System.out.print("["); System.out.print("[");
String hexNum = Integer.toHexString ((int) data & 0xFF); String hexNum = Integer.toHexString ((int) data & 0xFF);
if (((int) data & 0xFF) < 16) { if (((int) data & 0xFF) < 16) {
hexNum = "0" + hexNum; hexNum = "0" + hexNum;
@ -62,10 +60,8 @@ public class Utils {
} }
public static void printHexStringForByteArray(byte[] data){ public static void printHexStringForByteArray(byte[] data){
System.out.print("["); System.out.print("[");
for (int i = 0; i < data.length; ++i){ for (int i = 0; i < data.length; ++i){
String hexNum = Integer.toHexString ((int) data[i] & 0xFF); String hexNum = Integer.toHexString ((int) data[i] & 0xFF);
if (((int) data[i] & 0xFF) < 16) { if (((int) data[i] & 0xFF) < 16) {
hexNum = "0" + hexNum; hexNum = "0" + hexNum;
@ -78,7 +74,6 @@ public class Utils {
} }
public static ImageIcon getImageIcon(String resource){ public static ImageIcon getImageIcon(String resource){
URL imageURL = ClassLoader.getSystemResource(resource); URL imageURL = ClassLoader.getSystemResource(resource);
ImageIcon image = new ImageIcon(imageURL); ImageIcon image = new ImageIcon(imageURL);
return image; return image;

View File

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -4,7 +4,7 @@ import org.spongycastle.util.encoders.Hex;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.ethereum.net.RLP; import org.ethereum.net.RLP;
import org.ethereum.net.rlp.RLPList; import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.vo.BlockData; import org.ethereum.net.vo.Block;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -131,7 +131,7 @@ public class BlockTest {
RLPList rlpList = new RLPList(); RLPList rlpList = new RLPList();
RLP.parseObjects(payload, rlpList); RLP.parseObjects(payload, rlpList);
BlockData blockData = new BlockData(rlpList); Block blockData = new Block(rlpList);
RLPList.recursivePrint(rlpList); RLPList.recursivePrint(rlpList);
} }
} }

View File

@ -13,9 +13,9 @@ import org.ethereum.net.message.NotInChainMessage;
import org.ethereum.net.message.PeersMessage; import org.ethereum.net.message.PeersMessage;
import org.ethereum.net.message.TransactionsMessage; import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.net.rlp.RLPList; import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.vo.BlockData; import org.ethereum.net.vo.Block;
import org.ethereum.net.vo.PeerData; import org.ethereum.net.vo.PeerData;
import org.ethereum.net.vo.TransactionData; import org.ethereum.net.vo.Transaction;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.junit.Test; import org.junit.Test;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -76,7 +76,6 @@ public class MessagesTest {
Utils.toHexString(helloMessage.getPeerId()).toUpperCase() ); Utils.toHexString(helloMessage.getPeerId()).toUpperCase() );
} }
/* DISCONNECT_MESSAGE */ /* DISCONNECT_MESSAGE */
@Test /* DisconnectMessage 1 */ @Test /* DisconnectMessage 1 */
@ -109,7 +108,6 @@ public class MessagesTest {
DisconnectMessage.REASON_TCP_ERROR); DisconnectMessage.REASON_TCP_ERROR);
} }
/* PEERS */ /* PEERS */
@Test /* PeersMessage 1*/ @Test /* PeersMessage 1*/
@ -131,7 +129,6 @@ public class MessagesTest {
assertEquals(30303, peerData.getPort()); assertEquals(30303, peerData.getPort());
assertEquals("82A8A5831D3B4FB76CF130CDC8A2B162A85D005D82A1DCC9B73239035EADE6347EDE2FFC86571ABE348EA38699CE886AA3D425FE58182C433434AB4CFD7B5B88", assertEquals("82A8A5831D3B4FB76CF130CDC8A2B162A85D005D82A1DCC9B73239035EADE6347EDE2FFC86571ABE348EA38699CE886AA3D425FE58182C433434AB4CFD7B5B88",
Utils.toHexString( peerData.getPeerId() ).toUpperCase()); Utils.toHexString( peerData.getPeerId() ).toUpperCase());
} }
@Test /* PeersMessage 2 */ @Test /* PeersMessage 2 */
@ -160,7 +157,6 @@ public class MessagesTest {
assertEquals(30303, peerData.getPort()); assertEquals(30303, peerData.getPort());
assertEquals("F6155F1A60143B7D9D5D1A440D7D52FE6809F69E0C6F1E0024457E0D71DD88ADE3B13AAA940C89AC0610952B48BD832C42E343A13E61FFDB06010CFFC345E053", assertEquals("F6155F1A60143B7D9D5D1A440D7D52FE6809F69E0C6F1E0024457E0D71DD88ADE3B13AAA940C89AC0610952B48BD832C42E343A13E61FFDB06010CFFC345E053",
Utils.toHexString( peerData.getPeerId() ).toUpperCase()); Utils.toHexString( peerData.getPeerId() ).toUpperCase());
} }
@Test /* Peers msg parsing performance*/ @Test /* Peers msg parsing performance*/
@ -199,7 +195,7 @@ public class MessagesTest {
assertEquals(1, transactionsMessage.getTransactions().size()); assertEquals(1, transactionsMessage.getTransactions().size());
TransactionData tx = Transaction tx =
transactionsMessage.getTransactions().get(0); transactionsMessage.getTransactions().get(0);
assertEquals("558A3797E0DD3FBFAF761F1ADD6749C7D5DB313FDAC5CBA59F40E28AF7BBACD1", assertEquals("558A3797E0DD3FBFAF761F1ADD6749C7D5DB313FDAC5CBA59F40E28AF7BBACD1",
@ -236,7 +232,6 @@ public class MessagesTest {
Utils.toHexString( tx.getSignatureS() ).toUpperCase()); Utils.toHexString( tx.getSignatureS() ).toUpperCase());
} }
@Test /* Transactions message 2 */ @Test /* Transactions message 2 */
public void test_9(){ public void test_9(){
@ -251,7 +246,7 @@ public class MessagesTest {
assertEquals(3, transactionsMessage.getTransactions().size()); assertEquals(3, transactionsMessage.getTransactions().size());
TransactionData tx = Transaction tx =
transactionsMessage.getTransactions().get(0); transactionsMessage.getTransactions().get(0);
assertEquals("4B7D9670A92BF120D5B43400543B69304A14D767CF836A7F6ABFF4EDDE092895", assertEquals("4B7D9670A92BF120D5B43400543B69304A14D767CF836A7F6ABFF4EDDE092895",
@ -287,7 +282,6 @@ public class MessagesTest {
assertEquals("6D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC", assertEquals("6D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC",
Utils.toHexString( tx.getSignatureS() ).toUpperCase()); Utils.toHexString( tx.getSignatureS() ).toUpperCase());
tx = transactionsMessage.getTransactions().get(2); tx = transactionsMessage.getTransactions().get(2);
assertEquals("B0251A1BB20B44459DB5B5444AB53EDD9E12C46D0BA07FA401A797BEB967D53C", assertEquals("B0251A1BB20B44459DB5B5444AB53EDD9E12C46D0BA07FA401A797BEB967D53C",
@ -322,10 +316,8 @@ public class MessagesTest {
assertEquals("1E87172A861F6C37B5A9E3A5D0D7393152A7FBE41530E5BB8AC8F35433E5931B", assertEquals("1E87172A861F6C37B5A9E3A5D0D7393152A7FBE41530E5BB8AC8F35433E5931B",
Utils.toHexString(tx.getSignatureS()).toUpperCase()); Utils.toHexString(tx.getSignatureS()).toUpperCase());
} }
/* BLOCKS */ /* BLOCKS */
@Test /* BlocksMessage parsing 1*/ @Test /* BlocksMessage parsing 1*/
@ -339,12 +331,12 @@ public class MessagesTest {
RLP.parseObjects(payload, rlpList); RLP.parseObjects(payload, rlpList);
BlocksMessage blocksMessage = new BlocksMessage(rlpList); BlocksMessage blocksMessage = new BlocksMessage(rlpList);
List<BlockData> list = blocksMessage.getBlockDataList(); List<Block> list = blocksMessage.getBlockDataList();
System.out.println(blocksMessage); System.out.println(blocksMessage);
assertEquals(1, list.size()); assertEquals(1, list.size());
BlockData block = list.get(0); Block block = list.get(0);
assertEquals("36A24B56C6104E5A5C0E70B0553F1A4D6109D065D718D7443A6A475EC8C83905", assertEquals("36A24B56C6104E5A5C0E70B0553F1A4D6109D065D718D7443A6A475EC8C83905",
Utils.toHexString(block.getHash()).toUpperCase()); Utils.toHexString(block.getHash()).toUpperCase());
@ -373,7 +365,6 @@ public class MessagesTest {
Utils.toHexString(block.getNonce()).toUpperCase()); Utils.toHexString(block.getNonce()).toUpperCase());
} }
@Test /* BlocksMessage really big message parsing */ @Test /* BlocksMessage really big message parsing */
public void test11(){ public void test11(){
@ -384,13 +375,13 @@ public class MessagesTest {
RLP.parseObjects(payload, rlpList); RLP.parseObjects(payload, rlpList);
BlocksMessage blocksMessage = new BlocksMessage(rlpList); BlocksMessage blocksMessage = new BlocksMessage(rlpList);
List<BlockData> list = blocksMessage.getBlockDataList(); List<Block> list = blocksMessage.getBlockDataList();
System.out.println(blocksMessage); System.out.println(blocksMessage);
assertEquals(32, list.size()); assertEquals(32, list.size());
BlockData block = list.get(31); Block block = list.get(31);
assertEquals("518916DFB79C390BD7BFF75712174512C2F96BEC42A3F573355507AD1588CE0C", assertEquals("518916DFB79C390BD7BFF75712174512C2F96BEC42A3F573355507AD1588CE0C",
Utils.toHexString(block.getHash()).toUpperCase()); Utils.toHexString(block.getHash()).toUpperCase());
@ -419,11 +410,8 @@ public class MessagesTest {
Utils.toHexString(block.getNonce()).toUpperCase()); Utils.toHexString(block.getNonce()).toUpperCase());
System.out.println(blocksMessage); System.out.println(blocksMessage);
} }
/* GET_CHAIN */ /* GET_CHAIN */
@Test /* GET_CHAIN message parsing*/ @Test /* GET_CHAIN message parsing*/
@ -449,12 +437,8 @@ public class MessagesTest {
assertEquals("03AF21F3939C29C231200B1F790F16421A8923254CBF2A90455B9B8F28BE4562", assertEquals("03AF21F3939C29C231200B1F790F16421A8923254CBF2A90455B9B8F28BE4562",
Utils.toHexString( getChainMessage.getBlockHashList().get(25) ).toUpperCase()); Utils.toHexString( getChainMessage.getBlockHashList().get(25) ).toUpperCase());
} }
/* NOT_IN_CHAIN */ /* NOT_IN_CHAIN */
@Test /* NotInChainMessage parsing 1 */ @Test /* NotInChainMessage parsing 1 */
@ -472,7 +456,5 @@ public class MessagesTest {
assertEquals("E5E441F0877116011CCDECE2501A50B40C40418377037E16D0282B2B5E347138", assertEquals("E5E441F0877116011CCDECE2501A50B40C40418377037E16D0282B2B5E347138",
Utils.toHexString(notInChainMessage.getHash()).toUpperCase()); Utils.toHexString(notInChainMessage.getHash()).toUpperCase());
} }
} }