Skip to content
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
7 changes: 4 additions & 3 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ sha3 = "0.10"

num = { version = "0.4", default-features = false, features = ["alloc"] }
num-traits = "0.2"
num_cpus = "1.0"

hex = "0.4"
hex-literal = "0.4"
Expand Down Expand Up @@ -121,6 +122,7 @@ substrate-bn = "0.6"
dftx-rs = { git = "https://github.com/Jouzo/dftx-rs.git" }
bitcoin = "0.31"
cached = "0.46"
defichain-rpc = { version = "0.18.0", git = "https://github.com/Jouzo/rust-defichain-rpc.git" }

### Local crates
ain-cpp-imports = { path = "./ain-cpp-imports" }
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
serde.workspace = true
bincode.workspace = true
rocksdb.workspace = true
ain-cpp-imports.workspace = true
anyhow.workspace = true
num_cpus.workspace = true

10 changes: 8 additions & 2 deletions lib/ain-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fn get_db_options() -> Options {
options.create_if_missing(true);
options.create_missing_column_families(true);

let n = ain_cpp_imports::get_num_cores();
options.increase_parallelism(n);
let n = num_cpus::get();
options.increase_parallelism(n as i32);

let mut env = rocksdb::Env::new().unwrap();

Expand Down Expand Up @@ -239,3 +239,9 @@ impl From<BincodeError> for DBError {
DBError::Bincode(e)
}
}

