Skip to content

fix: backward compat for pre-contract_flags serialization#165

Merged
bennyhodl merged 3 commits into
bennyhodl:masterfrom
matthewjablack:fix/backward-compat-contract-flags
Jun 27, 2026
Merged

fix: backward compat for pre-contract_flags serialization#165
bennyhodl merged 3 commits into
bennyhodl:masterfrom
matthewjablack:fix/backward-compat-contract-flags

Conversation

@matthewjablack

Copy link
Copy Markdown
Contributor

Summary

Adds backward compatibility so contracts stored before the contract_flags PR (#157) can still be deserialized. Without this, upgrading to v1.1.0 breaks reading any existing contracts from the database.

Changes

  • dlc-messages/src/lib.rs — Replace impl_dlc_writeable! macro for OfferDlc with manual Writeable/Readable impls. The Readable impl peeks 4 bytes after the type: if the value is 1–10, it's protocol_version (new format); otherwise it's contract_flags + chain_hash start (old format, defaults protocol_version to 1). Serialize always writes new format.

  • ddk-manager/src/contract/ser.rs — Same treatment for OfferedContract. Peeks 1 byte after refund_locktime: 0x02/0x03 means old format (compressed pubkey prefix = start of counter_party, no contract_flags field), 0x00/0x01 means new format (contract_flags value).

  • ddk/src/storage/sled/contract.rs — Adds tests deserializing both old-format (testconfig/contract_binaries/old/Offered) and new-format (testconfig/contract_binaries/Offered) binaries to verify backward compat.

Context

After upgrading dlcd-rs to ddk 1.1.0, existing contracts in postgres (stored as serialized byte blobs) fail to deserialize with StorageError("Nonsense bytes") because the new Readable impl expects protocol_version (4 bytes) and contract_flags (1 byte) that don't exist in old data.

Replace macro-generated Readable impls with manual ones that detect
old format (no protocol_version/contract_flags) vs new format.

OfferDlc: peek first 4 bytes as u32 - if 1-10 it's protocol_version
(new format), otherwise it's contract_flags+chain_hash (old format).

OfferedContract: peek byte after refund_locktime - if 0x02/0x03 it's
a pubkey prefix (old format, no contract_flags), if 0x00/0x01 it's
contract_flags (new format).
@bennyhodl bennyhodl merged commit bba6db0 into bennyhodl:master Jun 27, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants