Skip to content

add(mining): Restore internal miner #9311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
277cdbe
Bumps ECC dep versions (using git sources) and updates their usage in…
arya2 Feb 20, 2025
4606c9a
removes dependency on bridgetree and imports types from incrementalme…
arya2 Feb 20, 2025
bda10ab
Removes unused patches
arya2 Feb 25, 2025
3e2cb0a
bumps ECC dep versions and replaces Zebra's usage of the now-deprecat…
arya2 Feb 25, 2025
f3ab732
Adds conversion impl from `HashType` for `SighashType`
arya2 Feb 25, 2025
d8ae973
fixes lints
arya2 Feb 26, 2025
cad6f8e
updates deny.toml
arya2 Feb 26, 2025
f9a841a
updates edition, adds redjubjub to cargo deny exceptions
arya2 Feb 26, 2025
a727686
reverts Rust edition bump
arya2 Feb 26, 2025
15b1e2d
fixes new usage of `add_output()`
arya2 Feb 26, 2025
72d7ab1
restores internal miner with equihash solver
arya2 Feb 28, 2025
94dd822
Adds a stored config and skips the network arg in tracing instrument
arya2 Feb 28, 2025
8f48568
Apply suggestions from code review
oxarbitrage Apr 2, 2025
62daf1e
Merge remote-tracking branch 'origin/main' into restore-internal-test…
oxarbitrage Apr 2, 2025
54b20d8
Merge branch 'main' into restore-internal-testnet-miner
mergify[bot] Apr 3, 2025
ce15f89
Merge branch 'main' into restore-internal-testnet-miner
upbqdn Apr 3, 2025
f3fca77
brings `internal_miner` config field out from behind the feature flag…
arya2 Apr 11, 2025
2e817f6
Merge remote-tracking branch 'origin/main' into restore-internal-test…
arya2 Apr 11, 2025
436533a
fixes compilation issues
arya2 Apr 11, 2025
09bf38c
addresses failing tests and fixes compilation issue
arya2 Apr 11, 2025
a7e6a22
Merge branch 'main' into restore-internal-testnet-miner
mergify[bot] Apr 14, 2025
1727878
Merge branch 'main' into restore-internal-testnet-miner
upbqdn Apr 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca4f333d4ccc9d23c06593733673026efa71a332e028b00f12cf427b9677dce9"
dependencies = [
"blake2b_simd",
"cc",
"core2",
"document-features",
]
Expand Down
8 changes: 1 addition & 7 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ shielded-scan = [
]

# Experimental internal miner support
# TODO: Internal miner feature functionality was removed at https://github.com/ZcashFoundation/zebra/issues/8180
# See what was removed at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-chain/Cargo.toml#L38-L43
# Restore support when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
internal-miner = []
internal-miner = ["equihash/solver"]

# Experimental elasticsearch support
elasticsearch = []
Expand Down Expand Up @@ -69,9 +66,6 @@ blake2s_simd = { workspace = true }
bs58 = { workspace = true, features = ["check"] }
byteorder = { workspace = true }

# TODO: Internal miner feature functionality was removed at https://github.com/ZcashFoundation/zebra/issues/8180
# See what was removed at https://github.com/ZcashFoundation/zebra/blob/v1.5.1/zebra-chain/Cargo.toml#L73-L85
# Restore support when conditions are met. https://github.com/ZcashFoundation/zebra/issues/8183
equihash = { workspace = true }

group = { workspace = true }
Expand Down
89 changes: 80 additions & 9 deletions zebra-chain/src/work/equihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,92 @@ impl Solution {
#[allow(clippy::unwrap_in_result)]
pub fn solve<F>(
mut header: Header,
mut _cancel_fn: F,
mut cancel_fn: F,
) -> 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
header.solution = Solution::for_proposal();
Ok(AtLeastOne::from_one(header))
use crate::shutdown::is_shutting_down;

let mut input = Vec::new();
header
.zcash_serialize(&mut input)
.expect("serialization into a vec can't fail");
// Take the part of the header before the nonce and solution.
// This data is kept constant for this solver run.
let input = &input[0..Solution::INPUT_LENGTH];

while !is_shutting_down() {
// Don't run the solver if we'd just cancel it anyway.
cancel_fn()?;

let solutions = equihash::tromp::solve_200_9(input, || {
// Cancel the solver if we have a new template.
if cancel_fn().is_err() {
return None;
}

// This skips the first nonce, which doesn't matter in practice.
Self::next_nonce(&mut header.nonce);
Some(*header.nonce)
});

let mut valid_solutions = Vec::new();

for solution in &solutions {
header.solution = Self::from_bytes(solution)
.expect("unexpected invalid solution: incorrect length");

// TODO: work out why we sometimes get invalid solutions here
if let Err(error) = header.solution.check(&header) {
info!(?error, "found invalid solution for header");
continue;
}

if Self::difficulty_is_valid(&header) {
valid_solutions.push(header);
}
}

match valid_solutions.try_into() {
Ok(at_least_one_solution) => return Ok(at_least_one_solution),
Err(_is_empty_error) => debug!(
solutions = ?solutions.len(),
"found valid solutions which did not pass the validity or difficulty checks"
),
}
}

Err(SolverCancelled)
}

