Skip to content

Commit

Permalink
looking much better
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Nov 29, 2024
1 parent 57610ba commit fa41560
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 271 deletions.
245 changes: 123 additions & 122 deletions core/src/base_reward_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,11 @@ use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError
use spl_math::precise_number::PreciseNumber;

use crate::{
ballot_box::BallotBox,
base_fee_group::BaseFeeGroup,
discriminators::Discriminators,
error::TipRouterError,
fees::{FeeConfig, Fees},
ncn_fee_group::NcnFeeGroup,
ballot_box::BallotBox, base_fee_group::BaseFeeGroup, discriminators::Discriminators,
error::TipRouterError, fees::Fees, ncn_fee_group::NcnFeeGroup,
ncn_reward_router::NcnRewardRouter,
};

#[derive(Default, Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct Rewards {
rewards: PodU64,
}

impl Rewards {
pub fn rewards(self) -> u64 {
self.rewards.into()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct NcnRewardRoute {
operator: Pubkey,
ncn_fee_group_rewards: [Rewards; 8],
}

impl Default for NcnRewardRoute {
fn default() -> Self {
Self {
operator: Pubkey::default(),
ncn_fee_group_rewards: [Rewards::default(); NcnFeeGroup::FEE_GROUP_COUNT],
}
}
}

impl NcnRewardRoute {
pub fn new(
operator: Pubkey,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<Self, TipRouterError> {
let mut route = Self {
operator,
ncn_fee_group_rewards: [Rewards::default(); NcnFeeGroup::FEE_GROUP_COUNT],
};

route.set_rewards(ncn_fee_group, rewards)?;

Ok(route)
}

pub const fn operator(&self) -> Pubkey {
self.operator
}

pub fn rewards(&self, ncn_fee_group: NcnFeeGroup) -> Result<u64, TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
Ok(self.ncn_fee_group_rewards[group_index].rewards())
}

fn set_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
self.ncn_fee_group_rewards[group_index].rewards = PodU64::from(rewards);

Ok(())
}

pub fn increment_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}

pub fn decrement_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_sub(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}
}

// PDA'd ["epoch_reward_router", NCN, NCN_EPOCH_SLOT]
#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, AccountDeserialize, ShankAccount)]
#[repr(C)]
Expand All @@ -125,15 +28,15 @@ pub struct BaseRewardRouter {

reserved: [u8; 128],

base_fee_group_rewards: [Rewards; 8],
ncn_fee_group_rewards: [Rewards; 8],
base_fee_group_rewards: [BaseRewardRouterRewards; 8],
ncn_fee_group_rewards: [BaseRewardRouterRewards; 8],

//TODO change to 256
ncn_fee_group_reward_routes: [NcnRewardRoute; 32],
}

impl Discriminator for BaseRewardRouter {
const DISCRIMINATOR: u8 = Discriminators::EpochRewardRouter as u8;
const DISCRIMINATOR: u8 = Discriminators::BaseRewardRouter as u8;
}

