What changed, and why it matters
This commit is a simple renaming cleanup. It replaces the old term 'VarInt' with 'CompactSize' in comments, variable names, error variants, and test strings. No behavior of the code changes, and no security issue is introduced or fixed.
No security action needed. This is a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is purely cosmetic/terminological: identifiers and documentation strings referencing the previously-removed VarInt type are updated to CompactSize. The diff shows only renames in bip152.rs, transaction.rs, consensus/encode.rs, consensus/error.rs, and consensus/serde.rs. The parsing logic, error handling, and serialization behavior remain identical.
Changed components
bitcoin/src/bip152.rsbitcoin/src/blockdata/transaction.rsbitcoin/src/consensus/encode.rsbitcoin/src/consensus/error.rsbitcoin/src/consensus/serde.rsInspect captured patch +28 / −28
diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs
index cf4ff313..446e1a8a 100644
--- a/bitcoin/src/bip152.rs
+++ b/bitcoin/src/bip152.rs
@@ -295,7 +295,7 @@ impl Encodable for BlockTransactionsRequest {
/// contains an entry with the value [`u64::MAX`] as `u64` overflows during differential encoding.
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = self.block_hash.consensus_encode(w)?;
- // Manually encode indexes because they are differentially encoded VarInts.
+ // Manually encode indexes because they are differentially encoded as CompactSize.
len += w.emit_compact_size(self.indexes.len())?;
let mut last_idx = 0;
for idx in &self.indexes {
@@ -311,7 +311,7 @@ impl Decodable for BlockTransactionsRequest {
Ok(BlockTransactionsRequest {
block_hash: BlockHash::consensus_decode(r)?,
indexes: {
- // Manually decode indexes because they are differentially encoded VarInts.
+ // Manually decode indexes because they are differentially encoded as CompactSize.
let nb_indexes = r.read_compact_size()? as usize;
// Since the number of indices ultimately represent transactions,
@@ -530,16 +530,16 @@ mod test {
#[test]
fn getblocktx_differential_encoding_de_and_serialization() {
let testcases = vec![
- // differentially encoded VarInts, indices
+ // differentially encoded CompactSizes, indices
(vec![4, 0, 5, 1, 10], vec![0, 6, 8, 19]),
(vec![1, 0], vec![0]),
(vec![5, 0, 0, 0, 0, 0], vec![0, 1, 2, 3, 4]),
(vec![3, 1, 1, 1], vec![1, 3, 5]),
- (vec![3, 0, 0, 253, 0, 1], vec![0, 1, 258]), // .., 253, 0, 1] == VarInt(256)
+ (vec![3, 0, 0, 253, 0, 1], vec![0, 1, 258]), // .., 253, 0, 1] == CompactSize(256)
];
let deser_errorcases = vec![
- vec![2, 255, 254, 255, 255, 255, 255, 255, 255, 255, 0], // .., 255, 254, .., 255] == VarInt(u64::MAX-1)
- vec![1, 255, 255, 255, 255, 255, 255, 255, 255, 255], // .., 255, 255, .., 255] == VarInt(u64::MAX)
+ vec![2, 255, 254, 255, 255, 255, 255, 255, 255, 255, 0], // .., 255, 254, .., 255] == CompactSize(u64::MAX-1)
+ vec![1, 255, 255, 255, 255, 255, 255, 255, 255, 255], // .., 255, 255, .., 255] == CompactSize(u64::MAX)
];
for testcase in testcases {
{
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index ba14279b..c017a06a 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -111,7 +111,7 @@ internal_macros::define_extension_trait! {
///
/// Keep in mind that when adding a TxIn to a transaction, the total weight of the transaction
/// might increase more than `TxIn::legacy_weight`. This happens when the new input added causes
- /// the input length `VarInt` to increase its encoding length.
+ /// the input length `CompactSize` to increase its encoding length.
fn legacy_weight(&self) -> Weight {
Weight::from_non_witness_data_size(self.base_size().to_u64())
}
@@ -124,7 +124,7 @@ internal_macros::define_extension_trait! {
///
/// Keep in mind that when adding a TxIn to a transaction, the total weight of the transaction
/// might increase more than `TxIn::segwit_weight`. This happens when:
- /// - the new input added causes the input length `VarInt` to increase its encoding length
+ /// - the new input added causes the input length `CompactSize` to increase its encoding length
/// - the new input is the first segwit input added - this will add an additional 2WU to the
/// transaction weight to take into account the SegWit marker
fn segwit_weight(&self) -> Weight {
@@ -158,7 +158,7 @@ internal_macros::define_extension_trait! {
///
/// Keep in mind that when adding a [`TxOut`] to a [`Transaction`] the total weight of the
/// transaction might increase more than `TxOut::weight`. This happens when the new output added
- /// causes the output length `VarInt` to increase its encoding length.
+ /// causes the output length `CompactSize` to increase its encoding length.
///
/// # Panics
///
@@ -764,7 +764,7 @@ impl Decodable for Transaction {
///
/// Note: the effective value of a [`Transaction`] may increase less than the effective value of
/// a [`TxOut`] when adding another [`TxOut`] to the transaction. This happens when the new
-/// [`TxOut`] added causes the output length `VarInt` to increase its encoding length.
+/// [`TxOut`] added causes the output length `CompactSize` to increase its encoding length.
///
/// # Parameters
///
diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs
index e1d907cb..ab14b298 100644
--- a/bitcoin/src/consensus/encode.rs
+++ b/bitcoin/src/consensus/encode.rs
@@ -221,7 +221,7 @@ impl<R: Read + ?Sized> ReadExt for R {
0xFF => {
let x = self.read_u64()?;
if x < 0x1_0000_0000 { // I.e., would have fit in a `u32`.
- Err(ParseError::NonMinimalVarInt.into())
+ Err(ParseError::NonMinimalCompactSize.into())
} else {
Ok(x)
}
@@ -229,7 +229,7 @@ impl<R: Read + ?Sized> ReadExt for R {
0xFE => {
let x = self.read_u32()?;
if x < 0x1_0000 { // I.e., would have fit in a `u16`.
- Err(ParseError::NonMinimalVarInt.into())
+ Err(ParseError::NonMinimalCompactSize.into())
} else {
Ok(x as u64)
}
@@ -237,7 +237,7 @@ impl<R: Read + ?Sized> ReadExt for R {
0xFD => {
let x = self.read_u16()?;
if x < 0xFD { // Could have been encoded as a `u8`.
- Err(ParseError::NonMinimalVarInt.into())
+ Err(ParseError::NonMinimalCompactSize.into())
} else {
Ok(x as u64)
}
@@ -779,50 +779,50 @@ mod tests {
discriminant(
&test_varint_encode(0xFF, &(0x100000000_u64 - 1).to_le_bytes()).unwrap_err()
),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&test_varint_encode(0xFE, &(0x10000_u64 - 1).to_le_bytes()).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&test_varint_encode(0xFD, &(0xFD_u64 - 1).to_le_bytes()).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0x00, 0x00]).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0x00, 0x00, 0x00]).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0xff, 0x00, 0x00]).unwrap_err()),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(
&deserialize::<Vec<u8>>(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
.unwrap_err()
),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
assert_eq!(
discriminant(
&deserialize::<Vec<u8>>(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00])
.unwrap_err()
),
- discriminant(&ParseError::NonMinimalVarInt.into())
+ discriminant(&ParseError::NonMinimalCompactSize.into())
);
let mut vec_256 = vec![0; 259];
diff --git a/bitcoin/src/consensus/error.rs b/bitcoin/src/consensus/error.rs
index be0bccc9..184daafc 100644
--- a/bitcoin/src/consensus/error.rs
+++ b/bitcoin/src/consensus/error.rs
@@ -168,8 +168,8 @@ pub enum ParseError {
/// The invalid checksum.
actual: [u8; 4],
},
- /// VarInt was encoded in a non-minimal way.
- NonMinimalVarInt,
+ /// CompactSize was encoded in a non-minimal way.
+ NonMinimalCompactSize,
/// Parsing error.
ParseFailed(&'static str),
/// Unsupported SegWit flag.
@@ -190,7 +190,7 @@ impl fmt::Display for ParseError {
write!(f, "allocation of oversized vector: requested {}, maximum {}", r, m),
InvalidChecksum { expected: ref e, actual: ref a } =>
write!(f, "invalid checksum: expected {:x}, actual {:x}", e.as_hex(), a.as_hex()),
- NonMinimalVarInt => write!(f, "non-minimal varint"),
+ NonMinimalCompactSize => write!(f, "non-minimal compact size"),
ParseFailed(ref s) => write!(f, "parse failed: {}", s),
UnsupportedSegwitFlag(ref swflag) =>
write!(f, "unsupported SegWit version: {}", swflag),
@@ -207,7 +207,7 @@ impl std::error::Error for ParseError {
MissingData
| OversizedVectorAllocation { .. }
| InvalidChecksum { .. }
- | NonMinimalVarInt
+ | NonMinimalCompactSize
| ParseFailed(_)
| UnsupportedSegwitFlag(_) => None,
}
diff --git a/bitcoin/src/consensus/serde.rs b/bitcoin/src/consensus/serde.rs
index a182e864..15b6e5f1 100644
--- a/bitcoin/src/consensus/serde.rs
+++ b/bitcoin/src/consensus/serde.rs
@@ -373,7 +373,7 @@ fn consensus_error_into_serde<E: serde::de::Error>(error: ParseError) -> E {
expected[0], expected[1], expected[2], expected[3]
)),
),
- ParseError::NonMinimalVarInt =>
+ ParseError::NonMinimalCompactSize =>
E::custom(format_args!("compact size was not encoded minimally")),
ParseError::ParseFailed(msg) => E::custom(msg),
ParseError::UnsupportedSegwitFlag(flag) =>
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.