Separate rewind from rescan

This commit is contained in:
Hanh 2022-09-07 10:38:16 +08:00
parent c30f2bb7f3
commit e530ac1104
6 changed files with 35 additions and 27 deletions

View File

@ -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);

View File

@ -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)
}

View File

@ -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<u32> {
pub async fn rewind_to(height: u32) -> anyhow::Result<u32> {
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<Channel>,

View File

@ -441,7 +441,6 @@ impl DecryptNode {
blocks: Vec<CompactBlock>,
) -> Vec<DecryptedBlock> {
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);

View File

@ -207,21 +207,17 @@ impl DbAdapter {
Ok(fvks)
}
pub fn trim_to_height(&mut self, height: u32, exact: bool) -> anyhow::Result<u32> {
pub fn trim_to_height(&mut self, height: u32) -> anyhow::Result<u32> {
// 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<u32> = 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<u32> = 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)
}

View File

@ -153,7 +153,7 @@ pub async fn sync(offset: Option<u32>) -> Result<(), Error> {
#[post("/rewind?<height>")]
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(())
}