Skip to content

Commit 7384789

Browse files
nagarajm22Jouzo
andauthored
WIP : Ocean Fixing transaction Indexing bugs (#2802)
* updated trasnction id while saving vin and vout with invalidate method * fixed format * fixed transaction data type and updated index_transaction * fixed transaction structure for indexing trasnaction,vin and vout * fixed transaction id to tuple * Restore cpp fmt * Remove unwrap in check_if_evm_tx * Remove ffi exports of index_transaction_* * Remove transaction clone * Remove second iteration for total_vout_value * Index vout by TransactionVoutKey * Fix TransactionVout typing * Index vin by TransactionVinKey * Index from getblock 2 format * Cleanup --------- Co-authored-by: jouzo <jdesclercs@gmail.com>
1 parent c8cc376 commit 7384789

43 files changed

Lines changed: 552 additions & 531 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ sha3 = "0.10"
6363

6464
num = { version = "0.4", default-features = false, features = ["alloc"] }
6565
num-traits = "0.2"
66+
num_cpus = "1.0"
6667

6768
hex = "0.4"
6869
hex-literal = "0.4"
@@ -121,6 +122,7 @@ substrate-bn = "0.6"
121122
dftx-rs = { git = "https://github.com/Jouzo/dftx-rs.git" }
122123
bitcoin = "0.31"
123124
cached = "0.46"
125+
defichain-rpc = { version = "0.18.0", git = "https://github.com/Jouzo/rust-defichain-rpc.git" }
124126

125127
### Local crates
126128
ain-cpp-imports = { path = "./ain-cpp-imports" }

lib/ain-db/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ edition = "2021"
99
serde.workspace = true
1010
bincode.workspace = true
1111
rocksdb.workspace = true
12-
ain-cpp-imports.workspace = true
1312
anyhow.workspace = true
13+
num_cpus.workspace = true
14+

lib/ain-db/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fn get_db_options() -> Options {
2121
options.create_if_missing(true);
2222
options.create_missing_column_families(true);
2323

24-
let n = ain_cpp_imports::get_num_cores();
25-
options.increase_parallelism(n);
24+
let n = num_cpus::get();
25+
options.increase_parallelism(n as i32);
2626

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

@@ -239,3 +239,9 @@ impl From<BincodeError> for DBError {
239239
DBError::Bincode(e)
240240
}
241241
}
242+
243+
impl From<anyhow::Error> for DBError {
244+
fn from(e: anyhow::Error) -> Self {
245+
DBError::Custom(e)
246+
}
247+
}

lib/ain-ocean/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ chrono = "0.4.31"
3939
cached.workspace = true
4040
lazy_static.workspace = true
4141
bincode.workspace = true
42-
defichain-rpc = { version = "0.18.0", git = "https://github.com/Jouzo/rust-defichain-rpc.git" }
42+
defichain-rpc.workspace = true
4343
jsonrpc = "0.14.1"
4444
serde_urlencoded = { version = "0.7" }

lib/ain-ocean/src/api/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async fn list_transaction_unspent(Path(Address { address }): Path<Address>) -> S
5757
format!("List unspent transactions for address {}", address)
5858
}
5959