/// Returns `true` if the `nonce` and `solution` in `header` meet the difficulty threshold.
///
/// # Panics
///
/// - If `header` contains an invalid difficulty threshold.
#[cfg(feature = "internal-miner")]
fn difficulty_is_valid(header: &Header) -> bool {
// Simplified from zebra_consensus::block::check::difficulty_is_valid().
let difficulty_threshold = header
.difficulty_threshold
.to_expanded()
.expect("unexpected invalid header template: invalid difficulty threshold");

// TODO: avoid calculating this hash multiple times
let hash = header.hash();

// Note: this comparison is a u256 integer comparison, like zcashd and bitcoin. Greater
// values represent *less* work.
hash <= difficulty_threshold
}

// 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
/// Modifies `nonce` to be the next integer in big-endian order.
/// Wraps to zero if the next nonce would overflow.
#[cfg(feature = "internal-miner")]
fn next_nonce(nonce: &mut [u8; 32]) {
let _ignore_overflow = crate::primitives::byte_array::increment_big_endian(&mut nonce[..]);
}
}

impl PartialEq<Solution> for Solution {
Expand Down
8 changes: 1 addition & 7 deletions zebra-rpc/src/config/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ pub struct Config {
/// 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")]
#[serde(default)]
pub internal_miner: bool,
}

Expand All @@ -50,10 +48,6 @@ impl Default for Config {
// TODO: do we want to default to v5 transactions and Zebra coinbase data?
extra_coinbase_data: None,
debug_like_zcashd: true,
// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ 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
#[cfg(feature = "internal-miner")]
internal_miner: true,
};

Expand Down
4 changes: 0 additions & 4 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,8 +1831,6 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
miner_address: miner_address.clone(),
extra_coinbase_data: None,
debug_like_zcashd: true,
// TODO: Use default field values when optional features are enabled in tests #8183
#[cfg(feature = "internal-miner")]
internal_miner: true,
};

Expand Down Expand Up @@ -2307,8 +2305,6 @@ async fn rpc_getdifficulty() {
miner_address: None,
extra_coinbase_data: None,
debug_like_zcashd: true,
// TODO: Use default field values when optional features are enabled in tests #8183
#[cfg(feature = "internal-miner")]
internal_miner: true,
};

Expand Down
5 changes: 3 additions & 2 deletions zebrad/src/components/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
}

/// Generates block templates using `rpc`, and sends them to mining threads using `template_sender`.
#[instrument(skip(rpc, template_sender))]
#[instrument(skip(rpc, template_sender, network))]
pub async fn generate_block_templates<
Mempool,
State,
Expand Down Expand Up @@ -266,8 +266,9 @@ where

