From 8188c5092c8dda910ee49d5ae1256a1135025e8f Mon Sep 17 00:00:00 2001 From: henry Date: Thu, 8 Mar 2018 00:49:21 -0400 Subject: [PATCH] Change all url using the new Crypto Coin Network Manager, also put the initial urls for bitshares --- .../apigenerator/GrapheneApiGenerator.java | 30 ++++++------ .../application/CrystalApplication.java | 21 +++++++++ .../manager/BitsharesAccountManager.java | 5 +- .../network/BitsharesCryptoNetVerifier.java | 14 ++++-- .../network/CryptoNetManager.java | 46 +++++++++++++++++-- .../network/CryptoNetVerifier.java | 10 ++-- .../crystalwallet/network/GetChainId.java | 2 +- 7 files changed, 100 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index 8272895..1e58d06 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -12,12 +12,14 @@ import cy.agorise.crystalwallet.dao.BitsharesAssetDao; import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao; import cy.agorise.crystalwallet.dao.CryptoCurrencyDao; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.CryptoNet; import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAssetInfo; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; +import cy.agorise.crystalwallet.network.CryptoNetManager; import cy.agorise.crystalwallet.network.WebSocketThread; import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Asset; @@ -58,12 +60,8 @@ public abstract class GrapheneApiGenerator { //TODO network connections //TODO make to work with all Graphene type, not only bitshares - public static String url = "http://185.208.208.147:11012"; public static String faucetUrl = "http://185.208.208.147:5010"; - private static String equivalentUrl = "http://185.208.208.147:8090"; - //public static String url = "wss://bitshares.openledger.info/ws"; - //private static Str ing equivalentUrl = "wss://bitshares.openledger.info/ws"; - + private static String equivalentUrl = "wss://bitshares.openledger.info/ws"; // The message broker for bitshares private static SubscriptionMessagesHub bitsharesSubscriptionHub = new SubscriptionMessagesHub("", "", true, new NodeErrorListener() { @@ -77,7 +75,7 @@ public abstract class GrapheneApiGenerator { /** * The subscription thread for the real time updates */ - private static WebSocketThread subscriptionThread = new WebSocketThread(bitsharesSubscriptionHub,url); + private static WebSocketThread subscriptionThread = new WebSocketThread(bitsharesSubscriptionHub, CryptoNetManager.getURL(CryptoNet.BITSHARES)); /** * This is used for manager each listener in the subscription thread */ @@ -110,7 +108,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -138,7 +136,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -165,7 +163,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -192,7 +190,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -219,7 +217,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -243,7 +241,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -288,7 +286,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -336,7 +334,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } @@ -509,7 +507,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); @@ -536,7 +534,7 @@ public abstract class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - }),url); + }),CryptoNetManager.getURL(CryptoNet.BITSHARES)); thread.start(); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index fc5f6c7..8b55ef4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -6,6 +6,8 @@ import android.content.Intent; import com.idescout.sql.SqlScoutServer; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.crystalwallet.network.CryptoNetManager; import cy.agorise.crystalwallet.service.CrystalWalletService; /** @@ -15,6 +17,20 @@ import cy.agorise.crystalwallet.service.CrystalWalletService; */ public class CrystalApplication extends Application { + public static String BITSHARES_URL[] = + { + "wss://de.palmpay.io/ws", // Custom node + "wss://bitshares.nu/ws", + "wss://dexnode.net/ws", // Dallas, USA + "wss://bitshares.crypto.fans/ws", // Munich, Germany + "wss://bitshares.openledger.info/ws", // Openledger node + "ws://185.208.208.147:8090" // Custom node + }; + + public static String BITSHARES_TESTNET_URL[] = + { + "http://185.208.208.147:11012", // Openledger node + }; @Override public void onCreate() { @@ -24,6 +40,11 @@ public class CrystalApplication extends Application { CrystalDatabase db = CrystalDatabase.getAppDatabase(this.getApplicationContext()); SqlScoutServer.create(this, getPackageName()); + //Using Bitshares Agorise Testnet + CryptoNetManager.addCryptoNetURL(CryptoNet.BITSHARES,BITSHARES_TESTNET_URL); + //Next line is for use the bitshares main net + //CryptoNetManager.addCryptoNetURL(CryptoNet.BITSHARES,BITSHARES_URL); + Intent intent = new Intent(getApplicationContext(), CrystalWalletService.class); startService(intent); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index da9b514..5b1a248 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -36,6 +36,7 @@ import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.GrapheneAccount; import cy.agorise.crystalwallet.models.GrapheneAccountInfo; +import cy.agorise.crystalwallet.network.CryptoNetManager; import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.AssetAmount; @@ -56,7 +57,7 @@ import cy.agorise.graphenej.operations.TransferOperationBuilder; */ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener { - private final static String BITSHARES_TESTNET_CHAIN_ID= "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8"; + //private final static String BITSHARES_TESTNET_CHAIN_ID= "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8"; private final static String SIMPLE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; private final static String DEFAULT_TIME_ZONE = "GMT"; @@ -395,7 +396,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); Transaction transaction = new Transaction(privateKey, null, operationList); - transaction.setChainId(BITSHARES_TESTNET_CHAIN_ID); + transaction.setChainId(CryptoNetManager.getChaindId(CryptoNet.BITSHARES)); ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java index c1839b8..8a47bce 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java +++ b/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java @@ -12,6 +12,7 @@ import cy.agorise.graphenej.models.WitnessResponse; public class BitsharesCryptoNetVerifier extends CryptoNetVerifier { + /** * TODO We need to change this to a type of subCryptoNet */ @@ -20,7 +21,7 @@ public class BitsharesCryptoNetVerifier extends CryptoNetVerifier { * Todo info need to be on the SubCryptoNet */ private final String CHAIN_ID = "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8";//testnet - + //private final String CHAIN_ID = "4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8";//mainnet @Override public void checkURL(final String url) { @@ -32,15 +33,22 @@ public class BitsharesCryptoNetVerifier extends CryptoNetVerifier { if(response.result.equals(CHAIN_ID)) { CryptoNetManager.verifiedCryptoNetURL(cryptoNet, url, System.currentTimeMillis() - startTime); }else{ - System.out.println("Error we are not in the net current chain id " + response.result + " excepted " + CHAIN_ID); + System.out.println(" BitsharesCryptoNetVerifier Error we are not in the net current chain id " + response.result + " excepted " + CHAIN_ID); + //TODO handle error bad chain } } } @Override public void onError(BaseResponse.Error error) { - + //TODO handle error } }),url); + thread.start(); + } + + @Override + public String getChainId() { + return CHAIN_ID; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java index f8bed24..c112f9f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java @@ -1,6 +1,7 @@ package cy.agorise.crystalwallet.network; import android.support.annotation.NonNull; +import android.util.Log; import java.util.ArrayList; import java.util.Collections; @@ -25,13 +26,24 @@ public abstract class CryptoNetManager { */ private static HashMap> TestedURLs = new HashMap<>(); + public static String getURL(CryptoNet crypto){ + return CryptoNetManager.getURL(crypto,0); + } + + public static String getURL(CryptoNet crypto, int index){ - if(TestedURLs.containsKey(crypto) && TestedURLs.get(crypto).size()index){ + StringBuilder debugString = new StringBuilder("CryptoNetManager urls times: "); + for(TestedURL url : TestedURLs.get(crypto)){ + debugString.append("\r\n ").append(url.getTime()).append(" ").append(url.getUrl()); + } + System.out.println(debugString.toString()); + return TestedURLs.get(crypto).get(index).getUrl(); } if(CryptoNetURLs.containsKey(crypto) && !CryptoNetURLs.get(crypto).isEmpty()){ - + return CryptoNetURLs.get(crypto).iterator().next(); } return null; } @@ -49,6 +61,24 @@ public abstract class CryptoNetManager { } CryptoNetURLs.get(crypto).add(url); + CryptoNetVerifier verifier = CryptoNetVerifier.getNetworkVerify(crypto); + if(verifier != null) { + verifier.checkURL(url); + } + } + + public static void addCryptoNetURL(CryptoNet crypto, String[] urls){ + if(!CryptoNetURLs.containsKey(crypto)){ + CryptoNetURLs.put(crypto,new HashSet()); + } + CryptoNetVerifier verifier = CryptoNetVerifier.getNetworkVerify(crypto); + + for(String url : urls) { + CryptoNetURLs.get(crypto).add(url); + if(verifier != null) { + verifier.checkURL(url); + } + } } public static void removeCryptoNetURL(CryptoNet crypto, String url){ @@ -65,11 +95,21 @@ public abstract class CryptoNetManager { TestedURL testedUrl = new TestedURL(time,url); if(!TestedURLs.get(crypto).contains(testedUrl)){ TestedURLs.get(crypto).add(testedUrl); - Collections.reverse(TestedURLs.get(crypto)); + Collections.sort(TestedURLs.get(crypto)); } + }else{ + //TODO add error handler } } + public static String getChaindId(CryptoNet crypto){ + CryptoNetVerifier verifier = CryptoNetVerifier.getNetworkVerify(crypto); + if(verifier != null) { + return verifier.getChainId(); + } + return null; + } + private static class TestedURL implements Comparable{ private long time; private String url; diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java index 5d507a8..7364b79 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java +++ b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java @@ -12,10 +12,14 @@ import cy.agorise.crystalwallet.enums.CryptoNet; public abstract class CryptoNetVerifier { - public static CryptoNetVerifier getNetworkVerify(CryptoNet cryptoNet){ - + static CryptoNetVerifier getNetworkVerify(CryptoNet cryptoNet){ + if(cryptoNet.getLabel().equals(CryptoNet.BITSHARES.getLabel())){ + return new BitsharesCryptoNetVerifier(); + } return null; } - public abstract void checkURL(String url); + public abstract void checkURL(final String url); + + public abstract String getChainId(); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java b/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java index c52b1cb..2863514 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java +++ b/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java @@ -32,7 +32,7 @@ public class GetChainId extends BaseGrapheneHandler { @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { - ApiCall getAccountByName = new ApiCall(0, "Get_Chain_Id", new ArrayList(), RPC.VERSION, 1); + ApiCall getAccountByName = new ApiCall(0, "get_chain_id", new ArrayList(), RPC.VERSION, 1); websocket.sendText(getAccountByName.toJsonString()); }