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: Initial Scan support #2829

Merged
merged 9 commits into from
Feb 22, 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
184 changes: 88 additions & 96 deletions lib/Cargo.lock

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions lib/ain-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::{
use anyhow::format_err;
use bincode;
use rocksdb::{
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, DBIterator, IteratorMode,
Options, DB,
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, DBIterator, Direction,
IteratorMode, Options, DB,
};
use serde::{de::DeserializeOwned, Serialize};

Expand Down Expand Up @@ -181,16 +181,22 @@ where
pub fn iter(
&self,
from: Option<C::Index>,
direction: Direction,
) -> Result<impl Iterator<Item = Result<(C::Index, C::Type)>> + '_> {
let skip = if from.as_ref().is_some() { 1 } else { 0 };
let index = from
.as_ref()
.map(|i| C::key(i))
.transpose()?
.unwrap_or_default();
let iterator_mode = from.map_or(IteratorMode::End, |_| {
IteratorMode::From(&index, rocksdb::Direction::Reverse)
});

let iterator_mode = match direction {
Direction::Forward => from.map_or(IteratorMode::Start, |_| {
IteratorMode::From(&index, Direction::Forward)
}),
Direction::Reverse => from.map_or(IteratorMode::End, |_| {
IteratorMode::From(&index, Direction::Reverse)
}),
};
Ok(self
.backend
.iterator_cf::<C>(self.handle()?, iterator_mode)
Expand All @@ -199,8 +205,7 @@ where
let value = bincode::deserialize(&value)?;
let key = C::get_key(key)?;
Ok((key, value))
})
.skip(skip))
}))
}
}

