From f4edf3ac41a1da5b15a39c5c7a561ef90137fe5a Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 29 Oct 2024 12:49:25 -0400 Subject: [PATCH 1/7] rm extra msg --- programs/drift_vaults/src/state/vault_depositor.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/programs/drift_vaults/src/state/vault_depositor.rs b/programs/drift_vaults/src/state/vault_depositor.rs index 47b5887f..14549fdd 100644 --- a/programs/drift_vaults/src/state/vault_depositor.rs +++ b/programs/drift_vaults/src/state/vault_depositor.rs @@ -362,8 +362,6 @@ impl VaultDepositor { } = vault.apply_fee(vault_protocol, vault_equity, now)?; let (manager_profit_share, protocol_profit_share) = self.apply_profit_share(vault_equity, vault, vault_protocol)?; - msg!("manager_profit_share: {}", manager_profit_share); - msg!("protocol_profit_share: {}", protocol_profit_share); let (withdraw_value, n_shares) = withdraw_unit.get_withdraw_value_and_shares( withdraw_amount, @@ -772,8 +770,6 @@ impl VaultDepositor { )?; let withdraw_amount = self.last_withdraw_request.value.min(shares_value); - msg!("withdraw amount: {}", withdraw_amount); - let mut spot_market = spot_market_map.get_ref_mut(&vault.spot_market_index)?; // Save relevant data before updating balances From 56caf4cfc07fb1b0e06e0f9c45013439f5c7cf80 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 29 Oct 2024 12:51:40 -0400 Subject: [PATCH 2/7] add missing validate to protocol_request_withdraw --- programs/drift_vaults/src/state/vault.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index 0a2ffff2..0b2ac23c 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -525,13 +525,13 @@ impl Vault { ErrorCode::InvalidVaultWithdrawSize, "Requested n_shares = 0" )?; - validate!( - vault_shares_before >= n_shares, - ErrorCode::InvalidVaultWithdrawSize, - "Requested n_shares={} > manager shares={}", - n_shares, - vault_shares_before, - )?; + validate!( + vault_shares_before >= n_shares, + ErrorCode::InvalidVaultWithdrawSize, + "Requested n_shares={} > manager shares={}", + n_shares, + vault_shares_before, + )?; let total_vault_shares_before = self.total_shares; let user_vault_shares_before = self.user_shares; @@ -801,6 +801,13 @@ impl Vault { ErrorCode::InvalidVaultWithdrawSize, "Requested n_shares = 0" )?; + validate!( + vault_shares_before >= n_shares, + ErrorCode::InvalidVaultWithdrawSize, + "Requested n_shares={} > manager shares={}", + n_shares, + vault_shares_before, + )?; let total_vault_shares_before = self.total_shares; let user_vault_shares_before = self.user_shares; From 8b1abfcb16cf0a77a2ad6b40f4ef2f64293689f0 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 29 Oct 2024 13:00:15 -0400 Subject: [PATCH 3/7] make sure manager shares dont underflow --- programs/drift_vaults/src/state/vault.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index 0b2ac23c..76b0658f 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -310,6 +310,9 @@ impl Vault { self.last_fee_update_ts = now; } + // this will underflow if there is an issue with protocol fee calc + self.get_manager_shares(vault_protocol)?; + Ok(VaultFee { management_fee_payment: management_fee_payment.cast::()?, management_fee_shares: management_fee_shares.cast::()?, From 08bc6ec11cf9a7c7e72e52912e4282497153f1a3 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 29 Oct 2024 13:08:11 -0400 Subject: [PATCH 4/7] another invariant --- programs/drift_vaults/src/state/vault.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index 76b0658f..6a2e8357 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -311,6 +311,12 @@ impl Vault { } // this will underflow if there is an issue with protocol fee calc + validate!( + self.total_shares >= self.user_shares, + ErrorCode::InvalidVaultSharesDetected, + "total_shares must be >= user_shares" + )?; + self.get_manager_shares(vault_protocol)?; Ok(VaultFee { From f013f25791223953808a575bc81747bac0bb7ee3 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 29 Oct 2024 13:15:15 -0400 Subject: [PATCH 5/7] add another invariant --- programs/drift_vaults/src/state/vault.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index 6a2e8357..b5f08091 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -132,6 +132,8 @@ impl Vault { let mut protocol_fee_shares: i128 = 0; let mut skip_ts_update = false; + let total_shares_before = self.total_shares; + let mut handle_no_protocol_fee = |vault: &mut Vault| -> Result<()> { let since_last = now.safe_sub(vault.last_fee_update_ts)?; @@ -317,6 +319,12 @@ impl Vault { "total_shares must be >= user_shares" )?; + validate!( + self.total_shares == total_shares_before, + ErrorCode::InvalidVaultSharesDetected, + "total_shares before and after not equal" + )?; + self.get_manager_shares(vault_protocol)?; Ok(VaultFee { From 15a3f0c2c49ce99bbbdf8d33ee7f3076c665866c Mon Sep 17 00:00:00 2001 From: wphan Date: Tue, 29 Oct 2024 13:30:38 -0700 Subject: [PATCH 6/7] fix invariant check --- programs/drift_vaults/src/state/vault.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index b5f08091..9323f0a1 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -542,13 +542,13 @@ impl Vault { ErrorCode::InvalidVaultWithdrawSize, "Requested n_shares = 0" )?; - validate!( - vault_shares_before >= n_shares, - ErrorCode::InvalidVaultWithdrawSize, - "Requested n_shares={} > manager shares={}", - n_shares, - vault_shares_before, - )?; + validate!( + vault_shares_before >= n_shares, + ErrorCode::InvalidVaultWithdrawSize, + "Requested n_shares={} > manager shares={}", + n_shares, + vault_shares_before, + )?; let total_vault_shares_before = self.total_shares; let user_vault_shares_before = self.user_shares; @@ -819,11 +819,11 @@ impl Vault { "Requested n_shares = 0" )?; validate!( - vault_shares_before >= n_shares, + protocol_shares_before >= n_shares, ErrorCode::InvalidVaultWithdrawSize, - "Requested n_shares={} > manager shares={}", + "Requested n_shares={} > protocol shares={}", n_shares, - vault_shares_before, + protocol_shares_before, )?; let total_vault_shares_before = self.total_shares; From fd8e1dd87a34d64aa7ecf353c1b5de1f0e53a333 Mon Sep 17 00:00:00 2001 From: wphan Date: Tue, 29 Oct 2024 14:01:32 -0700 Subject: [PATCH 7/7] rm incorrect invariant check --- programs/drift_vaults/src/state/vault.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/programs/drift_vaults/src/state/vault.rs b/programs/drift_vaults/src/state/vault.rs index 9323f0a1..a6f8eb8d 100644 --- a/programs/drift_vaults/src/state/vault.rs +++ b/programs/drift_vaults/src/state/vault.rs @@ -132,8 +132,6 @@ impl Vault { let mut protocol_fee_shares: i128 = 0; let mut skip_ts_update = false; - let total_shares_before = self.total_shares; - let mut handle_no_protocol_fee = |vault: &mut Vault| -> Result<()> { let since_last = now.safe_sub(vault.last_fee_update_ts)?; @@ -312,19 +310,13 @@ impl Vault { self.last_fee_update_ts = now; } - // this will underflow if there is an issue with protocol fee calc validate!( self.total_shares >= self.user_shares, ErrorCode::InvalidVaultSharesDetected, "total_shares must be >= user_shares" )?; - validate!( - self.total_shares == total_shares_before, - ErrorCode::InvalidVaultSharesDetected, - "total_shares before and after not equal" - )?; - + // this will underflow if there is an issue with protocol fee calc self.get_manager_shares(vault_protocol)?; Ok(VaultFee {