Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing sources #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 7 additions & 0 deletions src-tauri/src/address_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ where
pub async fn get_txid_from_vin(&self, vin: &Vin) -> crate::error::Result<Option<String>> {
self.database.get_txid_from_vin(vin).await
}

pub fn set_block_source<T>(&mut self, block_source: T)
where
T: BlockSource + Send + Sync + 'static,
{
self.block_source = block_source.instantiate();
}
}

#[cfg(test)]
Expand Down
61 changes: 51 additions & 10 deletions src-tauri/src/explorer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::address_index::block_file_source::BlockFileSource;
use crate::error::PIVXErrors;
use jsonrpsee::rpc_params;
use serde::Deserialize;
use std::path::PathBuf;
use tokio::sync::OnceCell;
use std::sync::Arc;
use tokio::sync::{OnceCell, RwLock};

use crate::address_index::{
database::Database, pivx_rpc::PIVXRpc, sql_lite::SqlLite, types::Vin, AddressIndex,
Expand All @@ -18,7 +20,7 @@ pub struct Explorer<D>
where
D: Database,
{
address_index: AddressIndex<D>,
address_index: Arc<RwLock<AddressIndex<D>>>,
pivx_rpc: PIVXRpc,
}

Expand All @@ -30,25 +32,34 @@ where
{
fn new(address_index: AddressIndex<D>, rpc: PIVXRpc) -> Self {
Self {
address_index,
address_index: Arc::new(RwLock::new(address_index)),
pivx_rpc: rpc,
}
}
}

static EXPLORER: OnceCell<DefaultExplorer> = OnceCell::const_new();
static PIVX_RPC: OnceCell<PIVXRpc> = OnceCell::const_new();

async fn get_explorer() -> &'static DefaultExplorer {
EXPLORER
async fn get_pivx_rpc() -> &'static PIVXRpc {
PIVX_RPC
.get_or_init(|| async {
let pivx_definition = PIVXDefinition;
let mut pivx = Binary::new_by_fetching(&pivx_definition)
.await
.expect("Failed to run PIVX");
pivx.wait_for_load(&pivx_definition).await.unwrap();
let pivx_rpc = PIVXRpc::new(&format!("http://127.0.0.1:{}", RPC_PORT), pivx)
PIVXRpc::new(&format!("http://127.0.0.1:{}", RPC_PORT), pivx)
.await
.unwrap();
.unwrap()
})
.await
}

async fn get_explorer() -> &'static DefaultExplorer {
EXPLORER
.get_or_init(|| async {
let pivx_rpc = get_pivx_rpc().await;
// FIXME: refactor this to accept HOME
let address_index = AddressIndex::new(
SqlLite::new(PathBuf::from("/home/duddino/test.sqlite"))
Expand Down Expand Up @@ -99,7 +110,13 @@ where
) -> crate::error::Result<Vec<TxHexWithBlockCount>> {
let mut txs = vec![];
for address in addresses {
for txid in self.address_index.get_address_txids(address).await? {
for txid in self
.address_index
.read()
.await
.get_address_txids(address)
.await?
{
if let Ok(tx) = self.get_transaction(&txid).await {
txs.push(tx);
}
Expand All @@ -112,7 +129,12 @@ where
&self,
vin: Vin,
) -> crate::error::Result<Option<TxHexWithBlockCount>> {
let txid = self.address_index.get_txid_from_vin(&vin).await?;
let txid = self
.address_index
.read()
.await
.get_txid_from_vin(&vin)
.await?;
if let Some(txid) = txid {
Ok(self.get_transaction(&txid).await.ok())
} else {
Expand Down Expand Up @@ -160,6 +182,25 @@ where
}

pub async fn sync(&self) -> crate::error::Result<()> {
self.address_index.clone().sync().await
self.address_index.write().await.sync().await
}

pub async fn switch_to_rpc_source(&self) -> crate::error::Result<()> {
self.address_index
.write()
.await
.set_block_source(get_pivx_rpc().await.clone());
Ok(())
}

pub async fn switch_to_blockfile_source(&self) -> crate::error::Result<()> {
// FIXME: Actually use a real path
let block_file_source =
BlockFileSource::new("/home/duddino/.local/share/pivx-rust/.pivx/blocks");
self.address_index
.write()
.await
.set_block_source(block_file_source);
Ok(())
}
}
2 changes: 2 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fn main() {
explorer_send_transaction,
explorer_get_tx_from_vin,
explorer_sync,
explorer_switch_to_rpc_source,
explorer_switch_to_blockfile_source,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
Loading