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

Some minor changes:

- Introduce an EmptyMessage class.
 - Make Message.bitcoinSerialize() method final.
 
Patch 1 of the lazy parsing patchset by Steve.
This commit is contained in:
Mike Hearn 2011-10-10 08:53:23 +00:00
parent 318afef956
commit 619325e993
8 changed files with 35 additions and 34 deletions

View File

@ -40,4 +40,5 @@ public class AddressMessage extends Message {
}
return builder.toString();
}
}

View File

@ -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. */

View File

@ -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.
}
}

View File

@ -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());
}
}

View File

@ -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() {

View File

@ -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
}
}

View File

@ -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 {
}
}

View File

@ -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
}
}