Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Nov 12, 2024
1 parent b1dd01e commit bd7e3b1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 16 deletions.
62 changes: 61 additions & 1 deletion core/src/epoch_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ use jito_bytemuck::{
types::{PodBool, PodU128, PodU16, PodU64},
AccountDeserialize, Discriminator,
};
use jito_vault_core::vault_operator_delegation::VaultOperatorDelegation;
use shank::{ShankAccount, ShankType};
use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey};
use spl_math::precise_number::PreciseNumber;

use crate::{discriminators::Discriminators, error::TipRouterError, fees::Fees};
use crate::{
discriminators::Discriminators, error::TipRouterError, fees::Fees, weight_table::WeightTable,
};

// PDA'd ["EPOCH_SNAPSHOT", NCN, NCN_EPOCH_SLOT]
#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, AccountDeserialize, ShankAccount)]
Expand Down Expand Up @@ -150,6 +154,10 @@ impl EpochSnapshot {
vault_operator_delegations: u64,
votes: u128,
) -> Result<(), TipRouterError> {
if self.finalized() {
return Err(TipRouterError::OperatorFinalized.into());
}

self.operators_registered = PodU64::from(
self.operators_registered()
.checked_add(1)
Expand Down Expand Up @@ -352,6 +360,10 @@ impl OperatorSnapshot {
vault_operator_delegations: u64,
votes: u128,
) -> Result<(), TipRouterError> {
if self.finalized() {
return Err(TipRouterError::VaultOperatorDelegationFinalized.into());
}

self.vault_operator_delegations_registered = PodU64::from(
self.vault_operator_delegations_registered()
.checked_add(1)
Expand Down Expand Up @@ -479,6 +491,46 @@ impl VaultOperatorDelegationSnapshot {
)
}

pub fn create_snapshot(
vault: Pubkey,
operator: Pubkey,
ncn: Pubkey,
ncn_epoch: u64,
bump: u8,
current_slot: u64,
st_mint: Pubkey,
vault_operator_delegation: &VaultOperatorDelegation,
weight_table: &WeightTable,
) -> Result<Self, ProgramError> {
let total_security = vault_operator_delegation
.delegation_state
.total_security()?;
let precise_total_security = PreciseNumber::new(total_security as u128)
.ok_or(TipRouterError::NewPreciseNumberError)?;

let precise_weight = weight_table.get_precise_weight(&st_mint)?;

let precise_total_votes = precise_total_security
.checked_mul(&precise_weight)
.ok_or(TipRouterError::ArithmeticOverflow)?;

let total_votes = precise_total_votes
.to_imprecise()
.ok_or(TipRouterError::CastToImpreciseNumberError)?;

Ok(Self::new_active(
vault,
operator,
ncn,
ncn_epoch,
bump,
current_slot,
st_mint,
total_security,
total_votes,
))
}

pub fn seeds(vault: &Pubkey, operator: &Pubkey, ncn: &Pubkey, ncn_epoch: u64) -> Vec<Vec<u8>> {
Vec::from_iter(
[
Expand Down Expand Up @@ -540,4 +592,12 @@ impl VaultOperatorDelegationSnapshot {
}
Ok(())
}

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

pub fn total_votes(&self) -> u128 {
self.total_votes.into()
}
}
4 changes: 4 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum TipRouterError {
WeightNotFound,
#[error("No operators in ncn")]
NoOperators,
#[error("Vault operator delegation is already finalized - should not happen")]
VaultOperatorDelegationFinalized,
#[error("Operator is already finalized - should not happen")]
OperatorFinalized,
}

impl<T> DecodeError<T> for TipRouterError {
Expand Down
52 changes: 37 additions & 15 deletions program/src/initialize_vault_operator_delegation_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ pub fn process_initialize_vault_operator_delegation_snapshot(
&vault_operator_delegation_snapshot_seeds,
)?;

let st_mint = {
let vault_data = vault.data.borrow();
let vault_account = Vault::try_from_slice_unchecked(&vault_data)?;
vault_account.supported_mint
};

let is_active: bool = {
let vault_ncn_ticket_data = vault_ncn_ticket.data.borrow();
let vault_ncn_ticket_account =
Expand Down Expand Up @@ -157,43 +163,59 @@ pub fn process_initialize_vault_operator_delegation_snapshot(
)?;

*vault_operator_delegation_snapshot_account = if is_active {
let (operator_fee_bps, vault_count): (u16, u64) = {
let operator_data = operator.data.borrow();
let operator_account = Operator::try_from_slice_unchecked(&operator_data)?;
(
operator_account.operator_fee_bps.into(),
operator_account.vault_count(),
)
};
let vault_operator_delegation_data = vault_operator_delegation.data.borrow();
let vault_operator_delegation_account =
VaultOperatorDelegation::try_from_slice_unchecked(&vault_operator_delegation_data)?;

let weight_table_data = weight_table.data.borrow();
let weight_table_account = WeightTable::try_from_slice_unchecked(&weight_table_data)?;

//TODO Ending here for the day
VaultOperatorDelegationSnapshot::new_active(
VaultOperatorDelegationSnapshot::create_snapshot(
*vault.key,
*operator.key,
*ncn.key,
ncn_epoch,
vault_operator_delegation_snapshot_bump,
current_slot,
operator_fee_bps,
vault_count,
)
st_mint,
vault_operator_delegation_account,
weight_table_account,
)?
} else {
VaultOperatorDelegationSnapshot::new_inactive(
*vault.key,
*operator.key,
*ncn.key,
ncn_epoch,
vault_operator_delegation_snapshot_bump,
current_slot,
st_mint,
)
};

// Increment operator registration for an inactive operator
if !is_active {
// Increment vault operator delegation
let mut operator_snapshot_data = operator_snapshot.try_borrow_mut_data()?;
let operator_snapshot_account =
OperatorSnapshot::try_from_slice_unchecked_mut(&mut operator_snapshot_data)?;

operator_snapshot_account.increment_vault_operator_delegation_registration(
current_slot,
vault_operator_delegation_snapshot_account.total_security(),
vault_operator_delegation_snapshot_account.total_votes(),
)?;

// If operator is finalized, increment operator registration
if operator_snapshot_account.finalized() {
let mut epoch_snapshot_data = epoch_snapshot.try_borrow_mut_data()?;
let epoch_snapshot_account =
EpochSnapshot::try_from_slice_unchecked_mut(&mut epoch_snapshot_data)?;

epoch_snapshot_account.increment_operator_registration(current_slot, 0, 0)?;
epoch_snapshot_account.increment_operator_registration(
current_slot,
operator_snapshot_account.valid_operator_vault_delegations(),
operator_snapshot_account.total_votes(),
)?;
}

Ok(())
Expand Down

0 comments on commit bd7e3b1

Please sign in to comment.