Skip to content

Commit cadb4be

Browse files
committed
Enables parts of the internal miner for use on Regtest when the solution isn't checked
1 parent b0298a2 commit cadb4be

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

zebra-chain/src/work/equihash.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Solution {
132132
#[cfg(feature = "internal-miner")]
133133
#[allow(clippy::unwrap_in_result)]
134134
pub fn solve<F>(
135-
mut _header: Header,
135+
mut header: Header,
136136
mut _cancel_fn: F,
137137
) -> Result<AtLeastOne<Header>, SolverCancelled>
138138
where
@@ -141,8 +141,8 @@ impl Solution {
141141
// TODO: Function code was removed as part of https://github.com/ZcashFoundation/zebra/issues/8180
142142
// Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-chain/src/work/equihash.rs#L115-L166
143143
// Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
144-
145-
Err(SolverCancelled)
144+
header.solution = Solution::for_proposal();
145+
Ok(AtLeastOne::from_one(header))
146146
}
147147

148148
// TODO: Some methods were removed as part of https://github.com/ZcashFoundation/zebra/issues/8180

zebra-rpc/src/config/mining.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub struct Config {
2929
///
3030
/// This developer-only config is not supported for general use.
3131
pub debug_like_zcashd: bool,
32+
33+
/// Mine blocks using Zebra's internal miner, without an external mining pool or equihash solver.
34+
///
35+
/// This experimental feature is only supported on testnet.
36+
/// Mainnet miners should use a mining pool with GPUs or ASICs designed for efficient mining.
37+
///
38+
/// The internal miner is off by default.
39+
#[cfg(feature = "internal-miner")]
40+
pub internal_miner: bool,
3241
}
3342

3443
impl Default for Config {
@@ -42,6 +51,8 @@ impl Default for Config {
4251
// TODO: Internal miner config code was removed as part of https://github.com/ZcashFoundation/zebra/issues/8180
4352
// Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-rpc/src/config/mining.rs#L61-L66
4453
// Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
54+
#[cfg(feature = "internal-miner")]
55+
internal_miner: false,
4556
}
4657
}
4758
}
@@ -61,6 +72,6 @@ impl Config {
6172
// TODO: Changed to return always false so internal miner is never started. Part of https://github.com/ZcashFoundation/zebra/issues/8180
6273
// Find the removed code at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-rpc/src/config/mining.rs#L83
6374
// Restore the code when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
64-
false
75+
self.internal_miner
6576
}
6677
}

zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub async fn test_responses<State, ReadState>(
101101
extra_coinbase_data: None,
102102
debug_like_zcashd: true,
103103
// TODO: Use default field values when optional features are enabled in tests #8183
104-
//..Default::default()
104+
..Default::default()
105105
};
106106

107107
// nu5 block height

zebra-rpc/src/methods/tests/vectors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
12861286
extra_coinbase_data: None,
12871287
debug_like_zcashd: true,
12881288
// TODO: Use default field values when optional features are enabled in tests #8183
1289-
//..Default::default()
1289+
..Default::default()
12901290
};
12911291

12921292
// nu5 block height
@@ -1735,7 +1735,7 @@ async fn rpc_getdifficulty() {
17351735
extra_coinbase_data: None,
17361736
debug_like_zcashd: true,
17371737
// TODO: Use default field values when optional features are enabled in tests #8183
1738-
//..Default::default()
1738+
..Default::default()
17391739
};
17401740

17411741
// nu5 block height

zebrad/src/commands/start.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl StartCmd {
359359
address_book,
360360
);
361361

362-
crate::components::miner::spawn_init(&config.mining, rpc)
362+
crate::components::miner::spawn_init(&config.network.network, &config.mining, rpc)
363363
} else {
364364
tokio::spawn(std::future::pending().in_current_span())
365365
};

zebrad/src/components/miner.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use tower::Service;
1616
use tracing::{Instrument, Span};
1717

