Skip to content

Conversation

@AvhiMaz
Copy link

@AvhiMaz AvhiMaz commented Nov 30, 2025

Problem

Deserialization failures all returned the same generic error, making debugging hard.

Solution

Added specific error types for:

  • Wrong data length
  • Invalid discriminator
  • Bad record data

Updated macros to use these errors and added a safety check to prevent panics during serialization.

Deploy Notes

  • No new dependencies
  • No migration needed

Fixes #112

Summary by CodeRabbit

  • Bug Fixes
    • Added three specific error types with clear messages for deserialization failures (invalid data length, invalid discriminator, invalid delegation-record data).
    • Strengthened data validation during serialization/deserialization to require exact expected lengths and to surface the new, more descriptive errors for mismatch or corrupt data.

✏️ Tip: You can customize this high-level summary in your review settings.

Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2025

Walkthrough

Added three new DlpError variants for precise deserialization failures and updated serialization/deserialization utilities to return those specific errors and enforce exact data length checks.

Changes

Cohort / File(s) Change Summary
Error Type Definition
src/error.rs
Added three public DlpError enum variants with explicit discriminants and messages: InvalidDataLength = 38 ("Invalid data length for deserialization"), InvalidDiscriminator = 39 ("Invalid discriminator for delegation record"), and InvalidDelegationRecordData = 40 ("Invalid delegation record deserialization"). Inserted after UndelegateBufferImmutable and before InfallibleError.
Serialization / Deserialization Utilities
src/state/utils/to_bytes.rs, src/state/utils/try_from_bytes.rs
to_bytes_with_discriminator now requires exact length 8 + size_of::<Self>() and returns DlpError::InvalidDataLength on mismatch. try_from_bytes_with_discriminator (and mutable/zero-copy and BORSH variants) now return DlpError::InvalidDataLength for length errors, DlpError::InvalidDiscriminator for discriminator mismatches, and DlpError::InvalidDelegationRecordData for deserialization failures instead of ProgramError::InvalidAccountData. No public signatures changed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Check the new discriminant values (38, 39, 40) for collisions with existing DlpError variants.
  • Validate the exact-length computation and off-by-one risks in to_bytes_with_discriminator.
  • Confirm all paths (zero-copy, mutable, BORSH) consistently map to the new error variants and that error messages are correct.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding specific error codes for deserialization failures, which aligns with the file changes and PR objectives.
Description check ✅ Passed The description covers the problem, solution, and deploy notes; however, it lacks the structured template sections and missing 'Before & After Screenshots' and 'Other changes' details.
Linked Issues check ✅ Passed The PR fully addresses issue #112 by adding three specific DlpError variants (InvalidDataLength, InvalidDiscriminator, InvalidDelegationRecordData) and updating macros to use them instead of generic ProgramError::InvalidAccountData.
Out of Scope Changes check ✅ Passed All changes are directly scoped to addressing issue #112: error enum additions and macro updates for deserialization error handling; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bd7d674 and 7ec081a.

📒 Files selected for processing (1)
  • src/error.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-15T11:45:25.802Z
Learnt from: snawaz
Repo: magicblock-labs/delegation-program PR: 107
File: src/entrypoint.rs:141-144
Timestamp: 2025-10-15T11:45:25.802Z
Learning: In the delegation-program codebase, prefer using `log!` (from pinocchio_log) over `msg!` for error and panic scenarios in the entrypoint code, as per maintainer preference.

Applied to files:

  • src/error.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: lint
🔇 Additional comments (1)
src/error.rs (1)

84-89: LGTM! New error variants correctly implement the PR objectives.

The three new error variants (InvalidDataLength, InvalidDiscriminator, InvalidDelegationRecordData) with discriminants 38, 39, 40 are correctly positioned sequentially and provide the specific deserialization failure information requested in issue #112. The error messages are clear, and the existing From<DlpError> implementations will automatically handle conversion to ProgramError for these new variants.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c891abb and bd7d674.

📒 Files selected for processing (3)
  • src/error.rs (1 hunks)
  • src/state/utils/to_bytes.rs (1 hunks)
  • src/state/utils/try_from_bytes.rs (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-28T18:27:42.542Z
Learnt from: snawaz
Repo: magicblock-labs/delegation-program PR: 111
File: src/diff/types.rs:87-102
Timestamp: 2025-10-28T18:27:42.542Z
Learning: In Solana runtime, `account_info.data()` is guaranteed to have 8-byte alignment.

Applied to files:

  • src/state/utils/to_bytes.rs
🧬 Code graph analysis (1)
src/state/utils/to_bytes.rs (2)
src/state/commit_record.rs (1)
  • size_of (37-37)
src/state/delegation_record.rs (1)
  • size_of (40-40)
🔇 Additional comments (1)
src/state/utils/to_bytes.rs (1)

9-11: Exact-length check removes panic risk but tightens the API contract

Computing expected_len = 8 + size_of::<Self>() and requiring data.len() == expected_len is a good safety improvement: it prevents copy_from_slice from panicking on mismatched buffer sizes and surfaces a clear DlpError::InvalidDataLength instead.

This does slightly tighten behavior versus accepting arbitrary slice lengths, so it’s worth double-checking that all call sites pass exactly-sized buffers (e.g., full account data for these records) and don’t rely on any extra trailing space.

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.

chore: improve error indicators when deserializing a delegation record fails

2 participants