Added ethereum restart with different wallet using init message.

This commit is contained in:
Adrian Tiberius 2015-08-04 22:23:13 +02:00
parent b585afb473
commit 6340cc47a4
5 changed files with 54 additions and 15 deletions

View File

@ -120,9 +120,9 @@ public class Ethereum implements org.ethereum.facade.Ethereum {
public byte[] createRandomAccount() { public byte[] createRandomAccount() {
byte[] randomAddress = HashUtil.sha3(HashUtil.randomPeerId()); byte[] randomPrivateKey = HashUtil.sha3(HashUtil.randomPeerId());
wallet.importKey(randomAddress); wallet.importKey(randomPrivateKey);
return randomAddress; return randomPrivateKey;
} }
public void loadBlockchain() { public void loadBlockchain() {

View File

@ -11,7 +11,9 @@ import org.ethereum.net.peerdiscovery.PeerInfo;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List;
public class EthereumConnector extends ServiceConnector { public class EthereumConnector extends ServiceConnector {
@ -23,6 +25,22 @@ public class EthereumConnector extends ServiceConnector {
super(context, serviceClass); super(context, serviceClass);
} }
public void init(List<String> privateKeys) {
if (!isBound)
return;
Message msg = Message.obtain(null, EthereumServiceMessage.MSG_INIT, 0, 0);
Bundle data = new Bundle();
data.putStringArrayList("privateKeys", (ArrayList)privateKeys);
msg.setData(data);
try {
serviceMessenger.send(msg);
} catch (RemoteException e) {
logger.error("Exception sending message(init) to service: " + e.getMessage());
}
}
/** /**
* Connect ethereum to peer * Connect ethereum to peer
* @param ip String Peer ip address * @param ip String Peer ip address

View File

@ -14,6 +14,7 @@ import org.ethereum.android.manager.BlockLoader;
import org.ethereum.android.service.events.EventData; import org.ethereum.android.service.events.EventData;
import org.ethereum.android.service.events.EventFlag; import org.ethereum.android.service.events.EventFlag;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.crypto.HashUtil;
import org.ethereum.facade.Ethereum; import org.ethereum.facade.Ethereum;
import org.ethereum.manager.AdminInfo; import org.ethereum.manager.AdminInfo;
import org.ethereum.net.peerdiscovery.PeerInfo; import org.ethereum.net.peerdiscovery.PeerInfo;
@ -40,6 +41,7 @@ public class EthereumRemoteService extends EthereumService {
static HashMap<String, Messenger> clientListeners = new HashMap<>(); static HashMap<String, Messenger> clientListeners = new HashMap<>();
static EnumMap<EventFlag, List<String>> listenersByType = new EnumMap<EventFlag, List<String>>(EventFlag.class); static EnumMap<EventFlag, List<String>> listenersByType = new EnumMap<EventFlag, List<String>>(EventFlag.class);
public boolean isEthereumStarted = false;
public EthereumRemoteService() { public EthereumRemoteService() {
@ -136,6 +138,10 @@ public class EthereumRemoteService extends EthereumService {
switch (message.what) { switch (message.what) {
case EthereumServiceMessage.MSG_INIT:
init(message);
break;
case EthereumServiceMessage.MSG_CONNECT: case EthereumServiceMessage.MSG_CONNECT:
connect(message); connect(message);
break; break;
@ -203,6 +209,21 @@ public class EthereumRemoteService extends EthereumService {
return true; return true;
} }
protected void init(Message message) {
if (isEthereumStarted) {
closeEthereum(null);
ethereum = null;
component = null;
isInitialized = false;
initializeEthereum();
}
Bundle data = message.getData();
List<String> privateKeys = data.getStringArrayList("privateKeys");
ethereum.init(privateKeys);
isEthereumStarted = true;
}
/** /**
* Connect to peer * Connect to peer
* *
@ -499,6 +520,7 @@ public class EthereumRemoteService extends EthereumService {
protected void closeEthereum(Message message) { protected void closeEthereum(Message message) {
ethereum.close(); ethereum.close();
isEthereumStarted = false;
logger.info("Closed ethereum."); logger.info("Closed ethereum.");
} }

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
import org.ethereum.android.di.components.DaggerEthereumComponent; import org.ethereum.android.di.components.DaggerEthereumComponent;
import org.ethereum.android.di.components.EthereumComponent;
import org.ethereum.android.di.modules.EthereumModule; import org.ethereum.android.di.modules.EthereumModule;
import org.ethereum.android.jsonrpc.JsonRpcServer; import org.ethereum.android.jsonrpc.JsonRpcServer;
import org.ethereum.android.service.events.BlockEventData; import org.ethereum.android.service.events.BlockEventData;
@ -21,7 +22,6 @@ import org.ethereum.core.TransactionReceipt;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.ethereum.android.Ethereum; import org.ethereum.android.Ethereum;
import org.ethereum.net.p2p.HelloMessage; import org.ethereum.net.p2p.HelloMessage;
import org.spongycastle.util.encoders.Hex;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -36,6 +36,7 @@ public class EthereumService extends Service {
static boolean isInitialized = false; static boolean isInitialized = false;
static Ethereum ethereum = null; static Ethereum ethereum = null;
static EthereumComponent component = null;
static JsonRpcServer jsonRpcServer; static JsonRpcServer jsonRpcServer;
static Thread jsonRpcServerThread; static Thread jsonRpcServerThread;
@ -61,6 +62,7 @@ public class EthereumService extends Service {
jsonRpcServerThread.interrupt(); jsonRpcServerThread.interrupt();
jsonRpcServerThread = null; jsonRpcServerThread = null;
} }
ethereum.close();
} }
protected void initializeEthereum() { protected void initializeEthereum() {
@ -73,20 +75,12 @@ public class EthereumService extends Service {
System.out.println("Database folder: " + databaseFolder); System.out.println("Database folder: " + databaseFolder);
SystemProperties.CONFIG.setDataBaseDir(databaseFolder); SystemProperties.CONFIG.setDataBaseDir(databaseFolder);
ethereum = (Ethereum)DaggerEthereumComponent.builder() component = DaggerEthereumComponent.builder()
.ethereumModule(new EthereumModule(this)) .ethereumModule(new EthereumModule(this))
.build().ethereum(); .build();
ethereum = (Ethereum)component.ethereum();
ethereum.addListener(new EthereumListener()); ethereum.addListener(new EthereumListener());
// TODO: Add init and add/remove address service messages
List<String> addresses = new ArrayList<String>();
byte[] cowAddr = HashUtil.sha3("cow".getBytes());
addresses.add(Hex.toHexString(cowAddr));
String secret = CONFIG.coinbaseSecret();
byte[] cbAddr = HashUtil.sha3(secret.getBytes());
addresses.add(Hex.toHexString(cbAddr));
ethereum.init(addresses);
isInitialized = true; isInitialized = true;
} else { } else {
System.out.println(" Already initialized"); System.out.println(" Already initialized");

View File

@ -77,4 +77,9 @@ public class EthereumServiceMessage {
* Command to the service to get the pernding transactions * Command to the service to get the pernding transactions
*/ */
public static final int MSG_GET_PENDING_TRANSACTIONS = 15; public static final int MSG_GET_PENDING_TRANSACTIONS = 15;
/**
* Command to the service to initialize with specific addresses. If already initialized, restart the service
*/
public static final int MSG_INIT = 16;
} }