diff --git a/ethereumj-core/pom.xml b/ethereumj-core/pom.xml index 6c3310c8..f44dc7e6 100644 --- a/ethereumj-core/pom.xml +++ b/ethereumj-core/pom.xml @@ -9,15 +9,12 @@ http://www.ethereumj.org 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 08d4a5e5..63def2c6 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Block.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Block.java @@ -115,7 +115,7 @@ public class Block { } public Block getParent() { - return WorldManager.getInstance().getBlockchain().getByNumber(this.getNumber() - 1); + return WorldManager.getInstance().getBlockchain().getBlockByNumber(this.getNumber() - 1); } public byte[] getParentHash() { 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 83d2f3ee..21f28528 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -50,7 +50,7 @@ import static org.ethereum.core.Denomination.SZABO; * Created on: 20/05/2014 10:44 * */ -public class Blockchain { +public class Blockchain implements org.ethereum.facade.Blockchain{ private static final Logger logger = LoggerFactory.getLogger("blockchain"); private static final Logger stateLogger = LoggerFactory.getLogger("state"); @@ -87,7 +87,7 @@ public class Blockchain { return blockCache.size(); } - public Block getByNumber(long blockNr) { + public Block getBlockByNumber(long blockNr) { return repository.getBlock(blockNr); } @@ -454,4 +454,7 @@ public class Blockchain { this.lastBlock = block; } + public void close(){ + blockQueue.close(); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java index 95be61b2..78f0d91d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java @@ -122,7 +122,7 @@ public class Repository { } public Blockchain loadBlockchain() { - Blockchain blockchain = new Blockchain(this); + Blockchain blockchain = WorldManager.getInstance().getBlockchain(); DBIterator iterator = chainDB.iterator(); try { if (!iterator.hasNext()) { diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java new file mode 100644 index 00000000..844f07e4 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java @@ -0,0 +1,17 @@ +package org.ethereum.facade; + +import org.ethereum.core.Block; + +/** + * www.ethereumJ.com + * + * @author: Roman Mandeleil + * Created on: 06/09/2014 08:31 + */ + +public interface Blockchain { + + public int getSize(); + public Block getBlockByNumber(long blockNr); + public long getGasPrice(); +} 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 bf4d6c0a..727387c9 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -6,6 +6,7 @@ import org.ethereum.net.client.ClientPeer; import org.ethereum.net.client.PeerData; import java.net.InetAddress; +import java.util.Set; /** * www.ethereumJ.com @@ -16,17 +17,55 @@ import java.net.InetAddress; public interface Ethereum { - public PeerData findPeer(PeerData peerData) throws InterruptedException; - public PeerData findPeer() throws InterruptedException; + /** + * Find a peer but not this one + * @param excludePeer - peer to exclude + * @return online peer if available otherwise null + */ + public PeerData findOnlinePeer(PeerData excludePeer) ; - public void stopPeerDiscover(); + + /** + * Find an online peer but not from excluded list + * + * @param excludePeerSet - peers to exclude + * @return online peer if available otherwise null + */ + public PeerData findOnlinePeer(Set excludePeerSet) ; + + /** + * @return online peer if available + */ + public PeerData findOnlinePeer(); + + + /** + * That block will block until online peer was found. + * + * @return online peer. + */ + public PeerData waitForOnlinePeer(); + + /* + * + * The set of peers returned + * by the method is not thread + * safe then should be traversed + * sync safe: + * synchronized (peers){ + * for (final PeerData peer : newPeers) {} + * } + * + */ + public Set getPeers(); + + public void startPeerDiscovery(); + public void stopPeerDiscovery(); public void connect(InetAddress addr, int port); public void connect(String ip, int port); - public Block getBlockByIndex(long index); - - public long getBlockChainSize(); + public Blockchain getBlockChain(); public void addListener(EthereumListener listener); @@ -37,4 +76,10 @@ public interface Ethereum { public void close(); + +// 1. WorldManager.getInstance().getWallet(); +// 2. // is blockchain still loading - if buffer is not empty + + + } 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 a8f05cde..3800d9b5 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -1,9 +1,9 @@ package org.ethereum.facade; import java.net.InetAddress; -import java.util.concurrent.BlockingQueue; +import java.util.HashSet; +import java.util.Set; -import org.ethereum.core.Block; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.client.ClientPeer; @@ -30,47 +30,83 @@ public class EthereumImpl implements Ethereum { * Find a peer but not this one * @param peerData - peer to exclude * @return online peer - * @throws InterruptedException */ @Override - public PeerData findPeer(PeerData peerData) throws InterruptedException { + public PeerData findOnlinePeer(PeerData peerData) { + Set excludePeers = new HashSet<>(); + excludePeers.add(peerData); + + return findOnlinePeer(excludePeers); + } + + @Override + public PeerData findOnlinePeer() { + + Set excludePeers = new HashSet<>(); + return findOnlinePeer(excludePeers); + } + + + @Override + public PeerData findOnlinePeer(Set excludePeers) { logger.info("Looking for online peers..."); - + final EthereumListener listener = WorldManager.getInstance().getListener(); if (listener != null) { listener.trace("Looking for online peer"); } WorldManager.getInstance().startPeerDiscovery(); - - final BlockingQueue peers = WorldManager.getInstance().getPeers(); - - PeerData peer = null; - - while ((peer = peers.take()) != null) { // it blocks until a peer is available. - - if (peer.isOnline() && !peer.equals(peerData)){ - logger.info("Found peer: {}", peer.toString()); + final Set peers = WorldManager.getInstance().getPeers(); + synchronized (peers) { - if (listener != null) - listener.trace(String.format("Found online peer: [ %s ]", peer.toString())); + for (PeerData peer : peers) { // it blocks until a peer is available. - return peer; + if (peer.isOnline() && !excludePeers.contains(peer)) { + + logger.info("Found peer: {}", peer.toString()); + + if (listener != null) + listener.trace(String.format("Found online peer: [ %s ]", peer.toString())); + + return peer; + } } } return null; } + @Override - public PeerData findPeer() throws InterruptedException { - return findPeer(null); + public PeerData waitForOnlinePeer(){ + + PeerData peer = null; + while(peer == null){ + + try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();} + peer = this.findOnlinePeer(); + } + + return peer; + } + + + @Override + public Set getPeers() { + return WorldManager.getInstance().getPeers(); } @Override - public void stopPeerDiscover(){ - WorldManager.getInstance().stopPeerDiscover(); + public void startPeerDiscovery(){ + WorldManager.getInstance().startPeerDiscovery(); + } + + + @Override + public void stopPeerDiscovery(){ + WorldManager.getInstance().stopPeerDiscovery(); } @Override @@ -87,14 +123,8 @@ public class EthereumImpl implements Ethereum { } @Override - public Block getBlockByIndex(long index){ - Block block = WorldManager.getInstance().getBlockchain().getByNumber(index); - return block; - } - - @Override - public long getBlockChainSize(){ - return WorldManager.getInstance().getBlockchain().getSize(); + public Blockchain getBlockChain() { + return WorldManager.getInstance().getBlockchain(); } @Override 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 e13e525e..3921fb90 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -5,12 +5,7 @@ import static org.ethereum.config.SystemProperties.CONFIG; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; +import java.util.*; import org.ethereum.core.AccountState; import org.ethereum.core.Blockchain; @@ -39,7 +34,7 @@ public class WorldManager { private PeerDiscovery peerDiscovery; - private final BlockingQueue peers = new LinkedBlockingQueue<>(); + private final Set peers = Collections.synchronizedSet(new HashSet()); private ClientPeer activePeer; @@ -82,20 +77,24 @@ public class WorldManager { } public void addPeers(final Set newPeers) { - for (final PeerData peer : newPeers) { - peers.add(peer); - if (peerDiscovery.isStarted()) { - peerDiscovery.addNewPeerData(peer); - } + + synchronized (peers){ + for (final PeerData peer : newPeers) { + if (peerDiscovery.isStarted() && !peers.contains(peer)) { + peerDiscovery.addNewPeerData(peer); + } + peers.add(peer); + } } + } public void startPeerDiscovery() { if (!peerDiscovery.isStarted()) peerDiscovery.start(); - }; + } - public void stopPeerDiscover(){ + public void stopPeerDiscovery(){ if (listener != null) listener.trace("Stopping peer discovery"); @@ -104,10 +103,6 @@ public class WorldManager { peerDiscovery.stop(); } - public void close() { - repository.close(); - } - public EthereumListener getListener() { return listener; } @@ -164,8 +159,15 @@ public class WorldManager { return activePeer; } - public BlockingQueue getPeers() { + public Set getPeers() { return peers; } + + public void close() { + stopPeerDiscovery(); + repository.close(); + blockchain.close(); + } + } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java b/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java index 0a0c416a..eeb5c633 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java @@ -23,7 +23,7 @@ public class BlockQueue { private Queue blockQueue = new ConcurrentLinkedQueue<>(); private Block lastBlock; - private Timer timer = new Timer(); + private Timer timer = new Timer("BlockQueueTimer"); public BlockQueue() { @@ -87,4 +87,9 @@ public class BlockQueue { return blockQueue.size(); } + public void close(){ + timer.cancel(); + timer.purge(); + } + } 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 524fbf5b..3e01b258 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 @@ -16,11 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongycastle.util.encoders.Hex; -import java.util.AbstractQueue; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Queue; +import java.util.*; import java.util.concurrent.TimeUnit; import static org.ethereum.config.SystemProperties.CONFIG; @@ -89,12 +85,14 @@ public class ClientPeer { handler.killTimers(); - final Queue peers = WorldManager.getInstance().getPeers(); + final Set peers = WorldManager.getInstance().getPeers(); - for (PeerData peer : peers){ - if (host.equals(peer.getInetAddress().getHostAddress()) && - port == peer.getPort()){ - peer.setOnline(false); + synchronized (peers){ + for (PeerData peer : peers){ + if (host.equals(peer.getInetAddress().getHostAddress()) && + port == peer.getPort()){ + peer.setOnline(false); + } } } 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 2a954b79..78e61c2f 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 @@ -41,10 +41,10 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { private Logger logger = LoggerFactory.getLogger("wire"); - private Timer chainAskTimer = new Timer(); + private Timer chainAskTimer = new Timer("ChainAskTimer"); private int secToAskForChain = 1; - private final Timer timer = new Timer(); + private final Timer timer = new Timer("MiscMessageTimer"); private boolean tearDown = false; diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java index f75ccc82..ba688780 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java @@ -4,9 +4,7 @@ import org.ethereum.net.client.PeerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Collection; -import java.util.List; -import java.util.Queue; +import java.util.Set; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -26,11 +24,11 @@ public class PeerDiscovery { private ThreadPoolExecutor executorPool; private PeerDiscoveryMonitorThread monitor; - private final Queue peers; + private final Set peers; private final AtomicBoolean started = new AtomicBoolean(false); - public PeerDiscovery(Queue peers) { + public PeerDiscovery(Set peers) { this.peers = peers; } @@ -66,6 +64,7 @@ public class PeerDiscovery { public void stop() { executorPool.shutdown(); monitor.shutdown(); + started.set(false); } public boolean isStarted() { diff --git a/ethereumj-studio/build-post-package.xml b/ethereumj-studio/build-post-package.xml new file mode 100644 index 00000000..d3916079 --- /dev/null +++ b/ethereumj-studio/build-post-package.xml @@ -0,0 +1,56 @@ + + + + Build file to package the EthereumJ client and dependencies into a zip-file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ethereumj-studio/pom.xml b/ethereumj-studio/pom.xml index 1520d400..44c7c2a0 100644 --- a/ethereumj-studio/pom.xml +++ b/ethereumj-studio/pom.xml @@ -4,7 +4,7 @@ org.ethereum ethereumj-studio jar - 0.5.5 + 0.5.6 EthereumJ http://www.ethereumj.org @@ -18,8 +18,7 @@ * Install jar with sources to the maven repository mvn source:jar install -Dmaven.test.skip=true - * Release to mvn repository in github (settings.xml should be updated with user/pass): - mvn clean deploy -Dmaven.test.skip=true + --> @@ -31,11 +30,6 @@ - - scm:git:git://github.com/ethereum/ethereumj.git - scm:git:ssh://git@github.com/ethereum/ethereumj.git - https://github.com/ethereum/ethereumj - @@ -69,7 +63,7 @@ 1.51.0.0 gen - github + @@ -208,7 +202,6 @@ - @@ -270,31 +262,6 @@ - - com.github.github - site-maven-plugin - 0.9 - - Maven artifacts for ${project.version} - true - ${project.build.directory}/mvn-repo - refs/heads/mvn-repo - **/* - - true - ethereumj - ethereum - - - - - - site - - deploy - - - diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/BlockChainTable.java b/ethereumj-studio/src/main/java/org/ethereum/gui/BlockChainTable.java index 18398f0a..14533e7e 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/BlockChainTable.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/BlockChainTable.java @@ -73,9 +73,9 @@ public class BlockChainTable extends JFrame { @Override public void actionPerformed(ActionEvent e) { - if (UIEthereumManager.ethereum.getBlockChainSize() - 1 < lastFindIndex) return; + if (UIEthereumManager.ethereum.getBlockChain().getSize() - 1 < lastFindIndex) return; - Block block = UIEthereumManager.ethereum.getBlockByIndex(lastFindIndex);; + Block block = UIEthereumManager.ethereum.getBlockChain().getBlockByNumber(lastFindIndex); StringSelection stsel = new StringSelection(block.toString()); Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard(); system.setContents(stsel,stsel); @@ -95,10 +95,10 @@ public class BlockChainTable extends JFrame { return; } - for (int i = lastFindIndex + 1; i < UIEthereumManager.ethereum.getBlockChainSize(); ++i) { + for (int i = lastFindIndex + 1; i < UIEthereumManager.ethereum.getBlockChain().getSize(); ++i) { - if (UIEthereumManager.ethereum.getBlockChainSize() - 1 < i) return; - Block block = UIEthereumManager.ethereum.getBlockByIndex(i); + if (UIEthereumManager.ethereum.getBlockChain().getSize() - 1 < i) return; + Block block = UIEthereumManager.ethereum.getBlockChain().getBlockByNumber(i); boolean found = block.toString().toLowerCase().contains(toFind.toLowerCase()); if (found) { // TODO: now we find the first occur diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/BlockTableModel.java b/ethereumj-studio/src/main/java/org/ethereum/gui/BlockTableModel.java index 5ae88165..7a114959 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/BlockTableModel.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/BlockTableModel.java @@ -16,7 +16,7 @@ public class BlockTableModel extends AbstractTableModel { public int getRowCount() { fireTableDataChanged(); - int rowCount = (int) UIEthereumManager.ethereum.getBlockChainSize(); + int rowCount = (int) UIEthereumManager.ethereum.getBlockChain().getSize(); return rowCount; } @@ -28,7 +28,7 @@ public class BlockTableModel extends AbstractTableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - Block block = UIEthereumManager.ethereum.getBlockByIndex(rowIndex); + Block block = UIEthereumManager.ethereum.getBlockChain().getBlockByNumber(rowIndex); if (block == null) return ""; diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableModel.java b/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableModel.java index e1c0c662..72243ffa 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableModel.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableModel.java @@ -10,7 +10,6 @@ import javax.swing.table.AbstractTableModel; import org.ethereum.geo.IpGeoDB; -import org.ethereum.manager.WorldManager; import org.ethereum.net.client.PeerData; import org.ethereum.net.message.HelloMessage; import org.ethereum.util.Utils; @@ -112,11 +111,15 @@ public class PeersTableModel extends AbstractTableModel { synchronized (peerInfoList) { peerInfoList.clear(); - final Queue peers = WorldManager.getInstance().getPeers(); - for (PeerData peer : peers) { - InetAddress addr = peer.getInetAddress(); - Location cr = IpGeoDB.getLocationForIp(addr); - peerInfoList.add(new PeerInfo(cr, addr, peer.isOnline(), peer.getHandshake(), peer.getLastCheckTime())); + final Set peers = UIEthereumManager.ethereum.getPeers(); + + synchronized (peers){ + + for (PeerData peer : peers) { + InetAddress addr = peer.getInetAddress(); + Location cr = IpGeoDB.getLocationForIp(addr); + peerInfoList.add(new PeerInfo(cr, addr, peer.isOnline(), peer.getHandshake(), peer.getLastCheckTime())); + } } } } diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableWindow.java b/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableWindow.java index dff1bc4a..2bebf801 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableWindow.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/PeersTableWindow.java @@ -1,7 +1,5 @@ package org.ethereum.gui; -import org.ethereum.manager.WorldManager; - import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -95,7 +93,7 @@ public class PeersTableWindow extends JFrame { }, 1000, 1000); if (CONFIG.peerDiscovery()) - WorldManager.getInstance().startPeerDiscovery(); + UIEthereumManager.ethereum.startPeerDiscovery(); } public void addCloseAction() { diff --git a/ethereumj-studio/src/main/resources/config/run.bat b/ethereumj-studio/src/main/resources/config/run.bat index 5ac8a30a..ea0b9d14 100644 --- a/ethereumj-studio/src/main/resources/config/run.bat +++ b/ethereumj-studio/src/main/resources/config/run.bat @@ -1,2 +1,2 @@ -call javaw -Xmx6144M -Dlog4j.configuration=file:./config/log4j.properties -jar ethereumj.jar +call javaw -Xmx6144M -Dlog4j.configuration=file:./config/log4j.properties -jar ethereumj-studio.jar pause \ No newline at end of file diff --git a/ethereumj-studio/src/main/resources/config/run.sh b/ethereumj-studio/src/main/resources/config/run.sh index e7c1275b..2ff1e5aa 100644 --- a/ethereumj-studio/src/main/resources/config/run.sh +++ b/ethereumj-studio/src/main/resources/config/run.sh @@ -1 +1 @@ -java -Xmx6144M -Dlog4j.configuration=file:./config/log4j.properties -jar ethereumj.jar +java -Xmx6144M -Dlog4j.configuration=file:./config/log4j.properties -jar ethereumj-studio.jar diff --git a/ethereumj-studio/src/main/resources/system.properties b/ethereumj-studio/src/main/resources/system.properties index 747d0db8..677e7454 100644 --- a/ethereumj-studio/src/main/resources/system.properties +++ b/ethereumj-studio/src/main/resources/system.properties @@ -25,12 +25,12 @@ peer.discovery.ip.list = 185.43.109.23:30303, \ #peer.active.port = 30303 # RomanJ -#peer.active.ip = 54.211.14.10 -#peer.active.port = 30303 +peer.active.ip = 54.211.14.10 +peer.active.port = 30303 # PoC-5 testnet -peer.active.ip = 185.43.109.23 -peer.active.port = 30303 +#peer.active.ip = 185.43.109.23 +#peer.active.port = 30303 # Localhost #peer.active.ip = 127.0.0.1