From 7db4d0892fb6eb1640f1a232394039f4762c1f0a Mon Sep 17 00:00:00 2001 From: romanman Date: Wed, 6 Aug 2014 20:34:16 +0300 Subject: [PATCH 1/5] EthereumListener update + onMessageRecv() onMessageSend() --- .../ethereum/listener/EthereumListener.java | 4 ++ .../net/client/EthereumProtocolHandler.java | 45 +++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/listener/EthereumListener.java b/ethereumj-core/src/main/java/org/ethereum/listener/EthereumListener.java index 51f14985..91a957d2 100644 --- a/ethereumj-core/src/main/java/org/ethereum/listener/EthereumListener.java +++ b/ethereumj-core/src/main/java/org/ethereum/listener/EthereumListener.java @@ -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); + } 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 682477c3..29791fc1 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 @@ -111,6 +111,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 +124,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 +140,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 +152,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 +162,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 +174,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 +190,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 +209,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 @@ -240,6 +263,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 +275,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 +294,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 +312,10 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { // new TransactionsMessage(new ArrayList(pendingTxList)); // sendMsg(txMsg, ctx); + + if (listener != null) + listener.onRecvMessage(GET_TRANSACTIONS_MESSAGE); + } } @@ -302,6 +337,10 @@ 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() { From 9f3598886974e8bf342667ccb87fb47c8ed83d74 Mon Sep 17 00:00:00 2001 From: romanman Date: Wed, 6 Aug 2014 20:35:58 +0300 Subject: [PATCH 2/5] EthereumListener update (addition) --- .../ethereum/net/client/EthereumProtocolHandler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 29791fc1..2e38e3e3 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 @@ -344,19 +344,19 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { } 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() { @@ -370,6 +370,6 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { byte[] hash = lastBlock.getHash(); GetChainMessage chainMessage = new GetChainMessage( SystemProperties.CONFIG.maxBlocksAsk(), hash); - msgQueue.sendMessage(chainMessage); + sendMsg(chainMessage); } } \ No newline at end of file From c0588237ca3af470e8ac0dd08f4a7ada0fb23504 Mon Sep 17 00:00:00 2001 From: romanman Date: Thu, 7 Aug 2014 10:01:33 +0300 Subject: [PATCH 3/5] (!!!) The System.exit(-1) on wrong state should not be in DB --- ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java index 918461fa..6a6881d9 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -210,7 +210,7 @@ public class Blockchain { 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 + // System.exit(-1); // Don't add block } } From 8fcf969533df3934fb0cb1ef3ad777e6a0c937bf Mon Sep 17 00:00:00 2001 From: romanman Date: Thu, 7 Aug 2014 12:22:20 +0300 Subject: [PATCH 4/5] Fix for latest merge --- ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java index 6a6881d9..e87d886a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -209,7 +209,7 @@ 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(); +// repository.close(); // System.exit(-1); // Don't add block } } From 7757a23147c4f1db1906f7d1b2ab33712ec83be4 Mon Sep 17 00:00:00 2001 From: romanman Date: Thu, 7 Aug 2014 12:58:45 +0300 Subject: [PATCH 5/5] Android adjustments + stylish string for block data + small fixes --- .../main/java/org/ethereum/core/Block.java | 22 +++++++++++++++++++ .../java/org/ethereum/core/BlockHeader.java | 21 +++++++++++++++++- .../java/org/ethereum/core/Transaction.java | 16 ++++++++++++++ .../org/ethereum/core/TransactionReceipt.java | 10 +++++++++ .../org/ethereum/facade/EthereumImpl.java | 4 ++++ .../net/client/EthereumProtocolHandler.java | 14 ++++++++---- 6 files changed, 82 insertions(+), 5 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 5a07e1d1..fa43aaef 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Block.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Block.java @@ -257,6 +257,28 @@ public class Block { toStringBuff.append("]"); return toStringBuff.toString(); } + + public String toStylishString(){ + + if (!parsed) parseRLP(); + + toStringBuff.setLength(0); + toStringBuff.append(" BlockData ["); + toStringBuff.append(" hash=" + + ByteUtil.toHexString(this.getHash())).append("
"); + toStringBuff.append(header.toStylishString()); + + for (TransactionReceipt tx : getTxReceiptList()) { + + toStringBuff.append("
"); + toStringBuff.append( tx.toStylishString() ); + toStringBuff.append("
"); + } + + toStringBuff.append("]"); + return toStringBuff.toString(); + + } private void parseTxs(RLPList txReceipts) { diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java index 20df987c..aa98a170 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java @@ -243,5 +243,24 @@ public class BlockHeader { toStringBuff.append(" nonce=" + ByteUtil.toHexString(nonce)).append(""); return toStringBuff.toString(); } - + + public String toStylishString() { + + toStringBuff.setLength(0); + toStringBuff.append(", parentHash=" + ByteUtil.toHexString(parentHash)).append("
"); + toStringBuff.append(", unclesHash=" + ByteUtil.toHexString(unclesHash)).append("
"); + toStringBuff.append(", coinbase=" + ByteUtil.toHexString(coinbase)).append("
"); + toStringBuff.append(", stateRoot=" + 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 + " (" + Utils.longToDateTime(timestamp) + ")").append("
"); + toStringBuff.append(", extraData=" + ByteUtil.toHexString(extraData)).append("
"); + toStringBuff.append(", nonce=" + ByteUtil.toHexString(nonce)).append("
"); + return toStringBuff.toString(); + } + } 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 a6a04bdc..4d908372 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java @@ -222,6 +222,22 @@ public class Transaction { " ]"; } + public String toStylishString() { + if (!parsed) rlpParse(); + return " TransactionData [" + " hash=" + ByteUtil.toHexString(hash) + "
" + + "-> , nonce=" + ByteUtil.toHexString(nonce) + "
" + + "-> , gasPrice=" + ByteUtil.toHexString(gasPrice) + "
" + + "-> , gas=" + ByteUtil.toHexString(gasLimit) + "
" + + "-> , receiveAddress=" + ByteUtil.toHexString(receiveAddress) + "
" + + "-> , value=" + ByteUtil.toHexString(value) + "
" + + "-> , data=" + ByteUtil.toHexString(data) + "
" + + "-> , signatureV=" + signature.v + "
" + + "-> , signatureR=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.r)) + "
" + + "-> , signatureS=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.s)) + "
" + + " ]"; + } + + /** * For signatures you have to keep also * RLP of the transaction without any signature data diff --git a/ethereumj-core/src/main/java/org/ethereum/core/TransactionReceipt.java b/ethereumj-core/src/main/java/org/ethereum/core/TransactionReceipt.java index 22c6b95b..14a034df 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/TransactionReceipt.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/TransactionReceipt.java @@ -45,4 +45,14 @@ public class TransactionReceipt { "\n , cumulativeGas=" + Hex.toHexString(cumulativeGas) + ']'; } + + + public String toStylishString() { + return " TransactionReceipt[" + + "
" + transaction.toStylishString() + + "
, postTxState=" + Hex.toHexString(postTxState) + + "
, cumulativeGas=" + Hex.toHexString(cumulativeGas) + + ']'; + } + } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index 7b6c4c1b..ecc6e7c4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -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 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 2e38e3e3..2feeabaf 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 @@ -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 @@ -233,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(); @@ -251,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();