mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-19 05:33:44 +01:00
Message: fold bitcoinSerialize()
into bitcoinSerializeToStream()
This commit is contained in:
parent
01bf08159d
commit
e882322505
@ -39,7 +39,7 @@ public abstract class AddressMessage extends Message {
|
|||||||
return;
|
return;
|
||||||
stream.write(VarInt.of(addresses.size()).encode());
|
stream.write(VarInt.of(addresses.size()).encode());
|
||||||
for (PeerAddress addr : addresses) {
|
for (PeerAddress addr : addresses) {
|
||||||
addr.bitcoinSerialize(stream);
|
addr.bitcoinSerializeToStream(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,52 +321,16 @@ public class Block extends Message {
|
|||||||
|
|
||||||
stream.write(VarInt.of(transactions.size()).encode());
|
stream.write(VarInt.of(transactions.size()).encode());
|
||||||
for (Transaction tx : transactions) {
|
for (Transaction tx : transactions) {
|
||||||
tx.bitcoinSerialize(stream);
|
tx.bitcoinSerializeToStream(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Special handling to check if we have a valid byte array for both header
|
|
||||||
* and transactions
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public byte[] bitcoinSerialize() {
|
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream(length == UNKNOWN_LENGTH ? HEADER_SIZE + guessTransactionsLength() : length);
|
|
||||||
try {
|
|
||||||
writeHeader(stream);
|
|
||||||
writeTransactions(stream);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Cannot happen, we are serializing to a memory stream.
|
|
||||||
}
|
|
||||||
return stream.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||||
writeHeader(stream);
|
writeHeader(stream);
|
||||||
// We may only have enough data to write the header.
|
|
||||||
writeTransactions(stream);
|
writeTransactions(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides a reasonable guess at the byte length of the transactions part of the block.
|
|
||||||
* The returned value will be accurate in 99% of cases and in those cases where not will probably slightly
|
|
||||||
* oversize.
|
|
||||||
*
|
|
||||||
* This is used to preallocate the underlying byte array for a ByteArrayOutputStream. If the size is under the
|
|
||||||
* real value the only penalty is resizing of the underlying byte array.
|
|
||||||
*/
|
|
||||||
private int guessTransactionsLength() {
|
|
||||||
if (transactions == null)
|
|
||||||
return 0;
|
|
||||||
int len = VarInt.sizeOf(transactions.size());
|
|
||||||
for (Transaction tx : transactions) {
|
|
||||||
// 255 is just a guess at an average tx length
|
|
||||||
len += tx.length == UNKNOWN_LENGTH ? 255 : tx.length;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void unCache() {
|
protected void unCache() {
|
||||||
// Since we have alternate uncache methods to use internally this will only ever be called by a child
|
// Since we have alternate uncache methods to use internally this will only ever be called by a child
|
||||||
|
@ -50,12 +50,4 @@ public abstract class EmptyMessage extends Message {
|
|||||||
@Override
|
@Override
|
||||||
protected void parse() throws ProtocolException {
|
protected void parse() throws ProtocolException {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see Message#bitcoinSerialize()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public byte[] bitcoinSerialize() {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -186,16 +186,6 @@ public abstract class Message {
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Serialize this message to the provided OutputStream using the bitcoin wire format.
|
|
||||||
*
|
|
||||||
* @param stream
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public final void bitcoinSerialize(OutputStream stream) throws IOException {
|
|
||||||
bitcoinSerializeToStream(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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().
|
||||||
*/
|
*/
|
||||||
|
@ -1553,11 +1553,11 @@ public class Transaction extends ChildMessage {
|
|||||||
// txin_count, txins
|
// txin_count, txins
|
||||||
stream.write(VarInt.of(inputs.size()).encode());
|
stream.write(VarInt.of(inputs.size()).encode());
|
||||||
for (TransactionInput in : inputs)
|
for (TransactionInput in : inputs)
|
||||||
in.bitcoinSerialize(stream);
|
in.bitcoinSerializeToStream(stream);
|
||||||
// txout_count, txouts
|
// txout_count, txouts
|
||||||
stream.write(VarInt.of(outputs.size()).encode());
|
stream.write(VarInt.of(outputs.size()).encode());
|
||||||
for (TransactionOutput out : outputs)
|
for (TransactionOutput out : outputs)
|
||||||
out.bitcoinSerialize(stream);
|
out.bitcoinSerializeToStream(stream);
|
||||||
// script_witnisses
|
// script_witnisses
|
||||||
if (useSegwit) {
|
if (useSegwit) {
|
||||||
for (TransactionInput in : inputs) {
|
for (TransactionInput in : inputs) {
|
||||||
|
@ -172,7 +172,7 @@ public class TransactionInput extends ChildMessage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||||
outpoint.bitcoinSerialize(stream);
|
outpoint.bitcoinSerializeToStream(stream);
|
||||||
stream.write(VarInt.of(scriptBytes.length).encode());
|
stream.write(VarInt.of(scriptBytes.length).encode());
|
||||||
stream.write(scriptBytes);
|
stream.write(scriptBytes);
|
||||||
ByteUtils.writeUint32LE(sequence, stream);
|
ByteUtils.writeUint32LE(sequence, stream);
|
||||||
|
@ -356,16 +356,6 @@ public class BlockTest {
|
|||||||
|
|
||||||
stream.write(VarInt.of(Integer.MAX_VALUE).encode());
|
stream.write(VarInt.of(Integer.MAX_VALUE).encode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] bitcoinSerialize() {
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
try {
|
|
||||||
bitcoinSerializeToStream(baos);
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
byte[] serializedBlock = block.bitcoinSerialize();
|
byte[] serializedBlock = block.bitcoinSerialize();
|
||||||
try {
|
try {
|
||||||
|
@ -1228,7 +1228,7 @@ public class FullBlockTestGenerator {
|
|||||||
checkState(VarInt.ofBytes(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size());
|
checkState(VarInt.ofBytes(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size());
|
||||||
|
|
||||||
for (Transaction transaction : b64Original.block.getTransactions())
|
for (Transaction transaction : b64Original.block.getTransactions())
|
||||||
transaction.bitcoinSerialize(stream);
|
transaction.bitcoinSerializeToStream(stream);
|
||||||
b64 = params.getSerializer().makeBlock(ByteBuffer.wrap(stream.toByteArray()));
|
b64 = params.getSerializer().makeBlock(ByteBuffer.wrap(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
|
||||||
|
@ -727,12 +727,12 @@ public class TransactionTest {
|
|||||||
long inputsSize = hackInputsSize ? Integer.MAX_VALUE : getInputs().size();
|
long inputsSize = hackInputsSize ? Integer.MAX_VALUE : getInputs().size();
|
||||||
stream.write(VarInt.of(inputsSize).encode());
|
stream.write(VarInt.of(inputsSize).encode());
|
||||||
for (TransactionInput in : getInputs())
|
for (TransactionInput in : getInputs())
|
||||||
in.bitcoinSerialize(stream);
|
in.bitcoinSerializeToStream(stream);
|
||||||
// txout_count, txouts
|
// txout_count, txouts
|
||||||
long outputsSize = hackOutputsSize ? Integer.MAX_VALUE : getOutputs().size();
|
long outputsSize = hackOutputsSize ? Integer.MAX_VALUE : getOutputs().size();
|
||||||
stream.write(VarInt.of(outputsSize).encode());
|
stream.write(VarInt.of(outputsSize).encode());
|
||||||
for (TransactionOutput out : getOutputs())
|
for (TransactionOutput out : getOutputs())
|
||||||
out.bitcoinSerialize(stream);
|
out.bitcoinSerializeToStream(stream);
|
||||||
// script_witnisses
|
// script_witnisses
|
||||||
if (useSegwit) {
|
if (useSegwit) {
|
||||||
for (TransactionInput in : getInputs()) {
|
for (TransactionInput in : getInputs()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user