Bump all native programs up a level
Don't categorize programs by a single backend.
This commit is contained in:
24
programs/rewards/Cargo.toml
Normal file
24
programs/rewards/Cargo.toml
Normal file
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "solana-rewards-program"
|
||||
version = "0.12.0"
|
||||
description = "Solana rewards program"
|
||||
authors = ["Solana Maintainers <maintainers@solana.com>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.1.2"
|
||||
log = "0.4.2"
|
||||
solana-logger = { path = "../../logger", version = "0.12.0" }
|
||||
solana-sdk = { path = "../../sdk", version = "0.12.0" }
|
||||
solana-rewards-api = { path = "../rewards_api", version = "0.12.0" }
|
||||
solana-vote-api = { path = "../vote_api", version = "0.12.0" }
|
||||
|
||||
[dev-dependencies]
|
||||
solana-runtime = { path = "../../runtime", version = "0.12.0" }
|
||||
|
||||
[lib]
|
||||
name = "solana_rewards_program"
|
||||
crate-type = ["cdylib"]
|
153
programs/rewards/src/lib.rs
Normal file
153
programs/rewards/src/lib.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
//! Rewards program
|
||||
//! Exchanges validation and storage proofs for lamports
|
||||
|
||||
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;
|
||||
use solana_sdk::solana_entrypoint;
|
||||
use solana_vote_api::vote_state::VoteState;
|
||||
|
||||
const INTEREST_PER_CREDIT_DIVISOR: u64 = 100; // Staker earns 1/INTEREST_PER_CREDIT_DIVISOR of interest per credit
|
||||
const MINIMUM_CREDITS_PER_REDEMPTION: u64 = 1; // Raise this to either minimize congestion or lengthen the interest period
|
||||
|
||||
// Below is a temporary solution for calculating validator rewards. It targets an ROI for
|
||||
// stakeholders and assumes the validator votes on every slot. It's convenient in that
|
||||
// the calculation does not require knowing the size of the staking pool.
|
||||
//
|
||||
// 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
|
||||
fn calc_vote_reward(credits: u64, stake: u64) -> Result<u64, ProgramError> {
|
||||
if credits < MINIMUM_CREDITS_PER_REDEMPTION {
|
||||
error!("Credit redemption too early");
|
||||
Err(ProgramError::GenericError)?;
|
||||
}
|
||||
Ok(credits * (stake / INTEREST_PER_CREDIT_DIVISOR))
|
||||
}
|
||||
|
||||
fn redeem_vote_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramError> {
|
||||
// The owner of the vote account needs to authorize having its credits cleared.
|
||||
if keyed_accounts[0].signer_key().is_none() {
|
||||
error!("account[0] is unsigned");
|
||||
Err(ProgramError::InvalidArgument)?;
|
||||
}
|
||||
|
||||
if !solana_vote_api::check_id(&keyed_accounts[0].account.owner) {
|
||||
error!("account[0] is not assigned to the VOTE_PROGRAM");
|
||||
Err(ProgramError::InvalidArgument)?;
|
||||
}
|
||||
|
||||
// TODO: Verify the next instruction in the transaction being processed is
|
||||
// VoteInstruction::ClearCredits and that it points to the same vote account
|
||||
// as keyed_accounts[0].
|
||||
|
||||
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata)?;
|
||||
|
||||
// TODO: This assumes the stake is static. If not, it should use the account value
|
||||
// at the time of voting, not at credit redemption.
|
||||
let stake = keyed_accounts[0].account.tokens;
|
||||
if stake == 0 {
|
||||
error!("staking account has no stake");
|
||||
Err(ProgramError::InvalidArgument)?;
|
||||
}
|
||||
|
||||
let lamports = calc_vote_reward(vote_state.credits(), stake)?;
|
||||
|
||||
// Transfer rewards from the rewards pool to the staking account.
|
||||
keyed_accounts[1].account.tokens -= lamports;
|
||||
keyed_accounts[0].account.tokens += lamports;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
solana_entrypoint!(entrypoint);
|
||||
fn entrypoint(
|
||||
_program_id: &Pubkey,
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
data: &[u8],
|
||||
_tick_height: u64,
|
||||
) -> Result<(), ProgramError> {
|
||||
solana_logger::setup();
|
||||
|
||||
trace!("process_instruction: {:?}", data);
|
||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||
|
||||
match deserialize(data).map_err(|_| ProgramError::InvalidUserdata)? {
|
||||
RewardsInstruction::RedeemVoteCredits => redeem_vote_credits(keyed_accounts),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use solana_rewards_api;
|
||||
use solana_rewards_api::rewards_state::RewardsState;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_vote_api::vote_instruction::Vote;
|
||||
use solana_vote_api::vote_state;
|
||||
|
||||
fn create_rewards_account(tokens: u64) -> Account {
|
||||
let space = RewardsState::max_size();
|
||||
Account::new(tokens, space, solana_rewards_api::id())
|
||||
}
|
||||
|
||||
fn redeem_vote_credits_(
|
||||
rewards_id: &Pubkey,
|
||||
rewards_account: &mut Account,
|
||||
vote_id: &Pubkey,
|
||||
vote_account: &mut Account,
|
||||
) -> Result<(), ProgramError> {
|
||||
let mut keyed_accounts = [
|
||||
KeyedAccount::new(vote_id, true, vote_account),
|
||||
KeyedAccount::new(rewards_id, false, rewards_account),
|
||||
];
|
||||
redeem_vote_credits(&mut keyed_accounts)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_redeem_vote_credits_via_program() {
|
||||
let staker_id = Keypair::new().pubkey();
|
||||
let mut staker_account = Account::new(100, 0, Pubkey::default());
|
||||
|
||||
let vote_id = Keypair::new().pubkey();
|
||||
let mut vote_account = vote_state::create_vote_account(100);
|
||||
vote_state::initialize_and_deserialize(
|
||||
&staker_id,
|
||||
&mut staker_account,
|
||||
&vote_id,
|
||||
&mut vote_account,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
|
||||
let vote = Vote::new(i as u64);
|
||||
let vote_state =
|
||||
vote_state::vote_and_deserialize(&vote_id, &mut vote_account, vote.clone())
|
||||
.unwrap();
|
||||
assert_eq!(vote_state.credits(), 0);
|
||||
}
|
||||
|
||||
let vote = Vote::new(vote_state::MAX_LOCKOUT_HISTORY as u64 + 1);
|
||||
let vote_state =
|
||||
vote_state::vote_and_deserialize(&vote_id, &mut vote_account, vote.clone()).unwrap();
|
||||
assert_eq!(vote_state.credits(), 1);
|
||||
|
||||
let rewards_id = Keypair::new().pubkey();
|
||||
let mut rewards_account = create_rewards_account(100);
|
||||
|
||||
let tokens_before = vote_account.tokens;
|
||||
|
||||
redeem_vote_credits_(
|
||||
&rewards_id,
|
||||
&mut rewards_account,
|
||||
&vote_id,
|
||||
&mut vote_account,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(vote_account.tokens > tokens_before);
|
||||
}
|
||||
}
|
104
programs/rewards/tests/rewards.rs
Normal file
104
programs/rewards/tests/rewards.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use solana_rewards_api::rewards_transaction::RewardsTransaction;
|
||||
use solana_runtime::bank::{Bank, Result};
|
||||
use solana_sdk::genesis_block::GenesisBlock;
|
||||
use solana_sdk::hash::hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_vote_api::vote_state::{self, VoteState};
|
||||
use solana_vote_api::vote_transaction::VoteTransaction;
|
||||
|
||||
struct RewardsBank<'a> {
|
||||
bank: &'a Bank,
|
||||
}
|
||||
|
||||
impl<'a> RewardsBank<'a> {
|
||||
fn new(bank: &'a Bank) -> Self {
|
||||
bank.add_native_program("solana_rewards_program", &solana_rewards_api::id());
|
||||
Self { bank }
|
||||
}
|
||||
|
||||
fn create_rewards_account(
|
||||
&self,
|
||||
from_keypair: &Keypair,
|
||||
rewards_id: Pubkey,
|
||||
lamports: u64,
|
||||
) -> Result<()> {
|
||||
let blockhash = self.bank.last_blockhash();
|
||||
let tx = RewardsTransaction::new_account(from_keypair, rewards_id, blockhash, lamports, 0);
|
||||
self.bank.process_transaction(&tx)
|
||||
}
|
||||
|
||||
fn create_vote_account(
|
||||
&self,
|
||||
from_keypair: &Keypair,
|
||||
vote_id: Pubkey,
|
||||
lamports: u64,
|
||||
) -> Result<()> {
|
||||
let blockhash = self.bank.last_blockhash();
|
||||
let tx =
|
||||
VoteTransaction::fund_staking_account(from_keypair, vote_id, blockhash, lamports, 0);
|
||||
self.bank.process_transaction(&tx)
|
||||
}
|
||||
|
||||
fn submit_vote(&self, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
||||
let blockhash = self.bank.last_blockhash();
|
||||
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, blockhash, 0);
|
||||
self.bank.process_transaction(&tx)?;
|
||||
self.bank.register_tick(&hash(blockhash.as_ref()));
|
||||
|
||||
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
|
||||
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
||||
}
|
||||
|
||||
fn redeem_credits(&self, rewards_id: Pubkey, vote_keypair: &Keypair) -> Result<VoteState> {
|
||||
let blockhash = self.bank.last_blockhash();
|
||||
let tx = RewardsTransaction::new_redeem_credits(&vote_keypair, rewards_id, blockhash, 0);
|
||||
self.bank.process_transaction(&tx)?;
|
||||
let vote_account = self.bank.get_account(&vote_keypair.pubkey()).unwrap();
|
||||
Ok(VoteState::deserialize(&vote_account.userdata).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_redeem_vote_credits_via_bank() {
|
||||
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
||||
let bank = Bank::new(&genesis_block);
|
||||
let rewards_bank = RewardsBank::new(&bank);
|
||||
|
||||
// Create a rewards account to hold all rewards pool tokens.
|
||||
let rewards_keypair = Keypair::new();
|
||||
let rewards_id = rewards_keypair.pubkey();
|
||||
rewards_bank
|
||||
.create_rewards_account(&from_keypair, 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();
|
||||
rewards_bank
|
||||
.create_vote_account(&from_keypair, vote_id, 100)
|
||||
.unwrap();
|
||||
|
||||
// The validator submits votes to accumulate credits.
|
||||
for i in 0..vote_state::MAX_LOCKOUT_HISTORY {
|
||||
let vote_state = rewards_bank.submit_vote(&vote_keypair, i as u64).unwrap();
|
||||
assert_eq!(vote_state.credits(), 0);
|
||||
}
|
||||
let vote_state = rewards_bank
|
||||
.submit_vote(&vote_keypair, vote_state::MAX_LOCKOUT_HISTORY as u64 + 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 = vote_id;
|
||||
let to_tokens = bank.get_balance(&vote_id);
|
||||
|
||||
// Periodically, the staker sumbits its vote account to the rewards pool
|
||||
// to exchange its credits for lamports.
|
||||
let vote_state = rewards_bank
|
||||
.redeem_credits(rewards_id, &vote_keypair)
|
||||
.unwrap();
|
||||
assert!(bank.get_balance(&to_id) > to_tokens);
|
||||
assert_eq!(vote_state.credits(), 0);
|
||||
}
|
Reference in New Issue
Block a user