From 921be066923d8713fe0a5bbee046dfe93e308a38 Mon Sep 17 00:00:00 2001 From: Duddino Date: Mon, 7 Oct 2024 09:48:19 +0200 Subject: [PATCH 1/2] Add sync --- src-tauri/src/address_index/mod.rs | 1 + src-tauri/src/address_index/sql_lite.rs | 1 + src-tauri/src/address_index/types.rs | 14 +++++++++++++ src-tauri/src/explorer/mod.rs | 26 ++++++++++++++++++++----- src-tauri/src/main.rs | 3 ++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/address_index/mod.rs b/src-tauri/src/address_index/mod.rs index abbc447..04bcc0f 100644 --- a/src-tauri/src/address_index/mod.rs +++ b/src-tauri/src/address_index/mod.rs @@ -11,6 +11,7 @@ use database::Database; use futures::StreamExt; use types::Vin; +#[derive(Clone)] pub struct AddressIndex { database: D, block_source: B, diff --git a/src-tauri/src/address_index/sql_lite.rs b/src-tauri/src/address_index/sql_lite.rs index e77dad2..4c218d9 100644 --- a/src-tauri/src/address_index/sql_lite.rs +++ b/src-tauri/src/address_index/sql_lite.rs @@ -4,6 +4,7 @@ use super::database::Database; use super::types::{Tx, Vin}; use rusqlite::{params, Connection}; +#[derive(Clone, Debug)] pub struct SqlLite { path: PathBuf, //connection: Connection, diff --git a/src-tauri/src/address_index/types.rs b/src-tauri/src/address_index/types.rs index 58aca1f..44c665c 100644 --- a/src-tauri/src/address_index/types.rs +++ b/src-tauri/src/address_index/types.rs @@ -8,6 +8,7 @@ pub struct Block { #[derive(Deserialize, Debug, Clone)] pub struct Tx { pub txid: String, + #[serde(deserialize_with = "skip_invalid")] pub vin: Vec, #[serde(deserialize_with = "concat_addresses")] @@ -27,7 +28,9 @@ struct ScriptPubKey { #[derive(Deserialize, Debug, Clone, Eq, Hash, PartialEq)] pub struct Vin { + #[serde(default)] pub txid: String, + #[serde(default)] pub n: u32, } @@ -45,6 +48,13 @@ where Ok(addresses) } +fn skip_invalid<'de, D>(deserializer: D) -> Result, D::Error> + where D: Deserializer<'de> +{ + let vins: Vec = Vec::deserialize(deserializer)?; + Ok(vins.into_iter().filter_map(|e|if e.txid.is_empty() {None}else { Some(e) }).collect()) +} + #[cfg(test)] pub mod test { use super::*; @@ -102,6 +112,10 @@ pub mod test { { "txid": "485", "n": 0 + }, + { + "coinbase": "anoenar", + "sequence": 134134134 }], "vout": [ { diff --git a/src-tauri/src/explorer/mod.rs b/src-tauri/src/explorer/mod.rs index ffbf7c4..a81b387 100644 --- a/src-tauri/src/explorer/mod.rs +++ b/src-tauri/src/explorer/mod.rs @@ -20,6 +20,7 @@ type TxHexWithBlockCount = (String, u64, u64); height: u64, }*/ +#[derive(Clone)] pub struct Explorer where D: Database, @@ -33,8 +34,8 @@ type DefaultExplorer = Explorer; impl Explorer where - D: Database + Send, - B: BlockSource + Send, + D: Database + Send + Clone, + B: BlockSource + Send + Clone, { fn new(address_index: AddressIndex, rpc: PIVXRpc) -> Self { Self { @@ -42,6 +43,7 @@ where pivx_rpc: rpc, } } + } static EXPLORER: OnceCell = OnceCell::const_new(); @@ -64,7 +66,17 @@ async fn get_explorer() -> &'static DefaultExplorer { pivx_rpc.clone(), ); std::mem::forget(pivx); - Explorer::new(address_index, pivx_rpc) + + let explorer = Explorer::new(address_index, pivx_rpc); + // Cloning is very cheap, it's just a Pathbuf and some Arcs + let explorer_clone = explorer.clone(); + tokio::spawn(async move { + if let Err(err) = explorer_clone.sync().await { + eprintln!("Warning: Syncing failed with error {}", err); + } + }); + + explorer }) .await } @@ -72,8 +84,8 @@ async fn get_explorer() -> &'static DefaultExplorer { #[generate_global_functions] impl Explorer where - D: Database + Send, - B: BlockSource + Send, + D: Database + Send + Clone, + B: BlockSource + Send + Clone, { pub async fn get_block(&self, block_height: u64) -> crate::error::Result { let block_hash: String = self @@ -157,4 +169,8 @@ where .call("sendrawtransaction", rpc_params![transaction]) .await } + + pub async fn sync(&self) -> crate::error::Result<()> { + self.address_index.clone().sync().await + } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index bf94b9f..4f3e8fa 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -51,7 +51,8 @@ fn main() { explorer_get_txs, explorer_get_transaction, explorer_send_transaction, - explorer_get_tx_from_vin + explorer_get_tx_from_vin, + explorer_sync, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); From 9f26671f22df4bb9faee006306a88d03faffd765 Mon Sep 17 00:00:00 2001 From: Duddino Date: Mon, 7 Oct 2024 09:57:13 +0200 Subject: [PATCH 2/2] Run cargo fmt --- src-tauri/src/address_index/types.rs | 8 ++++++-- src-tauri/src/explorer/mod.rs | 23 +++++++++++------------ src-tauri/src/main.rs | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/address_index/types.rs b/src-tauri/src/address_index/types.rs index 44c665c..b7c2fcc 100644 --- a/src-tauri/src/address_index/types.rs +++ b/src-tauri/src/address_index/types.rs @@ -49,10 +49,14 @@ where } fn skip_invalid<'de, D>(deserializer: D) -> Result, D::Error> - where D: Deserializer<'de> +where + D: Deserializer<'de>, { let vins: Vec = Vec::deserialize(deserializer)?; - Ok(vins.into_iter().filter_map(|e|if e.txid.is_empty() {None}else { Some(e) }).collect()) + Ok(vins + .into_iter() + .filter_map(|e| if e.txid.is_empty() { None } else { Some(e) }) + .collect()) } #[cfg(test)] diff --git a/src-tauri/src/explorer/mod.rs b/src-tauri/src/explorer/mod.rs index a81b387..527438a 100644 --- a/src-tauri/src/explorer/mod.rs +++ b/src-tauri/src/explorer/mod.rs @@ -43,7 +43,6 @@ where pivx_rpc: rpc, } } - } static EXPLORER: OnceCell = OnceCell::const_new(); @@ -66,17 +65,17 @@ async fn get_explorer() -> &'static DefaultExplorer { pivx_rpc.clone(), ); std::mem::forget(pivx); - + let explorer = Explorer::new(address_index, pivx_rpc); - // Cloning is very cheap, it's just a Pathbuf and some Arcs - let explorer_clone = explorer.clone(); - tokio::spawn(async move { - if let Err(err) = explorer_clone.sync().await { - eprintln!("Warning: Syncing failed with error {}", err); - } - }); - - explorer + // Cloning is very cheap, it's just a Pathbuf and some Arcs + let explorer_clone = explorer.clone(); + tokio::spawn(async move { + if let Err(err) = explorer_clone.sync().await { + eprintln!("Warning: Syncing failed with error {}", err); + } + }); + + explorer }) .await } @@ -171,6 +170,6 @@ where } pub async fn sync(&self) -> crate::error::Result<()> { - self.address_index.clone().sync().await + self.address_index.clone().sync().await } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 4f3e8fa..3876a95 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -52,7 +52,7 @@ fn main() { explorer_get_transaction, explorer_send_transaction, explorer_get_tx_from_vin, - explorer_sync, + explorer_sync, ]) .run(tauri::generate_context!()) .expect("error while running tauri application");