2020-07-09 23:51:01 -07:00
|
|
|
//! Checkpoint lists for checkpoint-based block verification
|
|
|
|
//!
|
|
|
|
//! Each checkpoint consists of a coinbase height and block header hash.
|
|
|
|
//!
|
|
|
|
//! Checkpoints can be used to verify their ancestors, by chaining backwards
|
|
|
|
//! to another checkpoint, via each block's parent block hash.
|
|
|
|
|
2020-07-29 04:12:53 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2020-10-12 14:08:23 -07:00
|
|
|
use crate::BoxError;
|
2020-07-15 02:51:54 -07:00
|
|
|
|
2020-07-10 00:46:27 -07:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, HashSet},
|
|
|
|
ops::RangeBounds,
|
2020-07-21 06:11:51 -07:00
|
|
|
str::FromStr,
|
2020-07-10 00:46:27 -07:00
|
|
|
};
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-08-15 23:20:01 -07:00
|
|
|
use zebra_chain::block;
|
2024-03-12 14:41:44 -07:00
|
|
|
use zebra_chain::parameters::Network;
|
2020-07-21 06:11:51 -07:00
|
|
|
|
2021-03-14 16:15:38 -07:00
|
|
|
/// The hard-coded checkpoints for mainnet, generated using the
|
|
|
|
/// `zebra-checkpoints` tool.
|
|
|
|
///
|
|
|
|
/// To regenerate the latest checkpoints, use the following commands:
|
|
|
|
/// ```sh
|
|
|
|
/// LAST_CHECKPOINT=$(tail -1 main-checkpoints.txt | cut -d' ' -f1)
|
|
|
|
/// echo "$LAST_CHECKPOINT"
|
|
|
|
/// zebra-checkpoints --cli /path/to/zcash-cli --last-checkpoint "$LAST_CHECKPOINT" >> main-checkpoints.txt &
|
|
|
|
/// tail -f main-checkpoints.txt
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// See the checkpoints [./README.md] for more details.
|
2020-07-21 06:11:51 -07:00
|
|
|
const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt");
|
2021-03-14 16:15:38 -07:00
|
|
|
|
|
|
|
/// The hard-coded checkpoints for testnet, generated using the
|
|
|
|
/// `zebra-checkpoints` tool.
|
|
|
|
///
|
|
|
|
/// To use testnet, use the testnet checkpoints file, and run
|
|
|
|
/// `zebra-checkpoints [other args] -- -testnet`.
|
|
|
|
///
|
|
|
|
/// See [`MAINNET_CHECKPOINTS`] for detailed `zebra-checkpoints` usage
|
|
|
|
/// information.
|
2020-07-21 06:11:51 -07:00
|
|
|
const TESTNET_CHECKPOINTS: &str = include_str!("test-checkpoints.txt");
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2024-03-12 14:41:44 -07:00
|
|
|
/// Network methods related to checkpoints
|
|
|
|
pub trait ParameterCheckpoint {
|
|
|
|
/// Returns the hash for the genesis block in `network`.
|
|
|
|
fn genesis_hash(&self) -> zebra_chain::block::Hash;
|
|
|
|
/// Returns the hard-coded checkpoint list for `network`.
|
|
|
|
fn checkpoint_list(&self) -> CheckpointList;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParameterCheckpoint for Network {
|
|
|
|
fn genesis_hash(&self) -> zebra_chain::block::Hash {
|
|
|
|
match self {
|
|
|
|
// zcash-cli getblockhash 0
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
Network::Mainnet => "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08"
|
|
|
|
.parse()
|
|
|
|
.expect("hard-coded hash parses"),
|
|
|
|
// See `zebra_chain::parameters::network::testnet` for more details.
|
|
|
|
Network::Testnet(params) => params.genesis_hash(),
|
2024-03-12 14:41:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn checkpoint_list(&self) -> CheckpointList {
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
let (checkpoints_for_network, should_fallback_to_genesis_hash_as_checkpoint) = match self {
|
|
|
|
Network::Mainnet => (MAINNET_CHECKPOINTS, false),
|
|
|
|
Network::Testnet(params) if params.is_default_testnet() => (TESTNET_CHECKPOINTS, false),
|
|
|
|
Network::Testnet(_params) => (TESTNET_CHECKPOINTS, true),
|
2024-03-12 14:41:44 -07:00
|
|
|
};
|
|
|
|
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
// Check that the list starts with the correct genesis block and parses checkpoint list.
|
|
|
|
let first_checkpoint_height = checkpoints_for_network
|
|
|
|
.lines()
|
|
|
|
.next()
|
|
|
|
.map(checkpoint_height_and_hash);
|
|
|
|
|
|
|
|
match first_checkpoint_height {
|
|
|
|
// parse calls CheckpointList::from_list
|
|
|
|
Some(Ok((block::Height(0), hash))) if hash == self.genesis_hash() => {
|
|
|
|
checkpoints_for_network
|
|
|
|
.parse()
|
|
|
|
.expect("hard-coded checkpoint list parses and validates")
|
|
|
|
}
|
|
|
|
_ if should_fallback_to_genesis_hash_as_checkpoint => {
|
|
|
|
CheckpointList::from_list([(block::Height(0), self.genesis_hash())])
|
|
|
|
.expect("hard-coded checkpoint list parses and validates")
|
2024-03-12 14:41:44 -07:00
|
|
|
}
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
Some(Ok((block::Height(0), _))) => {
|
|
|
|
panic!("the genesis checkpoint does not match the {self} genesis hash")
|
|
|
|
}
|
|
|
|
Some(Ok(_)) => panic!("checkpoints must start at the genesis block height 0"),
|
|
|
|
Some(Err(err)) => panic!("{err}"),
|
|
|
|
None => panic!(
|
|
|
|
"there must be at least one checkpoint on default networks, for the genesis block"
|
|
|
|
),
|
2024-03-12 14:41:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
/// Parses a checkpoint to a [`block::Height`] and [`block::Hash`].
|
|
|
|
fn checkpoint_height_and_hash(checkpoint: &str) -> Result<(block::Height, block::Hash), BoxError> {
|
|
|
|
let fields = checkpoint.split(' ').collect::<Vec<_>>();
|
|
|
|
if let [height, hash] = fields[..] {
|
|
|
|
Ok((height.parse()?, hash.parse()?))
|
|
|
|
} else {
|
|
|
|
Err(format!("Invalid checkpoint format: expected 2 space-separated fields but found {}: '{checkpoint}'", fields.len()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 05:10:36 -07:00
|
|
|
/// A list of block height and hash checkpoints.
|
2020-07-09 23:51:01 -07:00
|
|
|
///
|
|
|
|
/// Checkpoints should be chosen to avoid forks or chain reorganizations,
|
|
|
|
/// which only happen in the last few hundred blocks in the chain.
|
|
|
|
/// (zcashd allows chain reorganizations up to 99 blocks, and prunes
|
|
|
|
/// orphaned side-chains after 288 blocks.)
|
2020-07-22 05:10:36 -07:00
|
|
|
///
|
|
|
|
/// This is actually a bijective map, but since it is read-only, we use a
|
|
|
|
/// BTreeMap, and do the value uniqueness check on initialisation.
|
2020-07-29 03:51:26 -07:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
2022-02-17 18:00:24 -08:00
|
|
|
pub struct CheckpointList(BTreeMap<block::Height, block::Hash>);
|
2020-07-09 23:51:01 -07:00
|
|
|
|
2020-07-21 06:11:51 -07:00
|
|
|
impl FromStr for CheckpointList {
|
2020-09-21 11:54:06 -07:00
|
|
|
type Err = BoxError;
|
2020-07-21 06:11:51 -07:00
|
|
|
|
|
|
|
/// Parse a string into a CheckpointList.
|
|
|
|
///
|
2020-08-16 11:42:02 -07:00
|
|
|
/// Each line has one checkpoint, consisting of a `block::Height` and
|
2020-08-15 23:20:01 -07:00
|
|
|
/// `block::Hash`, separated by a single space.
|
2020-07-21 06:11:51 -07:00
|
|
|
///
|
|
|
|
/// Assumes that the provided genesis checkpoint is correct.
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2020-08-16 11:42:02 -07:00
|
|
|
let mut checkpoint_list: Vec<(block::Height, block::Hash)> = Vec::new();
|
2020-07-21 06:11:51 -07:00
|
|
|
|
|
|
|
for checkpoint in s.lines() {
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
checkpoint_list.push(checkpoint_height_and_hash(checkpoint)?);
|
2020-07-21 06:11:51 -07:00
|
|
|
}
|
|
|
|
|
2021-01-21 19:57:09 -08:00
|
|
|
CheckpointList::from_list(checkpoint_list)
|
2020-07-21 06:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:51:01 -07:00
|
|
|
impl CheckpointList {
|
2020-07-15 02:51:54 -07:00
|
|
|
/// Create a new checkpoint list for `network` from `checkpoint_list`.
|
2020-07-10 00:46:27 -07:00
|
|
|
///
|
2020-07-21 06:11:51 -07:00
|
|
|
/// Assumes that the provided genesis checkpoint is correct.
|
2020-07-10 00:46:27 -07:00
|
|
|
///
|
2020-07-21 06:11:51 -07:00
|
|
|
/// Checkpoint heights and checkpoint hashes must be unique.
|
2020-08-16 11:42:02 -07:00
|
|
|
/// There must be a checkpoint for a genesis block at block::Height 0.
|
2020-07-10 00:46:27 -07:00
|
|
|
/// (All other checkpoints are optional.)
|
2020-07-21 06:11:51 -07:00
|
|
|
pub(crate) fn from_list(
|
2020-08-16 11:42:02 -07:00
|
|
|
list: impl IntoIterator<Item = (block::Height, block::Hash)>,
|
2020-09-21 11:54:06 -07:00
|
|
|
) -> Result<Self, BoxError> {
|
2020-07-10 00:46:27 -07:00
|
|
|
// BTreeMap silently ignores duplicates, so we count the checkpoints
|
|
|
|
// before adding them to the map
|
2020-08-16 11:42:02 -07:00
|
|
|
let original_checkpoints: Vec<(block::Height, block::Hash)> = list.into_iter().collect();
|
2020-07-10 00:46:27 -07:00
|
|
|
let original_len = original_checkpoints.len();
|
|
|
|
|
2020-08-16 11:42:02 -07:00
|
|
|
let checkpoints: BTreeMap<block::Height, block::Hash> =
|
2020-07-10 00:46:27 -07:00
|
|
|
original_checkpoints.into_iter().collect();
|
2020-07-09 23:51:01 -07:00
|
|
|
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
// Check that the list starts with _some_ genesis block
|
2020-07-15 21:04:27 -07:00
|
|
|
match checkpoints.iter().next() {
|
add(consensus): Adds `disable_pow` and `genesis_hash` fields, converts `equihash::Solution` to an enum (#8421)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Adds genesis hash methods and fields to `testnet::Parameters` and its builder
* Copies Regtest genesis block from zcashd
* moves genesis hash check to Network.checkpoint_list()
* Checks the first line in the checkpoint list instead of the first item in the BTreeMap
* Checks that there is _some_ genesis block in the checkpoints list in `from_list()`, adds Regtest checkpoints (which only includes the genesis block)
* Adds a doc comment to `ParametersBuilder::default()`
* Adds a `disable_pow` field and skips checking proof of work if it's true
* Makes `equihash::Solution` an enum to support Regtest solutions, adds a test for validating and committing the Regtest genesis block
* use genesis_hash as first checkpoint when checkpoints are missing for configured testnets too
* Applies suggestions from code review.
* Reverts changes to `activation_height()` method
* Avoids panic in test
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-25 22:30:36 -07:00
|
|
|
Some((block::Height(0), _hash)) => {}
|
2020-07-15 21:04:27 -07:00
|
|
|
Some(_) => Err("checkpoints must start at the genesis block height 0")?,
|
2020-07-09 23:51:01 -07:00
|
|
|
None => Err("there must be at least one checkpoint, for the genesis block")?,
|
2020-07-15 02:51:54 -07:00
|
|
|
};
|
|
|
|
|
2020-07-10 00:46:27 -07:00
|
|
|
// This check rejects duplicate heights, whether they have the same or
|
|
|
|
// different hashes
|
|
|
|
if checkpoints.len() != original_len {
|
|
|
|
Err("checkpoint heights must be unique")?;
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:20:01 -07:00
|
|
|
let block_hashes: HashSet<&block::Hash> = checkpoints.values().collect();
|
2020-07-10 00:46:27 -07:00
|
|
|
if block_hashes.len() != original_len {
|
|
|
|
Err("checkpoint hashes must be unique")?;
|
|
|
|
}
|
|
|
|
|
2020-07-15 22:09:22 -07:00
|
|
|
// Make sure all the hashes are valid. In Bitcoin, [0; 32] is the null
|
|
|
|
// hash. It is also used as the parent hash of genesis blocks.
|
2020-08-15 23:20:01 -07:00
|
|
|
if block_hashes.contains(&block::Hash([0; 32])) {
|
2020-07-15 22:09:22 -07:00
|
|
|
Err("checkpoint list contains invalid checkpoint hash: found null hash")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let checkpoints = CheckpointList(checkpoints);
|
2020-08-16 11:42:02 -07:00
|
|
|
if checkpoints.max_height() > block::Height::MAX {
|
2020-07-15 22:09:22 -07:00
|
|
|
Err("checkpoint list contains invalid checkpoint: checkpoint height is greater than the maximum block height")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(checkpoints)
|
2020-07-09 23:51:01 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 10:51:51 -07:00
|
|
|
/// Return true if there is a checkpoint at `height`.
|
2020-07-09 23:51:01 -07:00
|
|
|
///
|
|
|
|
/// See `BTreeMap::contains_key()` for details.
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn contains(&self, height: block::Height) -> bool {
|
2020-07-09 23:51:01 -07:00
|
|
|
self.0.contains_key(&height)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the hash corresponding to the checkpoint at `height`,
|
|
|
|
/// or None if there is no checkpoint at that height.
|
|
|
|
///
|
|
|
|
/// See `BTreeMap::get()` for details.
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn hash(&self, height: block::Height) -> Option<block::Hash> {
|
2020-07-09 23:51:01 -07:00
|
|
|
self.0.get(&height).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the block height of the highest checkpoint in the checkpoint list.
|
|
|
|
///
|
|
|
|
/// If there is only a single checkpoint, then the maximum height will be
|
|
|
|
/// zero. (The genesis block.)
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn max_height(&self) -> block::Height {
|
2020-07-09 23:29:47 -07:00
|
|
|
self.max_height_in_range(..)
|
2020-07-09 23:51:01 -07:00
|
|
|
.expect("checkpoint lists must have at least one checkpoint")
|
|
|
|
}
|
|
|
|
|
2020-08-19 19:28:21 -07:00
|
|
|
/// Return the block height of the lowest checkpoint in a sub-range.
|
|
|
|
pub fn min_height_in_range<R>(&self, range: R) -> Option<block::Height>
|
|
|
|
where
|
|
|
|
R: RangeBounds<block::Height>,
|
|
|
|
{
|
|
|
|
self.0.range(range).map(|(height, _)| *height).next()
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:51:01 -07:00
|
|
|
/// Return the block height of the highest checkpoint in a sub-range.
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn max_height_in_range<R>(&self, range: R) -> Option<block::Height>
|
2020-07-09 23:51:01 -07:00
|
|
|
where
|
2020-08-16 11:42:02 -07:00
|
|
|
R: RangeBounds<block::Height>,
|
2020-07-09 23:51:01 -07:00
|
|
|
{
|
|
|
|
self.0.range(range).map(|(height, _)| *height).next_back()
|
|
|
|
}
|
2023-02-20 21:30:29 -08:00
|
|
|
|
|
|
|
/// Returns an iterator over all the checkpoints, in increasing height order.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&block::Height, &block::Hash)> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
/// Returns the checkpoint at `height`, as a zero-based index.
|
|
|
|
/// If `height` is not a checkpoint height, returns the checkpoint immediately before that height.
|
|
|
|
pub fn prev_checkpoint_index(&self, height: block::Height) -> usize {
|
|
|
|
self.0
|
|
|
|
.keys()
|
|
|
|
.rposition(|&key| key <= height)
|
|
|
|
.expect("checkpoints must start at the genesis block height 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of checkpoints in the list.
|
|
|
|
//
|
|
|
|
// Checkpoint lists are never empty by construction.
|
|
|
|
#[allow(clippy::len_without_is_empty)]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
2020-07-09 23:51:01 -07:00
|
|
|
}
|