add rewards syscall, groom some others (#4740)
This commit is contained in:
		| @@ -118,7 +118,11 @@ macro_rules! solana_name_id( | ||||
|         #[cfg(test)] | ||||
|         #[test] | ||||
|         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) | ||||
|         } | ||||
|     ) | ||||
| ); | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| //! This account contains the current cluster fees | ||||
| //! | ||||
| use crate::account::Account; | ||||
| use crate::account_utils::State; | ||||
| use crate::fee_calculator::FeeCalculator; | ||||
| use crate::syscall; | ||||
| use bincode::serialized_size; | ||||
| @@ -22,10 +21,10 @@ pub struct Fees { | ||||
|  | ||||
| impl Fees { | ||||
|     pub fn from(account: &Account) -> Option<Self> { | ||||
|         account.state().ok() | ||||
|         account.deserialize_data().ok() | ||||
|     } | ||||
|     pub fn to(&self, account: &mut Account) -> Option<()> { | ||||
|         account.set_state(self).ok() | ||||
|         account.serialize_data(self).ok() | ||||
|     } | ||||
|  | ||||
|     pub fn size_of() -> usize { | ||||
| @@ -33,8 +32,15 @@ impl Fees { | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub fn create_account(lamports: u64) -> Account { | ||||
|     Account::new(lamports, Fees::size_of(), &syscall::id()) | ||||
| pub fn create_account(lamports: u64, fee_calculator: &FeeCalculator) -> Account { | ||||
|     Account::new_data( | ||||
|         lamports, | ||||
|         &Fees { | ||||
|             fee_calculator: fee_calculator.clone(), | ||||
|         }, | ||||
|         &syscall::id(), | ||||
|     ) | ||||
|     .unwrap() | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| @@ -44,11 +50,8 @@ mod tests { | ||||
|     #[test] | ||||
|     fn test_fees_create_account() { | ||||
|         let lamports = 42; | ||||
|         let account = create_account(lamports); | ||||
|         let account = create_account(lamports, &FeeCalculator::default()); | ||||
|         let fees = Fees::from(&account).unwrap(); | ||||
|         assert_eq!( | ||||
|             fees.fee_calculator.lamports_per_signature, | ||||
|             FeeCalculator::default().lamports_per_signature | ||||
|         ); | ||||
|         assert_eq!(fees.fee_calculator, FeeCalculator::default()); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ use crate::pubkey::Pubkey; | ||||
|  | ||||
| pub mod current; | ||||
| pub mod fees; | ||||
| pub mod rewards; | ||||
| pub mod slot_hashes; | ||||
| pub mod tick_height; | ||||
|  | ||||
|   | ||||
							
								
								
									
										60
									
								
								sdk/src/syscall/rewards.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								sdk/src/syscall/rewards.rs
									
									
									
									
									
										Normal 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()); | ||||
|     } | ||||
| } | ||||
| @@ -3,7 +3,6 @@ | ||||
| //! this account carries the Bank's most recent blockhashes for some N parents | ||||
| //! | ||||
| use crate::account::Account; | ||||
| use crate::account_utils::State; | ||||
| use crate::hash::Hash; | ||||
| use crate::pubkey::Pubkey; | ||||
| use crate::syscall; | ||||
| @@ -35,10 +34,10 @@ pub struct SlotHashes { | ||||
|  | ||||
| impl SlotHashes { | ||||
|     pub fn from(account: &Account) -> Option<Self> { | ||||
|         account.state().ok() | ||||
|         account.deserialize_data().ok() | ||||
|     } | ||||
|     pub fn to(&self, account: &mut Account) -> Option<()> { | ||||
|         account.set_state(self).ok() | ||||
|         account.serialize_data(self).ok() | ||||
|     } | ||||
|  | ||||
|     pub fn size_of() -> usize { | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| //! This account contains the current cluster tick height | ||||
| //! | ||||
| use crate::account::Account; | ||||
| use crate::account_utils::State; | ||||
| use crate::syscall; | ||||
| use bincode::serialized_size; | ||||
|  | ||||
| @@ -16,14 +15,14 @@ crate::solana_name_id!(ID, "Sysca11TickHeight11111111111111111111111111"); | ||||
|  | ||||
| #[repr(C)] | ||||
| #[derive(Serialize, Deserialize, Debug, Default)] | ||||
| pub struct TickHeight(u64); | ||||
| pub struct TickHeight(pub u64); | ||||
|  | ||||
| impl TickHeight { | ||||
|     pub fn from(account: &Account) -> Option<u64> { | ||||
|         account.state().ok().map(|res: Self| res.0) | ||||
|     pub fn from(account: &Account) -> Option<Self> { | ||||
|         account.deserialize_data().ok() | ||||
|     } | ||||
|     pub fn to(tick_height: u64, account: &mut Account) -> Option<()> { | ||||
|         account.set_state(&TickHeight(tick_height)).ok() | ||||
|     pub fn to(&self, account: &mut Account) -> Option<()> { | ||||
|         account.serialize_data(self).ok() | ||||
|     } | ||||
|  | ||||
|     pub fn size_of() -> usize { | ||||
| @@ -31,8 +30,8 @@ impl TickHeight { | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub fn create_account(lamports: u64) -> Account { | ||||
|     Account::new(lamports, TickHeight::size_of(), &syscall::id()) | ||||
| pub fn create_account(lamports: u64, tick_height: u64) -> Account { | ||||
|     Account::new_data(lamports, &TickHeight(tick_height), &syscall::id()).unwrap() | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| @@ -41,8 +40,8 @@ mod tests { | ||||
|  | ||||
|     #[test] | ||||
|     fn test_tick_height_create_account() { | ||||
|         let account = create_account(1); | ||||
|         let account = create_account(1, 1); | ||||
|         let tick_height = TickHeight::from(&account).unwrap(); | ||||
|         assert_eq!(tick_height, 0); | ||||
|         assert_eq!(tick_height.0, 1); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user