sysvar trait (#6667)

* sysvar trait

* get the new guy in on it
This commit is contained in:
Rob Walker
2019-11-04 12:31:24 -08:00
committed by GitHub
parent b9b535c30f
commit efe260f12e
18 changed files with 183 additions and 303 deletions

View File

@ -10,7 +10,7 @@ use solana_sdk::{
rent,
sysvar::{
clock::Clock, fees::Fees, rent::Rent, rewards::Rewards, slot_hashes::SlotHashes,
stake_history::StakeHistory,
stake_history::StakeHistory, Sysvar,
},
};

View File

@ -10,7 +10,10 @@ use solana_sdk::{
instruction::{AccountMeta, Instruction, InstructionError},
instruction_processor_utils::{limited_deserialize, next_keyed_account, DecodeError},
pubkey::Pubkey,
system_instruction, sysvar,
system_instruction,
sysvar::{
self, clock::Clock, rent::Rent, rewards::Rewards, stake_history::StakeHistory, Sysvar,
},
};
/// Reasons the stake might have had an error
@ -330,7 +333,7 @@ pub fn process_instruction(
StakeInstruction::Initialize(authorized, lockup) => me.initialize(
&authorized,
&lockup,
&sysvar::rent::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&Rent::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
),
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => {
me.authorize(&authorized_pubkey, stake_authorize, &signers)
@ -340,7 +343,7 @@ pub fn process_instruction(
me.delegate_stake(
&vote,
&sysvar::clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&Clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&config::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&signers,
)
@ -352,8 +355,8 @@ pub fn process_instruction(
me.redeem_vote_credits(
vote,
rewards_pool,
&sysvar::rewards::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&sysvar::stake_history::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&Rewards::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&StakeHistory::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
)
}
StakeInstruction::Split(lamports) => {
@ -366,13 +369,13 @@ pub fn process_instruction(
me.withdraw(
lamports,
to,
&sysvar::clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&sysvar::stake_history::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&Clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&StakeHistory::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&signers,
)
}
StakeInstruction::Deactivate => me.deactivate_stake(
&sysvar::clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&Clock::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
&signers,
),
}
@ -390,7 +393,7 @@ mod tests {
.iter()
.map(|meta| {
if sysvar::clock::check_id(&meta.pubkey) {
sysvar::clock::new_account(1, 0, 0, 0, 0)
sysvar::clock::create_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) {
@ -614,7 +617,7 @@ mod tests {
KeyedAccount::new(
&sysvar::clock::id(),
false,
&mut sysvar::clock::new_account(1, 0, 0, 0, 0)
&mut sysvar::clock::create_account(1, 0, 0, 0, 0)
),
KeyedAccount::new(
&config::id(),

View File

@ -11,7 +11,7 @@ use solana_sdk::{
message::Message,
pubkey::Pubkey,
signature::{Keypair, KeypairUtil},
sysvar::{self, rewards::Rewards},
sysvar::{self, rewards::Rewards, Sysvar},
};
use solana_stake_api::{
id,

View File

@ -1,13 +1,14 @@
//! storage program
//! Receive mining proofs from miners, validate the answers
//! and give reward for good proofs.
use crate::storage_contract::StorageAccount;
use crate::storage_instruction::StorageInstruction;
use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
use solana_sdk::instruction_processor_utils::limited_deserialize;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::sysvar;
use crate::{storage_contract::StorageAccount, storage_instruction::StorageInstruction};
use solana_sdk::{
account::KeyedAccount,
instruction::InstructionError,
instruction_processor_utils::limited_deserialize,
pubkey::Pubkey,
sysvar::{clock::Clock, rewards::Rewards, Sysvar},
};
pub fn process_instruction(
_program_id: &Pubkey,
@ -40,7 +41,7 @@ pub fn process_instruction(
// This instruction must be signed by `me`
return Err(InstructionError::InvalidArgument);
}
let clock = sysvar::clock::from_keyed_account(&rest[0])?;
let clock = Clock::from_keyed_account(&rest[0])?;
storage_account.submit_mining_proof(
sha_state,
segment_index,
@ -54,7 +55,7 @@ pub fn process_instruction(
// This instruction must be signed by `me`
return Err(InstructionError::InvalidArgument);
}
let clock = sysvar::clock::from_keyed_account(&rest[0])?;
let clock = Clock::from_keyed_account(&rest[0])?;
storage_account.advertise_storage_recent_blockhash(hash, segment, clock)
}
StorageInstruction::ClaimStorageReward => {
@ -65,8 +66,8 @@ pub fn process_instruction(
let (rewards, rest) = rest.split_at_mut(1);
let (rewards_pools, owner) = rest.split_at_mut(1);
let rewards = sysvar::rewards::from_keyed_account(&rewards[0])?;
let clock = sysvar::clock::from_keyed_account(&clock[0])?;
let rewards = Rewards::from_keyed_account(&rewards[0])?;
let clock = 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)
@ -82,7 +83,7 @@ pub fn process_instruction(
return Err(InstructionError::InvalidArgument);
}
let me_id = storage_account.id;
let clock = sysvar::clock::from_keyed_account(&clock[0])?;
let clock = Clock::from_keyed_account(&clock[0])?;
let mut rest: Vec<_> = rest
.iter_mut()
.map(|keyed_account| {

View File

@ -17,8 +17,11 @@ use solana_sdk::{
pubkey::Pubkey,
signature::{Keypair, KeypairUtil, Signature},
system_instruction,
sysvar::clock::{self, Clock},
sysvar::rewards::{self, Rewards},
sysvar::{
clock::{self, Clock},
rewards::{self, Rewards},
Sysvar,
},
};
use solana_storage_api::{
id,
@ -131,13 +134,12 @@ 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::new_account(1, 0, 0, 0, 0);
let mut clock_account = clock::create_account(1, 0, 0, 0, 0);
Clock::to_account(
&Clock {
slot: DEFAULT_SLOTS_PER_SEGMENT * 2,
segment: 2,
epoch: 0,
leader_schedule_epoch: 0,
..Clock::default()
},
&mut clock_account,
);
@ -159,7 +161,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::new_account(1, 0, 0, 0, 0);
let mut clock_account = clock::create_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));
@ -184,13 +186,12 @@ fn test_invalid_accounts_len() {
Hash::default(),
);
// move tick height into segment 1
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
let mut clock_account = clock::create_account(1, 0, 0, 0, 0);
Clock::to_account(
&Clock {
slot: 16,
segment: 1,
epoch: 0,
leader_schedule_epoch: 0,
..Clock::default()
},
&mut clock_account,
);
@ -244,13 +245,12 @@ fn test_submit_mining_ok() {
Hash::default(),
);
// move slot into segment 1
let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
let mut clock_account = clock::create_account(1, 0, 0, 0, 0);
Clock::to_account(
&Clock {
slot: DEFAULT_SLOTS_PER_SEGMENT,
segment: 1,
epoch: 0,
leader_schedule_epoch: 0,
..Clock::default()
},
&mut clock_account,
);

View File

@ -15,7 +15,7 @@ use solana_sdk::{
instruction_processor_utils::{limited_deserialize, DecodeError},
pubkey::Pubkey,
system_instruction,
sysvar::{self, rent},
sysvar::{self, clock::Clock, slot_hashes::SlotHashes, Sysvar},
};
/// Reasons the stake might have had an error
@ -182,7 +182,7 @@ pub fn process_instruction(
if rest.is_empty() {
return Err(InstructionError::InvalidInstructionData);
}
rent::verify_rent_exemption(me, &rest[0])?;
sysvar::rent::verify_rent_exemption(me, &rest[0])?;
vote_state::initialize_account(me, &vote_init)
}
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
@ -197,8 +197,8 @@ pub fn process_instruction(
vote_state::process_vote(
me,
&sysvar::slot_hashes::from_keyed_account(&slot_hashes_and_clock[0])?,
&sysvar::clock::from_keyed_account(&slot_hashes_and_clock[1])?,
&SlotHashes::from_keyed_account(&slot_hashes_and_clock[0])?,
&Clock::from_keyed_account(&slot_hashes_and_clock[1])?,
other_signers,
&vote,
)
@ -236,11 +236,11 @@ mod tests {
.iter()
.map(|meta| {
if sysvar::clock::check_id(&meta.pubkey) {
sysvar::clock::new_account(1, 0, 0, 0, 0)
sysvar::clock::create_account(1, 0, 0, 0, 0)
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
sysvar::slot_hashes::create_account(1, &[])
} else if sysvar::rent::check_id(&meta.pubkey) {
sysvar::rent::create_account(1, &Rent::default())
Rent::default().create_account(1)
} else {
Account::default()
}