Skip to content

Commit 5afd2c9

Browse files
committed
Catch BitcoinRPCException on broadcastNetwork.
1 parent cc629a8 commit 5afd2c9

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void run() {
6767
};
6868

6969
private void refresh() {
70-
if (mSwitchOn){
70+
if (mSwitchOn) {
7171
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING || mDaemonStatus == DaemonStatus.UNKNOWN) {
7272
//refresh
7373
final Intent i = new Intent(this, RPCIntentService.class);
@@ -81,7 +81,7 @@ private void refresh() {
8181
// consistent state
8282
stopDaemonAndSetStatus();
8383
}
84-
} else{
84+
} else {
8585
// switch OFF
8686
if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN) {
8787
//refresh
@@ -260,17 +260,15 @@ else if (mDaemonStatus == DaemonStatus.STOPPED ){
260260
// the right status will get reflected
261261
break;
262262
case "exception":
263-
if (intent.hasExtra("exception")) {
264-
final String exe = intent.getStringExtra("exception");
265-
if (exe != null)
266-
Log.i(TAG, exe);
263+
final String exe = intent.getStringExtra("exception");
264+
if (exe != null) {
265+
Log.i(TAG, exe);
267266
}
268267

269-
if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN){
268+
if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN) {
270269
mDaemonStatus = DaemonStatus.STOPPED;
271270
mTvStatus.setText(getString(R.string.status_header, mDaemonStatus.toString()));
272-
}
273-
else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING){
271+
} else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING) {
274272
// if we get here it means that the daemon is *actually not* running but the screen is reflecting
275273
// as if its running or we are trying to start it. This is a bad state and we will simply
276274
// try to stop the daemon and get back to a consistent state
@@ -279,11 +277,11 @@ else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus
279277
//for mDaemonStatus = STOPPED we don't have to do anything
280278
break;
281279
case "localonion":
282-
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.UNKNOWN){
280+
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.UNKNOWN) {
283281
mDaemonStatus = DaemonStatus.RUNNING;
284282
mTvStatus.setText(getString(R.string.status_header, mDaemonStatus.toString()));
285283
}
286-
else if (mDaemonStatus == DaemonStatus.STOPPED ){
284+
else if (mDaemonStatus == DaemonStatus.STOPPED) {
287285
// if we get here it means that the daemon is *actually* running but the screen is reflecting
288286
// as OFF. This is a bad state and we will simply try to stop the daemon and get
289287
// back to a consistent state

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

Lines changed: 29 additions & 19 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) {
@@ -100,7 +102,7 @@ private BitcoindRpcClient getRpc() throws IOException {
100102
return new BitcoinJSONRPCClient(getRpcUrl());
101103
}
102104

103-
private void broadcastPeerlist() throws IOException {
105+
private void broadcastPeerList() throws IOException {
104106
final BitcoindRpcClient bitcoin = getRpc();
105107

106108
final Intent broadcastIntent = new Intent();
@@ -119,15 +121,17 @@ private void broadcastPeerlist() throws IOException {
119121

120122
}
121123

122-
private void broadcastNetwork() throws IOException {
124+
private void broadcastNetwork() throws IOException, BitcoinRPCException {
125+
Log.d(TAG, "broadcastNetwork");
123126
final BitcoindRpcClient bitcoin = getRpc();
124127
final Intent broadcastIntent = new Intent();
125128
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
126129
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
127130
broadcastIntent.putExtra(PARAM_OUT_MSG, "localonion");
131+
128132
final BitcoindRpcClient.NetworkInfo info = bitcoin.getNetworkInfo();
129-
for (final Object addrs : info.localAddresses()) {
130-
final Map data = (Map) addrs;
133+
for (final Object address : info.localAddresses()) {
134+
final Map data = (Map) address;
131135
final String host = (String) data.get("address");
132136
if (host != null && host.endsWith(".onion")) {
133137
final Long port = (Long) data.get("port");
@@ -139,14 +143,15 @@ private void broadcastNetwork() throws IOException {
139143
break;
140144
}
141145
}
146+
142147
final BitcoindRpcClient.BlockChainInfo blockChainInfo = bitcoin.getBlockChainInfo();
143148
broadcastIntent.putExtra("sync", blockChainInfo.verificationProgress().multiply(BigDecimal.valueOf(100)).intValue());
144149
broadcastIntent.putExtra("blocks", blockChainInfo.blocks());
145150
sendBroadcast(broadcastIntent);
146151
}
147152

148153
private void broadcastError(final Exception e) {
149-
Log.e(TAG, e.getClass().getName());
154+
Log.e(TAG, "broadcastError - " + e.getClass().getName());
150155
final Intent broadcastIntent = new Intent();
151156
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
152157
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
@@ -169,7 +174,7 @@ protected void onHandleIntent(final Intent intent) {
169174
conn.authenticate(IOUtils.toByteArray(new FileInputStream(cookie_path)));
170175
conn.shutdownTor("HALT");
171176
} catch (final IOException e) {
172-
e.printStackTrace();
177+
Log.w(TAG, "Error stopping TOR", e);
173178
}
174179

175180
while (true) {
@@ -231,7 +236,7 @@ protected void onHandleIntent(final Intent intent) {
231236

232237
if (request != null)
233238
if (request.equals("peerlist")) {
234-
broadcastPeerlist();
239+
broadcastPeerList();
235240
return;
236241
} else if (request.equals("localonion")) {
237242
broadcastNetwork();
@@ -250,17 +255,22 @@ protected void onHandleIntent(final Intent intent) {
250255
sendBroadcast(broadcastIntent);
251256

252257
} 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;
258+
if (i instanceof BitcoinRPCException) {
259+
final BitcoinRPCException bitcoinRPCException = (BitcoinRPCException) i;
260+
if (bitcoinRPCException.getResponseCode() == 500) {
261+
Log.d(TAG, "BitcoinRPCException - Internal Server Error");
262+
final Intent broadcastIntent = new Intent();
263+
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
264+
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
265+
broadcastIntent.putExtra(PARAM_OUT_MSG, "OK");
266+
sendBroadcast(broadcastIntent);
267+
return;
268+
} else {
269+
Log.e(TAG, "BitcoinRPCException", i);
270+
}
271+
} else {
272+
Log.e(TAG, "IOException", i);
262273
}
263-
264274
broadcastError(i);
265275
}
266276
}

0 commit comments

Comments
 (0)