From bee842776106bc39b1839cd2dc8866e6b9ee9733 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Wed, 27 Jul 2022 13:51:36 -0700 Subject: [PATCH] WalletAppKit: simplify blocking startup * Use `startAsync()` for the blocking case, too. Since we were already waiting on `downloadListner.await()` * Make sure `installShutdownHook()` is called in both cases (bug fix) * Add more comments --- .../java/org/bitcoinj/kits/WalletAppKit.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java index 40c829149..43147b96f 100644 --- a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java +++ b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java @@ -378,23 +378,24 @@ public class WalletAppKit extends AbstractIdleService { } vChain.addWallet(vWallet); vPeerGroup.addWallet(vWallet); + + // vChain, vWallet, and vPeerGroup all initialized; allow subclass (if any) a chance to adjust configuration onSetupCompleted(); - if (blockingStartup) { - vPeerGroup.start(); - // Make sure we shut down cleanly. - installShutdownHook(); + // Start the PeerGroup (asynchronously) and start downloading the blockchain (asynchronously) + vPeerGroup.startAsync().whenComplete((result, t) -> { + if (t == null) { + vPeerGroup.startBlockChainDownload(downloadListener); + } else { + throw new RuntimeException(t); + } + }); - vPeerGroup.startBlockChainDownload(downloadListener); - downloadListener.await(); - } else { - vPeerGroup.startAsync().whenComplete((result, t) -> { - if (t == null) { - vPeerGroup.startBlockChainDownload(downloadListener); - } else { - throw new RuntimeException(t); - } - }); + // Make sure we shut down cleanly. + installShutdownHook(); + + if (blockingStartup) { + downloadListener.await(); // Wait for the blockchain to download } }