Message hierarchy: Use int (rather than long) for several array and string lengths.

This commit is contained in:
Andreas Schildbach 2021-04-28 14:28:25 +02:00
parent 67399b6c13
commit bdc0310f1f
5 changed files with 23 additions and 22 deletions

View File

@ -70,7 +70,8 @@ public class AddressMessage extends Message {
@Override
protected void parse() throws ProtocolException {
long numAddresses = readVarInt().longValue();
VarInt numAddressesVarInt = readVarInt();
int numAddresses = numAddressesVarInt.intValue();
// Guard against ultra large messages that will crash us.
if (numAddresses > MAX_ADDRESSES)
throw new ProtocolException("Address message too large.");
@ -81,7 +82,7 @@ public class AddressMessage extends Message {
addresses.add(addr);
cursor += addr.getMessageSize();
}
length = new VarInt(addresses.size()).getSizeInBytes();
length = numAddressesVarInt.getSizeInBytes();
// The 4 byte difference is the uint32 timestamp that was introduced in version 31402
length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4);
}

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().longValue();
int numOutpoints = readVarInt().intValue();
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().longValue();
int numHeaders = readVarInt().intValue();
if (numHeaders > MAX_HEADERS)
throw new ProtocolException("Too many headers: got " + numHeaders + " which is larger than " +
MAX_HEADERS);

View File

@ -329,13 +329,13 @@ public abstract class Message {
}
protected byte[] readByteArray() throws ProtocolException {
long len = readVarInt().longValue();
return readBytes((int)len);
final int length = readVarInt().intValue();
return readBytes(length);
}
protected String readStr() throws ProtocolException {
long length = readVarInt().longValue();
return length == 0 ? "" : new String(readBytes((int) length), StandardCharsets.UTF_8); // optimization for empty strings
int length = readVarInt().intValue();
return length == 0 ? "" : new String(readBytes(length), StandardCharsets.UTF_8); // optimization for empty strings
}
protected Sha256Hash readHash() throws ProtocolException {

View File

@ -598,30 +598,30 @@ public class Transaction extends ChildMessage {
int cursor = offset + 4;
int i;
long scriptLen;
int scriptLen;
varint = new VarInt(buf, cursor);
long txInCount = varint.longValue();
int txInCount = varint.intValue();
cursor += varint.getOriginalSizeInBytes();
for (i = 0; i < txInCount; i++) {
// 36 = length of previous_outpoint
cursor += 36;
varint = new VarInt(buf, cursor);
scriptLen = varint.longValue();
scriptLen = varint.intValue();
// 4 = length of sequence field (unint32)
cursor += scriptLen + 4 + varint.getOriginalSizeInBytes();
}
varint = new VarInt(buf, cursor);
long txOutCount = varint.longValue();
int txOutCount = varint.intValue();
cursor += varint.getOriginalSizeInBytes();
for (i = 0; i < txOutCount; i++) {
// 8 = length of tx value field (uint64)
cursor += 8;
varint = new VarInt(buf, cursor);
scriptLen = varint.longValue();
scriptLen = varint.intValue();
cursor += scriptLen + varint.getOriginalSizeInBytes();
}
// 4 = length of lock_time field (uint32)
@ -683,26 +683,26 @@ public class Transaction extends ChildMessage {
}
private void parseInputs() {
long numInputs = readVarInt().longValue();
int numInputs = readVarInt().intValue();
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).longValue();
int scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH).intValue();
optimalEncodingMessageSize += TransactionOutPoint.MESSAGE_LENGTH + VarInt.sizeOf(scriptLen) + scriptLen + 4;
cursor += scriptLen + 4;
}
}
private void parseOutputs() {
long numOutputs = readVarInt().longValue();
int numOutputs = readVarInt().intValue();
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).longValue();
int scriptLen = readVarInt(8).intValue();
optimalEncodingMessageSize += 8 + VarInt.sizeOf(scriptLen) + scriptLen;
cursor += scriptLen;
}
@ -711,14 +711,14 @@ public class Transaction extends ChildMessage {
private void parseWitnesses() {
int numWitnesses = inputs.size();
for (int i = 0; i < numWitnesses; i++) {
long pushCount = readVarInt().longValue();
TransactionWitness witness = new TransactionWitness((int) pushCount);
getInput(i).setWitness(witness);
int pushCount = readVarInt().intValue();
optimalEncodingMessageSize += VarInt.sizeOf(pushCount);
TransactionWitness witness = new TransactionWitness(pushCount);
getInput(i).setWitness(witness);
for (int y = 0; y < pushCount; y++) {
long pushSize = readVarInt().longValue();
int pushSize = readVarInt().intValue();
optimalEncodingMessageSize += VarInt.sizeOf(pushSize) + pushSize;
byte[] push = readBytes((int) pushSize);
byte[] push = readBytes(pushSize);
witness.setPush(y, push);
}
}