diff --git a/Cargo.lock b/Cargo.lock index 54eb0bb0cf..4ff0d19df3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2206,12 +2206,13 @@ dependencies = [ name = "solana-rewards-program" version = "0.12.0" dependencies = [ - "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "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-runtime 0.12.0", "solana-sdk 0.12.0", "solana-vote-program 0.12.0", ] diff --git a/programs/native/rewards/Cargo.toml b/programs/native/rewards/Cargo.toml index c3e5c29789..9cc98904a1 100644 --- a/programs/native/rewards/Cargo.toml +++ b/programs/native/rewards/Cargo.toml @@ -19,6 +19,7 @@ solana-sdk = { path = "../../../sdk", version = "0.12.0" } [dev-dependencies] solana-vote-program = { path = "../vote", version = "0.12.0" } +solana-runtime = { path = "../../../runtime", version = "0.12.0" } [lib] name = "solana_rewards_program" diff --git a/programs/native/rewards/src/lib.rs b/programs/native/rewards/src/lib.rs index 3fd9d0fa3f..41017fc65b 100644 --- a/programs/native/rewards/src/lib.rs +++ b/programs/native/rewards/src/lib.rs @@ -1,9 +1,13 @@ //! Rewards program //! Exchanges validation and storage proofs for lamports +pub mod rewards_instruction; +pub mod rewards_program; +pub mod rewards_transaction; + +use crate::rewards_instruction::RewardsInstruction; use bincode::deserialize; use log::*; -use serde_derive::{Deserialize, Serialize}; use solana_sdk::account::KeyedAccount; use solana_sdk::native_program::ProgramError; use solana_sdk::pubkey::Pubkey; @@ -19,7 +23,7 @@ const MINIMUM_CREDITS_PER_REDEMPTION: u64 = 1; // Raise this to either minimize // // TODO: Migrate to reward mechanism described by the book: // https://github.com/solana-labs/solana/blob/master/book/src/ed_vce_state_validation_protocol_based_rewards.md -// https://github.com/solana-labs/solana/blob/master/book/src/staking-rewards.md#stake-weighted-rewards +// https://github.com/solana-labs/solana/blob/master/book/src/staking-rewards.md fn calc_vote_reward(credits: u64, stake: u64) -> Result { if credits < MINIMUM_CREDITS_PER_REDEMPTION { error!("Credit redemption too early"); @@ -71,11 +75,6 @@ fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Progra Ok(()) } -#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub enum RewardsInstruction { - RedeemVoteCredits, -} - solana_entrypoint!(entrypoint); fn entrypoint( _program_id: &Pubkey, @@ -96,8 +95,8 @@ fn entrypoint( #[cfg(test)] mod tests { use super::*; + use crate::rewards_program; use solana_sdk::account::Account; - use solana_sdk::rewards_program; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::vote_program::{self, Vote}; use solana_vote_program; diff --git a/programs/native/rewards/src/rewards_instruction.rs b/programs/native/rewards/src/rewards_instruction.rs new file mode 100644 index 0000000000..1594b87c74 --- /dev/null +++ b/programs/native/rewards/src/rewards_instruction.rs @@ -0,0 +1,6 @@ +use serde_derive::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] +pub enum RewardsInstruction { + RedeemVoteCredits, +} diff --git a/sdk/src/rewards_program.rs b/programs/native/rewards/src/rewards_program.rs similarity index 89% rename from sdk/src/rewards_program.rs rename to programs/native/rewards/src/rewards_program.rs index eb1f3183d8..a6a28f2bfb 100644 --- a/sdk/src/rewards_program.rs +++ b/programs/native/rewards/src/rewards_program.rs @@ -1,5 +1,6 @@ -use crate::pubkey::Pubkey; use bincode::serialized_size; +use serde_derive::{Deserialize, Serialize}; +use solana_sdk::pubkey::Pubkey; pub const REWARDS_PROGRAM_ID: [u8; 32] = [ 133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/programs/native/rewards/src/rewards_transaction.rs b/programs/native/rewards/src/rewards_transaction.rs new file mode 100644 index 0000000000..f701e1be90 --- /dev/null +++ b/programs/native/rewards/src/rewards_transaction.rs @@ -0,0 +1,87 @@ +//! The `rewards_transaction` module provides functionality for creating a global +//! rewards account and enabling stakers to redeem credits from their vote accounts. + +use crate::rewards_instruction::RewardsInstruction; +use crate::rewards_program; +use solana_sdk::hash::Hash; +use solana_sdk::pubkey::Pubkey; +use solana_sdk::signature::Keypair; +use solana_sdk::system_transaction::SystemTransaction; +use solana_sdk::transaction::Transaction; + +pub struct RewardsTransaction {} + +impl RewardsTransaction { + pub fn new_account( + from_keypair: &Keypair, + rewards_id: Pubkey, + last_id: Hash, + num_tokens: u64, + fee: u64, + ) -> Transaction { + SystemTransaction::new_program_account( + from_keypair, + rewards_id, + last_id, + num_tokens, + rewards_program::get_max_size() as u64, + rewards_program::id(), + fee, + ) + } + + pub fn new_redeem_credits( + keypair: &Keypair, + vote_id: Pubkey, + to_id: Pubkey, + last_id: Hash, + fee: u64, + ) -> Transaction { + let instruction = RewardsInstruction::RedeemVoteCredits; + Transaction::new( + keypair, + &[vote_id, to_id], + rewards_program::id(), + &instruction, + last_id, + fee, + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::rewards_program; + //use solana_runtime::execute_transaction; + use solana_sdk::hash::Hash; + use solana_sdk::native_loader::create_program_account; + use solana_sdk::signature::KeypairUtil; + use solana_sdk::system_program; + use solana_sdk::vote_program; + + #[test] + fn test_execute_rewards_transaction() { + let system_program_account = create_program_account("solana_system_program"); + let mut _new_account_loaders = vec![vec![(system_program::id(), system_program_account)]]; + let from_keypair = Keypair::new(); + let rewards_id = Keypair::new().pubkey(); + let last_id = Hash::default(); + let _tx = RewardsTransaction::new_account(&from_keypair, rewards_id, last_id, 10_000, 0); + //execute_transaction(&tx, &mut new_account_loaders[..], accounts, 0).unwrap(); + + let vote_program_account = create_program_account("solana_vote_program"); + let rewards_program_account = create_program_account("solana_rewards_program"); + let mut _new_redeem_credits_loaders = vec![ + vec![(rewards_program::id(), rewards_program_account)], + vec![(vote_program::id(), vote_program_account)], + ]; + + let vote_id = Keypair::new().pubkey(); + let to_id = from_keypair.pubkey(); + //let to_tokens = to_account.tokens; + let _tx = RewardsTransaction::new_redeem_credits(&from_keypair, vote_id, to_id, last_id, 0); + //execute_transaction(&tx, &mut new_redeem_credits_loaders[..], accounts, 0).unwrap(); + //assert!(to_account.tokens > to_tokens); + } +} diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 3cc587a062..dd6c482c37 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -12,7 +12,6 @@ pub mod native_program; pub mod packet; pub mod payment_plan; pub mod pubkey; -pub mod rewards_program; pub mod shortvec; pub mod signature; pub mod storage_program;