Skip to content

Commit

Permalink
dex(eviction): parse position id from index key
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed May 31, 2024
1 parent c4497e1 commit f733274
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions crates/core/component/dex/src/component/eviction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::collections::BTreeSet;
use crate::state_key::eviction_queue;
use anyhow::Result;
use cnidarium::StateWrite;
use penumbra_proto::DomainType;
use tracing::instrument;

use crate::{component::PositionManager, DirectedTradingPair, TradingPair};
Expand Down Expand Up @@ -84,13 +83,19 @@ pub(crate) trait EvictionManager: StateWrite {

let overhead_ab = stream_ab
.take(overhead_size as usize)
.and_then(|(_, raw_id)| async move { position::Id::decode(&*raw_id) })
.and_then(|(k, _)| async move {
let raw_id = eviction_queue::inventory_index::parse_id_from_key(k)?;
Ok(position::Id(raw_id))
})
.try_collect::<BTreeSet<position::Id>>()
.await?;

let overhead_ba = stream_ba
.take(overhead_size as usize)
.and_then(|(_, raw_id)| async move { position::Id::decode(&*raw_id) })
.and_then(|(k, _)| async move {
let raw_id = eviction_queue::inventory_index::parse_id_from_key(k)?;
Ok(position::Id(raw_id))
})
.try_collect::<BTreeSet<position::Id>>()
.await?;

Expand Down
7 changes: 7 additions & 0 deletions crates/core/component/dex/src/state_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub(crate) mod eviction_queue {
pub(crate) mod inventory_index {
use crate::lp::position;
use crate::DirectedTradingPair;
use anyhow::ensure;
use penumbra_num::Amount;

pub(crate) fn by_trading_pair(pair: &DirectedTradingPair) -> [u8; 107] {
Expand All @@ -245,5 +246,11 @@ pub(crate) mod eviction_queue {

full_key
}

pub(crate) fn parse_id_from_key(key: Vec<u8>) -> anyhow::Result<[u8; 32]> {
ensure!(key.len() == 155, "key must be 155 bytes");
let k = &key[123..155];
Ok(k.try_into()?)
}
}
}
6 changes: 0 additions & 6 deletions repro.sh

This file was deleted.

0 comments on commit f733274

Please sign in to comment.