Skip to content

Commit

Permalink
closer
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Dec 1, 2024
1 parent 580ca5a commit 3965b8a
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 138 deletions.
24 changes: 12 additions & 12 deletions core/src/ballot_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use spl_math::precise_number::PreciseNumber;

use crate::{
constants::PRECISE_CONSENSUS, discriminators::Discriminators, error::TipRouterError,
stake_weight::StakeWeight,
stake_weight::StakeWeights,
};

#[derive(Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod, ShankType)]
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Ballot {
pub struct BallotTally {
index: PodU16,
ballot: Ballot,
stake_weight: StakeWeight,
stake_weight: StakeWeights,
tally: PodU64,
reserved: [u8; 64],
}
Expand All @@ -56,15 +56,15 @@ impl Default for BallotTally {
Self {
index: PodU16::from(u16::MAX),
ballot: Ballot::default(),
stake_weight: StakeWeight::default(),
stake_weight: StakeWeights::default(),
tally: PodU64::from(0),
reserved: [0; 64],
}
}
}

impl BallotTally {
pub fn new(index: u16, ballot: Ballot, stake_weight: &StakeWeight) -> Self {
pub fn new(index: u16, ballot: Ballot, stake_weight: &StakeWeights) -> Self {
Self {
index: PodU16::from(index),
ballot,
Expand All @@ -78,7 +78,7 @@ impl BallotTally {
self.ballot
}

pub const fn stake_weight(&self) -> &StakeWeight {
pub const fn stake_weight(&self) -> &StakeWeights {
&self.stake_weight
}

Expand All @@ -94,7 +94,7 @@ impl BallotTally {
self.index() == u16::MAX
}

pub fn increment_tally(&mut self, stake_weight: &StakeWeight) -> Result<(), TipRouterError> {
pub fn increment_tally(&mut self, stake_weight: &StakeWeights) -> Result<(), TipRouterError> {
self.stake_weight.increment(stake_weight)?;
self.tally = PodU64::from(
self.tally()
Expand All @@ -111,7 +111,7 @@ impl BallotTally {
pub struct OperatorVote {
operator: Pubkey,
slot_voted: PodU64,
stake_weight: StakeWeight,
stake_weight: StakeWeights,
ballot_index: PodU16,
reserved: [u8; 64],
}
Expand All @@ -121,7 +121,7 @@ impl Default for OperatorVote {
Self {
operator: Pubkey::default(),
slot_voted: PodU64::from(0),
stake_weight: StakeWeight::default(),
stake_weight: StakeWeights::default(),
ballot_index: PodU16::from(u16::MAX),
reserved: [0; 64],
}
Expand All @@ -133,7 +133,7 @@ impl OperatorVote {
ballot_index: usize,
operator: Pubkey,
current_slot: u64,
stake_weight: &StakeWeight,
stake_weight: &StakeWeights,
) -> Self {
Self {
operator,
Expand All @@ -152,7 +152,7 @@ impl OperatorVote {
self.slot_voted.into()
}

pub const fn stake_weight(&self) -> &StakeWeight {
pub const fn stake_weight(&self) -> &StakeWeights {
&self.stake_weight
}

Expand Down Expand Up @@ -303,7 +303,7 @@ impl BallotBox {
fn increment_or_create_ballot_tally(
&mut self,
ballot: &Ballot,
stake_weight: &StakeWeight,
stake_weight: &StakeWeights,
) -> Result<usize, TipRouterError> {
let mut tally_index: usize = 0;
for tally in self.ballot_tallies.iter_mut() {
Expand Down Expand Up @@ -336,7 +336,7 @@ impl BallotBox {
&mut self,
operator: Pubkey,
ballot: Ballot,
stake_weight: &StakeWeight,
stake_weight: &StakeWeights,
current_slot: u64,
) -> Result<(), TipRouterError> {
let ballot_index = self.increment_or_create_ballot_tally(&ballot, stake_weight)?;
Expand Down
1 change: 1 addition & 0 deletions core/src/discriminators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pub enum Discriminators {
BallotBox = 0x20,
// Distribution
BaseRewardRouter = 0x30,
NcnRewardRouter = 0x31,
}
152 changes: 76 additions & 76 deletions core/src/epoch_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use spl_math::precise_number::PreciseNumber;

use crate::{
discriminators::Discriminators, error::TipRouterError, fees::Fees, ncn_fee_group::NcnFeeGroup,
stake_weight::StakeWeight, weight_table::WeightTable,
stake_weight::StakeWeights, weight_table::WeightTable,
};

// PDA'd ["epoch_snapshot", NCN, NCN_EPOCH_SLOT]
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct EpochSnapshot {
operators_registered: PodU64,
valid_operator_vault_delegations: PodU64,

stake_weight: StakeWeight,
stake_weights: StakeWeights,

/// Reserved space
reserved: [u8; 128],
Expand Down Expand Up @@ -69,7 +69,7 @@ impl EpochSnapshot {
vault_count: PodU64::from(vault_count),
operators_registered: PodU64::from(0),
valid_operator_vault_delegations: PodU64::from(0),
stake_weight: StakeWeight::default(),
stake_weights: StakeWeights::default(),
reserved: [0; 128],
}
}
Expand Down Expand Up @@ -146,8 +146,8 @@ impl EpochSnapshot {
self.valid_operator_vault_delegations.into()
}

pub const fn stake_weight(&self) -> &StakeWeight {
&self.stake_weight
pub const fn stake_weights(&self) -> &StakeWeights {
&self.stake_weights
}

pub const fn fees(&self) -> &Fees {
Expand All @@ -162,7 +162,7 @@ impl EpochSnapshot {
&mut self,
current_slot: u64,
vault_operator_delegations: u64,
stake_weight: &StakeWeight,
stake_weight: &StakeWeights,
) -> Result<(), TipRouterError> {
if self.finalized() {
return Err(TipRouterError::OperatorFinalized);
Expand All @@ -180,7 +180,7 @@ impl EpochSnapshot {
.ok_or(TipRouterError::ArithmeticOverflow)?,
);

self.stake_weight.increment(stake_weight)?;
self.stake_weights.increment(stake_weight)?;

if self.finalized() {
self.slot_finalized = PodU64::from(current_slot);
Expand Down Expand Up @@ -212,72 +212,13 @@ pub struct OperatorSnapshot {
vault_operator_delegations_registered: PodU64,
valid_operator_vault_delegations: PodU64,

stake_weight: StakeWeight,
stake_weights: StakeWeights,
reserved: [u8; 256],

//TODO change to 64
vault_operator_stake_weight: [VaultOperatorStakeWeight; 32],
}

#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, ShankType)]
#[repr(C)]
pub struct VaultOperatorStakeWeight {
vault: Pubkey,
vault_index: PodU64,
ncn_fee_group: NcnFeeGroup,
stake_weight: StakeWeight,
reserved: [u8; 32],
}

impl Default for VaultOperatorStakeWeight {
fn default() -> Self {
Self {
vault: Pubkey::default(),
ncn_fee_group: NcnFeeGroup::default(),
vault_index: PodU64::from(u64::MAX),
stake_weight: StakeWeight::default(),
reserved: [0; 32],
}
}
}

impl VaultOperatorStakeWeight {
pub fn new(
vault: Pubkey,
vault_index: u64,
ncn_fee_group: NcnFeeGroup,
stake_weight: &StakeWeight,
) -> Self {
Self {
vault,
vault_index: PodU64::from(vault_index),
ncn_fee_group,
stake_weight: *stake_weight,
reserved: [0; 32],
}
}

pub fn is_empty(&self) -> bool {
self.vault_index() == u64::MAX
}

pub fn vault_index(&self) -> u64 {
self.vault_index.into()
}

pub const fn stake_weight(&self) -> &StakeWeight {
&self.stake_weight
}

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

pub const fn ncn_fee_group(&self) -> NcnFeeGroup {
self.ncn_fee_group
}
}

impl Discriminator for OperatorSnapshot {
const DISCRIMINATOR: u8 = Discriminators::OperatorSnapshot as u8;
}
Expand Down Expand Up @@ -316,7 +257,7 @@ impl OperatorSnapshot {
vault_operator_delegation_count: PodU64::from(vault_operator_delegation_count),
vault_operator_delegations_registered: PodU64::from(0),
valid_operator_vault_delegations: PodU64::from(0),
stake_weight: StakeWeight::default(),
stake_weights: StakeWeights::default(),
reserved: [0; 256],
vault_operator_stake_weight: [VaultOperatorStakeWeight::default(); 32],
})
Expand Down Expand Up @@ -449,8 +390,8 @@ impl OperatorSnapshot {
self.valid_operator_vault_delegations.into()
}

pub const fn stake_weight(&self) -> &StakeWeight {
&self.stake_weight
pub const fn stake_weights(&self) -> &StakeWeights {
&self.stake_weights
}

pub fn finalized(&self) -> bool {
Expand All @@ -472,7 +413,7 @@ impl OperatorSnapshot {
vault: Pubkey,
vault_index: u64,
ncn_fee_group: NcnFeeGroup,
stake_weight: &StakeWeight,
stake_weights: &StakeWeights,
) -> Result<(), TipRouterError> {
if self.vault_operator_delegations_registered()
> Self::MAX_VAULT_OPERATOR_STAKE_WEIGHT as u64
Expand All @@ -485,7 +426,7 @@ impl OperatorSnapshot {
}

self.vault_operator_stake_weight[self.vault_operator_delegations_registered() as usize] =
VaultOperatorStakeWeight::new(vault, vault_index, ncn_fee_group, stake_weight);
VaultOperatorStakeWeight::new(vault, vault_index, ncn_fee_group, stake_weights);

Ok(())
}
Expand All @@ -496,29 +437,29 @@ impl OperatorSnapshot {
vault: Pubkey,
vault_index: u64,
ncn_fee_group: NcnFeeGroup,
stake_weight: &StakeWeight,
stake_weights: &StakeWeights,
) -> Result<(), TipRouterError> {
if self.finalized() {
return Err(TipRouterError::VaultOperatorDelegationFinalized);
}

self.insert_vault_operator_stake_weight(vault, vault_index, ncn_fee_group, stake_weight)?;
self.insert_vault_operator_stake_weight(vault, vault_index, ncn_fee_group, stake_weights)?;

self.vault_operator_delegations_registered = PodU64::from(
self.vault_operator_delegations_registered()
.checked_add(1)
.ok_or(TipRouterError::ArithmeticOverflow)?,
);

if stake_weight.stake_weight() > 0 {
if stake_weights.stake_weight() > 0 {
self.valid_operator_vault_delegations = PodU64::from(
self.valid_operator_vault_delegations()
.checked_add(1)
.ok_or(TipRouterError::ArithmeticOverflow)?,
);
}

self.stake_weight.increment(stake_weight)?;
self.stake_weights.increment(stake_weights)?;

if self.finalized() {
self.slot_finalized = PodU64::from(current_slot);
Expand Down Expand Up @@ -552,3 +493,62 @@ impl OperatorSnapshot {
Ok(total_stake_weight)
}
}

#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, ShankType)]
#[repr(C)]
pub struct VaultOperatorStakeWeight {
vault: Pubkey,
vault_index: PodU64,
ncn_fee_group: NcnFeeGroup,
stake_weight: StakeWeights,
reserved: [u8; 32],
}

impl Default for VaultOperatorStakeWeight {
fn default() -> Self {
Self {
vault: Pubkey::default(),
ncn_fee_group: NcnFeeGroup::default(),
vault_index: PodU64::from(u64::MAX),
stake_weight: StakeWeights::default(),
reserved: [0; 32],
}
}
}

impl VaultOperatorStakeWeight {
pub fn new(
vault: Pubkey,
vault_index: u64,
ncn_fee_group: NcnFeeGroup,
stake_weight: &StakeWeights,
) -> Self {
Self {
vault,
vault_index: PodU64::from(vault_index),
ncn_fee_group,
stake_weight: *stake_weight,
reserved: [0; 32],
}
}

pub fn is_empty(&self) -> bool {
self.vault_index() == u64::MAX
}

pub fn vault_index(&self) -> u64 {
self.vault_index.into()
}

pub const fn stake_weights(&self) -> &StakeWeights {
&self.stake_weight
}

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

pub const fn ncn_fee_group(&self) -> NcnFeeGroup {
self.ncn_fee_group
}
}
2 changes: 2 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub enum TipRouterError {
OperatorRewardNotFound,
#[error("Destination mismatch")]
DestinationMismatch,
#[error("Fee not active")]
FeeNotActive,
}

impl<T> DecodeError<T> for TipRouterError {
Expand Down
Loading

0 comments on commit 3965b8a

Please sign in to comment.