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;
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    ScheduledCommitSent(u64),
70    ScheduleBaseIntent(MagicBaseIntentArgs),
71}
72
73impl MagicBlockInstruction {
74    pub fn try_to_vec(&self) -> Result<Vec<u8>, bincode::Error> {
75        bincode::serialize(self)
76    }
77}
78
79#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
80pub struct AccountModification {
81    pub pubkey: Pubkey,
82    pub lamports: Option<u64>,
83    pub owner: Option<Pubkey>,
84    pub executable: Option<bool>,
85    pub data: Option<Vec<u8>>,
86    pub rent_epoch: Option<u64>,
87}
88
89#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
90pub struct AccountModificationForInstruction {
91    pub lamports: Option<u64>,
92    pub owner: Option<Pubkey>,
93    pub executable: Option<bool>,
94    pub data_key: Option<u64>,
95    pub rent_epoch: Option<u64>,
96}