magicblock-delegation-program 1.2.0

Delegation program for the Ephemeral Rollups
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use solana_program::{
    account_info::AccountInfo, bpf_loader_upgradeable,
    bpf_loader_upgradeable::UpgradeableLoaderState, msg,
    program_error::ProgramError, pubkey::Pubkey, system_program, sysvar,
};

use crate::{
    error::DlpError::InvalidAuthority, fees_vault_seeds,
    pda::validator_fees_vault_pda_from_validator,
    validator_fees_vault_seeds_from_validator,
};

/// Errors if:
/// - Account is not owned by expected program.
pub fn load_owned_pda(
    info: &AccountInfo,
    owner: &Pubkey,
    label: &str,
) -> Result<(), ProgramError> {
    if !info.owner.eq(owner) {
        msg!("Invalid account owner for {} ({})", label, info.key);
        return Err(ProgramError::InvalidAccountOwner);
    }

    Ok(())
}

/// Errors if:
/// - Account is not a signer.
pub fn load_signer(
    info: &AccountInfo,
    label: &str,
) -> Result<(), ProgramError> {
    if !info.is_signer {
        msg!("Account needs to be signer {} ({})", label, info.key);
        return Err(ProgramError::MissingRequiredSignature);
    }

    Ok(())
}

/// Errors if:
/// - Address does not match PDA derived from provided seeds.
pub fn load_pda(
    info: &AccountInfo,
    seeds: &[&[u8]],
    program_id: &Pubkey,
    is_writable: bool,
    label: &str,
) -> Result<u8, ProgramError> {
    let pda = Pubkey::find_program_address(seeds, program_id);

    if info.key.ne(&pda.0) {
        msg!("Invalid seeds for {} ({})", label, info.key);
        return Err(ProgramError::InvalidSeeds);
    }

    if is_writable && !info.is_writable {
        msg!("Account {} ({}) needs to be writable", label, info.key);
        return Err(ProgramError::Immutable);
    }

    Ok(pda.1)
}

/// Errors if:
/// - Address does not match PDA derived from provided seeds.
/// - Cannot load as an uninitialized account.
pub fn load_uninitialized_pda(
    info: &AccountInfo,
    seeds: &[&[u8]],
    program_id: &Pubkey,
    is_writable: bool,
    label: &str,
) -> Result<u8, ProgramError> {
    let pda = Pubkey::find_program_address(seeds, program_id);

    if info.key.ne(&pda.0) {
        msg!("Invalid seeds for account: {} ({})", label, info.key);
        return Err(ProgramError::InvalidSeeds);
    }

    load_uninitialized_account(info, is_writable, label)?;
    Ok(pda.1)
}

/// Errors if:
/// - Address does not match PDA derived from provided seeds.
/// - Owner is not the expected program.
/// - Account is not writable if set to writable.
pub fn load_initialized_pda(
    info: &AccountInfo,
    seeds: &[&[u8]],
    program_id: &Pubkey,
    is_writable: bool,
    label: &str,
) -> Result<u8, ProgramError> {
    let pda = Pubkey::find_program_address(seeds, program_id);

    if info.key.ne(&pda.0) {
        msg!("Invalid seeds for account: {}", info.key);
        return Err(ProgramError::InvalidSeeds);
    }

    load_owned_pda(info, program_id, label)?;

    if is_writable && !info.is_writable {
        msg!("Account {} is not writable", info.key);
        return Err(ProgramError::Immutable);
    }

    Ok(pda.1)
}

/// Errors if:
/// - Owner is not the system program.
/// - Data is not empty.
/// - Account is not writable.
#[allow(dead_code)]
pub fn load_uninitialized_account(
    info: &AccountInfo,
    is_writable: bool,
    label: &str,
) -> Result<(), ProgramError> {
    if info.owner.ne(&system_program::id()) {
        msg!(
            "Invalid owner for account: {}, account: {}, owner: {}",
            label,
            info.key,
            info.owner
        );
        return Err(ProgramError::InvalidAccountOwner);
    }

    if !info.data_is_empty() {
        msg!("Account {} ({}) needs to be uninitialized", label, info.key);
        return Err(ProgramError::AccountAlreadyInitialized);
    }

    if is_writable && !info.is_writable {
        msg!("Account {} ({}) needs to be writable", label, info.key);
        return Err(ProgramError::Immutable);
    }

    Ok(())
}

/// Errors if:
/// - Owner is not the sysvar address.
/// - Account cannot load with the expected address.
#[allow(dead_code)]
pub fn load_sysvar(
    info: &AccountInfo,
    key: Pubkey,
) -> Result<(), ProgramError> {
    if info.owner.ne(&sysvar::id()) {
        msg!("Invalid owner for sysvar: {}", info.key);
        return Err(ProgramError::InvalidAccountOwner);
    }

    load_account(info, key, false, "sysvar")
}

