solana_config_program/
lib.rs

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
#![allow(clippy::arithmetic_side_effects)]
pub mod config_instruction;
pub mod config_processor;
pub mod date_instruction;

pub use solana_sdk::config::program::id;
#[allow(deprecated)]
use solana_sdk::stake::config::Config as StakeConfig;
use {
    bincode::{deserialize, serialize, serialized_size},
    serde_derive::{Deserialize, Serialize},
    solana_sdk::{
        account::{Account, AccountSharedData},
        pubkey::Pubkey,
        short_vec,
    },
};

pub trait ConfigState: serde::Serialize + Default {
    /// Maximum space that the serialized representation will require
    fn max_space() -> u64;
}

// TODO move ConfigState into `solana_program` to implement trait locally
#[allow(deprecated)]
impl ConfigState for StakeConfig {
    fn max_space() -> u64 {
        serialized_size(&StakeConfig::default()).unwrap()
    }
}

/// A collection of keys to be stored in Config account data.
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ConfigKeys {
    // Each key tuple comprises a unique `Pubkey` identifier,
    // and `bool` whether that key is a signer of the data
    #[serde(with = "short_vec")]
    pub keys: Vec<(Pubkey, bool)>,
}

impl ConfigKeys {
    pub fn serialized_size(keys: Vec<(Pubkey, bool)>) -> u64 {
        serialized_size(&ConfigKeys { keys }).unwrap()
    }
}

pub fn get_config_data(bytes: &[u8]) -> Result<&[u8], bincode::Error> {
    deserialize::<ConfigKeys>(bytes)
        .and_then(|keys| serialized_size(&keys))
        .map(|offset| &bytes[offset as usize..])
}

// utility for pre-made Accounts
pub fn create_config_account<T: ConfigState>(
    keys: Vec<(Pubkey, bool)>,
    config_data: &T,
    lamports: u64,
) -> AccountSharedData {
    let mut data = serialize(&ConfigKeys { keys }).unwrap();
    data.extend_from_slice(&serialize(config_data).unwrap());
    AccountSharedData::from(Account {
        lamports,
        data,
        owner: id(),
        ..Account::default()
    })
}