Skip to content

Fix PDA derivation using global pda instead of delegatedAccount#150

Open
Meesut0 wants to merge 1 commit intomagicblock-labs:mainfrom
Meesut0:Meesut0-patch-1
Open

Fix PDA derivation using global pda instead of delegatedAccount#150
Meesut0 wants to merge 1 commit intomagicblock-labs:mainfrom
Meesut0:Meesut0-patch-1

Conversation

@Meesut0
Copy link

@Meesut0 Meesut0 commented Mar 15, 2026

Helper instruction builders derived PDAs from a global pda variable
instead of the provided delegatedAccount parameter.

This could produce incorrect PDA addresses if the helpers are called with
a delegated account different from the global pda.

This change updates the PDA derivation to use delegatedAccount.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected delegation operations to properly derive account references, ensuring accurate delegation record management and instruction processing.

Fix incorrect PDA derivation in helper instruction builders.

The functions `createCommitAccountInstruction`, `createFinalizeInstruction`, and
`createUndelegateInstruction` accept a `delegatedAccount` parameter but were
deriving PDAs from a global `pda` variable instead.

This could lead to incorrect PDA computation if these helpers are called with a
delegated account different from the global `pda`.

This change replaces `pda` with the provided `delegatedAccount` when deriving:
- commit state PDA
- commit record PDA
- delegation record PDA
- delegation metadata PDA
- undelegate buffer PDA

This makes the helpers consistent with their function signatures and ensures
correct PDA derivation.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 15, 2026

📝 Walkthrough

Walkthrough

This change modifies the test-delegation.ts integration test file to align PDA derivations with the delegatedAccount parameter. The update replaces PDA derivation function calls that previously used a pda parameter with calls using delegatedAccount. This includes adjustments to commit state, commit record, delegation record, and delegation metadata PDA functions. Additionally, undelegate-related PDA derivation now uses delegatedAccount.toBytes() instead of pda.toBytes() for buffer seed computation. The instruction creation helpers for commit, finalize, and undelegate operations were updated accordingly to maintain consistency in PDA source parameter.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

You can validate your CodeRabbit configuration file in your editor.

If your editor has YAML language server, you can enable auto-completion and validation by adding # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json at the top of your CodeRabbit configuration file.

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/integration/tests/test-delegation.ts (1)

441-441: ⚠️ Potential issue | 🟠 Major

Mark validator writable in commit and finalize helpers.

The createCommitAccountInstruction helper at line 441 calls the CommitState instruction (discriminator 1), which performs active lamport transfers FROM the validator to the commit state account. The validator account must be marked writable.

Similarly, createFinalizeInstruction at line 477 should be updated to mark validator writable for consistency with the corresponding Rust instruction builders (dlp-api/src/instruction_builder/commit_finalize.rs), which explicitly mark the validator as writable.

Suggested patch
@@
-      { pubkey: validator, isSigner: true, isWritable: false },
+      { pubkey: validator, isSigner: true, isWritable: true },
@@
-      { pubkey: validator, isSigner: true, isWritable: false },
+      { pubkey: validator, isSigner: true, isWritable: true },
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/integration/tests/test-delegation.ts` at line 441, The helper functions
createCommitAccountInstruction and createFinalizeInstruction currently mark the
validator account as isWritable: false but the on-chain CommitState
(discriminator 1) and corresponding finalize instruction transfer lamports from
the validator, so update both helpers to mark the validator account as writable
(set isWritable: true) where the validator is passed in (reference symbols
createCommitAccountInstruction, createFinalizeInstruction and the validator
account tuple { pubkey: validator, ... }) to match the Rust instruction builders
in commit_finalize.rs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@tests/integration/tests/test-delegation.ts`:
- Line 441: The helper functions createCommitAccountInstruction and
createFinalizeInstruction currently mark the validator account as isWritable:
false but the on-chain CommitState (discriminator 1) and corresponding finalize
instruction transfer lamports from the validator, so update both helpers to mark
the validator account as writable (set isWritable: true) where the validator is
passed in (reference symbols createCommitAccountInstruction,
createFinalizeInstruction and the validator account tuple { pubkey: validator,
... }) to match the Rust instruction builders in commit_finalize.rs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: ff27bb1d-2dcf-40ff-8505-4a74f6d9bd5e

📥 Commits

Reviewing files that changed from the base of the PR and between 78a5d0f and fb2b51d.

📒 Files selected for processing (1)
  • tests/integration/tests/test-delegation.ts

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.

1 participant