3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +00:00

Fix a regression introduced by the block height accessor changes that prevented DownloadListener from triggering. Fix more inconsistent locking issues.

This commit is contained in:
Mike Hearn 2012-12-11 00:22:25 +01:00
parent 372a23fbfe
commit 590fd82fdc
2 changed files with 15 additions and 13 deletions

View File

@ -237,7 +237,9 @@ public class Peer {
} else if (m instanceof AlertMessage) { } else if (m instanceof AlertMessage) {
processAlert((AlertMessage)m); processAlert((AlertMessage)m);
} else if (m instanceof VersionMessage) { } else if (m instanceof VersionMessage) {
synchronized (Peer.this) {
peerVersionMessage = (VersionMessage)m; peerVersionMessage = (VersionMessage)m;
}
EventListenerInvoker.invoke(lifecycleListeners, new EventListenerInvoker<PeerLifecycleListener>() { EventListenerInvoker.invoke(lifecycleListeners, new EventListenerInvoker<PeerLifecycleListener>() {
@Override @Override
public void invoke(PeerLifecycleListener listener) { public void invoke(PeerLifecycleListener listener) {
@ -245,6 +247,7 @@ public class Peer {
} }
}); });
} else if (m instanceof VersionAck) { } else if (m instanceof VersionAck) {
synchronized (Peer.this) {
if (peerVersionMessage == null) { if (peerVersionMessage == null) {
throw new ProtocolException("got a version ack before version"); throw new ProtocolException("got a version ack before version");
} }
@ -252,6 +255,7 @@ public class Peer {
throw new ProtocolException("got more than one version ack"); throw new ProtocolException("got more than one version ack");
} }
isAcked = true; isAcked = true;
}
} else if (m instanceof Ping) { } else if (m instanceof Ping) {
if (((Ping) m).hasNonce()) if (((Ping) m).hasNonce())
sendMessage(new Pong(((Ping) m).getNonce())); sendMessage(new Pong(((Ping) m).getNonce()));
@ -424,7 +428,7 @@ public class Peer {
// It is possible for the peer block height difference to be negative when blocks have been solved and broadcast // It is possible for the peer block height difference to be negative when blocks have been solved and broadcast
// since the time we first connected to the peer. However, it's weird and unexpected to receive a callback // since the time we first connected to the peer. However, it's weird and unexpected to receive a callback
// with negative "blocks left" in this case, so we clamp to zero so the API user doesn't have to think about it. // with negative "blocks left" in this case, so we clamp to zero so the API user doesn't have to think about it.
final int blocksLeft = Math.max(0, getPeerBlockHeightDifference()); final int blocksLeft = Math.max(0, (int)peerVersionMessage.bestHeight - blockChain.getBestChainHeight());
EventListenerInvoker.invoke(eventListeners, new EventListenerInvoker<PeerEventListener>() { EventListenerInvoker.invoke(eventListeners, new EventListenerInvoker<PeerEventListener>() {
@Override @Override
public void invoke(PeerEventListener listener) { public void invoke(PeerEventListener listener) {
@ -868,7 +872,7 @@ public class Peer {
// client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or
// there is a bug in the peer management code. // there is a bug in the peer management code.
Preconditions.checkState(params.allowEmptyPeerChains || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); Preconditions.checkState(params.allowEmptyPeerChains || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight);
return chainHeight - blockChain.getChainHead().getHeight(); return chainHeight - blockChain.getBestChainHeight();
} }
/** /**

View File

@ -310,10 +310,8 @@ public class PeerTest extends TestWithNetworkConnections {
inv.addItem(item); inv.addItem(item);
expect(listener.onPreMessageReceived(eq(peer), eq(inv))).andReturn(inv); expect(listener.onPreMessageReceived(eq(peer), eq(inv))).andReturn(inv);
expect(listener.onPreMessageReceived(eq(peer), eq(b2))).andReturn(b2); expect(listener.onPreMessageReceived(eq(peer), eq(b2))).andReturn(b2);
// We have two blocks in our chain (genesis and b1), so our height is 2. The other peer starts at // The listener gets the delta between the first announced height and our height.
// OTHER_PEER_CHAIN_HEIGHT and then when it announces an inv, its height is + 1, so the difference listener.onBlocksDownloaded(eq(peer), anyObject(Block.class), eq(OTHER_PEER_CHAIN_HEIGHT - 2));
// between our height and theirs is OTHER_PEER_CHAIN_HEIGHT + 1 - 2.
listener.onBlocksDownloaded(eq(peer), anyObject(Block.class), eq(OTHER_PEER_CHAIN_HEIGHT + 1 - 2));
expectLastCall(); expectLastCall();
control.replay(); control.replay();