1818
use zebra_chain::{
19-
block::{self, Block},
19+
block::{self, Block, Height},
2020
chain_sync_status::ChainSyncStatus,
2121
chain_tip::ChainTip,
2222
diagnostic::task::WaitForPanics,
23-
parameters::NetworkUpgrade,
23+
parameters::{Network, NetworkUpgrade},
2424
serialization::{AtLeastOne, ZcashSerialize},
2525
shutdown::is_shutting_down,
2626
work::equihash::{Solution, SolverCancelled},
@@ -61,6 +61,7 @@ pub const BLOCK_MINING_WAIT_TIME: Duration = Duration::from_secs(3);
6161
///
6262
/// See [`run_mining_solver()`] for more details.
6363
pub fn spawn_init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
64+
network: &Network,
6465
config: &Config,
6566
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
6667
) -> JoinHandle<Result<(), Report>>
@@ -94,10 +95,11 @@ where
9495
SyncStatus: ChainSyncStatus + Clone + Send + Sync + 'static,
9596
AddressBook: AddressBookPeers + Clone + Send + Sync + 'static,
9697
{
98+
let network = network.clone();
9799
let config = config.clone();
98100

99101
// TODO: spawn an entirely new executor here, so mining is isolated from higher priority tasks.
100-
tokio::spawn(init(config, rpc).in_current_span())
102+
tokio::spawn(init(network, config, rpc).in_current_span())
101103
}
102104

103105
/// Initialize the miner based on its config.
@@ -107,6 +109,7 @@ where
107109
///
108110
/// See [`run_mining_solver()`] for more details.
109111
pub async fn init<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>(
112+
network: Network,
110113
_config: Config,
111114
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
112115
) -> Result<(), Report>
@@ -163,7 +166,7 @@ where
163166
let mut abort_handles = Vec::new();
164167

165168
let template_generator = tokio::task::spawn(
166-
generate_block_templates(rpc.clone(), template_sender).in_current_span(),
169+
generate_block_templates(network, rpc.clone(), template_sender).in_current_span(),
167170
);
168171
abort_handles.push(template_generator.abort_handle());
169172
let template_generator = template_generator.wait_for_panics();
@@ -217,6 +220,7 @@ pub async fn generate_block_templates<
217220
SyncStatus,
218221
AddressBook,
219222
>(
223+
network: Network,
220224
rpc: GetBlockTemplateRpcImpl<Mempool, State, Tip, BlockVerifierRouter, SyncStatus, AddressBook>,
221225
template_sender: watch::Sender<Option<Arc<Block>>>,
222226
) -> Result<(), Report>
@@ -260,11 +264,11 @@ where
260264

261265
// Shut down the task when all the template receivers are dropped, or Zebra shuts down.
262266
while !template_sender.is_closed() && !is_shutting_down() {
263-
let template = rpc.get_block_template(Some(parameters.clone())).await;
267+
let template: Result<_, _> = rpc.get_block_template(Some(parameters.clone())).await;
264268

265269
// Wait for the chain to sync so we get a valid template.
266270
let Ok(template) = template else {
267-
debug!(
271+
info!(
268272
?BLOCK_TEMPLATE_WAIT_TIME,
269273
"waiting for a valid block template",
270274
);
@@ -291,9 +295,12 @@ where
291295
// Tell the next get_block_template() call to wait until the template has changed.
292296
parameters.long_poll_id = Some(template.long_poll_id);
293297

294-
let block =
295-
proposal_block_from_template(&template, TimeSource::CurTime, NetworkUpgrade::Nu5)
296-
.expect("unexpected invalid block template");
298+
let block = proposal_block_from_template(
299+
&template,
300+
TimeSource::CurTime,
301+
NetworkUpgrade::current(&network, Height(template.height)),
302+
)
303+
.expect("unexpected invalid block template");
297304

298305
// If the template has actually changed, send an updated template.
299306
template_sender.send_if_modified(|old_block| {

0 commit comments

Comments
 (0)