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 dc1ca034..a786c7e4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -1,6 +1,7 @@ package org.ethereum.core; import org.ethereum.db.DatabaseImpl; +import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.util.AdvancedDeviceUtils; import org.ethereum.util.ByteUtil; @@ -82,7 +83,7 @@ public class Blockchain { } public Block getByNumber(long blockNr) { - return new Block(index.get(blockNr)); + return new Block(chainDb.get(ByteUtil.longToBytes(blockNr))); } public void applyBlock(Block block) { @@ -115,7 +116,7 @@ public class Blockchain { String blockStateRootHash = Hex.toHexString(block.getStateRoot()); String worldStateRootHash = Hex.toHexString(WorldManager.getInstance().getRepository().getWorldState().getRootHash()); if(!blockStateRootHash.equals(worldStateRootHash)){ -// logger.warn("WARNING: STATE CONFLICT! block: {} worldstate {} mismatch", blockNum, worldStateRootHash); + logger.warn("WARNING: STATE CONFLICT! block: {} worldstate {} mismatch", block.getNumber(), worldStateRootHash); // Last fail on WARNING: STATE CONFLICT! block: 1157 worldstate b1d9a978451ef04c1639011d9516473d51c608dbd25906c89be791707008d2de mismatch // System.exit(-1); } @@ -130,6 +131,11 @@ public class Blockchain { logger.info("*** Block chain size: [ {} ]", this.getSize()); + EthereumListener listener = WorldManager.getInstance().getListener(); + if (listener != null) + listener.trace(String.format("Block chain size: [ %d ]", this.getSize())); + + /* if (lastBlock.getNumber() >= 30) { @@ -153,7 +159,7 @@ public class Blockchain { } this.chainDb.put(ByteUtil.longToBytes(block.getNumber()), block.getEncoded()); - this.index.put(block.getNumber(), block.getEncoded()); + this.index.put(block.getNumber(), block.getHash()); WorldManager.getInstance().getWallet().processBlock(block); this.setLastBlock(block); @@ -188,7 +194,7 @@ public class Blockchain { logger.debug("Displaying blocks stored in DB sorted on blocknumber"); for (iterator.seekToFirst(); iterator.hasNext();) { this.lastBlock = new Block(iterator.next().getValue()); - this.index.put(lastBlock.getNumber(), lastBlock.getEncoded()); + this.index.put(lastBlock.getNumber(), lastBlock.getHash()); logger.debug("Block #{} -> {}", lastBlock.getNumber(), lastBlock.toFlatString()); } } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java index b8708bcb..89d4519d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -1,5 +1,6 @@ package org.ethereum.facade; +import org.ethereum.core.Block; import org.ethereum.listener.EthereumListener; import org.ethereum.net.client.PeerData; @@ -16,7 +17,16 @@ public interface Ethereum { public PeerData findPeer(PeerData peerData); public PeerData findPeer(); + + public void stopPeerDiscover(); + public void connect(InetAddress addr, int port); public void connect(String ip, int port); + + public Block getBlockByIndex(long index); + + public long getBlockChainSize(); + public void addListener(EthereumListener listener); + } 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 2d209a79..f7fb5754 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -1,6 +1,6 @@ package org.ethereum.facade; -import org.ethereum.config.SystemProperties; +import org.ethereum.core.Block; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.client.ClientPeer; @@ -31,6 +31,11 @@ public class EthereumImpl implements Ethereum { public PeerData findPeer(PeerData peerData){ logger.info("Looking for online peer"); + EthereumListener listener = WorldManager.getInstance().getListener(); + if (listener != null) + listener.trace("Looking for online peer"); + + WorldManager.getInstance().startPeerDiscovery(); List peers = WorldManager.getInstance().getPeers(); boolean found = false; @@ -44,6 +49,10 @@ public class EthereumImpl implements Ethereum { if (peer.isOnline() && !peer.equals(peerData)){ logger.info("Found peer: {}", peer.toString()); + + if (listener != null) + listener.trace(String.format("Found online peer: [ %s ]", peer.toString())); + return peer; } ++i; @@ -56,6 +65,11 @@ public class EthereumImpl implements Ethereum { return findPeer(null); } + @Override + public void stopPeerDiscover(){ + WorldManager.getInstance().stopPeerDiscover(); + } + @Override public void connect(InetAddress addr, int port){ connect(addr.getHostName(), port); @@ -69,6 +83,17 @@ public class EthereumImpl implements Ethereum { port); } + @Override + public Block getBlockByIndex(long index){ + Block block = WorldManager.getInstance().getBlockChain().getByNumber(index); + return block; + } + + @Override + public long getBlockChainSize(){ + return WorldManager.getInstance().getBlockChain().getSize(); + } + @Override public void addListener(EthereumListener listener) { WorldManager.getInstance().addListener(listener); diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java index 53923cbe..aadd5eeb 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -407,6 +407,15 @@ public class WorldManager { peerDiscovery.start(); }; + public void stopPeerDiscover(){ + + if (listener != null) + listener.trace("Stopping peer discovery"); + + if (peerDiscovery.isStarted()) + peerDiscovery.stop(); + } + public BlockQueue getBlockQueue() { return blockQueue; } @@ -415,4 +424,8 @@ public class WorldManager { blockchain.close(); repository.close(); } + + public EthereumListener getListener() { + return listener; + } } 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 fd1afdef..ad452626 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 @@ -8,6 +8,7 @@ import io.netty.channel.FixedRecvByteBufAllocator; import org.ethereum.config.SystemProperties; import org.ethereum.core.Block; import org.ethereum.core.Transaction; +import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.Command; import org.ethereum.net.MessageQueue; @@ -119,6 +120,11 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { HelloMessage helloMessage = new HelloMessage(rlpList); logger.info(helloMessage.toString()); if (peerListener != null) peerListener.console(helloMessage.toString()); + + EthereumListener listener = WorldManager.getInstance().getListener(); + if (listener != null) + listener.trace(String.format("Got handshake: [ %s ]", helloMessage.toString())); + } // got DISCONNECT if (Command.fromInt(command) == DISCONNECT) { @@ -228,6 +234,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { }, 3000, secToAskForChain * 1000); } + if (blockList.isEmpty()) return; WorldManager.getInstance().getBlockQueue().addBlocks(blockList); if (peerListener != null) peerListener.console(blocksMessage.toString());