mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-23 22:46:56 +01:00
VarInt: Introduce intValue() and longValue() accessors and use them, deprecating access to the field.
This commit is contained in:
parent
742394c434
commit
ae4f6d43ce
5 changed files with 25 additions and 12 deletions
|
@ -299,7 +299,7 @@ public abstract class Message {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -601,27 +601,27 @@ public class Transaction extends ChildMessage {
|
|||
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)
|
||||
|
|
|
@ -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.
|
||||
|
@ -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;
|
||||
|
||||
|
@ -56,6 +61,14 @@ public class VarInt {
|
|||
}
|
||||
}
|
||||
|
||||
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.
|
||||
|
|
|
@ -1230,7 +1230,7 @@ public class FullBlockTestGenerator {
|
|||
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);
|
||||
|
|
|
@ -28,7 +28,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
@ -36,7 +36,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
@ -44,7 +44,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
@ -53,7 +53,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
@ -62,7 +62,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
@ -71,7 +71,7 @@ public class VarIntTest {
|
|||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue