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
This commit is contained in:
Arya 2024-05-10 14:28:05 -04:00 committed by GitHub
parent d2d1a18d81
commit a6569d4881
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 17 deletions

View File

@ -132,7 +132,7 @@ impl Solution {
#[cfg(feature = "internal-miner")] #[cfg(feature = "internal-miner")]
#[allow(clippy::unwrap_in_result)] #[allow(clippy::unwrap_in_result)]
pub fn solve<F>( pub fn solve<F>(
mut _header: Header, mut header: Header,
mut _cancel_fn: F, mut _cancel_fn: F,
) -> Result<AtLeastOne<Header>, SolverCancelled> ) -> Result<AtLeastOne<Header>, SolverCancelled>
where where
@ -141,8 +141,8 @@ impl Solution {
// TODO: Function code was removed as part of https://github.com/ZcashFoundation/zebra/issues/8180 // 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 // 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 // Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
header.solution = Solution::for_proposal();
Err(SolverCancelled) Ok(AtLeastOne::from_one(header))
} }
// TODO: Some methods were removed as part of https://github.com/ZcashFoundation/zebra/issues/8180 // TODO: Some methods were removed as part of https://github.com/ZcashFoundation/zebra/issues/8180

View File

@ -29,6 +29,17 @@ pub struct Config {
/// ///
/// This developer-only config is not supported for general use. /// This developer-only config is not supported for general use.
pub debug_like_zcashd: bool, 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 { 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 // 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 // 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 // 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 // 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 // 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 // Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
false self.internal_miner
} }
} }

View File

@ -101,7 +101,7 @@ pub async fn test_responses<State, ReadState>(
extra_coinbase_data: None, extra_coinbase_data: None,
debug_like_zcashd: true, debug_like_zcashd: true,
// TODO: Use default field values when optional features are enabled in tests #8183 // TODO: Use default field values when optional features are enabled in tests #8183
//..Default::default() ..Default::default()
}; };
// nu5 block height // nu5 block height

View File

@ -1286,7 +1286,7 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
extra_coinbase_data: None, extra_coinbase_data: None,
debug_like_zcashd: true, debug_like_zcashd: true,
// TODO: Use default field values when optional features are enabled in tests #8183 // TODO: Use default field values when optional features are enabled in tests #8183
//..Default::default() ..Default::default()
}; };
// nu5 block height // nu5 block height
@ -1735,7 +1735,7 @@ async fn rpc_getdifficulty() {
extra_coinbase_data: None, extra_coinbase_data: None,
debug_like_zcashd: true, debug_like_zcashd: true,
// TODO: Use default field values when optional features are enabled in tests #8183 // TODO: Use default field values when optional features are enabled in tests #8183
//..Default::default() ..Default::default()
}; };
// nu5 block height // nu5 block height

View File

@ -359,7 +359,7 @@ impl StartCmd {
address_book, address_book,
); );
crate::components::miner::spawn_init(&config.mining, rpc) crate::components::miner::spawn_init(&config.network.network, &config.mining, rpc)
} else { } else {
tokio::spawn(std::future::pending().in_current_span()) tokio::spawn(std::future::pending().in_current_span())
}; };

View File

@ -16,11 +16,11 @@ use tower::Service;
use tracing::{Instrument, Span}; use tracing::{Instrument, Span};
use zebra_chain::{ use zebra_chain::{
block::{self, Block}, block::{self, Block, Height},
chain_sync_status::ChainSyncStatus, chain_sync_status::ChainSyncStatus,
chain_tip::ChainTip, chain_tip::ChainTip,
diagnostic::task::WaitForPanics, diagnostic::task::WaitForPanics,
parameters::NetworkUpgrade, parameters::{Network, NetworkUpgrade},
serialization::{AtLeastOne, ZcashSerialize}, serialization::{AtLeastOne, ZcashSerialize},
shutdown::is_shutting_down, shutdown::is_shutting_down,
work::equihash::{Solution, SolverCancelled}, 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. /// See [`run_mining_solver()`] for more details.
pub fn spawn_init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>( pub fn spawn_init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
network: &Network,
config: &Config, config: &Config,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>, rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
) -> JoinHandle<Result<(), Report>> ) -> JoinHandle<Result<(), Report>>
@ -94,10 +95,11 @@ where
SyncStatus: ChainSyncStatus + Clone + Send + Sync + 'static, SyncStatus: ChainSyncStatus + Clone + Send + Sync + 'static,
AddressBook: AddressBookPeers + Clone + Send + Sync + 'static, AddressBook: AddressBookPeers + Clone + Send + Sync + 'static,
{ {
let network = network.clone();
let config = config.clone(); let config = config.clone();
// TODO: spawn an entirely new executor here, so mining is isolated from higher priority tasks. // 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. /// Initialize the miner based on its config.
@ -107,6 +109,7 @@ where
/// ///
/// See [`run_mining_solver()`] for more details. /// See [`run_mining_solver()`] for more details.
pub async fn init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>( pub async fn init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
network: Network,
_config: Config, _config: Config,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>, rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
) -> Result<(), Report> ) -> Result<(), Report>
@ -163,7 +166,7 @@ where
let mut abort_handles = Vec::new(); let mut abort_handles = Vec::new();
let template_generator = tokio::task::spawn( 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()); abort_handles.push(template_generator.abort_handle());
let template_generator = template_generator.wait_for_panics(); let template_generator = template_generator.wait_for_panics();
@ -217,6 +220,7 @@ pub async fn generate_block_templates<
SyncStatus, SyncStatus,
AddressBook, AddressBook,
>( >(
network: Network,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>, rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
template_sender: watch::Sender<Option<Arc<Block>>>, template_sender: watch::Sender<Option<Arc<Block>>>,
) -> Result<(), Report> ) -> Result<(), Report>
@ -260,11 +264,11 @@ where
// Shut down the task when all the template receivers are dropped, or Zebra shuts down. // Shut down the task when all the template receivers are dropped, or Zebra shuts down.
while !template_sender.is_closed() && !is_shutting_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. // Wait for the chain to sync so we get a valid template.
let Ok(template) = template else { let Ok(template) = template else {
debug!( info!(
?BLOCK_TEMPLATE_WAIT_TIME, ?BLOCK_TEMPLATE_WAIT_TIME,
"waiting for a valid block template", "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. // Tell the next get_block_template() call to wait until the template has changed.
parameters.long_poll_id = Some(template.long_poll_id); parameters.long_poll_id = Some(template.long_poll_id);
let block = let block = proposal_block_from_template(
proposal_block_from_template(&template, TimeSource::CurTime, NetworkUpgrade::Nu5) &template,
.expect("unexpected invalid block template"); TimeSource::CurTime,
NetworkUpgrade::current(&network, Height(template.height)),
)
.expect("unexpected invalid block template");
// If the template has actually changed, send an updated template. // If the template has actually changed, send an updated template.
template_sender.send_if_modified(|old_block| { template_sender.send_if_modified(|old_block| {