Message: remove length arguments from constructors of the hierarchy

There was never a case where `length` wasn't equal to `payload.length`,
which is supplied by the same constructors. This also does away with
a couple of constructor variants.
This commit is contained in:
Andreas Schildbach 2023-03-19 19:38:03 +01:00
parent 8faf61ae0c
commit aab3e32624
21 changed files with 77 additions and 109 deletions

View file

@ -28,8 +28,8 @@ public abstract class AddressMessage extends Message {
protected static final long MAX_ADDRESSES = 1000; protected static final long MAX_ADDRESSES = 1000;
protected List<PeerAddress> addresses; protected List<PeerAddress> addresses;
AddressMessage(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException { AddressMessage(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, serializer, length); super(params, payload, offset, serializer);
} }
@Override @Override

View file

@ -36,32 +36,28 @@ public class AddressV1Message extends AddressMessage {
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param serializer the serializer to use for this block. * @param serializer the serializer to use for this block.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
AddressV1Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException { AddressV1Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, serializer, length); super(params, payload, offset, serializer);
} }
/** /**
* Construct a new 'addr' message. * Construct a new 'addr' message.
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param serializer the serializer to use for this block. * @param serializer the serializer to use for this block.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
AddressV1Message(NetworkParameters params, byte[] payload, MessageSerializer serializer, int length) throws ProtocolException { AddressV1Message(NetworkParameters params, byte[] payload, MessageSerializer serializer) throws ProtocolException {
super(params, payload, 0, serializer, length); super(params, payload, 0, serializer);
} }
AddressV1Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException { AddressV1Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException {
super(params, payload, offset, params.getDefaultSerializer(), UNKNOWN_LENGTH); super(params, payload, offset, params.getDefaultSerializer());
} }
AddressV1Message(NetworkParameters params, byte[] payload) throws ProtocolException { AddressV1Message(NetworkParameters params, byte[] payload) throws ProtocolException {
super(params, payload, 0, params.getDefaultSerializer(), UNKNOWN_LENGTH); super(params, payload, 0, params.getDefaultSerializer());
} }
@Override @Override

View file

@ -37,28 +37,24 @@ public class AddressV2Message extends AddressMessage {
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param serializer the serializer to use for this block. * @param serializer the serializer to use for this block.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
AddressV2Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException { AddressV2Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, serializer, length); super(params, payload, offset, serializer);
} }
/** /**
* Construct a new 'addrv2' message. * Construct a new 'addrv2' message.
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param serializer the serializer to use for this block. * @param serializer the serializer to use for this block.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
AddressV2Message(NetworkParameters params, byte[] payload, MessageSerializer serializer, int length) throws ProtocolException { AddressV2Message(NetworkParameters params, byte[] payload, MessageSerializer serializer) throws ProtocolException {
super(params, payload, 0, serializer, length); super(params, payload, 0, serializer);
} }
AddressV2Message(NetworkParameters params, byte[] payload) throws ProtocolException { AddressV2Message(NetworkParameters params, byte[] payload) throws ProtocolException {
super(params, payload, 0, params.getDefaultSerializer(), UNKNOWN_LENGTH); super(params, payload, 0, params.getDefaultSerializer());
} }
@Override @Override

View file

@ -211,36 +211,36 @@ public class BitcoinSerializer extends MessageSerializer {
} }
try { try {
return makeMessage(header.command, header.size, payloadBytes, hash); return makeMessage(header.command, payloadBytes, hash);
} catch (Exception e) { } catch (Exception e) {
throw new ProtocolException("Error deserializing message " + ByteUtils.formatHex(payloadBytes) + "\n", e); throw new ProtocolException("Error deserializing message " + ByteUtils.formatHex(payloadBytes) + "\n", e);
} }
} }
private Message makeMessage(String command, int length, byte[] payloadBytes, byte[] hash) throws ProtocolException { private Message makeMessage(String command, byte[] payloadBytes, byte[] hash) throws ProtocolException {
// We use an if ladder rather than reflection because reflection is very slow on Android. // We use an if ladder rather than reflection because reflection is very slow on Android.
if (command.equals("version")) { if (command.equals("version")) {
return new VersionMessage(params, payloadBytes); return new VersionMessage(params, payloadBytes);
} else if (command.equals("inv")) { } else if (command.equals("inv")) {
return makeInventoryMessage(payloadBytes, length); return makeInventoryMessage(payloadBytes);
} else if (command.equals("block")) { } else if (command.equals("block")) {
return makeBlock(payloadBytes, length); return makeBlock(payloadBytes);
} else if (command.equals("merkleblock")) { } else if (command.equals("merkleblock")) {
return makeFilteredBlock(payloadBytes); return makeFilteredBlock(payloadBytes);
} else if (command.equals("getdata")) { } else if (command.equals("getdata")) {
return new GetDataMessage(params, payloadBytes, this, length); return new GetDataMessage(params, payloadBytes, this);
} else if (command.equals("getblocks")) { } else if (command.equals("getblocks")) {
return new GetBlocksMessage(params, payloadBytes); return new GetBlocksMessage(params, payloadBytes);
} else if (command.equals("getheaders")) { } else if (command.equals("getheaders")) {
return new GetHeadersMessage(params, payloadBytes); return new GetHeadersMessage(params, payloadBytes);
} else if (command.equals("tx")) { } else if (command.equals("tx")) {
return makeTransaction(payloadBytes, 0, length, hash); return makeTransaction(payloadBytes, 0, hash);
} else if (command.equals("sendaddrv2")) { } else if (command.equals("sendaddrv2")) {
return new SendAddrV2Message(params); return new SendAddrV2Message(params);
} else if (command.equals("addr")) { } else if (command.equals("addr")) {
return makeAddressV1Message(payloadBytes, length); return makeAddressV1Message(payloadBytes);
} else if (command.equals("addrv2")) { } else if (command.equals("addrv2")) {
return makeAddressV2Message(payloadBytes, length); return makeAddressV2Message(payloadBytes);
} else if (command.equals("ping")) { } else if (command.equals("ping")) {
return new Ping(params, payloadBytes); return new Ping(params, payloadBytes);
} else if (command.equals("pong")) { } else if (command.equals("pong")) {
@ -260,7 +260,7 @@ public class BitcoinSerializer extends MessageSerializer {
} else if (command.equals("sendheaders")) { } else if (command.equals("sendheaders")) {
return new SendHeadersMessage(params, payloadBytes); return new SendHeadersMessage(params, payloadBytes);
} else if (command.equals("feefilter")) { } else if (command.equals("feefilter")) {
return new FeeFilterMessage(params, payloadBytes, this, length); return new FeeFilterMessage(params, payloadBytes, this);
} else { } else {
return new UnknownMessage(params, command, payloadBytes); return new UnknownMessage(params, command, payloadBytes);
} }
@ -278,8 +278,8 @@ public class BitcoinSerializer extends MessageSerializer {
* serialization format support. * serialization format support.
*/ */
@Override @Override
public AddressV1Message makeAddressV1Message(byte[] payloadBytes, int length) throws ProtocolException { public AddressV1Message makeAddressV1Message(byte[] payloadBytes) throws ProtocolException {
return new AddressV1Message(params, payloadBytes, this, length); return new AddressV1Message(params, payloadBytes, this);
} }
/** /**
@ -287,8 +287,8 @@ public class BitcoinSerializer extends MessageSerializer {
* serialization format support. * serialization format support.
*/ */
@Override @Override
public AddressV2Message makeAddressV2Message(byte[] payloadBytes, int length) throws ProtocolException { public AddressV2Message makeAddressV2Message(byte[] payloadBytes) throws ProtocolException {
return new AddressV2Message(params, payloadBytes, this, length); return new AddressV2Message(params, payloadBytes, this);
} }
/** /**
@ -296,8 +296,8 @@ public class BitcoinSerializer extends MessageSerializer {
* serialization format support. * serialization format support.
*/ */
@Override @Override
public Block makeBlock(final byte[] payloadBytes, final int offset, final int length) throws ProtocolException { public Block makeBlock(byte[] payloadBytes, int offset) throws ProtocolException {
return new Block(params, payloadBytes, offset, this, length); return new Block(params, payloadBytes, offset, this);
} }
/** /**
@ -323,8 +323,8 @@ public class BitcoinSerializer extends MessageSerializer {
* serialization format support. * serialization format support.
*/ */
@Override @Override
public InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) throws ProtocolException { public InventoryMessage makeInventoryMessage(byte[] payloadBytes) throws ProtocolException {
return new InventoryMessage(params, payloadBytes, this, length); return new InventoryMessage(params, payloadBytes, this);
} }
/** /**
@ -332,9 +332,9 @@ public class BitcoinSerializer extends MessageSerializer {
* serialization format support. * serialization format support.
*/ */
@Override @Override
public Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader) public Transaction makeTransaction(byte[] payloadBytes, int offset, byte[] hashFromHeader)
throws ProtocolException { throws ProtocolException {
return new Transaction(params, payloadBytes, offset, null, this, length, hashFromHeader); return new Transaction(params, payloadBytes, offset, null, this, hashFromHeader);
} }
@Override @Override

