3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +00:00

Fix PeerGroup.peers iterator synchronization

This commit is contained in:
Miron Cuperman (devrandom) 2011-07-25 20:06:51 +00:00
parent 652a394fc1
commit 8469c8fd86

View File

@ -160,6 +160,7 @@ public class PeerGroup {
*/ */
public boolean broadcastTransaction(Transaction tx) { public boolean broadcastTransaction(Transaction tx) {
boolean success = false; boolean success = false;
synchronized (peers) {
for (Peer peer : peers) { for (Peer peer : peers) {
try { try {
peer.broadcastTransaction(tx); peer.broadcastTransaction(tx);
@ -168,6 +169,7 @@ public class PeerGroup {
log.error("failed to broadcast to " + peer, e); log.error("failed to broadcast to " + peer, e);
} }
} }
}
return success; return success;
} }
@ -196,10 +198,12 @@ public class PeerGroup {
peerPool.shutdownNow(); peerPool.shutdownNow();
synchronized (peers) {
for (Peer peer : peers) { for (Peer peer : peers) {
peer.disconnect(); peer.disconnect();
} }
} }
}
/* /*
* Try connecting to a peer. If we exceed the number of connections, delay and try * Try connecting to a peer. If we exceed the number of connections, delay and try
@ -268,9 +272,13 @@ public class PeerGroup {
// TODO be more nuanced about which peer to download from. We can also try // TODO be more nuanced about which peer to download from. We can also try
// downloading from multiple peers and handle the case when a new peer comes along // downloading from multiple peers and handle the case when a new peer comes along
// with a longer chain after we thought we were done. // with a longer chain after we thought we were done.
synchronized (peers) {
if (!peers.isEmpty()) if (!peers.isEmpty())
{
startBlockChainDownloadFromPeer(peers.iterator().next()); startBlockChainDownloadFromPeer(peers.iterator().next());
} }
}
}
/** /**
* Download the blockchain from peers. * Download the blockchain from peers.
@ -296,10 +304,13 @@ public class PeerGroup {
protected synchronized void handlePeerDeath(Peer peer) { protected synchronized void handlePeerDeath(Peer peer) {
if (peer == downloadPeer) { if (peer == downloadPeer) {
downloadPeer = null; downloadPeer = null;
if (downloadListener != null && !peers.isEmpty()) synchronized (peers) {
if (downloadListener != null && !peers.isEmpty()) {
startBlockChainDownloadFromPeer(peers.iterator().next()); startBlockChainDownloadFromPeer(peers.iterator().next());
} }
} }
}
}
private synchronized void startBlockChainDownloadFromPeer(Peer peer) { private synchronized void startBlockChainDownloadFromPeer(Peer peer) {
peer.addEventListener(downloadListener); peer.addEventListener(downloadListener);