impl From<anyhow::Error> for DBError {
fn from(e: anyhow::Error) -> Self {
DBError::Custom(e)
}
}
2 changes: 1 addition & 1 deletion lib/ain-ocean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ chrono = "0.4.31"
cached.workspace = true
lazy_static.workspace = true
bincode.workspace = true
defichain-rpc = { version = "0.18.0", git = "https://github.com/Jouzo/rust-defichain-rpc.git" }
defichain-rpc.workspace = true
jsonrpc = "0.14.1"
serde_urlencoded = { version = "0.7" }
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 @@ -57,7 +57,7 @@ async fn list_transaction_unspent(Path(Address { address }): Path<Address>) -> S
format!("List unspent transactions for address {}", address)
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new().nest(
"/:address",
Router::new()
Expand Down
37 changes: 23 additions & 14 deletions lib/ain-ocean/src/api/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::sync::Arc;

use ain_macros::ocean_endpoint;
use axum::{extract::Path, routing::get, Router};
use axum::{extract::Path, routing::get, Extension, Router};
use bitcoin::BlockHash;
use defichain_rpc::Client;
use serde::{Deserialize, Deserializer};

use super::response::ApiPagedResponse;
Expand All @@ -12,7 +11,7 @@ use crate::{
error::ApiError,
model::Block,
repository::RepositoryOps,
Result, SERVICES,
Result, Services,
};

pub enum HashOrHeight {
Expand All @@ -37,15 +36,18 @@ impl<'de> Deserialize<'de> for HashOrHeight {
}

#[ocean_endpoint]
async fn list_blocks(Query(query): Query<PaginationQuery>) -> Result<ApiPagedResponse<Block>> {
let blocks = SERVICES
async fn list_blocks(
Query(query): Query<PaginationQuery>,
Extension(services): Extension<Arc<Services>>,
) -> Result<ApiPagedResponse<Block>> {
let blocks = services
.block
.by_height
.list(None)?
.take(query.size)
.map(|item| {
let (_, id) = item?;
let b = SERVICES
let b = services
.block
.by_id
.get(&id)?
Expand All @@ -56,40 +58,47 @@ async fn list_blocks(Query(query): Query<PaginationQuery>) -> Result<ApiPagedRes
.collect::<Result<Vec<_>>>()?;

Ok(ApiPagedResponse::of(blocks, query.size, |block| {
block.clone().id
block.clone().hash
}))
}

#[ocean_endpoint]
async fn get_block(Path(id): Path<HashOrHeight>) -> Result<Option<Block>> {
async fn get_block(
Path(id): Path<HashOrHeight>,
Extension(services): Extension<Arc<Services>>,
) -> Result<Option<Block>> {
let block = if let Some(id) = match id {
HashOrHeight::Height(n) => SERVICES.block.by_height.get(&n)?,
HashOrHeight::Height(n) => services.block.by_height.get(&n)?,
HashOrHeight::Id(id) => Some(id),
} {
SERVICES.block.by_id.get(&id)?
services.block.by_id.get(&id)?
} else {
None
};

Ok(block)
}

async fn get_transactions(Path(hash): Path<BlockHash>) -> String {
async fn get_transactions(
Path(hash): Path<BlockHash>,
Extension(services): Extension<Arc<Services>>,
) -> String {
format!("Transactions for block with hash {}", hash)
}

// Get highest indexed block
#[ocean_endpoint]
async fn get_highest() -> Result<Option<Block>> {
let block = SERVICES.block.by_height.get_highest()?;
async fn get_highest(Extension(services): Extension<Arc<Services>>) -> Result<Option<Block>> {
let block = services.block.by_height.get_highest()?;

Ok(block)
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new()
.route("/", get(list_blocks))
.route("/highest", get(get_highest))
.route("/:id", get(get_block))
.route("/:hash/transactions", get(get_transactions))
.layer(Extension(services))
}
10 changes: 5 additions & 5 deletions lib/ain-ocean/src/api/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use defichain_rpc::{json::mining::SmartFeeEstimation, Client, RpcApi};
use serde::Deserialize;

use super::response::Response;
use crate::{api_query::Query, error::ApiError, Result};
use crate::{api_query::Query, error::ApiError, Result, Services};

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase")]
Expand All @@ -19,18 +19,18 @@ async fn estimate_fee(
Query(EstimateQuery {
confirmation_target,
}): Query<EstimateQuery>,
Extension(client): Extension<Arc<Client>>,
Extension(services): Extension<Arc<Services>>,
) -> Result<f64> {
let estimation: SmartFeeEstimation = client.call(
let estimation: SmartFeeEstimation = services.client.call(
"estimatesmartfee",
&[confirmation_target.into(), "CONSERVATIVE".into()],
)?;

Ok(estimation.feerate.unwrap_or(0.00005000))
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new()
.route("/estimate", get(estimate_fee))
.layer(Extension(state))
.layer(Extension(services))
}
20 changes: 10 additions & 10 deletions lib/ain-ocean/src/api/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::sync::Arc;
use ain_macros::ocean_endpoint;
use axum::{extract::Path, routing::get, Extension, Router};
use bitcoin::Txid;
use defichain_rpc::{json::governance::*, Client, GovernanceRPC};
use defichain_rpc::{json::governance::*, GovernanceRPC};
use serde::Deserialize;

use super::response::{ApiPagedResponse, Response};
use crate::{
api_query::{PaginationQuery, Query},
error::{ApiError, NotFoundKind, OceanError},
Result,
Result, Services,
};

#[derive(Deserialize, Default)]
Expand All @@ -27,7 +27,7 @@ pub struct GovernanceQuery {
#[ocean_endpoint]
async fn list_gov_proposals(
Query(query): Query<GovernanceQuery>,
Extension(client): Extension<Arc<Client>>,
Extension(services): Extension<Arc<Services>>,
) -> Result<ApiPagedResponse<ProposalInfo>> {
let size = match query.all {
Some(true) => 0,
Expand All @@ -43,7 +43,7 @@ async fn list_gov_proposals(
r#type: query.r#type,
cycle: query.cycle,
};
let proposals = client.list_gov_proposals(Some(opts))?;
let proposals = services.client.list_gov_proposals(Some(opts))?;

Ok(ApiPagedResponse::of(proposals, size, |proposal| {
proposal.proposal_id.to_string()
Expand All @@ -53,21 +53,21 @@ async fn list_gov_proposals(
#[ocean_endpoint]
async fn get_gov_proposal(
Path(proposal_id): Path<String>,
Extension(client): Extension<Arc<Client>>,
Extension(services): Extension<Arc<Services>>,
) -> Result<ProposalInfo> {
let txid: Txid = proposal_id
.parse()
.map_err(|_| OceanError::NotFound(NotFoundKind::Proposal))?;

let proposal = client.get_gov_proposal(txid)?;
let proposal = services.client.get_gov_proposal(txid)?;
Ok(proposal)
}

#[ocean_endpoint]
async fn list_gov_proposal_votes(
Path(proposal_id): Path<String>,
Query(query): Query<GovernanceQuery>,
Extension(client): Extension<Arc<Client>>,
Extension(services): Extension<Arc<Services>>,
) -> Result<ApiPagedResponse<ListVotesResult>> {
let proposal_id: Txid = proposal_id
.parse()
Expand Down Expand Up @@ -96,7 +96,7 @@ async fn list_gov_proposal_votes(
aggregate: None,
valid: None,
};
let votes = client.list_gov_proposal_votes(Some(opts))?;
let votes = services.client.list_gov_proposal_votes(Some(opts))?;
let len = votes.len();
Ok(ApiPagedResponse::of(votes, size, |_| {
if let Some(next) = start {
Expand All @@ -107,10 +107,10 @@ async fn list_gov_proposal_votes(
}))
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new()
.route("/proposals", get(list_gov_proposals))
.route("/proposals/:id", get(get_gov_proposal))
.route("/proposals/:id/votes", get(list_gov_proposal_votes))
.layer(Extension(state))
.layer(Extension(services))
}
9 changes: 4 additions & 5 deletions lib/ain-ocean/src/api/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use log::debug;

use crate::{
api_paged_response::ApiPagedResponse, api_query::PaginationQuery,
model::VaultAuctionBatchHistory, repository::RepositoryOps, Result, SERVICES,
model::VaultAuctionBatchHistory, repository::RepositoryOps, services, Result,
};

async fn list_scheme() -> String {
Expand Down Expand Up @@ -50,7 +50,6 @@ async fn list_vault_auction_history(
Path((vault_id, height, batch_index)): Path<(Txid, u32, u32)>,
Query(query): Query<PaginationQuery>,
) -> Result<Json<ApiPagedResponse<VaultAuctionBatchHistory>>> {
println!("listvault auction history");
debug!(
"Auction history for vault id {}, height {}, batch index {}",
vault_id, height, batch_index
Expand All @@ -75,7 +74,7 @@ async fn list_vault_auction_history(

let size = if query.size > 0 { query.size } else { 20 };

let auctions = SERVICES
let auctions = services
.auction
.by_height
.list(Some((vault_id, batch_index, next.0, next.1)))?
Expand All @@ -87,7 +86,7 @@ async fn list_vault_auction_history(
.map(|item| {
let (_, id) = item?;

let auction = SERVICES
let auction = services
.auction
.by_id
.get(&id)?
Expand All @@ -108,7 +107,7 @@ async fn list_auction() -> String {
"List of auctions".to_string()
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new()
.route("/schemes", get(list_scheme))
.route("/schemes/:id", get(get_scheme))
Expand Down
10 changes: 5 additions & 5 deletions lib/ain-ocean/src/api/masternode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

use crate::{
api_paged_response::ApiPagedResponse, api_query::PaginationQuery, model::Masternode,
repository::RepositoryOps, Result, SERVICES,
repository::RepositoryOps, services, Result,
};

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
Expand Down Expand Up @@ -108,14 +108,14 @@ async fn list_masternodes(
})
.transpose()?;

let masternodes = SERVICES
let masternodes = services
.masternode
.by_height
.list(next)?
.take(query.size)
.map(|item| {
let (_, id) = item?;
let mn = SERVICES
let mn = services
.masternode
.by_id
.get(&id)?
Expand All @@ -133,7 +133,7 @@ async fn list_masternodes(
}

async fn get_masternode(Path(masternode_id): Path<Txid>) -> Result<Json<Option<MasternodeData>>> {
let mn = SERVICES
let mn = services
.masternode
.by_id
.get(&masternode_id)?
Expand All @@ -142,7 +142,7 @@ async fn get_masternode(Path(masternode_id): Path<Txid>) -> Result<Json<Option<M
Ok(Json(mn))
}

pub fn router(state: Arc<Client>) -> Router {
pub fn router(services: Arc<Services>) -> Router {
Router::new()
.route("/", get(list_masternodes))
.route("/:id", get(get_masternode))
Expand Down
Loading