zebra_state/constants.rs
1//! Constants that impact state behaviour.
2
3use lazy_static::lazy_static;
4use regex::Regex;
5use semver::Version;
6
7// For doc comment links
8#[allow(unused_imports)]
9use crate::{
10 config::{self, Config},
11 constants,
12};
13
14pub use zebra_chain::transparent::MIN_TRANSPARENT_COINBASE_MATURITY;
15
16/// The maximum chain reorganisation height.
17///
18/// This threshold determines the maximum length of the best non-finalized chain.
19/// Larger reorganisations would allow double-spends of coinbase transactions.
20///
21/// This threshold uses the relevant chain for the block being verified by the
22/// non-finalized state.
23///
24/// For the best chain, coinbase spends are only allowed from blocks at or below
25/// the finalized tip. For other chains, coinbase spends can use outputs from
26/// early non-finalized blocks, or finalized blocks. But if that chain becomes
27/// the best chain, all non-finalized blocks past the [`MAX_BLOCK_REORG_HEIGHT`]
28/// will be finalized. This includes all mature coinbase outputs.
29//
30// TODO: change to HeightDiff
31pub const MAX_BLOCK_REORG_HEIGHT: u32 = MIN_TRANSPARENT_COINBASE_MATURITY - 1;
32
33/// The directory name used to distinguish the state database from Zebra's other databases or flat files.
34pub const STATE_DATABASE_KIND: &str = "state";
35
36/// The database format major version, incremented each time the on-disk database format has a
37/// breaking data format change.
38///
39/// Breaking changes include:
40/// - deleting a column family, or
41/// - changing a column family's data format in an incompatible way.
42///
43/// Breaking changes become minor version changes if:
44/// - we previously added compatibility code, and
45/// - it's available in all supported Zebra versions.
46///
47/// Instead of using this constant directly, use [`constants::state_database_format_version_in_code()`]
48/// or [`config::database_format_version_on_disk()`] to get the full semantic format version.
49const DATABASE_FORMAT_VERSION: u64 = 27;
50
51/// The database format minor version, incremented each time the on-disk database format has a
52/// significant data format change.
53///
54/// Significant changes include:
55/// - adding new column families,
56/// - changing the format of a column family in a compatible way, or
57/// - breaking changes with compatibility code in all supported Zebra versions.
58const DATABASE_FORMAT_MINOR_VERSION: u64 = 0;
59
60/// The database format patch version, incremented each time the on-disk database format has a
61/// significant format compatibility fix.
62const DATABASE_FORMAT_PATCH_VERSION: u64 = 0;
63
64/// Returns the full semantic version of the currently running state database format code.
65///
66/// This is the version implemented by the Zebra code that's currently running,
67/// the version on disk can be different.
68pub fn state_database_format_version_in_code() -> Version {
69 Version {
70 major: DATABASE_FORMAT_VERSION,
71 minor: DATABASE_FORMAT_MINOR_VERSION,
72 patch: DATABASE_FORMAT_PATCH_VERSION,
73 pre: semver::Prerelease::EMPTY,
74 #[cfg(feature = "indexer")]
75 build: semver::BuildMetadata::new("indexer").expect("hard-coded value should be valid"),
76 #[cfg(not(feature = "indexer"))]
77 build: semver::BuildMetadata::EMPTY,
78 }
79}
80
81/// The name of the file containing the database version.
82///
83/// Note: This file has historically omitted the major database version.
84///
85/// Use [`Config::version_file_path()`] to get the path to this file.
86pub(crate) const DATABASE_FORMAT_VERSION_FILE_NAME: &str = "version";
87
88/// The maximum number of blocks to check for NU5 transactions,
89/// before we assume we are on a pre-NU5 legacy chain.
90///
91/// Zebra usually only has to check back a few blocks on mainnet, but on testnet it can be a long
92/// time between v5 transactions.
93pub const MAX_LEGACY_CHAIN_BLOCKS: usize = 100_000;
94
95/// The maximum number of non-finalized chain forks Zebra will track.
96/// When this limit is reached, we drop the chain with the lowest work.
97///
98/// When the network is under heavy transaction load, there are around 5 active forks in the last
99/// 100 blocks. (1 fork per 20 blocks.) When block propagation is efficient, there is around
100/// 1 fork per 300 blocks.
101///
102/// This limits non-finalized chain memory to around:
103/// `10 forks * 100 blocks * 2 MB per block = 2 GB`
104pub const MAX_NON_FINALIZED_CHAIN_FORKS: usize = 10;
105
106/// The maximum number of block hashes allowed in `getblocks` responses in the Zcash network protocol.
107pub const MAX_FIND_BLOCK_HASHES_RESULTS: u32 = 500;
108
109/// The maximum number of block headers allowed in `getheaders` responses in the Zcash network protocol.
110pub const MAX_FIND_BLOCK_HEADERS_RESULTS: u32 = 160;
111
112/// The maximum number of invalidated block records.
113///
114/// This limits the memory use to around:
115/// `100 entries * up to 99 blocks * 2 MB per block = 20 GB`
116pub const MAX_INVALIDATED_BLOCKS: usize = 100;
117
118lazy_static! {
119 /// Regex that matches the RocksDB error when its lock file is already open.
120 pub static ref LOCK_FILE_ERROR: Regex = Regex::new("(lock file).*(temporarily unavailable)|(in use)|(being used by another process)|(Database likely already open)").expect("regex is valid");
121}