Add tests for Block and Header read limits
What changed, and why it matters
This commit only adds two new automated tests that check how many bytes a Block and Header decoder expects to read. It does not change any production code, fix a bug, or alter behavior. There is no security issue here.
No action needed; this is a test-only addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds unit tests block_decoder_read_limit and header_decoder_read_limit in primitives/src/block.rs. They construct a minimal block/header, encode it, feed it to the existing Block::decoder() and Header::decoder(), and assert that the decoder’s read_limit() reaches zero exactly when the object is fully decoded. No implementation code is modified.
Changed components
primitives/src/block.rs (tests only)Inspect captured patch +45 / −0
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 5bae25c2..a2d46ce1 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -1089,6 +1089,51 @@ mod tests {
matches!(block.validate(), Err(InvalidBlockError::InvalidCoinbase));
}
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn block_decoder_read_limit() {
+ let mut coinbase_in = crate::TxIn::EMPTY_COINBASE;
+ coinbase_in.script_sig = crate::ScriptSigBuf::from_bytes(vec![0u8; 2]);
+
+ let block = Block::new_unchecked(
+ dummy_header(),
+ vec![Transaction {
+ version: crate::transaction::Version::ONE,
+ lock_time: crate::absolute::LockTime::ZERO,
+ inputs: vec![coinbase_in],
+ outputs: vec![crate::TxOut {
+ amount: units::Amount::MIN,
+ script_pubkey: crate::ScriptPubKeyBuf::new(),
+ }],
+ }],
+ );
+
+ let bytes = encoding::encode_to_vec(&block);
+ let mut view = bytes.as_slice();
+
+ let mut decoder = Block::decoder();
+ assert!(decoder.read_limit() > 0);
+ let needs_more = decoder.push_bytes(&mut view).unwrap();
+ assert!(!needs_more);
+ assert_eq!(decoder.read_limit(), 0);
+ assert_eq!(decoder.end().unwrap(), block);
+ }
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn header_decoder_read_limit() {
+ let header = dummy_header();
+ let bytes = encoding::encode_to_vec(&header);
+ let mut view = bytes.as_slice();
+
+ let mut decoder = Header::decoder();
+ assert!(decoder.read_limit() > 0);
+ let needs_more = decoder.push_bytes(&mut view).unwrap();
+ assert!(!needs_more);
+ assert_eq!(decoder.read_limit(), 0);
+ assert_eq!(decoder.end().unwrap(), header);
+ }
+
#[test]
#[cfg(feature = "alloc")]
fn block_check_witness_commitment_optional() {
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.