Skip to content

NewTemplate::coinbase_prefix field accepts more bytes then expected #2372

Description

@lucasbalieiro

according to the spec 7.2:

the coinbase_prefix field in NewTemplate of the Template Distribution Protocol should have UP TO 8 bytes .

but the test below shows that we can successfully create a message with more bytes then it is suppose to have:

diff --git a/sv2/subprotocols/template-distribution/src/new_template.rs b/sv2/subprotocols/template-distribution/src/new_template.rs
index 8fd8b59c..343ca0a3 100644
--- a/sv2/subprotocols/template-distribution/src/new_template.rs
+++ b/sv2/subprotocols/template-distribution/src/new_template.rs
@@ -94,3 +94,75 @@ impl fmt::Display for NewTemplateOwned {
         )
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use alloc::vec;
+    use binary_sv2::{from_bytes, GetSize, Serialize};
+
+    /// Spec 7.2: coinbase_prefix is B0255 but the spec limits it to "up to 8 bytes
+    /// (not including the length byte)". A 9-byte payload exceeds this limit.
+    ///
+    /// This test proves the parser accepts oversized coinbase_prefix without error.
+    #[test]
+    fn oversized_coinbase_prefix_roundtrips() {
+        let prefix_9_bytes = vec![0xAB_u8; 9];
+        let empty: Vec<U256<'_>> = vec![];
+        let merkle = Seq0255::new(empty).unwrap();
+
+        let msg = NewTemplate {
+            template_id: 0,
+            future_template: false,
+            version: 0,
+            coinbase_tx_version: 2,
+            coinbase_prefix: B0255::new(&prefix_9_bytes).unwrap(),
+            coinbase_tx_input_sequence: 0,
+            coinbase_tx_value_remaining: 0,
+            coinbase_tx_outputs_count: 0,
+            coinbase_tx_outputs: B064K::new(&[]).unwrap(),
+            coinbase_tx_locktime: 0,
+            merkle_path: merkle,
+        };
+
+        let mut encoded = vec![0u8; msg.get_size()];
+        msg.clone().to_bytes(&mut encoded).unwrap();
+
+        let decoded: NewTemplate = from_bytes(&mut encoded).unwrap();
+
+        assert_eq!(
+            decoded.coinbase_prefix.as_bytes(),
+            prefix_9_bytes.as_slice(),
+            "parser should roundtrip a 9-byte coinbase_prefix (spec violation passes through)"
+        );
+    }
+
+    /// Spec 7.2: 8-byte coinbase_prefix is the maximum allowed.
+    #[test]
+    fn max_valid_coinbase_prefix_roundtrips() {
+        let prefix_8_bytes = vec![0xCD_u8; 8];
+        let empty: Vec<U256<'_>> = vec![];
+        let merkle = Seq0255::new(empty).unwrap();
+
+        let msg = NewTemplate {
+            template_id: 0,
+            future_template: false,
+            version: 0,
+            coinbase_tx_version: 2,
+            coinbase_prefix: B0255::new(&prefix_8_bytes).unwrap(),
+            coinbase_tx_input_sequence: 0,
+            coinbase_tx_value_remaining: 0,
+            coinbase_tx_outputs_count: 0,
+            coinbase_tx_outputs: B064K::new(&[]).unwrap(),
+            coinbase_tx_locktime: 0,
+            merkle_path: merkle,
+        };
+
+        let mut encoded = vec![0u8; msg.get_size()];
+        msg.clone().to_bytes(&mut encoded).unwrap();
+
+        let decoded: NewTemplate = from_bytes(&mut encoded).unwrap();
+
+        assert_eq!(decoded.coinbase_prefix.as_bytes(), prefix_8_bytes.as_slice());
+    }
+}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions