Add a library for creating Rewards transactions

And move out of the SDK
This commit is contained in:
Greg Fitzgerald 2019-02-14 10:22:20 -07:00
parent 2c5cbaff25
commit 7f3aca15dd
7 changed files with 105 additions and 11 deletions

3
Cargo.lock generated
View File

@ -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",
]

View File

@ -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"

View File

@ -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<u64, ProgramError> {
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;

View File

@ -0,0 +1,6 @@
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum RewardsInstruction {
RedeemVoteCredits,
}

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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;