3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-11 17:55:53 +00:00

Replace peerGroup.startAsync/awaitRunning with start() and awaitTerminated with stop().

This commit is contained in:
Mike Hearn 2014-11-13 23:11:16 +01:00
parent 10340b13a6
commit e7c00df740
9 changed files with 38 additions and 72 deletions

View File

@ -145,8 +145,7 @@ public class FilteredBlockAndPartialMerkleTreeTests extends TestWithPeerGroup {
blockChain.addWallet(wallet); blockChain.addWallet(wallet);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a peer. // Create a peer.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);

View File

@ -112,8 +112,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void listener() throws Exception { public void listener() throws Exception {
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.addEventListener(listener); peerGroup.addEventListener(listener);
// Create a couple of peers. // Create a couple of peers.
@ -155,6 +154,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
return new InetSocketAddress[]{new InetSocketAddress("localhost", 1)}; return new InetSocketAddress[]{new InetSocketAddress("localhost", 1)};
} }
} }
@Override @Override
public void shutdown() { public void shutdown() {
} }
@ -196,15 +196,13 @@ public class PeerGroupTest extends TestWithPeerGroup {
assertEquals(99, peerAddresses.size()); assertEquals(99, peerAddresses.size());
} }
}); });
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
} }
@Test @Test
public void receiveTxBroadcast() throws Exception { public void receiveTxBroadcast() throws Exception {
// Check that when we receive transactions on all our peers, we do the right thing. // Check that when we receive transactions on all our peers, we do the right thing.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a couple of peers. // Create a couple of peers.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
@ -241,10 +239,9 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void receiveTxBroadcastOnAddedWallet() throws Exception { public void receiveTxBroadcastOnAddeweldWallet() throws Exception {
// Check that when we receive transactions on all our peers, we do the right thing. // Check that when we receive transactions on all our peers, we do the right thing.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a peer. // Create a peer.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
@ -256,8 +253,8 @@ public class PeerGroupTest extends TestWithPeerGroup {
peerGroup.addWallet(wallet2); peerGroup.addWallet(wallet2);
blockChain.addWallet(wallet2); blockChain.addWallet(wallet2);
assertTrue(outbound(p1) instanceof BloomFilter); assertEquals(BloomFilter.class, waitForOutbound(p1).getClass());
assertTrue(outbound(p1) instanceof MemoryPoolMessage); assertEquals(MemoryPoolMessage.class, waitForOutbound(p1).getClass());
Coin value = COIN; Coin value = COIN;
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address2); Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address2);
@ -278,8 +275,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void singleDownloadPeer1() throws Exception { public void singleDownloadPeer1() throws Exception {
// Check that we don't attempt to retrieve blocks on multiple peers. // Check that we don't attempt to retrieve blocks on multiple peers.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a couple of peers. // Create a couple of peers.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
@ -322,8 +318,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Check that we don't attempt multiple simultaneous block chain downloads, when adding a new peer in the // Check that we don't attempt multiple simultaneous block chain downloads, when adding a new peer in the
// middle of an existing chain download. // middle of an existing chain download.
// Create a couple of peers. // Create a couple of peers.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a couple of peers. // Create a couple of peers.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
@ -358,8 +353,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
public void transactionConfidence() throws Exception { public void transactionConfidence() throws Exception {
// Checks that we correctly count how many peers broadcast a transaction, so we can establish some measure of // 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. // its trustworthyness assuming an untampered with internet connection.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
final Transaction[] event = new Transaction[2]; final Transaction[] event = new Transaction[2];
peerGroup.addEventListener(new AbstractPeerEventListener() { peerGroup.addEventListener(new AbstractPeerEventListener() {
@ -422,8 +416,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// The wallet was already added to the peer in setup. // The wallet was already added to the peer in setup.
final int WEEK = 86400 * 7; final int WEEK = 86400 * 7;
final long now = Utils.currentTimeSeconds(); final long now = Utils.currentTimeSeconds();
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000); assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000);
Wallet w2 = new Wallet(params); Wallet w2 = new Wallet(params);
ECKey key1 = new ECKey(); ECKey key1 = new ECKey();
@ -443,8 +436,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void noPings() throws Exception { public void noPings() throws Exception {
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.setPingIntervalMsec(0); peerGroup.setPingIntervalMsec(0);
VersionMessage versionMessage = new VersionMessage(params, 2); VersionMessage versionMessage = new VersionMessage(params, 2);
versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION;
@ -456,8 +448,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void pings() throws Exception { public void pings() throws Exception {
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.setPingIntervalMsec(100); peerGroup.setPingIntervalMsec(100);
VersionMessage versionMessage = new VersionMessage(params, 2); VersionMessage versionMessage = new VersionMessage(params, 2);
versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; versionMessage.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION;
@ -475,8 +466,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void downloadPeerSelection() throws Exception { public void downloadPeerSelection() throws Exception {
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
VersionMessage versionMessage2 = new VersionMessage(params, 2); VersionMessage versionMessage2 = new VersionMessage(params, 2);
versionMessage2.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION; versionMessage2.clientVersion = FilteredBlock.MIN_PROTOCOL_VERSION;
versionMessage2.localServices = VersionMessage.NODE_NETWORK; versionMessage2.localServices = VersionMessage.NODE_NETWORK;
@ -508,8 +498,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void peerTimeoutTest() throws Exception { public void peerTimeoutTest() throws Exception {
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.setConnectTimeoutMillis(100); peerGroup.setConnectTimeoutMillis(100);
final SettableFuture<Void> peerConnectedFuture = SettableFuture.create(); final SettableFuture<Void> peerConnectedFuture = SettableFuture.create();
@ -557,8 +546,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
blockJobs = true; blockJobs = true;
jobBlocks.release(2); // startup + first peer discovery jobBlocks.release(2); // startup + first peer discovery
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
jobBlocks.release(3); // One for each peer. jobBlocks.release(3); // One for each peer.
handleConnectToPeer(0); handleConnectToPeer(0);
@ -608,8 +596,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Cover bug 513. When a relevant transaction with a p2pubkey output is found, the Bloom filter should be // 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 // 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. // the same procedure. However a new node that's connected should get the fresh filter.
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
final ECKey key = wallet.currentReceiveKey(); final ECKey key = wallet.currentReceiveKey();
// Create a couple of peers. // Create a couple of peers.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
@ -642,8 +629,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// we exceed the lookahead threshold. // we exceed the lookahead threshold.
wallet.setKeychainLookaheadSize(5); wallet.setKeychainLookaheadSize(5);
wallet.setKeychainLookaheadThreshold(4); wallet.setKeychainLookaheadThreshold(4);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
// Create a couple of peers. // Create a couple of peers.
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
InboundMessageQueuer p2 = connectPeer(2); InboundMessageQueuer p2 = connectPeer(2);
@ -674,8 +660,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
@Test @Test
public void waitForNumPeers1() throws Exception { public void waitForNumPeers1() throws Exception {
ListenableFuture<List<Peer>> future = peerGroup.waitForPeers(3); ListenableFuture<List<Peer>> future = peerGroup.waitForPeers(3);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
assertFalse(future.isDone()); assertFalse(future.isDone());
connectPeer(1); connectPeer(1);
assertFalse(future.isDone()); assertFalse(future.isDone());
@ -700,8 +685,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
VersionMessage ver2 = new VersionMessage(params, 10); VersionMessage ver2 = new VersionMessage(params, 10);
ver2.clientVersion = newVer; ver2.clientVersion = newVer;
ver2.localServices = VersionMessage.NODE_NETWORK; ver2.localServices = VersionMessage.NODE_NETWORK;
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
assertFalse(future.isDone()); assertFalse(future.isDone());
connectPeer(1, ver1); connectPeer(1, ver1);
assertFalse(future.isDone()); assertFalse(future.isDone());
@ -733,8 +717,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
try { try {
peerGroup.setUseLocalhostPeerWhenPossible(true); peerGroup.setUseLocalhostPeerWhenPossible(true);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
local.accept().close(); // Probe connect local.accept().close(); // Probe connect
local.accept(); // Real connect local.accept(); // Real connect
// If we get here it used the local peer. Check no others are in use. // If we get here it used the local peer. Check no others are in use.
@ -768,8 +751,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
wallet.setKeychainLookaheadSize(4); wallet.setKeychainLookaheadSize(4);
wallet.setKeychainLookaheadThreshold(2); wallet.setKeychainLookaheadThreshold(2);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p1 = connectPeer(1);
assertTrue(p1.lastReceivedFilter.contains(keys.get(0).getPubKey())); assertTrue(p1.lastReceivedFilter.contains(keys.get(0).getPubKey()));
assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash())); assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash()));

View File

@ -59,8 +59,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup {
// Fix the random permutation that TransactionBroadcast uses to shuffle the peers. // Fix the random permutation that TransactionBroadcast uses to shuffle the peers.
TransactionBroadcast.random = new Random(0); TransactionBroadcast.random = new Random(0);
peerGroup.setMinBroadcastConnections(2); peerGroup.setMinBroadcastConnections(2);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
} }
@Override @Override

View File

@ -38,8 +38,7 @@ public class FetchBlock {
BlockStore blockStore = new MemoryBlockStore(params); BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore); BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain); PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort()); PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort());
peerGroup.addAddress(addr); peerGroup.addAddress(addr);
peerGroup.waitForPeers(1).get(); peerGroup.waitForPeers(1).get();

View File

@ -39,8 +39,7 @@ public class FetchTransactions {
BlockStore blockStore = new MemoryBlockStore(params); BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore); BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain); PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort())); peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort()));
peerGroup.waitForPeers(1).get(); peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0); Peer peer = peerGroup.getConnectedPeers().get(0);
@ -58,7 +57,6 @@ public class FetchTransactions {
} }
System.out.println("Done."); System.out.println("Done.");
peerGroup.stopAsync(); peerGroup.stop();
peerGroup.awaitTerminated();
} }
} }

View File

@ -115,8 +115,7 @@ public class PeerMonitor {
@Override @Override
public void windowClosing(WindowEvent windowEvent) { public void windowClosing(WindowEvent windowEvent) {
System.out.println("Shutting down ..."); System.out.println("Shutting down ...");
peerGroup.stopAsync(); peerGroup.stop();
peerGroup.awaitTerminated();
System.out.println("Shutdown complete."); System.out.println("Shutdown complete.");
System.exit(0); System.exit(0);
} }

View File

@ -58,8 +58,7 @@ public class RestoreFromSeed {
}; };
// Now we re-download the blockchain. This replays the chain into the wallet. Once this is completed our wallet should know of all its transactions and print the correct balance. // Now we re-download the blockchain. This replays the chain into the wallet. Once this is completed our wallet should know of all its transactions and print the correct balance.
peers.startAsync(); peers.start();
peers.awaitRunning();
peers.startBlockChainDownload(bListener); peers.startBlockChainDownload(bListener);
bListener.await(); bListener.await();
@ -68,8 +67,6 @@ public class RestoreFromSeed {
System.out.println(wallet.toString()); System.out.println(wallet.toString());
// shutting down again // shutting down again
peers.stopAsync(); peers.stop();
peers.awaitTerminated();
} }
} }

