Message: Make readVarInt() return a VarInt rather than long.

This commit is contained in:
Andreas Schildbach 2021-04-24 17:08:59 +02:00
parent ae4f6d43ce
commit 67399b6c13
12 changed files with 23 additions and 23 deletions

View file

@ -70,7 +70,7 @@ public class AddressMessage extends Message {
@Override
protected void parse() throws ProtocolException {
long numAddresses = readVarInt();
long numAddresses = readVarInt().longValue();
// Guard against ultra large messages that will crash us.
if (numAddresses > MAX_ADDRESSES)
throw new ProtocolException("Address message too large.");

View file

@ -229,7 +229,7 @@ public class Block extends Message {
return;
}
int numTransactions = (int) readVarInt();
int numTransactions = readVarInt().intValue();
optimalEncodingMessageSize += VarInt.sizeOf(numTransactions);
transactions = new ArrayList<>(Math.min(numTransactions, Utils.MAX_INITIAL_ARRAY_LENGTH));
for (int i = 0; i < numTransactions; i++) {

View file

@ -47,7 +47,7 @@ public class GetBlocksMessage extends Message {
protected void parse() throws ProtocolException {
cursor = offset;
version = readUint32();
int startCount = (int) readVarInt();
int startCount = readVarInt().intValue();
if (startCount > 500)
throw new ProtocolException("Number of locators cannot be > 500, received: " + startCount);
length = cursor - offset + ((startCount + 1) * 32);

View file

@ -61,7 +61,7 @@ public class GetUTXOsMessage extends Message {
@Override
protected void parse() throws ProtocolException {
includeMempool = readBytes(1)[0] == 1;
long numOutpoints = readVarInt();
long numOutpoints = readVarInt().longValue();
ImmutableList.Builder<TransactionOutPoint> list = ImmutableList.builder();
for (int i = 0; i < numOutpoints; i++) {
TransactionOutPoint outPoint = new TransactionOutPoint(params, payload, cursor);

View file

@ -66,7 +66,7 @@ public class HeadersMessage extends Message {
@Override
protected void parse() throws ProtocolException {
long numHeaders = readVarInt();
long numHeaders = readVarInt().longValue();
if (numHeaders > MAX_HEADERS)
throw new ProtocolException("Too many headers: got " + numHeaders + " which is larger than " +
MAX_HEADERS);

View file

@ -73,7 +73,7 @@ public abstract class ListMessage extends Message {
@Override
protected void parse() throws ProtocolException {
arrayLen = readVarInt();
arrayLen = readVarInt().longValue();
if (arrayLen > MAX_INVENTORY_ITEMS)
throw new ProtocolException("Too many items in INV message: " + arrayLen);
length = (int) (cursor - offset + (arrayLen * InventoryItem.MESSAGE_LENGTH));

View file

@ -291,15 +291,15 @@ public abstract class Message {
return new BigInteger(Utils.reverseBytes(readBytes(8)));
}
protected long readVarInt() throws ProtocolException {
protected VarInt readVarInt() throws ProtocolException {
return readVarInt(0);
}
protected long readVarInt(int offset) throws ProtocolException {
protected VarInt readVarInt(int offset) throws ProtocolException {
try {
VarInt varint = new VarInt(payload, cursor + offset);
cursor += offset + varint.getOriginalSizeInBytes();
return varint.longValue();
return varint;
} catch (ArrayIndexOutOfBoundsException e) {
throw new ProtocolException(e);
}
@ -329,12 +329,12 @@ public abstract class Message {
}
protected byte[] readByteArray() throws ProtocolException {
long len = readVarInt();
long len = readVarInt().longValue();
return readBytes((int)len);
}
protected String readStr() throws ProtocolException {
long length = readVarInt();
long length = readVarInt().longValue();
return length == 0 ? "" : new String(readBytes((int) length), StandardCharsets.UTF_8); // optimization for empty strings
}

View file

@ -117,12 +117,12 @@ public class PartialMerkleTree extends Message {
protected void parse() throws ProtocolException {
transactionCount = (int)readUint32();
int nHashes = (int) readVarInt();
int nHashes = readVarInt().intValue();
hashes = new ArrayList<>(Math.min(nHashes, Utils.MAX_INITIAL_ARRAY_LENGTH));
for (int i = 0; i < nHashes; i++)
hashes.add(readHash());
int nFlagBytes = (int) readVarInt();
int nFlagBytes = readVarInt().intValue();
matchedChildBits = readBytes(nFlagBytes);
length = cursor - offset;

View file

@ -683,26 +683,26 @@ public class Transaction extends ChildMessage {
}
private void parseInputs() {
long numInputs = readVarInt();
long numInputs = readVarInt().longValue();
optimalEncodingMessageSize += VarInt.sizeOf(numInputs);
inputs = new ArrayList<>(Math.min((int) numInputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
for (long i = 0; i < numInputs; i++) {
TransactionInput input = new TransactionInput(params, this, payload, cursor, serializer);
inputs.add(input);
long scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH);
long scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH).longValue();
optimalEncodingMessageSize += TransactionOutPoint.MESSAGE_LENGTH + VarInt.sizeOf(scriptLen) + scriptLen + 4;
cursor += scriptLen + 4;
}
}
private void parseOutputs() {
long numOutputs = readVarInt();
long numOutputs = readVarInt().longValue();
optimalEncodingMessageSize += VarInt.sizeOf(numOutputs);
outputs = new ArrayList<>(Math.min((int) numOutputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
for (long i = 0; i < numOutputs; i++) {
TransactionOutput output = new TransactionOutput(params, this, payload, cursor, serializer);
outputs.add(output);
long scriptLen = readVarInt(8);
long scriptLen = readVarInt(8).longValue();
optimalEncodingMessageSize += 8 + VarInt.sizeOf(scriptLen) + scriptLen;
cursor += scriptLen;
}
@ -711,12 +711,12 @@ public class Transaction extends ChildMessage {
private void parseWitnesses() {
int numWitnesses = inputs.size();
for (int i = 0; i < numWitnesses; i++) {
long pushCount = readVarInt();
long pushCount = readVarInt().longValue();
TransactionWitness witness = new TransactionWitness((int) pushCount);
getInput(i).setWitness(witness);
optimalEncodingMessageSize += VarInt.sizeOf(pushCount);
for (int y = 0; y < pushCount; y++) {
long pushSize = readVarInt();
long pushSize = readVarInt().longValue();
optimalEncodingMessageSize += VarInt.sizeOf(pushSize) + pushSize;
byte[] push = readBytes((int) pushSize);
witness.setPush(y, push);

View file

@ -162,7 +162,7 @@ public class TransactionInput extends ChildMessage {
protected void parse() throws ProtocolException {
outpoint = new TransactionOutPoint(params, payload, cursor, this, serializer);
cursor += outpoint.getMessageSize();
int scriptLen = (int) readVarInt();
int scriptLen = readVarInt().intValue();
length = cursor - offset + scriptLen + 4;
scriptBytes = readBytes(scriptLen);
sequence = readUint32();

View file

@ -137,7 +137,7 @@ public class TransactionOutput extends ChildMessage {
@Override
protected void parse() throws ProtocolException {
value = readInt64();
int scriptLen = (int) readVarInt();
int scriptLen = readVarInt().intValue();
length = cursor - offset + scriptLen;
scriptBytes = readBytes(scriptLen);
}

View file

@ -106,11 +106,11 @@ public class UTXOsMessage extends Message {
// hitsBitmap indicates which of the queried outputs were found in the UTXO set.
height = readUint32();
chainHead = readHash();
int numBytes = (int) readVarInt();
int numBytes = readVarInt().intValue();
if (numBytes < 0 || numBytes > InventoryMessage.MAX_INVENTORY_ITEMS / 8)
throw new ProtocolException("hitsBitmap out of range: " + numBytes);
hits = readBytes(numBytes);
int numOuts = (int) readVarInt();
int numOuts = readVarInt().intValue();
if (numOuts < 0 || numOuts > InventoryMessage.MAX_INVENTORY_ITEMS)
throw new ProtocolException("numOuts out of range: " + numOuts);
outputs = new ArrayList<>(numOuts);