diff --git a/Cargo.lock b/Cargo.lock index 4ff0d19df..dcfc23ed8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2202,6 +2202,16 @@ dependencies = [ "solana-sdk 0.12.0", ] +[[package]] +name = "solana-rewards-api" +version = "0.12.0" +dependencies = [ + "bincode 1.1.1 (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-sdk 0.12.0", +] + [[package]] name = "solana-rewards-program" version = "0.12.0" @@ -2212,6 +2222,7 @@ dependencies = [ "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", "solana-vote-program 0.12.0", diff --git a/Cargo.toml b/Cargo.toml index 17439c22c..e9fac40e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,7 @@ members = [ "programs/native/native_loader", "programs/native/noop", "programs/native/rewards", + "programs/native/rewards_api", "programs/native/storage", "programs/native/system", "programs/native/vote", diff --git a/programs/native/rewards/Cargo.toml b/programs/native/rewards/Cargo.toml index 9cc98904a..77513f926 100644 --- a/programs/native/rewards/Cargo.toml +++ b/programs/native/rewards/Cargo.toml @@ -16,6 +16,7 @@ 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" } [dev-dependencies] solana-vote-program = { path = "../vote", version = "0.12.0" } @@ -24,4 +25,3 @@ solana-runtime = { path = "../../../runtime", version = "0.12.0" } [lib] name = "solana_rewards_program" crate-type = ["cdylib"] - diff --git a/programs/native/rewards/src/lib.rs b/programs/native/rewards/src/lib.rs index 41017fc65..3c16fdc38 100644 --- a/programs/native/rewards/src/lib.rs +++ b/programs/native/rewards/src/lib.rs @@ -1,13 +1,9 @@ //! 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 solana_rewards_api::rewards_instruction::RewardsInstruction; use solana_sdk::account::KeyedAccount; use solana_sdk::native_program::ProgramError; use solana_sdk::pubkey::Pubkey; @@ -95,7 +91,7 @@ fn entrypoint( #[cfg(test)] mod tests { use super::*; - use crate::rewards_program; + use solana_rewards_api::rewards_program; use solana_sdk::account::Account; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::vote_program::{self, Vote}; diff --git a/programs/native/rewards/src/rewards_transaction.rs b/programs/native/rewards/src/rewards_transaction.rs deleted file mode 100644 index f701e1be9..000000000 --- a/programs/native/rewards/src/rewards_transaction.rs +++ /dev/null @@ -1,87 +0,0 @@ -//! 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/programs/native/rewards_api/Cargo.toml b/programs/native/rewards_api/Cargo.toml new file mode 100644 index 000000000..0dd602317 --- /dev/null +++ b/programs/native/rewards_api/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "solana-rewards-api" +version = "0.12.0" +description = "Solana rewards API" +authors = ["Solana Maintainers "] +repository = "https://github.com/solana-labs/solana" +license = "Apache-2.0" +homepage = "https://solana.com/" +edition = "2018" + +[dependencies] +bincode = "1.0.0" +serde = "1.0.87" +serde_derive = "1.0.87" +solana-sdk = { path = "../../../sdk", version = "0.12.0" } + +[lib] +name = "solana_rewards_api" +crate-type = ["lib"] diff --git a/programs/native/rewards_api/src/lib.rs b/programs/native/rewards_api/src/lib.rs new file mode 100644 index 000000000..0cd224de4 --- /dev/null +++ b/programs/native/rewards_api/src/lib.rs @@ -0,0 +1,3 @@ +pub mod rewards_instruction; +pub mod rewards_program; +pub mod rewards_transaction; diff --git a/programs/native/rewards/src/rewards_instruction.rs b/programs/native/rewards_api/src/rewards_instruction.rs similarity index 100% rename from programs/native/rewards/src/rewards_instruction.rs rename to programs/native/rewards_api/src/rewards_instruction.rs diff --git a/programs/native/rewards/src/rewards_program.rs b/programs/native/rewards_api/src/rewards_program.rs similarity index 100% rename from programs/native/rewards/src/rewards_program.rs rename to programs/native/rewards_api/src/rewards_program.rs diff --git a/programs/native/rewards_api/src/rewards_transaction.rs b/programs/native/rewards_api/src/rewards_transaction.rs new file mode 100644 index 000000000..cc7597ad7 --- /dev/null +++ b/programs/native/rewards_api/src/rewards_transaction.rs @@ -0,0 +1,50 @@ +//! 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, + ) + } +}