gemachain_program/
feature.rs

1//! Runtime features.
2//!
3//! Runtime features provide a mechanism for features to be simultaneously activated across the
4//! network. Since validators may choose when to upgrade, features must remain dormant until a
5//! sufficient majority of the network is running a version that would support a given feature.
6//!
7//! Feature activation is accomplished by:
8//! 1. Activation is requested by the feature authority, who issues a transaction to create the
9//!    feature account. The newly created feature account will have the value of
10//!    `Feature::default()`
11//! 2. When the next epoch is entered the runtime will check for new activation requests and
12//!    active them.  When this occurs, the activation slot is recorded in the feature account
13
14use crate::{
15    account_info::AccountInfo, clock::Slot, instruction::Instruction, program_error::ProgramError,
16    pubkey::Pubkey, rent::Rent, system_instruction,
17};
18
19crate::declare_id!("Feature111111111111111111111111111111111111");
20
21#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
22pub struct Feature {
23    pub activated_at: Option<Slot>,
24}
25
26impl Feature {
27    pub fn size_of() -> usize {
28        bincode::serialized_size(&Feature {
29            activated_at: Some(0),
30        })
31        .unwrap() as usize
32    }
33
34    pub fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
35        if *account_info.owner != id() {
36            return Err(ProgramError::InvalidArgument);
37        }
38        bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
39    }
40}
41
42/// Activate a feature
43pub fn activate(feature_id: &Pubkey, funding_address: &Pubkey, rent: &Rent) -> Vec<Instruction> {
44    activate_with_carats(
45        feature_id,
46        funding_address,
47        rent.minimum_balance(Feature::size_of()),
48    )
49}
50
51pub fn activate_with_carats(
52    feature_id: &Pubkey,
53    funding_address: &Pubkey,
54    carats: u64,
55) -> Vec<Instruction> {
56    vec![
57        system_instruction::transfer(funding_address, feature_id, carats),
58        system_instruction::allocate(feature_id, Feature::size_of() as u64),
59        system_instruction::assign(feature_id, &id()),
60    ]
61}
62
63#[cfg(test)]
64mod test {
65    use super::*;
66    use gemachain_program::clock::Slot;
67
68    #[test]
69    fn feature_sizeof() {
70        assert!(
71            Feature::size_of() >= bincode::serialized_size(&Feature::default()).unwrap() as usize
72        );
73        assert_eq!(Feature::default(), Feature { activated_at: None });
74
75        let features = [
76            Feature {
77                activated_at: Some(0),
78            },
79            Feature {
80                activated_at: Some(Slot::MAX),
81            },
82        ];
83        for feature in &features {
84            assert_eq!(
85                Feature::size_of(),
86                bincode::serialized_size(feature).unwrap() as usize
87            );
88        }
89    }
90}