/// Errors if:
/// - Address does not match the expected value.
/// - Expected to be writable, but is not.
pub fn load_account(
    info: &AccountInfo,
    key: Pubkey,
    is_writable: bool,
    label: &str,
) -> Result<(), ProgramError> {
    if info.key.ne(&key) {
        msg!("Expected key {} for {}, but got {}", key, label, info.key);
        return Err(ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        msg!("Account {} ({}) needs to be writable", label, info.key);
        return Err(ProgramError::Immutable);
    }

    Ok(())
}

/// Errors if:
/// - Address does not match the expected value.
/// - Account is not executable.
pub fn load_program(
    info: &AccountInfo,
    key: Pubkey,
    label: &str,
) -> Result<(), ProgramError> {
    if info.key.ne(&key) {
        msg!("Invalid program account: {} ({})", label, info.key);
        return Err(ProgramError::IncorrectProgramId);
    }

    if !info.executable {
        msg!("{} program is not executable: {}", label, info.key);
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}

/// Get the program upgrade authority for a given program
pub fn load_program_upgrade_authority(
    program: &Pubkey,
    program_data: &AccountInfo,
) -> Result<Option<Pubkey>, ProgramError> {
    let program_data_address = Pubkey::find_program_address(
        &[program.as_ref()],
        &bpf_loader_upgradeable::id(),
    )
    .0;

    // During tests, the upgrade authority is a test pubkey
    #[cfg(feature = "unit_test_config")]
    if program.eq(&crate::ID) {
        return Ok(Some(crate::consts::DEFAULT_VALIDATOR_IDENTITY));
    }

    if !program_data_address.eq(program_data.key) {
        msg!(
            "Expected program data address to be {}, but got {}",
            program_data_address,
            program_data.key
        );
        return Err(ProgramError::InvalidAccountData);
    }

    let program_account_data = program_data.try_borrow_data()?;
    if let UpgradeableLoaderState::ProgramData {
        upgrade_authority_address,
        ..
    } = bincode::deserialize(&program_account_data).map_err(|_| {
        msg!("Unable to deserialize ProgramData {}", program);
        ProgramError::InvalidAccountData
    })? {
        Ok(upgrade_authority_address)
    } else {
        msg!("Expected program account {} to hold ProgramData", program);
        Err(ProgramError::InvalidAccountData)
    }
}

/// Load fee vault PDA
/// - Protocol fees vault PDA
pub fn load_initialized_protocol_fees_vault(
    fees_vault: &AccountInfo,
    is_writable: bool,
) -> Result<(), ProgramError> {
    load_initialized_pda(
        fees_vault,
        fees_vault_seeds!(),
        &crate::id(),
        is_writable,
        "protocol fees vault",
    )?;
    Ok(())
}

/// Load validator fee vault PDA
/// - Validator fees vault PDA must be derived from the validator pubkey
/// - Validator fees vault PDA must be initialized with the expected seeds and owner
pub fn load_initialized_validator_fees_vault(
    validator: &AccountInfo,
    validator_fees_vault: &AccountInfo,
    is_writable: bool,
) -> Result<(), ProgramError> {
    let pda = validator_fees_vault_pda_from_validator(validator.key);
    if !pda.eq(validator_fees_vault.key) {
        msg!(
            "Invalid validator fees vault PDA, expected {} but got {}",
            pda,
            validator_fees_vault.key
        );
        return Err(InvalidAuthority.into());
    }
    load_initialized_pda(
        validator_fees_vault,
        validator_fees_vault_seeds_from_validator!(validator.key),
        &crate::id(),
        is_writable,
        "validator fees vault",
    )?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use solana_program::{
        account_info::AccountInfo, pubkey::Pubkey, system_program,
    };

    use super::load_program;
    use crate::processor::utils::loaders::{
        load_account, load_signer, load_sysvar, load_uninitialized_account,
    };

    #[test]
    pub fn test_signer_not_signer() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_signer(&info, "not signer").is_err());
    }

    #[test]
    pub fn test_load_uninitialized_account_bad_owner() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = crate::id();
        let info = AccountInfo::new(
            &key,
            false,
            true,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_uninitialized_account(&info, true, "bad owner").is_err());
    }

    #[test]
    pub fn test_load_uninitialized_account_data_not_empty() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [0];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            true,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(
            load_uninitialized_account(&info, true, "data not empty").is_err()
        );
    }

    #[test]
    pub fn test_load_uninitialized_account_not_writeable() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(
            load_uninitialized_account(&info, true, "not writeable").is_err()
        );
    }

    #[test]
    pub fn test_load_uninitialized_account_not_writeable_on_purpose() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(
            load_uninitialized_account(&info, false, "not writable").is_ok()
        );
    }

    #[test]
    pub fn test_load_sysvar_bad_owner() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_sysvar(&info, key).is_err());
    }

    #[test]
    pub fn test_load_account_bad_key() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_account(&info, Pubkey::new_unique(), false, "bad key")
            .is_err());
    }

    #[test]
    pub fn test_load_account_not_writeable() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_account(&info, key, true, "not writeable").is_err());
    }

    #[test]
    pub fn test_load_program_bad_key() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            true,
            0,
        );
        assert!(load_program(&info, Pubkey::new_unique(), "bad key").is_err());
    }

    #[test]
    pub fn test_load_program_not_executable() {
        let key = Pubkey::new_unique();
        let mut lamports = 1_000_000_000;
        let mut data = [];
        let owner = system_program::id();
        let info = AccountInfo::new(
            &key,
            false,
            false,
            &mut lamports,
            &mut data,
            &owner,
            false,
            0,
        );
        assert!(load_program(&info, key, "not executable").is_err());
    }
}