3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Simplify TCPNetworkConnection a bit and print the version message data when the peer sends it to us, not when we get a VerAck back from our own version announcement.

This commit is contained in:
Mike Hearn 2013-01-20 23:47:56 +01:00
parent caa92985e1
commit 19b032e2e4

View File

@ -60,10 +60,6 @@ public class TCPNetworkConnection implements NetworkConnection {
private BitcoinSerializer serializer = null; private BitcoinSerializer serializer = null;
private VersionMessage myVersionMessage; private VersionMessage myVersionMessage;
private static final Date checksummingProtocolChangeDate = new Date(1329696000000L);
private long messageCount;
private Channel channel; private Channel channel;
private NetworkHandler handler; private NetworkHandler handler;
@ -81,10 +77,7 @@ public class TCPNetworkConnection implements NetworkConnection {
public TCPNetworkConnection(NetworkParameters params, VersionMessage ver) { public TCPNetworkConnection(NetworkParameters params, VersionMessage ver) {
this.params = params; this.params = params;
this.myVersionMessage = ver; this.myVersionMessage = ver;
this.serializer = new BitcoinSerializer(this.params, true);
// So pre-Feb 2012, update checkumming property after version is read.
this.serializer = new BitcoinSerializer(this.params, false);
this.serializer.setUseChecksumming(Utils.now().after(checksummingProtocolChangeDate));
this.handler = new NetworkHandler(); this.handler = new NetworkHandler();
} }
@ -139,19 +132,13 @@ public class TCPNetworkConnection implements NetworkConnection {
write(channel, message); write(channel, message);
} }
private void onFirstMessage(Message m) throws IOException, ProtocolException { private void onVersionMessage(Message m) throws IOException, ProtocolException {
if (!(m instanceof VersionMessage)) { if (!(m instanceof VersionMessage)) {
// Bad peers might not follow the protocol. This has been seen in the wild (issue 81). // Bad peers might not follow the protocol. This has been seen in the wild (issue 81).
log.info("First message received was not a version message but rather " + m); log.info("First message received was not a version message but rather " + m);
return; return;
} }
versionMessage = (VersionMessage) m; versionMessage = (VersionMessage) m;
// Now it's our turn ...
// Send an ACK message stating we accept the peers protocol version.
write(channel, new VersionAck());
}
private void onSecondMessage() throws IOException, ProtocolException {
// Switch to the new protocol version. // Switch to the new protocol version.
int peerVersion = versionMessage.clientVersion; int peerVersion = versionMessage.clientVersion;
log.info("Connected to peer: version={}, subVer='{}', services=0x{}, time={}, blocks={}", new Object[] { log.info("Connected to peer: version={}, subVer='{}', services=0x{}, time={}, blocks={}", new Object[] {
@ -161,6 +148,9 @@ public class TCPNetworkConnection implements NetworkConnection {
new Date(versionMessage.time * 1000), new Date(versionMessage.time * 1000),
versionMessage.bestHeight versionMessage.bestHeight
}); });
// Now it's our turn ...
// Send an ACK message stating we accept the peers protocol version.
write(channel, new VersionAck());
// bitcoinj is a client mode implementation. That means there's not much point in us talking to other client // bitcoinj is a client mode implementation. That means there's not much point in us talking to other client
// mode nodes because we can't download the data from them we need to find/verify transactions. Some bogus // mode nodes because we can't download the data from them we need to find/verify transactions. Some bogus
// implementations claim to have a block chain in their services field but then report a height of zero, filter // implementations claim to have a block chain in their services field but then report a height of zero, filter
@ -170,8 +160,6 @@ public class TCPNetworkConnection implements NetworkConnection {
// Shut down the channel // Shut down the channel
throw new ProtocolException("Peer does not have a copy of the block chain."); throw new ProtocolException("Peer does not have a copy of the block chain.");
} }
// Newer clients use checksumming.
serializer.setUseChecksumming(peerVersion >= 209);
// Handshake is done! // Handshake is done!
if (handshakeFuture != null) if (handshakeFuture != null)
handshakeFuture.set(this); handshakeFuture.set(this);
@ -219,12 +207,8 @@ public class TCPNetworkConnection implements NetworkConnection {
protected Object decode(ChannelHandlerContext ctx, Channel chan, protected Object decode(ChannelHandlerContext ctx, Channel chan,
ChannelBuffer buffer, VoidEnum state) throws Exception { ChannelBuffer buffer, VoidEnum state) throws Exception {
Message message = serializer.deserialize(new ChannelBufferInputStream(buffer)); Message message = serializer.deserialize(new ChannelBufferInputStream(buffer));
messageCount++; if (message instanceof VersionMessage)
if (messageCount == 1) { onVersionMessage(message);
onFirstMessage(message);
} else if (messageCount == 2) {
onSecondMessage();
}
return message; return message;
} }