VarInt: migrate to byte[] serialize() from byte[] encode()

This commit is contained in:
Andreas Schildbach 2023-04-03 21:27:41 +02:00
parent 98d86375ae
commit edf889306b
21 changed files with 42 additions and 42 deletions

View File

@ -145,11 +145,11 @@ public class VarInt {
}
/**
* Encodes the value into its minimal representation.
* Allocates a byte array and serializes the value into its minimal representation.
*
* @return the minimal encoded bytes of the value
*/
public byte[] encode() {
public byte[] serialize() {
ByteBuffer buf = ByteBuffer.allocate(sizeOf(value));
return write(buf).array();
}

View File

@ -42,7 +42,7 @@ public abstract class AddressMessage extends Message {
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
if (addresses == null)
return;
stream.write(VarInt.of(addresses.size()).encode());
stream.write(VarInt.of(addresses.size()).serialize());
for (PeerAddress addr : addresses) {
addr.bitcoinSerializeToStream(stream);
}

View File

@ -283,7 +283,7 @@ public class Block extends Message {
return;
}
stream.write(VarInt.of(transactions.size()).encode());
stream.write(VarInt.of(transactions.size()).serialize());
for (Transaction tx : transactions) {
tx.bitcoinSerializeToStream(stream);
}

View File

@ -165,7 +165,7 @@ public class BloomFilter extends Message {
*/
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(VarInt.of(data.length).encode());
stream.write(VarInt.of(data.length).serialize());
stream.write(data);
ByteUtils.writeInt32LE(hashFuncs, stream);
ByteUtils.writeInt32LE(nTweak, stream);

View File

@ -81,7 +81,7 @@ public class GetBlocksMessage extends Message {
// 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
// them, until we end up at the genesis block. See CBlockLocator::Set()
stream.write(VarInt.of(locator.size()).encode());
stream.write(VarInt.of(locator.size()).serialize());
for (Sha256Hash hash : locator.getHashes()) {
// Have to reverse as wire format is little endian.
stream.write(hash.getReversedBytes());

View File

@ -60,7 +60,7 @@ public class HeadersMessage extends Message {
@Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(VarInt.of(blockHeaders.size()).encode());
stream.write(VarInt.of(blockHeaders.size()).serialize());
for (Block header : blockHeaders) {
header.cloneAsHeader().bitcoinSerializeToStream(stream);
stream.write(0);

View File

@ -86,7 +86,7 @@ public abstract class ListMessage extends Message {
@Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(VarInt.of(items.size()).encode());
stream.write(VarInt.of(items.size()).serialize());
for (InventoryItem i : items) {
// Write out the type code.
ByteUtils.writeInt32LE(i.type.code, stream);

View File

@ -114,11 +114,11 @@ public class PartialMerkleTree extends Message {
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
writeInt32LE(transactionCount, stream);
stream.write(VarInt.of(hashes.size()).encode());
stream.write(VarInt.of(hashes.size()).serialize());
for (Sha256Hash hash : hashes)
stream.write(hash.getReversedBytes());
stream.write(VarInt.of(matchedChildBits.length).encode());
stream.write(VarInt.of(matchedChildBits.length).serialize());
stream.write(matchedChildBits);
}

View File

@ -150,15 +150,15 @@ public class PeerAddress extends Message {
ByteUtils.writeInt32LE(time.get().getEpochSecond(), stream);
}
if (protocolVersion == 2) {
stream.write(VarInt.of(services.bits()).encode());
stream.write(VarInt.of(services.bits()).serialize());
if (addr != null) {
if (addr instanceof Inet4Address) {
stream.write(0x01);
stream.write(VarInt.of(4).encode());
stream.write(VarInt.of(4).serialize());
stream.write(addr.getAddress());
} else if (addr instanceof Inet6Address) {
stream.write(0x02);
stream.write(VarInt.of(16).encode());
stream.write(VarInt.of(16).serialize());
stream.write(addr.getAddress());
} else {
throw new IllegalStateException();
@ -168,12 +168,12 @@ public class PeerAddress extends Message {
if (onionAddress.length == 10) {
// TORv2
stream.write(0x03);
stream.write(VarInt.of(10).encode());
stream.write(VarInt.of(10).serialize());
stream.write(onionAddress);
} else if (onionAddress.length == 32 + 2 + 1) {
// TORv3
stream.write(0x04);
stream.write(VarInt.of(32).encode());
stream.write(VarInt.of(32).serialize());
byte[] pubkey = Arrays.copyOfRange(onionAddress, 0, 32);
byte[] checksum = Arrays.copyOfRange(onionAddress, 32, 34);
byte torVersion = onionAddress[34];

View File

@ -106,11 +106,11 @@ public class RejectMessage extends Message {
@Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
stream.write(VarInt.of(messageBytes.length).encode());
stream.write(VarInt.of(messageBytes.length).serialize());
stream.write(messageBytes);
stream.write(code.code);
byte[] reasonBytes = reason.getBytes(StandardCharsets.UTF_8);
stream.write(VarInt.of(reasonBytes.length).encode());
stream.write(VarInt.of(reasonBytes.length).serialize());
stream.write(reasonBytes);
if ("block".equals(message) || "tx".equals(message))
stream.write(messageHash.getReversedBytes());

View File

@ -1451,7 +1451,7 @@ public class Transaction extends Message {
BigInteger.valueOf(output.getValue().getValue()),
bosHashOutputs
);
bosHashOutputs.write(VarInt.of(output.getScriptBytes().length).encode());
bosHashOutputs.write(VarInt.of(output.getScriptBytes().length).serialize());
bosHashOutputs.write(output.getScriptBytes());
}
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
@ -1461,7 +1461,7 @@ public class Transaction extends Message {
BigInteger.valueOf(this.outputs.get(inputIndex).getValue().getValue()),
bosHashOutputs
);
bosHashOutputs.write(VarInt.of(this.outputs.get(inputIndex).getScriptBytes().length).encode());
bosHashOutputs.write(VarInt.of(this.outputs.get(inputIndex).getScriptBytes().length).serialize());
bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes());
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
}
@ -1470,7 +1470,7 @@ public class Transaction extends Message {
bos.write(hashSequence);
bos.write(inputs.get(inputIndex).getOutpoint().getHash().getReversedBytes());
writeInt32LE(inputs.get(inputIndex).getOutpoint().getIndex(), bos);
bos.write(VarInt.of(scriptCode.length).encode());
bos.write(VarInt.of(scriptCode.length).serialize());
bos.write(scriptCode);
writeInt64LE(BigInteger.valueOf(prevValue.getValue()), bos);
writeInt32LE(inputs.get(inputIndex).getSequenceNumber(), bos);
@ -1523,11 +1523,11 @@ public class Transaction extends Message {
stream.write(1);
}
// txin_count, txins
stream.write(VarInt.of(inputs.size()).encode());
stream.write(VarInt.of(inputs.size()).serialize());
for (TransactionInput in : inputs)
in.bitcoinSerializeToStream(stream);
// txout_count, txouts
stream.write(VarInt.of(outputs.size()).encode());
stream.write(VarInt.of(outputs.size()).serialize());
for (TransactionOutput out : outputs)
out.bitcoinSerializeToStream(stream);
// script_witnisses

View File

@ -175,7 +175,7 @@ public class TransactionInput extends Message {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
outpoint.bitcoinSerializeToStream(stream);
stream.write(VarInt.of(scriptBytes.length).encode());
stream.write(VarInt.of(scriptBytes.length).serialize());
stream.write(scriptBytes);
ByteUtils.writeInt32LE(sequence, stream);
}

View File

@ -145,7 +145,7 @@ public class TransactionOutput extends Message {
Objects.requireNonNull(scriptBytes);
ByteUtils.writeInt64LE(value, stream);
// TODO: Move script serialization into the Script class, where it belongs.
stream.write(VarInt.of(scriptBytes.length).encode());
stream.write(VarInt.of(scriptBytes.length).serialize());
stream.write(scriptBytes);
}

View File

@ -89,9 +89,9 @@ public class TransactionWitness {
}
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(VarInt.of(pushes.size()).encode());
stream.write(VarInt.of(pushes.size()).serialize());
for (byte[] push : pushes) {
stream.write(VarInt.of(push.length).encode());
stream.write(VarInt.of(push.length).serialize());
stream.write(push);
}
}

View File

@ -182,7 +182,7 @@ public class VersionMessage extends Message {
ByteUtils.writeInt32LE(0, buf);
// Now comes subVer.
byte[] subVerBytes = subVer.getBytes(StandardCharsets.UTF_8);
buf.write(VarInt.of(subVerBytes.length).encode());
buf.write(VarInt.of(subVerBytes.length).serialize());
buf.write(subVerBytes);
// Size of known block chain.
ByteUtils.writeInt32LE(bestHeight, buf);

View File

@ -1421,7 +1421,7 @@ public class ECKey implements EncryptableItem {
bos.write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES);
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
VarInt size = VarInt.of(messageBytes.length);
bos.write(size.encode());
bos.write(size.serialize());
bos.write(messageBytes);
return bos.toByteArray();
} catch (IOException e) {

View File

@ -37,8 +37,8 @@ public class VarIntTest {
assertEquals(value, a.intValue());
assertEquals(size, a.getSizeInBytes());
assertEquals(size, a.getOriginalSizeInBytes());
assertEquals(size, a.encode().length);
assertEquals(value, VarInt.ofBytes(a.encode(), 0).intValue());
assertEquals(size, a.serialize().length);
assertEquals(value, VarInt.ofBytes(a.serialize(), 0).intValue());
}
@Test(expected = RuntimeException.class)
@ -52,7 +52,7 @@ public class VarIntTest {
@Parameters(method = "longTestVectors")
public void testIntGetErr2(int value, int size) {
VarInt a = VarInt.of(value);
VarInt.ofBytes(a.encode(), 0).intValue();
VarInt.ofBytes(a.serialize(), 0).intValue();
}
@Test
@ -62,8 +62,8 @@ public class VarIntTest {
assertEquals(value, a.longValue());
assertEquals(size, a.getSizeInBytes());
assertEquals(size, a.getOriginalSizeInBytes());
assertEquals(size, a.encode().length);
assertEquals(value, VarInt.ofBytes(a.encode(), 0).longValue());
assertEquals(size, a.serialize().length);
assertEquals(value, VarInt.ofBytes(a.serialize(), 0).longValue());
}
@Test

View File

@ -321,7 +321,7 @@ public class BlockTest {
ByteUtils.writeInt32LE(getDifficultyTarget(), stream);
ByteUtils.writeInt32LE(getNonce(), stream);
stream.write(VarInt.of(Integer.MAX_VALUE).encode());
stream.write(VarInt.of(Integer.MAX_VALUE).serialize());
}
};
byte[] serializedBlock = block.bitcoinSerialize();

View File

@ -32,7 +32,7 @@ public class MessageTest {
@Test(expected = ProtocolException.class)
public void readStrOfExtremeLength() {
VarInt length = VarInt.of(Integer.MAX_VALUE);
ByteBuffer payload = ByteBuffer.wrap(length.encode());
ByteBuffer payload = ByteBuffer.wrap(length.serialize());
new VarStrMessage(payload);
}
@ -51,7 +51,7 @@ public class MessageTest {
@Test(expected = ProtocolException.class)
public void readByteArrayOfExtremeLength() {
VarInt length = VarInt.of(Integer.MAX_VALUE);
ByteBuffer payload = ByteBuffer.wrap(length.encode());
ByteBuffer payload = ByteBuffer.wrap(length.serialize());
new VarBytesMessage(payload);
}

View File

@ -734,12 +734,12 @@ public class TransactionTest {
}
// txin_count, txins
long inputsSize = hackInputsSize ? Integer.MAX_VALUE : getInputs().size();
stream.write(VarInt.of(inputsSize).encode());
stream.write(VarInt.of(inputsSize).serialize());
for (TransactionInput in : getInputs())
in.bitcoinSerializeToStream(stream);
// txout_count, txouts
long outputsSize = hackOutputsSize ? Integer.MAX_VALUE : getOutputs().size();
stream.write(VarInt.of(outputsSize).encode());
stream.write(VarInt.of(outputsSize).serialize());
for (TransactionOutput out : getOutputs())
out.bitcoinSerializeToStream(stream);
// script_witnisses
@ -747,10 +747,10 @@ public class TransactionTest {
for (TransactionInput in : getInputs()) {
TransactionWitness witness = in.getWitness();
long pushCount = hackWitnessPushCountSize ? Integer.MAX_VALUE : witness.getPushCount();
stream.write(VarInt.of(pushCount).encode());
stream.write(VarInt.of(pushCount).serialize());
for (int i = 0; i < witness.getPushCount(); i++) {
byte[] push = witness.getPush(i);
stream.write(VarInt.of(push.length).encode());
stream.write(VarInt.of(push.length).serialize());
stream.write(push);
}

View File

@ -238,12 +238,12 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
writeInt32LE(getTransactionCount(), stream);
// Add Integer.MAX_VALUE instead of hashes.size()
stream.write(VarInt.of(Integer.MAX_VALUE).encode());
stream.write(VarInt.of(Integer.MAX_VALUE).serialize());
//stream.write(VarInt.of(hashes.size()).encode());
for (Sha256Hash hash : hashes)
stream.write(hash.getReversedBytes());
stream.write(VarInt.of(bits.length).encode());
stream.write(VarInt.of(bits.length).serialize());
stream.write(bits);
}
};