View File

@ -80,8 +80,7 @@ public class BuildCheckpoints {
} }
}, Threading.SAME_THREAD); }, Threading.SAME_THREAD);
peerGroup.startAsync(); peerGroup.start();
peerGroup.awaitRunning();
peerGroup.downloadBlockChain(); peerGroup.downloadBlockChain();
checkState(checkpoints.size() > 0); checkState(checkpoints.size() > 0);
@ -90,8 +89,7 @@ public class BuildCheckpoints {
writeBinaryCheckpoints(checkpoints, PLAIN_CHECKPOINTS_FILE); writeBinaryCheckpoints(checkpoints, PLAIN_CHECKPOINTS_FILE);
writeTextualCheckpoints(checkpoints, TEXTUAL_CHECKPOINTS_FILE); writeTextualCheckpoints(checkpoints, TEXTUAL_CHECKPOINTS_FILE);
peerGroup.stopAsync(); peerGroup.stop();
peerGroup.awaitTerminated();
store.close(); store.close();
// Sanity check the created files. // Sanity check the created files.

View File

@ -445,8 +445,7 @@ public class WalletTool {
private static void rotate() throws BlockStoreException { private static void rotate() throws BlockStoreException {
setup(); setup();
peers.startAsync(); peers.start();
peers.awaitRunning();
// Set a key rotation time and possibly broadcast the resulting maintenance transactions. // Set a key rotation time and possibly broadcast the resulting maintenance transactions.
long rotationTimeSecs = Utils.currentTimeSeconds(); long rotationTimeSecs = Utils.currentTimeSeconds();
if (options.has(dateFlag)) { if (options.has(dateFlag)) {
@ -583,8 +582,7 @@ public class WalletTool {
} }
setup(); setup();
peers.startAsync(); peers.start();
peers.awaitRunning();
// Wait for peers to connect, the tx to be sent to one of them and for it to be propagated across the // 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 // network. Once propagation is complete and we heard the transaction back from all our peers, it will
// be committed to the wallet. // be committed to the wallet.
@ -700,8 +698,7 @@ public class WalletTool {
ListenableFuture<PaymentProtocol.Ack> future = session.sendPayment(ImmutableList.of(req.tx), null, null); ListenableFuture<PaymentProtocol.Ack> future = session.sendPayment(ImmutableList.of(req.tx), null, null);
if (future == null) { if (future == null) {
// No payment_url for submission so, broadcast and wait. // No payment_url for submission so, broadcast and wait.
peers.startAsync(); peers.start();
peers.awaitRunning();
peers.broadcastTransaction(req.tx).get(); peers.broadcastTransaction(req.tx).get();
} else { } else {
PaymentProtocol.Ack ack = future.get(); PaymentProtocol.Ack ack = future.get();
@ -864,8 +861,7 @@ public class WalletTool {
setup(); setup();
int startTransactions = wallet.getTransactions(true).size(); int startTransactions = wallet.getTransactions(true).size();
DownloadListener listener = new DownloadListener(); DownloadListener listener = new DownloadListener();
peers.startAsync(); peers.start();
peers.awaitRunning();
peers.startBlockChainDownload(listener); peers.startBlockChainDownload(listener);
try { try {
listener.await(); listener.await();
@ -886,8 +882,7 @@ public class WalletTool {
private static void shutdown() { private static void shutdown() {
try { try {
if (peers == null) return; // setup() never called so nothing to do. if (peers == null) return; // setup() never called so nothing to do.
peers.stopAsync(); peers.stop();
peers.awaitTerminated();
saveWallet(walletFile); saveWallet(walletFile);
store.close(); store.close();
wallet = null; wallet = null;