Skip to content

Commit 777483a

Browse files
caiofaustinocaiof-adyen
authored andcommitted
Catch BitcoinRPCException on broadcastNetwork but ignore error code 0.
1 parent cc629a8 commit 777483a

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

app/src/main/java/com/greenaddress/abcore/RPCIntentService.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ private String getRpcUrl() throws IOException {
5858
String password = p.getProperty("rpcpassword");
5959
final String testnet = p.getProperty("testnet");
6060
final String nonMainnet = testnet == null || !testnet.equals("1") ? p.getProperty("regtest") : testnet;
61+
6162
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
6263
final String useDistribution = prefs.getString("usedistribution", "core");
64+
6365
if (user == null || password == null) {
6466
final String cookie = String.format("%s/%s", p.getProperty("datadir"), ".cookie");
6567
final String cookieTestnet = String.format("%s/%s", p.getProperty("datadir"), "testnet3/.cookie");
@@ -68,12 +70,12 @@ private String getRpcUrl() throws IOException {
6870
final String daemon = "liquid".equals(useDistribution) ? cookieLiquid : cookie;
6971

7072
final String fCookie = nonMainnet == null || !nonMainnet.equals("1") ? daemon : cookieTestnet;
71-
final File file = new File(fCookie);
73+
final File cookieFile = new File(fCookie);
7274

7375
final StringBuilder text = new StringBuilder();
7476

7577
try {
76-
final BufferedReader br = new BufferedReader(new FileReader(file));
78+
final BufferedReader br = new BufferedReader(new FileReader(cookieFile));
7779
String line;
7880

7981
while ((line = br.readLine()) != null) {
@@ -119,15 +121,16 @@ private void broadcastPeerlist() throws IOException {
119121

120122
}
121123

122-
private void broadcastNetwork() throws IOException {
124+
private void broadcastNetwork() throws IOException, BitcoinRPCException {
123125
final BitcoindRpcClient bitcoin = getRpc();
124126
final Intent broadcastIntent = new Intent();
125127
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
126128
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
127129
broadcastIntent.putExtra(PARAM_OUT_MSG, "localonion");
130+
128131
final BitcoindRpcClient.NetworkInfo info = bitcoin.getNetworkInfo();
129-
for (final Object addrs : info.localAddresses()) {
130-
final Map data = (Map) addrs;
132+
for (final Object address : info.localAddresses()) {
133+
final Map data = (Map) address;
131134
final String host = (String) data.get("address");
132135
if (host != null && host.endsWith(".onion")) {
133136
final Long port = (Long) data.get("port");
@@ -139,14 +142,15 @@ private void broadcastNetwork() throws IOException {
139142
break;
140143
}
141144
}
145+
142146
final BitcoindRpcClient.BlockChainInfo blockChainInfo = bitcoin.getBlockChainInfo();
143147
broadcastIntent.putExtra("sync", blockChainInfo.verificationProgress().multiply(BigDecimal.valueOf(100)).intValue());
144148
broadcastIntent.putExtra("blocks", blockChainInfo.blocks());
145149
sendBroadcast(broadcastIntent);
146150
}
147151

148152
private void broadcastError(final Exception e) {
149-
Log.e(TAG, e.getClass().getName());
153+
Log.e(TAG, "broadcastError - " + e.getClass().getName());
150154
final Intent broadcastIntent = new Intent();
151155
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
152156
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
@@ -250,17 +254,26 @@ protected void onHandleIntent(final Intent intent) {
250254
sendBroadcast(broadcastIntent);
251255

252256
} catch (final BitcoinRPCException | IOException i) {
253-
Log.i(TAG, "EXE", i);
254-
255-
if (i instanceof BitcoinRPCException && (((BitcoinRPCException) i).getResponseCode() == 500)) {
256-
final Intent broadcastIntent = new Intent();
257-
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
258-
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
259-
broadcastIntent.putExtra(PARAM_OUT_MSG, "OK");
260-
sendBroadcast(broadcastIntent);
261-
return;
257+
if (i instanceof BitcoinRPCException) {
258+
final BitcoinRPCException bitcoinRPCException = (BitcoinRPCException) i;
259+
if (bitcoinRPCException.getResponseCode() == 500) {
260+
Log.d(TAG, "BitcoinRPCException - Internal Server Error");
261+
final Intent broadcastIntent = new Intent();
262+
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
263+
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
264+
broadcastIntent.putExtra(PARAM_OUT_MSG, "OK");
265+
sendBroadcast(broadcastIntent);
266+
return;
267+
} else if (bitcoinRPCException.getResponseCode() == 0){
268+
// This happens if Tor is yet initialized.
269+
Log.d(TAG, "Empty BitcoinRPCException");
270+
return;
271+
} else {
272+
Log.e(TAG, "BitcoinRPCException", i);
273+
}
274+
} else {
275+
Log.e(TAG, "IOException", i);
262276
}
263-
264277
broadcastError(i);
265278
}
266279
}

0 commit comments

Comments
 (0)