View file

@ -155,13 +155,11 @@ public class Block extends Message {
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param payloadBytes the payload to extract the block from. * @param payloadBytes the payload to extract the block from.
* @param serializer the serializer to use for this message. * @param serializer the serializer to use for this message.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
public Block(NetworkParameters params, byte[] payloadBytes, MessageSerializer serializer, int length) public Block(NetworkParameters params, byte[] payloadBytes, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payloadBytes, 0, serializer, length); super(params, payloadBytes, 0, serializer);
} }
/** /**
@ -170,13 +168,11 @@ public class Block extends Message {
* @param payloadBytes the payload to extract the block from. * @param payloadBytes the payload to extract the block from.
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param serializer the serializer to use for this message. * @param serializer the serializer to use for this message.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
public Block(NetworkParameters params, byte[] payloadBytes, int offset, MessageSerializer serializer, int length) public Block(NetworkParameters params, byte[] payloadBytes, int offset, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payloadBytes, offset, serializer, length); super(params, payloadBytes, offset, serializer);
} }
/** /**
@ -188,14 +184,12 @@ public class Block extends Message {
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param parent The message element which contains this block, maybe null for no parent. * @param parent The message element which contains this block, maybe null for no parent.
* @param serializer the serializer to use for this block. * @param serializer the serializer to use for this block.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
public Block(NetworkParameters params, byte[] payloadBytes, int offset, @Nullable Message parent, MessageSerializer serializer, int length) public Block(NetworkParameters params, byte[] payloadBytes, int offset, @Nullable Message parent, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
// TODO: Keep the parent // TODO: Keep the parent
super(params, payloadBytes, offset, serializer, length); super(params, payloadBytes, offset, serializer);
} }
/** /**
@ -268,7 +262,7 @@ public class Block extends Message {
int numTransactions = numTransactionsVarInt.intValue(); int numTransactions = numTransactionsVarInt.intValue();
transactions = new ArrayList<>(Math.min(numTransactions, Utils.MAX_INITIAL_ARRAY_LENGTH)); transactions = new ArrayList<>(Math.min(numTransactions, Utils.MAX_INITIAL_ARRAY_LENGTH));
for (int i = 0; i < numTransactions; i++) { for (int i = 0; i < numTransactions; i++) {
Transaction tx = new Transaction(params, payload, cursor, this, serializer, UNKNOWN_LENGTH, null); Transaction tx = new Transaction(params, payload, cursor, this, serializer, null);
// Label the transaction as coming from the P2P network, so code that cares where we first saw it knows. // Label the transaction as coming from the P2P network, so code that cares where we first saw it knows.
tx.getConfidence().setSource(TransactionConfidence.Source.NETWORK); tx.getConfidence().setSource(TransactionConfidence.Source.NETWORK);
transactions.add(tx); transactions.add(tx);

View file

@ -37,8 +37,8 @@ public abstract class ChildMessage extends Message {
} }
public ChildMessage(NetworkParameters params, byte[] payload, int offset, @Nullable Message parent, public ChildMessage(NetworkParameters params, byte[] payload, int offset, @Nullable Message parent,
MessageSerializer serializer, int length) throws ProtocolException { MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, serializer, length); super(params, payload, offset, serializer);
this.parent = parent; this.parent = parent;
} }

View file

@ -64,17 +64,17 @@ class DummySerializer extends MessageSerializer {
} }
@Override @Override
public AddressV1Message makeAddressV1Message(byte[] payloadBytes, int length) throws UnsupportedOperationException { public AddressV1Message makeAddressV1Message(byte[] payloadBytes) throws UnsupportedOperationException {
throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE); throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE);
} }
@Override @Override
public AddressV2Message makeAddressV2Message(byte[] payloadBytes, int length) throws UnsupportedOperationException { public AddressV2Message makeAddressV2Message(byte[] payloadBytes) throws UnsupportedOperationException {
throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE); throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE);
} }
@Override @Override
public Block makeBlock(byte[] payloadBytes, int offset, int length) throws UnsupportedOperationException { public Block makeBlock(byte[] payloadBytes, int offset) throws UnsupportedOperationException {
throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE); throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE);
} }
@ -89,12 +89,12 @@ class DummySerializer extends MessageSerializer {
} }
@Override @Override
public InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) throws UnsupportedOperationException { public InventoryMessage makeInventoryMessage(byte[] payloadBytes) throws UnsupportedOperationException {
throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE); throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE);
} }
@Override @Override
public Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hash) throws UnsupportedOperationException { public Transaction makeTransaction(byte[] payloadBytes, int offset, byte[] hash) throws UnsupportedOperationException {
throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE); throw new UnsupportedOperationException(DEFAULT_EXCEPTION_MESSAGE);
} }

View file

@ -34,8 +34,8 @@ import java.math.BigInteger;
public class FeeFilterMessage extends Message { public class FeeFilterMessage extends Message {
private Coin feeRate; private Coin feeRate;
public FeeFilterMessage(NetworkParameters params, byte[] payloadBytes, BitcoinSerializer serializer, int length) { public FeeFilterMessage(NetworkParameters params, byte[] payloadBytes, BitcoinSerializer serializer) {
super(params, payloadBytes, 0, serializer, length); super(params, payloadBytes, 0, serializer);
} }
@Override @Override

View file

@ -36,13 +36,11 @@ public class GetDataMessage extends ListMessage {
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param payload Bitcoin protocol formatted byte array containing message content. * @param payload Bitcoin protocol formatted byte array containing message content.
* @param serializer the serializer to use for this message. * @param serializer the serializer to use for this message.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
public GetDataMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer, int length) public GetDataMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payload, serializer, length); super(params, payload, serializer);
} }
public GetDataMessage(NetworkParameters params) { public GetDataMessage(NetworkParameters params) {

View file

@ -76,7 +76,7 @@ public class HeadersMessage extends Message {
final BitcoinSerializer serializer = this.params.getSerializer(true); final BitcoinSerializer serializer = this.params.getSerializer(true);
for (int i = 0; i < numHeaders; ++i) { for (int i = 0; i < numHeaders; ++i) {
final Block newBlockHeader = serializer.makeBlock(payload, cursor, UNKNOWN_LENGTH); final Block newBlockHeader = serializer.makeBlock(payload, cursor);
if (newBlockHeader.hasTransactions()) { if (newBlockHeader.hasTransactions()) {
throw new ProtocolException("Block header does not end with a null byte"); throw new ProtocolException("Block header does not end with a null byte");
} }

View file

@ -40,13 +40,11 @@ public class InventoryMessage extends ListMessage {
* @param params NetworkParameters object. * @param params NetworkParameters object.
* @param payload Bitcoin protocol formatted byte array containing message content. * @param payload Bitcoin protocol formatted byte array containing message content.
* @param serializer the serializer to use for this message. * @param serializer the serializer to use for this message.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
public InventoryMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer, int length) public InventoryMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payload, serializer, length); super(params, payload, serializer);
} }
public InventoryMessage(NetworkParameters params) { public InventoryMessage(NetworkParameters params) {

View file

@ -43,9 +43,9 @@ public abstract class ListMessage extends Message {
super(params, bytes, 0); super(params, bytes, 0);
} }
public ListMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer, int length) public ListMessage(NetworkParameters params, byte[] payload, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payload, 0, serializer, length); super(params, payload, 0, serializer);
} }
public ListMessage(NetworkParameters params) { public ListMessage(NetworkParameters params) {

View file

@ -79,16 +79,14 @@ public abstract class Message {
* @param payload Bitcoin protocol formatted byte array containing message content. * @param payload Bitcoin protocol formatted byte array containing message content.
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param serializer the serializer to use for this message. * @param serializer the serializer to use for this message.
* @param length The length of message payload if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @throws ProtocolException * @throws ProtocolException
*/ */
protected Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException { protected Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException {
this.serializer = serializer; this.serializer = serializer;
this.params = params; this.params = params;
this.payload = payload; this.payload = payload;
this.cursor = this.offset = offset; this.cursor = this.offset = offset;
this.length = length; this.length = payload.length;
parse(); parse();
@ -102,7 +100,7 @@ public abstract class Message {
} }
protected Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException { protected Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException {
this(params, payload, offset, params.getDefaultSerializer(), UNKNOWN_LENGTH); this(params, payload, offset, params.getDefaultSerializer());
} }
// These methods handle the serialization/deserialization using the custom Bitcoin protocol. // These methods handle the serialization/deserialization using the custom Bitcoin protocol.

View file

@ -65,36 +65,26 @@ public abstract class MessageSerializer {
* Make an address message from the payload. Extension point for alternative * Make an address message from the payload. Extension point for alternative
* serialization format support. * serialization format support.
*/ */
public abstract AddressV1Message makeAddressV1Message(byte[] payloadBytes, int length) throws ProtocolException, UnsupportedOperationException; public abstract AddressV1Message makeAddressV1Message(byte[] payloadBytes) throws ProtocolException, UnsupportedOperationException;
/** /**
* Make an address message from the payload. Extension point for alternative * Make an address message from the payload. Extension point for alternative
* serialization format support. * serialization format support.
*/ */
public abstract AddressV2Message makeAddressV2Message(byte[] payloadBytes, int length) throws ProtocolException, UnsupportedOperationException; public abstract AddressV2Message makeAddressV2Message(byte[] payloadBytes) throws ProtocolException, UnsupportedOperationException;
/** /**
* Make a block from the payload, using an offset of zero and the payload * Make a block from the payload, using an offset of zero.
* length as block length.
*/ */
public final Block makeBlock(byte[] payloadBytes) throws ProtocolException { public final Block makeBlock(byte[] payloadBytes) throws ProtocolException {
return makeBlock(payloadBytes, 0, payloadBytes.length); return makeBlock(payloadBytes, 0);
} }
/** /**
* Make a block from the payload, using an offset of zero and the provided * Make a block from the payload, using an offset of zero. Extension point for alternative
* length as block length.
*/
public final Block makeBlock(byte[] payloadBytes, int length) throws ProtocolException {
return makeBlock(payloadBytes, 0, length);
}
/**
* Make a block from the payload, using an offset of zero and the provided
* length as block length. Extension point for alternative
* serialization format support. * serialization format support.
*/ */
public abstract Block makeBlock(final byte[] payloadBytes, final int offset, final int length) throws ProtocolException, UnsupportedOperationException; public abstract Block makeBlock(byte[] payloadBytes, int offset) throws ProtocolException, UnsupportedOperationException;
/** /**
* Make an filter message from the payload. Extension point for alternative * Make an filter message from the payload. Extension point for alternative
@ -112,7 +102,7 @@ public abstract class MessageSerializer {
* Make an inventory message from the payload. Extension point for alternative * Make an inventory message from the payload. Extension point for alternative
* serialization format support. * serialization format support.
*/ */
public abstract InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) throws ProtocolException, UnsupportedOperationException; public abstract InventoryMessage makeInventoryMessage(byte[] payloadBytes) throws ProtocolException, UnsupportedOperationException;
/** /**
* Make a transaction from the payload. Extension point for alternative * Make a transaction from the payload. Extension point for alternative
@ -123,7 +113,7 @@ public abstract class MessageSerializer {
* serializer (i.e. for messages with no network parameters), or because * serializer (i.e. for messages with no network parameters), or because
* it does not support deserializing transactions. * it does not support deserializing transactions.
*/ */
public abstract Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hash) throws ProtocolException, UnsupportedOperationException; public abstract Transaction makeTransaction(byte[] payloadBytes, int offset, byte[] hash) throws ProtocolException, UnsupportedOperationException;
/** /**
* Make a transaction from the payload. Extension point for alternative * Make a transaction from the payload. Extension point for alternative
@ -148,7 +138,7 @@ public abstract class MessageSerializer {
* it does not support deserializing transactions. * it does not support deserializing transactions.
*/ */
public final Transaction makeTransaction(byte[] payloadBytes, int offset) throws ProtocolException { public final Transaction makeTransaction(byte[] payloadBytes, int offset) throws ProtocolException {
return makeTransaction(payloadBytes, offset, payloadBytes.length, null); return makeTransaction(payloadBytes, offset, null);
} }
public abstract void seekPastMagicBytes(ByteBuffer in) throws BufferUnderflowException; public abstract void seekPastMagicBytes(ByteBuffer in) throws BufferUnderflowException;

