private static void syncChain() { try { setup(); int startTransactions = wallet.getTransactions(true).size(); DownloadListener listener = new DownloadListener(); peers.startAndWait(); peers.startBlockChainDownload(listener); try { listener.await(); } catch (InterruptedException e) { System.err.println("Chain download interrupted, quitting ..."); System.exit(1); } int endTransactions = wallet.getTransactions(true).size(); if (endTransactions > startTransactions) { System.out.println("Synced " + (endTransactions - startTransactions) + " transactions."); } } catch (BlockStoreException e) { System.err.println("Error reading block chain file " + chainFileName + ": " + e.getMessage()); e.printStackTrace(); } }
// Sets up all objects needed for network communication but does not bring up the peers. private static void setup() throws BlockStoreException { if (store != null) return; // Already done. // Will create a fresh chain if one doesn't exist or there is an issue with this one. if (!chainFileName.exists() && wallet.getTransactions(true).size() > 0) { // No chain, so reset the wallet as we will be downloading from scratch. System.out.println("Chain file is missing so clearing transactions from the wallet."); reset(); } if (mode == ValidationMode.SPV) { store = new SPVBlockStore(params, chainFileName); chain = new BlockChain(params, wallet, store); } else if (mode == ValidationMode.FULL) { FullPrunedBlockStore s = new H2FullPrunedBlockStore(params, chainFileName.getAbsolutePath(), 5000); store = s; chain = new FullPrunedBlockChain(params, wallet, s); } // This will ensure the wallet is saved when it changes. wallet.autosaveToFile(walletFile, 200, TimeUnit.MILLISECONDS, null); peers = new PeerGroup(params, chain); peers.setUserAgent("WalletTool", "1.0"); peers.addWallet(wallet); if (options.has("peers")) { String peersFlag = (String) options.valueOf("peers"); String[] peerAddrs = peersFlag.split(","); for (String peer : peerAddrs) { try { peers.addAddress(new PeerAddress(InetAddress.getByName(peer), params.getPort())); } catch (UnknownHostException e) { System.err.println( "Could not understand peer domain name/IP address: " + peer + ": " + e.getMessage()); System.exit(1); } } } else { peers.addPeerDiscovery(new DnsDiscovery(params)); } }