diff --git a/binding.h b/binding.h index c524883..d09c436 100644 --- a/binding.h +++ b/binding.h @@ -64,7 +64,9 @@ char *send_multi_payment(char *recipients_json, void skip_to_last_height(uint8_t coin); -uint32_t rewind_to_height(uint32_t height, bool exact); +uint32_t rewind_to(uint32_t height); + +void rescan_from(uint32_t height); int64_t mempool_sync(void); diff --git a/src/api/dart_ffi.rs b/src/api/dart_ffi.rs index 1ab7c56..b3102aa 100644 --- a/src/api/dart_ffi.rs +++ b/src/api/dart_ffi.rs @@ -328,8 +328,15 @@ pub async unsafe extern "C" fn skip_to_last_height(coin: u8) { #[tokio::main] #[no_mangle] -pub async unsafe extern "C" fn rewind_to_height(height: u32, exact: bool) -> u32 { - let res = crate::api::sync::rewind_to_height(height, exact).await; +pub async unsafe extern "C" fn rewind_to(height: u32) -> u32 { + let res = crate::api::sync::rewind_to(height).await; + log_result(res) +} + +#[tokio::main] +#[no_mangle] +pub async unsafe extern "C" fn rescan_from(height: u32) { + let res = crate::api::sync::rescan_from(height).await; log_result(res) } diff --git a/src/api/sync.rs b/src/api/sync.rs index 824d076..460b198 100644 --- a/src/api/sync.rs +++ b/src/api/sync.rs @@ -94,16 +94,20 @@ pub async fn skip_to_last_height(coin: u8) -> anyhow::Result<()> { // and get the tree_state from the server // this option is used when we rescan from a given height // We ignore any transaction that occurred before -pub async fn rewind_to_height(height: u32, exact: bool) -> anyhow::Result { +pub async fn rewind_to(height: u32) -> anyhow::Result { let c = CoinConfig::get_active(); - let height = c.db()?.trim_to_height(height, exact)?; - if exact { - let mut client = c.connect_lwd().await?; - fetch_and_store_tree_state(c.coin, &mut client, height).await?; - } + let height = c.db()?.trim_to_height(height)?; Ok(height) } +pub async fn rescan_from(height: u32) -> anyhow::Result<()> { + let c = CoinConfig::get_active(); + c.db()?.truncate_sync_data()?; + let mut client = c.connect_lwd().await?; + fetch_and_store_tree_state(c.coin, &mut client, height).await?; + Ok(()) +} + async fn fetch_and_store_tree_state( coin: u8, client: &mut CompactTxStreamerClient, diff --git a/src/chain.rs b/src/chain.rs index 866f3d9..0e28eec 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -441,7 +441,6 @@ impl DecryptNode { blocks: Vec, ) -> Vec { let use_gpu = { *USE_GPU.lock().unwrap() }; - log::info!("use gpu = {}", use_gpu); if use_gpu { #[cfg(feature = "cuda")] return self.cuda_decrypt_blocks(network, blocks); diff --git a/src/db.rs b/src/db.rs index 4df0e72..f6ebaae 100644 --- a/src/db.rs +++ b/src/db.rs @@ -207,21 +207,17 @@ impl DbAdapter { Ok(fvks) } - pub fn trim_to_height(&mut self, height: u32, exact: bool) -> anyhow::Result { + pub fn trim_to_height(&mut self, height: u32) -> anyhow::Result { // snap height to an existing checkpoint - let height = if exact { - height - } else { - let height = self.connection.query_row( - "SELECT MAX(height) from blocks WHERE height <= ?1", - params![height], - |row| { - let height: Option = row.get(0)?; - Ok(height) - }, - )?; - height.unwrap_or(0) - }; + let height = self.connection.query_row( + "SELECT MAX(height) from blocks WHERE height <= ?1", + params![height], + |row| { + let height: Option = row.get(0)?; + Ok(height) + }, + )?; + let height = height.unwrap_or(0); log::info!("Rewind to height: {}", height); let tx = self.connection.transaction()?; @@ -825,7 +821,7 @@ impl DbAdapter { pub fn delete_incomplete_scan(&mut self) -> anyhow::Result<()> { let synced_height = self.get_last_sync_height()?; if let Some(synced_height) = synced_height { - self.trim_to_height(synced_height, true)?; + self.trim_to_height(synced_height)?; } Ok(()) } @@ -1060,7 +1056,7 @@ impl DbAdapter { } self.connection.execute("INSERT INTO blocks(height,hash,timestamp,sapling_tree) VALUES (?1,?2,?3,?4) ON CONFLICT(height) DO NOTHING", params![account_info.height, hex::decode(&account_info.hash)?, account_info.timestamp, hex::decode(&account_info.sapling_tree)?])?; - self.trim_to_height(account_info.height, true)?; + self.trim_to_height(account_info.height)?; Ok(ids) } diff --git a/src/main/rpc.rs b/src/main/rpc.rs index fccf72a..93ee7ed 100644 --- a/src/main/rpc.rs +++ b/src/main/rpc.rs @@ -153,7 +153,7 @@ pub async fn sync(offset: Option) -> Result<(), Error> { #[post("/rewind?")] pub async fn rewind(height: u32) -> Result<(), Error> { - warp_api_ffi::api::sync::rewind_to_height(height).await?; + warp_api_ffi::api::sync::rewind_to(height).await?; Ok(()) }