diff --git a/core/src/epoch_snapshot.rs b/core/src/epoch_snapshot.rs index c29fcd6..bb3a1be 100644 --- a/core/src/epoch_snapshot.rs +++ b/core/src/epoch_snapshot.rs @@ -488,134 +488,16 @@ impl OperatorSnapshot { Ok(()) } -} - -// PDA'd ["OPERATOR_SNAPSHOT", VAULT, OPERATOR, NCN, NCN_EPOCH_SLOT] -#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, AccountDeserialize, ShankAccount)] -#[repr(C)] -pub struct VaultOperatorDelegationSnapshot { - vault: Pubkey, - operator: Pubkey, - ncn: Pubkey, - ncn_epoch: PodU64, - bump: u8, - - slot_created: PodU64, - - is_active: PodBool, - - vault_index: PodU64, - - st_mint: Pubkey, - total_security: PodU64, - total_votes: PodU128, - - reserved: [u8; 128], -} - -impl Discriminator for VaultOperatorDelegationSnapshot { - const DISCRIMINATOR: u8 = Discriminators::VaultOperatorDelegationSnapshot as u8; -} - -impl VaultOperatorDelegationSnapshot { - #[allow(clippy::too_many_arguments)] - pub fn new( - vault: Pubkey, - operator: Pubkey, - ncn: Pubkey, - ncn_epoch: u64, - bump: u8, - current_slot: u64, - is_active: bool, - vault_index: u64, - st_mint: Pubkey, - total_security: u64, - total_votes: u128, - ) -> Self { - Self { - vault, - operator, - ncn, - ncn_epoch: PodU64::from(ncn_epoch), - bump, - slot_created: PodU64::from(current_slot), - is_active: PodBool::from(is_active), - vault_index: PodU64::from(vault_index), - st_mint, - total_security: PodU64::from(total_security), - total_votes: PodU128::from(total_votes), - reserved: [0; 128], - } - } - #[allow(clippy::too_many_arguments)] - pub fn new_active( - vault: Pubkey, - operator: Pubkey, - ncn: Pubkey, - ncn_epoch: u64, - bump: u8, - current_slot: u64, - st_mint: Pubkey, - vault_index: u64, - total_security: u64, - total_votes: u128, - ) -> Self { - Self::new( - vault, - operator, - ncn, - ncn_epoch, - bump, - current_slot, - true, - vault_index, - st_mint, - total_security, - total_votes, - ) - } - - pub fn new_inactive( - vault: Pubkey, - operator: Pubkey, - ncn: Pubkey, - ncn_epoch: u64, - bump: u8, - current_slot: u64, - vault_index: u64, - st_mint: Pubkey, - ) -> Self { - Self::new( - vault, - operator, - ncn, - ncn_epoch, - bump, - current_slot, - false, - vault_index, - st_mint, - 0, - 0, - ) - } - #[allow(clippy::too_many_arguments)] - pub fn create_snapshot( - vault: Pubkey, - operator: Pubkey, - ncn: Pubkey, - ncn_epoch: u64, - bump: u8, - current_slot: u64, - vault_index: u64, - st_mint: Pubkey, + pub fn calculate_total_votes( vault_operator_delegation: &VaultOperatorDelegation, weight_table: &WeightTable, - ) -> Result { + st_mint: &Pubkey, + ) -> Result { let total_security = vault_operator_delegation .delegation_state .total_security()?; + let precise_total_security = PreciseNumber::new(total_security as u128) .ok_or(TipRouterError::NewPreciseNumberError)?; @@ -629,95 +511,6 @@ impl VaultOperatorDelegationSnapshot { .to_imprecise() .ok_or(TipRouterError::CastToImpreciseNumberError)?; - Ok(Self::new_active( - vault, - operator, - ncn, - ncn_epoch, - bump, - current_slot, - st_mint, - vault_index, - total_security, - total_votes, - )) - } - - pub fn seeds(vault: &Pubkey, operator: &Pubkey, ncn: &Pubkey, ncn_epoch: u64) -> Vec> { - Vec::from_iter( - [ - b"VAULT_OPERATOR_DELEGATION_SNAPSHOT".to_vec(), - vault.to_bytes().to_vec(), - operator.to_bytes().to_vec(), - ncn.to_bytes().to_vec(), - ncn_epoch.to_le_bytes().to_vec(), - ] - .iter() - .cloned(), - ) - } - - pub fn find_program_address( - program_id: &Pubkey, - vault: &Pubkey, - operator: &Pubkey, - ncn: &Pubkey, - ncn_epoch: u64, - ) -> (Pubkey, u8, Vec>) { - let seeds = Self::seeds(vault, operator, ncn, ncn_epoch); - let seeds_iter: Vec<_> = seeds.iter().map(|s| s.as_slice()).collect(); - let (pda, bump) = Pubkey::find_program_address(&seeds_iter, program_id); - (pda, bump, seeds) - } - - pub fn load( - program_id: &Pubkey, - vault: &Pubkey, - operator: &Pubkey, - ncn: &Pubkey, - ncn_epoch: u64, - vault_operator_delegation_snapshot: &AccountInfo, - expect_writable: bool, - ) -> Result<(), ProgramError> { - if vault_operator_delegation_snapshot.owner.ne(program_id) { - msg!("Operator Snapshot account has an invalid owner"); - return Err(ProgramError::InvalidAccountOwner); - } - if vault_operator_delegation_snapshot.data_is_empty() { - msg!("Operator Snapshot account data is empty"); - return Err(ProgramError::InvalidAccountData); - } - if expect_writable && !vault_operator_delegation_snapshot.is_writable { - msg!("Operator Snapshot account is not writable"); - return Err(ProgramError::InvalidAccountData); - } - if vault_operator_delegation_snapshot.data.borrow()[0].ne(&Self::DISCRIMINATOR) { - msg!("Operator Snapshot account discriminator is invalid"); - return Err(ProgramError::InvalidAccountData); - } - if vault_operator_delegation_snapshot - .key - .ne(&Self::find_program_address(program_id, vault, operator, ncn, ncn_epoch).0) - { - msg!("Operator Snapshot account is not at the correct PDA"); - return Err(ProgramError::InvalidAccountData); - } - Ok(()) - } - - pub fn total_security(&self) -> u64 { - self.total_security.into() - } - - pub fn total_votes(&self) -> u128 { - self.total_votes.into() - } - - pub fn vault_index(&self) -> u64 { - self.vault_index.into() - } - - pub fn vault(&self) -> Pubkey { - self.vault + Ok(total_votes) } } diff --git a/integration_tests/tests/fixtures/tip_router_client.rs b/integration_tests/tests/fixtures/tip_router_client.rs index 16f5af2..a69e316 100644 --- a/integration_tests/tests/fixtures/tip_router_client.rs +++ b/integration_tests/tests/fixtures/tip_router_client.rs @@ -12,7 +12,7 @@ use jito_tip_router_client::{ types::ConfigAdminRole, }; use jito_tip_router_core::{ - epoch_snapshot::{EpochSnapshot, OperatorSnapshot, VaultOperatorDelegationSnapshot}, + epoch_snapshot::{EpochSnapshot, OperatorSnapshot}, error::TipRouterError, ncn_config::NcnConfig, tracked_mints::TrackedMints, @@ -587,16 +587,6 @@ impl TipRouterClient { let weight_table = WeightTable::find_program_address(&jito_tip_router_program::id(), &ncn, ncn_epoch).0; - let vault_operator_delegation_snapshot = - VaultOperatorDelegationSnapshot::find_program_address( - &jito_tip_router_program::id(), - &vault, - &operator, - &ncn, - ncn_epoch, - ) - .0; - let ix = InitializeVaultOperatorDelegationSnapshotBuilder::new() .ncn_config(config_pda) .restaking_config(restaking_config) @@ -609,11 +599,8 @@ impl TipRouterClient { .weight_table(weight_table) .epoch_snapshot(epoch_snapshot) .operator_snapshot(operator_snapshot) - .vault_operator_delegation_snapshot(vault_operator_delegation_snapshot) - .payer(self.payer.pubkey()) .vault_program(jito_vault_program::id()) .restaking_program(jito_restaking_program::id()) - .system_program(system_program::id()) .first_slot_of_ncn_epoch(slot) .instruction(); diff --git a/program/src/initialize_vault_operator_delegation_snapshot.rs b/program/src/initialize_vault_operator_delegation_snapshot.rs index 4cfe33f..b7fe402 100644 --- a/program/src/initialize_vault_operator_delegation_snapshot.rs +++ b/program/src/initialize_vault_operator_delegation_snapshot.rs @@ -1,13 +1,9 @@ use jito_bytemuck::AccountDeserialize; -use jito_jsm_core::{ - create_account, - loader::{load_signer, load_system_account, load_system_program}, -}; use jito_restaking_core::{ config::Config, ncn::Ncn, ncn_vault_ticket::NcnVaultTicket, operator::Operator, }; use jito_tip_router_core::{ - epoch_snapshot::{EpochSnapshot, OperatorSnapshot, VaultOperatorDelegationSnapshot}, + epoch_snapshot::{EpochSnapshot, OperatorSnapshot}, loaders::load_ncn_epoch, ncn_config::NcnConfig, weight_table::WeightTable, @@ -18,7 +14,7 @@ use jito_vault_core::{ }; use solana_program::{ account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, msg, - program_error::ProgramError, pubkey::Pubkey, rent::Rent, sysvar::Sysvar, + program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar, }; pub fn process_initialize_vault_operator_delegation_snapshot( @@ -27,7 +23,7 @@ pub fn process_initialize_vault_operator_delegation_snapshot( first_slot_of_ncn_epoch: Option, ) -> ProgramResult { //TODO remove payer, system_program, and vault_operator_delegation_snapshot - let [ncn_config, restaking_config, ncn, operator, vault, vault_ncn_ticket, ncn_vault_ticket, vault_operator_delegation, weight_table, epoch_snapshot, operator_snapshot, vault_operator_delegation_snapshot, payer, vault_program, restaking_program, system_program] = + let [ncn_config, restaking_config, ncn, operator, vault, vault_ncn_ticket, ncn_vault_ticket, vault_operator_delegation, weight_table, epoch_snapshot, operator_snapshot, vault_program, restaking_program] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); @@ -72,12 +68,6 @@ pub fn process_initialize_vault_operator_delegation_snapshot( )?; } - //TODO redact - // load_system_account(vault_operator_delegation_snapshot, true)?; - // load_system_program(system_program)?; - // //TODO check that it is not writable - // load_signer(payer, false)?; - let current_slot = Clock::get()?.slot; let (ncn_epoch, ncn_epoch_length) = load_ncn_epoch(restaking_config, current_slot, first_slot_of_ncn_epoch)?; @@ -93,43 +83,6 @@ pub fn process_initialize_vault_operator_delegation_snapshot( true, )?; - let ( - vault_operator_delegation_snapshot_pubkey, - vault_operator_delegation_snapshot_bump, - mut vault_operator_delegation_snapshot_seeds, - ) = VaultOperatorDelegationSnapshot::find_program_address( - program_id, - vault.key, - operator.key, - ncn.key, - ncn_epoch, - ); - vault_operator_delegation_snapshot_seeds.push(vec![vault_operator_delegation_snapshot_bump]); - - if vault_operator_delegation_snapshot_pubkey.ne(operator_snapshot.key) { - msg!("Incorrect vault operator delegation snapshot PDA"); - return Err(ProgramError::InvalidAccountData); - } - - //TODO redact - // msg!( - // "Initializing vault operator delegation snapshot {} for NCN: {} at epoch: {}", - // epoch_snapshot.key, - // ncn.key, - // ncn_epoch - // ); - // create_account( - // payer, - // operator_snapshot, - // system_program, - // program_id, - // &Rent::get()?, - // 8_u64 - // .checked_add(std::mem::size_of::() as u64) - // .unwrap(), - // &vault_operator_delegation_snapshot_seeds, - // )?; - let (vault_index, st_mint) = { let vault_data = vault.data.borrow(); let vault_account = Vault::try_from_slice_unchecked(&vault_data)?; @@ -158,16 +111,7 @@ pub fn process_initialize_vault_operator_delegation_snapshot( vault_ncn_okay && ncn_vault_okay && delegation_dne }; - // let mut vault_operator_delegation_snapshot_data: std::cell::RefMut<'_, &mut [u8]> = - // operator_snapshot.try_borrow_mut_data()?; - // vault_operator_delegation_snapshot_data[0] = VaultOperatorDelegationSnapshot::DISCRIMINATOR; - // let vault_operator_delegation_snapshot_account = - // VaultOperatorDelegationSnapshot::try_from_slice_unchecked_mut( - // &mut vault_operator_delegation_snapshot_data, - // )?; - - // *vault_operator_delegation_snapshot_account = if is_active { - let vault_operator_delegation_snapshot_account = if is_active { + let total_votes: u128 = if is_active { let vault_operator_delegation_data = vault_operator_delegation.data.borrow(); let vault_operator_delegation_account = VaultOperatorDelegation::try_from_slice_unchecked(&vault_operator_delegation_data)?; @@ -175,30 +119,15 @@ pub fn process_initialize_vault_operator_delegation_snapshot( 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::create_snapshot( - *vault.key, - *operator.key, - *ncn.key, - ncn_epoch, - vault_operator_delegation_snapshot_bump, - current_slot, - vault_index, - st_mint, - vault_operator_delegation_account, - weight_table_account, - )? + let total_votes = OperatorSnapshot::calculate_total_votes( + &vault_operator_delegation_account, + &weight_table_account, + &st_mint, + )?; + + total_votes } else { - VaultOperatorDelegationSnapshot::new_inactive( - *vault.key, - *operator.key, - *ncn.key, - ncn_epoch, - vault_operator_delegation_snapshot_bump, - current_slot, - vault_index, - st_mint, - ) + 0u128 }; // Increment vault operator delegation @@ -208,9 +137,9 @@ pub fn process_initialize_vault_operator_delegation_snapshot( operator_snapshot_account.increment_vault_operator_delegation_registration( current_slot, - vault_operator_delegation_snapshot_account.vault(), - vault_operator_delegation_snapshot_account.vault_index(), - vault_operator_delegation_snapshot_account.total_votes(), + *vault.key, + vault_index, + total_votes, )?; // If operator is finalized, increment operator registration