add rewards syscall, groom some others (#4740)

This commit is contained in:
Rob Walker
2019-06-19 19:46:47 -07:00
committed by GitHub
parent 63503ad589
commit 7ea522e851
7 changed files with 100 additions and 43 deletions

View File

@ -33,9 +33,9 @@ use solana_sdk::native_loader;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, Signature}; use solana_sdk::signature::{Keypair, Signature};
use solana_sdk::syscall::current; use solana_sdk::syscall::current;
use solana_sdk::syscall::fees::{self, Fees}; use solana_sdk::syscall::fees;
use solana_sdk::syscall::slot_hashes::{self, SlotHashes}; use solana_sdk::syscall::slot_hashes::{self, SlotHashes};
use solana_sdk::syscall::tick_height::{self, TickHeight}; use solana_sdk::syscall::tick_height;
use solana_sdk::system_transaction; use solana_sdk::system_transaction;
use solana_sdk::timing::{duration_as_ms, duration_as_ns, duration_as_us, MAX_RECENT_BLOCKHASHES}; use solana_sdk::timing::{duration_as_ms, duration_as_ns, duration_as_us, MAX_RECENT_BLOCKHASHES};
use solana_sdk::transaction::{Result, Transaction, TransactionError}; use solana_sdk::transaction::{Result, Transaction, TransactionError};
@ -424,25 +424,14 @@ impl Bank {
} }
fn update_fees(&self) { fn update_fees(&self) {
let mut account = self self.store_account(&fees::id(), &fees::create_account(1, &self.fee_calculator));
.get_account(&fees::id())
.unwrap_or_else(|| fees::create_account(1));
let mut fees = Fees::from(&account).unwrap();
fees.fee_calculator = self.fee_calculator.clone();
fees.to(&mut account).unwrap();
self.store_account(&fees::id(), &account);
} }
fn update_tick_height(&self) { fn update_tick_height(&self) {
let mut account = self self.store_account(
.get_account(&tick_height::id()) &tick_height::id(),
.unwrap_or_else(|| tick_height::create_account(1)); &tick_height::create_account(1, self.tick_height()),
);
TickHeight::to(self.tick_height(), &mut account).unwrap();
self.store_account(&tick_height::id(), &account);
} }
// update reward for previous epoch // update reward for previous epoch
@ -1401,6 +1390,8 @@ mod tests {
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
use solana_sdk::poh_config::PohConfig; use solana_sdk::poh_config::PohConfig;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::syscall::fees::Fees;
use solana_sdk::syscall::tick_height::TickHeight;
use solana_sdk::system_instruction; use solana_sdk::system_instruction;
use solana_sdk::system_transaction; use solana_sdk::system_transaction;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
@ -2519,7 +2510,7 @@ mod tests {
} }
let tick_account = bank.get_account(&tick_height::id()).unwrap(); let tick_account = bank.get_account(&tick_height::id()).unwrap();
let tick_height = TickHeight::from(&tick_account).unwrap(); let tick_height = TickHeight::from(&tick_account).unwrap().0;
assert_eq!(bank.tick_height(), tick_height); assert_eq!(bank.tick_height(), tick_height);
assert_eq!(tick_height, 10); assert_eq!(tick_height, 10);
} }

View File

@ -118,7 +118,11 @@ macro_rules! solana_name_id(
#[cfg(test)] #[cfg(test)]
#[test] #[test]
fn test_name_id() { fn test_name_id() {
assert_eq!(id().to_string(), $name); // un-comment me to see what the id should look like, given a name
// if id().to_string() != $name {
// panic!("id for `{}` should be `{:?}`", $name, bs58::decode($name).into_vec().unwrap());
// }
assert_eq!(id().to_string(), $name)
} }
) )
); );

View File

