SDK cleanup to reduce featurization (#5856)

This commit is contained in:
Jack May
2019-09-09 16:38:52 -07:00
committed by GitHub
parent 468095ede2
commit 61fe1aa9cf
13 changed files with 121 additions and 182 deletions

View File

@ -1,67 +1,44 @@
pub mod account;
pub mod account_utils;
pub mod bpf_loader;
pub mod clock;
pub mod fee_calculator;
pub mod hash;
pub mod inflation;
pub mod instruction;
pub mod instruction_processor_utils;
pub mod loader_instruction;
pub mod message;
pub mod native_loader;
pub mod packet;
pub mod poh_config;
pub mod pubkey;
pub mod rent;
pub mod rpc_port;
pub mod short_vec;
pub mod system_instruction;
pub mod system_program;
pub mod sysvar;
pub mod timing;
// On-chain program modules
#[cfg(feature = "program")]
// On-chain program specific modules
pub mod account_info;
#[cfg(feature = "program")]
pub mod entrypoint;
#[cfg(feature = "program")]
pub mod log;
#[cfg(feature = "program")]
pub mod program_test;
// Kitchen sink modules
#[cfg(feature = "kitchen_sink")]
pub mod account;
#[cfg(feature = "kitchen_sink")]
pub mod account_utils;
#[cfg(feature = "kitchen_sink")]
pub mod bpf_loader;
#[cfg(feature = "kitchen_sink")]
// Modules not usable by on-chain programs
#[cfg(not(feature = "program"))]
pub mod client;
#[cfg(feature = "kitchen_sink")]
pub mod fee_calculator;
#[cfg(feature = "kitchen_sink")]
#[cfg(not(feature = "program"))]
pub mod genesis_block;
#[cfg(feature = "kitchen_sink")]
pub mod hash;
#[cfg(feature = "kitchen_sink")]
pub mod inflation;
#[cfg(feature = "kitchen_sink")]
pub mod instruction;
#[cfg(feature = "kitchen_sink")]
pub mod instruction_processor_utils;
#[cfg(feature = "kitchen_sink")]
pub mod loader_instruction;
#[cfg(feature = "kitchen_sink")]
pub mod message;
#[cfg(feature = "kitchen_sink")]
pub mod native_loader;
#[cfg(feature = "kitchen_sink")]
pub mod packet;
#[cfg(feature = "kitchen_sink")]
pub mod poh_config;
#[cfg(feature = "kitchen_sink")]
pub mod rent;
#[cfg(feature = "kitchen_sink")]
pub mod rpc_port;
#[cfg(feature = "kitchen_sink")]
pub mod short_vec;
#[cfg(feature = "kitchen_sink")]
#[cfg(not(feature = "program"))]
pub mod signature;
#[cfg(feature = "kitchen_sink")]
pub mod system_instruction;
#[cfg(feature = "kitchen_sink")]
pub mod system_program;
#[cfg(feature = "kitchen_sink")]
#[cfg(not(feature = "program"))]
pub mod system_transaction;
#[cfg(feature = "kitchen_sink")]
pub mod timing;
#[cfg(feature = "kitchen_sink")]
#[cfg(not(feature = "program"))]
pub mod transaction;
#[cfg(feature = "kitchen_sink")]
#[cfg(not(feature = "program"))]
pub mod transport;
#[macro_use]

View File

@ -52,7 +52,6 @@ impl Pubkey {
Self::new(&rand::random::<[u8; 32]>())
}
#[cfg(feature = "program")]
pub fn log(&self) {
use crate::log::sol_log_64;
for (i, k) in self.0.iter().enumerate() {

View File

@ -1,7 +1,11 @@
//! This account contains the clock slot, epoch, and stakers_epoch
//!
pub use crate::clock::{Epoch, Segment, Slot};
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::clock::{Epoch, Segment, Slot};
use crate::sysvar;
use bincode::serialized_size;
const ID: [u8; 32] = [
6, 167, 213, 23, 24, 199, 116, 201, 40, 86, 99, 152, 105, 29, 94, 182, 139, 94, 184, 163, 155,
@ -18,3 +22,63 @@ pub struct Clock {
pub epoch: Epoch,
pub stakers_epoch: Epoch,
}
impl Clock {
pub fn size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to_account(&self, account: &mut Account) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn from_account_info(account: &AccountInfo) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to_account_info(&self, account: &mut AccountInfo) -> Option<()> {
account.serialize_data(self).ok()
}
}
pub fn new_account(
lamports: u64,
slot: Slot,
segment: Segment,
epoch: Epoch,
stakers_epoch: Epoch,
) -> Account {
Account::new_data(
lamports,
&Clock {
slot,
segment,
epoch,
stakers_epoch,
},
&sysvar::id(),
)
.unwrap()
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Clock, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Clock::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let account = new_account(1, 0, 0, 0, 0);
let clock = Clock::from_account(&account).unwrap();
assert_eq!(clock, Clock::default());
}
}

View File

@ -1,64 +0,0 @@
//! Serialize clock support for Account
//!
use crate::account::Account;
use crate::sysvar;
use crate::sysvar::clock::{check_id, Clock};
use bincode::serialized_size;
pub use crate::clock::{Epoch, Slot};
impl Clock {
pub fn from(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
}
pub fn new(
lamports: u64,
slot: Slot,
segment: Segment,
epoch: Epoch,
stakers_epoch: Epoch,
) -> Account {
Account::new_data(
lamports,
&Clock {
slot,
segment,
epoch,
stakers_epoch,
},
&sysvar::id(),
)
.unwrap()
}
use crate::account::KeyedAccount;
use crate::clock::Segment;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Clock, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Clock::from(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = new(1, 0, 0, 0, 0);
let clock = Clock::from(&account).unwrap();
assert_eq!(clock, Clock::default());
}
}

View File

@ -1,18 +0,0 @@
//! Serialize clock support for AccountInfo
//!
use crate::account_info::AccountInfo;
use crate::sysvar::clock::Clock;
use bincode::serialized_size;
impl Clock {
pub fn from(account: &AccountInfo) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut AccountInfo) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
}

View File

@ -1,25 +1,13 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
#[cfg(feature = "kitchen_sink")]
use crate::pubkey::Pubkey;
pub mod clock;
#[cfg(feature = "program")]
pub mod clock_account_info;
#[cfg(feature = "kitchen_sink")]
pub mod clock_account;
#[cfg(feature = "kitchen_sink")]
pub mod fees;
#[cfg(feature = "kitchen_sink")]
pub mod rewards;
#[cfg(feature = "kitchen_sink")]
pub mod slot_hashes;
#[cfg(feature = "kitchen_sink")]
pub mod stake_history;
#[cfg(feature = "kitchen_sink")]
pub fn is_sysvar_id(id: &Pubkey) -> bool {
clock::check_id(id) || fees::check_id(id) || rewards::check_id(id) || slot_hashes::check_id(id)
}