impl BaseRewardRouter {
Expand All @@ -146,8 +49,10 @@ impl BaseRewardRouter {
reward_pool: PodU64::from(0),
rewards_processed: PodU64::from(0),
reserved: [0; 128],
base_fee_group_rewards: [Rewards::default(); NcnFeeGroup::FEE_GROUP_COUNT],
ncn_fee_group_rewards: [Rewards::default(); NcnFeeGroup::FEE_GROUP_COUNT],
base_fee_group_rewards: [BaseRewardRouterRewards::default();
NcnFeeGroup::FEE_GROUP_COUNT],
ncn_fee_group_rewards: [BaseRewardRouterRewards::default();
NcnFeeGroup::FEE_GROUP_COUNT],
ncn_fee_group_reward_routes: [NcnRewardRoute::default(); 32],
}
}
Expand Down Expand Up @@ -275,11 +180,11 @@ impl BaseRewardRouter {
let rewards_to_process = self.ncn_fee_group_rewards(*group)?;
let winning_reward_stake_weight =
winning_stake_weight.ncn_fee_group_stake_weight(*group)?;
let operator_reward_stake_weight =
let ncn_route_reward_stake_weight =
votes.stake_weight().ncn_fee_group_stake_weight(*group)?;

let ncn_fee_group_route_reward = Self::calculate_ncn_fee_group_route_reward(
operator_reward_stake_weight,
ncn_route_reward_stake_weight,
winning_reward_stake_weight,
rewards_to_process,
)?;
Expand Down Expand Up @@ -345,41 +250,42 @@ impl BaseRewardRouter {
}

fn calculate_ncn_fee_group_route_reward(
operator_reward_stake_weight: u128,
ncn_route_reward_stake_weight: u128,
winning_reward_stake_weight: u128,
rewards_to_process: u64,
) -> Result<u64, TipRouterError> {
if operator_reward_stake_weight == 0 || rewards_to_process == 0 {
if ncn_route_reward_stake_weight == 0 || rewards_to_process == 0 {
return Ok(0);
}

let precise_rewards_to_process = PreciseNumber::new(rewards_to_process as u128)
.ok_or(TipRouterError::NewPreciseNumberError)?;

let precise_operator_reward_stake_weight = PreciseNumber::new(operator_reward_stake_weight)
.ok_or(TipRouterError::NewPreciseNumberError)?;
let precise_ncn_route_reward_stake_weight =
PreciseNumber::new(ncn_route_reward_stake_weight)
.ok_or(TipRouterError::NewPreciseNumberError)?;

let precise_winning_reward_stake_weight = PreciseNumber::new(winning_reward_stake_weight)
.ok_or(TipRouterError::NewPreciseNumberError)?;

let precise_operator_reward = precise_rewards_to_process
.checked_mul(&precise_operator_reward_stake_weight)
let precise_ncn_route_reward = precise_rewards_to_process
.checked_mul(&precise_ncn_route_reward_stake_weight)
.and_then(|x| x.checked_div(&precise_winning_reward_stake_weight))
.ok_or(TipRouterError::ArithmeticOverflow)?;

let floored_precise_operator_reward = precise_operator_reward
let floored_precise_ncn_route_reward = precise_ncn_route_reward
.floor()
.ok_or(TipRouterError::ArithmeticFloorError)?;

let operator_reward_u128: u128 = floored_precise_operator_reward
let ncn_route_reward_u128: u128 = floored_precise_ncn_route_reward
.to_imprecise()
.ok_or(TipRouterError::CastToImpreciseNumberError)?;

let operator_reward: u64 = operator_reward_u128
let ncn_route_reward: u64 = ncn_route_reward_u128
.try_into()
.map_err(|_| TipRouterError::CastToU64Error)?;

Ok(operator_reward)
Ok(ncn_route_reward)
}

// ------------------ REWARD TALLIES ---------------------
Expand Down Expand Up @@ -561,16 +467,16 @@ impl BaseRewardRouter {
return Ok(());
}

for operator_reward in self.ncn_fee_group_reward_routes.iter_mut() {
if operator_reward.operator == operator {
operator_reward.increment_rewards(ncn_fee_group, rewards)?;
for ncn_route_reward in self.ncn_fee_group_reward_routes.iter_mut() {
if ncn_route_reward.operator == operator {
ncn_route_reward.increment_rewards(ncn_fee_group, rewards)?;
return Ok(());
}
}

for operator_reward in self.ncn_fee_group_reward_routes.iter_mut() {
if operator_reward.operator == Pubkey::default() {
*operator_reward = NcnRewardRoute::new(operator, ncn_fee_group, rewards)?;
for ncn_route_reward in self.ncn_fee_group_reward_routes.iter_mut() {
if ncn_route_reward.operator == Pubkey::default() {
*ncn_route_reward = NcnRewardRoute::new(operator, ncn_fee_group, rewards)?;
return Ok(());
}
}
Expand Down Expand Up @@ -600,3 +506,98 @@ impl BaseRewardRouter {
Err(TipRouterError::OperatorRewardNotFound)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct NcnRewardRoute {
operator: Pubkey,
ncn_fee_group_rewards: [BaseRewardRouterRewards; 8],
}

impl Default for NcnRewardRoute {
fn default() -> Self {
Self {
operator: Pubkey::default(),
ncn_fee_group_rewards: [BaseRewardRouterRewards::default();
NcnFeeGroup::FEE_GROUP_COUNT],
}
}
}

impl NcnRewardRoute {
pub fn new(
operator: Pubkey,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<Self, TipRouterError> {
let mut route = Self {
operator,
ncn_fee_group_rewards: [BaseRewardRouterRewards::default();
NcnFeeGroup::FEE_GROUP_COUNT],
};

route.set_rewards(ncn_fee_group, rewards)?;

Ok(route)
}

pub const fn operator(&self) -> Pubkey {
self.operator
}

pub fn rewards(&self, ncn_fee_group: NcnFeeGroup) -> Result<u64, TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
Ok(self.ncn_fee_group_rewards[group_index].rewards())
}

fn set_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
self.ncn_fee_group_rewards[group_index].rewards = PodU64::from(rewards);

Ok(())
}

pub fn increment_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}

pub fn decrement_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_sub(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct BaseRewardRouterRewards {
rewards: PodU64,
}

impl BaseRewardRouterRewards {
pub fn rewards(self) -> u64 {
self.rewards.into()
}
}
2 changes: 1 addition & 1 deletion core/src/discriminators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub enum Discriminators {
// Voting
BallotBox = 0x20,
// Distribution
EpochRewardRouter = 0x30,
BaseRewardRouter = 0x30,
}
20 changes: 7 additions & 13 deletions core/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use solana_program::pubkey::Pubkey;
use spl_math::precise_number::PreciseNumber;

use crate::{
base_fee_group::{self, BaseFeeGroup},
constants::MAX_FEE_BPS,
error::TipRouterError,
ncn_fee_group::{self, NcnFeeGroup},
base_fee_group::BaseFeeGroup, constants::MAX_FEE_BPS, error::TipRouterError,
ncn_fee_group::NcnFeeGroup,
};

/// Fee Config. Allows for fee updates to take place in a future epoch without requiring an update.
Expand Down Expand Up @@ -249,10 +247,6 @@ impl FeeConfig {

// ------------- Setters -------------
/// Updates the Fee Config
/// Any option set to None will be ignored
/// `new_wallet`` and `new_block_engine_fee_bps` will take effect immediately
/// `new_ncn_fee_bps` will set the fee group specified in `new_ncn_fee_group`
/// if no `new_ncn_fee_group` is specified, the default ncn group will be set
pub fn update_fee_config(

Check failure on line 250 in core/src/fees.rs

View workflow job for this annotation

GitHub Actions / lint

this function has too many arguments (8/7)
&mut self,
new_block_engine_fee_bps: Option<u16>,
Expand All @@ -276,11 +270,11 @@ impl FeeConfig {
};

if let Some(new_base_fee_wallet) = new_base_fee_wallet {
self.set_base_fee_wallet(base_fee_group, new_base_fee_wallet);
self.set_base_fee_wallet(base_fee_group, new_base_fee_wallet)?;
}

if let Some(new_base_fee_bps) = new_base_fee_bps {
self.set_base_fee_bps(base_fee_group, new_base_fee_bps, current_epoch);
self.set_base_fee_bps(base_fee_group, new_base_fee_bps, current_epoch)?;
}

// NCN FEE
Expand All @@ -291,7 +285,7 @@ impl FeeConfig {
};

if let Some(new_ncn_fee_bps) = new_ncn_fee_bps {
self.set_ncn_fee_bps(ncn_fee_group, new_ncn_fee_bps, current_epoch);
self.set_ncn_fee_bps(ncn_fee_group, new_ncn_fee_bps, current_epoch)?;
}

// ACTIVATION EPOCH
Expand Down Expand Up @@ -511,9 +505,9 @@ impl Fee {

#[cfg(test)]
mod tests {
use solana_program::pubkey::Pubkey;
// use solana_program::pubkey::Pubkey;

use super::*;
// use super::*;

// #[test]
// fn test_update_fees() {
Expand Down
Loading

0 comments on commit fa41560

Please sign in to comment.