Adapting for Ethereum as a library style:

+ building studio script adjust
  + PeerDiscovery regression fix and adjust
This commit is contained in:
romanman 2014-09-06 11:31:55 +03:00
parent 59a76b3cb6
commit 19f371eb77
21 changed files with 259 additions and 139 deletions

View File

@ -9,15 +9,12 @@
<url>http://www.ethereumj.org</url>
<!--
* To deploy the classes in one jar and dependencies to another dir
mvn clean package -Dmaven.test.skip=true
* Install jar with sources to the local maven repository
mvn install -Dmaven.test.skip=true
* To generate source for ANTLR parser/lexer
mvn antlr4:antlr4
* Install jar with sources to the maven repository
mvn install -Dmaven.test.skip=true
-->
<developers>

View File

@ -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() {

View File

@ -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();
}
}

View File

@ -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()) {

View File

@ -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();
}

View File

@ -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<PeerData> 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<PeerData> 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
}

View File

@ -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<PeerData> excludePeers = new HashSet<>();
excludePeers.add(peerData);
return findOnlinePeer(excludePeers);
}
@Override
public PeerData findOnlinePeer() {
Set<PeerData> excludePeers = new HashSet<>();
return findOnlinePeer(excludePeers);
}
@Override
public PeerData findOnlinePeer(Set<PeerData> 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<PeerData> 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<PeerData> 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<PeerData> 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

View File

@ -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<PeerData> peers = new LinkedBlockingQueue<>();
private final Set<PeerData> peers = Collections.synchronizedSet(new HashSet<PeerData>());
private ClientPeer activePeer;
@ -82,20 +77,24 @@ public class WorldManager {
}
public void addPeers(final Set<PeerData> 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<PeerData> getPeers() {
public Set<PeerData> getPeers() {
return peers;
}
public void close() {
stopPeerDiscovery();
repository.close();
blockchain.close();
}
}

View File

@ -23,7 +23,7 @@ public class BlockQueue {
private Queue<Block> 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();
}
}

View File

@ -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<PeerData> peers = WorldManager.getInstance().getPeers();
final Set<PeerData> 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);
}
}
}

View File

@ -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;

View File

@ -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<PeerData> peers;
private final Set<PeerData> peers;
private final AtomicBoolean started = new AtomicBoolean(false);
public PeerDiscovery(Queue<PeerData> peers) {
public PeerDiscovery(Set<PeerData> peers) {
this.peers = peers;
}
@ -66,6 +64,7 @@ public class PeerDiscovery {
public void stop() {
executorPool.shutdown();
monitor.shutdown();
started.set(false);
}
public boolean isStarted() {

View File

@ -0,0 +1,56 @@
<project name="ethereumj-postpackage" default="run" basedir=".">
<description>
Build file to package the EthereumJ client and dependencies into a zip-file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist" />
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<echo message="Ant is taking control [assembly the package]"> </echo>
<echo message="${maven.project.artifactId}"> </echo>
<echo message="${maven.project.version}"> </echo>
</target>
<target name="make-package">
<mkdir dir="./target/package"/>
<copy todir="./target/package/ethereumj/lib">
<fileset dir="./target/dependency"/>
</copy>
<copy file="./target/${maven.project.artifactId}-${maven.project.version}.jar"
tofile="./target/package/ethereumj/${maven.project.artifactId}.jar"/>
<copy todir="./target/package/ethereumj/">
<fileset dir="./target/classes/config"/>
</copy>
<copy file="./target/classes/log4j.properties" todir="./target/package/ethereumj/config"/>
<copy file="./target/classes/system.properties" todir="./target/package/ethereumj/config"/>
<copy file="./target/classes/GeoLiteCity.dat" todir="./target/package/ethereumj/config"/>
<copy todir="./target/package/ethereumj/samples">
<fileset dir="./samples"/>
</copy>
<zip destfile="ethereumj-${DSTAMP}-${TSTAMP}.zip" basedir="./target/package">
</zip>
</target>
<target name="run" depends="init, make-package"></target>
</project>

View File

@ -4,7 +4,7 @@
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-studio</artifactId>
<packaging>jar</packaging>
<version>0.5.5</version>
<version>0.5.6</version>
<name>EthereumJ</name>
<url>http://www.ethereumj.org</url>
@ -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
-->
<developers>
@ -31,11 +30,6 @@
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/ethereum/ethereumj.git</connection>
<developerConnection>scm:git:ssh://git@github.com/ethereum/ethereumj.git</developerConnection>
<url>https://github.com/ethereum/ethereumj</url>
</scm>
<distributionManagement>
<repository>
@ -69,7 +63,7 @@
<spongycastle.version>1.51.0.0</spongycastle.version>
<generated.sourceDirectory>gen</generated.sourceDirectory>
<github.global.server>github</github.global.server>
</properties>
<dependencies>
@ -208,7 +202,6 @@
</configuration>
</plugin>
<!--
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
@ -228,7 +221,6 @@
</execution>
</executions>
</plugin>
-->
<plugin>
@ -270,31 +262,6 @@
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.9</version>
<configuration>
<message>Maven artifacts for ${project.version}</message> <!-- git commit message -->
<noJekyll>true</noJekyll> <!-- disable webpage processing -->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
<branch>refs/heads/mvn-repo</branch> <!-- remote branch name -->
<includes><include>**/*</include></includes>
<!--<excludes><exclude>**/*sources*</exclude></excludes>-->
<force>true</force>
<repositoryName>ethereumj</repositoryName> <!-- github repo name -->
<repositoryOwner>ethereum</repositoryOwner> <!-- github repo owner -->
</configuration>
<executions>
<!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>

View File

@ -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

View File

@ -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 "";

View File

@ -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<PeerData> 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<PeerData> 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()));
}
}
}
}

View File

@ -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() {

View File

@ -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

View File

@ -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

View File

@ -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