magicblock_magic_program_api/
instruction.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use solana_program::pubkey::Pubkey;
5
6use crate::args::{MagicBaseIntentArgs, ScheduleTaskArgs};
7
8#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
9pub enum MagicBlockInstruction {
10    /// Modify one or more accounts
11    ///
12    /// # Account references
13    ///  - **0.**    `[WRITE, SIGNER]` Validator Authority
14    ///  - **1..n.** `[WRITE]` Accounts to modify
15    ///  - **n+1**  `[SIGNER]` (Implicit NativeLoader)
16    ModifyAccounts(HashMap<Pubkey, AccountModificationForInstruction>),
17
18    /// Schedules the accounts provided at end of accounts Vec to be committed.
19    /// It should be invoked from the program whose PDA accounts are to be
20    /// committed.
21    ///
22    /// This is the first part of scheduling a commit.
23    /// A second transaction [MagicBlockInstruction::AcceptScheduleCommits] has to run in order
24    /// to finish scheduling the commit.
25    ///
26    /// # Account references
27    /// - **0.**   `[WRITE, SIGNER]` Payer requesting the commit to be scheduled
28    /// - **1.**   `[WRITE]`         Magic Context Account containing to which we store
29    ///                              the scheduled commits
30    /// - **2..n** `[]`              Accounts to be committed
31    ScheduleCommit,
32
33    /// This is the exact same instruction as [MagicBlockInstruction::ScheduleCommit] except
34    /// that the [ScheduledCommit] is flagged such that when accounts are committed, a request
35    /// to undelegate them is included with the same transaction.
36    /// Additionally the validator will refuse anymore transactions for the specific account
37    /// since they are no longer considered delegated to it.
38    ///
39    /// This is the first part of scheduling a commit.
40    /// A second transaction [MagicBlockInstruction::AcceptScheduleCommits] has to run in order
41    /// to finish scheduling the commit.
42    ///
43    /// # Account references
44    /// - **0.**   `[WRITE, SIGNER]` Payer requesting the commit to be scheduled
45    /// - **1.**   `[WRITE]`         Magic Context Account containing to which we store
46    ///                              the scheduled commits
47    /// - **2..n** `[]`              Accounts to be committed and undelegated
48    ScheduleCommitAndUndelegate,
49
50    /// Moves the scheduled commit from the MagicContext to the global scheduled commits
51    /// map. This is the second part of scheduling a commit.
52    ///
53    /// It is run at the start of the slot to update the global scheduled commits map just
54    /// in time for the validator to realize the commits right after.
55    ///
56    /// # Account references
57    /// - **0.**  `[SIGNER]` Validator Authority
58    /// - **1.**  `[WRITE]`  Magic Context Account containing the initially scheduled commits
59    AcceptScheduleCommits,
60
61    /// Records the attempt to realize a scheduled commit on chain.
62    ///
63    /// The signature of this transaction can be pre-calculated since we pass the
64    /// ID of the scheduled commit and retrieve the signature from a globally
65    /// stored hashmap.
66    ///
67    /// We implement it this way so we can log the signature of this transaction
68    /// as part of the [MagicBlockInstruction::ScheduleCommit] instruction.
69    /// Args: (intent_id, bump) - bump is needed in order to guarantee unique transactions
70    ScheduledCommitSent((u64, u64)),
71    ScheduleBaseIntent(MagicBaseIntentArgs),
72
73    /// Schedule a new task for execution
74    ///
75    /// # Account references
76    /// - **0.**    `[WRITE, SIGNER]` Payer (payer)
77    /// - **1.**    `[WRITE]`         Task context account
78    /// - **2..n**  `[]`              Accounts included in the task
79    ScheduleTask(ScheduleTaskArgs),
80
81    /// Cancel a task
82    ///
83    /// # Account references
84    /// - **0.** `[WRITE, SIGNER]` Task authority
85    /// - **1.** `[WRITE]`         Task context account
86    CancelTask {
87        task_id: u64,
88    },
89
90    /// Process all tasks
91    ///
92    /// # Account references
93    /// - **0.** `[SIGNER]`         Validator authority
94    /// - **1.** `[WRITE]`          Task context account
95    ProcessTasks,
96
97    /// Disables the executable check, needed to modify the data of a program
98    /// in preparation to deploying it via LoaderV4 and to modify its authority.
99    ///
100    /// # Account references
101    /// - **0.** `[SIGNER]`         Validator authority
102    DisableExecutableCheck,
103
104    /// Enables the executable check, and should run after
105    /// a program is deployed with the LoaderV4 and we modified its authority
106    ///
107    /// # Account references
108    /// - **0.** `[SIGNER]`         Validator authority
109    EnableExecutableCheck,
110}
111
112impl MagicBlockInstruction {
113    pub fn try_to_vec(&self) -> Result<Vec<u8>, bincode::Error> {
114        bincode::serialize(self)
115    }
116}
117
118#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
119pub struct AccountModification {
120    pub pubkey: Pubkey,
121    pub lamports: Option<u64>,
122    pub owner: Option<Pubkey>,
123    pub executable: Option<bool>,
124    pub data: Option<Vec<u8>>,
125    // TODO(bmuddha/thlorenz): deprecate rent_epoch
126    // https://github.com/magicblock-labs/magicblock-validator/issues/580
127    pub rent_epoch: Option<u64>,
128    pub delegated: Option<bool>,
129}
130
131#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
132pub struct AccountModificationForInstruction {
133    pub lamports: Option<u64>,
134    pub owner: Option<Pubkey>,
135    pub executable: Option<bool>,
136    pub data_key: Option<u64>,
137    // TODO(bmuddha/thlorenz): deprecate rent_epoch
138    // https://github.com/magicblock-labs/magicblock-validator/issues/580
139    pub rent_epoch: Option<u64>,
140    pub delegated: Option<bool>,
141}