`NewTemplate::coinbase_prefix` field accepts more bytes then expected

Open Beginner friendly
#2,372 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
networking

Research direction

Start in sv2/subprotocols/template-distribution/src/new_template.rs and run the existing tests around NewTemplate serialization. Use the supplied oversized and maximum-length cases to verify the boundary behavior. Done means an 8-byte coinbase_prefix remains valid while a 9-byte value is rejected according to Template Distribution Protocol spec 7.2.

Written by the indexing model from the issue text.

Description

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());
+    }
+}
Dominant language
Rust
Stars
352
Forks
200
Avg merge
4d 17h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stratum-mining/stratum

All issues in stratum-mining/stratum

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.