Move Feature struct to solana-program

This commit is contained in:
Michael Vines
2020-10-30 13:40:55 -07:00
parent 8415c22b59
commit 4b65e32f22
10 changed files with 140 additions and 105 deletions

View File

@ -0,0 +1,86 @@
/// Runtime features.
///
/// Feature activation is accomplished by:
/// 1. Activation is requested by the feature authority, who issues a transaction to create the
/// feature account. The newly created feature account will have the value of
/// `Feature::default()`
/// 2. When the next epoch is entered the runtime will check for new activation requests and
/// active them. When this occurs, the activation slot is recorded in the feature account
///
use crate::{
account_info::AccountInfo, clock::Slot, instruction::Instruction, program_error::ProgramError,
pubkey::Pubkey, rent::Rent, system_instruction,
};
crate::declare_id!("Feature111111111111111111111111111111111111");
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub struct Feature {
pub activated_at: Option<Slot>,
}
impl Feature {
pub fn size_of() -> usize {
bincode::serialized_size(&Feature {
activated_at: Some(0),
})
.unwrap() as usize
}
pub fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
if *account_info.owner != id() {
return Err(ProgramError::InvalidArgument);
}
bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
}
}
/// Activate a feature
pub fn activate(feature_id: &Pubkey, funding_address: &Pubkey, rent: &Rent) -> Vec<Instruction> {
activate_with_lamports(
feature_id,
funding_address,
rent.minimum_balance(Feature::size_of()),
)
}
pub fn activate_with_lamports(
feature_id: &Pubkey,
funding_address: &Pubkey,
lamports: u64,
) -> Vec<Instruction> {
vec![
system_instruction::transfer(funding_address, feature_id, lamports),
system_instruction::allocate(feature_id, Feature::size_of() as u64),
system_instruction::assign(feature_id, &id()),
]
}
#[cfg(test)]
mod test {
use super::*;
use solana_program::clock::Slot;
#[test]
fn feature_sizeof() {
assert!(
Feature::size_of() >= bincode::serialized_size(&Feature::default()).unwrap() as usize
);
assert_eq!(Feature::default(), Feature { activated_at: None });
let features = [
Feature {
activated_at: Some(0),
},
Feature {
activated_at: Some(Slot::MAX),
},
];
for feature in &features {
assert_eq!(
Feature::size_of(),
bincode::serialized_size(feature).unwrap() as usize
);
}
}
}

View File

@ -12,6 +12,7 @@ pub mod decode_error;
pub mod entrypoint;
pub mod entrypoint_deprecated;
pub mod epoch_schedule;
pub mod feature;
pub mod fee_calculator;
pub mod hash;
pub mod incinerator;

View File

@ -1,44 +1,23 @@
use solana_sdk::{account::Account, clock::Slot};
use crate::account::Account;
pub use solana_program::feature::*;
solana_sdk::declare_id!("Feature111111111111111111111111111111111111");
/// The `Feature` struct is the on-chain representation of a runtime feature.
///
/// Feature activation is accomplished by:
/// 1. Activation is requested by the feature authority, who issues a transaction to create the
/// feature account. The newly created feature account will have the value of
/// `Feature::default()`
/// 2. When the next epoch is entered the runtime will check for new activation requests and
/// active them. When this occurs, the activation slot is recorded in the feature account
///
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub struct Feature {
pub activated_at: Option<Slot>,
pub fn from_account(account: &Account) -> Option<Feature> {
if account.owner != id() {
None
} else {
bincode::deserialize(&account.data).ok()
}
}
impl Feature {
pub fn size_of() -> usize {
bincode::serialized_size(&Self {
activated_at: Some(0),
})
.unwrap() as usize
}
pub fn from_account(account: &Account) -> Option<Self> {
if account.owner != id() {
None
} else {
bincode::deserialize(&account.data).ok()
}
}
pub fn to_account(&self, account: &mut Account) -> Option<()> {
bincode::serialize_into(&mut account.data[..], self).ok()
}
pub fn create_account(&self, lamports: u64) -> Account {
let data_len = Self::size_of().max(bincode::serialized_size(self).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &id());
self.to_account(&mut account).unwrap();
account
}
pub fn to_account(feature: &Feature, account: &mut Account) -> Option<()> {
bincode::serialize_into(&mut account.data[..], feature).ok()
}
pub fn create_account(feature: &Feature, lamports: u64) -> Account {
let data_len = Feature::size_of().max(bincode::serialized_size(feature).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &id());
to_account(feature, &mut account).unwrap();
account
}
#[cfg(test)]
@ -49,31 +28,8 @@ mod test {
fn feature_deserialize_none() {
let just_initialized = Account::new(42, Feature::size_of(), &id());
assert_eq!(
Feature::from_account(&just_initialized),
from_account(&just_initialized),
Some(Feature { activated_at: None })
);
}
#[test]
fn feature_sizeof() {
assert!(
Feature::size_of() >= bincode::serialized_size(&Feature::default()).unwrap() as usize
);
assert_eq!(Feature::default(), Feature { activated_at: None });
let features = [
Feature {
activated_at: Some(0),
},
Feature {
activated_at: Some(Slot::MAX),
},
];
for feature in &features {
assert_eq!(
Feature::size_of(),
bincode::serialized_size(feature).unwrap() as usize
);
}
}
}