Skip to content
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

Ocean: fix outdated pool #3046

Merged
merged 7 commits into from
Sep 14, 2024
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
10 changes: 10 additions & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ pub mod ffi {
pub rbf_fee_increment: u64,
}

#[derive(Debug, Clone)]
pub struct PoolPairCreationHeight {
pub id: u32,
pub id_token_a: u32,
pub id_token_b: u32,
pub creation_height: u32,
}

#[derive(Debug, Clone)]
pub struct DST20Token {
pub id: u64,
Expand Down Expand Up @@ -49,6 +57,7 @@ pub mod ffi {
unsafe extern "C++" {
include!("ffi/ffiexports.h");
type Attributes;
type PoolPairCreationHeight;
type DST20Token;
type TransactionData;
type SystemTxType;
Expand Down Expand Up @@ -79,6 +88,7 @@ pub mod ffi {
fn getEthSyncStatus() -> [i64; 2];
fn getAttributeValues(mnview_ptr: usize) -> Attributes;
fn CppLogPrintf(message: String);
fn getPoolPairs() -> Vec<PoolPairCreationHeight>;
#[allow(clippy::ptr_arg)]
fn getDST20Tokens(mnview_ptr: usize, tokens: &mut Vec<DST20Token>) -> bool;
fn getClientVersion() -> String;
Expand Down
16 changes: 16 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ mod ffi {
pub rbf_fee_increment: u64,
}

pub struct PoolPairCreationHeight {
pub id: u32,
pub id_token_a: u32,
pub id_token_b: u32,
pub creation_height: u32,
}

pub struct DST20Token {
pub id: u64,
pub name: String,
Expand Down Expand Up @@ -125,6 +132,10 @@ mod ffi {
// Just the logs are skipped.
}

pub fn getPoolPairs() -> Vec<PoolPairCreationHeight> {
unimplemented!("{}", UNIMPL_MSG)
}

#[allow(clippy::ptr_arg)]
pub fn getDST20Tokens(_mnview_ptr: usize, _tokens: &mut Vec<DST20Token>) -> bool {
unimplemented!("{}", UNIMPL_MSG)
Expand Down Expand Up @@ -175,6 +186,7 @@ mod ffi {
}

pub use ffi::Attributes;
pub use ffi::PoolPairCreationHeight;
pub use ffi::SystemTxData;
pub use ffi::SystemTxType;
pub use ffi::TokenAmount;
Expand Down Expand Up @@ -339,6 +351,10 @@ pub fn log_print(message: &str) {
ffi::CppLogPrintf(message.to_owned());
}

pub fn get_pool_pairs() -> Vec<ffi::PoolPairCreationHeight> {
ffi::getPoolPairs()
}

/// Fetches all DST20 tokens in view, returns the result of the migration
#[allow(clippy::ptr_arg)]
pub fn get_dst20_tokens(mnview_ptr: usize, tokens: &mut Vec<ffi::DST20Token>) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ async fn list_transaction_unspent(
.skip(usize::from(query.next.is_some()))
.take(query.size)
.take_while(|item| match item {
Ok((k, _)) => k.0 == hid.clone(),
Ok((k, _)) => k.0 == hid,
_ => true,
})
.map(|item| {
Expand Down
28 changes: 5 additions & 23 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod loan_token;
mod masternode;
pub mod oracle;
pub mod oracle_test;
pub mod poolpair;
pub mod poolswap;
pub mod transaction;
pub mod tx_result;
Expand All @@ -20,7 +19,7 @@ use ain_dftx::{deserialize, is_skipped_tx, DfTx, Stack};
use defichain_rpc::json::blockchain::{Block, Transaction, Vin, VinStandard};
use helper::check_if_evm_tx;
use log::trace;
pub use poolswap::{PoolCreationHeight, PoolSwapAggregatedInterval, AGGREGATED_INTERVALS};
pub use poolswap::{PoolSwapAggregatedInterval, AGGREGATED_INTERVALS};
use rust_decimal::{prelude::FromPrimitive, Decimal};
use snafu::OptionExt;

Expand Down Expand Up @@ -64,7 +63,8 @@ fn get_bucket(block: &Block<Transaction>, interval: i64) -> i64 {
}

fn index_block_start(services: &Arc<Services>, block: &Block<Transaction>) -> Result<()> {
let pool_pairs = list_pool_pairs_by_height(services)?;
let mut pool_pairs = ain_cpp_imports::get_pool_pairs();
pool_pairs.sort_by(|a, b| b.creation_height.cmp(&a.creation_height));

for interval in AGGREGATED_INTERVALS {
for pool_pair in &pool_pairs {
Expand Down Expand Up @@ -120,25 +120,9 @@ fn index_block_start(services: &Arc<Services>, block: &Block<Transaction>) -> Re
Ok(())
}

fn list_pool_pairs_by_height(services: &Arc<Services>) -> Result<Vec<PoolCreationHeight>> {
services
.poolpair
.by_height
.list(None, SortOrder::Descending)?
.map(|el| {
let ((k, _), (pool_id, id_token_a, id_token_b)) = el?;
Ok(PoolCreationHeight {
id: pool_id,
id_token_a,
id_token_b,
creation_height: k,
})
})
.collect::<Result<Vec<_>>>()
}

fn invalidate_block_start(services: &Arc<Services>, block: &Block<Transaction>) -> Result<()> {
let pool_pairs = list_pool_pairs_by_height(services)?;
let mut pool_pairs = ain_cpp_imports::get_pool_pairs();
pool_pairs.sort_by(|a, b| b.creation_height.cmp(&a.creation_height));

for interval in AGGREGATED_INTERVALS {
for pool_pair in &pool_pairs {
Expand Down Expand Up @@ -764,7 +748,6 @@ pub fn index_block(services: &Arc<Services>, block: Block<Transaction>) -> Resul
DfTx::PoolSwap(data) => data.index(services, &ctx)?,
DfTx::SetLoanToken(data) => data.index(services, &ctx)?,
DfTx::CompositeSwap(data) => data.index(services, &ctx)?,
DfTx::CreatePoolPair(data) => data.index(services, &ctx)?,
DfTx::PlaceAuctionBid(data) => data.index(services, &ctx)?,
_ => (),
}
Expand Down Expand Up @@ -862,7 +845,6 @@ pub fn invalidate_block(services: &Arc<Services>, block: Block<Transaction>) ->
DfTx::PoolSwap(data) => data.invalidate(services, &ctx)?, // check
DfTx::SetLoanToken(data) => data.invalidate(services, &ctx)?,
DfTx::CompositeSwap(data) => data.invalidate(services, &ctx)?,
DfTx::CreatePoolPair(data) => data.invalidate(services, &ctx)?,
DfTx::PlaceAuctionBid(data) => data.invalidate(services, &ctx)?,
_ => (),
}
Expand Down
57 changes: 0 additions & 57 deletions lib/ain-ocean/src/indexer/poolpair.rs

This file was deleted.

18 changes: 2 additions & 16 deletions lib/ain-ocean/src/indexer/poolswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ pub enum PoolSwapAggregatedInterval {
OneHour = 60 * 60,
}

#[derive(Debug, Clone)]
pub struct PoolCreationHeight {
pub id: u32,
pub id_token_a: u32,
pub id_token_b: u32,
pub creation_height: u32,
}

fn index_swap_aggregated(
services: &Arc<Services>,
pool_id: u32,
Expand Down Expand Up @@ -248,7 +240,7 @@ impl Index for CompositeSwap {
let from_amount = self.pool_swap.from_amount;
let to_token_id = self.pool_swap.to_token_id.0;

let Some(TxResult::PoolSwap(PoolSwapResult { to_amount, .. })) =
let Some(TxResult::PoolSwap(PoolSwapResult { to_amount, pool_id })) =
services.result.get(&txid)?
else {
trace!("Missing swap result for {}", txid.to_string());
Expand All @@ -260,13 +252,7 @@ impl Index for CompositeSwap {
let pools = self.pools.as_ref();

let pool_ids = if pools.is_empty() {
let Some(pool_id) = services
.poolpair
.by_id
.get(&(from_token_id as u32, to_token_id as u32))?
else {
return Err("Missing pool_id".into());
};
// the pool_id from finals wap is the only swap while pools is empty
Vec::from([pool_id])
} else {
pools.iter().map(|pool| pool.id.0 as u32).collect()
Expand Down
12 changes: 1 addition & 11 deletions lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use indexer::{
index_block, invalidate_block,
oracle::invalidate_oracle_interval,
transaction::{index_transaction, invalidate_transaction},
tx_result, PoolCreationHeight,
tx_result,
};
use parking_lot::Mutex;
use petgraph::graphmap::UnGraphMap;
Expand Down Expand Up @@ -55,11 +55,6 @@ pub struct PoolService {
by_id: PoolSwap,
}

pub struct PoolPairService {
by_height: PoolPairByHeight,
by_id: PoolPair,
}

pub struct PoolSwapAggregatedService {
by_id: PoolSwapAggregated,
by_key: PoolSwapAggregatedKey,
Expand Down Expand Up @@ -135,7 +130,6 @@ pub struct Services {
pub auction: AuctionService,
pub result: TxResult,
pub pool: PoolService,
pub poolpair: PoolPairService,
pub pool_swap_aggregated: PoolSwapAggregatedService,
pub transaction: TransactionService,
pub oracle: OracleService,
Expand Down Expand Up @@ -171,10 +165,6 @@ impl Services {
by_height: VaultAuctionHistoryByHeight::new(Arc::clone(&store)),
},
result: TxResult::new(Arc::clone(&store)),
poolpair: PoolPairService {
by_height: PoolPairByHeight::new(Arc::clone(&store)),
by_id: PoolPair::new(Arc::clone(&store)),
},
pool: PoolService {
by_id: PoolSwap::new(Arc::clone(&store)),
},
Expand Down
4 changes: 0 additions & 4 deletions lib/ain-ocean/src/model/tx_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct PoolSwapResult {
#[derive(Serialize, Deserialize, Debug)]
pub enum TxResult {
PoolSwap(PoolSwapResult),
CreatePoolPair(u32),
None,
}

Expand All @@ -23,9 +22,6 @@ impl From<(u8, usize)> for TxResult {
CustomTxType::PoolSwap | CustomTxType::PoolSwapV2 => {
Self::PoolSwap(unsafe { *(result_ptr as *const PoolSwapResult) })
}
CustomTxType::CreatePoolPair => {
Self::CreatePoolPair(unsafe { *(result_ptr as *const u32) })
}
_ => Self::None,
}
}
Expand Down
20 changes: 1 addition & 19 deletions lib/ain-ocean/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,6 @@ define_table! {
SecondaryIndex = PoolSwapAggregated
}

define_table! {
#[derive(Debug)]
pub struct PoolPair {
key_type = (u32, u32),
value_type = u32,
}
}

define_table! {
#[derive(Debug)]
pub struct PoolPairByHeight {
key_type = (u32, usize),
value_type = (u32, u32, u32),
}
}

define_table! {
#[derive(Debug)]
pub struct PriceTicker {
Expand Down Expand Up @@ -486,7 +470,7 @@ define_table! {
SecondaryIndex = VaultAuctionHistory
}

pub const COLUMN_NAMES: [&str; 37] = [
pub const COLUMN_NAMES: [&str; 35] = [
Block::NAME,
BlockByHeight::NAME,
MasternodeStats::NAME,
Expand All @@ -508,8 +492,6 @@ pub const COLUMN_NAMES: [&str; 37] = [
PoolSwapAggregated::NAME,
PoolSwapAggregatedKey::NAME,
PoolSwap::NAME,
PoolPair::NAME,
PoolPairByHeight::NAME,
PriceTicker::NAME,
PriceTickerKey::NAME,
RawBlock::NAME,
Expand Down
7 changes: 0 additions & 7 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,6 @@ pub mod ffi {
pub tx_hash: [u8; 32],
}

pub struct PoolCreationHeight {
pub id: u32,
pub id_token_a: u32,
pub id_token_b: u32,
pub creation_height: u32,
}

extern "Rust" {
type BlockTemplateWrapper;
// In-fallible functions
Expand Down
7 changes: 1 addition & 6 deletions src/dfi/consensus/poolpairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ Res CPoolPairsConsensus::operator()(const CCreatePoolPairMessage &obj) const {
}
}

if (auto res = mnview.SetPoolPair(tokenId, height, poolPair); !res) {
return res;
}

return OceanSetTxResult(std::make_pair(CustomTxType::CreatePoolPair, tx.GetHash()),
static_cast<std::size_t>(reinterpret_cast<uintptr_t>(&tokenId->v)));
return mnview.SetPoolPair(tokenId, height, poolPair);
}

Res CPoolPairsConsensus::operator()(const CUpdatePoolPairMessage &obj) const {
Expand Down
Loading
Loading