From f099e22b571302bd5b55d19042068f284f0da422 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Wed, 23 Oct 2024 17:40:33 +0800 Subject: [PATCH] opt: rm oracle_price_active key --- lib/ain-ocean/src/api/loan.rs | 63 ++++++++++--------- lib/ain-ocean/src/api/prices.rs | 54 +++++++++++----- lib/ain-ocean/src/indexer/loan_token.rs | 29 ++++----- lib/ain-ocean/src/lib.rs | 2 - .../src/model/oracle_price_active.rs | 5 +- lib/ain-ocean/src/storage/mod.rs | 12 +--- 6 files changed, 89 insertions(+), 76 deletions(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index c44c07a34d..1891763771 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -36,7 +36,7 @@ use super::{ use crate::{ error::{ApiError, Error, NotFoundKind, NotFoundSnafu}, model::{OraclePriceActive, VaultAuctionBatchHistory}, - storage::{RepositoryOps, SecondaryIndex, SortOrder}, + storage::{RepositoryOps, SortOrder}, Result, }; @@ -131,13 +131,17 @@ fn get_active_price( fixed_interval_price_id: String, ) -> Result> { let (token, currency) = parse_fixed_interval_price(&fixed_interval_price_id)?; - let repo = &ctx.services.oracle_price_active; - let key = repo.by_key.get(&(token, currency))?; - let price = if let Some(key) = key { - repo.by_id.get(&key)? - } else { - None - }; + let price = ctx + .services + .oracle_price_active + .by_id + .list(Some((token, currency, u32::MAX)), SortOrder::Descending)? + .next() + .map(|item| { + let (_, v) = item?; + Ok::(v) + }) + .transpose()?; Ok(price) } @@ -254,13 +258,17 @@ async fn list_loan_token( let fixed_interval_price_id = flatten_token.fixed_interval_price_id.clone(); let (token, currency) = parse_fixed_interval_price(&fixed_interval_price_id)?; - let repo = &ctx.services.oracle_price_active; - let key = repo.by_key.get(&(token, currency))?; - let active_price = if let Some(key) = key { - repo.by_id.get(&key)? - } else { - None - }; + let active_price = ctx + .services + .oracle_price_active + .by_id + .list(Some((token, currency, u32::MAX)), SortOrder::Descending)? + .next() + .map(|item| { + let (_, v) = item?; + Ok::(v) + }) + .transpose()?; let token = LoanToken { token_id: flatten_token.data.creation_tx.clone(), @@ -658,23 +666,20 @@ async fn map_token_amounts( log::error!("Token {token_symbol} not found"); continue; }; - let repo = &ctx.services.oracle_price_active; - let keys = repo - .by_key - .list(None, SortOrder::Descending)? - .collect::>(); - log::trace!("list_auctions keys: {:?}, token_id: {:?}", keys, id); - let active_price = repo - .by_key - .list(None, SortOrder::Descending)? - .take(1) + let active_price = ctx.services.oracle_price_active + .by_id + .list(Some((token_info.symbol.clone(), "USD".to_string(), u32::MAX)), SortOrder::Descending)? .take_while(|item| match item { - Ok((k, _)) => k.0 == id, + Ok((k, _)) => k.0 == token_info.symbol && k.1 == "USD", _ => true, }) - .map(|el| repo.by_key.retrieve_primary_value(el)) - .collect::>>()?; + .next() + .map(|item| { + let (_, v) = item?; + Ok::(v) + }) + .transpose()?; vault_token_amounts.push(VaultTokenAmountResponse { id, @@ -683,7 +688,7 @@ async fn map_token_amounts( symbol: token_info.symbol, symbol_key: token_info.symbol_key, name: token_info.name, - active_price: active_price.first().cloned(), + active_price, }); } diff --git a/lib/ain-ocean/src/api/prices.rs b/lib/ain-ocean/src/api/prices.rs index 9eb9365768..7d24785ef2 100644 --- a/lib/ain-ocean/src/api/prices.rs +++ b/lib/ain-ocean/src/api/prices.rs @@ -22,10 +22,9 @@ use super::{ AppContext, }; use crate::{ - error::{ApiError, Error, OtherSnafu}, + error::{ApiError, OtherSnafu}, model::{ - BlockContext, OracleIntervalSeconds, OraclePriceActive, - OraclePriceAggregatedIntervalAggregated, PriceTicker, + BlockContext, OracleIntervalSeconds, OraclePriceActive, OraclePriceActiveNext, OraclePriceAggregatedIntervalAggregated, PriceTicker }, storage::{RepositoryOps, SortOrder}, Result, @@ -233,29 +232,56 @@ async fn get_feed( )) } +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct OraclePriceActiveResponse { + pub id: String, // token-currency-height + pub key: String, // token-currency + pub sort: String, // height + pub active: Option, + pub next: Option, + pub is_live: bool, + pub block: BlockContext, +} + +impl OraclePriceActiveResponse { + fn from_with_id(token: &String, currency: &String, v: OraclePriceActive) -> Self { + Self { + id: format!("{}-{}-{}", token, currency, v.block.height), + key: format!("{}-{}", token, currency), + sort: hex::encode(v.block.height.to_be_bytes()).to_string(), + active: v.active, + next: v.next, + is_live: v.is_live, + block: v.block, + } + } +} + #[ocean_endpoint] async fn get_feed_active( Path(key): Path, Query(query): Query, Extension(ctx): Extension>, -) -> Result> { +) -> Result> { let (token, currency) = parse_token_currency(&key)?; - let key = (token, currency); - let repo = &ctx.services.oracle_price_active; + let id = (token.clone(), currency.clone(), u32::MAX); let price_active = ctx .services .oracle_price_active - .by_key - .list(Some(key), SortOrder::Descending)? + .by_id + .list(Some(id), SortOrder::Descending)? + .take_while(|item| match item { + Ok(((t, c, _), _)) => t == &token && c == ¤cy, + _ => true, + }) .take(query.size) - .flat_map(|item| { - let (_, id) = item?; - let item = repo.by_id.get(&id)?; - Ok::, Error>(item) + .map(|item| { + let ((token, currency, _), v) = item?; + Ok(OraclePriceActiveResponse::from_with_id(&token, ¤cy, v)) }) - .flatten() - .collect::>(); + .collect::>>()?; Ok(ApiPagedResponse::of(price_active, query.size, |price| { price.sort.to_string() diff --git a/lib/ain-ocean/src/indexer/loan_token.rs b/lib/ain-ocean/src/indexer/loan_token.rs index 181de23073..a4f5317907 100644 --- a/lib/ain-ocean/src/indexer/loan_token.rs +++ b/lib/ain-ocean/src/indexer/loan_token.rs @@ -102,7 +102,6 @@ pub fn index_active_price(services: &Arc, block: &BlockContext) -> Res fn map_active_price( block: &BlockContext, - ticker_id: (String, String), aggregated_price: OraclePriceAggregated, prev_price: Option, ) -> OraclePriceActive { @@ -123,9 +122,6 @@ fn map_active_price( }; OraclePriceActive { - id: (ticker_id.0.clone(), ticker_id.1.clone(), block.height), - key: ticker_id, - sort: hex::encode(block.height.to_be_bytes()), active: active_price.clone(), next: next_price.clone(), is_live: is_live(active_price, next_price), @@ -165,10 +161,12 @@ pub fn perform_active_price_tick( ticker_id: (Token, Currency), block: &BlockContext, ) -> Result<()> { - let repo = &services.oracle_price_aggregated; - let prev = repo + let id = (ticker_id.0.clone(), ticker_id.1.clone(), u32::MAX); + + let prev = services + .oracle_price_aggregated .by_id - .list(Some ((ticker_id.0.clone(), ticker_id.1.clone(), u32::MAX)), SortOrder::Descending)? + .list(Some (id.clone()), SortOrder::Descending)? .next() .transpose()?; @@ -177,23 +175,22 @@ pub fn perform_active_price_tick( }; let repo = &services.oracle_price_active; - let prev_key = repo - .by_key - .list(Some(ticker_id.clone()), SortOrder::Descending)? + let prev = repo + .by_id + .list(Some(id.clone()), SortOrder::Descending)? .next() .transpose()?; - let prev_price = if let Some((_, prev_id)) = prev_key { - repo.by_id.get(&prev_id)? + let prev_price = if let Some((_, prev)) = prev { + Some(prev) } else { None }; - let active_price = map_active_price(block, ticker_id, aggregated_price, prev_price); - - repo.by_id.put(&active_price.id, &active_price)?; + let active_price = map_active_price(block, aggregated_price, prev_price); - repo.by_key.put(&active_price.key, &active_price.id)?; + let new_id = (id.0, id.1, block.height); + repo.by_id.put(&new_id, &active_price)?; Ok(()) } diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 57d5d267e7..e0902d44cd 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -74,7 +74,6 @@ pub struct OraclePriceFeedService { by_id: OraclePriceFeed, } pub struct OraclePriceActiveService { - by_key: OraclePriceActiveKey, by_id: OraclePriceActive, } pub struct OraclePriceAggregatedIntervalService { @@ -181,7 +180,6 @@ impl Services { by_id: OraclePriceFeed::new(Arc::clone(&store)), }, oracle_price_active: OraclePriceActiveService { - by_key: OraclePriceActiveKey::new(Arc::clone(&store)), by_id: OraclePriceActive::new(Arc::clone(&store)), }, oracle_price_aggregated_interval: OraclePriceAggregatedIntervalService { diff --git a/lib/ain-ocean/src/model/oracle_price_active.rs b/lib/ain-ocean/src/model/oracle_price_active.rs index b913ea9d7c..3b9740f08e 100644 --- a/lib/ain-ocean/src/model/oracle_price_active.rs +++ b/lib/ain-ocean/src/model/oracle_price_active.rs @@ -4,13 +4,10 @@ use serde::{Deserialize, Serialize}; use super::BlockContext; pub type OraclePriceActiveId = (String, String, u32); //token-currency-height -pub type OraclePriceActiveKey = (String, String); //token-currency + #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct OraclePriceActive { - pub id: OraclePriceActiveId, - pub key: OraclePriceActiveKey, - pub sort: String, //height pub active: Option, pub next: Option, pub is_live: bool, diff --git a/lib/ain-ocean/src/storage/mod.rs b/lib/ain-ocean/src/storage/mod.rs index 52b3d551b4..803a4d4c62 100644 --- a/lib/ain-ocean/src/storage/mod.rs +++ b/lib/ain-ocean/src/storage/mod.rs @@ -164,15 +164,6 @@ define_table! { } } -define_table! { - #[derive(Debug)] - pub struct OraclePriceActiveKey { - key_type = model::OraclePriceActiveKey, - value_type = model::OraclePriceActiveId, - }, - SecondaryIndex = OraclePriceActive -} - define_table! { #[derive(Debug)] pub struct OraclePriceAggregated { @@ -426,7 +417,7 @@ define_table! { SecondaryIndex = VaultAuctionHistory } -pub const COLUMN_NAMES: [&str; 30] = [ +pub const COLUMN_NAMES: [&str; 29] = [ Block::NAME, BlockByHeight::NAME, MasternodeStats::NAME, @@ -435,7 +426,6 @@ pub const COLUMN_NAMES: [&str; 30] = [ Oracle::NAME, OracleHistory::NAME, OraclePriceActive::NAME, - OraclePriceActiveKey::NAME, OraclePriceAggregated::NAME, OraclePriceAggregatedInterval::NAME, OraclePriceFeed::NAME,