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