Skip to content

Commit

Permalink
Refactor obtaining of activation heights
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed Feb 8, 2024
1 parent 1cfed24 commit c7a7e61
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
13 changes: 13 additions & 0 deletions zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ impl Network {
pub fn is_a_test_network(&self) -> bool {
*self != Network::Mainnet
}

/// Returns the minimum Sapling birthday height.
pub fn sapling_activation_height(self) -> Height {
// Assume that the genesis block never contains shielded inputs or outputs.
//
// # Consensus
//
// For Zcash mainnet and the public testnet, Sapling activates above genesis,
// so this is always true.
super::NetworkUpgrade::Sapling
.activation_height(self)
.expect("Sapling activation height needs to be set")
}
}

impl FromStr for Network {
Expand Down
2 changes: 1 addition & 1 deletion zebra-scan/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Service<Request> for ScanService {

return async move {
Ok(Response::Info {
min_sapling_birthday_height: db.min_sapling_birthday_height(),
min_sapling_birthday_height: db.network().sapling_activation_height(),
})
}
.boxed();
Expand Down
4 changes: 3 additions & 1 deletion zebra-scan/src/service/scan_task/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use color_eyre::{eyre::eyre, Report};
use tokio::sync::oneshot;

use zcash_primitives::{sapling::SaplingIvk, zip32::DiversifiableFullViewingKey};
use zebra_chain::{block::Height, transaction::Transaction};
use zebra_chain::{block::Height, parameters::Network, transaction::Transaction};
use zebra_state::SaplingScanningKey;

use super::ScanTask;
Expand Down Expand Up @@ -61,11 +61,13 @@ impl ScanTask {
SaplingScanningKey,
(Vec<DiversifiableFullViewingKey>, Vec<SaplingIvk>),
>,
network: Network,
) -> Result<
HashMap<SaplingScanningKey, (Vec<DiversifiableFullViewingKey>, Vec<SaplingIvk>, Height)>,
Report,
> {
let mut new_keys = HashMap::new();
let sapling_activation_height = network.sapling_activation_height();

loop {
let cmd = match cmd_receiver.try_recv() {
Expand Down
4 changes: 2 additions & 2 deletions zebra-scan/src/service/scan_task/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn start(
cmd_receiver: Receiver<ScanTaskCommand>,
) -> Result<(), Report> {
let network = storage.network();
let sapling_activation_height = storage.min_sapling_birthday_height();
let sapling_activation_height = network.sapling_activation_height();

// Do not scan and notify if we are below sapling activation height.
wait_for_height(
Expand Down Expand Up @@ -126,7 +126,7 @@ pub async fn start(
}
}

let new_keys = ScanTask::process_messages(&cmd_receiver, &mut parsed_keys)?;
let new_keys = ScanTask::process_messages(&cmd_receiver, &mut parsed_keys, network)?;

// TODO: Check if the `start_height` is at or above the current height
if !new_keys.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion zebra-scan/src/service/scan_task/scan/scan_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn scan_range(
state: State,
storage: Storage,
) -> Result<(), BoxError> {
let sapling_activation_height = storage.min_sapling_birthday_height();
let sapling_activation_height = storage.network().sapling_activation_height();
// Do not scan and notify if we are below sapling activation height.
wait_for_height(
sapling_activation_height,
Expand Down
20 changes: 1 addition & 19 deletions zebra-scan/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
use std::collections::{BTreeMap, HashMap};

use zebra_chain::{
block::Height,
parameters::{Network, NetworkUpgrade},
};
use zebra_chain::{block::Height, parameters::Network};
use zebra_state::TransactionIndex;

use crate::config::Config;
Expand Down Expand Up @@ -126,19 +123,4 @@ impl Storage {
) -> BTreeMap<Height, Vec<SaplingScannedResult>> {
self.sapling_results_for_key(sapling_key)
}

// Parameters

/// Returns the minimum sapling birthday height for the configured network.
pub fn min_sapling_birthday_height(&self) -> Height {
// Assume that the genesis block never contains shielded inputs or outputs.
//
// # Consensus
//
// For Zcash mainnet and the public testnet, Sapling activates above genesis,
// so this is always true.
NetworkUpgrade::Sapling
.activation_height(self.network())
.unwrap_or(Height(0))
}
}
2 changes: 1 addition & 1 deletion zebra-scan/src/storage/db/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Storage {
sapling_key: &SaplingScanningKey,
birthday_height: Option<Height>,
) {
let min_birthday_height = self.min_sapling_birthday_height();
let min_birthday_height = self.network().sapling_activation_height();

// The birthday height must be at least the minimum height for that pool.
let birthday_height = birthday_height
Expand Down

0 comments on commit c7a7e61

Please sign in to comment.