Skip to content

feat: added internal built-in magic root program - #23

Open
bmuddha wants to merge 7 commits into
processorfrom
programs
Open

feat: added internal built-in magic root program#23
bmuddha wants to merge 7 commits into
processorfrom
programs

Conversation

@bmuddha

@bmuddha bmuddha commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

What changed

Added the engine's internal program crates: MagicRoot and v42 calculator
implementations with separate interface crates.

Why

Engine account CRUD needs privileged operations ordinary programs cannot perform,
while callers and tests need instruction schemas without depending on execution
implementations.

Part of #4.

Closes #30.

Impact

  • magic-root-interface defines MagicRootInstruction and instruction
    composition; magic-root-program provides the native entrypoint.
  • Patch applies field changes and balances lamport deltas against the authority;
    Finalize loads executable targets into the transaction program cache.
  • Delete marks accounts closed, while PostFinalize invokes follow-up
    instructions after rejecting immutable writable accounts.
  • The v42 interface, builder, and program provide a deterministic fixture for
    runtime and integration tests.

Reviewer notes

MagicRoot is authority-gated: every invocation must be top-level and signed by
the thread-local AUTHORITY. Authorization and decoding complete before the
target account is borrowed.

Follow-up

The engine crate registers MagicRoot and exposes account CRUD through these
instructions upstack.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@bmuddha, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 15e6c1af-9407-4fed-ae5b-f58d529f6ac8

📥 Commits

Reviewing files that changed from the base of the PR and between 859bb43 and 6cc17f1.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (1)
  • programs/magic-root-program/src/post_finalize.rs
📝 Walkthrough

Walkthrough

The change adds the MagicRoot instruction interface and program. It supports authority checks, account patching, finalization, deletion, executable loading, and restricted post-finalize CPI execution.

Changes

MagicRoot program

Layer / File(s) Summary
Instruction contract and composition
programs/magic-root-interface/...
Defines MagicRootInstruction, wincode serialization, instruction composition, account sequencing, and forwarded account metadata handling.
Authorized processing and dispatch
Cargo.toml, programs/magic-root-program/Cargo.toml, programs/magic-root-program/src/lib.rs, programs/magic-root-program/src/processor.rs, programs/magic-root-program/src/tests.rs
Adds the workspace package, program entrypoint, authorization checks, instruction decoding, variant dispatch, and authorization coverage.
Account lifecycle and post-finalize execution
programs/magic-root-program/src/account.rs, programs/magic-root-program/src/post_finalize.rs, programs/magic-root-program/README.md
Adds validated patching, lamport balancing, finalization, executable program-cache loading, deletion, post-finalize CPI restrictions, and lifecycle documentation.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant MagicRootEntrypoint
  participant processor
  participant account
  participant post_finalize

  Caller->>MagicRootEntrypoint: Submit serialized MagicRootInstruction
  MagicRootEntrypoint->>processor: Process InvokeContext
  processor->>processor: Authorize caller and AUTHORITY
  processor->>processor: Decode and dispatch instruction
  processor->>account: Apply Patch, Finalize, or Delete
  processor->>post_finalize: Execute PostFinalize instructions
  post_finalize->>Caller: Invoke forwarded CPI actions
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning MagicRoot requirements are implemented, but the required v42 interface, builder, and program crates are absent from the changeset. Add the v42 interface, builder, and program crates, or update the linked issue and PR scope to exclude them.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding the internal built-in MagicRoot program.
Description check ✅ Passed The description discusses MagicRoot, related interfaces, privileged operations, and the intended v42 fixture changes.
Out of Scope Changes check ✅ Passed The reviewed changes support the MagicRoot interface and program objectives, with no unrelated code changes identified.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch programs

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.

@bmuddha

bmuddha commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@programs/magic-root-program/src/account.rs`:
- Around line 15-40: Update the account patch flow around the mutable target
borrow so it is released before borrowing authority account index 0. Keep the
target borrow alive through patch application and balance calculations, then
explicitly drop or scope it before
ctx.transaction_context.accounts().try_borrow_mut(0), allowing target 0 without
AccountBorrowFailed.
- Around line 50-70: Update finalize to copy the account data into an owned
value and explicitly release the account RefMut before constructing the
ProgramCacheEntry or storing it through ctx.program_cache_for_tx_batch. Preserve
the installed flags on successful loading, and either restore the previous flags
on the ProgramCacheEntry::new error path using the existing previous binding or
revise the README wording to describe runtime instruction-level rollback rather
than program-level behavior.

In `@programs/magic-root-program/src/processor.rs`:
- Around line 30-47: Update authorize to explicitly reject calls whose caller_id
equals the MagicRoot program ID before accepting builtin callers, returning
InstructionError::CallDepth and preserving the existing diagnostic pattern. Keep
the existing builtin validation for all other callers so MagicRoot cannot
authorize itself regardless of post_finalize behavior.
- Around line 48-52: Update authorize to validate that the authority account is
an actual transaction signer, not merely the account at index 0. Use the
transaction context’s signer-checking API for the AUTHORITY key and reject
unauthorized or unsigned callers before allowing MagicRoot account mutations.

In `@programs/magic-root-program/src/tests.rs`:
- Around line 43-44: Update the imports in the tests module to bring
ProgramCacheForTxBatch and ProgramRuntimeEnvironments into scope, first
confirming each type’s module path in the vendored runtime crate. Keep the
existing cache initialization in the test unchanged and use the verified
explicit paths or imports.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a02f1198-b204-49a6-9173-9850441942d2

📥 Commits

Reviewing files that changed from the base of the PR and between ac70027 and f3d8516.

📒 Files selected for processing (11)
  • Cargo.toml
  • programs/magic-root-interface/Cargo.toml
  • programs/magic-root-interface/README.md
  • programs/magic-root-interface/src/lib.rs
  • programs/magic-root-program/Cargo.toml
  • programs/magic-root-program/README.md
  • programs/magic-root-program/src/account.rs
  • programs/magic-root-program/src/lib.rs
  • programs/magic-root-program/src/post_finalize.rs
  • programs/magic-root-program/src/processor.rs
  • programs/magic-root-program/src/tests.rs

Comment thread programs/magic-root-program/src/account.rs
Comment thread programs/magic-root-program/src/account.rs
Comment thread programs/magic-root-program/src/processor.rs
Comment thread programs/magic-root-program/src/processor.rs
Comment thread programs/magic-root-program/src/tests.rs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@programs/magic-root-program/src/post_finalize.rs`:
- Around line 17-22: Update the post-finalize invocation around
ctx.native_invoke to stop deriving signers from action.accounts[].is_signer;
pass an empty signer set by default, and only provide the finalized target or
another fixed, validated signer set when explicitly required by post-finalize
CPI.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: bd7cfcfc-2863-436b-b306-8db068244fc3

📥 Commits

Reviewing files that changed from the base of the PR and between d436103 and 859bb43.

📒 Files selected for processing (2)
  • Cargo.toml
  • programs/magic-root-program/src/post_finalize.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • Cargo.toml

Comment thread programs/magic-root-program/src/post_finalize.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coderabbit Trigger coderabbit review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add MagicRoot and v42 engine program crates

2 participants