Skip to content

Commit

Permalink
Merge pull request #16 from Duddino/sync
Browse files Browse the repository at this point in the history
Add sync
  • Loading branch information
Duddino authored Oct 7, 2024
2 parents 05b5e70 + 9f26671 commit 6c0223e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/address_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use database::Database;
use futures::StreamExt;
use types::Vin;

#[derive(Clone)]
pub struct AddressIndex<D: Database, B: BlockSource> {
database: D,
block_source: B,
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/address_index/sql_lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src-tauri/src/address_index/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vin>,

#[serde(deserialize_with = "concat_addresses")]
Expand All @@ -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,
}

Expand All @@ -45,6 +48,17 @@ where
Ok(addresses)
}

fn skip_invalid<'de, D>(deserializer: D) -> Result<Vec<Vin>, D::Error>
where
D: Deserializer<'de>,
{
let vins: Vec<Vin> = 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::*;
Expand Down Expand Up @@ -102,6 +116,10 @@ pub mod test {
{
"txid": "485",
"n": 0
},
{
"coinbase": "anoenar",
"sequence": 134134134
}],
"vout": [
{
Expand Down
25 changes: 20 additions & 5 deletions src-tauri/src/explorer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TxHexWithBlockCount = (String, u64, u64);
height: u64,
}*/

#[derive(Clone)]
pub struct Explorer<D, B>
where
D: Database,
Expand All @@ -33,8 +34,8 @@ type DefaultExplorer = Explorer<SqlLite, PIVXRpc>;

impl<D, B> Explorer<D, B>
where
D: Database + Send,
B: BlockSource + Send,
D: Database + Send + Clone,
B: BlockSource + Send + Clone,
{
fn new(address_index: AddressIndex<D, B>, rpc: PIVXRpc) -> Self {
Self {
Expand Down Expand Up @@ -64,16 +65,26 @@ 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
}

#[generate_global_functions]
impl<D, B> Explorer<D, B>
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<String> {
let block_hash: String = self
Expand Down Expand Up @@ -157,4 +168,8 @@ where
.call("sendrawtransaction", rpc_params![transaction])
.await
}

pub async fn sync(&self) -> crate::error::Result<()> {
self.address_index.clone().sync().await
}
}
3 changes: 2 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 6c0223e

Please sign in to comment.