-
Notifications
You must be signed in to change notification settings - Fork 15
serviceability-instruction: scaffold pure instruction-builder crate (RFC-26 R0) #4049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juan-malbeclabs
merged 5 commits into
main
from
feat/rfc26-r0-instruction-builder-scaffold
Jul 14, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
db8fc0a
serviceability-instruction: scaffold pure instruction-builder crate (…
juan-malbeclabs aa7be87
serviceability-instruction: address RFC-26 R0 review feedback
juan-malbeclabs 15fff6a
serviceability-instruction: document writable-flag byte-parity in dev…
juan-malbeclabs d55a4e5
Merge branch 'main' into feat/rfc26-r0-instruction-builder-scaffold
juan-malbeclabs 34562c2
Merge branch 'main' into feat/rfc26-r0-instruction-builder-scaffold
juan-malbeclabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| name = "doublezero-serviceability-instruction" | ||
|
|
||
| version.workspace = true | ||
| authors.workspace = true | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
|
|
||
| [lib] | ||
| name = "doublezero_serviceability_instruction" | ||
|
|
||
| # Pure, RPC-free instruction builders for the doublezero-serviceability program | ||
| # (RFC-26). Dependencies are deliberately limited to the onchain program crate | ||
| # and lightweight instruction/compute-budget helpers — NO solana-client, tokio, | ||
| # or backon. This acyclic graph is what enforces purity: the crate cannot reach | ||
| # RPC because it never depends on the RPC tree. | ||
| [dependencies] | ||
| doublezero-serviceability.workspace = true | ||
| solana-program.workspace = true | ||
| solana-system-interface.workspace = true | ||
| solana-compute-budget-interface.workspace = true |
104 changes: 104 additions & 0 deletions
104
crates/doublezero-serviceability-instruction/src/common.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //! The anti-drift keystone. | ||
| //! | ||
| //! [`build`] is the single place that appends the trailing accounts every | ||
| //! serviceability instruction shares, so the payer/system convention lives in | ||
| //! exactly one reviewable location instead of being re-encoded per builder. | ||
|
|
||
| use doublezero_serviceability::instructions::DoubleZeroInstruction; | ||
| use solana_compute_budget_interface::ComputeBudgetInstruction; | ||
| use solana_program::{ | ||
| instruction::{AccountMeta, Instruction}, | ||
| pubkey::Pubkey, | ||
| }; | ||
|
|
||
| /// Protocol-max compute-unit limit for a serviceability transaction. Mirrors | ||
| /// `sdk/rs/src/client.rs::MAX_COMPUTE_UNIT_LIMIT`. | ||
| pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000; | ||
| /// Protocol-max heap-frame request (256 KiB). Mirrors | ||
| /// `sdk/rs/src/client.rs::MAX_HEAP_FRAME_BYTES`. | ||
| pub const MAX_HEAP_FRAME_BYTES: u32 = 256 * 1024; | ||
|
|
||
| /// Assemble a single serviceability [`Instruction`] from its instruction-specific | ||
| /// account metas (in processor order, WITHOUT payer/system) plus the shared | ||
| /// trailing `[payer, system]`. | ||
| /// | ||
| /// This is the permanent **no-permission** path: instructions that never route | ||
| /// through `authorize()` (e.g. `CreateUser`) call this. Instructions that do | ||
| /// route through `authorize()` call [`build_with_permission`] instead. | ||
| /// | ||
| /// The wire encoding is `1 tag byte + borsh(args)`, obtained for free from | ||
| /// [`DoubleZeroInstruction::pack`] — builders never hand-write tag bytes. | ||
| pub(crate) fn build( | ||
| program_id: &Pubkey, | ||
| instruction: DoubleZeroInstruction, | ||
| mut accounts: Vec<AccountMeta>, | ||
| payer: &Pubkey, | ||
| ) -> Instruction { | ||
| accounts.push(AccountMeta::new(*payer, true)); | ||
| // The system program is marked **writable** for byte-parity with today's | ||
| // `client.rs::assemble_instructions` (`AccountMeta::new`); the runtime demotes | ||
| // reserved keys, so this is harmless. Byte-parity is deliberate — the golden | ||
| // fixtures freeze this flag, and diverging from the SDK would defeat the | ||
| // drop-in-replacement goal. The RFC glossary is aligned to match. | ||
| accounts.push(AccountMeta::new( | ||
|
juan-malbeclabs marked this conversation as resolved.
|
||
| solana_system_interface::program::ID, | ||
| false, | ||
| )); | ||
|
|
||
| Instruction::new_with_bytes(*program_id, &instruction.pack(), accounts) | ||
| } | ||
|
|
||
| /// Assemble an instruction whose processor routes through `authorize()` and will | ||
| /// therefore carry a trailing, read-only Permission PDA (derived from the payer, | ||
| /// never a caller-supplied argument) once the permission model is rolled out. | ||
| /// | ||
| /// Builders are **assigned** to this method per-instruction as they are | ||
| /// implemented; the append itself is **deferred** (RFC-26 "Permission account"). | ||
| /// Today it simply delegates to [`build`], so assigning a builder here is | ||
| /// behavior-preserving. At the permission rollout, enable the append **here, in | ||
|
juan-malbeclabs marked this conversation as resolved.
|
||
| /// this one place** — it then activates for every builder already assigned to | ||
| /// this method at once, while builders on [`build`] stay untouched. | ||
| /// | ||
| /// The Permission account MUST be appended **last**: `authorize()` reads it as | ||
| /// the final account and identifies it by PDA match (`get_permission_pda(payer)`) | ||
| /// — so the variable-length / length-detected families tolerate it (the feed / | ||
| /// tenant slots sit ahead of the payer and are never confused with it). To | ||
| /// activate, assemble here instead of delegating: | ||
| /// | ||
| /// ```ignore | ||
| /// accounts.push(AccountMeta::new(*payer, true)); | ||
| /// accounts.push(AccountMeta::new(solana_system_interface::program::ID, false)); | ||
| /// let (permission, _) = | ||
| /// doublezero_serviceability::pda::get_permission_pda(program_id, payer); | ||
| /// accounts.push(AccountMeta::new_readonly(permission, false)); // MUST be last | ||
| /// Instruction::new_with_bytes(*program_id, &instruction.pack(), accounts) | ||
| /// ``` | ||
| /// | ||
| /// **Activation precondition.** This append is a pure, offline operation: it | ||
| /// cannot check whether the payer's Permission PDA actually exists onchain. When | ||
| /// the account does not exist, `authorize()` fails **hard** with | ||
| /// `InvalidAccountData` — the program-ownership check runs before the | ||
| /// foundation-recovery/legacy fallback (see `authorize.rs`), so a missing | ||
| /// Permission account is never routed to the legacy allowlist path. Today's SDK | ||
| /// sidesteps this by appending the PDA only after an RPC existence check, which a | ||
| /// pure builder cannot replicate. The append MUST therefore stay deferred until | ||
| /// every payer is guaranteed a Permission account (e.g. `RequirePermissionAccounts` | ||
| /// enforced), or activating it would break every payer without one. | ||
| pub(crate) fn build_with_permission( | ||
| program_id: &Pubkey, | ||
| instruction: DoubleZeroInstruction, | ||
| accounts: Vec<AccountMeta>, | ||
| payer: &Pubkey, | ||
| ) -> Instruction { | ||
| build(program_id, instruction, accounts, payer) | ||
| } | ||
|
|
||
| /// The transaction-level compute-budget prelude: set the compute-unit limit and | ||
| /// request the heap frame. This is NOT inside each builder — the caller prepends | ||
| /// it once per transaction over the built instruction(s). | ||
| pub fn compute_budget_prelude() -> [Instruction; 2] { | ||
| [ | ||
| ComputeBudgetInstruction::set_compute_unit_limit(MAX_COMPUTE_UNIT_LIMIT), | ||
| ComputeBudgetInstruction::request_heap_frame(MAX_HEAP_FRAME_BYTES), | ||
| ] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.