primitives: add unit test for incomplete transaction decoding
What changed, and why it matters
This commit only adds a new unit test that checks how the library handles incomplete Bitcoin transaction data. It does not change any production code, fix a bug, or introduce a security feature. The test verifies that decoding truncated transaction bytes correctly produces an error rather than a valid transaction.
No security action required. Review the existing decoder behavior if desired, but the commit itself is a routine test improvement.
Security signals we found
No production code changes
Test-only addition
No bug fix or security patch present in diff
Evidence from the diff
The diff adds a loop in primitives/src/transaction.rs that truncates a known-valid transaction byte vector by various amounts (1, 10, 20, 50, 100, half, and full length), feeds the truncated bytes into Transaction::decoder(), and asserts that decoder.end() returns an error. This is purely a test addition; no decoding logic is modified.
Changed components
primitives/src/transaction.rs (test module only)Inspect captured patch +10 / −0
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 3a438439..ada1b188 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -2116,6 +2116,16 @@ mod tests {
decoder.push_bytes(&mut slice).unwrap();
let tx = decoder.end().unwrap();
+ // Attempt various truncations
+ for i in [1, 10, 20, 50, 100, tx_bytes.len() / 2, tx_bytes.len()] {
+ let mut decoder = Transaction::decoder();
+ let mut slice = &tx_bytes[..tx_bytes.len() - i];
+ // push_bytes will not fail because the data is not invalid, just truncated
+ decoder.push_bytes(&mut slice).unwrap();
+ // ...but end() will fail because we will be in some incomplete state
+ decoder.end().unwrap_err();
+ }
+
// All these tests aren't really needed because if they fail, the hash check at the end
// will also fail. But these will show you where the failure is so I'll leave them in.
assert_eq!(tx.version, Version::TWO);
Why this scored 12/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.