diff --git a/core/pom.xml b/core/pom.xml index da191cf4..f552d799 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -151,7 +151,7 @@ com.google.code.findbugs:jsr305:1.3.9:jar:null:compile:40719ea6961c0cb6afaeb6a921eaa1f6afd4cfdf - com.google.guava:guava:13.0.1:jar:null:compile:0d6f22b1e60a2f1ef99e22c9f5fde270b2088365 + com.google.guava:guava:16.0.1:jar:null:compile:5fa98cd1a63c99a44dd8d3b77e4762b066a5d0c5 com.google.protobuf:protobuf-java:2.5.0:jar:null:compile:a10732c76bfacdbd633a7eb0f7968b1059a65dfa com.h2database:h2:1.3.167:jar:null:compile:d3867d586f087e53eb12fc65e5693d8ee9a5da17 com.lambdaworks:scrypt:1.3.3:jar:null:compile:06d6813de41e177189e1722717979b4fb5454b1d @@ -254,7 +254,7 @@ com.google.guava guava - 13.0.1 + 16.0.1 com.google.code.findbugs diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 5e220d2e..9ac0251d 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -1,5 +1,6 @@ /** * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +15,6 @@ * limitations under the License. */ - package com.google.bitcoin.core; import com.google.bitcoin.net.ClientConnectionManager; @@ -682,7 +682,8 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac protected void startUp() throws Exception { // This is run in a background thread by the Service implementation. vPingTimer = new Timer("Peer pinging thread", true); - channels.startAndWait(); + channels.startAsync(); + channels.awaitRunning(); triggerConnections(); } @@ -691,7 +692,8 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac // This is run on a separate thread by the Service implementation. vPingTimer.cancel(); // Blocking close of all sockets. - channels.stopAndWait(); + channels.stopAsync(); + channels.awaitTerminated(); for (PeerDiscovery peerDiscovery : peerDiscoverers) { peerDiscovery.shutdown(); } diff --git a/core/src/main/java/com/google/bitcoin/kits/WalletAppKit.java b/core/src/main/java/com/google/bitcoin/kits/WalletAppKit.java index 57934aca..6823bf69 100644 --- a/core/src/main/java/com/google/bitcoin/kits/WalletAppKit.java +++ b/core/src/main/java/com/google/bitcoin/kits/WalletAppKit.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +25,8 @@ import com.google.bitcoin.store.WalletProtobufSerializer; import com.google.common.util.concurrent.AbstractIdleService; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; +import com.google.common.util.concurrent.Service; import java.io.File; import java.io.FileInputStream; @@ -228,26 +231,29 @@ public class WalletAppKit extends AbstractIdleService { onSetupCompleted(); if (blockingStartup) { - vPeerGroup.startAndWait(); + vPeerGroup.startAsync(); + vPeerGroup.awaitRunning(); // 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() { + vPeerGroup.startAsync(); + vPeerGroup.addListener(new Service.Listener() { @Override - public void onSuccess(State result) { + public void running() { final PeerEventListener l = downloadListener == null ? new DownloadListener() : downloadListener; vPeerGroup.startBlockChainDownload(l); } @Override - public void onFailure(Throwable t) { - throw new RuntimeException(t); + public void failed(State from, Throwable failure) { + throw new RuntimeException(failure); } - }); + }, MoreExecutors.sameThreadExecutor()); } } catch (BlockStoreException e) { throw new IOException(e); @@ -264,7 +270,8 @@ public class WalletAppKit extends AbstractIdleService { if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { - WalletAppKit.this.stopAndWait(); + WalletAppKit.this.stopAsync(); + WalletAppKit.this.awaitTerminated(); } catch (Exception e) { throw new RuntimeException(e); } @@ -276,7 +283,8 @@ public class WalletAppKit extends AbstractIdleService { protected void shutDown() throws Exception { // Runs in a separate thread. try { - vPeerGroup.stopAndWait(); + vPeerGroup.stopAsync(); + vPeerGroup.awaitTerminated(); vWallet.saveToFile(vWalletFile); vStore.close(); diff --git a/core/src/main/java/com/google/bitcoin/net/NioClient.java b/core/src/main/java/com/google/bitcoin/net/NioClient.java index 37f9fc67..34dace15 100644 --- a/core/src/main/java/com/google/bitcoin/net/NioClient.java +++ b/core/src/main/java/com/google/bitcoin/net/NioClient.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +49,7 @@ public class NioClient implements MessageWriteTarget { @Override public void connectionClosed() { upstreamParser.connectionClosed(); - manager.stop(); + manager.stopAsync(); } @Override @@ -90,7 +91,8 @@ public class NioClient implements MessageWriteTarget { */ public NioClient(final SocketAddress serverAddress, final StreamParser parser, final int connectTimeoutMillis) throws IOException { - manager.startAndWait(); + manager.startAsync(); + manager.awaitRunning(); handler = new Handler(parser, connectTimeoutMillis); manager.openConnection(serverAddress, handler); } diff --git a/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelServerListener.java b/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelServerListener.java index 3da360b4..d4ed47fd 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelServerListener.java +++ b/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelServerListener.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,7 +143,8 @@ public class PaymentChannelServerListener { return new ServerHandler(new InetSocketAddress(inetAddress, port), timeoutSeconds).socketProtobufHandler; } }, new InetSocketAddress(port)); - server.startAndWait(); + server.startAsync(); + server.awaitRunning(); } /** @@ -176,6 +178,7 @@ public class PaymentChannelServerListener { * wallet.

*/ public void close() { - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); } } diff --git a/core/src/test/java/com/google/bitcoin/core/BitcoindComparisonTool.java b/core/src/test/java/com/google/bitcoin/core/BitcoindComparisonTool.java index a1240d94..54bd4961 100644 --- a/core/src/test/java/com/google/bitcoin/core/BitcoindComparisonTool.java +++ b/core/src/test/java/com/google/bitcoin/core/BitcoindComparisonTool.java @@ -1,5 +1,6 @@ /* * Copyright 2012 Matt Corallo. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,7 +143,7 @@ public class BitcoindComparisonTool { bitcoindChainHead = params.getGenesisBlock().getHash(); // Connect to bitcoind and make sure it has no blocks - peers.start(); + peers.startAsync(); peers.setMaxConnections(1); peers.downloadBlockChain(); diff --git a/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java b/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java index 50fc877a..314a52dc 100644 --- a/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java +++ b/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java @@ -1,3 +1,20 @@ +/** + * Copyright 2012 Matt Corallo + * Copyright 2014 Andreas Schildbach + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.google.bitcoin.core; import com.google.bitcoin.core.TransactionConfidence.ConfidenceType; @@ -101,7 +118,8 @@ public class FilteredBlockAndPartialMerkleTreeTests extends TestWithPeerGroup { peerGroup.addWallet(wallet); blockChain.addWallet(wallet); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); // Create a peer. InboundMessageQueuer p1 = connectPeer(1); @@ -143,7 +161,7 @@ public class FilteredBlockAndPartialMerkleTreeTests extends TestWithPeerGroup { // Peer 1 goes away. closePeer(peerOf(p1)); - peerGroup.stop(); + peerGroup.stopAsync(); super.tearDown(); } } diff --git a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java index 5f98b5b6..d0f79a72 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java @@ -1,5 +1,6 @@ /* * Copyright 2011 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -98,12 +99,14 @@ public class PeerGroupTest extends TestWithPeerGroup { public void tearDown() throws Exception { super.tearDown(); Utils.finishMockSleep(); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); } @Test public void listener() throws Exception { - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.addEventListener(listener); // Create a couple of peers. @@ -147,7 +150,8 @@ public class PeerGroupTest extends TestWithPeerGroup { public void shutdown() { } }); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); latch.await(); // Check that we did indeed throw an exception. If we got here it means we threw and then PeerGroup tried // again a bit later. @@ -157,7 +161,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void receiveTxBroadcast() throws Exception { // Check that when we receive transactions on all our peers, we do the right thing. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); // Create a couple of peers. InboundMessageQueuer p1 = connectPeer(1); @@ -190,13 +195,15 @@ public class PeerGroupTest extends TestWithPeerGroup { inbound(p2, new NotFoundMessage(unitTestParams, getdata.getItems())); pingAndWait(p2); assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); } @Test public void singleDownloadPeer1() throws Exception { // Check that we don't attempt to retrieve blocks on multiple peers. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); // Create a couple of peers. InboundMessageQueuer p1 = connectPeer(1); @@ -232,7 +239,7 @@ public class PeerGroupTest extends TestWithPeerGroup { // Peer 2 fetches it next time it hears an inv (should it fetch immediately?). inbound(p2, inv); assertTrue(outbound(p2) instanceof GetDataMessage); - peerGroup.stop(); + peerGroup.stopAsync(); } @Test @@ -240,7 +247,8 @@ public class PeerGroupTest extends TestWithPeerGroup { // Check that we don't attempt multiple simultaneous block chain downloads, when adding a new peer in the // middle of an existing chain download. // Create a couple of peers. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); // Create a couple of peers. InboundMessageQueuer p1 = connectPeer(1); @@ -269,14 +277,15 @@ public class PeerGroupTest extends TestWithPeerGroup { InboundMessageQueuer p2 = connectPeer(2); Message message = (Message)outbound(p2); assertNull(message == null ? "" : message.toString(), message); - peerGroup.stop(); + peerGroup.stopAsync(); } @Test public void transactionConfidence() throws Exception { // Checks that we correctly count how many peers broadcast a transaction, so we can establish some measure of // its trustworthyness assuming an untampered with internet connection. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); final Transaction[] event = new Transaction[2]; peerGroup.addEventListener(new AbstractPeerEventListener() { @@ -338,7 +347,8 @@ public class PeerGroupTest extends TestWithPeerGroup { // The wallet was already added to the peer in setup. final int WEEK = 86400 * 7; final long now = Utils.currentTimeSeconds(); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000); Wallet w2 = new Wallet(params); ECKey key1 = new ECKey(); @@ -358,7 +368,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void noPings() throws Exception { - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.setPingIntervalMsec(0); VersionMessage versionMessage = new VersionMessage(params, 2); versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; @@ -370,7 +381,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void pings() throws Exception { - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.setPingIntervalMsec(100); VersionMessage versionMessage = new VersionMessage(params, 2); versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; @@ -388,7 +400,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void downloadPeerSelection() throws Exception { - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); VersionMessage versionMessage2 = new VersionMessage(params, 2); versionMessage2.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; versionMessage2.localServices = VersionMessage.NODE_NETWORK; @@ -420,7 +433,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void peerTimeoutTest() throws Exception { - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.setConnectTimeoutMillis(100); final SettableFuture peerConnectedFuture = SettableFuture.create(); @@ -459,7 +473,8 @@ public class PeerGroupTest extends TestWithPeerGroup { }); peerGroup.setMaxConnections(3); Utils.setMockSleep(true); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); handleConnectToPeer(0); handleConnectToPeer(1); @@ -512,7 +527,8 @@ public class PeerGroupTest extends TestWithPeerGroup { // Cover bug 513. When a relevant transaction with a p2pubkey output is found, the Bloom filter should be // recalculated to include that transaction hash but not re-broadcast as the remote nodes should have followed // the same procedure. However a new node that's connected should get the fresh filter. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); final ECKey key = wallet.getKeys().get(0); // Create a couple of peers. InboundMessageQueuer p1 = connectPeer(1); @@ -542,7 +558,8 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void testBloomResendOnNewKey() throws Exception { // Check that when we add a new key to the wallet, the Bloom filter is re-calculated and re-sent. - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); // Create a couple of peers. InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p2 = connectPeer(2); diff --git a/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java b/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java index 1b67479f..0a67c1ee 100644 --- a/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java +++ b/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java @@ -1,5 +1,6 @@ /* * Copyright 2011 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -89,8 +90,10 @@ public class TestWithNetworkConnections { blockChain = new BlockChain(unitTestParams, wallet, blockStore); startPeerServers(); - if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER) - channels.startAndWait(); + if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER) { + channels.startAsync(); + channels.awaitRunning(); + } socketAddress = new InetSocketAddress("127.0.0.1", 1111); } @@ -118,7 +121,8 @@ public class TestWithNetworkConnections { }; } }, new InetSocketAddress("127.0.0.1", 2000 + i)); - peerServers[i].startAndWait(); + peerServers[i].startAsync(); + peerServers[i].awaitRunning(); } public void tearDown() throws Exception { @@ -132,7 +136,8 @@ public class TestWithNetworkConnections { } protected void stopPeerServer(int i) { - peerServers[i].stopAndWait(); + peerServers[i].stopAsync(); + peerServers[i].awaitTerminated(); } protected InboundMessageQueuer connect(Peer peer, VersionMessage versionMessage) throws Exception { diff --git a/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java b/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java index aeeeada9..8eb687cb 100644 --- a/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java +++ b/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java @@ -1,5 +1,6 @@ /** * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,13 +59,15 @@ public class TransactionBroadcastTest extends TestWithPeerGroup { // Fix the random permutation that TransactionBroadcast uses to shuffle the peers. TransactionBroadcast.random = new Random(0); peerGroup.setMinBroadcastConnections(2); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); } @After public void tearDown() throws Exception { super.tearDown(); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); } @Test diff --git a/core/src/test/java/com/google/bitcoin/net/NetworkAbstractionTests.java b/core/src/test/java/com/google/bitcoin/net/NetworkAbstractionTests.java index dab55eec..5e8a75d2 100644 --- a/core/src/test/java/com/google/bitcoin/net/NetworkAbstractionTests.java +++ b/core/src/test/java/com/google/bitcoin/net/NetworkAbstractionTests.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,10 +54,10 @@ public class NetworkAbstractionTests { this.clientType = clientType; if (clientType == 0) { channels = new NioClientManager(); - channels.start(); + channels.startAsync(); } else if (clientType == 1) { channels = new BlockingClientManager(); - channels.start(); + channels.startAsync(); } else channels = null; } @@ -117,7 +118,8 @@ public class NetworkAbstractionTests { }, Protos.TwoWayChannelMessage.getDefaultInstance(), 1000, 0); } }, new InetSocketAddress("localhost", 4243)); - server.startAndWait(); + server.startAsync(); + server.awaitRunning(); ProtobufParser clientHandler = new ProtobufParser( new ProtobufParser.Listener() { @@ -154,7 +156,8 @@ public class NetworkAbstractionTests { serverConnectionClosed.get(); clientConnectionClosed.get(); - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); assertFalse(server.isRunning()); } @@ -199,7 +202,8 @@ public class NetworkAbstractionTests { }, Protos.TwoWayChannelMessage.getDefaultInstance(), 1000, 10); } }, new InetSocketAddress("localhost", 4243)); - server.startAndWait(); + server.startAsync(); + server.awaitRunning(); openConnection(new InetSocketAddress("localhost", 4243), new ProtobufParser( new ProtobufParser.Listener() { @@ -254,7 +258,8 @@ public class NetworkAbstractionTests { clientConnection2Closed.get(); serverConnection2Closed.get(); - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); } @Test @@ -289,7 +294,8 @@ public class NetworkAbstractionTests { }, Protos.TwoWayChannelMessage.getDefaultInstance(), 0x10000, 0); } }, new InetSocketAddress("localhost", 4243)); - server.startAndWait(); + server.startAsync(); + server.awaitRunning(); ProtobufParser clientHandler = new ProtobufParser( new ProtobufParser.Listener() { @@ -397,7 +403,8 @@ public class NetworkAbstractionTests { serverConnectionClosed.get(); clientConnectionClosed.get(); - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); } @Test @@ -451,7 +458,8 @@ public class NetworkAbstractionTests { }, Protos.TwoWayChannelMessage.getDefaultInstance(), 1000, 0); } }, new InetSocketAddress("localhost", 4243)); - server.startAndWait(); + server.startAsync(); + server.awaitRunning(); ProtobufParser client1Handler = new ProtobufParser( new ProtobufParser.Listener() { @@ -538,16 +546,18 @@ public class NetworkAbstractionTests { // Try to create a race condition by triggering handlerThread closing and client3 closing at the same time // This often triggers ClosedByInterruptException in handleKey - server.stop(); + server.stopAsync(); server.selector.wakeup(); client3.closeConnection(); client3ConnectionClosed.get(); serverConnectionClosed3.get(); - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); client2ConnectionClosed.get(); serverConnectionClosed2.get(); - server.stopAndWait(); + server.stopAsync(); + server.awaitTerminated(); } } diff --git a/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelClient.java b/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelClient.java index e62d226b..a3cd2d7f 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelClient.java +++ b/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelClient.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,7 +74,8 @@ public class ExamplePaymentChannelClient { wallet().addExtension(new StoredPaymentChannelClientStates(wallet(), peerGroup())); } }; - appKit.startAndWait(); + appKit.startAsync(); + appKit.awaitRunning(); // We now have active network connections and a fully synced wallet. // Add a new key which will be used for the multisig contract. appKit.wallet().addKey(myKey); @@ -103,7 +105,8 @@ public class ExamplePaymentChannelClient { log.info("Waiting ..."); Thread.sleep(60 * 60 * 1000); // 1 hour. log.info("Stopping ..."); - appKit.stopAndWait(); + appKit.stopAsync(); + appKit.awaitTerminated(); } private void openAndSend(int timeoutSecs, InetSocketAddress server, String channelID) throws IOException, ValueOutOfRangeException, InterruptedException { diff --git a/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelServer.java b/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelServer.java index 9de80439..507b6705 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelServer.java +++ b/examples/src/main/java/com/google/bitcoin/examples/ExamplePaymentChannelServer.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +58,8 @@ public class ExamplePaymentChannelServer implements PaymentChannelServerListener wallet().addExtension(storedStates); } }; - appKit.startAndWait(); + appKit.startAsync(); + appKit.awaitRunning(); System.out.println(appKit.wallet()); diff --git a/examples/src/main/java/com/google/bitcoin/examples/FetchBlock.java b/examples/src/main/java/com/google/bitcoin/examples/FetchBlock.java index f634b477..ecc71999 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/FetchBlock.java +++ b/examples/src/main/java/com/google/bitcoin/examples/FetchBlock.java @@ -1,5 +1,6 @@ /* * Copyright 2011 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +38,8 @@ public class FetchBlock { BlockStore blockStore = new MemoryBlockStore(params); BlockChain chain = new BlockChain(params, blockStore); PeerGroup peerGroup = new PeerGroup(params, chain); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort()); peerGroup.addAddress(addr); peerGroup.waitForPeers(1).get(); @@ -48,6 +50,6 @@ public class FetchBlock { System.out.println("Waiting for node to send us the requested block: " + blockHash); Block block = future.get(); System.out.println(block); - peerGroup.stop(); + peerGroup.stopAsync(); } } diff --git a/examples/src/main/java/com/google/bitcoin/examples/FetchTransactions.java b/examples/src/main/java/com/google/bitcoin/examples/FetchTransactions.java index 9d18b14b..65cf08e4 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/FetchTransactions.java +++ b/examples/src/main/java/com/google/bitcoin/examples/FetchTransactions.java @@ -1,5 +1,6 @@ /* * Copyright 2012 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +39,8 @@ public class FetchTransactions { BlockStore blockStore = new MemoryBlockStore(params); BlockChain chain = new BlockChain(params, blockStore); PeerGroup peerGroup = new PeerGroup(params, chain); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort())); peerGroup.waitForPeers(1).get(); Peer peer = peerGroup.getConnectedPeers().get(0); @@ -56,6 +58,7 @@ public class FetchTransactions { } System.out.println("Done."); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); } } diff --git a/examples/src/main/java/com/google/bitcoin/examples/ForwardingService.java b/examples/src/main/java/com/google/bitcoin/examples/ForwardingService.java index 8fadab7c..c13824da 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/ForwardingService.java +++ b/examples/src/main/java/com/google/bitcoin/examples/ForwardingService.java @@ -1,5 +1,6 @@ /** * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,7 +75,8 @@ public class ForwardingService { } // Download the block chain and wait until it's done. - kit.startAndWait(); + kit.startAsync(); + kit.awaitRunning(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { diff --git a/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java b/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java index 8c52ae75..987c3085 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java +++ b/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java @@ -1,5 +1,6 @@ /* * Copyright 2012 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +58,7 @@ public class PeerMonitor { public PeerMonitor() { setupNetwork(); setupGUI(); - peerGroup.start(); + peerGroup.startAsync(); } private void setupNetwork() { @@ -113,7 +114,8 @@ public class PeerMonitor { @Override public void windowClosing(WindowEvent windowEvent) { System.out.println("Shutting down ..."); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); System.out.println("Shutdown complete."); System.exit(0); } diff --git a/examples/src/main/java/com/google/bitcoin/examples/PrivateKeys.java b/examples/src/main/java/com/google/bitcoin/examples/PrivateKeys.java index d5f17abd..a8ce95d1 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/PrivateKeys.java +++ b/examples/src/main/java/com/google/bitcoin/examples/PrivateKeys.java @@ -1,5 +1,6 @@ /** * Copyright 2011 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,9 +62,9 @@ public class PrivateKeys { final PeerGroup peerGroup = new PeerGroup(params, chain); peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost())); - peerGroup.start(); + peerGroup.startAsync(); peerGroup.downloadBlockChain(); - peerGroup.stop(); + peerGroup.stopAsync(); // And take them! System.out.println("Claiming " + Utils.bitcoinValueToFriendlyString(wallet.getBalance()) + " coins"); diff --git a/examples/src/main/java/com/google/bitcoin/examples/RefreshWallet.java b/examples/src/main/java/com/google/bitcoin/examples/RefreshWallet.java index 2b2db0b0..7458683d 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/RefreshWallet.java +++ b/examples/src/main/java/com/google/bitcoin/examples/RefreshWallet.java @@ -1,5 +1,6 @@ /* * Copyright 2011 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +42,7 @@ public class RefreshWallet { final PeerGroup peerGroup = new PeerGroup(params, chain); peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost())); - peerGroup.start(); + peerGroup.startAsync(); wallet.addEventListener(new AbstractWalletEventListener() { @Override @@ -53,7 +54,7 @@ public class RefreshWallet { // Now download and process the block chain. peerGroup.downloadBlockChain(); - peerGroup.stop(); + peerGroup.stopAsync(); wallet.saveToFile(file); System.out.println("\nDone!\n"); System.out.println(wallet.toString()); diff --git a/tools/src/main/java/com/google/bitcoin/tools/BuildCheckpoints.java b/tools/src/main/java/com/google/bitcoin/tools/BuildCheckpoints.java index 048c9d93..fd59e015 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/BuildCheckpoints.java +++ b/tools/src/main/java/com/google/bitcoin/tools/BuildCheckpoints.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.google.bitcoin.tools; import com.google.bitcoin.core.*; @@ -58,7 +75,8 @@ public class BuildCheckpoints { } }, Threading.SAME_THREAD); - peerGroup.startAndWait(); + peerGroup.startAsync(); + peerGroup.awaitRunning(); peerGroup.downloadBlockChain(); checkState(checkpoints.size() > 0); @@ -85,7 +103,8 @@ public class BuildCheckpoints { digestOutputStream.close(); fileOutputStream.close(); - peerGroup.stopAndWait(); + peerGroup.stopAsync(); + peerGroup.awaitTerminated(); store.close(); // Sanity check the created file. diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 97a3f7b2..34d0878c 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -1,5 +1,6 @@ /* * Copyright 2012 Google Inc. + * Copyright 2014 Andreas Schildbach * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -452,7 +453,8 @@ public class WalletTool { } setup(); - peers.startAndWait(); + peers.startAsync(); + peers.awaitRunning(); // Wait for peers to connect, the tx to be sent to one of them and for it to be propagated across the // network. Once propagation is complete and we heard the transaction back from all our peers, it will // be committed to the wallet. @@ -557,7 +559,8 @@ public class WalletTool { ListenableFuture future = session.sendPayment(ImmutableList.of(req.tx), null, null); if (future == null) { // No payment_url for submission so, broadcast and wait. - peers.startAndWait(); + peers.startAsync(); + peers.awaitRunning(); peers.broadcastTransaction(req.tx).get(); } else { PaymentSession.Ack ack = future.get(); @@ -651,7 +654,7 @@ public class WalletTool { break; } - peers.start(); + peers.startAsync(); try { latch.await(); } catch (InterruptedException e) { @@ -713,7 +716,8 @@ public class WalletTool { setup(); int startTransactions = wallet.getTransactions(true).size(); DownloadListener listener = new DownloadListener(); - peers.startAndWait(); + peers.startAsync(); + peers.awaitRunning(); peers.startBlockChainDownload(listener); try { listener.await(); @@ -734,7 +738,8 @@ public class WalletTool { private static void shutdown() { try { if (peers == null) return; // setup() never called so nothing to do. - peers.stopAndWait(); + peers.stopAsync(); + peers.awaitTerminated(); saveWallet(walletFile); store.close(); wallet = null; diff --git a/tools/src/main/java/com/google/bitcoin/tools/WatchMempool.java b/tools/src/main/java/com/google/bitcoin/tools/WatchMempool.java index 4d76df13..bbbc1e1d 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WatchMempool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WatchMempool.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013 Google Inc. + * Copyright 2014 Andreas Schildbach + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.google.bitcoin.tools; import com.google.bitcoin.core.*; @@ -27,6 +44,6 @@ public class WatchMempool { } } }); - peerGroup.start(); + peerGroup.startAsync(); } } diff --git a/wallettemplate/pom.xml b/wallettemplate/pom.xml index 66e2ebaa..8e1f7826 100644 --- a/wallettemplate/pom.xml +++ b/wallettemplate/pom.xml @@ -37,7 +37,7 @@ com.google.guava guava - 13.0 + 16.0.1 com.aquafx-project diff --git a/wallettemplate/src/main/java/wallettemplate/Main.java b/wallettemplate/src/main/java/wallettemplate/Main.java index 734fcf3b..c49b51a4 100644 --- a/wallettemplate/src/main/java/wallettemplate/Main.java +++ b/wallettemplate/src/main/java/wallettemplate/Main.java @@ -92,8 +92,9 @@ public class Main extends Application { // or progress widget to keep the user engaged whilst we initialise, but we don't. bitcoin.setDownloadListener(controller.progressBarUpdater()) .setBlockingStartup(false) - .setUserAgent(APP_NAME, "1.0") - .startAndWait(); + .setUserAgent(APP_NAME, "1.0"); + bitcoin.startAsync(); + bitcoin.awaitRunning(); // Don't make the user wait for confirmations for now, as the intention is they're sending it their own money! bitcoin.wallet().allowSpendingUnconfirmedTransactions(); bitcoin.peerGroup().setMaxConnections(11); @@ -162,7 +163,8 @@ public class Main extends Application { @Override public void stop() throws Exception { - bitcoin.stopAndWait(); + bitcoin.stopAsync(); + bitcoin.awaitTerminated(); super.stop(); }