ByteUtils: rename all writeUInt*() helpers to writeInt*()

This creates overloads in some cases.
This commit is contained in:
Andreas Schildbach 2023-03-24 18:51:35 +01:00
parent f3c4b5fcf4
commit 47a5d73035
19 changed files with 79 additions and 80 deletions

View File

@ -174,11 +174,11 @@ public class VarInt {
break; break;
case 3: case 3:
buf.put((byte) 253); buf.put((byte) 253);
ByteUtils.writeUint16LE((int) value, buf); ByteUtils.writeInt16LE((int) value, buf);
break; break;
case 5: case 5:
buf.put((byte) 254); buf.put((byte) 254);
ByteUtils.writeUint32LE(value, buf); ByteUtils.writeInt32LE(value, buf);
break; break;
default: default:
buf.put((byte) 255); buf.put((byte) 255);

View File

@ -110,7 +110,7 @@ public class ByteUtils {
* @return the buffer * @return the buffer
* @throws BufferOverflowException if the value doesn't fit the remaining buffer * @throws BufferOverflowException if the value doesn't fit the remaining buffer
*/ */
public static ByteBuffer writeUint16LE(int val, ByteBuffer buf) throws BufferOverflowException { public static ByteBuffer writeInt16LE(int val, ByteBuffer buf) throws BufferOverflowException {
return buf.order(ByteOrder.LITTLE_ENDIAN).putShort((short) val); return buf.order(ByteOrder.LITTLE_ENDIAN).putShort((short) val);
} }
@ -122,10 +122,10 @@ public class ByteUtils {
* @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or * @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or
* if the value doesn't fit the remaining buffer * if the value doesn't fit the remaining buffer
*/ */
public static void writeUint16LE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException { public static void writeInt16LE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException {
check(offset >= 0 && offset <= out.length - 2, () -> check(offset >= 0 && offset <= out.length - 2, () ->
new ArrayIndexOutOfBoundsException(offset)); new ArrayIndexOutOfBoundsException(offset));
writeUint16LE(val, ByteBuffer.wrap(out, offset, out.length - offset)); writeInt16LE(val, ByteBuffer.wrap(out, offset, out.length - offset));
} }
/** /**
@ -135,7 +135,7 @@ public class ByteUtils {
* @return the buffer * @return the buffer
* @throws BufferOverflowException if the value doesn't fit the remaining buffer * @throws BufferOverflowException if the value doesn't fit the remaining buffer
*/ */
public static ByteBuffer writeUint16BE(int val, ByteBuffer buf) throws BufferOverflowException { public static ByteBuffer writeInt16BE(int val, ByteBuffer buf) throws BufferOverflowException {
return buf.order(ByteOrder.BIG_ENDIAN).putShort((short) val); return buf.order(ByteOrder.BIG_ENDIAN).putShort((short) val);
} }
@ -147,10 +147,10 @@ public class ByteUtils {
* @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or * @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or
* if the value doesn't fit the remaining buffer * if the value doesn't fit the remaining buffer
*/ */
public static void writeUint16BE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException { public static void writeInt16BE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException {
check(offset >= 0 && offset <= out.length - 2, () -> check(offset >= 0 && offset <= out.length - 2, () ->
new ArrayIndexOutOfBoundsException(offset)); new ArrayIndexOutOfBoundsException(offset));
writeUint16BE(val, ByteBuffer.wrap(out, offset, out.length - offset)); writeInt16BE(val, ByteBuffer.wrap(out, offset, out.length - offset));
} }
/** /**
@ -171,7 +171,7 @@ public class ByteUtils {
* @return the buffer * @return the buffer
* @throws BufferOverflowException if the value doesn't fit the remaining buffer * @throws BufferOverflowException if the value doesn't fit the remaining buffer
*/ */
public static ByteBuffer writeUint32LE(long val, ByteBuffer buf) throws BufferOverflowException { public static ByteBuffer writeInt32LE(long val, ByteBuffer buf) throws BufferOverflowException {
return buf.order(ByteOrder.LITTLE_ENDIAN).putInt((int) val); return buf.order(ByteOrder.LITTLE_ENDIAN).putInt((int) val);
} }
@ -183,10 +183,10 @@ public class ByteUtils {
* @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or * @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or
* if the value doesn't fit the remaining buffer * if the value doesn't fit the remaining buffer
*/ */
public static void writeUint32LE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException { public static void writeInt32LE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException {
check(offset >= 0 && offset <= out.length - 4, () -> check(offset >= 0 && offset <= out.length - 4, () ->
new ArrayIndexOutOfBoundsException(offset)); new ArrayIndexOutOfBoundsException(offset));
writeUint32LE(val, ByteBuffer.wrap(out, offset, out.length - offset)); writeInt32LE(val, ByteBuffer.wrap(out, offset, out.length - offset));
} }
/** /**
@ -196,7 +196,7 @@ public class ByteUtils {
* @return the buffer * @return the buffer
* @throws BufferOverflowException if the value doesn't fit the remaining buffer * @throws BufferOverflowException if the value doesn't fit the remaining buffer
*/ */
public static ByteBuffer writeUint32BE(long val, ByteBuffer buf) throws BufferOverflowException { public static ByteBuffer writeInt32BE(long val, ByteBuffer buf) throws BufferOverflowException {
return buf.order(ByteOrder.BIG_ENDIAN).putInt((int) val); return buf.order(ByteOrder.BIG_ENDIAN).putInt((int) val);
} }
@ -208,10 +208,10 @@ public class ByteUtils {
* @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or * @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or
* if the value doesn't fit the remaining buffer * if the value doesn't fit the remaining buffer
*/ */
public static void writeUint32BE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException { public static void writeInt32BE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException {
check(offset >= 0 && offset <= out.length - 4, () -> check(offset >= 0 && offset <= out.length - 4, () ->
new ArrayIndexOutOfBoundsException(offset)); new ArrayIndexOutOfBoundsException(offset));
writeUint32BE(val, ByteBuffer.wrap(out, offset, out.length - offset)); writeInt32BE(val, ByteBuffer.wrap(out, offset, out.length - offset));
} }
/** /**
@ -245,9 +245,9 @@ public class ByteUtils {
* @param stream strean to be written into * @param stream strean to be written into
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static void writeUint16LE(int val, OutputStream stream) throws IOException { public static void writeInt16LE(int val, OutputStream stream) throws IOException {
byte[] buf = new byte[2]; byte[] buf = new byte[2];
writeUint16LE(val, ByteBuffer.wrap(buf)); writeInt16LE(val, ByteBuffer.wrap(buf));
stream.write(buf); stream.write(buf);
} }
@ -257,9 +257,9 @@ public class ByteUtils {
* @param stream strean to be written into * @param stream strean to be written into
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static void writeUint16BE(int val, OutputStream stream) throws IOException { public static void writeInt16BE(int val, OutputStream stream) throws IOException {
byte[] buf = new byte[2]; byte[] buf = new byte[2];
writeUint16BE(val, ByteBuffer.wrap(buf)); writeInt16BE(val, ByteBuffer.wrap(buf));
stream.write(buf); stream.write(buf);
} }
@ -281,9 +281,9 @@ public class ByteUtils {
* @param stream strean to be written into * @param stream strean to be written into
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static void writeUint32LE(long val, OutputStream stream) throws IOException { public static void writeInt32LE(long val, OutputStream stream) throws IOException {
byte[] buf = new byte[4]; byte[] buf = new byte[4];
writeUint32LE(val, ByteBuffer.wrap(buf)); writeInt32LE(val, ByteBuffer.wrap(buf));
stream.write(buf); stream.write(buf);
} }
@ -293,9 +293,9 @@ public class ByteUtils {
* @param stream strean to be written into * @param stream strean to be written into
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static void writeUint32BE(long val, OutputStream stream) throws IOException { public static void writeInt32BE(long val, OutputStream stream) throws IOException {
byte[] buf = new byte[4]; byte[] buf = new byte[4];
writeUint32BE(val, ByteBuffer.wrap(buf)); writeInt32BE(val, ByteBuffer.wrap(buf));
stream.write(buf); stream.write(buf);
} }
@ -317,7 +317,7 @@ public class ByteUtils {
* @param stream strean to be written into * @param stream strean to be written into
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static void writeUint64LE(BigInteger val, OutputStream stream) throws IOException { public static void writeInt64LE(BigInteger val, OutputStream stream) throws IOException {
byte[] bytes = val.toByteArray(); byte[] bytes = val.toByteArray();
if (bytes.length > 8) { if (bytes.length > 8) {
throw new RuntimeException("Input too large to encode into a uint64"); throw new RuntimeException("Input too large to encode into a uint64");
@ -536,7 +536,7 @@ public class ByteUtils {
if (includeLength) { if (includeLength) {
byte[] result = new byte[length + 4]; byte[] result = new byte[length + 4];
System.arraycopy(array, 0, result, length - array.length + 3, array.length); System.arraycopy(array, 0, result, length - array.length + 3, array.length);
writeUint32BE(length, result, 0); writeInt32BE(length, result, 0);
if (isNegative) if (isNegative)
result[4] |= 0x80; result[4] |= 0x80;
return result; return result;

View File

@ -32,7 +32,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import static org.bitcoinj.base.internal.ByteUtils.readUint32; import static org.bitcoinj.base.internal.ByteUtils.readUint32;
import static org.bitcoinj.base.internal.ByteUtils.writeUint32BE;
/** /**
* <p>Methods to serialize and de-serialize messages to the Bitcoin network format as defined in * <p>Methods to serialize and de-serialize messages to the Bitcoin network format as defined in
@ -117,7 +116,7 @@ public class BitcoinSerializer extends MessageSerializer {
@Override @Override
public void serialize(String name, byte[] message, OutputStream out) throws IOException { public void serialize(String name, byte[] message, OutputStream out) throws IOException {
byte[] header = new byte[4 + COMMAND_LEN + 4 + 4 /* checksum */]; byte[] header = new byte[4 + COMMAND_LEN + 4 + 4 /* checksum */];
writeUint32BE(params.getPacketMagic(), header, 0); ByteUtils.writeInt32BE(params.getPacketMagic(), header, 0);
// The header array is initialized to zero by Java so we don't have to worry about // The header array is initialized to zero by Java so we don't have to worry about
// NULL terminating the string here. // NULL terminating the string here.
@ -125,7 +124,7 @@ public class BitcoinSerializer extends MessageSerializer {
header[4 + i] = (byte) (name.codePointAt(i) & 0xFF); header[4 + i] = (byte) (name.codePointAt(i) & 0xFF);
} }
ByteUtils.writeUint32LE(message.length, header, 4 + COMMAND_LEN); ByteUtils.writeInt32LE(message.length, header, 4 + COMMAND_LEN);
byte[] hash = Sha256Hash.hashTwice(message); byte[] hash = Sha256Hash.hashTwice(message);
System.arraycopy(hash, 0, header, 4 + COMMAND_LEN + 4, 4); System.arraycopy(hash, 0, header, 4 + COMMAND_LEN + 4, 4);

View File

@ -299,12 +299,12 @@ public class Block extends Message {
// default for testing // default for testing
void writeHeader(OutputStream stream) throws IOException { void writeHeader(OutputStream stream) throws IOException {
ByteUtils.writeUint32LE(version, stream); ByteUtils.writeInt32LE(version, stream);
stream.write(prevBlockHash.getReversedBytes()); stream.write(prevBlockHash.getReversedBytes());
stream.write(getMerkleRoot().getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes());
ByteUtils.writeUint32LE(time.getEpochSecond(), stream); ByteUtils.writeInt32LE(time.getEpochSecond(), stream);
ByteUtils.writeUint32LE(difficultyTarget, stream); ByteUtils.writeInt32LE(difficultyTarget, stream);
ByteUtils.writeUint32LE(nonce, stream); ByteUtils.writeInt32LE(nonce, stream);
} }
private void writeTransactions(OutputStream stream) throws IOException { private void writeTransactions(OutputStream stream) throws IOException {

View File

@ -166,7 +166,7 @@ public class BloomFilter extends Message {
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(VarInt.of(data.length).encode()); stream.write(VarInt.of(data.length).encode());
stream.write(data); stream.write(data);
ByteUtils.writeUint32LE(hashFuncs, stream); ByteUtils.writeInt32LE(hashFuncs, stream);
ByteUtils.writeInt32LE(nTweak, stream); ByteUtils.writeInt32LE(nTweak, stream);
stream.write(nFlags); stream.write(nFlags);
} }

View File

@ -78,7 +78,7 @@ public class GetBlocksMessage extends Message {
@Override @Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
// Version, for some reason. // Version, for some reason.
ByteUtils.writeUint32LE(serializer.getProtocolVersion(), stream); ByteUtils.writeInt32LE(serializer.getProtocolVersion(), 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()

View File

@ -95,7 +95,7 @@ public abstract class ListMessage extends Message {
stream.write(VarInt.of(items.size()).encode()); stream.write(VarInt.of(items.size()).encode());
for (InventoryItem i : items) { for (InventoryItem i : items) {
// Write out the type code. // Write out the type code.
ByteUtils.writeUint32LE(i.type.code, stream); ByteUtils.writeInt32LE(i.type.code, stream);
// And now the hash. // And now the hash.
stream.write(i.hash.getReversedBytes()); stream.write(i.hash.getReversedBytes());
} }

View File

@ -33,7 +33,7 @@ import java.util.Objects;
import static org.bitcoinj.base.internal.ByteUtils.checkBitLE; import static org.bitcoinj.base.internal.ByteUtils.checkBitLE;
import static org.bitcoinj.base.internal.ByteUtils.reverseBytes; import static org.bitcoinj.base.internal.ByteUtils.reverseBytes;
import static org.bitcoinj.base.internal.ByteUtils.writeUint32LE; import static org.bitcoinj.base.internal.ByteUtils.writeInt32LE;
/** /**
* <p>A data structure that contains proofs of block inclusion for one or more transactions, in an efficient manner.</p> * <p>A data structure that contains proofs of block inclusion for one or more transactions, in an efficient manner.</p>
@ -111,7 +111,7 @@ public class PartialMerkleTree extends Message {
@Override @Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException { public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
writeUint32LE(transactionCount, stream); writeInt32LE(transactionCount, stream);
stream.write(VarInt.of(hashes.size()).encode()); stream.write(VarInt.of(hashes.size()).encode());
for (Sha256Hash hash : hashes) for (Sha256Hash hash : hashes)

View File

@ -156,7 +156,7 @@ public class PeerAddress extends ChildMessage {
throw new IllegalStateException("invalid protocolVersion: " + protocolVersion); throw new IllegalStateException("invalid protocolVersion: " + protocolVersion);
if (protocolVersion >= 1) { if (protocolVersion >= 1) {
ByteUtils.writeUint32LE(time.get().getEpochSecond(), stream); ByteUtils.writeInt32LE(time.get().getEpochSecond(), stream);
} }
if (protocolVersion == 2) { if (protocolVersion == 2) {
stream.write(VarInt.of(services.longValue()).encode()); stream.write(VarInt.of(services.longValue()).encode());
@ -198,7 +198,7 @@ public class PeerAddress extends ChildMessage {
throw new IllegalStateException(); throw new IllegalStateException();
} }
} else { } else {
ByteUtils.writeUint64LE(services, stream); // nServices. ByteUtils.writeInt64LE(services, stream); // nServices.
if (addr != null) { if (addr != null) {
// Java does not provide any utility to map an IPv4 address into IPv6 space, so we have to do it by // Java does not provide any utility to map an IPv4 address into IPv6 space, so we have to do it by
// hand. // hand.
@ -225,7 +225,7 @@ public class PeerAddress extends ChildMessage {
} }
} }
// And write out the port. Unlike the rest of the protocol, address and port is in big endian byte order. // And write out the port. Unlike the rest of the protocol, address and port is in big endian byte order.
ByteUtils.writeUint16BE(port, stream); ByteUtils.writeInt16BE(port, stream);
} }
@Override @Override

View File

@ -71,8 +71,8 @@ import java.util.TreeMap;
import static org.bitcoinj.base.internal.Preconditions.checkArgument; import static org.bitcoinj.base.internal.Preconditions.checkArgument;
import static org.bitcoinj.base.internal.Preconditions.checkState; import static org.bitcoinj.base.internal.Preconditions.checkState;
import static org.bitcoinj.core.NetworkParameters.ProtocolVersion.WITNESS_VERSION; import static org.bitcoinj.core.NetworkParameters.ProtocolVersion.WITNESS_VERSION;
import static org.bitcoinj.base.internal.ByteUtils.writeUint32LE; import static org.bitcoinj.base.internal.ByteUtils.writeInt32LE;
import static org.bitcoinj.base.internal.ByteUtils.writeUint64LE; import static org.bitcoinj.base.internal.ByteUtils.writeInt64LE;
/** /**
* <p>A transaction represents the movement of coins from some addresses to some other addresses. It can also represent * <p>A transaction represents the movement of coins from some addresses to some other addresses. It can also represent
@ -1340,7 +1340,7 @@ public class Transaction extends ChildMessage {
ByteArrayOutputStream bos = new ByteArrayOutputStream(255); // just a guess at an average tx length ByteArrayOutputStream bos = new ByteArrayOutputStream(255); // just a guess at an average tx length
tx.bitcoinSerializeToStream(bos, false); tx.bitcoinSerializeToStream(bos, false);
// We also have to write a hash type (sigHashType is actually an unsigned char) // We also have to write a hash type (sigHashType is actually an unsigned char)
writeUint32LE(0x000000ff & sigHashType, bos); writeInt32LE(0x000000ff & sigHashType, bos);
// Note that this is NOT reversed to ensure it will be signed correctly. If it were to be printed out // Note that this is NOT reversed to ensure it will be signed correctly. If it were to be printed out
// however then we would expect that it is IS reversed. // however then we would expect that it is IS reversed.
Sha256Hash hash = Sha256Hash.twiceOf(bos.toByteArray()); Sha256Hash hash = Sha256Hash.twiceOf(bos.toByteArray());
@ -1448,7 +1448,7 @@ public class Transaction extends ChildMessage {
ByteArrayOutputStream bosHashPrevouts = new ByteArrayOutputStream(256); ByteArrayOutputStream bosHashPrevouts = new ByteArrayOutputStream(256);
for (TransactionInput input : this.inputs) { for (TransactionInput input : this.inputs) {
bosHashPrevouts.write(input.getOutpoint().getHash().getReversedBytes()); bosHashPrevouts.write(input.getOutpoint().getHash().getReversedBytes());
writeUint32LE(input.getOutpoint().getIndex(), bosHashPrevouts); writeInt32LE(input.getOutpoint().getIndex(), bosHashPrevouts);
} }
hashPrevouts = Sha256Hash.hashTwice(bosHashPrevouts.toByteArray()); hashPrevouts = Sha256Hash.hashTwice(bosHashPrevouts.toByteArray());
} }
@ -1456,7 +1456,7 @@ public class Transaction extends ChildMessage {
if (!anyoneCanPay && signAll) { if (!anyoneCanPay && signAll) {
ByteArrayOutputStream bosSequence = new ByteArrayOutputStream(256); ByteArrayOutputStream bosSequence = new ByteArrayOutputStream(256);
for (TransactionInput input : this.inputs) { for (TransactionInput input : this.inputs) {
writeUint32LE(input.getSequenceNumber(), bosSequence); writeInt32LE(input.getSequenceNumber(), bosSequence);
} }
hashSequence = Sha256Hash.hashTwice(bosSequence.toByteArray()); hashSequence = Sha256Hash.hashTwice(bosSequence.toByteArray());
} }
@ -1464,7 +1464,7 @@ public class Transaction extends ChildMessage {
if (signAll) { if (signAll) {
ByteArrayOutputStream bosHashOutputs = new ByteArrayOutputStream(256); ByteArrayOutputStream bosHashOutputs = new ByteArrayOutputStream(256);
for (TransactionOutput output : this.outputs) { for (TransactionOutput output : this.outputs) {
writeUint64LE( writeInt64LE(
BigInteger.valueOf(output.getValue().getValue()), BigInteger.valueOf(output.getValue().getValue()),
bosHashOutputs bosHashOutputs
); );
@ -1474,7 +1474,7 @@ public class Transaction extends ChildMessage {
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray()); hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
} else if (basicSigHashType == SigHash.SINGLE.value && inputIndex < outputs.size()) { } else if (basicSigHashType == SigHash.SINGLE.value && inputIndex < outputs.size()) {
ByteArrayOutputStream bosHashOutputs = new ByteArrayOutputStream(256); ByteArrayOutputStream bosHashOutputs = new ByteArrayOutputStream(256);
writeUint64LE( writeInt64LE(
BigInteger.valueOf(this.outputs.get(inputIndex).getValue().getValue()), BigInteger.valueOf(this.outputs.get(inputIndex).getValue().getValue()),
bosHashOutputs bosHashOutputs
); );
@ -1482,18 +1482,18 @@ public class Transaction extends ChildMessage {
bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes()); bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes());
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray()); hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
} }
writeUint32LE(version, bos); writeInt32LE(version, bos);
bos.write(hashPrevouts); bos.write(hashPrevouts);
bos.write(hashSequence); bos.write(hashSequence);
bos.write(inputs.get(inputIndex).getOutpoint().getHash().getReversedBytes()); bos.write(inputs.get(inputIndex).getOutpoint().getHash().getReversedBytes());
writeUint32LE(inputs.get(inputIndex).getOutpoint().getIndex(), bos); writeInt32LE(inputs.get(inputIndex).getOutpoint().getIndex(), bos);
bos.write(VarInt.of(scriptCode.length).encode()); bos.write(VarInt.of(scriptCode.length).encode());
bos.write(scriptCode); bos.write(scriptCode);
writeUint64LE(BigInteger.valueOf(prevValue.getValue()), bos); writeInt64LE(BigInteger.valueOf(prevValue.getValue()), bos);
writeUint32LE(inputs.get(inputIndex).getSequenceNumber(), bos); writeInt32LE(inputs.get(inputIndex).getSequenceNumber(), bos);
bos.write(hashOutputs); bos.write(hashOutputs);
writeUint32LE(this.vLockTime.rawValue(), bos); writeInt32LE(this.vLockTime.rawValue(), bos);
writeUint32LE(0x000000ff & sigHashType, bos); writeInt32LE(0x000000ff & sigHashType, bos);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); // Cannot happen. throw new RuntimeException(e); // Cannot happen.
} }
@ -1533,7 +1533,7 @@ public class Transaction extends ChildMessage {
*/ */
protected void bitcoinSerializeToStream(OutputStream stream, boolean useSegwit) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream, boolean useSegwit) throws IOException {
// version // version
writeUint32LE(version, stream); writeInt32LE(version, stream);
// marker, flag // marker, flag
if (useSegwit) { if (useSegwit) {
stream.write(0); stream.write(0);
@ -1554,7 +1554,7 @@ public class Transaction extends ChildMessage {
} }
} }
// lock_time // lock_time
writeUint32LE(vLockTime.rawValue(), stream); writeInt32LE(vLockTime.rawValue(), stream);
} }
/** /**

View File

@ -180,7 +180,7 @@ public class TransactionInput extends ChildMessage {
outpoint.bitcoinSerializeToStream(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.writeInt32LE(sequence, stream);
} }
/** /**

View File

@ -117,7 +117,7 @@ public class TransactionOutPoint extends ChildMessage {
@Override @Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(hash.getReversedBytes()); stream.write(hash.getReversedBytes());
ByteUtils.writeUint32LE(index, stream); ByteUtils.writeInt32LE(index, stream);
} }
/** /**

View File

@ -160,23 +160,23 @@ public class VersionMessage extends Message {
@Override @Override
public void bitcoinSerializeToStream(OutputStream buf) throws IOException { public void bitcoinSerializeToStream(OutputStream buf) throws IOException {
ByteUtils.writeUint32LE(clientVersion, buf); ByteUtils.writeInt32LE(clientVersion, buf);
ByteUtils.writeUint32LE(localServices, buf); ByteUtils.writeInt32LE(localServices, buf);
ByteUtils.writeUint32LE(localServices >> 32, buf); ByteUtils.writeInt32LE(localServices >> 32, buf);
ByteUtils.writeInt64LE(time.getEpochSecond(), buf); ByteUtils.writeInt64LE(time.getEpochSecond(), buf);
receivingAddr.bitcoinSerializeToStream(buf); receivingAddr.bitcoinSerializeToStream(buf);
fromAddr.bitcoinSerializeToStream(buf); fromAddr.bitcoinSerializeToStream(buf);
// Next up is the "local host nonce", this is to detect the case of connecting // Next up is the "local host nonce", this is to detect the case of connecting
// back to yourself. We don't care about this as we won't be accepting inbound // back to yourself. We don't care about this as we won't be accepting inbound
// connections. // connections.
ByteUtils.writeUint32LE(0, buf); ByteUtils.writeInt32LE(0, buf);
ByteUtils.writeUint32LE(0, buf); ByteUtils.writeInt32LE(0, buf);
// Now comes subVer. // Now comes subVer.
byte[] subVerBytes = subVer.getBytes(StandardCharsets.UTF_8); byte[] subVerBytes = subVer.getBytes(StandardCharsets.UTF_8);
buf.write(VarInt.of(subVerBytes.length).encode()); buf.write(VarInt.of(subVerBytes.length).encode());
buf.write(subVerBytes); buf.write(subVerBytes);
// Size of known block chain. // Size of known block chain.
ByteUtils.writeUint32LE(bestHeight, buf); ByteUtils.writeInt32LE(bestHeight, buf);
if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)) { if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)) {
buf.write(relayTxesBeforeFilter ? 1 : 0); buf.write(relayTxesBeforeFilter ? 1 : 0);
} }

View File

@ -457,7 +457,7 @@ public class Script {
os.write(buf); os.write(buf);
} else if (buf.length < 65536) { } else if (buf.length < 65536) {
os.write(OP_PUSHDATA2); os.write(OP_PUSHDATA2);
ByteUtils.writeUint16LE(buf.length, os); ByteUtils.writeInt16LE(buf.length, os);
os.write(buf); os.write(buf);
} else { } else {
throw new RuntimeException("Unimplemented"); throw new RuntimeException("Unimplemented");

View File

@ -120,11 +120,11 @@ public class ScriptChunk {
} else if (opcode == OP_PUSHDATA2) { } else if (opcode == OP_PUSHDATA2) {
checkState(data.length <= 0xFFFF); checkState(data.length <= 0xFFFF);
stream.write(OP_PUSHDATA2); stream.write(OP_PUSHDATA2);
ByteUtils.writeUint16LE(data.length, stream); ByteUtils.writeInt16LE(data.length, stream);
} else if (opcode == OP_PUSHDATA4) { } else if (opcode == OP_PUSHDATA4) {
checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE); checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
stream.write(OP_PUSHDATA4); stream.write(OP_PUSHDATA4);
ByteUtils.writeUint32LE(data.length, stream); ByteUtils.writeInt32LE(data.length, stream);
} else { } else {
throw new RuntimeException("Unimplemented"); throw new RuntimeException("Unimplemented");
} }

View File

@ -314,12 +314,12 @@ public class BlockTest {
Block block = new Block(TESTNET, 1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<Transaction>()) { Block block = new Block(TESTNET, 1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<Transaction>()) {
@Override @Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
ByteUtils.writeUint32LE(getVersion(), stream); ByteUtils.writeInt32LE(getVersion(), stream);
stream.write(getPrevBlockHash().getReversedBytes()); stream.write(getPrevBlockHash().getReversedBytes());
stream.write(getMerkleRoot().getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes());
ByteUtils.writeUint32LE(getTimeSeconds(), stream); ByteUtils.writeInt32LE(getTimeSeconds(), stream);
ByteUtils.writeUint32LE(getDifficultyTarget(), stream); ByteUtils.writeInt32LE(getDifficultyTarget(), stream);
ByteUtils.writeUint32LE(getNonce(), stream); ByteUtils.writeInt32LE(getNonce(), stream);
stream.write(VarInt.of(Integer.MAX_VALUE).encode()); stream.write(VarInt.of(Integer.MAX_VALUE).encode());
} }

View File

@ -192,10 +192,10 @@ public class FullBlockTestGenerator {
public boolean add(Rule element) { public boolean add(Rule element) {
if (outStream != null && element instanceof BlockAndValidity) { if (outStream != null && element instanceof BlockAndValidity) {
try { try {
ByteUtils.writeUint32BE(params.getPacketMagic(), outStream); ByteUtils.writeInt32BE(params.getPacketMagic(), outStream);
byte[] block = ((BlockAndValidity) element).block.bitcoinSerialize(); byte[] block = ((BlockAndValidity) element).block.bitcoinSerialize();
byte[] length = new byte[4]; byte[] length = new byte[4];
ByteUtils.writeUint32BE(block.length, length, 0); ByteUtils.writeInt32BE(block.length, length, 0);
outStream.write(ByteUtils.reverseBytes(length)); outStream.write(ByteUtils.reverseBytes(length));
outStream.write(block); outStream.write(block);
((BlockAndValidity)element).block = null; ((BlockAndValidity)element).block = null;
@ -1222,8 +1222,8 @@ public class FullBlockTestGenerator {
byte[] varIntBytes = new byte[9]; byte[] varIntBytes = new byte[9];
varIntBytes[0] = (byte) 255; varIntBytes[0] = (byte) 255;
ByteUtils.writeUint32LE((long)b64Original.block.getTransactions().size(), varIntBytes, 1); ByteUtils.writeInt32LE((long)b64Original.block.getTransactions().size(), varIntBytes, 1);
ByteUtils.writeUint32LE(((long)b64Original.block.getTransactions().size()) >>> 32, varIntBytes, 5); ByteUtils.writeInt32LE(((long)b64Original.block.getTransactions().size()) >>> 32, varIntBytes, 5);
stream.write(varIntBytes); stream.write(varIntBytes);
checkState(VarInt.ofBytes(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size()); checkState(VarInt.ofBytes(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size());
@ -1383,7 +1383,7 @@ public class FullBlockTestGenerator {
Arrays.fill(outputScript, (byte) OP_CHECKSIG); Arrays.fill(outputScript, (byte) OP_CHECKSIG);
// If we push an element that is too large, the CHECKSIGs after that push are still counted // If we push an element that is too large, the CHECKSIGs after that push are still counted
outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4; outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4;
ByteUtils.writeUint32LE(Script.MAX_SCRIPT_ELEMENT_SIZE + 1, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1); ByteUtils.writeInt32LE(Script.MAX_SCRIPT_ELEMENT_SIZE + 1, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript)); tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b73); addOnlyInputToTransaction(tx, b73);
b73.addTransaction(tx); b73.addTransaction(tx);
@ -1449,7 +1449,7 @@ public class FullBlockTestGenerator {
Arrays.fill(outputScript, (byte) OP_CHECKSIG); Arrays.fill(outputScript, (byte) OP_CHECKSIG);
// If we push an element that is filled with CHECKSIGs, they (obviously) arent counted // If we push an element that is filled with CHECKSIGs, they (obviously) arent counted
outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4; outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4;
ByteUtils.writeUint32LE(Block.MAX_BLOCK_SIGOPS, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1); ByteUtils.writeInt32LE(Block.MAX_BLOCK_SIGOPS, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript)); tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b76); addOnlyInputToTransaction(tx, b76);
b76.addTransaction(tx); b76.addTransaction(tx);

View File

@ -57,7 +57,7 @@ import java.util.function.IntFunction;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.bitcoinj.base.internal.ByteUtils; import org.bitcoinj.base.internal.ByteUtils;
import static org.bitcoinj.base.internal.ByteUtils.writeUint32LE; import static org.bitcoinj.base.internal.ByteUtils.writeInt32LE;
import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -711,7 +711,7 @@ public class TransactionTest {
@Override @Override
protected void bitcoinSerializeToStream(OutputStream stream, boolean useSegwit) throws IOException { protected void bitcoinSerializeToStream(OutputStream stream, boolean useSegwit) throws IOException {
// version // version
writeUint32LE(getVersion(), stream); writeInt32LE(getVersion(), stream);
// marker, flag // marker, flag
if (useSegwit) { if (useSegwit) {
stream.write(0); stream.write(0);
@ -743,7 +743,7 @@ public class TransactionTest {
} }
} }
// lock_time // lock_time
writeUint32LE(lockTime().rawValue(), stream); writeInt32LE(lockTime().rawValue(), stream);
} }
} }

View File

@ -48,7 +48,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.bitcoinj.base.internal.ByteUtils.writeUint32LE; import static org.bitcoinj.base.internal.ByteUtils.writeInt32LE;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -236,7 +236,7 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
hashes.add(Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000003")); hashes.add(Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000003"));
PartialMerkleTree pmt = new PartialMerkleTree(TESTNET, bits, hashes, 3) { PartialMerkleTree pmt = new PartialMerkleTree(TESTNET, bits, hashes, 3) {
public void bitcoinSerializeToStream(OutputStream stream) throws IOException { public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
writeUint32LE(getTransactionCount(), stream); writeInt32LE(getTransactionCount(), stream);
// Add Integer.MAX_VALUE instead of hashes.size() // Add Integer.MAX_VALUE instead of hashes.size()
stream.write(VarInt.of(Integer.MAX_VALUE).encode()); stream.write(VarInt.of(Integer.MAX_VALUE).encode());
//stream.write(VarInt.of(hashes.size()).encode()); //stream.write(VarInt.of(hashes.size()).encode());