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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use {
    serde::Serialize,
    solana_sdk::{
        bpf_loader_upgradeable::{self, UpgradeableLoaderState},
        client::Client,
        instruction::{AccountMeta, Instruction},
        loader_instruction,
        message::Message,
        pubkey::Pubkey,
        signature::{Keypair, Signer},
        system_instruction,
    },
    std::{env, fs::File, io::Read, path::PathBuf},
};

const CHUNK_SIZE: usize = 512; // Size of chunk just needs to fit into tx

pub fn load_program_from_file(name: &str) -> Vec<u8> {
    let mut pathbuf = {
        let current_exe = env::current_exe().unwrap();
        PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
    };
    pathbuf.push("bpf/");
    pathbuf.push(name);
    pathbuf.set_extension("so");
    let mut file = File::open(&pathbuf).unwrap_or_else(|err| {
        panic!("Failed to open {}: {}", pathbuf.display(), err);
    });
    let mut program = Vec::new();
    file.read_to_end(&mut program).unwrap();
    program
}

pub fn load_and_finalize_deprecated_program<T: Client>(
    bank_client: &T,
    loader_id: &Pubkey,
    program_keypair: Option<Keypair>,
    payer_keypair: &Keypair,
    name: &str,
) -> (Keypair, Instruction) {
    let program = load_program_from_file(name);
    let program_keypair = program_keypair.unwrap_or_else(|| {
        let program_keypair = Keypair::new();
        let instruction = system_instruction::create_account(
            &payer_keypair.pubkey(),
            &program_keypair.pubkey(),
            1.max(
                bank_client
                    .get_minimum_balance_for_rent_exemption(program.len())
                    .unwrap(),
            ),
            program.len() as u64,
            loader_id,
        );
        let message = Message::new(&[instruction], Some(&payer_keypair.pubkey()));
        bank_client
            .send_and_confirm_message(&[payer_keypair, &program_keypair], message)
            .unwrap();
        program_keypair
    });
    let chunk_size = CHUNK_SIZE;
    let mut offset = 0;
    for chunk in program.chunks(chunk_size) {
        let instruction =
            loader_instruction::write(&program_keypair.pubkey(), loader_id, offset, chunk.to_vec());
        let message = Message::new(&[instruction], Some(&payer_keypair.pubkey()));
        bank_client
            .send_and_confirm_message(&[payer_keypair, &program_keypair], message)
            .unwrap();
        offset += chunk_size as u32;
    }
    let instruction = loader_instruction::finalize(&program_keypair.pubkey(), loader_id);
    (program_keypair, instruction)
}

pub fn create_deprecated_program<T: Client>(
    bank_client: &T,
    loader_id: &Pubkey,
    payer_keypair: &Keypair,
    name: &str,
) -> Pubkey {
    let (program_keypair, instruction) =
        load_and_finalize_deprecated_program(bank_client, loader_id, None, payer_keypair, name);
    let message = Message::new(&[instruction], Some(&payer_keypair.pubkey()));
    bank_client
        .send_and_confirm_message(&[payer_keypair, &program_keypair], message)
        .unwrap();
    program_keypair.pubkey()
}

pub fn load_upgradeable_buffer<T: Client>(
    bank_client: &T,
    from_keypair: &Keypair,
    buffer_keypair: &Keypair,
    buffer_authority_keypair: &Keypair,
    name: &str,
) -> Vec<u8> {
    let program = load_program_from_file(name);
    let buffer_pubkey = buffer_keypair.pubkey();
    let buffer_authority_pubkey = buffer_authority_keypair.pubkey();

    bank_client
        .send_and_confirm_message(
            &[from_keypair, buffer_keypair],
            Message::new(
                &bpf_loader_upgradeable::create_buffer(
                    &from_keypair.pubkey(),
                    &buffer_pubkey,
                    &buffer_authority_pubkey,
                    1.max(
                        bank_client
                            .get_minimum_balance_for_rent_exemption(program.len())
                            .unwrap(),
                    ),
                    program.len(),
                )
                .unwrap(),
                Some(&from_keypair.pubkey()),
            ),
        )
        .unwrap();

    let chunk_size = CHUNK_SIZE;
    let mut offset = 0;
    for chunk in program.chunks(chunk_size) {
        let message = Message::new(
            &[bpf_loader_upgradeable::write(
                &buffer_pubkey,
                &buffer_authority_pubkey,
                offset,
                chunk.to_vec(),
            )],
            Some(&from_keypair.pubkey()),
        );
        bank_client
            .send_and_confirm_message(&[from_keypair, buffer_authority_keypair], message)
            .unwrap();
        offset += chunk_size as u32;
    }

    program
}

pub fn load_upgradeable_program<T: Client>(
    bank_client: &T,
    from_keypair: &Keypair,
    buffer_keypair: &Keypair,
    executable_keypair: &Keypair,
    authority_keypair: &Keypair,
    name: &str,
) {
    let program = load_upgradeable_buffer(
        bank_client,
        from_keypair,
        buffer_keypair,
        authority_keypair,
        name,
    );

    let message = Message::new(
        &bpf_loader_upgradeable::deploy_with_max_program_len(
            &from_keypair.pubkey(),
            &executable_keypair.pubkey(),
            &buffer_keypair.pubkey(),
            &authority_keypair.pubkey(),
            1.max(
                bank_client
                    .get_minimum_balance_for_rent_exemption(
                        UpgradeableLoaderState::size_of_program(),
                    )
                    .unwrap(),
            ),
            program.len() * 2,
        )
        .unwrap(),
        Some(&from_keypair.pubkey()),
    );
    bank_client
        .send_and_confirm_message(
            &[from_keypair, executable_keypair, authority_keypair],
            message,
        )
        .unwrap();
}

pub fn upgrade_program<T: Client>(
    bank_client: &T,
    payer_keypair: &Keypair,
    buffer_keypair: &Keypair,
    executable_pubkey: &Pubkey,
    authority_keypair: &Keypair,
    name: &str,
) {
    load_upgradeable_buffer(
        bank_client,
        payer_keypair,
        buffer_keypair,
        authority_keypair,
        name,
    );
    let message = Message::new(
        &[bpf_loader_upgradeable::upgrade(
            executable_pubkey,
            &buffer_keypair.pubkey(),
            &authority_keypair.pubkey(),
            &payer_keypair.pubkey(),
        )],
        Some(&payer_keypair.pubkey()),
    );
    bank_client
        .send_and_confirm_message(&[payer_keypair, authority_keypair], message)
        .unwrap();
}

pub fn set_upgrade_authority<T: Client>(
    bank_client: &T,
    from_keypair: &Keypair,
    program_pubkey: &Pubkey,
    current_authority_keypair: &Keypair,
    new_authority_pubkey: Option<&Pubkey>,
) {
    let message = Message::new(
        &[bpf_loader_upgradeable::set_upgrade_authority(
            program_pubkey,
            &current_authority_keypair.pubkey(),
            new_authority_pubkey,
        )],
        Some(&from_keypair.pubkey()),
    );
    bank_client
        .send_and_confirm_message(&[from_keypair, current_authority_keypair], message)
        .unwrap();
}

// Return an Instruction that invokes `program_id` with `data` and required
// a signature from `from_pubkey`.
pub fn create_invoke_instruction<T: Serialize>(
    from_pubkey: Pubkey,
    program_id: Pubkey,
    data: &T,
) -> Instruction {
    let account_metas = vec![AccountMeta::new(from_pubkey, true)];
    Instruction::new_with_bincode(program_id, data, account_metas)
}