Skip to content

fix oraclemap subscription bonk/bonk-perp #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ unsafe_pub = []
abi_stable = { version = "0.11", default-features = false }
ahash = "0.8.11"
anchor-lang = { version = "0.31", features = ["derive"] }
arrayvec = "0.7.6"
base64 = "0.22"
bytemuck = "1.17"
dashmap = "6"
Expand All @@ -50,7 +51,6 @@ tokio-stream = "0.1.17"
tokio-tungstenite = { version = "0.26", features = ["native-tls"] }

drift-pubsub-client = { version = "0.1.1", path = "crates/pubsub-client" }
arrayvec = "0.7.6"

[dev-dependencies]
bytes = "1"
Expand Down
32 changes: 11 additions & 21 deletions crates/src/oraclemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct OracleMap {
/// Oracle data keyed by pubkey
pub oraclemap: Arc<DashMap<(Pubkey, u8), Oracle, ahash::RandomState>>,
/// Oracle subscription handles by pubkey
subscriptions: DashMap<(Pubkey, u8), UnsubHandle, ahash::RandomState>,
subscriptions: DashMap<Pubkey, UnsubHandle, ahash::RandomState>,
/// Oracle (pubkey, source) by MarketId (immutable)
pub oracle_by_market: ReadOnlyView<MarketId, (Pubkey, OracleSource)>,
latest_slot: Arc<AtomicU64>,
Expand Down Expand Up @@ -118,12 +118,10 @@ impl OracleMap {
.expect("oracle exists"); // caller did not supply in `OracleMap::new()`

// markets can share oracle pubkeys, only want one sub per oracle pubkey
if self
.subscriptions
.contains_key(&(*oracle_pubkey, *oracle_source as u8))
if self.subscriptions.contains_key(oracle_pubkey)
|| pending_subscriptions
.iter()
.any(|(_, o)| &o.pubkey == oracle_pubkey && o.source == *oracle_source)
.any(|(_, o)| &o.pubkey == oracle_pubkey)
{
log::debug!(target: LOG_TARGET, "subscription exists: {market:?}/{oracle_pubkey:?}");
continue;
Expand Down Expand Up @@ -156,8 +154,7 @@ impl OracleMap {

while let Some((info, unsub)) = subscription_futs.next().await {
log::debug!(target: LOG_TARGET, "subscribed market oracle: {:?}", info.market);
self.subscriptions
.insert((info.pubkey, info.source as u8), unsub?);
self.subscriptions.insert(info.pubkey, unsub?);
}

log::debug!(target: LOG_TARGET, "subscribed");
Expand All @@ -168,12 +165,10 @@ impl OracleMap {
pub fn unsubscribe(&self, markets: &[MarketId]) -> SdkResult<()> {
for market in markets {
if let Some((oracle_pubkey, oracle_source)) = self.oracle_by_market.get(market) {
if let Some((market, unsub)) = self
.subscriptions
.remove(&(*oracle_pubkey, *oracle_source as u8))
{
if let Some((_, unsub)) = self.subscriptions.remove(&oracle_pubkey) {
let _ = unsub.send(());
self.oraclemap.remove(&market);
self.oraclemap
.remove(&(*oracle_pubkey, *oracle_source as u8));
}
}
}
Expand All @@ -184,11 +179,7 @@ impl OracleMap {

/// Unsubscribe from all oracle updates
pub fn unsubscribe_all(&self) -> SdkResult<()> {
let all_markets: Vec<MarketId> = self
.subscriptions
.iter()
.filter_map(|s| self.oraclemap.get(s.key()).map(|o| o.market))
.collect();
let all_markets: Vec<MarketId> = self.oracle_by_market.keys().copied().collect();
self.unsubscribe(&all_markets)
}

Expand Down Expand Up @@ -258,9 +249,8 @@ impl OracleMap {

/// Returns true if the oraclemap has a subscription for `market`
pub fn is_subscribed(&self, market: &MarketId) -> bool {
if let Some((oracle_pubkey, oracle_source)) = self.oracle_by_market.get(market) {
self.subscriptions
.contains_key(&(*oracle_pubkey, *oracle_source as u8))
if let Some((oracle_pubkey, _oracle_source)) = self.oracle_by_market.get(market) {
self.subscriptions.contains_key(oracle_pubkey)
} else {
false
}
Expand Down Expand Up @@ -343,7 +333,7 @@ fn update_handler(
});
}
Err(err) => {
log::error!("Failed to get oracle price: {err:?}")
log::error!("Failed to get oracle price: {err:?}, {oracle_pubkey:?}")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/src/websocket_account_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl WebsocketAccountSubscriber {
}
}
None => {
log::error!(target: LOG_TARGET, "{subscription_name}: Ws ended unexpectedly");
log::error!(target: LOG_TARGET, "{subscription_name}: Ws ended unexpectedly: {pubkey:?}");
break Err(());
}
}
Expand Down
Loading