Skip to content

Commit 4c29cd3

Browse files
committed
chore: ensure sub before adding to LRU cache
1 parent e80ffc3 commit 4c29cd3

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

magicblock-chainlink/src/remote_account_provider/lru_cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl AccountsLruCache {
128128
self.accounts_to_never_evict.iter().cloned().collect()
129129
}
130130

131+
pub fn can_evict(&self, pubkey: &Pubkey) -> bool {
132+
!self.accounts_to_never_evict.contains(pubkey)
133+
}
134+
131135
pub fn pubkeys(&self) -> Vec<Pubkey> {
132136
let subs = self
133137
.subscribed_accounts

magicblock-chainlink/src/remote_account_provider/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,31 +732,28 @@ impl<T: ChainRpcClient, U: ChainPubsubClient> RemoteAccountProvider<T, U> {
732732
&self,
733733
pubkey: &Pubkey,
734734
) -> RemoteAccountProviderResult<()> {
735-
// If an account is evicted then we need to unsubscribe from it first
735+
// 1. First realize subscription
736+
if let Err(err) = self.pubsub_client.subscribe(*pubkey).await {
737+
return Err(err);
738+
}
739+
740+
// 2. Add to LRU cache
741+
// If an account is evicted then we need to unsubscribe from it
736742
// and then inform upstream that we are no longer tracking it
737743
if let Some(evicted) = self.lrucache_subscribed_accounts.add(*pubkey) {
738744
trace!("Evicting {pubkey}");
739745

740746
// 1. Unsubscribe from the account directly (LRU has already removed it)
741747
if let Err(err) = self.pubsub_client.unsubscribe(evicted).await {
748+
// Should we retry here?
742749
warn!(
743750
"Failed to unsubscribe from pubsub for evicted account {evicted}: {err:?}");
744-
// Rollback the LRU add since eviction failed
745-
self.lrucache_subscribed_accounts.remove(pubkey);
746-
return Err(err);
747751
}
748752

749753
// 2. Inform upstream so it can remove it from the store
750754
self.send_removal_update(evicted).await?;
751755
}
752756

753-
// 3. Subscribe to the new account (only after successful eviction handling)
754-
if let Err(err) = self.pubsub_client.subscribe(*pubkey).await {
755-
// Rollback the LRU add since subscription failed
756-
self.lrucache_subscribed_accounts.remove(pubkey);
757-
return Err(err);
758-
}
759-
760757
Ok(())
761758
}
762759

@@ -803,6 +800,13 @@ impl<T: ChainRpcClient, U: ChainPubsubClient> RemoteAccountProvider<T, U> {
803800
&self,
804801
pubkey: &Pubkey,
805802
) -> RemoteAccountProviderResult<()> {
803+
if !self.lrucache_subscribed_accounts.can_evict(pubkey) {
804+
warn!(
805+
"Tried to unsubscribe from account {} that should never be evicted",
806+
pubkey
807+
);
808+
return Ok(());
809+
}
806810
if !self.lrucache_subscribed_accounts.contains(pubkey) {
807811
warn!(
808812
"Tried to unsubscribe from account {} that was not subscribed in the LRU cache",

0 commit comments

Comments
 (0)