primitives: move a const to the module level
What changed, and why it matters
This commit is a minor code cleanup: it moves a fixed byte pattern (the Bitcoin witness commitment marker) from inside a function to the top of the file so the same constant can be reused in a test. There is no change to behavior, no bug fix, and no security impact.
No action needed; this is a non-functional refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors WITNESS_COMMITMENT_MAGIC from a local const named MAGIC inside witness_commitment_from_coinbase() to a module-level const, and updates the function and its unit test to reference the shared constant. The byte values, comparison logic, and witness commitment extraction behavior are unchanged.
Changed components
primitives/src/block.rsInspect captured patch +9 / −6
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index efe89d61..65806740 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -54,6 +54,10 @@ pub use self::error::{
#[doc(inline)]
pub use crate::hash_types::{BlockHash, BlockHashDecoder, BlockHashEncoder, WitnessCommitment};
+// Consists of OP_RETURN, OP_PUSHBYTES_36, and four "witness header" bytes.
+#[cfg(feature = "alloc")]
+const WITNESS_COMMITMENT_MAGIC: [u8; 6] = [0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];
+
/// Marker for whether or not a block has been validated.
///
/// We define valid as:
@@ -420,9 +424,6 @@ pub fn compute_witness_root(transactions: &[Transaction]) -> Option<WitnessMerkl
#[cfg(feature = "alloc")]
fn witness_commitment_from_coinbase(coinbase: &Transaction) -> Option<WitnessCommitment> {
- // Consists of OP_RETURN, OP_PUSHBYTES_36, and four "witness header" bytes.
- const MAGIC: [u8; 6] = [0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];
-
if !coinbase.is_coinbase() {
return None;
}
@@ -431,7 +432,10 @@ fn witness_commitment_from_coinbase(coinbase: &Transaction) -> Option<WitnessCom
if let Some(pos) = coinbase
.outputs
.iter()
- .rposition(|o| o.script_pubkey.len() >= 38 && o.script_pubkey.as_bytes()[0..6] == MAGIC)
+ .rposition(|o| {
+ o.script_pubkey.len() >= 38
+ && o.script_pubkey.as_bytes()[0..6] == WITNESS_COMMITMENT_MAGIC
+ })
{
let bytes =
<[u8; 32]>::try_from(&coinbase.outputs[pos].script_pubkey.as_bytes()[6..38]).unwrap();
@@ -1595,9 +1599,8 @@ mod tests {
#[cfg(feature = "alloc")]
fn witness_commitment_from_coinbase_simple() {
// Add witness commitment to the coinbase
- let magic = [0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];
let mut pubkey_bytes = [0; 38];
- pubkey_bytes[0..6].copy_from_slice(&magic);
+ pubkey_bytes[0..6].copy_from_slice(&WITNESS_COMMITMENT_MAGIC);
let witness_commitment =
WitnessCommitment::from_byte_array(pubkey_bytes[6..38].try_into().unwrap());
let commitment_script = crate::script::ScriptBuf::from_bytes(pubkey_bytes.to_vec());
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.