From a6569d4881d58b25f7f2f55a6b558144447bce4f Mon Sep 17 00:00:00 2001 From: Arya Date: Fri, 10 May 2024 14:28:05 -0400 Subject: [PATCH] change(mining): Restores parts of the internal-miner feature for use on Regtest (#8506) * Allow configurable Nu5 activation height on Regtest * Fixes test * removes outdated TODO * Adds `current_with_activation_height()` method and uses it in `Commitment::from_bytes()` * Enables parts of the internal miner for use on Regtest when the solution isn't checked * Update internal miner config field docs --- zebra-chain/src/work/equihash.rs | 6 ++--- zebra-rpc/src/config/mining.rs | 15 ++++++++++- .../tests/snapshot/get_block_template_rpcs.rs | 2 +- zebra-rpc/src/methods/tests/vectors.rs | 4 +-- zebrad/src/commands/start.rs | 2 +- zebrad/src/components/miner.rs | 25 ++++++++++++------- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/zebra-chain/src/work/equihash.rs b/zebra-chain/src/work/equihash.rs index 200a59f8041..f79b9212e53 100644 --- a/zebra-chain/src/work/equihash.rs +++ b/zebra-chain/src/work/equihash.rs @@ -132,7 +132,7 @@ impl Solution { #[cfg(feature = "internal-miner")] #[allow(clippy::unwrap_in_result)] pub fn solve( - mut _header: Header, + mut header: Header, mut _cancel_fn: F, ) -> Result, SolverCancelled> where @@ -141,8 +141,8 @@ impl Solution { // 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 - - Err(SolverCancelled) + header.solution = Solution::for_proposal(); + Ok(AtLeastOne::from_one(header)) } // TODO: Some methods were removed as part of https://github.com/ZcashFoundation/zebra/issues/8180 diff --git a/zebra-rpc/src/config/mining.rs b/zebra-rpc/src/config/mining.rs index 040b3a9e7a7..0594d96b822 100644 --- a/zebra-rpc/src/config/mining.rs +++ b/zebra-rpc/src/config/mining.rs @@ -29,6 +29,17 @@ pub struct Config { /// /// This developer-only config is not supported for general use. pub debug_like_zcashd: bool, + + /// Mine blocks using Zebra's internal miner, without an external mining pool or equihash solver. + /// + /// This experimental feature is only supported on regtest as it uses null solutions and skips checking + /// for a valid Proof of Work. + /// + /// The internal miner is off by default. + // TODO: Restore equihash solver and recommend that Mainnet miners should use a mining pool with + // GPUs or ASICs designed for efficient mining. + #[cfg(feature = "internal-miner")] + pub internal_miner: bool, } impl Default for Config { @@ -42,6 +53,8 @@ impl Default for Config { // TODO: Internal miner config 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-rpc/src/config/mining.rs#L61-L66 // Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183 + #[cfg(feature = "internal-miner")] + internal_miner: false, } } } @@ -61,6 +74,6 @@ impl Config { // TODO: Changed to return always false so internal miner is never started. Part of https://github.com/ZcashFoundation/zebra/issues/8180 // Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-rpc/src/config/mining.rs#L83 // Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183 - false + self.internal_miner } } diff --git a/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs b/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs index 7517b3e4dbe..04b3139913f 100644 --- a/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs +++ b/zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs @@ -101,7 +101,7 @@ pub async fn test_responses( extra_coinbase_data: None, debug_like_zcashd: true, // TODO: Use default field values when optional features are enabled in tests #8183 - //..Default::default() + ..Default::default() }; // nu5 block height diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index fe80a8dd236..4d830c51e93 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -1286,7 +1286,7 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) { extra_coinbase_data: None, debug_like_zcashd: true, // TODO: Use default field values when optional features are enabled in tests #8183 - //..Default::default() + ..Default::default() }; // nu5 block height @@ -1735,7 +1735,7 @@ async fn rpc_getdifficulty() { extra_coinbase_data: None, debug_like_zcashd: true, // TODO: Use default field values when optional features are enabled in tests #8183 - //..Default::default() + ..Default::default() }; // nu5 block height diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index 7efabf08ecc..2f2dabf0daa 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -359,7 +359,7 @@ impl StartCmd { address_book, ); - crate::components::miner::spawn_init(&config.mining, rpc) + crate::components::miner::spawn_init(&config.network.network, &config.mining, rpc) } else { tokio::spawn(std::future::pending().in_current_span()) }; diff --git a/zebrad/src/components/miner.rs b/zebrad/src/components/miner.rs index 47732257b4f..193f30e8c5b 100644 --- a/zebrad/src/components/miner.rs +++ b/zebrad/src/components/miner.rs @@ -16,11 +16,11 @@ use tower::Service; use tracing::{Instrument, Span}; use zebra_chain::{ - block::{self, Block}, + block::{self, Block, Height}, chain_sync_status::ChainSyncStatus, chain_tip::ChainTip, diagnostic::task::WaitForPanics, - parameters::NetworkUpgrade, + parameters::{Network, NetworkUpgrade}, serialization::{AtLeastOne, ZcashSerialize}, shutdown::is_shutting_down, work::equihash::{Solution, SolverCancelled}, @@ -61,6 +61,7 @@ pub const BLOCK_MINING_WAIT_TIME: Duration = Duration::from_secs(3); /// /// See [`run_mining_solver()`] for more details. pub fn spawn_init( + network: &Network, config: &Config, rpc: GetBlockTemplateRpcImpl, ) -> JoinHandle> @@ -94,10 +95,11 @@ where SyncStatus: ChainSyncStatus + Clone + Send + Sync + 'static, AddressBook: AddressBookPeers + Clone + Send + Sync + 'static, { + let network = network.clone(); let config = config.clone(); // TODO: spawn an entirely new executor here, so mining is isolated from higher priority tasks. - tokio::spawn(init(config, rpc).in_current_span()) + tokio::spawn(init(network, config, rpc).in_current_span()) } /// Initialize the miner based on its config. @@ -107,6 +109,7 @@ where /// /// See [`run_mining_solver()`] for more details. pub async fn init( + network: Network, _config: Config, rpc: GetBlockTemplateRpcImpl, ) -> Result<(), Report> @@ -163,7 +166,7 @@ where let mut abort_handles = Vec::new(); let template_generator = tokio::task::spawn( - generate_block_templates(rpc.clone(), template_sender).in_current_span(), + generate_block_templates(network, rpc.clone(), template_sender).in_current_span(), ); abort_handles.push(template_generator.abort_handle()); let template_generator = template_generator.wait_for_panics(); @@ -217,6 +220,7 @@ pub async fn generate_block_templates< SyncStatus, AddressBook, >( + network: Network, rpc: GetBlockTemplateRpcImpl, template_sender: watch::Sender>>, ) -> Result<(), Report> @@ -260,11 +264,11 @@ where // Shut down the task when all the template receivers are dropped, or Zebra shuts down. while !template_sender.is_closed() && !is_shutting_down() { - let template = rpc.get_block_template(Some(parameters.clone())).await; + let template: Result<_, _> = rpc.get_block_template(Some(parameters.clone())).await; // Wait for the chain to sync so we get a valid template. let Ok(template) = template else { - debug!( + info!( ?BLOCK_TEMPLATE_WAIT_TIME, "waiting for a valid block template", ); @@ -291,9 +295,12 @@ where // Tell the next get_block_template() call to wait until the template has changed. parameters.long_poll_id = Some(template.long_poll_id); - let block = - proposal_block_from_template(&template, TimeSource::CurTime, NetworkUpgrade::Nu5) - .expect("unexpected invalid block template"); + let block = proposal_block_from_template( + &template, + TimeSource::CurTime, + NetworkUpgrade::current(&network, Height(template.height)), + ) + .expect("unexpected invalid block template"); // If the template has actually changed, send an updated template. template_sender.send_if_modified(|old_block| {