Replaced RoboSpring with Dagger2

This commit is contained in:
Adrian Tiberius 2015-05-22 21:19:38 +02:00
parent ea2fc0239f
commit 9b29b9ea95
50 changed files with 721 additions and 255 deletions

View File

@ -1,5 +1,14 @@
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
@ -52,11 +61,14 @@ android {
}
dependencies {
apt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.google.dagger:dagger:2.0'
compile 'com.android.support:multidex:1.0.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile project(':ethereumj-core')
compile fileTree(include: ['*.jar'], dir: '../libraries')
//compile 'com.android.support:appcompat-v7:21.0.3'
compile files('./libs/robospring-1.0.7.jar')
//compile files('./libs/robospring-1.0.7.jar')
//compile 'com.litl:android-leveldb:1.0.3@aar'
//compile "org.slf4j:slf4j-android:1.6.1-RC1"
compile 'com.android.support:support-v4:22.1.1'

View File

@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:name="org.ethereum.ethereum_android.EthereumApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

View File

@ -0,0 +1,38 @@
package org.ethereum.ethereum_android;
import android.app.Application;
import android.support.multidex.MultiDexApplication;
import javax.inject.Singleton;
import dagger.Component;
import org.ethereum.ethereum_android.di.components.ApplicationComponent;
import org.ethereum.ethereum_android.di.components.DaggerApplicationComponent;
import org.ethereum.ethereum_android.di.modules.ApplicationModule;
/**
* Created by userica on 20.05.2015.
*/
public class EthereumApplication extends MultiDexApplication {
private ApplicationComponent applicationComponent = null;
@Override public void onCreate() {
super.onCreate();
if (applicationComponent == null) {
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
}
public void setComponent(ApplicationComponent component) {
this.applicationComponent = component;
}
public ApplicationComponent getApplicationComponent() {
return applicationComponent;
}
}

View File

@ -1,9 +1,11 @@
package org.ethereum.ethereum_android;
import android.content.Context;
import org.ethereum.EthereumFactory;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.AccountState;
import org.ethereum.facade.Ethereum;
import org.ethereum.EthereumFactory;
import org.ethereum.facade.Repository;
import org.ethereum.listener.EthereumListenerAdapter;
@ -18,9 +20,9 @@ public class EthereumManager {
public static String log = "";
public EthereumManager(android.content.Context androidContext) {
public EthereumManager(Ethereum ethereum) {
ethereum = EthereumFactory.getEthereum(androidContext);
this.ethereum = ethereum;
this.addListener();
}
@ -28,7 +30,7 @@ public class EthereumManager {
ethereum.connect(SystemProperties.CONFIG.activePeerIP(),
SystemProperties.CONFIG.activePeerPort(),
"e3d09d2f829950b5f3f82d1bddb6f5388bff2f2cca880fa47923df4d8129e8c9b5ba5d4371efcffc416b0cefe20cb55b81b2b71183464713a86e60b423b77947");
SystemProperties.CONFIG.activePeerNodeid());
}
public void loadAccounts() {

View File

@ -16,12 +16,20 @@ import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContext;
import org.ethereum.di.components.DaggerEthereumComponent;
import org.ethereum.di.components.EthereumComponent;
import org.ethereum.di.modules.EthereumModule;
import org.ethereum.ethereum_android.di.components.ApplicationComponent;
import org.ethereum.ethereum_android.di.modules.ActivityModule;
import org.ethereum.facade.Ethereum;
import javax.inject.Inject;
public class MainActivity extends ActionBarActivity implements OnClickListener, NavigationDrawerCallbacks {
public static ApplicationContext context = null;
// public static ApplicationContext context = null;
private static final String TAG = "MyActivity";
private static Integer quit = 0;
private TextView text1;
@ -29,14 +37,23 @@ public class MainActivity extends ActionBarActivity implements OnClickListener,
private Button walletButton;
public EthereumManager ethereumManager = null;
Ethereum ethereum = null;
private NavigationDrawerFragment mNavigationDrawerFragment;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this.getApplicationComponent().inject(this);
System.setProperty("sun.arch.data.model", "32");
System.setProperty("leveldb.mmap", "false");
ethereum = DaggerEthereumComponent.builder()
.ethereumModule(new EthereumModule(this))
.build().ethereum();
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
@ -51,9 +68,8 @@ public class MainActivity extends ActionBarActivity implements OnClickListener,
StrictMode.enableDefaults();
System.setProperty("sun.arch.data.model", "32");
System.setProperty("leveldb.mmap", "false");
new PostTask().execute(getApplicationContext());
new PostTask().execute(ethereum);
Thread t = new Thread() {
@ -80,6 +96,14 @@ public class MainActivity extends ActionBarActivity implements OnClickListener,
t.start();
}
protected ApplicationComponent getApplicationComponent() {
return ((EthereumApplication)getApplication()).getApplicationComponent();
}
protected ActivityModule getActivityModule() {
return new ActivityModule(this);
}
public void onClick(View v) {
switch (v.getId()) {
/*
@ -135,7 +159,7 @@ public class MainActivity extends ActionBarActivity implements OnClickListener,
}
// The definition of our task class
private class PostTask extends AsyncTask<android.content.Context, Integer, String> {
private class PostTask extends AsyncTask<Ethereum, Integer, String> {
@Override
@ -145,10 +169,10 @@ public class MainActivity extends ActionBarActivity implements OnClickListener,
}
@Override
protected String doInBackground(android.content.Context... params) {
android.content.Context context=params[0];
protected String doInBackground(Ethereum... params) {
Ethereum ethereum = params[0];
Log.v(TAG, "111");
ethereumManager = new EthereumManager(context);
ethereumManager = new EthereumManager(ethereum);
Log.v(TAG, "222");
ethereumManager.connect();
Log.v(TAG, "333");

View File

@ -0,0 +1,14 @@
package org.ethereum.ethereum_android.di;
import java.lang.annotation.Retention;
import javax.inject.Scope;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by userica on 22.05.2015.
*/
@Scope
@Retention(RUNTIME)
public @interface PerActivity {}

View File

@ -0,0 +1,18 @@
package org.ethereum.ethereum_android.di.components;
import android.app.Activity;
import dagger.Component;
import org.ethereum.ethereum_android.di.PerActivity;
import org.ethereum.ethereum_android.di.modules.ActivityModule;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
Activity activity();
}

View File

@ -0,0 +1,22 @@
package org.ethereum.ethereum_android.di.components;
import android.content.Context;
import org.ethereum.ethereum_android.MainActivity;
import org.ethereum.ethereum_android.di.modules.ApplicationModule;
import org.ethereum.facade.Ethereum;
import dagger.Component;
import javax.inject.Singleton;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@Singleton // Constraints this component to one-per-application or unscoped bindings.
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(MainActivity mainActivity);
//Exposed to sub-graphs.
Context context();
}

View File

@ -0,0 +1,13 @@
package org.ethereum.ethereum_android.di.components;
import android.content.Context;
import org.ethereum.di.components.EthereumComponent;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
public interface EthereumApplicationComponent extends EthereumComponent {
Context context();
}

View File

@ -0,0 +1,33 @@
package org.ethereum.ethereum_android.di.modules;
import android.app.Activity;
import org.ethereum.ethereum_android.di.PerActivity;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumImpl;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@Module
public class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
@Provides
@PerActivity
Activity activity() {
return this.activity;
}
}

View File

@ -0,0 +1,32 @@
package org.ethereum.ethereum_android.di.modules;
import android.content.Context;
import org.ethereum.ethereum_android.EthereumApplication;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumImpl;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@Module
public class ApplicationModule {
private final EthereumApplication application;
public ApplicationModule(EthereumApplication application) {
this.application = application;
}
@Provides @Singleton
Context provideApplicationContext() {
return this.application;
}
}

View File

@ -1,3 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
buildscript {
repositories {
maven {
@ -8,11 +11,12 @@ buildscript {
}
dependencies {
classpath 'me.champeau.gradle:antlr4-gradle-plugin:0.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
apply plugin: 'me.champeau.gradle.antlr4'
apply plugin: 'com.android.library'
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
@ -75,8 +79,11 @@ ext {
}
dependencies {
apt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.google.dagger:dagger:2.0'
//compile 'com.android.support:appcompat-v7:+'
compile fileTree(include: ['*.jar'], dir: 'libs')
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile('com.j256.ormlite:ormlite-android:4.48') {
exclude group: 'commons-logging', module: 'commons-logging'

View File

@ -2,24 +2,27 @@ package org.ethereum;
import org.ethereum.config.SystemProperties;
import org.ethereum.di.components.DaggerEthereumComponent;
import org.ethereum.di.components.EthereumComponent;
import org.ethereum.di.modules.EthereumModule;
import org.ethereum.facade.Ethereum;
import org.robospring.RoboSpring;
import org.springframework.context.ApplicationContext;
//import org.robospring.RoboSpring;
//import org.springframework.context.ApplicationContext;
/**
* Created by userica on 06.05.2015.
*/
public class EthereumFactory {
public static ApplicationContext context = null;
// public static ApplicationContext context = null;
public static EthereumComponent ethereumComponent = null;
public static Ethereum getEthereum(android.content.Context androidContext) {
RoboSpring.autowire(androidContext);
context = RoboSpring.getContext();
Ethereum ethereum = context.getBean(org.ethereum.facade.Ethereum.class);
ethereum.setContext(context);
return ethereum;
// RoboSpring.autowire(androidContext);
// context = RoboSpring.getContext();
// Ethereum ethereum = context.getBean(org.ethereum.facade.Ethereum.class);
// ethereum.setContext(context);
ethereumComponent = DaggerEthereumComponent.builder()
.ethereumModule(new EthereumModule(androidContext))
.build();
return ethereumComponent.ethereum();
}
}

View File

@ -18,7 +18,7 @@ public class SystemProperties {
private final static int DEFAULT_TX_APPROVE_TIMEOUT = 10;
private final static String DEFAULT_DISCOVERY_PEER_LIST = "poc-9.ethdev.com:30303";
private final static String DEFAULT_ACTIVE_PEER_NODEID = "e3d09d2f829950b5f3f82d1bddb6f5388bff2f2cca880fa47923df4d8129e8c9b5ba5d4371efcffc416b0cefe20cb55b81b2b71183464713a86e60b423b77947"; // FIXME
private final static String DEFAULT_ACTIVE_PEER_NODEID = "bf01b54b6bc7faa203286dfb8359ce11d7b1fe822968fb4991f508d6f5a36ab7d9ae8af9b0d61c0467fb08567e0fb71cfb9925a370b69f9ede97927db473d1f5"; // FIXME
private final static String DEFAULT_ACTIVE_PEER_IP = "poc-9.ethdev.com";
private final static int DEFAULT_ACTIVE_PORT = 30303;
private final static String DEFAULT_SAMPLES_DIR = "samples";

View File

@ -1,10 +1,11 @@
package org.ethereum.core;
import org.ethereum.crypto.ECKey;
import org.ethereum.facade.Repository;
import org.ethereum.manager.WorldManager;
import org.ethereum.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -15,6 +16,8 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
/**
* Representation of an actual account or contract
*/
@ -28,10 +31,11 @@ public class Account {
private Set<Transaction> pendingTransactions =
Collections.synchronizedSet(new HashSet<Transaction>());
@Autowired
WorldManager worldManager;
Repository repository;
public Account() {
@Inject
public Account(Repository repository) {
this.repository = repository;
}
public void init() {
@ -46,7 +50,7 @@ public class Account {
public BigInteger getNonce() {
AccountState accountState =
worldManager.getRepository().getAccountState(getAddress());
repository.getAccountState(getAddress());
return accountState.getNonce();
}
@ -54,7 +58,7 @@ public class Account {
public BigInteger getBalance() {
AccountState accountState =
worldManager.getRepository().getAccountState(this.getAddress());
repository.getAccountState(this.getAddress());
BigInteger balance = BigInteger.ZERO;

View File

@ -14,11 +14,12 @@ import org.ethereum.trie.Trie;
import org.ethereum.trie.TrieImpl;
import org.ethereum.util.*;
import org.ethereum.vm.ProgramInvokeFactory;
import org.ethereum.vm.ProgramInvokeFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Component;
//import org.springframework.util.FileSystemUtils;
import org.apache.commons.io.FileUtils;
@ -32,6 +33,8 @@ import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.*;
import javax.inject.Inject;
import static org.ethereum.config.Constants.*;
import static org.ethereum.config.SystemProperties.CONFIG;
import static org.ethereum.core.Denomination.SZABO;
@ -82,34 +85,26 @@ public class BlockchainImpl implements Blockchain {
// @Qualifier("pendingTransactions")
private Set<Transaction> pendingTransactions = new HashSet<>();
@Autowired
private Repository repository;
private Repository track;
@Autowired
private BlockStore blockStore;
private Block bestBlock;
private BigInteger totalDifficulty = BigInteger.ZERO;
@Autowired
Wallet wallet;
@Autowired
private EthereumListener listener;
@Autowired
private BlockQueue blockQueue;
@Autowired
private ChannelManager channelManager;
private boolean syncDoneCalled = false;
@Autowired
ProgramInvokeFactory programInvokeFactory;
@Autowired
private AdminInfo adminInfo;
private List<Chain> altChains = new ArrayList<>();
@ -122,14 +117,18 @@ public class BlockchainImpl implements Blockchain {
//todo: autowire over constructor
@Inject
public BlockchainImpl(BlockStore blockStore, Repository repository,
Wallet wallet, AdminInfo adminInfo,
EthereumListener listener) {
EthereumListener listener, ChannelManager channelManager) {
this.blockStore = blockStore;
this.repository = repository;
this.wallet = wallet;
this.adminInfo = adminInfo;
this.listener = listener;
this.channelManager = channelManager;
this.blockQueue = new BlockQueue(this);
this.programInvokeFactory = new ProgramInvokeFactoryImpl();
}
@Override

View File

@ -2,7 +2,9 @@ package org.ethereum.core;
import org.ethereum.crypto.ECKey;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.facade.Repository;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.server.Channel;
import org.ethereum.net.submit.WalletTransaction;
import org.slf4j.Logger;
@ -10,8 +12,8 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.stereotype.Component;
@ -35,6 +37,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Provider;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@ -44,6 +47,8 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.inject.Inject;
/**
* The Wallet handles the management of accounts with addresses and private keys.
* New accounts can be generated and added to the wallet and existing accounts can be queried.
@ -66,22 +71,23 @@ public class Wallet {
private Map<String, Account> rows = new HashMap<>();
private long high;
@Autowired
private WorldManager worldManager;
@Autowired
private ApplicationContext context;
Repository repository;
private List<WalletListener> listeners = new ArrayList<>();
private Map<ByteArrayWrapper, Transaction> transactionMap = new HashMap<>();
public void setWorldManager(WorldManager worldManager) {
this.worldManager = worldManager;
@Inject
Provider<Account> accountProvider;
@Inject
public Wallet(Repository repository) {
this.repository = repository;
}
public void addNewAccount() {
Account account = new Account();
Account account = accountProvider.get();
account.init();
String address = Hex.toHexString(account.getEcKey().getAddress());
rows.put(address, account);
@ -90,7 +96,7 @@ public class Wallet {
}
public void importKey(byte[] privKey) {
Account account = context.getBean(Account.class);
Account account = accountProvider.get();
account.init(ECKey.fromPrivate(privKey));
String address = Hex.toHexString(account.getEcKey().getAddress());
rows.put(address, account);
@ -107,7 +113,7 @@ public class Wallet {
public AccountState getAccountState(byte[] address) {
AccountState accountState =
worldManager.getRepository().getAccountState(address);
repository.getAccountState(address);
return accountState;
}

View File

@ -6,7 +6,7 @@ import org.ethereum.datasource.KeyValueDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
//import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@ -16,7 +16,10 @@ import java.net.URI;
import java.util.Map;
import java.util.Set;
import static org.springframework.util.StringUtils.hasLength;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//import static org.springframework.util.StringUtils.hasLength;
//@Component
public class RedisConnectionImpl implements RedisConnection {
@ -25,11 +28,12 @@ public class RedisConnectionImpl implements RedisConnection {
private JedisPool jedisPool;
@PostConstruct
public void tryConnect() {
if (!SystemProperties.CONFIG.isRedisEnabled()) return;
String redisCloudUrl = System.getenv(REDISCLOUD_URL);
if (!hasLength(redisCloudUrl)) {
if (redisCloudUrl.isEmpty()) {
logger.info("Cannot connect to Redis. 'REDISCLOUD_URL' environment variable is not defined.");
return;
}
@ -44,9 +48,13 @@ public class RedisConnectionImpl implements RedisConnection {
}
}
private static boolean hasText(String string) {
return string != null && !string.isEmpty() && !string.trim().isEmpty();
}
private static JedisPool createJedisPool(URI redisUri) {
String userInfo = redisUri.getUserInfo();
if (StringUtils.hasText(userInfo)) {
if (hasText(userInfo)) {
/*
return new JedisPool(new JedisPoolConfig(),
redisUri.getHost(),
@ -64,6 +72,7 @@ public class RedisConnectionImpl implements RedisConnection {
return null;
}
@PreDestroy
public void destroy() {
if (jedisPool != null) {
jedisPool.destroy();

View File

@ -16,7 +16,7 @@ import static org.ethereum.util.ByteUtil.*;
* Created on: 29/01/2015 20:43
*/
public class InMemoryBlockStore implements BlockStore{
public class InMemoryBlockStore implements BlockStore {
final static public int MAX_BLOCKS = 1000;

View File

@ -0,0 +1,16 @@
package org.ethereum.di.components;
import org.ethereum.di.modules.BlockchainModule;
import org.ethereum.di.modules.EthereumModule;
import javax.inject.Singleton;
import dagger.Component;
/**
* Created by Adrian Tiberius on 21.05.2015.
*/
@Singleton
@Component(modules = BlockchainModule.class)
public interface BlockchainComponent {
}

View File

@ -0,0 +1,23 @@
package org.ethereum.di.components;
import android.content.Context;
import org.ethereum.di.modules.EthereumModule;
import org.ethereum.facade.Ethereum;
import javax.inject.Singleton;
import dagger.Component;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@Singleton
@Component(modules = EthereumModule.class)
public interface EthereumComponent {
//void inject(EthereumManager ethereumManager);
Context context();
Ethereum ethereum();
}

View File

@ -0,0 +1,124 @@
package org.ethereum.di.modules;
import android.content.Context;
import org.ethereum.core.BlockchainImpl;
import org.ethereum.core.Wallet;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.BlockStore;
import org.ethereum.db.InMemoryBlockStore;
import org.ethereum.db.RepositoryImpl;
import org.ethereum.facade.Blockchain;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumImpl;
import org.ethereum.facade.Repository;
import org.ethereum.listener.CompositeEthereumListener;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.AdminInfo;
import org.ethereum.manager.BlockLoader;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.BlockQueue;
import org.ethereum.net.client.PeerClient;
import org.ethereum.net.eth.EthHandler;
import org.ethereum.net.peerdiscovery.PeerDiscovery;
import org.ethereum.net.server.ChannelManager;
import org.ethereum.net.shh.ShhHandler;
import org.ethereum.vm.ProgramInvokeFactory;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Created by Adrian Tiberius on 20.05.2015.
*/
@Module
public class EthereumModule {
private Context context;
public EthereumModule(Context context) {
this.context = context;
}
@Provides
@Singleton
Ethereum provideEthereum(WorldManager worldManager, AdminInfo adminInfo, ChannelManager channelManager,
BlockLoader blockLoader, PeerClient backupPeerClient) {
return new EthereumImpl(worldManager, adminInfo, channelManager, blockLoader, backupPeerClient);
}
@Provides
@Singleton
WorldManager provideWorldManager(Blockchain blockchain, Repository repository, Wallet wallet, PeerDiscovery peerDiscovery
,BlockStore blockStore, ChannelManager channelManager, EthereumListener listener) {
return new WorldManager(blockchain, repository, wallet, peerDiscovery, blockStore, channelManager, listener);
}
@Provides
@Singleton
Blockchain provideBlockchain(BlockStore blockStore, Repository repository,
Wallet wallet, AdminInfo adminInfo,
EthereumListener listener, ChannelManager channelManager) {
return new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener, channelManager);
}
@Provides
@Singleton
BlockStore provideBlockStore() {
return new InMemoryBlockStore();
}
@Provides
@Singleton
Repository provideRepository() {
LevelDbDataSource detailsDS = new LevelDbDataSource();
detailsDS.setContext(context);
LevelDbDataSource stateDS = new LevelDbDataSource();
stateDS.setContext(context);
return new RepositoryImpl(detailsDS, stateDS);
}
@Provides
@Singleton
AdminInfo provideAdminInfo() {
return new AdminInfo();
}
@Provides
@Singleton
EthereumListener provideEthereumListener() {
return new CompositeEthereumListener();
}
@Provides
@Singleton
PeerDiscovery providePeerDiscovery() {
return new PeerDiscovery();
}
@Provides
EthHandler provideEthHandler() {
return new EthHandler();
}
@Provides
ShhHandler provideShhHandler() {
return new ShhHandler();
}
@Provides
@Singleton
Context provideContext() {
return context;
}
@Provides
String provideRemoteId() {
return "e3d09d2f829950b5f3f82d1bddb6f5388bff2f2cca880fa47923df4d8129e8c9b5ba5d4371efcffc416b0cefe20cb55b81b2b71183464713a86e60b423b77947";
}
}

View File

@ -3,12 +3,13 @@ package org.ethereum.facade;
import org.ethereum.db.BlockStore;
import org.ethereum.db.InMemoryBlockStore;
//import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.transaction.annotation.Propagation;
//import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
/**
*
@ -19,7 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
//@Import(CommonConfig.class)
public class DefaultConfig {
@Autowired CommonConfig commonConfig;
@Inject CommonConfig commonConfig;
//@Bean
//@Transactional(propagation = Propagation.SUPPORTS)

View File

@ -8,7 +8,6 @@ import org.ethereum.manager.BlockLoader;
import org.ethereum.net.client.PeerClient;
import org.ethereum.net.peerdiscovery.PeerInfo;
import org.ethereum.net.server.ChannelManager;
import org.springframework.context.ApplicationContext;
import java.math.BigInteger;
import java.net.InetAddress;
@ -63,8 +62,6 @@ public interface Ethereum {
*/
public Set<PeerInfo> getPeers();
public void setContext(ApplicationContext context);
public void startPeerDiscovery();
public void stopPeerDiscovery();

View File

@ -6,7 +6,7 @@ import org.ethereum.net.shh.ShhHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.stereotype.Component;
@ -18,7 +18,7 @@ import org.springframework.context.ApplicationContext;
public class EthereumFactory {
private static final Logger logger = LoggerFactory.getLogger("general");
public static ApplicationContext context = null;
//public static ApplicationContext context = null;
public static Ethereum createEthereum() {
return createEthereum(DefaultConfig.class);
@ -30,7 +30,7 @@ public class EthereumFactory {
logger.info("capability shh version: [{}]", ShhHandler.VERSION);
// context = new AnnotationConfigApplicationContext(clazz);
return context.getBean(Ethereum.class);
return null;//context.getBean(Ethereum.class);
}
}

View File

@ -15,8 +15,8 @@ import org.ethereum.net.submit.TransactionTask;
import org.ethereum.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.stereotype.Component;
//import javax.annotation.PostConstruct;
@ -27,7 +27,10 @@ import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.robospring.RoboSpring;
//import org.robospring.RoboSpring;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Provider;
import static org.ethereum.config.SystemProperties.CONFIG;
@ -40,30 +43,36 @@ public class EthereumImpl implements Ethereum {
private static final Logger logger = LoggerFactory.getLogger("facade");
@Autowired
@Inject
EthereumListener listener;
WorldManager worldManager;
@Autowired
AdminInfo adminInfo;
@Autowired
ChannelManager channelManager;
PeerServer peerServer;
@Autowired
ApplicationContext ctx;
@Autowired
BlockLoader blockLoader;
public EthereumImpl() {
//@Inject
Provider<PeerClient> peerClientProvider;
@Inject
public EthereumImpl(WorldManager worldManager, AdminInfo adminInfo,
ChannelManager channelManager, BlockLoader blockLoader, Provider<PeerClient> peerClientProvider) {
System.out.println();
logger.info("EthereumImpl constructor");
this.worldManager = worldManager;
this.adminInfo = adminInfo;
this.channelManager = channelManager;
this.blockLoader = blockLoader;
this.peerClientProvider = peerClientProvider;
}
//@PostConstruct
@PostConstruct
public void init() {
worldManager.loadBlockchain();
if (CONFIG.listenPort() > 0) {
@ -76,10 +85,6 @@ public class EthereumImpl implements Ethereum {
);
}
}
public void setContext(ApplicationContext ctx) {
this.ctx = ctx;
}
/**
* Find a peer but not this one
@ -104,7 +109,7 @@ public class EthereumImpl implements Ethereum {
public PeerInfo findOnlinePeer(Set<PeerInfo> excludePeers) {
logger.info("Looking for online peers...");
final EthereumListener listener = worldManager.getListener();
final EthereumListener listener = this.listener;
listener.trace("Looking for online peer");
worldManager.startPeerDiscovery();
@ -160,7 +165,7 @@ public class EthereumImpl implements Ethereum {
PeerClient peerClient = worldManager.getActivePeer();
if (peerClient == null)
peerClient = ctx.getBean(PeerClient.class);
peerClient = peerClientProvider.get();
worldManager.setActivePeer(peerClient);
peerClient.connect(ip, port, remoteId);
@ -192,7 +197,7 @@ public class EthereumImpl implements Ethereum {
PeerClient peer = worldManager.getActivePeer();
if (peer == null) {
peer = new PeerClient();
peer = peerClientProvider.get();
worldManager.setActivePeer(peer);
}
return peer;

View File

@ -3,12 +3,13 @@ package org.ethereum.facade;
import org.ethereum.db.BlockStore;
import org.ethereum.db.InMemoryBlockStore;
//import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.transaction.annotation.Propagation;
//import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
/**
*
@ -19,7 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
//@Import(CommonConfig.class)
public class RemoteConfig {
@Autowired CommonConfig commonConfig;
@Inject CommonConfig commonConfig;
// todo: init total difficulty
// todo: init last 1000 blocks

View File

@ -12,6 +12,9 @@ import org.ethereum.jsontestsuite.model.BlockTck;
import org.ethereum.listener.CompositeEthereumListener;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.AdminInfo;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.BlockQueue;
import org.ethereum.net.server.ChannelManager;
import org.ethereum.util.ByteUtil;
import org.ethereum.vm.*;
import org.ethereum.vmtrace.ProgramTrace;
@ -22,6 +25,8 @@ import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.util.*;
import javax.inject.Inject;
import static org.ethereum.jsontestsuite.Utils.parseData;
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
@ -36,6 +41,18 @@ public class TestRunner {
private boolean setNewStateRoot;
private String bestStateRoot;
@Inject
public WorldManager worldManager;
@Inject
public ChannelManager channelManager;
@Inject
public ProgramInvokeFactory programInvokeFactory;
@Inject
public BlockQueue blockqueue;
public List<String> runTestSuite(TestSuite testSuite) {
Iterator<TestCase> testIterator = testSuite.iterator();
@ -64,17 +81,15 @@ public class TestRunner {
BlockStore blockStore = new InMemoryBlockStore();
blockStore.saveBlock(genesis, new ArrayList<TransactionReceipt>());
Wallet wallet = new Wallet();
Wallet wallet = new Wallet(repository);
AdminInfo adminInfo = new AdminInfo();
EthereumListener listener = new CompositeEthereumListener();
ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener);
BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener, channelManager);
blockchain.setBestBlock(genesis);
blockchain.setTotalDifficulty(BigInteger.ZERO);
blockchain.setProgramInvokeFactory(programInvokeFactory);
programInvokeFactory.setBlockchain(blockchain);
// todo: validate root of the genesis *!!!*

View File

@ -2,7 +2,7 @@ package org.ethereum.manager;
//import org.springframework.stereotype.Component;
//import javax.annotation.PostConstruct;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@ -20,7 +20,7 @@ public class AdminInfo {
private List<Long> blockExecTime = new LinkedList<>();
// @PostConstruct
@PostConstruct
public void init() {
startupTimeStamp = System.currentTimeMillis();
}

View File

@ -5,25 +5,28 @@ import org.ethereum.core.Block;
import org.ethereum.facade.Blockchain;
import org.ethereum.net.BlockQueue;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
//import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import java.util.List;
import javax.inject.Inject;
import static org.ethereum.config.SystemProperties.CONFIG;
@Component
//@Component
public class BlockLoader {
@Autowired
private Blockchain blockchain;
@Inject
Blockchain blockchain;
public void loadBlocks(){
@ -31,7 +34,8 @@ public class BlockLoader {
try {
File blocksFile = new File(fileSrc);
System.out.println("Loading blocks: " + fileSrc);
List<String> blocksList = Files.readAllLines(blocksFile.toPath(), StandardCharsets.UTF_8);
//List<String> blocksList = Files.readAllLines(blocksFile.toPath(), StandardCharsets.UTF_8);
List<String> blocksList = FileUtils.readLines(blocksFile, StandardCharsets.UTF_8);
for (String blockRLP : blocksList){

View File

@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import java.math.BigInteger;
@ -33,6 +33,10 @@ import java.util.Set;
import static org.ethereum.config.SystemProperties.CONFIG;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
/**
* WorldManager is a singleton containing references to different parts of the system.
*
@ -44,39 +48,39 @@ public class WorldManager {
private static final Logger logger = LoggerFactory.getLogger("general");
@Autowired
private Blockchain blockchain;
@Autowired
private Repository repository;
@Autowired
private Wallet wallet;
// @Autowired
private PeerClient activePeer;
@Autowired
private PeerDiscovery peerDiscovery;
@Autowired
private BlockStore blockStore;
@Autowired
private ChannelManager channelManager;
@Autowired
// TODO: What is this doing here ?
private AdminInfo adminInfo;
@Autowired
private EthereumListener listener;
public WorldManager() {
@Inject
public WorldManager(Blockchain blockchain, Repository repository, Wallet wallet, PeerDiscovery peerDiscovery
,BlockStore blockStore, ChannelManager channelManager, EthereumListener listener) {
logger.info("World manager instantiated");
this.blockchain = blockchain;
this.repository = repository;
this.wallet = wallet;
this.peerDiscovery = peerDiscovery;
this.blockStore = blockStore;
this.channelManager = channelManager;
this.listener = listener;
}
// @PostConstruct
@PostConstruct
public void init() {
byte[] cowAddr = HashUtil.sha3("cow".getBytes());
wallet.importKey(cowAddr);
@ -206,7 +210,7 @@ public class WorldManager {
}
// @PreDestroy
@PreDestroy
public void close() {
stopPeerDiscovery();
repository.close();

View File

@ -7,7 +7,7 @@ import org.ethereum.facade.Blockchain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import java.math.BigInteger;
@ -15,6 +15,8 @@ import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import javax.inject.Inject;
import static java.lang.Thread.sleep;
import static org.ethereum.config.SystemProperties.CONFIG;
import static org.ethereum.core.ImportResult.NO_PARENT;
@ -56,11 +58,11 @@ public class BlockQueue {
private Timer timer = new Timer("BlockQueueTimer");
@Autowired
Blockchain blockchain;
public BlockQueue() {
public BlockQueue(Blockchain blockchain) {
this.blockchain = blockchain;
Runnable queueProducer = new Runnable(){
@Override

View File

@ -10,7 +10,7 @@ import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -19,6 +19,8 @@ import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.inject.Inject;
import static org.ethereum.net.message.StaticMessages.DISCONNECT_MESSAGE;
/**
@ -45,9 +47,9 @@ public class MessageQueue {
private Queue<MessageRoundtrip> messageQueue = new ConcurrentLinkedQueue<>();
private ChannelHandlerContext ctx = null;
private final Timer timer = new Timer("MessageQueue");
@Autowired
WorldManager worldManager;
@Inject
EthereumListener listener;
boolean hasPing = false;
public MessageQueue() {
@ -78,7 +80,7 @@ public class MessageQueue {
public void receivedMessage(Message msg) throws InterruptedException {
worldManager.getListener().trace("[Recv: " + msg + "]");
listener.trace("[Recv: " + msg + "]");
if (messageQueue.peek() != null) {
MessageRoundtrip messageRoundtrip = messageQueue.peek();
@ -114,7 +116,6 @@ public class MessageQueue {
Message msg = messageRoundtrip.getMsg();
EthereumListener listener = worldManager.getListener();
listener.onSendMessage(msg);
ctx.writeAndFlush(msg);

View File

@ -1,5 +1,6 @@
package org.ethereum.net.client;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.server.ChannelManager;
import org.ethereum.net.server.EthereumChannelInitializer;
@ -15,8 +16,11 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import javax.inject.Inject;
import javax.inject.Provider;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -35,25 +39,27 @@ public class PeerClient {
private boolean peerDiscoveryMode = false;
@Autowired
private ApplicationContext ctx;
EthereumListener listener;
@Autowired
WorldManager worldManager;
ChannelManager channelManager;
@Autowired
public ChannelManager channelManager;
Provider<EthereumChannelInitializer> ethereumChannelInitializerProvider;
public PeerClient() {
@Inject
public PeerClient(EthereumListener listener, ChannelManager channelManager,
Provider<EthereumChannelInitializer> ethereumChannelInitializerProvider) {
logger.info("Peer client instantiated");
this.listener = listener;
this.channelManager = channelManager;
this.ethereumChannelInitializerProvider = ethereumChannelInitializerProvider;
}
public void connect(String host, int port, String remoteId) {
EventLoopGroup workerGroup = new NioEventLoopGroup();
worldManager.getListener().trace("Connecting to: " + host + ":" + port);
listener.trace("Connecting to: " + host + ":" + port);
EthereumChannelInitializer ethereumChannelInitializer = (EthereumChannelInitializer)ctx.getBean("ethereumChannelInitializer", EthereumChannelInitializer.class);
EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerProvider.get();
ethereumChannelInitializer.setRemoteId(remoteId);
try {

View File

@ -3,8 +3,10 @@ package org.ethereum.net.eth;
import org.ethereum.core.Block;
import org.ethereum.core.Genesis;
import org.ethereum.core.Transaction;
import org.ethereum.core.Wallet;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.facade.Blockchain;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.BlockQueue;
import org.ethereum.net.MessageQueue;
@ -19,7 +21,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -27,6 +29,8 @@ import java.math.BigInteger;
import java.util.*;
import javax.inject.Inject;
import static org.ethereum.config.SystemProperties.CONFIG;
import static org.ethereum.net.message.StaticMessages.GET_TRANSACTIONS_MESSAGE;
import static org.ethereum.util.ByteUtil.wrap;
@ -71,11 +75,15 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
private Timer getBlocksTimer = new Timer("GetBlocksTimer");
private Timer getTxTimer = new Timer("GetTransactionsTimer");
@Autowired
private Blockchain blockchain;
@Inject
Blockchain blockchain;
@Inject
EthereumListener listener;
@Inject
Wallet wallet;
@Autowired
private WorldManager worldManager;
private List<ByteArrayWrapper> sentHashes;
private Block lastBlock = Genesis.getInstance();
@ -90,7 +98,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
public void activate() {
logger.info("ETH protocol activated");
worldManager.getListener().trace("ETH protocol activated");
listener.trace("ETH protocol activated");
sendStatus();
}
@ -106,7 +114,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
if (EthMessageCodes.inRange(msg.getCommand().asByte()))
logger.info("EthHandler invoke: [{}]", msg.getCommand());
worldManager.getListener().trace(String.format("EthHandler invoke: [%s]", msg.getCommand()));
listener.trace(String.format("EthHandler invoke: [%s]", msg.getCommand()));
switch (msg.getCommand()) {
case STATUS:
@ -156,11 +164,10 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
private void processTransactions(TransactionsMessage msg) {
Set<Transaction> txSet = msg.getTransactions();
worldManager.getBlockchain().
addPendingTransactions(txSet);
blockchain.addPendingTransactions(txSet);
for (Transaction tx : txSet) {
worldManager.getWallet().addTransaction(tx);
wallet.addTransaction(tx);
}
}
@ -417,8 +424,7 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
private void sendPendingTransactions() {
Set<Transaction> pendingTxs =
worldManager.getBlockchain()
.getPendingTransactions();
blockchain.getPendingTransactions();
TransactionsMessage msg = new TransactionsMessage(pendingTxs);
msgQueue.sendMessage(msg);
}

View File

@ -3,6 +3,7 @@ package org.ethereum.net.p2p;
import io.netty.buffer.ByteBuf;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.MessageQueue;
import org.ethereum.net.client.Capability;
@ -12,6 +13,7 @@ import org.ethereum.net.eth.NewBlockMessage;
import org.ethereum.net.eth.TransactionsMessage;
import org.ethereum.net.message.ReasonCode;
import org.ethereum.net.message.StaticMessages;
import org.ethereum.net.peerdiscovery.PeerDiscovery;
import org.ethereum.net.peerdiscovery.PeerInfo;
import org.ethereum.net.rlpx.FrameCodec;
import org.ethereum.net.server.Channel;
@ -25,7 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -41,6 +43,8 @@ import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import javax.inject.Inject;
import static org.ethereum.net.message.StaticMessages.*;
/**
@ -73,9 +77,12 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
private HelloMessage handshakeHelloMessage = null;
private Set<PeerInfo> lastPeersSent;
@Autowired
WorldManager worldManager;
@Inject
EthereumListener listener;
@Inject
PeerDiscovery peerDiscovery;
private Channel channel;
public P2pHandler() {
@ -88,10 +95,6 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
this.peerDiscoveryMode = peerDiscoveryMode;
}
public void setWorldManager(WorldManager worldManager) {
this.worldManager = worldManager;
}
public void setPeerDiscoveryMode(boolean peerDiscoveryMode) {
this.peerDiscoveryMode = peerDiscoveryMode;
}
@ -101,7 +104,7 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
logger.info("P2P protocol activated");
msgQueue.activate(ctx);
worldManager.getListener().trace("P2P protocol activated");
listener.trace("P2P protocol activated");
startTimers();
}
@ -112,7 +115,7 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
if (P2pMessageCodes.inRange(msg.getCommand().asByte()))
logger.info("P2PHandler invoke: [{}]", msg.getCommand());
worldManager.getListener().trace(String.format("P2PHandler invoke: [%s]", msg.getCommand()));
listener.trace(String.format("P2PHandler invoke: [%s]", msg.getCommand()));
switch (msg.getCommand()) {
case HELLO:
@ -167,7 +170,7 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
}
private void processPeers(ChannelHandlerContext ctx, PeersMessage peersMessage) {
worldManager.getPeerDiscovery().addPeers(peersMessage.getPeers());
peerDiscovery.addPeers(peersMessage.getPeers());
}
private void sendGetPeers() {
@ -176,7 +179,7 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
private void sendPeers() {
Set<PeerInfo> peers = worldManager.getPeerDiscovery().getPeers();
Set<PeerInfo> peers = peerDiscovery.getPeers();
if (lastPeersSent != null && peers.equals(lastPeersSent)) {
logger.info("No new peers discovered don't answer for GetPeers");
@ -234,8 +237,8 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
confirmedPeer.getCapabilities().addAll(msg.getCapabilities());
//todo calculate the Offsets
worldManager.getPeerDiscovery().getPeers().add(confirmedPeer);
worldManager.getListener().onHandShakePeer(msg);
peerDiscovery.getPeers().add(confirmedPeer);
listener.onHandShakePeer(msg);
}
}

View File

@ -5,6 +5,8 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.MessageQueue;
import org.ethereum.net.client.Capability;
@ -16,13 +18,16 @@ import org.ethereum.net.shh.ShhHandler;
import org.ethereum.net.wire.MessageCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Provider;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
@ -38,33 +43,33 @@ public class DiscoveryChannel {
private boolean peerDiscoveryMode = false;
@Autowired
WorldManager worldManager;
@Inject
EthereumListener listener;
@Inject
Provider<MessageCodec> messageCodecProvider;
@Autowired
MessageQueue messageQueue;
@Autowired
P2pHandler p2pHandler;
@Autowired
EthHandler ethHandler;
@Autowired
ShhHandler shhHandler;
@Autowired
ApplicationContext ctx;
public DiscoveryChannel() {
@Inject
public DiscoveryChannel(MessageQueue messageQueue, P2pHandler p2pHandler
, EthHandler ethHandler, ShhHandler shhHandler) {
this.messageQueue = messageQueue;
this.p2pHandler = p2pHandler;
this.ethHandler = ethHandler;
this.shhHandler = shhHandler;
}
public void connect(String host, int port) {
EventLoopGroup workerGroup = new NioEventLoopGroup();
worldManager.getListener().trace("Connecting to: " + host + ":" + port);
listener.trace("Connecting to: " + host + ":" + port);
try {
Bootstrap b = new Bootstrap();
@ -85,7 +90,7 @@ public class DiscoveryChannel {
shhHandler.setMsgQueue(messageQueue);
final MessageCodec decoder = ctx.getBean(MessageCodec.class);
final MessageCodec decoder = messageCodecProvider.get();//ctx.getBean(MessageCodec.class);
b.handler(
@ -125,7 +130,6 @@ public class DiscoveryChannel {
workerGroup.shutdownGracefully();
if (!peerDiscoveryMode) {
// EthereumListener listener = WorldManager.getInstance().getListener();
// listener.onPeerDisconnect(host, port);
}

View File

@ -5,8 +5,8 @@ import org.ethereum.net.p2p.Peer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.stereotype.Component;
import java.net.InetAddress;
@ -27,6 +27,10 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;
import javax.inject.Provider;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
@ -45,12 +49,11 @@ public class PeerDiscovery {
private ThreadPoolExecutor executorPool;
private RejectedExecutionHandler rejectionHandler;
@Autowired
private ApplicationContext ctx;
private final AtomicBoolean started = new AtomicBoolean(false);
@Inject
Provider<WorkerThread> workerThreadProvider;
public void start() {
// RejectedExecutionHandler implementation
@ -73,7 +76,7 @@ public class PeerDiscovery {
addPeers(peerDataList);
for (PeerInfo peerData : this.peers) {
WorkerThread workerThread = ctx.getBean(WorkerThread.class);
WorkerThread workerThread = workerThreadProvider.get();//ctx.getBean(WorkerThread.class);
workerThread.init(peerData, executorPool);
executorPool.execute(workerThread);
}
@ -123,7 +126,7 @@ public class PeerDiscovery {
private void startWorker(PeerInfo peerInfo) {
logger.debug("Add new peer for discovery: {}", peerInfo);
WorkerThread workerThread = ctx.getBean(WorkerThread.class);
WorkerThread workerThread = workerThreadProvider.get();//ctx.getBean(WorkerThread.class);
workerThread.init(peerInfo, executorPool);
executorPool.execute(workerThread);
}

View File

@ -1,15 +1,19 @@
package org.ethereum.net.peerdiscovery;
import org.ethereum.net.server.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
import java.util.concurrent.ThreadPoolExecutor;
import javax.inject.Inject;
import javax.inject.Provider;
/**
* @author Roman Mandeleil
* @since 22.05.2014
@ -23,8 +27,8 @@ public class WorkerThread implements Runnable {
private PeerInfo peerInfo;
private ThreadPoolExecutor poolExecutor;
@Autowired
ApplicationContext ctx;
@Inject
Provider<DiscoveryChannel> discoveryChannelProvider;
public WorkerThread() {
}
@ -48,7 +52,8 @@ public class WorkerThread implements Runnable {
try {
DiscoveryChannel discoveryChannel = ctx.getBean(DiscoveryChannel.class);
// TODO: check if this works
DiscoveryChannel discoveryChannel = discoveryChannelProvider.get();//ctx.getBean(DiscoveryChannel.class);
discoveryChannel.connect(peerInfo.getAddress().getHostAddress(), peerInfo.getPort());
peerInfo.setOnline(true);

View File

@ -14,7 +14,7 @@ import org.ethereum.net.shh.ShhHandler;
import org.ethereum.net.wire.MessageCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -22,6 +22,8 @@ import java.io.IOException;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import javax.inject.Inject;
import static org.ethereum.net.message.StaticMessages.HELLO_MESSAGE;
/**
@ -34,22 +36,16 @@ public class Channel {
private final static Logger logger = LoggerFactory.getLogger("net");
@Autowired
ChannelManager channelManager;
@Autowired
MessageQueue msgQueue;
@Autowired
P2pHandler p2pHandler;
@Autowired
EthHandler ethHandler;
@Autowired
ShhHandler shhHandler;
@Autowired
MessageCodec messageCodec;
InetSocketAddress inetSocketAddress;
@ -57,8 +53,15 @@ public class Channel {
private long startupTS;
public Channel() {
@Inject
public Channel(ChannelManager channelManager, MessageQueue msgQueue, P2pHandler p2pHandler
, EthHandler ethHandler, ShhHandler shhHandler, MessageCodec messageCodec) {
this.channelManager = channelManager;
this.msgQueue = msgQueue;
this.p2pHandler = p2pHandler;
this.ethHandler = ethHandler;
this.shhHandler = shhHandler;
this.messageCodec = messageCodec;
}
public void init(String remoteId) {

View File

@ -3,12 +3,13 @@ package org.ethereum.net.server;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import java.util.ArrayList;
@ -20,6 +21,9 @@ import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
//import javax.annotation.PostConstruct;
/**
@ -35,15 +39,15 @@ public class ChannelManager {
List<Channel> channels = Collections.synchronizedList(new ArrayList<Channel>());
Map<ByteArrayWrapper, Block> blockCache = new HashMap<>();
@Autowired
WorldManager worldManager;
@Inject
EthereumListener listener;
public ChannelManager() {
}
// @PostConstruct
@PostConstruct
public void init() {
scheduleChannelCollector();
}
@ -114,7 +118,7 @@ public class ChannelManager {
}
if (channels.size() == 0) {
worldManager.getListener().onNoConnections();
listener.onNoConnections();
}
}
}, 2000, 5000);

View File

@ -6,16 +6,17 @@ import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.facade.Blockchain;
import org.ethereum.manager.WorldManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Provider;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
@ -28,28 +29,20 @@ public class EthereumChannelInitializer extends ChannelInitializer<NioSocketChan
private static final Logger logger = LoggerFactory.getLogger("net");
@Autowired
private ApplicationContext ctx;
@Autowired
Blockchain blockchain;
@Autowired
ChannelManager channelManager;
@Autowired
WorldManager worldManager;
String remoteId;
public EthereumChannelInitializer() {
logger.info("Channel initializer instantiated");
this.remoteId = "e3d09d2f829950b5f3f82d1bddb6f5388bff2f2cca880fa47923df4d8129e8c9b5ba5d4371efcffc416b0cefe20cb55b81b2b71183464713a86e60b423b77947";
}
@Inject
Provider<Channel> channelProvider;
public EthereumChannelInitializer(String remoteId) {
@Inject
public EthereumChannelInitializer(Blockchain blockchain, ChannelManager channelManager, String remoteId) {
logger.info("Channel initializer instantiated");
this.blockchain = blockchain;
this.channelManager = channelManager;
this.remoteId = remoteId;
}
@ -58,7 +51,7 @@ public class EthereumChannelInitializer extends ChannelInitializer<NioSocketChan
logger.info("Open connection, channel: {}", ch.toString());
Channel channel = ctx.getBean(Channel.class);
Channel channel = channelProvider.get();
channel.init(remoteId);
channelManager.addChannel(channel);

View File

@ -1,5 +1,6 @@
package org.ethereum.net.server;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import io.netty.bootstrap.ServerBootstrap;
@ -14,8 +15,9 @@ import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import javax.inject.Inject;
import static org.ethereum.config.SystemProperties.CONFIG;
@ -31,17 +33,18 @@ public class PeerServer {
private static final Logger logger = LoggerFactory.getLogger("net");
@Autowired
public ChannelManager channelManager;
// TODO: this was removed ???
@Autowired
public EthereumChannelInitializer ethereumChannelInitializer;
@Autowired
WorldManager worldManger;
EthereumListener listener;
public PeerServer() {
@Inject
public PeerServer(ChannelManager channelManager, EthereumChannelInitializer ethereumChannelInitializer, EthereumListener listener) {
this.channelManager = channelManager;
this.ethereumChannelInitializer = ethereumChannelInitializer;
this.listener = listener;
}
@ -51,7 +54,7 @@ public class PeerServer {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
worldManger.getListener().trace("Listening on port " + port);
listener.trace("Listening on port " + port);
try {

View File

@ -1,5 +1,6 @@
package org.ethereum.net.shh;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.MessageQueue;
@ -9,7 +10,9 @@ import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.inject.Inject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
@ -29,8 +32,8 @@ public class ShhHandler extends SimpleChannelInboundHandler<ShhMessage> {
private final static Logger logger = LoggerFactory.getLogger("net");
@Autowired
WorldManager worldManager;
@Inject
public EthereumListener listener;
public ShhHandler() {
}
@ -47,7 +50,7 @@ public class ShhHandler extends SimpleChannelInboundHandler<ShhMessage> {
if (ShhMessageCodes.inRange(msg.getCommand().asByte()))
logger.info("ShhHandler invoke: [{}]", msg.getCommand());
worldManager.getListener().trace(String.format("ShhHandler invoke: [%s]", msg.getCommand()));
listener.trace(String.format("ShhHandler invoke: [%s]", msg.getCommand()));
switch (msg.getCommand()) {
case STATUS:
@ -80,7 +83,7 @@ public class ShhHandler extends SimpleChannelInboundHandler<ShhMessage> {
public void activate() {
logger.info("SHH protocol activated");
worldManager.getListener().trace("SHH protocol activated");
listener.trace("SHH protocol activated");
this.active = true;
}

View File

@ -21,13 +21,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.math.ec.ECPoint;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Scope;
//import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import static org.ethereum.net.rlpx.FrameCodec.Frame;
/**
@ -61,8 +63,8 @@ public class MessageCodec extends ByteToMessageCodec<Message> {
}
}
@Autowired
WorldManager worldManager;
@Inject
EthereumListener listener;
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
@ -92,7 +94,6 @@ public class MessageCodec extends ByteToMessageCodec<Message> {
if (loggerNet.isInfoEnabled())
loggerNet.info("From: \t{} \tRecv: \t{}", ctx.channel().remoteAddress(), msg);
EthereumListener listener = worldManager.getListener();
listener.onRecvMessage(msg);
out.add(msg);
@ -102,7 +103,7 @@ public class MessageCodec extends ByteToMessageCodec<Message> {
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
String output = String.format("To: \t%s \tSend: \t%s", ctx.channel().remoteAddress(), msg);
worldManager.getListener().trace(output);
listener.trace(output);
if (loggerNet.isInfoEnabled())
loggerNet.info("To: \t{} \tSend: \t{}", ctx.channel().remoteAddress(), msg);

View File

@ -25,7 +25,7 @@ import static org.ethereum.config.SystemProperties.CONFIG;
import static org.ethereum.util.BIUtil.*;
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
//import static org.springframework.util.StringUtils.isEmpty;
import static org.springframework.util.StringUtils.hasLength;
//import static org.springframework.util.StringUtils.hasLength;
/**
* @author Roman Mandeleil
@ -790,7 +790,7 @@ public class Program {
public void saveProgramTraceToFile(String fileName) {
if (!CONFIG.vmTrace() || !hasLength(CONFIG.vmTraceDir())) return;
if (!CONFIG.vmTrace() || CONFIG.vmTraceDir().isEmpty()) return;
String dir = CONFIG.databaseDir() + "/" + CONFIG.vmTraceDir() + "/";

View File

@ -10,9 +10,11 @@ import org.ethereum.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import java.math.BigInteger;
@ -26,8 +28,9 @@ public class ProgramInvokeFactoryImpl implements ProgramInvokeFactory {
private static final Logger logger = LoggerFactory.getLogger("VM");
@Autowired
private Blockchain blockchain;
//@Autowired
@Inject
public Blockchain blockchain;
// Invocation by the wire tx

View File

@ -7,9 +7,9 @@ peer.discovery.ip.list = poc-7.ethdev.com:30303,\
# Peer Server Zero (poc-7.ethdev.com)
peer.active.ip = 162.243.46.9
peer.active.port = 30403
peer.active.nodeid = e3d09d2f829950b5f3f82d1bddb6f5388bff2f2cca880fa47923df4d8129e8c9b5ba5d4371efcffc416b0cefe20cb55b81b2b71183464713a86e60b423b77947
peer.active.ip = 139.162.13.89
peer.active.port = 30303
peer.active.nodeid = bf01b54b6bc7faa203286dfb8359ce11d7b1fe822968fb4991f508d6f5a36ab7d9ae8af9b0d61c0467fb08567e0fb71cfb9925a370b69f9ede97927db473d1f5
# Peer for server to listen for incoming

Binary file not shown.