@ -1,7 +1,6 @@
//! This account contains the current cluster fees //! This account contains the current cluster fees
//! //!
use crate::account::Account; use crate::account::Account;
use crate::account_utils::State;
use crate::fee_calculator::FeeCalculator; use crate::fee_calculator::FeeCalculator;
use crate::syscall; use crate::syscall;
use bincode::serialized_size; use bincode::serialized_size;
@ -22,10 +21,10 @@ pub struct Fees {
impl Fees { impl Fees {
pub fn from(account: &Account) -> Option<Self> { pub fn from(account: &Account) -> Option<Self> {
account.state().ok() account.deserialize_data().ok()
} }
pub fn to(&self, account: &mut Account) -> Option<()> { pub fn to(&self, account: &mut Account) -> Option<()> {
account.set_state(self).ok() account.serialize_data(self).ok()
} }
pub fn size_of() -> usize { pub fn size_of() -> usize {
@ -33,8 +32,15 @@ impl Fees {
} }
} }
pub fn create_account(lamports: u64) -> Account { pub fn create_account(lamports: u64, fee_calculator: &FeeCalculator) -> Account {
Account::new(lamports, Fees::size_of(), &syscall::id()) Account::new_data(
lamports,
&Fees {
fee_calculator: fee_calculator.clone(),
},
&syscall::id(),
)
.unwrap()
} }
#[cfg(test)] #[cfg(test)]
@ -44,11 +50,8 @@ mod tests {
#[test] #[test]
fn test_fees_create_account() { fn test_fees_create_account() {
let lamports = 42; let lamports = 42;
let account = create_account(lamports); let account = create_account(lamports, &FeeCalculator::default());
let fees = Fees::from(&account).unwrap(); let fees = Fees::from(&account).unwrap();
assert_eq!( assert_eq!(fees.fee_calculator, FeeCalculator::default());
fees.fee_calculator.lamports_per_signature,
FeeCalculator::default().lamports_per_signature
);
} }
} }

View File

@ -4,6 +4,7 @@ use crate::pubkey::Pubkey;
pub mod current; pub mod current;
pub mod fees; pub mod fees;
pub mod rewards;
pub mod slot_hashes; pub mod slot_hashes;
pub mod tick_height; pub mod tick_height;

View File

@ -0,0 +1,60 @@
//! This account contains the current cluster rewards point values
//!
use crate::account::Account;
use crate::syscall;
use bincode::serialized_size;
/// account pubkey
const ID: [u8; 32] = [
6, 167, 211, 138, 69, 219, 174, 221, 84, 28, 161, 202, 169, 28, 9, 210, 255, 70, 57, 99, 48,
156, 150, 32, 59, 104, 53, 117, 192, 0, 0, 0,
];
crate::solana_name_id!(ID, "Sysca11Rewards11111111111111111111111111111");
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Rewards {
pub validator_point_value: f64,
pub replicator_point_value: f64,
}
impl Rewards {
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 create_account(
lamports: u64,
validator_point_value: f64,
replicator_point_value: f64,
) -> Account {
Account::new_data(
lamports,
&Rewards {
validator_point_value,
replicator_point_value,
},
&syscall::id(),
)
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = create_account(1, 0.0, 0.0);
let rewards = Rewards::from(&account).unwrap();
assert_eq!(rewards, Rewards::default());
}
}

View File

@ -3,7 +3,6 @@
//! this account carries the Bank's most recent blockhashes for some N parents //! this account carries the Bank's most recent blockhashes for some N parents
//! //!
use crate::account::Account; use crate::account::Account;
use crate::account_utils::State;
use crate::hash::Hash; use crate::hash::Hash;
use crate::pubkey::Pubkey; use crate::pubkey::Pubkey;
use crate::syscall; use crate::syscall;
@ -35,10 +34,10 @@ pub struct SlotHashes {
impl SlotHashes { impl SlotHashes {
pub fn from(account: &Account) -> Option<Self> { pub fn from(account: &Account) -> Option<Self> {
account.state().ok() account.deserialize_data().ok()
} }
pub fn to(&self, account: &mut Account) -> Option<()> { pub fn to(&self, account: &mut Account) -> Option<()> {
account.set_state(self).ok() account.serialize_data(self).ok()
} }
pub fn size_of() -> usize { pub fn size_of() -> usize {

View File

@ -1,7 +1,6 @@
//! This account contains the current cluster tick height //! This account contains the current cluster tick height
//! //!
use crate::account::Account; use crate::account::Account;
use crate::account_utils::State;
use crate::syscall; use crate::syscall;
use bincode::serialized_size; use bincode::serialized_size;
@ -16,14 +15,14 @@ crate::solana_name_id!(ID, "Sysca11TickHeight11111111111111111111111111");
#[repr(C)] #[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)]
pub struct TickHeight(u64); pub struct TickHeight(pub u64);
impl TickHeight { impl TickHeight {
pub fn from(account: &Account) -> Option<u64> { pub fn from(account: &Account) -> Option<Self> {
account.state().ok().map(|res: Self| res.0) account.deserialize_data().ok()
} }
pub fn to(tick_height: u64, account: &mut Account) -> Option<()> { pub fn to(&self, account: &mut Account) -> Option<()> {
account.set_state(&TickHeight(tick_height)).ok() account.serialize_data(self).ok()
} }
pub fn size_of() -> usize { pub fn size_of() -> usize {
@ -31,8 +30,8 @@ impl TickHeight {
} }
} }
pub fn create_account(lamports: u64) -> Account { pub fn create_account(lamports: u64, tick_height: u64) -> Account {
Account::new(lamports, TickHeight::size_of(), &syscall::id()) Account::new_data(lamports, &TickHeight(tick_height), &syscall::id()).unwrap()
} }
#[cfg(test)] #[cfg(test)]
@ -41,8 +40,8 @@ mod tests {
#[test] #[test]
fn test_tick_height_create_account() { fn test_tick_height_create_account() {
let account = create_account(1); let account = create_account(1, 1);
let tick_height = TickHeight::from(&account).unwrap(); let tick_height = TickHeight::from(&account).unwrap();
assert_eq!(tick_height, 0); assert_eq!(tick_height.0, 1);
} }
} }