diff --git a/Cargo.lock b/Cargo.lock index c27de12999..bee8850b4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2218,10 +2218,7 @@ version = "0.12.0" dependencies = [ "bincode 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", "solana-logger 0.12.0", - "solana-metrics 0.12.0", "solana-rewards-api 0.12.0", "solana-runtime 0.12.0", "solana-sdk 0.12.0", diff --git a/programs/native/rewards/Cargo.toml b/programs/native/rewards/Cargo.toml index 4dce4bccaf..3e112019ff 100644 --- a/programs/native/rewards/Cargo.toml +++ b/programs/native/rewards/Cargo.toml @@ -11,10 +11,7 @@ edition = "2018" [dependencies] bincode = "1.0.0" log = "0.4.2" -serde = "1.0.87" -serde_derive = "1.0.87" solana-logger = { path = "../../../logger", version = "0.12.0" } -solana-metrics = { path = "../../../metrics", version = "0.12.0" } solana-sdk = { path = "../../../sdk", version = "0.12.0" } solana-rewards-api = { path = "../rewards_api", version = "0.12.0" } diff --git a/programs/native/rewards/src/lib.rs b/programs/native/rewards/src/lib.rs index 23f7c38bcb..4611ac4ca4 100644 --- a/programs/native/rewards/src/lib.rs +++ b/programs/native/rewards/src/lib.rs @@ -120,7 +120,7 @@ mod tests { } #[test] - fn test_redeem_vote_credits() { + fn test_redeem_vote_credits_via_program() { let from_id = Keypair::new().pubkey(); let mut from_account = Account::new(100, 0, Pubkey::default()); diff --git a/programs/native/rewards/tests/rewards.rs b/programs/native/rewards/tests/rewards.rs new file mode 100644 index 0000000000..9836f348af --- /dev/null +++ b/programs/native/rewards/tests/rewards.rs @@ -0,0 +1,148 @@ +use solana_rewards_api::rewards_program; +use solana_rewards_api::rewards_transaction::RewardsTransaction; +use solana_runtime::{execute_transaction, RuntimeError}; +use solana_sdk::account::Account; +use solana_sdk::hash::Hash; +use solana_sdk::native_loader::create_program_account; +use solana_sdk::pubkey::Pubkey; +use solana_sdk::signature::{Keypair, KeypairUtil}; +use solana_sdk::system_program; +use solana_sdk::vote_program::{self, VoteState}; +use solana_sdk::vote_transaction::VoteTransaction; + +fn create_rewards_account( + from_keypair: &Keypair, + from_account: &mut Account, + rewards_id: Pubkey, + lamports: u64, +) -> Result { + let system_program_account = create_program_account("solana_system_program"); + let loaders = &mut [vec![(system_program::id(), system_program_account)]]; + let last_id = Hash::default(); + let tx = RewardsTransaction::new_account(from_keypair, rewards_id, last_id, lamports, 0); + let accounts = &mut [from_account.clone(), Account::new(0, 0, Pubkey::default())]; + execute_transaction(&tx, loaders, accounts, 0)?; + + // Simulate committing account after successful transaction + from_account.tokens = accounts[0].tokens; + + Ok(accounts[1].clone()) +} + +fn create_vote_account( + from_keypair: &Keypair, + from_account: &mut Account, + vote_id: Pubkey, + lamports: u64, +) -> Result { + let system_program_account = create_program_account("solana_system_program"); + let vote_program_account = create_program_account("solana_vote_program"); + let loaders = &mut [ + vec![(system_program::id(), system_program_account)], + vec![(vote_program::id(), vote_program_account)], + ]; + let last_id = Hash::default(); + let tx = VoteTransaction::new_account(from_keypair, vote_id, last_id, lamports, 0); + let accounts = &mut [from_account.clone(), Account::new(0, 0, Pubkey::default())]; + execute_transaction(&tx, loaders, accounts, 0)?; + + // Simulate committing account after successful transaction + from_account.tokens = accounts[1].tokens; + + Ok(accounts[1].clone()) +} + +fn submit_vote( + vote_keypair: &Keypair, + vote_account: &mut Account, + tick_height: u64, +) -> Result { + let vote_program_account = create_program_account("solana_vote_program"); + let loaders = &mut [vec![(vote_program::id(), vote_program_account)]]; + let last_id = Hash::default(); + let tx = VoteTransaction::new_vote(vote_keypair, tick_height, last_id, 0); + let accounts = &mut [vote_account.clone()]; + execute_transaction(&tx, loaders, accounts, 0)?; + + // Simulate committing account after successful transaction + vote_account + .userdata + .clone_from_slice(&accounts[0].userdata); + + Ok(VoteState::deserialize(&vote_account.userdata).unwrap()) +} + +fn redeem_credits( + rewards_id: Pubkey, + rewards_account: &mut Account, + vote_keypair: &Keypair, + vote_account: &mut Account, + to_id: Pubkey, + to_account: &mut Account, +) -> Result { + let last_id = Hash::default(); + let rewards_program_account = create_program_account("solana_rewards_program"); + let loaders = &mut [vec![(rewards_program::id(), rewards_program_account)]]; + + let accounts = &mut [ + vote_account.clone(), + rewards_account.clone(), + to_account.clone(), + ]; + let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, to_id, last_id, 0); + execute_transaction(&tx, loaders, accounts, 0)?; + + // Simulate committing account after successful transaction + vote_account + .userdata + .clone_from_slice(&accounts[0].userdata); + rewards_account.tokens = accounts[1].tokens; + to_account.tokens = accounts[2].tokens; + + Ok(VoteState::deserialize(&vote_account.userdata).unwrap()) +} + +#[test] +fn test_redeem_vote_credits_via_runtime() { + // Create a rewards account to hold all rewards pool tokens. + let from_keypair = Keypair::new(); + let mut from_account = Account::new(10_000, 0, Pubkey::default()); + let rewards_keypair = Keypair::new(); + let rewards_id = rewards_keypair.pubkey(); + let mut rewards_account = + create_rewards_account(&from_keypair, &mut from_account, rewards_id, 100).unwrap(); + + // A staker create a vote account account and delegates a validator to vote on its behalf. + let vote_keypair = Keypair::new(); + let vote_id = vote_keypair.pubkey(); + let mut vote_account = + create_vote_account(&from_keypair, &mut from_account, vote_id, 100).unwrap(); + + // The validator submits votes to accumulate credits. + for _ in 0..vote_program::MAX_VOTE_HISTORY { + let vote_state = submit_vote(&vote_keypair, &mut vote_account, 1).unwrap(); + assert_eq!(vote_state.credits(), 0); + } + let vote_state = submit_vote(&vote_keypair, &mut vote_account, 1).unwrap(); + assert_eq!(vote_state.credits(), 1); + + // TODO: Add VoteInstruction::RegisterStakerId so that we don't need to point the "to" + // account to the "from" account. + let to_id = from_keypair.pubkey(); + let mut to_account = from_account; + let to_tokens = to_account.tokens; + + // Periodically, the staker sumbits its vote account to the rewards pool + // to exchange its credits for lamports. + let vote_state = redeem_credits( + rewards_id, + &mut rewards_account, + &vote_keypair, + &mut vote_account, + to_id, + &mut to_account, + ) + .unwrap(); + assert!(to_account.tokens > to_tokens); + assert_eq!(vote_state.credits(), 0); +}