Expand Down
11 changes: 9 additions & 2 deletions lib/ain-evm/src/storage/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ impl Rollback for BlockStore {
let block_deployed_codes_cf = self.column::<columns::BlockDeployedCodeHashes>();
let address_codes_cf = self.column::<columns::AddressCodeMap>();

for item in block_deployed_codes_cf.iter(Some((block.header.number, H160::zero())))? {
for item in block_deployed_codes_cf.iter(
Some((block.header.number, H160::zero())),
rocksdb::Direction::Reverse,
)? {
let ((block_number, address), hash) = item?;

if block_number == block.header.number {
Expand Down Expand Up @@ -325,7 +328,11 @@ impl BlockStore {
let response_max_size = usize::try_from(ain_cpp_imports::get_max_response_byte_size())
.map_err(|_| format_err!("failed to convert response size limit to usize"))?;

for item in self.column::<C>().iter(from)?.take(limit) {
for item in self
.column::<C>()
.iter(from, rocksdb::Direction::Reverse)?
.take(limit)
{
let (k, v) = item?;

if out.len() > response_max_size {
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ pub fn repository_derive(input: TokenStream) -> TokenStream {
Ok(self.col.delete(id)?)
}

fn list<'a>(&'a self, from: Option<#key_type_ident>) -> Result<Box<dyn Iterator<Item = std::result::Result<(#key_type_ident, #value_type_ident), ain_db::DBError>> + 'a>>
fn list<'a>(&'a self, from: Option<#key_type_ident>, dir: crate::storage::SortOrder) -> Result<Box<dyn Iterator<Item = std::result::Result<(#key_type_ident, #value_type_ident), ain_db::DBError>> + 'a>>
{
let it = self.col.iter(from)?;
let it = self.col.iter(from, dir.into())?;
Ok(Box::new(it))
}
}
Expand Down
45 changes: 40 additions & 5 deletions lib/ain-ocean/src/api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use super::{
AppContext,
};
use crate::{
api::common::Paginate,
error::{ApiError, Error},
model::Block,
model::{Block, Transaction},
repository::RepositoryOps,
storage::SortOrder,
Result,
};

Expand Down Expand Up @@ -46,6 +48,7 @@ async fn list_blocks(
) -> Result<ApiPagedResponse<Block>> {
let next = query
.next
.as_ref()
.map(|q| {
let height = q
.parse::<u32>()
Expand All @@ -58,8 +61,8 @@ async fn list_blocks(
.services
.block
.by_height
.list(next)?
.take(query.size)
.list(next, SortOrder::Descending)?
.paginate(&query)
.map(|item| {
let (_, id) = item?;
let b = ctx
Expand Down Expand Up @@ -95,11 +98,43 @@ async fn get_block(
Ok(Response::new(block))
}

#[ocean_endpoint]
async fn get_transactions(
Path(hash): Path<BlockHash>,
Query(query): Query<PaginationQuery>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> String {
format!("Transactions for block with hash {}", hash)
) -> Result<ApiPagedResponse<Transaction>> {
let next = query.next.as_ref().map_or(Ok((hash, 0)), |q| {
let height = q
.parse::<usize>()
.map_err(|_| format_err!("Invalid height"))?;
Ok::<(BlockHash, usize), Error>((hash, height))
})?;

let txs = ctx
.services
.transaction
.by_block_hash
.list(Some(next), SortOrder::Ascending)?
.paginate(&query)
.take_while(|item| match item {
Ok(((h, _), _)) => h == &hash,
_ => true,
})
.map(|item| {
let (_, id) = item?;
let tx = ctx
.services
.transaction
.by_id
.get(&id)?
.ok_or("Missing tx index")?;

Ok(tx)
})
.collect::<Result<Vec<_>>>()?;

Ok(ApiPagedResponse::of(txs, query.size, |tx| tx.order))
}

// Get highest indexed block
Expand Down
14 changes: 11 additions & 3 deletions lib/ain-ocean/src/api/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,32 @@ pub fn find_token_balance(tokens: Vec<String>, symbol: &str) -> Decimal {
/// .list_loan_schemes()
/// .await?
/// .into_iter()
/// .paginate(&query, skip_while)
/// .fake_paginate(&query, skip_while)
/// .collect();
///
/// assert!(res.len() <= query.size, "The result should not contain more items than the specified limit");
/// assert!(res[0].id > query.next.unwrap(), "The result should start after the requested start id");
/// ```
pub trait Paginate<'a, T>: Iterator<Item = T> + Sized {
fn paginate<F>(
fn fake_paginate<F>(
self,
query: &PaginationQuery,
skip_while: F,
) -> Box<dyn Iterator<Item = T> + 'a>
where
F: FnMut(&T) -> bool + 'a;
fn paginate(self, query: &PaginationQuery) -> Box<dyn Iterator<Item = T> + 'a>;
}

impl<'a, T, I> Paginate<'a, T> for I
where
I: Iterator<Item = T> + 'a,
{
fn paginate<F>(self, query: &PaginationQuery, skip_while: F) -> Box<dyn Iterator<Item = T> + 'a>
fn fake_paginate<F>(
self,
query: &PaginationQuery,
skip_while: F,
) -> Box<dyn Iterator<Item = T> + 'a>
where
F: FnMut(&T) -> bool + 'a,
{
Expand All @@ -137,4 +142,7 @@ where
.take(query.size),
)
}
fn paginate(self, query: &PaginationQuery) -> Box<dyn Iterator<Item = T> + 'a> {
Box::new(self.skip(query.next.is_some() as usize).take(query.size))
}
}
13 changes: 8 additions & 5 deletions lib/ain-ocean/src/api/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
error::{ApiError, Error, NotFoundKind},
model::VaultAuctionBatchHistory,
repository::RepositoryOps,
storage::SortOrder,
Result,
};

Expand Down Expand Up @@ -63,7 +64,7 @@ async fn list_scheme(
.list_loan_schemes()
.await?
.into_iter()
.paginate(&query, skip_while)
.fake_paginate(&query, skip_while)
.map(Into::into)
.collect();
Ok(ApiPagedResponse::of(res, query.size, |loan_scheme| {
Expand All @@ -76,7 +77,6 @@ async fn get_scheme(
Path(scheme_id): Path<String>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> Result<Response<LoanSchemeData>> {
println!("[get_scheme]");
Ok(Response::new(
ctx.client.get_loan_scheme(scheme_id).await?.into(),
))
Expand Down Expand Up @@ -120,7 +120,7 @@ async fn list_collateral_token(

let fut = tokens
.into_iter()
.paginate(&query, skip_while)
.fake_paginate(&query, skip_while)
.map(|v| async {
let (id, info) = get_token_cached(&ctx, &v.token_id).await?;
Ok::<CollateralToken, Error>(CollateralToken::from_with_id(id, v, info))
Expand Down Expand Up @@ -187,7 +187,7 @@ async fn list_loan_token(
interest: el.interest,
})
})
.paginate(&query, |token| match &query.next {
.fake_paginate(&query, |token| match &query.next {
None => false,
Some(v) => v != &token.id,
})
Expand Down Expand Up @@ -259,7 +259,10 @@ async fn list_vault_auction_history(
.services
.auction
.by_height
.list(Some((vault_id, batch_index, next.0, next.1)))?
.list(
Some((vault_id, batch_index, next.0, next.1)),
SortOrder::Descending,
)?
.take(size)
.take_while(|item| match item {
Ok((k, _)) => k.0 == vault_id && k.1 == batch_index,
Expand Down
7 changes: 5 additions & 2 deletions lib/ain-ocean/src/api/masternode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use super::{
AppContext,
};
use crate::{
api::common::Paginate,
error::{ApiError, Error, NotFoundKind},
model::Masternode,
repository::RepositoryOps,
storage::SortOrder,
Result,
};

Expand Down Expand Up @@ -105,6 +107,7 @@ async fn list_masternodes(
) -> Result<ApiPagedResponse<MasternodeData>> {
let next = query
.next
.as_ref()
.map(|q| {
let height = q[0..8]
.parse::<u32>()
Expand All @@ -121,8 +124,8 @@ async fn list_masternodes(
.services
.masternode
.by_height
.list(next)?
.take(query.size)
.list(next, SortOrder::Descending)?
.paginate(&query)
.map(|item| {
let ((_, id), _) = item?;
let mn = ctx
Expand Down
37 changes: 21 additions & 16 deletions lib/ain-ocean/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod query;
mod response;
mod stats;
mod tokens;
// mod transactions;
mod transactions;

use defichain_rpc::Client;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -59,19 +59,24 @@ pub async fn ocean_router(services: &Arc<Services>, client: Arc<Client>) -> Resu
services: services.clone(),
});

Ok(Router::new()
// .nest("/address", address::router(Arc::clone(&context)))
.nest("/governance", governance::router(Arc::clone(&context)))
.nest("/loans", loan::router(Arc::clone(&context)))
.nest("/fee", fee::router(Arc::clone(&context)))
.nest("/masternodes", masternode::router(Arc::clone(&context)))
// .nest("/oracles", oracle::router(Arc::clone(&context)))
// .nest("/poolpairs", poolpairs::router(Arc::clone(&context)))
// .nest("/prices", prices::router(Arc::clone(&context)))
// .nest("/rawtx", rawtx::router(Arc::clone(&context)))
.nest("/stats", stats::router(Arc::clone(&context)))
.nest("/tokens", tokens::router(Arc::clone(&context)))
// .nest("/transactions", transactions::router(Arc::clone(&context)))
.nest("/blocks", block::router(Arc::clone(&context)))
.fallback(not_found))
let network = ain_cpp_imports::get_network();

Ok(Router::new().nest(
format!("/v0/{network}").as_str(),
Router::new()
// .nest("/address/", address::router(Arc::clone(&context)))
.nest("/governance", governance::router(Arc::clone(&context)))
.nest("/loans", loan::router(Arc::clone(&context)))
.nest("/fee", fee::router(Arc::clone(&context)))
.nest("/masternodes", masternode::router(Arc::clone(&context)))
// .nest("/oracles", oracle::router(Arc::clone(&context)))
// .nest("/poolpairs", poolpairs::router(Arc::clone(&context)))
// .nest("/prices", prices::router(Arc::clone(&context)))
// .nest("/rawtx", rawtx::router(Arc::clone(&context)))
.nest("/stats", stats::router(Arc::clone(&context)))
.nest("/tokens", tokens::router(Arc::clone(&context)))
.nest("/transactions", transactions::router(Arc::clone(&context)))
.nest("/blocks", block::router(Arc::clone(&context)))
.fallback(not_found),
))
}
1 change: 1 addition & 0 deletions lib/ain-ocean/src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct PaginationQuery {
#[serde_as(as = "DisplayFromStr")]
#[serde(default = "default_pagination_size")]
pub size: usize,
#[serde(default)]
#[serde(deserialize_with = "undefined_to_none")]
pub next: Option<String>,
}
Expand Down
18 changes: 15 additions & 3 deletions lib/ain-ocean/src/api/stats/stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use cached::proc_macro::cached;
use defichain_rpc::{
Expand Down Expand Up @@ -100,15 +100,27 @@ pub async fn get_count(ctx: &Arc<AppContext>) -> Result<Count> {
})
}

// TODO Shove it into network struct when available
lazy_static::lazy_static! {
pub static ref BURN_ADDRESS: HashMap<&'static str, &'static str> = HashMap::from([
("mainnet", "8defichainBurnAddressXXXXXXXdRQkSm"),
("testnet", "7DefichainBurnAddressXXXXXXXdMUE5n"),
("devnet", "7DefichainBurnAddressXXXXXXXdMUE5n"),
("changi", "7DefichainBurnAddressXXXXXXXdMUE5n"),
("regtest", "mfburnZSAM7Gs1hpDeNaMotJXSGA7edosG"),
]);
}

#[cached(
result = true,
time = 1800,
key = "String",
convert = r#"{ format!("burned_total") }"#
)]
pub async fn get_burned_total(client: &Client) -> Result<Decimal> {
static ADDRESS: &'static str = "76a914f7874e8821097615ec345f74c7e5bcf61b12e2ee88ac";
let mut tokens = client.get_account(ADDRESS, None, Some(true)).await?;
let network = ain_cpp_imports::get_network();
let burn_address = BURN_ADDRESS.get(network.as_str()).unwrap();
let mut tokens = client.get_account(&burn_address, None, Some(true)).await?;
let burn_info = client.get_burn_info().await?;

let utxo = Decimal::from_f64(burn_info.amount).ok_or(Error::DecimalError)?;
Expand Down
Loading
Loading