60-
pub fn router(state: Arc<Client>) -> Router {
60+
pub fn router(services: Arc<Services>) -> Router {
6161
Router::new().nest(
6262
"/:address",
6363
Router::new()

lib/ain-ocean/src/api/block.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::sync::Arc;
22

33
use ain_macros::ocean_endpoint;
4-
use axum::{extract::Path, routing::get, Router};
4+
use axum::{extract::Path, routing::get, Extension, Router};
55
use bitcoin::BlockHash;
6-
use defichain_rpc::Client;
76
use serde::{Deserialize, Deserializer};
87

98
use super::response::ApiPagedResponse;
@@ -12,7 +11,7 @@ use crate::{
1211
error::ApiError,
1312
model::Block,
1413
repository::RepositoryOps,
15-
Result, SERVICES,
14+
Result, Services,
1615
};
1716

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

3938
#[ocean_endpoint]
40-
async fn list_blocks(Query(query): Query<PaginationQuery>) -> Result<ApiPagedResponse<Block>> {
41-
let blocks = SERVICES
39+
async fn list_blocks(
40+
Query(query): Query<PaginationQuery>,
41+
Extension(services): Extension<Arc<Services>>,
42+
) -> Result<ApiPagedResponse<Block>> {
43+
let blocks = services
4244
.block
4345
.by_height
4446
.list(None)?
4547
.take(query.size)
4648
.map(|item| {
4749
let (_, id) = item?;
48-
let b = SERVICES
50+
let b = services
4951
.block
5052
.by_id
5153
.get(&id)?
@@ -56,40 +58,47 @@ async fn list_blocks(Query(query): Query<PaginationQuery>) -> Result<ApiPagedRes
5658
.collect::<Result<Vec<_>>>()?;
5759

5860
Ok(ApiPagedResponse::of(blocks, query.size, |block| {
59-
block.clone().id
61+
block.clone().hash
6062
}))
6163
}
6264

6365
#[ocean_endpoint]
64-
async fn get_block(Path(id): Path<HashOrHeight>) -> Result<Option<Block>> {
66+
async fn get_block(
67+
Path(id): Path<HashOrHeight>,
68+
Extension(services): Extension<Arc<Services>>,
69+
) -> Result<Option<Block>> {
6570
let block = if let Some(id) = match id {
66-
HashOrHeight::Height(n) => SERVICES.block.by_height.get(&n)?,
71+
HashOrHeight::Height(n) => services.block.by_height.get(&n)?,
6772
HashOrHeight::Id(id) => Some(id),
6873
} {
69-
SERVICES.block.by_id.get(&id)?
74+
services.block.by_id.get(&id)?
7075
} else {
7176
None
7277
};
7378

7479
Ok(block)
7580
}
7681

77-
async fn get_transactions(Path(hash): Path<BlockHash>) -> String {
82+
async fn get_transactions(
83+
Path(hash): Path<BlockHash>,
84+
Extension(services): Extension<Arc<Services>>,
85+
) -> String {
7886
format!("Transactions for block with hash {}", hash)
7987
}
8088

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

8694
Ok(block)
8795
}
8896

89-
pub fn router(state: Arc<Client>) -> Router {
97+
pub fn router(services: Arc<Services>) -> Router {
9098
Router::new()
9199
.route("/", get(list_blocks))
92100
.route("/highest", get(get_highest))
93101
.route("/:id", get(get_block))
94102
.route("/:hash/transactions", get(get_transactions))
103+
.layer(Extension(services))
95104
}

lib/ain-ocean/src/api/fee.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use defichain_rpc::{json::mining::SmartFeeEstimation, Client, RpcApi};
66
use serde::Deserialize;
77

88
use super::response::Response;
9-
use crate::{api_query::Query, error::ApiError, Result};
9+
use crate::{api_query::Query, error::ApiError, Result, Services};
1010

1111
#[derive(Deserialize, Default)]
1212
#[serde(rename_all = "camelCase")]
@@ -19,18 +19,18 @@ async fn estimate_fee(
1919
Query(EstimateQuery {
2020
confirmation_target,
2121
}): Query<EstimateQuery>,
22-
Extension(client): Extension<Arc<Client>>,
22+
Extension(services): Extension<Arc<Services>>,
2323
) -> Result<f64> {
24-
let estimation: SmartFeeEstimation = client.call(
24+
let estimation: SmartFeeEstimation = services.client.call(
2525
"estimatesmartfee",
2626
&[confirmation_target.into(), "CONSERVATIVE".into()],
2727
)?;
2828

2929
Ok(estimation.feerate.unwrap_or(0.00005000))
3030
}
3131

32-
pub fn router(state: Arc<Client>) -> Router {
32+
pub fn router(services: Arc<Services>) -> Router {
3333
Router::new()
3434
.route("/estimate", get(estimate_fee))
35-
.layer(Extension(state))
35+
.layer(Extension(services))
3636
}

lib/ain-ocean/src/api/governance.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::sync::Arc;
33
use ain_macros::ocean_endpoint;
44
use axum::{extract::Path, routing::get, Extension, Router};
55
use bitcoin::Txid;
6-
use defichain_rpc::{json::governance::*, Client, GovernanceRPC};
6+
use defichain_rpc::{json::governance::*, GovernanceRPC};
77
use serde::Deserialize;
88

99
use super::response::{ApiPagedResponse, Response};
1010
use crate::{
1111
api_query::{PaginationQuery, Query},
1212
error::{ApiError, NotFoundKind, OceanError},
13-
Result,
13+
Result, Services,
1414
};
1515

1616
#[derive(Deserialize, Default)]
@@ -27,7 +27,7 @@ pub struct GovernanceQuery {
2727
#[ocean_endpoint]
2828
async fn list_gov_proposals(
2929
Query(query): Query<GovernanceQuery>,
30-
Extension(client): Extension<Arc<Client>>,
30+
Extension(services): Extension<Arc<Services>>,
3131
) -> Result<ApiPagedResponse<ProposalInfo>> {
3232
let size = match query.all {
3333
Some(true) => 0,
@@ -43,7 +43,7 @@ async fn list_gov_proposals(
4343
r#type: query.r#type,
4444
cycle: query.cycle,
4545
};
46-
let proposals = client.list_gov_proposals(Some(opts))?;
46+
let proposals = services.client.list_gov_proposals(Some(opts))?;
4747

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

62-
let proposal = client.get_gov_proposal(txid)?;
62+
let proposal = services.client.get_gov_proposal(txid)?;
6363
Ok(proposal)
6464
}
6565

6666
#[ocean_endpoint]
6767
async fn list_gov_proposal_votes(
6868
Path(proposal_id): Path<String>,
6969
Query(query): Query<GovernanceQuery>,
70-
Extension(client): Extension<Arc<Client>>,
70+
Extension(services): Extension<Arc<Services>>,
7171
) -> Result<ApiPagedResponse<ListVotesResult>> {
7272
let proposal_id: Txid = proposal_id
7373
.parse()
@@ -96,7 +96,7 @@ async fn list_gov_proposal_votes(
9696
aggregate: None,
9797
valid: None,
9898
};
99-
let votes = client.list_gov_proposal_votes(Some(opts))?;
99+
let votes = services.client.list_gov_proposal_votes(Some(opts))?;
100100
let len = votes.len();
101101
Ok(ApiPagedResponse::of(votes, size, |_| {
102102
if let Some(next) = start {
@@ -107,10 +107,10 @@ async fn list_gov_proposal_votes(
107107
}))
108108
}
109109

110-
pub fn router(state: Arc<Client>) -> Router {
110+
pub fn router(services: Arc<Services>) -> Router {
111111
Router::new()
112112
.route("/proposals", get(list_gov_proposals))
113113
.route("/proposals/:id", get(get_gov_proposal))
114114
.route("/proposals/:id/votes", get(list_gov_proposal_votes))
115-
.layer(Extension(state))
115+
.layer(Extension(services))
116116
}

lib/ain-ocean/src/api/loan.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use log::debug;
1111

1212
use crate::{
1313
api_paged_response::ApiPagedResponse, api_query::PaginationQuery,
14-
model::VaultAuctionBatchHistory, repository::RepositoryOps, Result, SERVICES,
14+
model::VaultAuctionBatchHistory, repository::RepositoryOps, services, Result,
1515
};
1616

1717
async fn list_scheme() -> String {
@@ -50,7 +50,6 @@ async fn list_vault_auction_history(
5050
Path((vault_id, height, batch_index)): Path<(Txid, u32, u32)>,
5151
Query(query): Query<PaginationQuery>,
5252
) -> Result<Json<ApiPagedResponse<VaultAuctionBatchHistory>>> {
53-
println!("listvault auction history");
5453
debug!(
5554
"Auction history for vault id {}, height {}, batch index {}",
5655
vault_id, height, batch_index
@@ -75,7 +74,7 @@ async fn list_vault_auction_history(
7574

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

78-
let auctions = SERVICES
77+
let auctions = services
7978
.auction
8079
.by_height
8180
.list(Some((vault_id, batch_index, next.0, next.1)))?
@@ -87,7 +86,7 @@ async fn list_vault_auction_history(
8786
.map(|item| {
8887
let (_, id) = item?;
8988

90-
let auction = SERVICES
89+
let auction = services
9190
.auction
9291
.by_id
9392
.get(&id)?
@@ -108,7 +107,7 @@ async fn list_auction() -> String {
108107
"List of auctions".to_string()
109108
}
110109

111-
pub fn router(state: Arc<Client>) -> Router {
110+
pub fn router(services: Arc<Services>) -> Router {
112111
Router::new()
113112
.route("/schemes", get(list_scheme))
114113
.route("/schemes/:id", get(get_scheme))

0 commit comments

Comments
 (0)