Skip to content

Commit

Permalink
change(mining): Restores parts of the internal-miner feature for use …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
arya2 authored May 10, 2024
1 parent d2d1a18 commit a6569d4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
6 changes: 3 additions & 3 deletions zebra-chain/src/work/equihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Solution {
#[cfg(feature = "internal-miner")]
#[allow(clippy::unwrap_in_result)]
pub fn solve<F>(
mut _header: Header,
mut header: Header,
mut _cancel_fn: F,
) -> Result<AtLeastOne<Header>, SolverCancelled>
where
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion zebra-rpc/src/config/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub async fn test_responses<State, ReadState>(
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
Expand Down
4 changes: 2 additions & 2 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
};
Expand Down
25 changes: 16 additions & 9 deletions zebrad/src/components/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
network: &Network,
config: &Config,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
) -> JoinHandle<Result<(), Report>>
Expand Down Expand Up @@ -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.
Expand All @@ -107,6 +109,7 @@ where
///
/// See [`run_mining_solver()`] for more details.
pub async fn init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
network: Network,
_config: Config,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
) -> Result<(), Report>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -217,6 +220,7 @@ pub async fn generate_block_templates<
SyncStatus,
AddressBook,
>(
network: Network,
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
template_sender: watch::Sender<Option<Arc<Block>>>,
) -> Result<(), Report>
Expand Down Expand Up @@ -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",
);
Expand All @@ -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| {
Expand Down

0 comments on commit a6569d4

Please sign in to comment.