3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +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) {
boolean success = false;
synchronized (peers) {
for (Peer peer : peers) {
try {
peer.broadcastTransaction(tx);
@ -168,6 +169,7 @@ public class PeerGroup {
log.error("failed to broadcast to " + peer, e);
}
}
}
return success;
}
@ -196,10 +198,12 @@ public class PeerGroup {
peerPool.shutdownNow();
synchronized (peers) {
for (Peer peer : peers) {
peer.disconnect();
}
}
}
/*
* 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
// downloading from multiple peers and handle the case when a new peer comes along
// with a longer chain after we thought we were done.
synchronized (peers) {
if (!peers.isEmpty())
{
startBlockChainDownloadFromPeer(peers.iterator().next());
}
}
}
/**
* Download the blockchain from peers.
@ -296,10 +304,13 @@ public class PeerGroup {
protected synchronized void handlePeerDeath(Peer peer) {
if (peer == downloadPeer) {
downloadPeer = null;
if (downloadListener != null && !peers.isEmpty())
synchronized (peers) {
if (downloadListener != null && !peers.isEmpty()) {
startBlockChainDownloadFromPeer(peers.iterator().next());
}
}
}
}
private synchronized void startBlockChainDownloadFromPeer(Peer peer) {
peer.addEventListener(downloadListener);