From 9286ec469fd03a427cf6189b9268a255cf055c73 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 7 Mar 2024 18:07:26 +0800 Subject: [PATCH 01/12] fix --- dex/src/state.rs | 50 ++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 88b97abc..f596b4d3 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -2950,35 +2950,39 @@ fn action_out_event( ) -> DexResult { check_assert!(owner_slot < 128)?; check_assert_eq!(&open_orders.slot_side(owner_slot), &Some(side))?; - check_assert_eq!(open_orders.orders[owner_slot as usize], order_id)?; + // check_assert_eq!(open_orders.orders[owner_slot as usize], order_id)?; - let fully_out = native_qty_still_locked == 0; + if open_orders.orders[owner_slot as usize] == order_id { + let fully_out = native_qty_still_locked == 0; - match side { - Side::Bid => { - if release_funds { - open_orders.native_pc_free += native_qty_unlocked; + match side { + Side::Bid => { + if release_funds { + open_orders.native_pc_free += native_qty_unlocked; + } + check_assert!(open_orders.native_pc_free <= open_orders.native_pc_total)?; } - check_assert!(open_orders.native_pc_free <= open_orders.native_pc_total)?; - } - Side::Ask => { - if release_funds { - open_orders.native_coin_free += native_qty_unlocked; + Side::Ask => { + if release_funds { + open_orders.native_coin_free += native_qty_unlocked; + } + check_assert!(open_orders.native_coin_free <= open_orders.native_coin_total)?; } - check_assert!(open_orders.native_coin_free <= open_orders.native_coin_total)?; + }; + if let Some(client_id) = client_order_id { + debug_assert_eq!( + client_id.get(), + identity(open_orders.client_order_ids[owner_slot as usize]) + ); + } + if fully_out { + open_orders.remove_order(owner_slot)?; } - }; - if let Some(client_id) = client_order_id { - debug_assert_eq!( - client_id.get(), - identity(open_orders.client_order_ids[owner_slot as usize]) - ); - } - if fully_out { - open_orders.remove_order(owner_slot)?; - } - Ok(()) + Ok(()) + } else { + Ok(()) + } } #[cfg_attr(not(feature = "program"), allow(unused))] From 9da2574e2e9b45adde200a900e53652e0d112100 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 7 Mar 2024 18:14:21 +0800 Subject: [PATCH 02/12] fix --- dex/src/state.rs | 52 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index f596b4d3..e4b9391b 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -2952,37 +2952,37 @@ fn action_out_event( check_assert_eq!(&open_orders.slot_side(owner_slot), &Some(side))?; // check_assert_eq!(open_orders.orders[owner_slot as usize], order_id)?; - if open_orders.orders[owner_slot as usize] == order_id { - let fully_out = native_qty_still_locked == 0; + if open_orders.orders[owner_slot as usize] != order_id { + return Ok(()); + } - match side { - Side::Bid => { - if release_funds { - open_orders.native_pc_free += native_qty_unlocked; - } - check_assert!(open_orders.native_pc_free <= open_orders.native_pc_total)?; - } - Side::Ask => { - if release_funds { - open_orders.native_coin_free += native_qty_unlocked; - } - check_assert!(open_orders.native_coin_free <= open_orders.native_coin_total)?; + let fully_out = native_qty_still_locked == 0; + + match side { + Side::Bid => { + if release_funds { + open_orders.native_pc_free += native_qty_unlocked; } - }; - if let Some(client_id) = client_order_id { - debug_assert_eq!( - client_id.get(), - identity(open_orders.client_order_ids[owner_slot as usize]) - ); + check_assert!(open_orders.native_pc_free <= open_orders.native_pc_total)?; } - if fully_out { - open_orders.remove_order(owner_slot)?; + Side::Ask => { + if release_funds { + open_orders.native_coin_free += native_qty_unlocked; + } + check_assert!(open_orders.native_coin_free <= open_orders.native_coin_total)?; } - - Ok(()) - } else { - Ok(()) + }; + if let Some(client_id) = client_order_id { + debug_assert_eq!( + client_id.get(), + identity(open_orders.client_order_ids[owner_slot as usize]) + ); + } + if fully_out { + open_orders.remove_order(owner_slot)?; } + + Ok(()) } #[cfg_attr(not(feature = "program"), allow(unused))] From 3022eaa0e45106c12fe27161c362434a2262132b Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 7 Mar 2024 18:55:21 +0800 Subject: [PATCH 03/12] fix --- dex/src/state.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index e4b9391b..6b1f6a03 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -2949,10 +2949,12 @@ fn action_out_event( client_order_id: Option, ) -> DexResult { check_assert!(owner_slot < 128)?; - check_assert_eq!(&open_orders.slot_side(owner_slot), &Some(side))?; + // check_assert_eq!(&open_orders.slot_side(owner_slot), &Some(side))?; // check_assert_eq!(open_orders.orders[owner_slot as usize], order_id)?; - if open_orders.orders[owner_slot as usize] != order_id { + if open_orders.orders[owner_slot as usize] != order_id + || &open_orders.slot_side(owner_slot) != &Some(side) + { return Ok(()); } From ede5318e568eb16d304cbcb7396de602e4b8b51e Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 21 Mar 2024 19:44:28 +1100 Subject: [PATCH 04/12] oo check fix --- dex/src/state.rs | 62 +++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 6b1f6a03..bb9053d3 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3445,15 +3445,11 @@ impl State { let owner: [u64; 4] = event.owner; let owner_index: Result = open_orders_accounts .binary_search_by_key(&owner, |account_info| account_info.key.to_aligned_bytes()); - let mut open_orders: RefMut = match owner_index { + let open_orders_result: DexResult> = match owner_index { Err(_) => break, - Ok(i) => market.load_orders_mut( - &open_orders_accounts[i], - None, - program_id, - None, - None, - )?, + Ok(i) => { + market.load_orders_mut(&open_orders_accounts[i], None, program_id, None, None) + } }; match event.as_view()? { @@ -3471,12 +3467,14 @@ impl State { } => { match side { Side::Bid if maker => { + let mut open_orders = open_orders_result.unwrap(); open_orders.native_pc_total -= native_qty_paid; open_orders.native_coin_total += native_qty_received; open_orders.native_coin_free += native_qty_received; open_orders.native_pc_free += native_fee_or_rebate; } Side::Ask if maker => { + let mut open_orders = open_orders_result.unwrap(); open_orders.native_coin_total -= native_qty_paid; open_orders.native_pc_total += native_qty_received; open_orders.native_pc_free += native_qty_received; @@ -3485,13 +3483,24 @@ impl State { }; if !maker { let referrer_rebate = fees::referrer_rebate(native_fee_or_rebate); - open_orders.referrer_rebates_accrued += referrer_rebate; + + match open_orders_result { + Ok(open_orders) => { + open_orders.referrer_rebates_accrued += referrer_rebate + } + Err(e) => continue, + } } if let Some(client_id) = client_order_id { - debug_assert_eq!( - client_id.get(), - identity(open_orders.client_order_ids[owner_slot as usize]) - ); + match open_orders_result { + Ok(open_orders) => { + debug_assert_eq!( + client_id.get(), + identity(open_orders.client_order_ids[owner_slot as usize]) + ); + } + Err(e) => continue, + } } } EventView::Out { @@ -3503,18 +3512,21 @@ impl State { owner: _, owner_slot, client_order_id, - } => { - action_out_event( - &mut open_orders, - order_id, - side, - release_funds, - native_qty_unlocked, - native_qty_still_locked, - owner_slot, - client_order_id, - )?; - } + } => match open_orders_result { + Ok(open_orders) => { + action_out_event( + &mut open_orders, + order_id, + side, + release_funds, + native_qty_unlocked, + native_qty_still_locked, + owner_slot, + client_order_id, + )?; + } + Err(e) => continue, + }, }; event_q From 259118909672a5914a5190ab6d00acd7d04d2783 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 21 Mar 2024 19:52:28 +1100 Subject: [PATCH 05/12] change --- dex/src/state.rs | 63 +++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index bb9053d3..cda01d19 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3445,10 +3445,19 @@ impl State { let owner: [u64; 4] = event.owner; let owner_index: Result = open_orders_accounts .binary_search_by_key(&owner, |account_info| account_info.key.to_aligned_bytes()); - let open_orders_result: DexResult> = match owner_index { + let mut open_orders: RefMut = match owner_index { Err(_) => break, Ok(i) => { - market.load_orders_mut(&open_orders_accounts[i], None, program_id, None, None) + match market.load_orders_mut( + &open_orders_accounts[i], + None, + program_id, + None, + None, + ) { + Ok(acc) => acc, + Err(_) => continue, + } } }; @@ -3467,14 +3476,12 @@ impl State { } => { match side { Side::Bid if maker => { - let mut open_orders = open_orders_result.unwrap(); open_orders.native_pc_total -= native_qty_paid; open_orders.native_coin_total += native_qty_received; open_orders.native_coin_free += native_qty_received; open_orders.native_pc_free += native_fee_or_rebate; } Side::Ask if maker => { - let mut open_orders = open_orders_result.unwrap(); open_orders.native_coin_total -= native_qty_paid; open_orders.native_pc_total += native_qty_received; open_orders.native_pc_free += native_qty_received; @@ -3483,24 +3490,13 @@ impl State { }; if !maker { let referrer_rebate = fees::referrer_rebate(native_fee_or_rebate); - - match open_orders_result { - Ok(open_orders) => { - open_orders.referrer_rebates_accrued += referrer_rebate - } - Err(e) => continue, - } + open_orders.referrer_rebates_accrued += referrer_rebate; } if let Some(client_id) = client_order_id { - match open_orders_result { - Ok(open_orders) => { - debug_assert_eq!( - client_id.get(), - identity(open_orders.client_order_ids[owner_slot as usize]) - ); - } - Err(e) => continue, - } + debug_assert_eq!( + client_id.get(), + identity(open_orders.client_order_ids[owner_slot as usize]) + ); } } EventView::Out { @@ -3512,21 +3508,18 @@ impl State { owner: _, owner_slot, client_order_id, - } => match open_orders_result { - Ok(open_orders) => { - action_out_event( - &mut open_orders, - order_id, - side, - release_funds, - native_qty_unlocked, - native_qty_still_locked, - owner_slot, - client_order_id, - )?; - } - Err(e) => continue, - }, + } => { + action_out_event( + &mut open_orders, + order_id, + side, + release_funds, + native_qty_unlocked, + native_qty_still_locked, + owner_slot, + client_order_id, + )?; + } }; event_q From fa22948f846271da9736c5baf79f26fc85b1523c Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Thu, 21 Mar 2024 19:54:30 +1100 Subject: [PATCH 06/12] add comment --- dex/src/state.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/dex/src/state.rs b/dex/src/state.rs index cda01d19..f87fa4db 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3456,6 +3456,7 @@ impl State { None, ) { Ok(acc) => acc, + // TODO: Actually we could leave this if we do open orders accoutn seeds checking in the zeta program Err(_) => continue, } } From fe58b88a0fa4e3d6601dbe5209f193a40e35485a Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Mon, 25 Mar 2024 15:53:20 +1100 Subject: [PATCH 07/12] revert --- dex/src/state.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index f87fa4db..0e1c42f3 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3446,20 +3446,14 @@ impl State { let owner_index: Result = open_orders_accounts .binary_search_by_key(&owner, |account_info| account_info.key.to_aligned_bytes()); let mut open_orders: RefMut = match owner_index { + Ok(i) => market.load_orders_mut( + &open_orders_accounts[i], + None, + program_id, + None, + None, + )?, Err(_) => break, - Ok(i) => { - match market.load_orders_mut( - &open_orders_accounts[i], - None, - program_id, - None, - None, - ) { - Ok(acc) => acc, - // TODO: Actually we could leave this if we do open orders accoutn seeds checking in the zeta program - Err(_) => continue, - } - } }; match event.as_view()? { From 50f61170f84cd9c75bf8d4da36394e968ee188e2 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Tue, 26 Mar 2024 09:20:04 +1100 Subject: [PATCH 08/12] fix --- dex/src/state.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 0e1c42f3..8b2e8b15 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3446,13 +3446,16 @@ impl State { let owner_index: Result = open_orders_accounts .binary_search_by_key(&owner, |account_info| account_info.key.to_aligned_bytes()); let mut open_orders: RefMut = match owner_index { - Ok(i) => market.load_orders_mut( + Ok(i) => match market.load_orders_mut( &open_orders_accounts[i], None, program_id, None, None, - )?, + ) { + Ok(x) => x, + Err(_) => continue, + }, Err(_) => break, }; From 8a31787662bd8d38af964908d2ab5d3ae12b632c Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Tue, 26 Mar 2024 10:43:22 +1100 Subject: [PATCH 09/12] fix --- dex/src/state.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 8b2e8b15..9bde93a6 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3454,7 +3454,13 @@ impl State { None, ) { Ok(x) => x, - Err(_) => continue, + Err(_) => { + event_q + .pop_front() + .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) + .unwrap(); + continue; + } }, Err(_) => break, }; From f38f1f5750fbaa03278055e8fa6093900a9e9d48 Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 26 Mar 2024 12:08:23 +1100 Subject: [PATCH 10/12] fix --- dex/src/state.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 9bde93a6..4a8bc987 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -1,8 +1,12 @@ #![cfg_attr(not(feature = "program"), allow(unused))] use num_enum::TryFromPrimitive; use std::{ - cell::RefMut, convert::identity, convert::TryInto, mem::size_of, num::NonZeroU64, ops::Deref, - ops::DerefMut, + cell::RefMut, + convert::{identity, TryInto}, + mem::size_of, + num::NonZeroU64, + ops::{Deref, DerefMut}, + str::FromStr, }; use arrayref::{array_ref, array_refs, mut_array_refs}; @@ -16,7 +20,7 @@ use num_traits::FromPrimitive; use safe_transmute::{self, to_bytes::transmute_to_bytes, trivial::TriviallyTransmutable}; use solana_program::{ - account_info::AccountInfo, clock::Clock, program_error::ProgramError, program_pack::Pack, + account_info::AccountInfo, clock::Clock, msg, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, rent::Rent, sysvar::Sysvar, }; use spl_token::error::TokenError; @@ -3437,8 +3441,22 @@ impl State { } = args; for _i in 0u16..limit { + if *open_orders_accounts[0].key + == Pubkey::from_str("9ToLAwKbk4fGhaE6oRxrZSTciunaNsvGzSff8htBXxKH").unwrap() + { + msg!("Overriding and breaking"); + event_q + .pop_front() + .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) + .unwrap(); + break; + } + let event = match event_q.peek_front() { - None => break, + None => { + msg!("Breaking 1"); + break; + } Some(e) => e, }; @@ -3455,6 +3473,7 @@ impl State { ) { Ok(x) => x, Err(_) => { + msg!("Caught error in loading orders, popping front"); event_q .pop_front() .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) @@ -3462,7 +3481,10 @@ impl State { continue; } }, - Err(_) => break, + Err(_) => { + msg!("Breaking 2"); + break; + } }; match event.as_view()? { From 3b5f198073e758d4bdc3456c22ba99e37a7f406e Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 26 Mar 2024 12:28:14 +1100 Subject: [PATCH 11/12] Revert "fix" This reverts commit f38f1f5750fbaa03278055e8fa6093900a9e9d48. --- dex/src/state.rs | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 4a8bc987..9bde93a6 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -1,12 +1,8 @@ #![cfg_attr(not(feature = "program"), allow(unused))] use num_enum::TryFromPrimitive; use std::{ - cell::RefMut, - convert::{identity, TryInto}, - mem::size_of, - num::NonZeroU64, - ops::{Deref, DerefMut}, - str::FromStr, + cell::RefMut, convert::identity, convert::TryInto, mem::size_of, num::NonZeroU64, ops::Deref, + ops::DerefMut, }; use arrayref::{array_ref, array_refs, mut_array_refs}; @@ -20,7 +16,7 @@ use num_traits::FromPrimitive; use safe_transmute::{self, to_bytes::transmute_to_bytes, trivial::TriviallyTransmutable}; use solana_program::{ - account_info::AccountInfo, clock::Clock, msg, program_error::ProgramError, program_pack::Pack, + account_info::AccountInfo, clock::Clock, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, rent::Rent, sysvar::Sysvar, }; use spl_token::error::TokenError; @@ -3441,22 +3437,8 @@ impl State { } = args; for _i in 0u16..limit { - if *open_orders_accounts[0].key - == Pubkey::from_str("9ToLAwKbk4fGhaE6oRxrZSTciunaNsvGzSff8htBXxKH").unwrap() - { - msg!("Overriding and breaking"); - event_q - .pop_front() - .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) - .unwrap(); - break; - } - let event = match event_q.peek_front() { - None => { - msg!("Breaking 1"); - break; - } + None => break, Some(e) => e, }; @@ -3473,7 +3455,6 @@ impl State { ) { Ok(x) => x, Err(_) => { - msg!("Caught error in loading orders, popping front"); event_q .pop_front() .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) @@ -3481,10 +3462,7 @@ impl State { continue; } }, - Err(_) => { - msg!("Breaking 2"); - break; - } + Err(_) => break, }; match event.as_view()? { From 359465d51a49781ca594911137e4a50f61c67142 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Tue, 26 Mar 2024 13:39:32 +1100 Subject: [PATCH 12/12] revert --- dex/src/state.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/dex/src/state.rs b/dex/src/state.rs index 9bde93a6..0e1c42f3 100644 --- a/dex/src/state.rs +++ b/dex/src/state.rs @@ -3446,22 +3446,13 @@ impl State { let owner_index: Result = open_orders_accounts .binary_search_by_key(&owner, |account_info| account_info.key.to_aligned_bytes()); let mut open_orders: RefMut = match owner_index { - Ok(i) => match market.load_orders_mut( + Ok(i) => market.load_orders_mut( &open_orders_accounts[i], None, program_id, None, None, - ) { - Ok(x) => x, - Err(_) => { - event_q - .pop_front() - .map_err(|()| DexErrorCode::ConsumeEventsQueueFailure) - .unwrap(); - continue; - } - }, + )?, Err(_) => break, };