View file

@ -89,7 +89,7 @@ public class PeerAddress extends ChildMessage {
* @throws ProtocolException * @throws ProtocolException
*/ */
public PeerAddress(NetworkParameters params, byte[] payload, int offset, Message parent, MessageSerializer serializer) throws ProtocolException { public PeerAddress(NetworkParameters params, byte[] payload, int offset, Message parent, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, parent, serializer, UNKNOWN_LENGTH); super(params, payload, offset, parent, serializer);
} }
/** /**

View file

@ -259,16 +259,14 @@ public class Transaction extends ChildMessage {
* @param offset The location of the first payload byte within the array. * @param offset The location of the first payload byte within the array.
* @param parent The parent of the transaction. * @param parent The parent of the transaction.
* @param setSerializer The serializer to use for this transaction. * @param setSerializer The serializer to use for this transaction.
* @param length The length of message if known. Usually this is provided when deserializing of the wire
* as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
* @param hashFromHeader Used by BitcoinSerializer. The serializer has to calculate a hash for checksumming so to * @param hashFromHeader Used by BitcoinSerializer. The serializer has to calculate a hash for checksumming so to
* avoid wasting the considerable effort a set method is provided so the serializer can set it. No verification * avoid wasting the considerable effort a set method is provided so the serializer can set it. No verification
* is performed on this hash. * is performed on this hash.
* @throws ProtocolException * @throws ProtocolException
*/ */
public Transaction(NetworkParameters params, byte[] payload, int offset, @Nullable Message parent, public Transaction(NetworkParameters params, byte[] payload, int offset, @Nullable Message parent,
MessageSerializer setSerializer, int length, @Nullable byte[] hashFromHeader) throws ProtocolException { MessageSerializer setSerializer, @Nullable byte[] hashFromHeader) throws ProtocolException {
super(params, payload, offset, parent, setSerializer, length); super(params, payload, offset, parent, setSerializer);
if (hashFromHeader != null) { if (hashFromHeader != null) {
cachedWTxId = Sha256Hash.wrapReversed(hashFromHeader); cachedWTxId = Sha256Hash.wrapReversed(hashFromHeader);
if (!hasWitnesses()) if (!hasWitnesses())
@ -279,9 +277,9 @@ public class Transaction extends ChildMessage {
/** /**
* Creates a transaction by reading payload. Length of a transaction is fixed. * Creates a transaction by reading payload. Length of a transaction is fixed.
*/ */
public Transaction(NetworkParameters params, byte[] payload, @Nullable Message parent, MessageSerializer setSerializer, int length) public Transaction(NetworkParameters params, byte[] payload, @Nullable Message parent, MessageSerializer setSerializer)
throws ProtocolException { throws ProtocolException {
super(params, payload, 0, parent, setSerializer, length); super(params, payload, 0, parent, setSerializer);
} }
/** /**

View file

@ -145,7 +145,7 @@ public class TransactionInput extends ChildMessage {
*/ */
public TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] payload, int offset, MessageSerializer serializer) public TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] payload, int offset, MessageSerializer serializer)
throws ProtocolException { throws ProtocolException {
super(params, payload, offset, parentTransaction, serializer, UNKNOWN_LENGTH); super(params, payload, offset, parentTransaction, serializer);
this.value = null; this.value = null;
} }

View file

@ -96,7 +96,7 @@ public class TransactionOutPoint extends ChildMessage {
* @throws ProtocolException * @throws ProtocolException
*/ */
public TransactionOutPoint(NetworkParameters params, byte[] payload, int offset, Message parent, MessageSerializer serializer) throws ProtocolException { public TransactionOutPoint(NetworkParameters params, byte[] payload, int offset, Message parent, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, parent, serializer, MESSAGE_LENGTH); super(params, payload, offset, parent, serializer);
} }
@Override @Override

View file

@ -88,7 +88,7 @@ public class TransactionOutput extends ChildMessage {
* @throws ProtocolException * @throws ProtocolException
*/ */
public TransactionOutput(NetworkParameters params, @Nullable Transaction parent, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException { public TransactionOutput(NetworkParameters params, @Nullable Transaction parent, byte[] payload, int offset, MessageSerializer serializer) throws ProtocolException {
super(params, payload, offset, parent, serializer, UNKNOWN_LENGTH); super(params, payload, offset, parent, serializer);
availableForSpending = true; availableForSpending = true;
} }

View file

@ -367,7 +367,7 @@ public class BlockTest {
}; };
byte[] serializedBlock = block.bitcoinSerialize(); byte[] serializedBlock = block.bitcoinSerialize();
try { try {
TESTNET.getDefaultSerializer().makeBlock(serializedBlock, serializedBlock.length); TESTNET.getDefaultSerializer().makeBlock(serializedBlock);
fail("We expect ProtocolException with the fixed code and OutOfMemoryError with the buggy code, so this is weird"); fail("We expect ProtocolException with the fixed code and OutOfMemoryError with the buggy code, so this is weird");
} catch (ProtocolException e) { } catch (ProtocolException e) {
//Expected, do nothing //Expected, do nothing

View file

@ -1228,7 +1228,7 @@ public class FullBlockTestGenerator {
for (Transaction transaction : b64Original.block.getTransactions()) for (Transaction transaction : b64Original.block.getTransactions())
transaction.bitcoinSerialize(stream); transaction.bitcoinSerialize(stream);
b64 = params.getSerializer(true).makeBlock(stream.toByteArray(), stream.size()); b64 = params.getSerializer(true).makeBlock(stream.toByteArray());
// The following checks are checking to ensure block serialization functions in the way needed for this test // The following checks are checking to ensure block serialization functions in the way needed for this test
// If they fail, it is likely not an indication of error, but an indication that this test needs rewritten // If they fail, it is likely not an indication of error, but an indication that this test needs rewritten