From 619325e993e6a65447b20be06251ab0a09d64b09 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 10 Oct 2011 08:53:23 +0000 Subject: [PATCH] Some minor changes: - Introduce an EmptyMessage class. - Make Message.bitcoinSerialize() method final. Patch 1 of the lazy parsing patchset by Steve. --- .../google/bitcoin/core/AddressMessage.java | 1 + src/com/google/bitcoin/core/Block.java | 2 +- .../google/bitcoin/core/GetAddrMessage.java | 3 +- .../google/bitcoin/core/GetBlocksMessage.java | 19 +++++------- src/com/google/bitcoin/core/Message.java | 9 ++++-- src/com/google/bitcoin/core/Ping.java | 3 +- .../google/bitcoin/core/UnknownMessage.java | 3 +- src/com/google/bitcoin/core/VersionAck.java | 29 +++++++++---------- 8 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/com/google/bitcoin/core/AddressMessage.java b/src/com/google/bitcoin/core/AddressMessage.java index 7fe4c192..770fc467 100644 --- a/src/com/google/bitcoin/core/AddressMessage.java +++ b/src/com/google/bitcoin/core/AddressMessage.java @@ -40,4 +40,5 @@ public class AddressMessage extends Message { } return builder.toString(); } + } diff --git a/src/com/google/bitcoin/core/Block.java b/src/com/google/bitcoin/core/Block.java index eaf5bbe4..329336a9 100644 --- a/src/com/google/bitcoin/core/Block.java +++ b/src/com/google/bitcoin/core/Block.java @@ -37,7 +37,7 @@ import static com.google.bitcoin.core.Utils.*; * or request one specifically using {@link Peer#getBlock(Sha256Hash)}, or grab one from a downloaded {@link BlockChain}. */ public class Block extends Message { - private static final Logger log = LoggerFactory.getLogger(Block.class); + private static final Logger log = LoggerFactory.getLogger(Block.class); private static final long serialVersionUID = 2738848929966035281L; /** How many bytes are required to represent a block header. */ diff --git a/src/com/google/bitcoin/core/GetAddrMessage.java b/src/com/google/bitcoin/core/GetAddrMessage.java index 11d92a8a..50922c6e 100644 --- a/src/com/google/bitcoin/core/GetAddrMessage.java +++ b/src/com/google/bitcoin/core/GetAddrMessage.java @@ -16,7 +16,7 @@ package com.google.bitcoin.core; -public class GetAddrMessage extends Message { +public class GetAddrMessage extends EmptyMessage { public GetAddrMessage(NetworkParameters params) { super(params); } @@ -25,4 +25,5 @@ public class GetAddrMessage extends Message { void parse() throws ProtocolException { // TODO: Implement this. } + } diff --git a/src/com/google/bitcoin/core/GetBlocksMessage.java b/src/com/google/bitcoin/core/GetBlocksMessage.java index 9b71765b..e97191b2 100644 --- a/src/com/google/bitcoin/core/GetBlocksMessage.java +++ b/src/com/google/bitcoin/core/GetBlocksMessage.java @@ -16,8 +16,8 @@ package com.google.bitcoin.core; -import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.List; public class GetBlocksMessage extends Message { @@ -52,24 +52,19 @@ public class GetBlocksMessage extends Message { return b.toString(); } - public byte[] bitcoinSerialize() { - try { - ByteArrayOutputStream buf = new ByteArrayOutputStream(); + void bitcoinSerializeToStream(OutputStream stream) throws IOException { // Version, for some reason. - Utils.uint32ToByteStreamLE(NetworkParameters.PROTOCOL_VERSION, buf); + Utils.uint32ToByteStreamLE(NetworkParameters.PROTOCOL_VERSION, stream); // Then a vector of block hashes. This is actually a "block locator", a set of block // identifiers that spans the entire chain with exponentially increasing gaps between // them, until we end up at the genesis block. See CBlockLocator::Set() - buf.write(new VarInt(locator.size()).encode()); + stream.write(new VarInt(locator.size()).encode()); for (Sha256Hash hash : locator) { // Have to reverse as wire format is little endian. - buf.write(Utils.reverseBytes(hash.getBytes())); + stream.write(Utils.reverseBytes(hash.getBytes())); } // Next, a block ID to stop at. - buf.write(stopHash.getBytes()); - return buf.toByteArray(); - } catch (IOException e) { - throw new RuntimeException(e); // Cannot happen. - } + stream.write(stopHash.getBytes()); + } } diff --git a/src/com/google/bitcoin/core/Message.java b/src/com/google/bitcoin/core/Message.java index 540e06a8..01e91c73 100644 --- a/src/com/google/bitcoin/core/Message.java +++ b/src/com/google/bitcoin/core/Message.java @@ -20,6 +20,9 @@ import java.io.*; import java.math.BigInteger; import java.util.Arrays; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * A Message is a data structure that can be serialized/deserialized using both the BitCoin proprietary serialization * format and built-in Java object serialization. Specific types of messages that are used both in the block chain, @@ -28,7 +31,8 @@ import java.util.Arrays; * This class is not useful for library users. If you want to talk to the network see the {@link Peer} class. */ public abstract class Message implements Serializable { - private static final long serialVersionUID = -3561053461717079135L; + private static final Logger log = LoggerFactory.getLogger(Message.class); + private static final long serialVersionUID = -3561053461717079135L; public static final int MAX_SIZE = 0x02000000; @@ -86,7 +90,7 @@ public abstract class Message implements Serializable { // are serialized to the wallet. abstract void parse() throws ProtocolException; - public byte[] bitcoinSerialize() { + final public byte[] bitcoinSerialize() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { bitcoinSerializeToStream(stream); @@ -101,6 +105,7 @@ public abstract class Message implements Serializable { * Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize(). */ void bitcoinSerializeToStream(OutputStream stream) throws IOException { + log.debug("Warning: {} class has not implemented bitcoinSerializeToStream method. Generating message with no payload", getClass()); } int getMessageSize() { diff --git a/src/com/google/bitcoin/core/Ping.java b/src/com/google/bitcoin/core/Ping.java index 7c1f3ee4..8db3cb35 100644 --- a/src/com/google/bitcoin/core/Ping.java +++ b/src/com/google/bitcoin/core/Ping.java @@ -16,9 +16,10 @@ package com.google.bitcoin.core; -public class Ping extends Message { +public class Ping extends EmptyMessage { @Override void parse() throws ProtocolException { // nothing to parse } + } diff --git a/src/com/google/bitcoin/core/UnknownMessage.java b/src/com/google/bitcoin/core/UnknownMessage.java index 6471d63e..9e795a86 100644 --- a/src/com/google/bitcoin/core/UnknownMessage.java +++ b/src/com/google/bitcoin/core/UnknownMessage.java @@ -16,7 +16,7 @@ package com.google.bitcoin.core; -public class UnknownMessage extends Message { +public class UnknownMessage extends EmptyMessage { private static final long serialVersionUID = 3614705938207918775L; private String name; @@ -33,4 +33,5 @@ public class UnknownMessage extends Message { @Override public void parse() throws ProtocolException { } + } diff --git a/src/com/google/bitcoin/core/VersionAck.java b/src/com/google/bitcoin/core/VersionAck.java index 818696e8..4326b489 100644 --- a/src/com/google/bitcoin/core/VersionAck.java +++ b/src/com/google/bitcoin/core/VersionAck.java @@ -14,28 +14,25 @@ * limitations under the License. */ - package com.google.bitcoin.core; /** - * The verack message, sent by a client accepting the version message they received from their peer. + * The verack message, sent by a client accepting the version message they + * received from their peer. */ -public class VersionAck - extends Message -{ - public VersionAck() - { +public class VersionAck extends EmptyMessage { + public VersionAck() { - } + } - // this is needed by the BitcoinSerializer - public VersionAck(NetworkParameters params, byte[] payload) { + // this is needed by the BitcoinSerializer + public VersionAck(NetworkParameters params, byte[] payload) { - } + } + + @Override + void parse() throws ProtocolException { + // nothing to parse for now + } - @Override - void parse() throws ProtocolException - { - // nothing to parse for now - } }