Advertisement
shmoula

Untitled

Jan 7th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. @Override
  2.     protected void startUp() throws Exception {
  3.         // Runs in a separate thread.
  4.         if (!directory.exists()) {
  5.             if (!directory.mkdir()) {
  6.                 throw new IOException("Could not create named directory.");
  7.             }
  8.         }
  9.         FileInputStream walletStream = null;
  10.         try {
  11.             File chainFile = new File(directory, filePrefix + ".spvchain");
  12.             boolean chainFileExists = chainFile.exists();
  13.             vWalletFile = new File(directory, filePrefix + ".wallet");
  14.             boolean shouldReplayWallet = vWalletFile.exists() && !chainFileExists;
  15.  
  16.             vStore = new SPVBlockStore(params, chainFile);
  17.             if (!chainFileExists && checkpoints != null) {
  18.                 // Ugly hack! We have to create the wallet once here to learn the earliest key time, and then throw it
  19.                 // away. The reason is that wallet extensions might need access to peergroups/chains/etc so we have to
  20.                 // create the wallet later, but we need to know the time early here before we create the BlockChain
  21.                 // object.
  22.                 long time = Long.MAX_VALUE;
  23.                 if (vWalletFile.exists()) {
  24.                     Wallet wallet = new Wallet(params);
  25.                     FileInputStream stream = new FileInputStream(vWalletFile);
  26.                     new WalletProtobufSerializer().readWallet(WalletProtobufSerializer.parseToProto(stream), wallet);
  27.                     time = wallet.getEarliestKeyCreationTime();
  28.                 }
  29.                 CheckpointManager.checkpoint(params, checkpoints, vStore, time);
  30.             }
  31.             vChain = new BlockChain(params, vStore);
  32.             vPeerGroup = new PeerGroup(params, vChain);
  33.             if (this.userAgent != null)
  34.                 vPeerGroup.setUserAgent(userAgent, version);
  35.             if (vWalletFile.exists()) {
  36.                 walletStream = new FileInputStream(vWalletFile);
  37.                 vWallet = new Wallet(params);
  38.                 addWalletExtensions(); // All extensions must be present before we deserialize
  39.                 new WalletProtobufSerializer().readWallet(WalletProtobufSerializer.parseToProto(walletStream), vWallet);
  40.                 if (shouldReplayWallet)
  41.                     vWallet.clearTransactions(0);
  42.             } else {
  43.                 vWallet = new Wallet(params);
  44.                 vWallet.addKey(new ECKey());
  45.                 addWalletExtensions();
  46.             }
  47.             if (useAutoSave) vWallet.autosaveToFile(vWalletFile, 1, TimeUnit.SECONDS, null);
  48.             // Set up peer addresses or discovery first, so if wallet extensions try to broadcast a transaction
  49.             // before we're actually connected the broadcast waits for an appropriate number of connections.
  50.             if (peerAddresses != null) {
  51.                 for (PeerAddress addr : peerAddresses) vPeerGroup.addAddress(addr);
  52.                 peerAddresses = null;
  53.             } else {
  54.                 vPeerGroup.addPeerDiscovery(new DnsDiscovery(params));
  55.             }
  56.             vChain.addWallet(vWallet);
  57.             vPeerGroup.addWallet(vWallet);
  58.             onSetupCompleted();
  59.  
  60.             if (blockingStartup) {
  61.                 vPeerGroup.startAndWait();
  62.                 // Make sure we shut down cleanly.
  63.                 installShutdownHook();
  64.                 // TODO: Be able to use the provided download listener when doing a blocking startup.
  65.                 final DownloadListener listener = new DownloadListener();
  66.                 vPeerGroup.startBlockChainDownload(listener);
  67.                 listener.await();
  68.             } else {
  69.                 Futures.addCallback(vPeerGroup.start(), new FutureCallback<State>() {
  70.                     @Override
  71.                     public void onSuccess(State result) {
  72.                         final PeerEventListener l = downloadListener == null ? new DownloadListener() : downloadListener;
  73.                         vPeerGroup.startBlockChainDownload(l);
  74.                     }
  75.  
  76.                     @Override
  77.                     public void onFailure(Throwable t) {
  78.                         throw new RuntimeException(t);
  79.                     }
  80.                 });
  81.             }
  82.         } catch (BlockStoreException e) {
  83.             throw new IOException(e);
  84.         } finally {
  85.             if (walletStream != null) walletStream.close();
  86.         }
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement