This commit is contained in:
nicksavers 2014-08-08 18:13:26 +01:00
commit 692d5c595a
8 changed files with 135 additions and 15 deletions

View File

@ -247,6 +247,28 @@ public class Block {
toStringBuff.append("]");
return toStringBuff.toString();
}
public String toStylishString(){
if (!parsed) parseRLP();
toStringBuff.setLength(0);
toStringBuff.append("<font color=\"${header_color}\"> BlockData </font> [");
toStringBuff.append(" <font color=\"${attribute_color}\"> hash</font>=" +
ByteUtil.toHexString(this.getHash())).append("<br/>");
toStringBuff.append(header.toStylishString());
for (TransactionReceipt tx : getTxReceiptList()) {
toStringBuff.append("<br/>");
toStringBuff.append( tx.toStylishString() );
toStringBuff.append("<br/>");
}
toStringBuff.append("]");
return toStringBuff.toString();
}
private void parseTxs(byte[] expectedRoot, RLPList txReceipts) {

View File

@ -247,5 +247,24 @@ public class BlockHeader {
toStringBuff.append(" nonce=" + toHexString(nonce)).append("");
return toStringBuff.toString();
}
public String toStylishString() {
toStringBuff.setLength(0);
toStringBuff.append(", <font color=\"${attribute_color}\"> parentHash</font>=" + ByteUtil.toHexString(parentHash)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> unclesHash</font>=" + ByteUtil.toHexString(unclesHash)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> coinbase</font>=" + ByteUtil.toHexString(coinbase)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> stateRoot</font>=" + ByteUtil.toHexString(stateRoot)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> txTrieHash</font>=" + ByteUtil.toHexString(txTrieRoot)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> difficulty</font>=" + ByteUtil.toHexString(difficulty)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> number</font>=" + number).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> minGasPrice</font>=" + minGasPrice).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> gasLimit</font>=" + gasLimit).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> gasUsed</font>=" + gasUsed).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> timestamp</font>=" + timestamp + " (" + Utils.longToDateTime(timestamp) + ")").append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> extraData</font>=" + ByteUtil.toHexString(extraData)).append("<br/>");
toStringBuff.append(", <font color=\"${attribute_color}\"> nonce</font>=" + ByteUtil.toHexString(nonce)).append("<br/>");
return toStringBuff.toString();
}
}

View File

@ -209,8 +209,8 @@ public class Blockchain {
if(!blockStateRootHash.equals(worldStateRootHash)){
logger.error("ERROR: STATE CONFLICT! block: {} worldstate {} mismatch", block.getNumber(), worldStateRootHash);
// Last conflict on block 1501 -> worldstate 27920c6c7acd42c8a7ac8a835d4c0e0a45590deb094d6b72a8493fac5d7a3654
repository.close();
System.exit(-1); // Don't add block
// repository.close();
// System.exit(-1); // Don't add block
}
}

View File

@ -222,6 +222,22 @@ public class Transaction {
" ]";
}
public String toStylishString() {
if (!parsed) rlpParse();
return " <font color=\"${sub_header_color}\"> TransactionData </font>[" + "<font color=\"${attribute_color}\"> hash</font>=" + ByteUtil.toHexString(hash) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> nonce</font>=" + ByteUtil.toHexString(nonce) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> gasPrice</font>=" + ByteUtil.toHexString(gasPrice) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> gas</font>=" + ByteUtil.toHexString(gasLimit) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> receiveAddress</font>=" + ByteUtil.toHexString(receiveAddress) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> value</font>=" + ByteUtil.toHexString(value) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> data</font>=" + ByteUtil.toHexString(data) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> signatureV</font>=" + signature.v + "<br/>" +
"-> , <font color=\"${attribute_color}\"> signatureR</font>=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.r)) + "<br/>" +
"-> , <font color=\"${attribute_color}\"> signatureS</font>=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.s)) + "<br/>" +
" ]";
}
/**
* For signatures you have to keep also
* RLP of the transaction without any signature data

View File

@ -45,4 +45,14 @@ public class TransactionReceipt {
"\n , cumulativeGas=" + Hex.toHexString(cumulativeGas) +
']';
}
public String toStylishString() {
return "<font color=\"${sub_header_color}\"> TransactionReceipt</font>[" +
"<br/> " + transaction.toStylishString() +
"<br/> , <font color=\"${attribute_color}\">postTxState</font>=" + Hex.toHexString(postTxState) +
"<br/> , <font color=\"${attribute_color}\">cumulativeGas</font>=" + Hex.toHexString(cumulativeGas) +
']';
}
}

View File

@ -22,6 +22,10 @@ public class EthereumImpl implements Ethereum {
private Logger logger = LoggerFactory.getLogger("facade");
public EthereumImpl() {
WorldManager.getInstance().loadBlockchain();
}
/**
* Find a peer but not this one
* @param peerData - peer to exclude

View File

@ -1,6 +1,7 @@
package org.ethereum.listener;
import org.ethereum.core.Block;
import org.ethereum.net.message.Message;
/**
* www.ethereumJ.com
@ -13,4 +14,7 @@ public interface EthereumListener {
public void trace(String output);
public void onBlock(Block block);
public void onRecvMessage(Message message);
public void onSendMessage(Message message);
}

View File

@ -42,7 +42,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
private Logger logger = LoggerFactory.getLogger("wire");
private Timer chainAskTimer = new Timer();
private int secToAskForChain = 7;
private int secToAskForChain = 1;
private final Timer timer = new Timer();
@ -102,7 +102,13 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
}
}, 2000, 30000);
sendGetChain();
chainAskTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
sendGetChain();
}
}, 1000, secToAskForChain * 1000);
}
@Override
@ -111,6 +117,8 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info("[Recv msg: [{}] ]", Hex.toHexString(payload));
EthereumListener listener = WorldManager.getInstance().getListener();
byte command = RLP.getCommandCode(payload);
// got HELLO
@ -122,9 +130,10 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info(helloMessage.toString());
if (peerListener != null) peerListener.console(helloMessage.toString());
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
if (listener != null){
listener.trace(String.format("Got handshake: [ %s ]", helloMessage.toString()));
listener.onRecvMessage(helloMessage);
}
}
// got DISCONNECT
@ -137,6 +146,11 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info(disconnectMessage.toString());
if (peerListener != null) peerListener.console(disconnectMessage.toString());
if (listener != null)
listener.onRecvMessage(disconnectMessage);
}
// got PING send pong
@ -144,6 +158,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
if (peerListener != null) peerListener.console("[Recv: PING]");
msgQueue.receivedMessage(PING_MESSAGE);
sendPong();
if (listener != null)
listener.onRecvMessage(PING_MESSAGE);
}
// got PONG mark it
@ -151,6 +168,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
if (peerListener != null) peerListener.console("[Recv: PONG]");
this.lastPongTime = System.currentTimeMillis();
msgQueue.receivedMessage(PONG_MESSAGE);
if (listener != null)
listener.onRecvMessage(PONG_MESSAGE);
}
// got GETPEERS send peers
@ -160,6 +180,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
msgQueue.receivedMessage(GET_PEERS_MESSAGE);
// TODO: send peer list
if (listener != null)
listener.onRecvMessage(GET_PEERS_MESSAGE);
}
// got PEERS
@ -173,6 +196,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info(peersMessage.toString());
if (peerListener != null) peerListener.console(peersMessage.toString());
if (listener != null)
listener.onRecvMessage(peersMessage);
}
// got TRANSACTIONS
@ -189,6 +215,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info(transactionsMessage.toString());
if (peerListener != null) peerListener.console(transactionsMessage.toString());
if (listener != null)
listener.onRecvMessage(transactionsMessage);
}
// got BLOCKS
@ -210,7 +239,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
chainAskTimer.cancel();
chainAskTimer.purge();
chainAskTimer = new Timer();
chainAskTimer.schedule(new TimerTask() {
chainAskTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
sendGetChain();
@ -228,7 +257,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
chainAskTimer.cancel();
chainAskTimer.purge();
chainAskTimer = new Timer();
chainAskTimer.schedule(new TimerTask() {
chainAskTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
sendGetChain();
@ -240,6 +269,8 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
WorldManager.getInstance().getBlockchain().getBlockQueue().addBlocks(blockList);
if (peerListener != null) peerListener.console(blocksMessage.toString());
if (listener != null)
listener.onRecvMessage(blocksMessage);
}
// got GETCHAIN
@ -250,10 +281,13 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
RLPList rlpList = RLP.decode2(payload);
GetChainMessage getChainMessage = new GetChainMessage(rlpList);
// todo: send blocks
logger.info(getChainMessage.toString());
if (peerListener != null) peerListener.console(getChainMessage.toString());
if (listener != null)
listener.onRecvMessage(getChainMessage);
}
// got NOTINCHAIN
@ -266,6 +300,9 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
logger.info(notInChainMessage.toString());
if (peerListener != null) peerListener.console(notInChainMessage.toString());
if (listener != null)
listener.onRecvMessage(notInChainMessage);
}
// got GETTRANSACTIONS
@ -281,6 +318,10 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
// new TransactionsMessage(new ArrayList(pendingTxList));
// sendMsg(txMsg, ctx);
if (listener != null)
listener.onRecvMessage(GET_TRANSACTIONS_MESSAGE);
}
}
@ -302,22 +343,26 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
public void sendMsg(Message msg) {
msgQueue.sendMessage(msg);
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
listener.onSendMessage(msg);
}
private void sendPing() {
msgQueue.sendMessage(PING_MESSAGE);
sendMsg(PING_MESSAGE);
}
private void sendPong() {
msgQueue.sendMessage(PONG_MESSAGE);
sendMsg(PONG_MESSAGE);
}
private void sendGetPeers() {
msgQueue.sendMessage(GET_PEERS_MESSAGE);
sendMsg(GET_PEERS_MESSAGE);
}
private void sendGetTransactions() {
msgQueue.sendMessage(GET_TRANSACTIONS_MESSAGE);
sendMsg(GET_TRANSACTIONS_MESSAGE);
}
private void sendGetChain() {
@ -331,6 +376,6 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
byte[] hash = lastBlock.getHash();
GetChainMessage chainMessage =
new GetChainMessage( SystemProperties.CONFIG.maxBlocksAsk(), hash);
msgQueue.sendMessage(chainMessage);
sendMsg(chainMessage);
}
}