SDK cleanup to reduce featurization (#5856)
This commit is contained in:
parent
468095ede2
commit
61fe1aa9cf
|
@ -8,7 +8,7 @@ use solana_sdk::{
|
|||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
|
||||
match Clock::from(&accounts[2]) {
|
||||
match Clock::from_account_info(&accounts[2]) {
|
||||
Some(clock) => {
|
||||
info!("slot, segment, epoch, stakers_epoch");
|
||||
info!(
|
||||
|
|
|
@ -179,7 +179,7 @@ pub fn process_instruction(
|
|||
me.delegate_stake(
|
||||
vote,
|
||||
stake,
|
||||
&sysvar::clock_account::from_keyed_account(&rest[1])?,
|
||||
&sysvar::clock::from_keyed_account(&rest[1])?,
|
||||
&config::from_keyed_account(&rest[2])?,
|
||||
)
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ pub fn process_instruction(
|
|||
me.withdraw(
|
||||
lamports,
|
||||
&mut to,
|
||||
&sysvar::clock_account::from_keyed_account(&sysvar[0])?,
|
||||
&sysvar::clock::from_keyed_account(&sysvar[0])?,
|
||||
&sysvar::stake_history::from_keyed_account(&sysvar[1])?,
|
||||
)
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ pub fn process_instruction(
|
|||
let vote = &mut vote[0];
|
||||
let clock = &rest[0];
|
||||
|
||||
me.deactivate_stake(vote, &sysvar::clock_account::from_keyed_account(&clock)?)
|
||||
me.deactivate_stake(vote, &sysvar::clock::from_keyed_account(&clock)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ mod tests {
|
|||
.iter()
|
||||
.map(|meta| {
|
||||
if sysvar::clock::check_id(&meta.pubkey) {
|
||||
sysvar::clock_account::new(1, 0, 0, 0, 0)
|
||||
sysvar::clock::new_account(1, 0, 0, 0, 0)
|
||||
} else if sysvar::rewards::check_id(&meta.pubkey) {
|
||||
sysvar::rewards::create_account(1, 0.0, 0.0)
|
||||
} else if sysvar::stake_history::check_id(&meta.pubkey) {
|
||||
|
@ -336,7 +336,7 @@ mod tests {
|
|||
KeyedAccount::new(
|
||||
&sysvar::clock::id(),
|
||||
false,
|
||||
&mut sysvar::clock_account::new(1, 0, 0, 0, 0)
|
||||
&mut sysvar::clock::new_account(1, 0, 0, 0, 0)
|
||||
),
|
||||
KeyedAccount::new(
|
||||
&config::id(),
|
||||
|
|
|
@ -42,7 +42,7 @@ pub fn process_instruction(
|
|||
// This instruction must be signed by `me`
|
||||
Err(InstructionError::InvalidArgument)?;
|
||||
}
|
||||
let clock = sysvar::clock_account::from_keyed_account(&rest[0])?;
|
||||
let clock = sysvar::clock::from_keyed_account(&rest[0])?;
|
||||
storage_account.submit_mining_proof(
|
||||
sha_state,
|
||||
segment_index,
|
||||
|
@ -56,7 +56,7 @@ pub fn process_instruction(
|
|||
// This instruction must be signed by `me`
|
||||
Err(InstructionError::InvalidArgument)?;
|
||||
}
|
||||
let clock = sysvar::clock_account::from_keyed_account(&rest[0])?;
|
||||
let clock = sysvar::clock::from_keyed_account(&rest[0])?;
|
||||
storage_account.advertise_storage_recent_blockhash(hash, segment, clock)
|
||||
}
|
||||
StorageInstruction::ClaimStorageReward => {
|
||||
|
@ -68,7 +68,7 @@ pub fn process_instruction(
|
|||
let (rewards_pools, owner) = rest.split_at_mut(1);
|
||||
|
||||
let rewards = sysvar::rewards::from_keyed_account(&rewards[0])?;
|
||||
let clock = sysvar::clock_account::from_keyed_account(&clock[0])?;
|
||||
let clock = sysvar::clock::from_keyed_account(&clock[0])?;
|
||||
let mut owner = StorageAccount::new(*owner[0].unsigned_key(), &mut owner[0].account);
|
||||
|
||||
storage_account.claim_storage_reward(&mut rewards_pools[0], clock, rewards, &mut owner)
|
||||
|
@ -84,7 +84,7 @@ pub fn process_instruction(
|
|||
Err(InstructionError::InvalidArgument)?;
|
||||
}
|
||||
let me_id = storage_account.id;
|
||||
let clock = sysvar::clock_account::from_keyed_account(&clock[0])?;
|
||||
let clock = sysvar::clock::from_keyed_account(&clock[0])?;
|
||||
let mut rest: Vec<_> = rest
|
||||
.iter_mut()
|
||||
.map(|keyed_account| {
|
||||
|
|
|
@ -15,8 +15,7 @@ use solana_sdk::pubkey::Pubkey;
|
|||
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
|
||||
use solana_sdk::system_instruction;
|
||||
use solana_sdk::sysvar::clock::{self, Clock};
|
||||
use solana_sdk::sysvar::rewards::Rewards;
|
||||
use solana_sdk::sysvar::{clock_account, rewards};
|
||||
use solana_sdk::sysvar::rewards::{self, Rewards};
|
||||
use solana_storage_api::id;
|
||||
use solana_storage_api::storage_contract::StorageAccount;
|
||||
use solana_storage_api::storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE};
|
||||
|
@ -124,8 +123,8 @@ fn test_proof_bounds() {
|
|||
Hash::default(),
|
||||
);
|
||||
// the proof is for segment 0, need to move the slot into segment 2
|
||||
let mut clock_account = clock_account::new(1, 0, 0, 0, 0);
|
||||
Clock::to(
|
||||
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
|
||||
Clock::to_account(
|
||||
&Clock {
|
||||
slot: DEFAULT_SLOTS_PER_SEGMENT * 2,
|
||||
segment: 2,
|
||||
|
@ -152,7 +151,7 @@ fn test_serialize_overflow() {
|
|||
let clock_id = clock::id();
|
||||
let mut keyed_accounts = Vec::new();
|
||||
let mut user_account = Account::default();
|
||||
let mut clock_account = clock_account::new(1, 0, 0, 0, 0);
|
||||
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
|
||||
keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
|
||||
keyed_accounts.push(KeyedAccount::new(&clock_id, false, &mut clock_account));
|
||||
|
||||
|
@ -177,8 +176,8 @@ fn test_invalid_accounts_len() {
|
|||
Hash::default(),
|
||||
);
|
||||
// move tick height into segment 1
|
||||
let mut clock_account = clock_account::new(1, 0, 0, 0, 0);
|
||||
Clock::to(
|
||||
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
|
||||
Clock::to_account(
|
||||
&Clock {
|
||||
slot: 16,
|
||||
segment: 1,
|
||||
|
@ -237,8 +236,8 @@ fn test_submit_mining_ok() {
|
|||
Hash::default(),
|
||||
);
|
||||
// move slot into segment 1
|
||||
let mut clock_account = clock_account::new(1, 0, 0, 0, 0);
|
||||
Clock::to(
|
||||
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
|
||||
Clock::to_account(
|
||||
&Clock {
|
||||
slot: DEFAULT_SLOTS_PER_SEGMENT,
|
||||
segment: 1,
|
||||
|
|
|
@ -154,7 +154,7 @@ pub fn process_instruction(
|
|||
vote_state::process_vote(
|
||||
me,
|
||||
&sysvar::slot_hashes::from_keyed_account(&slot_hashes_and_clock[0])?,
|
||||
&sysvar::clock_account::from_keyed_account(&slot_hashes_and_clock[1])?,
|
||||
&sysvar::clock::from_keyed_account(&slot_hashes_and_clock[1])?,
|
||||
other_signers,
|
||||
&vote,
|
||||
)
|
||||
|
@ -188,7 +188,7 @@ mod tests {
|
|||
.iter()
|
||||
.map(|meta| {
|
||||
if sysvar::clock::check_id(&meta.pubkey) {
|
||||
sysvar::clock_account::new(1, 0, 0, 0, 0)
|
||||
sysvar::clock::new_account(1, 0, 0, 0, 0)
|
||||
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
|
||||
sysvar::slot_hashes::create_account(1, &[])
|
||||
} else {
|
||||
|
|
|
@ -41,7 +41,7 @@ use solana_sdk::{
|
|||
signature::{Keypair, Signature},
|
||||
system_transaction,
|
||||
sysvar::{
|
||||
clock, clock_account, fees, rewards,
|
||||
clock, fees, rewards,
|
||||
slot_hashes::{self, SlotHashes},
|
||||
stake_history,
|
||||
},
|
||||
|
@ -400,7 +400,7 @@ impl Bank {
|
|||
fn update_clock(&self) {
|
||||
self.store_account(
|
||||
&clock::id(),
|
||||
&clock_account::new(
|
||||
&clock::new_account(
|
||||
1,
|
||||
self.slot,
|
||||
get_segment_from_slot(self.slot, self.slots_per_segment),
|
||||
|
|
|
@ -11,17 +11,12 @@ edition = "2018"
|
|||
[features]
|
||||
# On-chain program specific dependencies
|
||||
program = []
|
||||
# Kitchen sink specific dependencies
|
||||
kitchen_sink = [
|
||||
# Dependencies that are not compatible or needed for on-chain programs
|
||||
default = [
|
||||
"assert_matches",
|
||||
"byteorder",
|
||||
"chrono",
|
||||
"generic-array",
|
||||
"itertools",
|
||||
"log",
|
||||
"memmap",
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"rand",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
|
@ -29,7 +24,6 @@ kitchen_sink = [
|
|||
"solana-logger",
|
||||
"untrusted",
|
||||
]
|
||||
default = ["kitchen_sink"]
|
||||
|
||||
[dependencies]
|
||||
assert_matches = { version = "1.3.0", optional = true }
|
||||
|
@ -37,13 +31,13 @@ bincode = "1.1.4"
|
|||
bs58 = "0.2.5"
|
||||
byteorder = { version = "1.3.2", optional = true }
|
||||
chrono = { version = "0.4.9", features = ["serde"], optional = true }
|
||||
generic-array = { version = "0.13.2", default-features = false, features = ["serde", "more_lengths"], optional = true }
|
||||
generic-array = { version = "0.13.2", default-features = false, features = ["serde", "more_lengths"] }
|
||||
hex = "0.3.2"
|
||||
itertools = { version = "0.8.0", optional = true }
|
||||
log = { version = "0.4.8", optional = true }
|
||||
itertools = { version = "0.8.0" }
|
||||
log = { version = "0.4.8" }
|
||||
memmap = { version = "0.6.2", optional = true }
|
||||
num-derive = { version = "0.2", optional = true }
|
||||
num-traits = { version = "0.2", optional = true }
|
||||
num-derive = { version = "0.2" }
|
||||
num-traits = { version = "0.2" }
|
||||
rand = { version = "0.6.5", optional = true }
|
||||
rayon = { version = "1.2.0", optional = true }
|
||||
serde = "1.0.99"
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue