Skip to content

Commit 2f80041

Browse files
program.py: route from_bytes_backrefs through deser_auto
The previous implementation went through CLVMTree.from_bytes → deserialize_as_tuples, which is a classic-format-only parser. As a result, Program.from_bytes_backrefs() raised ValueError on any blob using the 0xfe backref opcode, despite the method's name implying backref support. After this change: - still rejects the serde_2026 magic prefix up front - delegates to deser_auto (Rust binding), which handles both classic and backref formats - adds regression test that round-trips a tree-with-shared-subtrees through backref encoder and from_bytes_backrefs (asserting 0xfe presence) Fixes Cursor bot finding on PR #708. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 658f886 commit 2f80041

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

wheel/python/clvm_rs/program.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def from_bytes_backrefs(cls, blob: bytes) -> Program:
5151
"""Deserialize classic or backrefs format only (rejects serde_2026)."""
5252
if blob.startswith(SERDE_2026_MAGIC_PREFIX):
5353
raise ValueError("unexpected serde_2026 format; use from_bytes() for auto-detection")
54-
obj, cursor = cls.from_bytes_with_cursor(blob, 0)
55-
return obj
54+
return cls.wrap(deser_auto(blob))
5655

5756
@classmethod
5857
def from_bytes_with_cursor(

wheel/python/tests/test_serialize.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_repr_clvm_tree(self):
140140
with self.assertRaises(ValueError):
141141
Program.fromhex("ff8085")
142142

143-
o = Program.from_bytes_backrefs(bytes.fromhex("ff808185"))
143+
o, _ = Program.from_bytes_with_cursor(bytes.fromhex("ff808185"), 0)
144144
self.assertEqual(repr(o._unwrapped_pair[0]), "<CLVMTree: 80>")
145145
self.assertEqual(repr(o._unwrapped_pair[1]), "<CLVMTree: 8185>")
146146

@@ -179,6 +179,24 @@ def test_backrefs_parser_rejects_2026(self):
179179
with self.assertRaises(ValueError):
180180
Program.from_bytes_backrefs(prefixed)
181181

182+
def test_from_bytes_backrefs_with_actual_backrefs(self):
183+
"""Test that from_bytes_backrefs actually parses backrefs format (0xfe opcodes)."""
184+
# Construct a tree with shared subtrees that will trigger backref encoding
185+
shared = Program.to([b"shared_atom", b"another_shared_atom"])
186+
p = Program.to([shared, shared, shared, shared, shared])
187+
188+
# Serialize using backrefs format
189+
blob = serialize(deserialize(bytes(p), "legacy"), "backrefs")
190+
191+
# Assert the blob contains at least one 0xfe backref opcode
192+
self.assertIn(0xfe, blob, "backrefs-encoded blob should contain at least one 0xfe byte")
193+
194+
# Round-trip through from_bytes_backrefs
195+
p2 = Program.from_bytes_backrefs(blob)
196+
197+
# Verify equality (by tree hash)
198+
self.assertEqual(p, p2)
199+
182200

183201
class ClvmTreeToLazyNodeTest(unittest.TestCase):
184202
"""Tests for clvm_tree_to_lazy_node."""

0 commit comments

Comments
 (0)