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:
parent
318afef956
commit
619325e993
@ -40,4 +40,5 @@ public class AddressMessage extends Message {
|
|||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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}.
|
* or request one specifically using {@link Peer#getBlock(Sha256Hash)}, or grab one from a downloaded {@link BlockChain}.
|
||||||
*/
|
*/
|
||||||
public class Block extends Message {
|
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;
|
private static final long serialVersionUID = 2738848929966035281L;
|
||||||
|
|
||||||
/** How many bytes are required to represent a block header. */
|
/** How many bytes are required to represent a block header. */
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
public class GetAddrMessage extends Message {
|
public class GetAddrMessage extends EmptyMessage {
|
||||||
public GetAddrMessage(NetworkParameters params) {
|
public GetAddrMessage(NetworkParameters params) {
|
||||||
super(params);
|
super(params);
|
||||||
}
|
}
|
||||||
@ -25,4 +25,5 @@ public class GetAddrMessage extends Message {
|
|||||||
void parse() throws ProtocolException {
|
void parse() throws ProtocolException {
|
||||||
// TODO: Implement this.
|
// TODO: Implement this.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GetBlocksMessage extends Message {
|
public class GetBlocksMessage extends Message {
|
||||||
@ -52,24 +52,19 @@ public class GetBlocksMessage extends Message {
|
|||||||
return b.toString();
|
return b.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] bitcoinSerialize() {
|
void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||||
try {
|
|
||||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
||||||
// Version, for some reason.
|
// 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
|
// 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
|
// identifiers that spans the entire chain with exponentially increasing gaps between
|
||||||
// them, until we end up at the genesis block. See CBlockLocator::Set()
|
// 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) {
|
for (Sha256Hash hash : locator) {
|
||||||
// Have to reverse as wire format is little endian.
|
// 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.
|
// Next, a block ID to stop at.
|
||||||
buf.write(stopHash.getBytes());
|
stream.write(stopHash.getBytes());
|
||||||
return buf.toByteArray();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,9 @@ import java.io.*;
|
|||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Arrays;
|
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
|
* 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,
|
* 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.
|
* 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 {
|
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;
|
public static final int MAX_SIZE = 0x02000000;
|
||||||
|
|
||||||
@ -86,7 +90,7 @@ public abstract class Message implements Serializable {
|
|||||||
// are serialized to the wallet.
|
// are serialized to the wallet.
|
||||||
abstract void parse() throws ProtocolException;
|
abstract void parse() throws ProtocolException;
|
||||||
|
|
||||||
public byte[] bitcoinSerialize() {
|
final public byte[] bitcoinSerialize() {
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
bitcoinSerializeToStream(stream);
|
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().
|
* Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize().
|
||||||
*/
|
*/
|
||||||
void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||||
|
log.debug("Warning: {} class has not implemented bitcoinSerializeToStream method. Generating message with no payload", getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
int getMessageSize() {
|
int getMessageSize() {
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
public class Ping extends Message {
|
public class Ping extends EmptyMessage {
|
||||||
@Override
|
@Override
|
||||||
void parse() throws ProtocolException {
|
void parse() throws ProtocolException {
|
||||||
// nothing to parse
|
// nothing to parse
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
public class UnknownMessage extends Message {
|
public class UnknownMessage extends EmptyMessage {
|
||||||
private static final long serialVersionUID = 3614705938207918775L;
|
private static final long serialVersionUID = 3614705938207918775L;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ -33,4 +33,5 @@ public class UnknownMessage extends Message {
|
|||||||
@Override
|
@Override
|
||||||
public void parse() throws ProtocolException {
|
public void parse() throws ProtocolException {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,28 +14,25 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.google.bitcoin.core;
|
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
|
public class VersionAck extends EmptyMessage {
|
||||||
extends Message
|
public VersionAck() {
|
||||||
{
|
|
||||||
public VersionAck()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is needed by the BitcoinSerializer
|
// this is needed by the BitcoinSerializer
|
||||||
public VersionAck(NetworkParameters params, byte[] payload) {
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user