1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Warp Synchronize

use crate::coinconfig::CoinConfig;
use crate::db::PlainNote;
use crate::scan::{AMProgressCallback, Progress};
use crate::{AccountData, BlockId, CompactTxStreamerClient, DbAdapter};
use std::sync::Arc;
use tokio::sync::Mutex;
use tonic::transport::Channel;
use tonic::Request;
use zcash_primitives::sapling::Note;
use crate::commitment::CTree;

const DEFAULT_CHUNK_SIZE: u32 = 100_000;

/// Asynchronously perform warp sync
/// # Arguments
/// * `coin`: 0 for zcash, 1 for ycash
/// * `get_tx`: true to retrieve transaction details
/// * `anchor_offset`: minimum number of confirmations for note selection
/// * `max_cost`: tx that have a higher spending cost are excluded
/// * `progress_callback`: function callback during synchronization
/// * `cancel`: cancellation mutex, set to true to abort
pub async fn coin_sync(
    coin: u8,
    get_tx: bool,
    anchor_offset: u32,
    max_cost: u32,
    progress_callback: impl Fn(Progress) + Send + 'static,
    cancel: &'static std::sync::Mutex<bool>,
) -> anyhow::Result<()> {
    let p_cb = Arc::new(Mutex::new(progress_callback));
    coin_sync_impl(
        coin,
        get_tx,
        DEFAULT_CHUNK_SIZE,
        anchor_offset,
        max_cost,
        p_cb.clone(),
        cancel,
    )
    .await?;
    coin_sync_impl(
        coin,
        get_tx,
        DEFAULT_CHUNK_SIZE,
        0,
        u32::MAX,
        p_cb.clone(),
        cancel,
    )
    .await?;
    Ok(())
}

async fn coin_sync_impl(
    coin: u8,
    get_tx: bool,
    chunk_size: u32,
    target_height_offset: u32,
    max_cost: u32,
    progress_callback: AMProgressCallback,
    cancel: &'static std::sync::Mutex<bool>,
) -> anyhow::Result<()> {
    let c = CoinConfig::get(coin);
    crate::scan::sync_async(
        c.coin_type,
        chunk_size,
        get_tx,
        c.db_path.as_ref().unwrap(),
        target_height_offset,
        max_cost,
        progress_callback,
        cancel,
        c.lwd_url.as_ref().unwrap(),
    )
    .await?;
    Ok(())
}

/// Return the latest block height
pub async fn get_latest_height() -> anyhow::Result<u32> {
    let c = CoinConfig::get_active();
    let mut client = c.connect_lwd().await?;
    let last_height = crate::chain::get_latest_height(&mut client).await?;
    Ok(last_height)
}

/// Return the latest block height synchronized
pub fn get_synced_height() -> anyhow::Result<u32> {
    let c = CoinConfig::get_active();
    let db = c.db()?;
    db.get_last_sync_height().map(|h| h.unwrap_or(0))
}

/// Skip block synchronization and directly mark the chain synchronized
/// Used for new accounts that have no transaction history
/// # Arguments
/// * `coin`: 0 for zcash, 1 for ycash
pub async fn skip_to_last_height(coin: u8) -> anyhow::Result<()> {
    let c = CoinConfig::get(coin);
    let mut client = c.connect_lwd().await?;
    let last_height = crate::chain::get_latest_height(&mut client).await?;
    fetch_and_store_tree_state(coin, &mut client, last_height).await?;
    Ok(())
}

/// Rewind to a previous block height
///
/// Height is snapped to a closest earlier checkpoint.
/// The effective height is returned
pub async fn rewind_to(height: u32) -> anyhow::Result<u32> {
    let c = CoinConfig::get_active();
    let height = c.db()?.trim_to_height(height)?;
    Ok(height)
}

/// Synchronize from a given 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>,
    height: u32,
) -> anyhow::Result<()> {
    let c = CoinConfig::get(coin);
    let block_id = BlockId {
        height: height as u64,
        hash: vec![],
    };
    let block = client.get_block(block_id.clone()).await?.into_inner();
    let tree_state = client
        .get_tree_state(Request::new(block_id))
        .await?
        .into_inner();
    let tree = CTree::read(&*hex::decode(&tree_state.sapling_tree)?)?;
    let db = c.db()?;
    DbAdapter::store_block(&db.connection, height, &block.hash, block.time, &tree)?;
    Ok(())
}

/// Return the date of sapling activation
pub async fn get_activation_date() -> anyhow::Result<u32> {
    let c = CoinConfig::get_active();
    let mut client = c.connect_lwd().await?;
    let date_time = crate::chain::get_activation_date(c.chain.network(), &mut client).await?;
    Ok(date_time)
}

/// Return the block height for a given timestamp
/// # Arguments
/// * `time`: seconds since epoch
pub async fn get_block_by_time(time: u32) -> anyhow::Result<u32> {
    let c = CoinConfig::get_active();
    let mut client = c.connect_lwd().await?;
    let date_time = crate::chain::get_block_by_time(c.chain.network(), &mut client, time).await?;
    Ok(date_time)
}

fn trial_decrypt(
    height: u32,
    cmu: &[u8],
    epk: &[u8],
    ciphertext: &[u8],
) -> anyhow::Result<Option<Note>> {
    let c = CoinConfig::get_active();
    let AccountData { fvk, .. } = c.db().unwrap().get_account_info(c.id_account)?;
    let note =
        crate::scan::trial_decrypt_one(c.chain.network(), height, &fvk, cmu, epk, ciphertext)?;
    Ok(note)
}