1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Instructions for the v4 built-in loader program.
use crate::{
account::AccountMeta, bpf_loader::BPF_LOADER_ID, instruction::Instruction, pubkey::Pubkey,
};
#[repr(u8)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum LoaderInstruction {
/// Write ELF data into an undeployed program account.
///
/// # Account references
/// 0. `[writable]` The program account to write to.
/// 1. `[signer]` The authority of the program.
Write {
/// Offset at which to write the given bytes.
offset: u32,
/// Serialized program data
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
},
/// Changes the size of an undeployed program account.
///
/// A program account is automatically initialized when its size is first increased.
/// In this initial truncate, the program account needs to be a signer and
/// it also sets the authority needed for subsequent operations.
/// Decreasing to size zero closes the program account and resets it
/// into an uninitialized state.
/// Providing additional lamports upfront might be necessary to reach rent exemption.
/// Superflous funds are transferred to the recipient account.
///
/// # Account references
/// 0. `[(signer), writable]` The program account to change the size of.
/// 1. `[signer]` The authority of the program.
/// 2. `[writable]` Optional, the recipient account.
Truncate {
/// The new size after the operation.
new_size: u32,
},
/// Verify the data of a program account to be a valid ELF.
///
/// If this succeeds the program becomes executable, and is ready to use.
/// A source program account can be provided to overwrite the data before deployment
/// in one step, instead retracting the program and writing to it and redeploying it.
/// The source program is truncated to zero (thus closed) and lamports necessary for
/// rent exemption are transferred, in case that the source was bigger than the program.
///
/// # Account references
/// 0. `[writable]` The program account to deploy.
/// 1. `[signer]` The authority of the program.
/// 2. `[writable]` Optional, an undeployed source program account to take data and lamports from.
Deploy,
/// Undo the deployment of a program account.
///
/// The program is no longer executable and goes into maintenance.
/// Necessary for writing data and truncating.
///
/// # Account references
/// 0. `[writable]` The program account to retract.
/// 1. `[signer]` The authority of the program.
Retract,
/// Transfers the authority over a program account.
///
/// # Account references
/// 0. `[writable]` The program account to change the authority of.
/// 1. `[signer]` The current authority of the program.
/// 2. `[signer]` The new authority of the program.
TransferAuthority,
/// Finalizes the program account, rendering it immutable.
///
/// # Account references
/// 0. `[writable]` The program account to change the authority of.
/// 1. `[signer]` The current authority of the program.
/// 2. `[]` The next version of the program (can be itself).
Finalize,
}
pub fn write(
program_account: Pubkey,
authority: Pubkey,
offset: u32,
bytes: Vec<u8>,
) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, false),
AccountMeta::new_readonly(authority, true),
],
data: bincode::serialize(&LoaderInstruction::Write { offset, bytes }).unwrap(),
}
}
pub fn truncate(program_account: Pubkey, authority: Pubkey, new_size: u32) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, true),
AccountMeta::new_readonly(authority, true),
],
data: bincode::serialize(&LoaderInstruction::Truncate { new_size }).unwrap(),
}
}
pub fn deploy(program_account: Pubkey, authority: Pubkey) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, false),
AccountMeta::new_readonly(authority, true),
],
data: bincode::serialize(&LoaderInstruction::Deploy).unwrap(),
}
}
pub fn retract(program_account: Pubkey, authority: Pubkey) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, false),
AccountMeta::new_readonly(authority, true),
],
data: bincode::serialize(&LoaderInstruction::Retract).unwrap(),
}
}
pub fn transfer_authority(
program_account: Pubkey,
current_authority: Pubkey,
new_authority: Pubkey,
) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, true),
AccountMeta::new_readonly(current_authority, true),
AccountMeta::new_readonly(new_authority, true),
],
data: bincode::serialize(&LoaderInstruction::TransferAuthority).unwrap(),
}
}
pub fn finalize(program_account: Pubkey, authority: Pubkey, next_version: Pubkey) -> Instruction {
Instruction {
program_id: BPF_LOADER_ID,
accounts: vec![
AccountMeta::new(program_account, true),
AccountMeta::new_readonly(authority, true),
AccountMeta::new_readonly(next_version, false),
],
data: bincode::serialize(&LoaderInstruction::Finalize).unwrap(),
}
}
pub fn is_write_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 0 == instruction_data[0]
}
pub fn is_truncate_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 1 == instruction_data[0]
}
pub fn is_deploy_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 2 == instruction_data[0]
}
pub fn is_retract_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 3 == instruction_data[0]
}
pub fn is_transfer_authority_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 4 == instruction_data[0]
}
pub fn is_finalize_instruction(instruction_data: &[u8]) -> bool {
!instruction_data.is_empty() && 5 == instruction_data[0]
}