Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void startUp() throws Exception {
- // Runs in a separate thread.
- if (!directory.exists()) {
- if (!directory.mkdir()) {
- throw new IOException("Could not create named directory.");
- }
- }
- FileInputStream walletStream = null;
- try {
- File chainFile = new File(directory, filePrefix + ".spvchain");
- boolean chainFileExists = chainFile.exists();
- vWalletFile = new File(directory, filePrefix + ".wallet");
- boolean shouldReplayWallet = vWalletFile.exists() && !chainFileExists;
- vStore = new SPVBlockStore(params, chainFile);
- if (!chainFileExists && checkpoints != null) {
- // Ugly hack! We have to create the wallet once here to learn the earliest key time, and then throw it
- // away. The reason is that wallet extensions might need access to peergroups/chains/etc so we have to
- // create the wallet later, but we need to know the time early here before we create the BlockChain
- // object.
- long time = Long.MAX_VALUE;
- if (vWalletFile.exists()) {
- Wallet wallet = new Wallet(params);
- FileInputStream stream = new FileInputStream(vWalletFile);
- new WalletProtobufSerializer().readWallet(WalletProtobufSerializer.parseToProto(stream), wallet);
- time = wallet.getEarliestKeyCreationTime();
- }
- CheckpointManager.checkpoint(params, checkpoints, vStore, time);
- }
- vChain = new BlockChain(params, vStore);
- vPeerGroup = new PeerGroup(params, vChain);
- if (this.userAgent != null)
- vPeerGroup.setUserAgent(userAgent, version);
- if (vWalletFile.exists()) {
- walletStream = new FileInputStream(vWalletFile);
- vWallet = new Wallet(params);
- addWalletExtensions(); // All extensions must be present before we deserialize
- new WalletProtobufSerializer().readWallet(WalletProtobufSerializer.parseToProto(walletStream), vWallet);
- if (shouldReplayWallet)
- vWallet.clearTransactions(0);
- } else {
- vWallet = new Wallet(params);
- vWallet.addKey(new ECKey());
- addWalletExtensions();
- }
- if (useAutoSave) vWallet.autosaveToFile(vWalletFile, 1, TimeUnit.SECONDS, null);
- // Set up peer addresses or discovery first, so if wallet extensions try to broadcast a transaction
- // before we're actually connected the broadcast waits for an appropriate number of connections.
- if (peerAddresses != null) {
- for (PeerAddress addr : peerAddresses) vPeerGroup.addAddress(addr);
- peerAddresses = null;
- } else {
- vPeerGroup.addPeerDiscovery(new DnsDiscovery(params));
- }
- vChain.addWallet(vWallet);
- vPeerGroup.addWallet(vWallet);
- onSetupCompleted();
- if (blockingStartup) {
- vPeerGroup.startAndWait();
- // Make sure we shut down cleanly.
- installShutdownHook();
- // TODO: Be able to use the provided download listener when doing a blocking startup.
- final DownloadListener listener = new DownloadListener();
- vPeerGroup.startBlockChainDownload(listener);
- listener.await();
- } else {
- Futures.addCallback(vPeerGroup.start(), new FutureCallback<State>() {
- @Override
- public void onSuccess(State result) {
- final PeerEventListener l = downloadListener == null ? new DownloadListener() : downloadListener;
- vPeerGroup.startBlockChainDownload(l);
- }
- @Override
- public void onFailure(Throwable t) {
- throw new RuntimeException(t);
- }
- });
- }
- } catch (BlockStoreException e) {
- throw new IOException(e);
- } finally {
- if (walletStream != null) walletStream.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement