zebra_scan/config.rs
1//! Configuration for blockchain scanning tasks.
2
3use std::{fmt::Debug, net::SocketAddr};
4
5use indexmap::IndexMap;
6use serde::{Deserialize, Serialize};
7
8use zebra_state::Config as DbConfig;
9
10use crate::storage::SaplingScanningKey;
11
12#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
13#[serde(deny_unknown_fields, default)]
14/// Configuration for scanning.
15pub struct Config {
16 /// The sapling keys to scan for and the birthday height of each of them.
17 ///
18 /// Currently only supports Extended Full Viewing Keys in ZIP-32 format.
19 //
20 // TODO: allow keys without birthdays
21 pub sapling_keys_to_scan: IndexMap<SaplingScanningKey, u32>,
22
23 /// IP address and port for the zebra-scan gRPC server.
24 ///
25 /// Note: The gRPC server is disabled by default.
26 /// To enable the gRPC server, set a listen address in the config:
27 /// ```toml
28 /// [shielded-scan]
29 /// listen_addr = '127.0.0.1:8231'
30 /// ```
31 ///
32 /// The recommended ports for the gRPC server are:
33 /// - Mainnet: 127.0.0.1:8231
34 /// - Testnet: 127.0.0.1:18231
35 pub listen_addr: Option<SocketAddr>,
36
37 /// The scanner results database config.
38 //
39 // TODO: Remove fields that are only used by the state, and create a common database config.
40 #[serde(flatten)]
41 pub db_config: DbConfig,
42}
43
44impl Debug for Config {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 f.debug_struct("Config")
47 // Security: don't log private keys, birthday heights might also be private
48 .field("sapling_keys_to_scan", &self.sapling_keys_to_scan.len())
49 .field("db_config", &self.db_config)
50 .finish()
51 }
52}
53
54impl Default for Config {
55 fn default() -> Self {
56 Self {
57 sapling_keys_to_scan: IndexMap::new(),
58 listen_addr: None,
59
60 // TODO: Add a const generic for specifying the default cache_dir path, like 'zebra' or 'zebra-scan'?
61 db_config: DbConfig::default(),
62 }
63 }
64}
65
66impl Config {
67 /// Returns a config for a temporary database that is deleted when it is dropped.
68 pub fn ephemeral() -> Self {
69 Self {
70 db_config: DbConfig::ephemeral(),
71 ..Self::default()
72 }
73 }
74
75 /// Returns the database-specific config.
76 pub fn db_config(&self) -> &DbConfig {
77 &self.db_config
78 }
79
80 /// Returns the database-specific config as mutable.
81 pub fn db_config_mut(&mut self) -> &mut DbConfig {
82 &mut self.db_config
83 }
84}