Skip to content

Commit 4dc0ccd

Browse files
committed
add options to sync state method returns to fix bugs with API use cases and add saving to new address generation on the condtion sync is not running
1 parent eda6d8c commit 4dc0ccd

File tree

11 files changed

+69
-48
lines changed

11 files changed

+69
-48
lines changed

libtonode-tests/tests/concrete.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ mod fast {
10061006
let seed_phrase = Mnemonic::<bip0039::English>::from_entropy([1; 32])
10071007
.unwrap()
10081008
.to_string();
1009-
let recipient1 = client_builder
1009+
let mut recipient1 = client_builder
10101010
.build_client(seed_phrase, 0, false, regtest_network)
10111011
.await;
10121012
let base_transparent_receiver = "tmS9nbexug7uT8x1cMTLP1ABEyKXpMjR5F1";
@@ -2232,11 +2232,14 @@ mod slow {
22322232
let recipient_to_faucet_amount = 5_000u64;
22332233
// check start state
22342234
faucet.sync_and_await().await.unwrap();
2235-
let wallet_height = faucet.do_wallet_last_scanned_height().await;
2236-
assert_eq!(
2237-
wallet_height.as_fixed_point_u64(0).unwrap(),
2238-
BASE_HEIGHT as u64
2239-
);
2235+
let wallet_fully_scanned_height = faucet
2236+
.wallet
2237+
.lock()
2238+
.await
2239+
.sync_state
2240+
.fully_scanned_height()
2241+
.unwrap();
2242+
assert_eq!(wallet_fully_scanned_height, BASE_HEIGHT.into());
22402243
let three_blocks_reward = block_rewards::CANOPY
22412244
.checked_mul(BASE_HEIGHT as u64)
22422245
.unwrap();
@@ -2629,7 +2632,8 @@ mod slow {
26292632
.lock()
26302633
.await
26312634
.sync_state
2632-
.fully_scanned_height(),
2635+
.fully_scanned_height()
2636+
.unwrap(),
26332637
4.into()
26342638
);
26352639

libtonode-tests/tests/wallet.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,16 @@ mod load_wallet {
191191

192192
// Without sync push server forward 2 blocks
193193
zingolib::testutils::increase_server_height(&regtest_manager, 2).await;
194-
let client_wallet_height = faucet.do_wallet_last_scanned_height().await;
194+
let client_fully_scanned_height = faucet
195+
.wallet
196+
.lock()
197+
.await
198+
.sync_state
199+
.fully_scanned_height()
200+
.unwrap();
195201

196202
// Verify that wallet is still back at 6.
197-
assert_eq!(client_wallet_height.as_fixed_point_u64(0).unwrap(), 8);
203+
assert_eq!(client_fully_scanned_height, 8.into());
198204

199205
// Interrupt generating send
200206
from_inputs::quick_send(

pepper-sync/src/sync.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ where
174174
.get_sync_state()
175175
.unwrap()
176176
.highest_scanned_height()
177+
.expect("scan ranges must be non-empty")
177178
+ 1;
178179
let mut interval = tokio::time::interval(Duration::from_millis(50));
179180
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
@@ -607,8 +608,12 @@ where
607608
W: SyncWallet + SyncBlocks + SyncNullifiers + SyncTransactions,
608609
{
609610
let sync_state = wallet.get_sync_state().unwrap();
610-
let fully_scanned_height = sync_state.fully_scanned_height();
611-
let highest_scanned_height = sync_state.highest_scanned_height();
611+
let fully_scanned_height = sync_state
612+
.fully_scanned_height()
613+
.expect("scan ranges must be non-empty");
614+
let highest_scanned_height = sync_state
615+
.highest_scanned_height()
616+
.expect("scan ranges must be non-empty");
612617
let sync_start_height = sync_state.initial_sync_state.sync_start_height;
613618

614619
let scanned_block_range_bounds = sync_state

pepper-sync/src/sync/state.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ pub(super) async fn update_scan_ranges(
9494
)?;
9595
set_chain_tip_scan_range(consensus_parameters, sync_state, chain_height)?;
9696

97-
let verification_height = sync_state.highest_scanned_height() + 1;
97+
let verification_height = sync_state
98+
.highest_scanned_height()
99+
.expect("scan ranges must be non-empty")
100+
+ 1;
98101
if verification_height <= chain_height {
99102
set_verify_scan_range(sync_state, verification_height, VerifyEnd::VerifyLowest);
100103
}
@@ -111,7 +114,10 @@ fn merge_scanned_ranges(sync_state: &mut SyncState) {
111114
.enumerate()
112115
.filter(|(_, scan_range)| {
113116
scan_range.priority() == ScanPriority::Scanned
114-
&& scan_range.block_range().end - 1 <= sync_state.fully_scanned_height()
117+
&& scan_range.block_range().end - 1
118+
<= sync_state
119+
.fully_scanned_height()
120+
.expect("scan ranges must be non-empty")
115121
})
116122
.collect::<Vec<_>>();
117123

@@ -649,7 +655,11 @@ pub(super) async fn set_initial_state<W>(
649655
) where
650656
W: SyncWallet + SyncBlocks,
651657
{
652-
let fully_scanned_height = wallet.get_sync_state().unwrap().fully_scanned_height();
658+
let fully_scanned_height = wallet
659+
.get_sync_state()
660+
.unwrap()
661+
.fully_scanned_height()
662+
.expect("scan ranges must be non-empty");
653663
let (sync_start_sapling_tree_size, sync_start_orchard_tree_size) = final_tree_sizes(
654664
consensus_parameters,
655665
fetch_request_sender.clone(),

pepper-sync/src/wallet.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,41 +137,34 @@ impl SyncState {
137137
}
138138

139139
/// Returns the block height at which all blocks equal to and below this height are scanned.
140-
///
141-
/// Will panic if called before scan ranges are updated for the first time.
142-
pub fn fully_scanned_height(&self) -> BlockHeight {
140+
/// Returns `None` if `self.scan_ranges` is empty.
141+
pub fn fully_scanned_height(&self) -> Option<BlockHeight> {
143142
if let Some(scan_range) = self
144143
.scan_ranges
145144
.iter()
146145
.find(|scan_range| scan_range.priority() != ScanPriority::Scanned)
147146
{
148-
scan_range.block_range().start - 1
147+
Some(scan_range.block_range().start - 1)
149148
} else {
150149
self.scan_ranges
151150
.last()
152-
.expect("scan ranges always non-empty")
153-
.block_range()
154-
.end
155-
- 1
151+
.map(|range| range.block_range().end - 1)
156152
}
157153
}
158154

159155
/// Returns the highest block height that has been scanned.
160-
///
161156
/// If no scan ranges have been scanned, returns the block below the wallet birthday.
162-
/// Will panic if called before scan ranges are updated for the first time.
163-
pub fn highest_scanned_height(&self) -> BlockHeight {
157+
/// Returns `None` if `self.scan_ranges` is empty.
158+
pub fn highest_scanned_height(&self) -> Option<BlockHeight> {
164159
if let Some(last_scanned_range) = self
165160
.scan_ranges
166161
.iter()
167162
.filter(|scan_range| scan_range.priority() == ScanPriority::Scanned)
168163
.last()
169164
{
170-
last_scanned_range.block_range().end - 1
165+
Some(last_scanned_range.block_range().end - 1)
171166
} else {
172-
self.wallet_birthday()
173-
.expect("scan ranges always non-empty")
174-
- 1
167+
self.wallet_birthday().map(|birthday| birthday - 1)
175168
}
176169
}
177170

pepper-sync/src/wallet/serialization.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ impl NullifierMap {
192192
/// Deserialize into `reader`
193193
pub fn read<R: Read>(mut reader: R) -> std::io::Result<Self> {
194194
let _version = reader.read_u8()?;
195-
196195
let sapling = Vector::read(&mut reader, |mut r| {
197196
let mut nullifier_bytes = [0u8; 32];
198197
r.read_exact(&mut nullifier_bytes)?;

zingolib/src/commands.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,22 +1586,21 @@ struct HeightCommand {}
15861586
impl Command for HeightCommand {
15871587
fn help(&self) -> &'static str {
15881588
indoc! {r#"
1589-
Get the latest block height that the wallet is at.
1589+
Returns the blockchain height at the time the wallet last requested the latest block height from the server.
1590+
15901591
Usage:
15911592
height
15921593
1593-
Pass 'true' (default) to sync to the server to get the latest block height. Pass 'false' to get the latest height in the wallet without checking with the server.
1594-
15951594
"#}
15961595
}
15971596

15981597
fn short_help(&self) -> &'static str {
1599-
"Get the latest block height that the wallet is at"
1598+
"Returns the blockchain height at the time the wallet last requested the latest block height from the server."
16001599
}
16011600

16021601
fn exec(&self, _args: &[&str], lightclient: &mut LightClient) -> String {
16031602
RT.block_on(async move {
1604-
object! { "height" => lightclient.do_wallet_last_scanned_height().await}.pretty(2)
1603+
object! { "height" => json::JsonValue::from(lightclient.wallet.lock().await.sync_state.wallet_height().map(u32::from).unwrap_or(0))}.pretty(2)
16051604
})
16061605
}
16071606
}

zingolib/src/lightclient.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use json::{array, object, JsonValue};
44
use log::error;
5-
use pepper_sync::{error::SyncError, wallet::SyncResult};
5+
use pepper_sync::{
6+
error::SyncError,
7+
wallet::{SyncMode, SyncResult},
8+
};
69
use serde::Serialize;
710
use std::sync::{atomic::AtomicU8, Arc};
811
use tokio::{
@@ -465,7 +468,7 @@ impl LightClient {
465468
}
466469

467470
/// Generates a new unified address from the given `addr_type`.
468-
pub async fn do_new_address(&self, addr_type: &str) -> Result<JsonValue, String> {
471+
pub async fn do_new_address(&mut self, addr_type: &str) -> Result<JsonValue, String> {
469472
//TODO: Placeholder interface
470473
let desired_receivers = ReceiverSelection {
471474
sapling: addr_type.contains('z'),
@@ -480,7 +483,9 @@ impl LightClient {
480483
.generate_unified_address(desired_receivers)
481484
.map_err(|e| e.to_string())?;
482485

483-
// self.save_internal_rust().await?;
486+
if SyncMode::from_atomic_u8(self.sync_mode.clone()) == SyncMode::NotRunning {
487+
self.save_internal_rust().await?;
488+
}
484489

485490
Ok(array![new_address.encode(&self.config.chain)])
486491
}

zingolib/src/lightclient/describe.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,6 @@ impl LightClient {
216216
finsight::TotalValueToAddress(by_address_total)
217217
}
218218

219-
/// TODO: Add Doc Comment Here!
220-
// TODO: remove
221-
pub async fn do_wallet_last_scanned_height(&self) -> JsonValue {
222-
json::JsonValue::from(u32::from(
223-
self.wallet.lock().await.sync_state.fully_scanned_height(),
224-
))
225-
}
226-
227219
/// TODO: Add Doc Comment Here!
228220
pub fn get_server(&self) -> std::sync::RwLockReadGuard<http::Uri> {
229221
self.config.lightwalletd_uri.read().unwrap()

zingolib/src/testutils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,15 @@ pub async fn sync_to_target_height(
277277
) -> Result<(), LightClientError> {
278278
// sync first so ranges exist for the `fully_scanned_height` call
279279
client.sync_and_await().await?;
280-
while u32::from(client.wallet.lock().await.sync_state.fully_scanned_height())
281-
< target_block_height
280+
while u32::from(
281+
client
282+
.wallet
283+
.lock()
284+
.await
285+
.sync_state
286+
.fully_scanned_height()
287+
.unwrap(),
288+
) < target_block_height
282289
{
283290
tokio::time::sleep(Duration::from_millis(500)).await;
284291
client.sync_and_await().await?;

0 commit comments

Comments
 (0)