// Wait for the chain to sync so we get a valid template.
let Ok(template) = template else {
info!(
warn!(
?BLOCK_TEMPLATE_WAIT_TIME,
?template,
"waiting for a valid block template",
);

Expand Down
84 changes: 84 additions & 0 deletions zebrad/tests/common/configs/v1.9.0-internal-miner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Default configuration for zebrad.
#
# This file can be used as a skeleton for custom configs.
#
# Unspecified fields use default values. Optional fields are Some(field) if the
# field is present and None if it is absent.
#
# This file is generated as an example using zebrad's current defaults.
# You should set only the config options you want to keep, and delete the rest.
# Only a subset of fields are present in the skeleton, since optional values
# whose default is None are omitted.
#
# The config format (including a complete list of sections and fields) is
# documented here:
# https://docs.rs/zebrad/latest/zebrad/config/struct.ZebradConfig.html
#
# zebrad attempts to load configs in the following order:
#
# 1. The -c flag on the command line, e.g., `zebrad -c myconfig.toml start`;
# 2. The file `zebrad.toml` in the users's preference directory (platform-dependent);
# 3. The default config.
#
# The user's preference directory and the default path to the `zebrad` config are platform dependent,
# based on `dirs::preference_dir`, see https://docs.rs/dirs/latest/dirs/fn.preference_dir.html :
#
# | Platform | Value | Example |
# | -------- | ------------------------------------- | ---------------------------------------------- |
# | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/zebrad.toml` |
# | macOS | `$HOME/Library/Preferences` | `/Users/Alice/Library/Preferences/zebrad.toml` |
# | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Local\zebrad.toml` |

[consensus]
checkpoint_sync = true

[mempool]
eviction_memory_time = "1h"
tx_cost_limit = 80000000
debug_enable_at_height = 0

[metrics]

[mining]
miner_address = 't27eWDgjFYJGVXmzrXeVjnb5J3uXDM9xH9v'
internal_miner = true

[network]
cache_dir = true
crawl_new_peer_interval = "1m 1s"
initial_mainnet_peers = [
"dnsseed.z.cash:8233",
"dnsseed.str4d.xyz:8233",
"mainnet.seeder.zfnd.org:8233",
"mainnet.is.yolo.money:8233",
]
initial_testnet_peers = [
"dnsseed.testnet.z.cash:18233",
"testnet.seeder.zfnd.org:18233",
"testnet.is.yolo.money:18233",
]
listen_addr = "0.0.0.0:8233"
max_connections_per_ip = 1
network = "Testnet"
peerset_initial_target_size = 25

[rpc]
debug_force_finished_sync = false
parallel_cpu_threads = 0

[state]
cache_dir = "cache_dir"
delete_old_database = true
ephemeral = false

[sync]
checkpoint_verify_concurrency_limit = 1000
download_concurrency_limit = 50
full_verify_concurrency_limit = 20
parallel_cpu_threads = 0

[tracing]
buffer_limit = 128000
force_use_color = false
use_color = true
use_journald = false
85 changes: 85 additions & 0 deletions zebrad/tests/common/configs/v2.2.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Default configuration for zebrad.
#
# This file can be used as a skeleton for custom configs.
#
# Unspecified fields use default values. Optional fields are Some(field) if the
# field is present and None if it is absent.
#
# This file is generated as an example using zebrad's current defaults.
# You should set only the config options you want to keep, and delete the rest.
# Only a subset of fields are present in the skeleton, since optional values
# whose default is None are omitted.
#
# The config format (including a complete list of sections and fields) is
# documented here:
# https://docs.rs/zebrad/latest/zebrad/config/struct.ZebradConfig.html
#
# zebrad attempts to load configs in the following order:
#
# 1. The -c flag on the command line, e.g., `zebrad -c myconfig.toml start`;
# 2. The file `zebrad.toml` in the users's preference directory (platform-dependent);
# 3. The default config.
#
# The user's preference directory and the default path to the `zebrad` config are platform dependent,
# based on `dirs::preference_dir`, see https://docs.rs/dirs/latest/dirs/fn.preference_dir.html :
#
# | Platform | Value | Example |
# | -------- | ------------------------------------- | ---------------------------------------------- |
# | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/zebrad.toml` |
# | macOS | `$HOME/Library/Preferences` | `/Users/Alice/Library/Preferences/zebrad.toml` |
# | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Local\zebrad.toml` |

[consensus]
checkpoint_sync = true

[mempool]
eviction_memory_time = "1h"
tx_cost_limit = 80000000

[metrics]

[mining]
debug_like_zcashd = true
internal_miner = false

[network]
cache_dir = true
crawl_new_peer_interval = "1m 1s"
initial_mainnet_peers = [
"dnsseed.z.cash:8233",
"dnsseed.str4d.xyz:8233",
"mainnet.seeder.zfnd.org:8233",
"mainnet.is.yolo.money:8233",
]
initial_testnet_peers = [
"dnsseed.testnet.z.cash:18233",
"testnet.seeder.zfnd.org:18233",
"testnet.is.yolo.money:18233",
]
listen_addr = "0.0.0.0:8233"
max_connections_per_ip = 1
network = "Mainnet"
peerset_initial_target_size = 25

[rpc]
cookie_dir = "cache_dir"
debug_force_finished_sync = false
enable_cookie_auth = true
parallel_cpu_threads = 0

[state]
cache_dir = "cache_dir"
delete_old_database = true
ephemeral = false

[sync]
checkpoint_verify_concurrency_limit = 1000
download_concurrency_limit = 50
full_verify_concurrency_limit = 20
parallel_cpu_threads = 0

[tracing]
buffer_limit = 128000
force_use_color = false
use_color = true
use_journald = false
Loading