Add storage point tracking and tie in storage rewards to economics (#4824)
* Add storage point tracking and tie in storage rewards to epochs and economics * Prevent validators from updating their validations for a segment * Fix test * Retain syscall scoping for readability * Update Credits to own epoch tracking
This commit is contained in:
@ -269,8 +269,6 @@ impl Default for BlockhashQueue {
|
||||
}
|
||||
}
|
||||
|
||||
pub const DUMMY_REPLICATOR_POINTS: u64 = 100;
|
||||
|
||||
impl Bank {
|
||||
pub fn new(genesis_block: &GenesisBlock) -> Self {
|
||||
Self::new_with_paths(&genesis_block, None)
|
||||
@ -463,22 +461,21 @@ impl Bank {
|
||||
|
||||
let validator_points = self.stakes.write().unwrap().claim_points();
|
||||
|
||||
let replicator_rewards =
|
||||
self.inflation.replicator(year) * self.capitalization() as f64 * period;
|
||||
let storage_rewards = self.inflation.storage(year) * self.capitalization() as f64 * period;
|
||||
|
||||
let replicator_points = DUMMY_REPLICATOR_POINTS; // TODO: real value for points earned last epoch
|
||||
let storage_points = self.storage_accounts.write().unwrap().claim_points();
|
||||
|
||||
self.store_account(
|
||||
&rewards::id(),
|
||||
&rewards::create_account(
|
||||
1,
|
||||
validator_rewards / validator_points as f64,
|
||||
replicator_rewards / replicator_points as f64,
|
||||
storage_rewards / storage_points as f64,
|
||||
),
|
||||
);
|
||||
|
||||
self.capitalization.fetch_add(
|
||||
(validator_rewards + replicator_rewards) as usize,
|
||||
(validator_rewards + storage_rewards) as usize,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
}
|
||||
@ -1471,8 +1468,13 @@ mod tests {
|
||||
let ((vote_id, mut vote_account), stake) =
|
||||
crate::stakes::tests::create_staked_node_accounts(1_0000);
|
||||
|
||||
// set up stakes and vote accounts
|
||||
let ((validator_id, validator_account), (replicator_id, replicator_account)) =
|
||||
crate::storage_utils::tests::create_storage_accounts_with_credits(100);
|
||||
|
||||
// set up stakes,vote, and storage accounts
|
||||
bank.store_account(&stake.0, &stake.1);
|
||||
bank.store_account(&validator_id, &validator_account);
|
||||
bank.store_account(&replicator_id, &replicator_account);
|
||||
|
||||
// generate some rewards
|
||||
let mut vote_state = VoteState::from(&vote_account).unwrap();
|
||||
@ -1484,6 +1486,7 @@ mod tests {
|
||||
bank.store_account(&vote_id, &vote_account);
|
||||
|
||||
let validator_points = bank.stakes.read().unwrap().points();
|
||||
let storage_points = bank.storage_accounts.read().unwrap().points();
|
||||
|
||||
// put a child bank in epoch 1, which calls update_rewards()...
|
||||
let bank1 = Bank::new_from_parent(
|
||||
@ -1504,8 +1507,8 @@ mod tests {
|
||||
|
||||
assert!(
|
||||
((rewards.validator_point_value * validator_points as f64
|
||||
+ rewards.replicator_point_value * DUMMY_REPLICATOR_POINTS as f64) - // TODO: need replicator points per-epoch
|
||||
inflation as f64)
|
||||
+ rewards.storage_point_value * storage_points as f64)
|
||||
- inflation as f64)
|
||||
.abs()
|
||||
< 1.0 // rounding, truncating
|
||||
);
|
||||
|
@ -7,11 +7,15 @@ use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Default, Clone, PartialEq, Debug, Deserialize, Serialize)]
|
||||
pub struct StorageAccounts {
|
||||
/// validator storage accounts
|
||||
/// validator storage accounts and their credits
|
||||
validator_accounts: HashSet<Pubkey>,
|
||||
|
||||
/// replicator storage accounts
|
||||
/// replicator storage accounts and their credits
|
||||
replicator_accounts: HashSet<Pubkey>,
|
||||
|
||||
/// unclaimed points.
|
||||
// 1 point == 1 storage account credit
|
||||
points: HashMap<Pubkey, u64>,
|
||||
}
|
||||
|
||||
pub fn is_storage(account: &Account) -> bool {
|
||||
@ -21,21 +25,35 @@ pub fn is_storage(account: &Account) -> bool {
|
||||
impl StorageAccounts {
|
||||
pub fn store(&mut self, pubkey: &Pubkey, account: &Account) {
|
||||
if let Ok(storage_state) = account.state() {
|
||||
if let StorageContract::ReplicatorStorage { .. } = storage_state {
|
||||
if let StorageContract::ReplicatorStorage { credits, .. } = storage_state {
|
||||
if account.lamports == 0 {
|
||||
self.replicator_accounts.remove(pubkey);
|
||||
} else {
|
||||
self.replicator_accounts.insert(*pubkey);
|
||||
self.points.insert(*pubkey, credits.current_epoch);
|
||||
}
|
||||
} else if let StorageContract::ValidatorStorage { .. } = storage_state {
|
||||
} else if let StorageContract::ValidatorStorage { credits, .. } = storage_state {
|
||||
if account.lamports == 0 {
|
||||
self.validator_accounts.remove(pubkey);
|
||||
} else {
|
||||
self.validator_accounts.insert(*pubkey);
|
||||
self.points.insert(*pubkey, credits.current_epoch);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// currently unclaimed points
|
||||
pub fn points(&self) -> u64 {
|
||||
self.points.values().sum()
|
||||
}
|
||||
|
||||
/// "claims" points, resets points to 0
|
||||
pub fn claim_points(&mut self) -> u64 {
|
||||
let points = self.points();
|
||||
self.points.clear();
|
||||
points
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validator_accounts(bank: &Bank) -> HashMap<Pubkey, Account> {
|
||||
@ -61,13 +79,14 @@ pub fn replicator_accounts(bank: &Bank) -> HashMap<Pubkey, Account> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::bank_client::BankClient;
|
||||
use solana_sdk::client::SyncClient;
|
||||
use solana_sdk::genesis_block::create_genesis_block;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_storage_api::storage_contract::{StorageAccount, STORAGE_ACCOUNT_SPACE};
|
||||
use solana_storage_api::{storage_instruction, storage_processor};
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -113,4 +132,97 @@ mod tests {
|
||||
assert_eq!(validator_accounts(bank.as_ref()).len(), 1);
|
||||
assert_eq!(replicator_accounts(bank.as_ref()).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_points() {
|
||||
// note: storage_points == storage_credits
|
||||
let credits = 42;
|
||||
let mut storage_accounts = StorageAccounts::default();
|
||||
assert_eq!(storage_accounts.points(), 0);
|
||||
assert_eq!(storage_accounts.claim_points(), 0);
|
||||
|
||||
// create random validator and replicator accounts with `credits`
|
||||
let ((validator_pubkey, validator_account), (replicator_pubkey, replicator_account)) =
|
||||
create_storage_accounts_with_credits(credits);
|
||||
|
||||
storage_accounts.store(&validator_pubkey, &validator_account);
|
||||
storage_accounts.store(&replicator_pubkey, &replicator_account);
|
||||
// check that 2x credits worth of points are available
|
||||
assert_eq!(storage_accounts.points(), credits * 2);
|
||||
|
||||
let ((validator_pubkey, validator_account), (replicator_pubkey, mut replicator_account)) =
|
||||
create_storage_accounts_with_credits(credits);
|
||||
|
||||
storage_accounts.store(&validator_pubkey, &validator_account);
|
||||
storage_accounts.store(&replicator_pubkey, &replicator_account);
|
||||
// check that 4x credits worth of points are available
|
||||
assert_eq!(storage_accounts.points(), credits * 2 * 2);
|
||||
|
||||
storage_accounts.store(&validator_pubkey, &validator_account);
|
||||
storage_accounts.store(&replicator_pubkey, &replicator_account);
|
||||
// check that storing again has no effect
|
||||
assert_eq!(storage_accounts.points(), credits * 2 * 2);
|
||||
|
||||
let storage_contract = &mut replicator_account.state().unwrap();
|
||||
if let StorageContract::ReplicatorStorage {
|
||||
credits: account_credits,
|
||||
..
|
||||
} = storage_contract
|
||||
{
|
||||
account_credits.current_epoch += 1;
|
||||
}
|
||||
replicator_account.set_state(storage_contract).unwrap();
|
||||
storage_accounts.store(&replicator_pubkey, &replicator_account);
|
||||
|
||||
// check that incremental store increases credits
|
||||
assert_eq!(storage_accounts.points(), credits * 2 * 2 + 1);
|
||||
|
||||
assert_eq!(storage_accounts.claim_points(), credits * 2 * 2 + 1);
|
||||
// check that once redeemed, the points are gone
|
||||
assert_eq!(storage_accounts.claim_points(), 0);
|
||||
}
|
||||
|
||||
pub fn create_storage_accounts_with_credits(
|
||||
credits: u64,
|
||||
) -> ((Pubkey, Account), (Pubkey, Account)) {
|
||||
let validator_pubkey = Pubkey::new_rand();
|
||||
let replicator_pubkey = Pubkey::new_rand();
|
||||
|
||||
let mut validator_account =
|
||||
Account::new(1, STORAGE_ACCOUNT_SPACE as usize, &solana_storage_api::id());
|
||||
let mut validator = StorageAccount::new(validator_pubkey, &mut validator_account);
|
||||
validator
|
||||
.initialize_validator_storage(validator_pubkey)
|
||||
.unwrap();
|
||||
let storage_contract = &mut validator_account.state().unwrap();
|
||||
if let StorageContract::ValidatorStorage {
|
||||
credits: account_credits,
|
||||
..
|
||||
} = storage_contract
|
||||
{
|
||||
account_credits.current_epoch = credits;
|
||||
}
|
||||
validator_account.set_state(storage_contract).unwrap();
|
||||
|
||||
let mut replicator_account =
|
||||
Account::new(1, STORAGE_ACCOUNT_SPACE as usize, &solana_storage_api::id());
|
||||
let mut replicator = StorageAccount::new(replicator_pubkey, &mut replicator_account);
|
||||
replicator
|
||||
.initialize_replicator_storage(replicator_pubkey)
|
||||
.unwrap();
|
||||
let storage_contract = &mut replicator_account.state().unwrap();
|
||||
if let StorageContract::ReplicatorStorage {
|
||||
credits: account_credits,
|
||||
..
|
||||
} = storage_contract
|
||||
{
|
||||
account_credits.current_epoch = credits;
|
||||
}
|
||||
replicator_account.set_state(storage_contract).unwrap();
|
||||
|
||||
(
|
||||
(validator_pubkey, validator_account),
|
||||
(replicator_pubkey, replicator_account),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user