Skip to content

Commit

Permalink
VarInt: Introduce intValue() and longValue() accessors and use them, …
Browse files Browse the repository at this point in the history
…deprecating access to the field.
  • Loading branch information
Andreas Schildbach authored and ripcurlx committed Aug 23, 2021
1 parent d8a7ad7 commit 8b2b1b8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ protected long readVarInt(int offset) throws ProtocolException {
try {
VarInt varint = new VarInt(payload, cursor + offset);
cursor += offset + varint.getOriginalSizeInBytes();
return varint.value;
return varint.longValue();
} catch (ArrayIndexOutOfBoundsException e) {
throw new ProtocolException(e);
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/bitcoinj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,27 +608,27 @@ protected static int calcLength(byte[] buf, int offset) {
long scriptLen;

varint = new VarInt(buf, cursor);
long txInCount = varint.value;
long txInCount = varint.longValue();
cursor += varint.getOriginalSizeInBytes();

for (i = 0; i < txInCount; i++) {
// 36 = length of previous_outpoint
cursor += 36;
varint = new VarInt(buf, cursor);
scriptLen = varint.value;
scriptLen = varint.longValue();
// 4 = length of sequence field (unint32)
cursor += scriptLen + 4 + varint.getOriginalSizeInBytes();
}

varint = new VarInt(buf, cursor);
long txOutCount = varint.value;
long txOutCount = varint.longValue();
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.value;
scriptLen = varint.longValue();
cursor += scriptLen + varint.getOriginalSizeInBytes();
}
// 4 = length of lock_time field (uint32)
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/org/bitcoinj/core/VarInt.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2011 Google Inc.
* Copyright 2021 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +17,14 @@

package org.bitcoinj.core;

import com.google.common.primitives.Ints;

/**
* A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
*/
public class VarInt {
/** @deprecated use {{@link #intValue()} or {{@link #longValue()}}} */
@Deprecated
public final long value;
private final int originallyEncodedSize;

Expand Down Expand Up @@ -56,6 +61,14 @@ public VarInt(byte[] buf, int offset) {
}
}

public long longValue() {
return value;
}

public int intValue() {
return Ints.checkedCast(value);
}

/**
* Returns the original number of bytes used to encode the value if it was
* deserialized from a byte array, or the minimum encoded size if it was not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ public boolean add(Rule element) {
Utils.uint32ToByteArrayLE((long)b64Original.block.getTransactions().size(), varIntBytes, 1);
Utils.uint32ToByteArrayLE(((long)b64Original.block.getTransactions().size()) >>> 32, varIntBytes, 5);
stream.write(varIntBytes);
checkState(new VarInt(varIntBytes, 0).value == b64Original.block.getTransactions().size());
checkState(new VarInt(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size());

for (Transaction transaction : b64Original.block.getTransactions())
transaction.bitcoinSerialize(stream);
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/java/org/bitcoinj/core/VarIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ public void testBytes() throws Exception {
VarInt a = new VarInt(10); // with widening conversion
assertEquals(1, a.getSizeInBytes());
assertEquals(1, a.encode().length);
assertEquals(10, new VarInt(a.encode(), 0).value);
assertEquals(10, new VarInt(a.encode(), 0).intValue());
}

@Test
public void testShorts() throws Exception {
VarInt a = new VarInt(64000); // with widening conversion
assertEquals(3, a.getSizeInBytes());
assertEquals(3, a.encode().length);
assertEquals(64000, new VarInt(a.encode(), 0).value);
assertEquals(64000, new VarInt(a.encode(), 0).intValue());
}

@Test
public void testShortFFFF() throws Exception {
VarInt a = new VarInt(0xFFFFL);
assertEquals(3, a.getSizeInBytes());
assertEquals(3, a.encode().length);
assertEquals(0xFFFFL, new VarInt(a.encode(), 0).value);
assertEquals(0xFFFFL, new VarInt(a.encode(), 0).intValue());
}

@Test
Expand All @@ -53,7 +53,7 @@ public void testInts() throws Exception {
assertEquals(5, a.getSizeInBytes());
assertEquals(5, a.encode().length);
byte[] bytes = a.encode();
assertEquals(0xAABBCCDDL, 0xFFFFFFFFL & new VarInt(bytes, 0).value);
assertEquals(0xAABBCCDDL, new VarInt(bytes, 0).longValue());
}

@Test
Expand All @@ -62,7 +62,7 @@ public void testIntFFFFFFFF() throws Exception {
assertEquals(5, a.getSizeInBytes());
assertEquals(5, a.encode().length);
byte[] bytes = a.encode();
assertEquals(0xFFFFFFFFL, 0xFFFFFFFFL & new VarInt(bytes, 0).value);
assertEquals(0xFFFFFFFFL, new VarInt(bytes, 0).longValue());
}

@Test
Expand All @@ -71,7 +71,7 @@ public void testLong() throws Exception {
assertEquals(9, a.getSizeInBytes());
assertEquals(9, a.encode().length);
byte[] bytes = a.encode();
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value);
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).longValue());
}

@Test
Expand Down

0 comments on commit 8b2b1b8

Please sign in to comment.