Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Oct 6, 2024
1 parent c2d8fc5 commit 744c2df
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 55 deletions.
2 changes: 1 addition & 1 deletion global_function_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn generate_global_functions(_attr: TokenStream, item: TokenStream) -> Token
let function_name = &Ident::new(&format!("{}_{}", struct_name_snake_case, method_name.to_string()), method_name.span());
let return_type = match &method.sig.output {
ReturnType::Default => panic!("Return a type you lazy ass"),
ReturnType::Type(arrow, return_type) => {
ReturnType::Type(_, return_type) => {
&match &**return_type {
Type::Path(return_type) => {
match return_type.path.segments.last() {
Expand Down
2 changes: 0 additions & 2 deletions src-tauri/src/address_index/pivx_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use jsonrpsee::core::client::ClientT;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::http_client::HttpClient;
use jsonrpsee::rpc_params;
use jsonrpsee::types::Params;
use reqwest::header::{HeaderMap, HeaderValue};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/address_index/sql_lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ COMMIT;

impl Database for SqlLite {
async fn get_address_txids(&self, address: &str) -> crate::error::Result<Vec<String>> {
let mut connection = self.connect()?;
let connection = self.connect()?;
let mut stmt = connection.prepare("SELECT txid FROM transactions WHERE address=?1")?;
let mut rows = stmt.query([address])?;
let mut txids = vec![];
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Database for SqlLite {
}

async fn get_txid_from_vin(&self, vin: &Vin) -> crate::error::Result<Option<String>> {
let mut connection = self.connect()?;
let connection = self.connect()?;
let mut stmt =
connection.prepare("SELECT spender_txid FROM vin WHERE txid=?1 AND n=?2;")?;
let mut rows = stmt.query(params![vin.txid, vin.n])?;
Expand Down
55 changes: 8 additions & 47 deletions src-tauri/src/explorer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use address_index::address_extractor;
use error::PIVXErrors;
use futures::{StreamExt, TryStreamExt};
use jsonrpsee::rpc_params;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use std::path::PathBuf;
use tokio::sync::OnceCell;
// TODO: remove this import
use crate::*;

use crate::address_index::{
block_source::BlockSource,
database::Database,
pivx_rpc::PIVXRpc,
sql_lite::SqlLite,
types::{Tx, Vin},
AddressIndex,
block_source::BlockSource, database::Database, pivx_rpc::PIVXRpc, sql_lite::SqlLite,
types::Vin, AddressIndex,
};
use global_function_macro::generate_global_functions;

Expand Down Expand Up @@ -62,10 +56,8 @@ async fn get_explorer() -> &'static DefaultExplorer {
let pivx_rpc = PIVXRpc::new(&format!("http://127.0.0.1:{}", RPC_PORT))
.await
.unwrap();
let mut address_index = AddressIndex::new(
SqlLite::new(PathBuf::from("/home/duddino/test.sqlite"))
.await
.unwrap(),
let address_index = AddressIndex::new(
SqlLite::new(PathBuf::from("~/test.sqlite")).await.unwrap(),
pivx_rpc.clone(),
);
std::mem::forget(pivx);
Expand Down Expand Up @@ -93,7 +85,7 @@ where
}

pub async fn get_block_count(&self) -> crate::error::Result<u64> {
Ok(self.pivx_rpc.call("getblockcount", rpc_params![]).await?)
self.pivx_rpc.call("getblockcount", rpc_params![]).await
}

/// Gets all raw transactions containing one of `address`
Expand All @@ -104,7 +96,7 @@ where
let mut txs = vec![];
for address in addresses {
for txid in self.address_index.get_address_txids(address).await? {
if let Some(tx) = self.get_transaction(&txid).await.ok() {
if let Ok(tx) = self.get_transaction(&txid).await {
txs.push(tx);
}
}
Expand Down Expand Up @@ -158,39 +150,8 @@ where
}

pub async fn send_transaction(&self, transaction: &str) -> crate::error::Result<String> {
Ok(self
.pivx_rpc
self.pivx_rpc
.call("sendrawtransaction", rpc_params![transaction])
.await?)
}
}

#[cfg(test)]
mod test {
use super::*;
use std::time::Duration;
#[tokio::test]
async fn temp_test() -> crate::error::Result<()> {
use crate::*;
use std::path::PathBuf;
let pivx_definition = PIVXDefinition;
let pivx = binary::Binary::new_by_fetching(&pivx_definition)
.await
.expect("Failed to run PIVX");
let pivx_rpc = PIVXRpc::new(&format!("http://127.0.0.1:{}", RPC_PORT))
.await
.unwrap();
let mut address_index = AddressIndex::new(
SqlLite::new(PathBuf::from("/home/duddino/test.sqlite"))
.await
.unwrap(),
pivx_rpc.clone(),
);
let explorer = Explorer::new(address_index, pivx_rpc);
tokio::time::sleep(Duration::from_secs(60)).await;
std::mem::forget(pivx);

println!("{}", explorer.get_block(123).await?);
Ok(())
}
}
4 changes: 1 addition & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

use std::path::PathBuf;

use address_index::{
block_file_source::BlockFileSource, pivx_rpc::PIVXRpc, sql_lite::SqlLite, AddressIndex,
};
use address_index::{block_file_source::BlockFileSource, sql_lite::SqlLite, AddressIndex};
use pivx::PIVXDefinition;

mod address_index;
Expand Down

0 comments on commit 744c2df

Please sign in to comment.