zebra/zebra-chain/src/work/equihash.rs

197 lines
6.9 KiB
Rust
Raw Normal View History

//! Equihash Solution and related items.
use std::{fmt, io};
use serde_big_array::BigArray;
use crate::{
block::Header,
serialization::{
zcash_serialize_bytes, SerializationError, ZcashDeserialize, ZcashDeserializeInto,
ZcashSerialize,
},
};
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
#[cfg(feature = "internal-miner")]
use crate::serialization::AtLeastOne;
/// The error type for Equihash validation.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
#[error("invalid equihash solution for BlockHeader")]
pub struct Error(#[from] equihash::Error);
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
/// The error type for Equihash solving.
#[derive(Copy, Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error("solver was cancelled")]
pub struct SolverCancelled;
/// The size of an Equihash solution in bytes (always 1344).
pub(crate) const SOLUTION_SIZE: usize = 1344;
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
/// The size of an Equihash solution in bytes on Regtest (always 36).
pub(crate) const REGTEST_SOLUTION_SIZE: usize = 36;
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
/// Equihash Solution in compressed format.
///
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
/// A wrapper around `[u8; n]` where `n` is the solution size because
/// Rust doesn't implement common traits like `Debug`, `Clone`, etc.
/// for collections like arrays beyond lengths 0 to 32.
///
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
/// The size of an Equihash solution in bytes is always 1344 on Mainnet and Testnet, and
/// is always 36 on Regtest so the length of this type is fixed.
#[derive(Deserialize, Serialize)]
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
// It's okay to use the extra space on Regtest
#[allow(clippy::large_enum_variant)]
pub enum Solution {
/// Equihash solution on Mainnet or Testnet
Common(#[serde(with = "BigArray")] [u8; SOLUTION_SIZE]),
/// Equihash solution on Regtest
Regtest(#[serde(with = "BigArray")] [u8; REGTEST_SOLUTION_SIZE]),
}
impl Solution {
/// The length of the portion of the header used as input when verifying
/// equihash solutions, in bytes.
///
/// Excludes the 32-byte nonce, which is passed as a separate argument
/// to the verification function.
pub const INPUT_LENGTH: usize = 4 + 32 * 3 + 4 * 2;
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
/// Returns the inner value of the [`Solution`] as a byte slice.
fn value(&self) -> &[u8] {
match self {
Solution::Common(solution) => solution.as_slice(),
Solution::Regtest(solution) => solution.as_slice(),
}
}
/// Returns `Ok(())` if `EquihashSolution` is valid for `header`
#[allow(clippy::unwrap_in_result)]
pub fn check(&self, header: &Header) -> Result<(), Error> {
let nonce = &header.nonce;
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 (solution, n, k) = match self {
Solution::Common(solution) => (solution.as_slice(), 200, 9),
Solution::Regtest(solution) => (solution.as_slice(), 48, 5),
};
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 mut input = Vec::new();
header
.zcash_serialize(&mut input)
.expect("serialization into a vec can't fail");
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
// The part of the header before the nonce and solution.
// This data is kept constant during solver runs, so the verifier API takes it separately.
let input = &input[0..Solution::INPUT_LENGTH];
equihash::is_valid_solution(n, k, input, nonce.as_ref(), solution)?;
Ok(())
}
change(test): Create test harness for calling getblocktemplate in proposal mode, but don't use it yet (#5884) * adds ValidateBlock request to state * adds `Request` enum in block verifier skips solution check for BlockProposal requests calls CheckBlockValidity instead of Commit block for BlockProposal requests * uses new Request in references to chain verifier * adds getblocktemplate proposal mode response type * makes getblocktemplate-rpcs feature in zebra-consensus select getblocktemplate-rpcs in zebra-state * Adds PR review revisions * adds info log in CheckBlockProposalValidity * Reverts replacement of match statement * adds `GetBlockTemplate::capabilities` fn * conditions calling checkpoint verifier on !request.is_proposal * updates references to validate_and_commit_non_finalized * adds snapshot test, updates test vectors * adds `should_count_metrics` to NonFinalizedState * Returns an error from chain verifier for block proposal requests below checkpoint height adds feature flags * adds "proposal" to GET_BLOCK_TEMPLATE_CAPABILITIES_FIELD * adds back block::Request to zebra-consensus lib * updates snapshots * Removes unnecessary network arg * skips req in tracing intstrument for read state * Moves out block proposal validation to its own fn * corrects `difficulty_threshold_is_valid` docs adds/fixes some comments, adds TODOs general cleanup from a self-review. * Update zebra-state/src/service.rs * Apply suggestions from code review Co-authored-by: teor <teor@riseup.net> * Update zebra-rpc/src/methods/get_block_template_rpcs.rs Co-authored-by: teor <teor@riseup.net> * check best chain tip * Update zebra-state/src/service.rs Co-authored-by: teor <teor@riseup.net> * Applies cleanup suggestions from code review * updates gbt acceptance test to make a block proposal * fixes json parsing mistake * adds retries * returns reject reason if there are no retries left * moves result deserialization to RPCRequestClient method, adds docs, moves jsonrpc_core to dev-dependencies * moves sleep(EXPECTED_TX_TIME) out of loop * updates/adds info logs in retry loop * Revert "moves sleep(EXPECTED_TX_TIME) out of loop" This reverts commit f7f0926f4050519687a79afc16656c3f345c004b. * adds `allow(dead_code)` * tests with curtime, mintime, & maxtime * Fixes doc comment * Logs error responses from chain_verifier CheckProposal requests * Removes retry loop, adds num_txs log * removes verbose info log * sorts mempool_txs before generating merkle root * Make imports conditional on a feature * Disable new CI tests until bugs are fixed Co-authored-by: teor <teor@riseup.net> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2023-01-16 20:03:40 -08:00
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
/// Returns a [`Solution`] containing the bytes from `solution`.
/// Returns an error if `solution` is the wrong length.
pub fn from_bytes(solution: &[u8]) -> Result<Self, SerializationError> {
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
match solution.len() {
// Won't panic, because we just checked the length.
SOLUTION_SIZE => {
let mut bytes = [0; SOLUTION_SIZE];
bytes.copy_from_slice(solution);
Ok(Self::Common(bytes))
}
REGTEST_SOLUTION_SIZE => {
let mut bytes = [0; REGTEST_SOLUTION_SIZE];
bytes.copy_from_slice(solution);
Ok(Self::Regtest(bytes))
}
_unexpected_len => Err(SerializationError::Parse(
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
"incorrect equihash solution size",
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
)),
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
}
}
change(test): Create test harness for calling getblocktemplate in proposal mode, but don't use it yet (#5884) * adds ValidateBlock request to state * adds `Request` enum in block verifier skips solution check for BlockProposal requests calls CheckBlockValidity instead of Commit block for BlockProposal requests * uses new Request in references to chain verifier * adds getblocktemplate proposal mode response type * makes getblocktemplate-rpcs feature in zebra-consensus select getblocktemplate-rpcs in zebra-state * Adds PR review revisions * adds info log in CheckBlockProposalValidity * Reverts replacement of match statement * adds `GetBlockTemplate::capabilities` fn * conditions calling checkpoint verifier on !request.is_proposal * updates references to validate_and_commit_non_finalized * adds snapshot test, updates test vectors * adds `should_count_metrics` to NonFinalizedState * Returns an error from chain verifier for block proposal requests below checkpoint height adds feature flags * adds "proposal" to GET_BLOCK_TEMPLATE_CAPABILITIES_FIELD * adds back block::Request to zebra-consensus lib * updates snapshots * Removes unnecessary network arg * skips req in tracing intstrument for read state * Moves out block proposal validation to its own fn * corrects `difficulty_threshold_is_valid` docs adds/fixes some comments, adds TODOs general cleanup from a self-review. * Update zebra-state/src/service.rs * Apply suggestions from code review Co-authored-by: teor <teor@riseup.net> * Update zebra-rpc/src/methods/get_block_template_rpcs.rs Co-authored-by: teor <teor@riseup.net> * check best chain tip * Update zebra-state/src/service.rs Co-authored-by: teor <teor@riseup.net> * Applies cleanup suggestions from code review * updates gbt acceptance test to make a block proposal * fixes json parsing mistake * adds retries * returns reject reason if there are no retries left * moves result deserialization to RPCRequestClient method, adds docs, moves jsonrpc_core to dev-dependencies * moves sleep(EXPECTED_TX_TIME) out of loop * updates/adds info logs in retry loop * Revert "moves sleep(EXPECTED_TX_TIME) out of loop" This reverts commit f7f0926f4050519687a79afc16656c3f345c004b. * adds `allow(dead_code)` * tests with curtime, mintime, & maxtime * Fixes doc comment * Logs error responses from chain_verifier CheckProposal requests * Removes retry loop, adds num_txs log * removes verbose info log * sorts mempool_txs before generating merkle root * Make imports conditional on a feature * Disable new CI tests until bugs are fixed Co-authored-by: teor <teor@riseup.net> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2023-01-16 20:03:40 -08:00
/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
#[cfg(feature = "getblocktemplate-rpcs")]
change(test): Create test harness for calling getblocktemplate in proposal mode, but don't use it yet (#5884) * adds ValidateBlock request to state * adds `Request` enum in block verifier skips solution check for BlockProposal requests calls CheckBlockValidity instead of Commit block for BlockProposal requests * uses new Request in references to chain verifier * adds getblocktemplate proposal mode response type * makes getblocktemplate-rpcs feature in zebra-consensus select getblocktemplate-rpcs in zebra-state * Adds PR review revisions * adds info log in CheckBlockProposalValidity * Reverts replacement of match statement * adds `GetBlockTemplate::capabilities` fn * conditions calling checkpoint verifier on !request.is_proposal * updates references to validate_and_commit_non_finalized * adds snapshot test, updates test vectors * adds `should_count_metrics` to NonFinalizedState * Returns an error from chain verifier for block proposal requests below checkpoint height adds feature flags * adds "proposal" to GET_BLOCK_TEMPLATE_CAPABILITIES_FIELD * adds back block::Request to zebra-consensus lib * updates snapshots * Removes unnecessary network arg * skips req in tracing intstrument for read state * Moves out block proposal validation to its own fn * corrects `difficulty_threshold_is_valid` docs adds/fixes some comments, adds TODOs general cleanup from a self-review. * Update zebra-state/src/service.rs * Apply suggestions from code review Co-authored-by: teor <teor@riseup.net> * Update zebra-rpc/src/methods/get_block_template_rpcs.rs Co-authored-by: teor <teor@riseup.net> * check best chain tip * Update zebra-state/src/service.rs Co-authored-by: teor <teor@riseup.net> * Applies cleanup suggestions from code review * updates gbt acceptance test to make a block proposal * fixes json parsing mistake * adds retries * returns reject reason if there are no retries left * moves result deserialization to RPCRequestClient method, adds docs, moves jsonrpc_core to dev-dependencies * moves sleep(EXPECTED_TX_TIME) out of loop * updates/adds info logs in retry loop * Revert "moves sleep(EXPECTED_TX_TIME) out of loop" This reverts commit f7f0926f4050519687a79afc16656c3f345c004b. * adds `allow(dead_code)` * tests with curtime, mintime, & maxtime * Fixes doc comment * Logs error responses from chain_verifier CheckProposal requests * Removes retry loop, adds num_txs log * removes verbose info log * sorts mempool_txs before generating merkle root * Make imports conditional on a feature * Disable new CI tests until bugs are fixed Co-authored-by: teor <teor@riseup.net> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2023-01-16 20:03:40 -08:00
pub fn for_proposal() -> Self {
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
// TODO: Accept network as an argument, and if it's Regtest, return the shorter null solution.
Self::Common([0; SOLUTION_SIZE])
change(test): Create test harness for calling getblocktemplate in proposal mode, but don't use it yet (#5884) * adds ValidateBlock request to state * adds `Request` enum in block verifier skips solution check for BlockProposal requests calls CheckBlockValidity instead of Commit block for BlockProposal requests * uses new Request in references to chain verifier * adds getblocktemplate proposal mode response type * makes getblocktemplate-rpcs feature in zebra-consensus select getblocktemplate-rpcs in zebra-state * Adds PR review revisions * adds info log in CheckBlockProposalValidity * Reverts replacement of match statement * adds `GetBlockTemplate::capabilities` fn * conditions calling checkpoint verifier on !request.is_proposal * updates references to validate_and_commit_non_finalized * adds snapshot test, updates test vectors * adds `should_count_metrics` to NonFinalizedState * Returns an error from chain verifier for block proposal requests below checkpoint height adds feature flags * adds "proposal" to GET_BLOCK_TEMPLATE_CAPABILITIES_FIELD * adds back block::Request to zebra-consensus lib * updates snapshots * Removes unnecessary network arg * skips req in tracing intstrument for read state * Moves out block proposal validation to its own fn * corrects `difficulty_threshold_is_valid` docs adds/fixes some comments, adds TODOs general cleanup from a self-review. * Update zebra-state/src/service.rs * Apply suggestions from code review Co-authored-by: teor <teor@riseup.net> * Update zebra-rpc/src/methods/get_block_template_rpcs.rs Co-authored-by: teor <teor@riseup.net> * check best chain tip * Update zebra-state/src/service.rs Co-authored-by: teor <teor@riseup.net> * Applies cleanup suggestions from code review * updates gbt acceptance test to make a block proposal * fixes json parsing mistake * adds retries * returns reject reason if there are no retries left * moves result deserialization to RPCRequestClient method, adds docs, moves jsonrpc_core to dev-dependencies * moves sleep(EXPECTED_TX_TIME) out of loop * updates/adds info logs in retry loop * Revert "moves sleep(EXPECTED_TX_TIME) out of loop" This reverts commit f7f0926f4050519687a79afc16656c3f345c004b. * adds `allow(dead_code)` * tests with curtime, mintime, & maxtime * Fixes doc comment * Logs error responses from chain_verifier CheckProposal requests * Removes retry loop, adds num_txs log * removes verbose info log * sorts mempool_txs before generating merkle root * Make imports conditional on a feature * Disable new CI tests until bugs are fixed Co-authored-by: teor <teor@riseup.net> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2023-01-16 20:03:40 -08:00
}
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
/// Mines and returns one or more [`Solution`]s based on a template `header`.
/// The returned header contains a valid `nonce` and `solution`.
///
/// If `cancel_fn()` returns an error, returns early with `Err(SolverCancelled)`.
///
/// The `nonce` in the header template is taken as the starting nonce. If you are running multiple
/// solvers at the same time, start them with different nonces.
/// The `solution` in the header template is ignored.
///
/// This method is CPU and memory-intensive. It uses 144 MB of RAM and one CPU core while running.
/// It can run for minutes or hours if the network difficulty is high.
#[cfg(feature = "internal-miner")]
#[allow(clippy::unwrap_in_result)]
pub fn solve<F>(
mut _header: Header,
mut _cancel_fn: F,
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
) -> Result<AtLeastOne<Header>, SolverCancelled>
where
F: FnMut() -> Result<(), SolverCancelled>,
{
// TODO: Function code was removed as part of https://github.com/ZcashFoundation/zebra/issues/8180
// Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-chain/src/work/equihash.rs#L115-L166
// Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
Err(SolverCancelled)
}
// TODO: Some methods were removed as part of https://github.com/ZcashFoundation/zebra/issues/8180
// Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-chain/src/work/equihash.rs#L171-L196
// Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
}
impl PartialEq<Solution> for Solution {
fn eq(&self, other: &Solution) -> bool {
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
self.value() == other.value()
}
}
impl fmt::Debug for Solution {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("EquihashSolution")
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
.field(&hex::encode(self.value()))
.finish()
}
}
// These impls all only exist because of array length restrictions.
impl Copy for Solution {}
impl Clone for Solution {
fn clone(&self) -> Self {
*self
}
}
impl Eq for Solution {}
#[cfg(any(test, feature = "proptest-impl"))]
impl Default for Solution {
fn default() -> Self {
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
Self::Common([0; SOLUTION_SIZE])
}
}
impl ZcashSerialize for Solution {
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
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
zcash_serialize_bytes(&self.value().to_vec(), writer)
}
}
impl ZcashDeserialize for Solution {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let solution: Vec<u8> = (&mut reader).zcash_deserialize_into()?;
feat(mine): Add an internal Zcash miner to Zebra (#8136) * Patch equihash to use the solver branch * Add an internal-miner feature and set up its dependencies * Remove 'Experimental' from mining RPC docs * Fix a nightly clippy::question_mark lint * Move a byte array utility function to zebra-chain * fixup! Add an internal-miner feature and set up its dependencies * Add an equihash::Solution::solve() method with difficulty checks * Check solution is valid before returning it * Add a TODO to check for peers before mining * Move config validation into GetBlockTemplateRpcImpl::new() * fixup! fixup! Add an internal-miner feature and set up its dependencies * Use the same generic constraints for GetBlockTemplateRpcImpl struct and impls * Start adding an internal miner component * Add the miner task to the start command * Add basic miner code * Split out a method to mine one block * Spawn to a blocking thread * Wait until a valid template is available * Handle shutdown * Run mining on low priority threads * Ignore some invalid solutions * Use a difference nonce for each solver thread * Update TODOs * Change the patch into a renamed dependency to simplify crate releases * Clean up instrumentation and TODOs * Make RPC instances cloneable and clean up generics * Make LongPollId Copy so it's easier to use * Add API to restart mining if there's a new block template * Actually restart mining if there's a new block template * Tidy instrumentation * fixup! Move config validation into GetBlockTemplateRpcImpl::new() * fixup! Make RPC instances cloneable and clean up generics * Run the template generator and one miner concurrently * Reduce logging * Fix a bug in getblocktemplate RPC tip change detection * Work around some watch channel change bugs * Rate-limit template changes in the receiver * Run one mining solver per available core * Use updated C code with double-free protection * Update to the latest solver branch * Return and submit all valid solutions * Document what INPUT_LENGTH means * Fix watch channel change detection * Don't return early when a mining task fails * Spawn async miner tasks to avoid cooperative blocking, deadlocks, and improve shutdown responsiveness * Make existing parallelism docs and configs consistent * Add a mining parallelism config * Use the minimum of the configured or available threads for mining * Ignore optional feature fields in tests * Downgrade some frequent logs to debug * Document new zebrad features and tasks * Describe the internal-miner feature in the CHANGELOG * Update dependency to de-duplicate equihash solutions * Use futures::StreamExt instead of TryStreamExt * Fix a panic message typo
2024-01-11 06:41:01 -08:00
Self::from_bytes(&solution)
}
}