mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 17:26:28 +01:00
Message: get rid of payload
field
It's now passed into the `parse()` method, and passed along from there.
This commit is contained in:
parent
9c9548dc23
commit
b9d37e024f
24 changed files with 37 additions and 42 deletions
|
@ -48,7 +48,7 @@ public class AddressV1Message extends AddressMessage {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
final VarInt numAddressesVarInt = VarInt.read(payload);
|
||||
int numAddresses = numAddressesVarInt.intValue();
|
||||
// Guard against ultra large messages that will crash us.
|
||||
|
|
|
@ -48,7 +48,7 @@ public class AddressV2Message extends AddressMessage {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
final VarInt numAddressesVarInt = VarInt.read(payload);
|
||||
int numAddresses = numAddressesVarInt.intValue();
|
||||
// Guard against ultra large messages that will crash us.
|
||||
|
|
|
@ -221,7 +221,7 @@ public class Block extends Message {
|
|||
/**
|
||||
* Parse transactions from the block.
|
||||
*/
|
||||
protected void parseTransactions() throws ProtocolException {
|
||||
protected void parseTransactions(ByteBuffer payload) throws ProtocolException {
|
||||
VarInt numTransactionsVarInt = VarInt.read(payload);
|
||||
int numTransactions = numTransactionsVarInt.intValue();
|
||||
transactions = new ArrayList<>(Math.min(numTransactions, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
||||
|
@ -234,7 +234,7 @@ public class Block extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
// header
|
||||
payload.mark();
|
||||
version = ByteUtils.readUint32(payload);
|
||||
|
@ -248,7 +248,7 @@ public class Block extends Message {
|
|||
|
||||
// transactions
|
||||
if (payload.hasRemaining()) // otherwise this message is just a header
|
||||
parseTransactions();
|
||||
parseTransactions(payload);
|
||||
}
|
||||
|
||||
public static Block createGenesis(NetworkParameters n) {
|
||||
|
|
|
@ -149,7 +149,7 @@ public class BloomFilter extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
data = Buffers.readLengthPrefixedBytes(payload);
|
||||
if (data.length > MAX_FILTER_SIZE)
|
||||
throw new ProtocolException ("Bloom filter out of size range.");
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.bitcoinj.core;
|
|||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* <p>Parent class for header only messages that don't have a payload.
|
||||
|
@ -43,6 +44,6 @@ public abstract class EmptyMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public class FeeFilterMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
feeRate = Coin.ofSat(ByteUtils.readInt64(payload));
|
||||
check(feeRate.signum() >= 0, () -> new ProtocolException("fee rate out of range: " + feeRate));
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class FilteredBlock extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
byte[] headerBytes = Buffers.readBytes(payload, Block.HEADER_SIZE);
|
||||
header = new Block(params, ByteBuffer.wrap(headerBytes));
|
||||
merkleTree = new PartialMerkleTree(params, payload);
|
||||
|
|
|
@ -49,7 +49,7 @@ public class GetBlocksMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
version = ByteUtils.readUint32(payload);
|
||||
int startCount = VarInt.read(payload).intValue();
|
||||
if (startCount > 500)
|
||||
|
|
|
@ -68,7 +68,7 @@ public class HeadersMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
int numHeaders = VarInt.read(payload).intValue();
|
||||
if (numHeaders > MAX_HEADERS)
|
||||
throw new ProtocolException("Too many headers: got " + numHeaders + " which is larger than " +
|
||||
|
|
|
@ -66,7 +66,7 @@ public abstract class ListMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
long arrayLen = VarInt.read(payload).longValue();
|
||||
if (arrayLen > MAX_INVENTORY_ITEMS)
|
||||
throw new ProtocolException("Too many items in INV message: " + arrayLen);
|
||||
|
|
|
@ -41,9 +41,6 @@ public abstract class Message {
|
|||
|
||||
public static final int MAX_SIZE = 0x02000000; // 32MB
|
||||
|
||||
// The raw message payload bytes themselves.
|
||||
protected ByteBuffer payload;
|
||||
|
||||
protected final MessageSerializer serializer;
|
||||
|
||||
@Nullable
|
||||
|
@ -74,15 +71,12 @@ public abstract class Message {
|
|||
protected Message(NetworkParameters params, ByteBuffer payload, MessageSerializer serializer) throws ProtocolException {
|
||||
this.serializer = serializer;
|
||||
this.params = params;
|
||||
this.payload = payload;
|
||||
|
||||
try {
|
||||
parse();
|
||||
parse(payload);
|
||||
} catch(BufferUnderflowException e) {
|
||||
throw new ProtocolException(e);
|
||||
}
|
||||
|
||||
this.payload = null;
|
||||
}
|
||||
|
||||
protected Message(ByteBuffer payload) throws ProtocolException {
|
||||
|
@ -95,7 +89,7 @@ public abstract class Message {
|
|||
|
||||
// These methods handle the serialization/deserialization using the custom Bitcoin protocol.
|
||||
|
||||
protected abstract void parse() throws BufferUnderflowException, ProtocolException;
|
||||
protected abstract void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException;
|
||||
|
||||
/**
|
||||
* <p>To be called before any change of internal values including any setters. This ensures any cached byte array is
|
||||
|
|
|
@ -123,7 +123,7 @@ public class PartialMerkleTree extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
transactionCount = (int) ByteUtils.readUint32(payload);
|
||||
|
||||
int nHashes = VarInt.read(payload).intValue();
|
||||
|
|
|
@ -229,7 +229,7 @@ public class PeerAddress extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
int protocolVersion = serializer.getProtocolVersion();
|
||||
if (protocolVersion < 0 || protocolVersion > 2)
|
||||
throw new IllegalStateException("invalid protocolVersion: " + protocolVersion);
|
||||
|
|
|
@ -57,7 +57,7 @@ public class Ping extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
nonce = ByteUtils.readInt64(payload);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class Pong extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
nonce = ByteUtils.readInt64(payload);
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ public class RejectMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
message = Buffers.readLengthPrefixedString(payload);
|
||||
code = RejectCode.fromCode(payload.get());
|
||||
reason = Buffers.readLengthPrefixedString(payload);
|
||||
|
|
|
@ -647,7 +647,7 @@ public class Transaction extends Message {
|
|||
* transaction is segwit or not.
|
||||
*/
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
boolean allowWitness = allowWitness();
|
||||
|
||||
|
||||
|
@ -655,27 +655,27 @@ public class Transaction extends Message {
|
|||
version = ByteUtils.readUint32(payload);
|
||||
byte flags = 0;
|
||||
// Try to parse the inputs. In case the dummy is there, this will be read as an empty array list.
|
||||
parseInputs();
|
||||
parseInputs(payload);
|
||||
if (inputs.size() == 0 && allowWitness) {
|
||||
// We read a dummy or an empty input
|
||||
flags = payload.get();
|
||||
|
||||
if (flags != 0) {
|
||||
parseInputs();
|
||||
parseOutputs();
|
||||
parseInputs(payload);
|
||||
parseOutputs(payload);
|
||||
} else {
|
||||
outputs = new ArrayList<>(0);
|
||||
}
|
||||
} else {
|
||||
// We read non-empty inputs. Assume normal outputs follows.
|
||||
parseOutputs();
|
||||
parseOutputs(payload);
|
||||
}
|
||||
|
||||
if (((flags & 1) != 0) && allowWitness) {
|
||||
// The witness flag is present, and we support witnesses.
|
||||
flags ^= 1;
|
||||
// script_witnesses
|
||||
parseWitnesses();
|
||||
parseWitnesses(payload);
|
||||
if (!hasWitnesses()) {
|
||||
// It's illegal to encode witnesses when all witness stacks are empty.
|
||||
throw new ProtocolException("Superfluous witness record");
|
||||
|
@ -689,7 +689,7 @@ public class Transaction extends Message {
|
|||
vLockTime = LockTime.of(ByteUtils.readUint32(payload));
|
||||
}
|
||||
|
||||
private void parseInputs() {
|
||||
private void parseInputs(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
VarInt numInputsVarInt = VarInt.read(payload);
|
||||
int numInputs = numInputsVarInt.intValue();
|
||||
inputs = new ArrayList<>(Math.min((int) numInputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
||||
|
@ -704,7 +704,7 @@ public class Transaction extends Message {
|
|||
}
|
||||
}
|
||||
|
||||
private void parseOutputs() {
|
||||
private void parseOutputs(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
VarInt numOutputsVarInt = VarInt.read(payload);
|
||||
int numOutputs = numOutputsVarInt.intValue();
|
||||
outputs = new ArrayList<>(Math.min((int) numOutputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
||||
|
@ -719,7 +719,7 @@ public class Transaction extends Message {
|
|||
}
|
||||
}
|
||||
|
||||
private void parseWitnesses() {
|
||||
private void parseWitnesses(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
int numWitnesses = inputs.size();
|
||||
for (int i = 0; i < numWitnesses; i++) {
|
||||
VarInt pushCountVarInt = VarInt.read(payload);
|
||||
|
|
|
@ -153,7 +153,7 @@ public class TransactionInput extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
outpoint = new TransactionOutPoint(payload);
|
||||
int scriptLen = VarInt.read(payload).intValue();
|
||||
scriptBytes = Buffers.readBytes(payload, scriptLen);
|
||||
|
|
|
@ -94,7 +94,7 @@ public class TransactionOutPoint extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
hash = Sha256Hash.read(payload);
|
||||
index = ByteUtils.readUint32(payload);
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ public class TransactionOutput extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
value = ByteUtils.readInt64(payload);
|
||||
// Negative values obviously make no sense, except for -1 which is used as a sentinel value when calculating
|
||||
// SIGHASH_SINGLE signatures, so unfortunately we have to allow that here.
|
||||
|
|
|
@ -112,7 +112,7 @@ public class VersionMessage extends Message {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
clientVersion = (int) ByteUtils.readUint32(payload);
|
||||
localServices = Services.read(payload);
|
||||
time = Instant.ofEpochSecond(ByteUtils.readInt64(payload));
|
||||
|
|
|
@ -233,7 +233,7 @@ public class BitcoinSerializerTest {
|
|||
|
||||
Message unknownMessage = new Message() {
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
}
|
||||
};
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(ADDRESS_MESSAGE_BYTES.length);
|
||||
|
|
|
@ -42,7 +42,7 @@ public class MessageTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
Buffers.readLengthPrefixedString(payload);
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ public class MessageTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void parse() throws BufferUnderflowException, ProtocolException {
|
||||
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
|
||||
Buffers.readLengthPrefixedBytes(payload);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ public class PeerAddressTest {
|
|||
public void equalsContract() {
|
||||
EqualsVerifier.forClass(PeerAddress.class)
|
||||
.suppress(Warning.NONFINAL_FIELDS)
|
||||
.withIgnoredFields("time", "params", "payload", "serializer")
|
||||
.withIgnoredFields("time", "params", "serializer")
|
||||
.usingGetClass()
|
||||
.verify();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue