← Watch feed
Improve test coverage of CompactTarget
What changed, and why it matters
This commit only adds new unit tests for the CompactTarget type. It does not change any production code, fix bugs, or alter behavior. There is no security relevance.
Recommended action
No action required; this is a test-coverage-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds five test cases to primitives/src/pow.rs exercising CompactTargetDecoder::read_limit, encoding round-trip, deprecated to_hex, error Display/source, and ordering. No implementation code is modified.
Changed components
primitives/src/pow.rs (tests only)Inspect captured patch +43 / −0
diff --git a/primitives/src/pow.rs b/primitives/src/pow.rs
index 75f4d143..c5d7b36d 100644
--- a/primitives/src/pow.rs
+++ b/primitives/src/pow.rs
@@ -128,9 +128,52 @@ impl std::error::Error for CompactTargetDecoderError {
mod tests {
#[cfg(feature = "alloc")]
use alloc::format;
+ #[cfg(feature = "alloc")]
+ use alloc::string::ToString;
+ #[cfg(feature = "std")]
+ use std::error::Error as _;
+
+ use encoding::Decoder as _;
use super::*;
+ #[test]
+ fn compact_target_decoder_read_limit() {
+ // read_limit is one u32 = 4 bytes for empty decoder
+ assert_eq!(CompactTargetDecoder::default().read_limit(), 4);
+ assert_eq!(<CompactTarget as encoding::Decodable>::decoder().read_limit(), 4);
+ }
+
+ #[test]
+ fn compact_target_decoder_round_trip() {
+ let bits: u32 = 0x1d00_ffff;
+ let compact_target =
+ encoding::decode_from_slice::<CompactTarget>(&bits.to_le_bytes()).unwrap();
+ assert_eq!(compact_target.to_consensus(), bits);
+ }
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ #[allow(deprecated)]
+ fn compact_target_to_hex() {
+ let compact_target = CompactTarget::from_consensus(0x1d00_ffff);
+ assert_eq!(compact_target.to_hex(), "1d00ffff");
+ }
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn compact_target_decoder_error_display_and_source() {
+ let mut slice = [0u8; 3].as_slice();
+ let mut decoder = CompactTargetDecoder::new();
+
+ assert!(decoder.push_bytes(&mut slice).unwrap());
+
+ let err = decoder.end().unwrap_err();
+ assert!(!err.to_string().is_empty());
+ #[cfg(feature = "std")]
+ assert!(err.source().is_some());
+ }
+
#[test]
fn compact_target_ordering() {
let lower = CompactTarget::from_consensus(0x1d00_fffe);
Risk score
Our methodology →Why this scored 15/100
Human-validated context
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
No validated notes yet.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.