Skip to content

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

Open
bmuddha wants to merge 2 commits into
processorfrom
programs
Open

feat: added internal built-in magic root program#23
bmuddha wants to merge 2 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

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: fb058fde-76c5-4d41-b704-06abee6b742c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Warning

.coderabbit.yaml has a parsing error

The CodeRabbit configuration file in this repository has a parsing error and default settings were used instead. Please fix the error(s) in the configuration file. You can initialize chat with CodeRabbit to get help with the configuration file.

💥 Parsing errors (1)
Validation error: Invalid input: expected boolean, received string at "reviews.auto_review.enabled"
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Cargo.toml, programs/magic-root-interface/...
Defines MagicRootInstruction, wincode serialization, instruction composition, account sequencing, and forwarded account metadata handling.
Authorized processing and dispatch
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 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 covered, but the required v42 interface, builder, and program crates are not present in the listed changes [#30]. Add the missing v42 calculator interface, builder, and program crates, or link the PR that implements them.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding the internal built-in MagicRoot program.
Description check ✅ Passed The description directly explains the MagicRoot and v42 program crates, their purpose, behavior, and follow-up engine wiring.
Out of Scope Changes check ✅ Passed The listed manifests, implementation files, documentation, and tests support the MagicRoot and workspace objectives without unrelated code changes.
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 on lines +50 to +70
let mut account = ctx.transaction_context.accounts().try_borrow_mut(target)?;
account.set_flags(flags);
if !account.executable() {
return Ok(());
}
let pubkey = ctx.transaction_context.get_key_of_account_at_index(target)?;
let entry = ProgramCacheEntry::new(
ctx.environment_config
.program_runtime_environments_for_execution
.get_env_for_execution()
.clone(),
account.data(),
)
.map_err(|_| {
ic_msg!(ctx, "MagicRoot: program load failed {}", pubkey);
InstructionError::ProgramEnvironmentSetupFailure
})?
.into();
ctx.program_cache_for_tx_batch.store_modified_entry(*pubkey, entry);
ic_msg!(ctx, "MagicRoot: finalized program load");
Ok(())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

finalize holds the account guard across a mutable use of ctx, and does not roll back flags.

Two problems in this function:

  1. The RefMut guard bound at Line 50 has a Drop implementation, so its borrow of ctx.transaction_context lives until the end of the function. Line 68 needs &mut ctx.program_cache_for_tx_batch. Confirm this compiles; if it does, the guard still holds the account borrow across the cache store for no reason. Copy the data and drop the guard before Line 55.
  2. programs/magic-root-program/README.md Line 28 states that "failed executable loading rolls back the installed flags". Line 51 installs the flags. The error path at Lines 63-66 returns without restoring the previous flags. If the intended rollback is the runtime's instruction-level account rollback, state that in the README instead of describing it as program behavior.
♻️ Proposed fix for the borrow scope
     let mut account = ctx.transaction_context.accounts().try_borrow_mut(target)?;
+    let previous = account.flags();
     account.set_flags(flags);
     if !account.executable() {
         return Ok(());
     }
+    let data = account.data().to_vec();
+    drop(account);
     let pubkey = ctx.transaction_context.get_key_of_account_at_index(target)?;
     let entry = ProgramCacheEntry::new(
         ctx.environment_config
             .program_runtime_environments_for_execution
             .get_env_for_execution()
             .clone(),
-        account.data(),
+        &data,
     )

The previous binding above supports an explicit flag restore if you choose to implement the rollback in the program rather than document the runtime behavior.

🤖 Prompt for 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.

In `@programs/magic-root-program/src/account.rs` around lines 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.

Source: Path instructions

Comment on lines +30 to +47
pub(crate) fn authorize(ctx: &InvokeContext<'_, '_>) -> Result<(), InstructionError> {
let height = ctx.get_stack_height();
if height != TRANSACTION_LEVEL_STACK_HEIGHT {
let instruction = ctx.transaction_context.get_current_instruction_context()?;
let caller = ctx
.transaction_context
.get_instruction_context_at_index_in_trace(instruction.get_index_of_caller())?;
let caller_id = caller.get_program_key()?;
let is_builtin = ctx
.program_cache_for_tx_batch
.find(caller_id)
.filter(|e| matches!(e.program, ProgramCacheEntryType::Builtin(_)))
.is_some();
if !is_builtin {
ic_msg!(ctx, "MagicRoot: non-builtin caller {}", caller_id);
return Err(InstructionError::CallDepth);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

authorize does not enforce the documented "MagicRoot itself cannot be the caller" rule.

programs/magic-root-program/README.md Line 10 states that MagicRoot cannot be the caller. authorize only requires the immediate caller to be a builtin in the program cache. MagicRoot is itself registered as a builtin, so a MagicRoot-to-MagicRoot CPI passes this check. Today post_finalize::process blocks actions that target magic_root_interface::ID, so the recursion is blocked one layer up. The invariant is therefore documented in authorize and in the README, but enforced somewhere else.

Add the explicit self-caller rejection so the check matches the documentation and survives future changes to post_finalize.

🛡️ Proposed fix
         let caller_id = caller.get_program_key()?;
+        if *caller_id == magic_root_interface::ID {
+            ic_msg!(ctx, "MagicRoot: recursive caller");
+            return Err(InstructionError::CallDepth);
+        }
         let is_builtin = ctx
             .program_cache_for_tx_batch
             .find(caller_id)
             .filter(|e| matches!(e.program, ProgramCacheEntryType::Builtin(_)))
             .is_some();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub(crate) fn authorize(ctx: &InvokeContext<'_, '_>) -> Result<(), InstructionError> {
let height = ctx.get_stack_height();
if height != TRANSACTION_LEVEL_STACK_HEIGHT {
let instruction = ctx.transaction_context.get_current_instruction_context()?;
let caller = ctx
.transaction_context
.get_instruction_context_at_index_in_trace(instruction.get_index_of_caller())?;
let caller_id = caller.get_program_key()?;
let is_builtin = ctx
.program_cache_for_tx_batch
.find(caller_id)
.filter(|e| matches!(e.program, ProgramCacheEntryType::Builtin(_)))
.is_some();
if !is_builtin {
ic_msg!(ctx, "MagicRoot: non-builtin caller {}", caller_id);
return Err(InstructionError::CallDepth);
}
}
pub(crate) fn authorize(ctx: &InvokeContext<'_, '_>) -> Result<(), InstructionError> {
let height = ctx.get_stack_height();
if height != TRANSACTION_LEVEL_STACK_HEIGHT {
let instruction = ctx.transaction_context.get_current_instruction_context()?;
let caller = ctx
.transaction_context
.get_instruction_context_at_index_in_trace(instruction.get_index_of_caller())?;
let caller_id = caller.get_program_key()?;
if *caller_id == magic_root_interface::ID {
ic_msg!(ctx, "MagicRoot: recursive caller");
return Err(InstructionError::CallDepth);
}
let is_builtin = ctx
.program_cache_for_tx_batch
.find(caller_id)
.filter(|e| matches!(e.program, ProgramCacheEntryType::Builtin(_)))
.is_some();
if !is_builtin {
ic_msg!(ctx, "MagicRoot: non-builtin caller {}", caller_id);
return Err(InstructionError::CallDepth);
}
}
🤖 Prompt for 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.

In `@programs/magic-root-program/src/processor.rs` around lines 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.

Source: Path instructions

Comment thread programs/magic-root-program/src/processor.rs
Comment on lines +43 to +44
let mut cache = ProgramCacheForTxBatch::default();
let environments = ProgramRuntimeEnvironments::default();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

ProgramCacheForTxBatch and ProgramRuntimeEnvironments are not imported.

The use block at Lines 1-14 imports only ProgramCacheEntry and ProgramCacheEntryType from loaded_programs. Lines 43 and 44 name ProgramCacheForTxBatch and ProgramRuntimeEnvironments without a path and without a glob import. The test module does not compile.

🐛 Proposed fix
     solana_program_runtime::{
-        loaded_programs::{ProgramCacheEntry, ProgramCacheEntryType},
+        loaded_programs::{
+            ProgramCacheEntry, ProgramCacheEntryType, ProgramCacheForTxBatch,
+            ProgramRuntimeEnvironments,
+        },
         solana_sbpf::program::BuiltinFunctionDefinition,
         with_mock_invoke_context,
     },

Confirm the module path of each type in the vendored runtime crate before applying the fix.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let mut cache = ProgramCacheForTxBatch::default();
let environments = ProgramRuntimeEnvironments::default();
use solana_program_runtime::{
loaded_programs::{
ProgramCacheEntry, ProgramCacheEntryType, ProgramCacheForTxBatch,
ProgramRuntimeEnvironments,
},
solana_sbpf::program::BuiltinFunctionDefinition,
with_mock_invoke_context,
};
🤖 Prompt for 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.

In `@programs/magic-root-program/src/tests.rs` around lines 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.

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.

Add MagicRoot and v42 engine program crates

1 participant