2020-08-15 00:14:07 -07:00
|
|
|
//! Equihash Solution and related items.
|
|
|
|
|
|
|
|
use std::{fmt, io};
|
|
|
|
|
2022-03-30 18:33:52 -07:00
|
|
|
use serde_big_array::BigArray;
|
|
|
|
|
2021-11-05 11:24:24 -07:00
|
|
|
use crate::{
|
|
|
|
block::Header,
|
|
|
|
serialization::{
|
2022-03-30 18:33:52 -07:00
|
|
|
zcash_serialize_bytes, SerializationError, ZcashDeserialize, ZcashDeserializeInto,
|
|
|
|
ZcashSerialize,
|
2021-11-05 11:24:24 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-01-11 06:41:01 -08:00
|
|
|
#[cfg(feature = "internal-miner")]
|
|
|
|
use crate::serialization::AtLeastOne;
|
|
|
|
|
|
|
|
/// The error type for Equihash validation.
|
2020-09-03 18:56:09 -07:00
|
|
|
#[non_exhaustive]
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("invalid equihash solution for BlockHeader")]
|
|
|
|
pub struct Error(#[from] equihash::Error);
|
|
|
|
|
2024-01-11 06:41:01 -08:00
|
|
|
/// The error type for Equihash solving.
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, thiserror::Error)]
|
|
|
|
#[error("solver was cancelled")]
|
|
|
|
pub struct SolverCancelled;
|
|
|
|
|
2020-08-15 00:14:07 -07:00
|
|
|
/// The size of an Equihash solution in bytes (always 1344).
|
|
|
|
pub(crate) const SOLUTION_SIZE: usize = 1344;
|
|
|
|
|
2024-01-11 06:41:01 -08:00
|
|
|
/// Equihash Solution in compressed format.
|
2020-08-15 00:14:07 -07:00
|
|
|
///
|
|
|
|
/// A wrapper around [u8; 1344] because Rust doesn't implement common
|
|
|
|
/// traits like `Debug`, `Clone`, etc for collections like array
|
|
|
|
/// beyond lengths 0 to 32.
|
|
|
|
///
|
|
|
|
/// The size of an Equihash solution in bytes is always 1344 so the
|
|
|
|
/// length of this type is fixed.
|
|
|
|
#[derive(Deserialize, Serialize)]
|
2022-03-30 18:33:52 -07:00
|
|
|
pub struct Solution(#[serde(with = "BigArray")] pub [u8; SOLUTION_SIZE]);
|
2020-08-15 00:14:07 -07:00
|
|
|
|
|
|
|
impl Solution {
|
|
|
|
/// The length of the portion of the header used as input when verifying
|
2020-10-28 04:59:57 -07:00
|
|
|
/// equihash solutions, in bytes.
|
|
|
|
///
|
|
|
|
/// Excludes the 32-byte nonce, which is passed as a separate argument
|
|
|
|
/// to the verification function.
|
2020-08-15 00:14:07 -07:00
|
|
|
pub const INPUT_LENGTH: usize = 4 + 32 * 3 + 4 * 2;
|
2020-09-03 18:56:09 -07:00
|
|
|
|
2020-10-12 14:21:05 -07:00
|
|
|
/// Returns `Ok(())` if `EquihashSolution` is valid for `header`
|
2022-06-27 23:22:07 -07:00
|
|
|
#[allow(clippy::unwrap_in_result)]
|
2020-10-12 14:21:05 -07:00
|
|
|
pub fn check(&self, header: &Header) -> Result<(), Error> {
|
2020-09-03 18:56:09 -07:00
|
|
|
let n = 200;
|
|
|
|
let k = 9;
|
2020-10-12 14:21:05 -07:00
|
|
|
let nonce = &header.nonce;
|
2020-09-03 18:56:09 -07:00
|
|
|
let solution = &self.0;
|
|
|
|
let mut input = Vec::new();
|
|
|
|
|
2020-10-12 14:21:05 -07:00
|
|
|
header
|
2020-09-03 18:56:09 -07:00
|
|
|
.zcash_serialize(&mut input)
|
|
|
|
.expect("serialization into a vec can't fail");
|
|
|
|
|
2024-01-11 06:41:01 -08:00
|
|
|
// The part of the header before the nonce and solution.
|
|
|
|
// This data is kept constant during solver runs, so the verifier API takes it separately.
|
2020-09-03 18:56:09 -07:00
|
|
|
let input = &input[0..Solution::INPUT_LENGTH];
|
|
|
|
|
2023-01-17 05:57:22 -08:00
|
|
|
equihash::is_valid_solution(n, k, input, nonce.as_ref(), solution)?;
|
2020-09-03 18:56:09 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-16 20:03:40 -08:00
|
|
|
|
2024-01-11 06:41:01 -08:00
|
|
|
/// Returns a [`Solution`] containing the bytes from `solution`.
|
|
|
|
/// Returns an error if `solution` is the wrong length.
|
|
|
|
pub fn from_bytes(solution: &[u8]) -> Result<Self, SerializationError> {
|
|
|
|
if solution.len() != SOLUTION_SIZE {
|
|
|
|
return Err(SerializationError::Parse(
|
|
|
|
"incorrect equihash solution size",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut bytes = [0; SOLUTION_SIZE];
|
|
|
|
// Won't panic, because we just checked the length.
|
|
|
|
bytes.copy_from_slice(solution);
|
|
|
|
|
|
|
|
Ok(Self(bytes))
|
|
|
|
}
|
|
|
|
|
2023-01-16 20:03:40 -08:00
|
|
|
/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
|
2024-01-11 06:41:01 -08:00
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
2023-01-16 20:03:40 -08:00
|
|
|
pub fn for_proposal() -> Self {
|
|
|
|
Self([0; SOLUTION_SIZE])
|
|
|
|
}
|
2024-01-11 06:41:01 -08:00
|
|
|
|
|
|
|
/// Mines and returns one or more [`Solution`]s based on a template `header`.
|
|
|
|
/// The returned header contains a valid `nonce` and `solution`.
|
|
|
|
///
|
|
|
|
/// If `cancel_fn()` returns an error, returns early with `Err(SolverCancelled)`.
|
|
|
|
///
|
|
|
|
/// The `nonce` in the header template is taken as the starting nonce. If you are running multiple
|
|
|
|
/// solvers at the same time, start them with different nonces.
|
|
|
|
/// The `solution` in the header template is ignored.
|
|
|
|
///
|
|
|
|
/// This method is CPU and memory-intensive. It uses 144 MB of RAM and one CPU core while running.
|
|
|
|
/// It can run for minutes or hours if the network difficulty is high.
|
|
|
|
#[cfg(feature = "internal-miner")]
|
|
|
|
#[allow(clippy::unwrap_in_result)]
|
|
|
|
pub fn solve<F>(
|
2024-01-23 10:02:52 -08:00
|
|
|
mut _header: Header,
|
|
|
|
mut _cancel_fn: F,
|
2024-01-11 06:41:01 -08:00
|
|
|
) -> Result<AtLeastOne<Header>, SolverCancelled>
|
|
|
|
where
|
|
|
|
F: FnMut() -> Result<(), SolverCancelled>,
|
|
|
|
{
|
2024-01-23 10:02:52 -08:00
|
|
|
// 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
|
2024-01-11 06:41:01 -08:00
|
|
|
|
|
|
|
Err(SolverCancelled)
|
|
|
|
}
|
|
|
|
|
2024-01-23 10:02:52 -08:00
|
|
|
// 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
|
2020-08-15 00:14:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Solution> for Solution {
|
|
|
|
fn eq(&self, other: &Solution) -> bool {
|
|
|
|
self.0.as_ref() == other.0.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Solution {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("EquihashSolution")
|
|
|
|
.field(&hex::encode(&self.0[..]))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These impls all only exist because of array length restrictions.
|
|
|
|
|
|
|
|
impl Copy for Solution {}
|
|
|
|
|
|
|
|
impl Clone for Solution {
|
|
|
|
fn clone(&self) -> Self {
|
2023-07-05 00:11:27 -07:00
|
|
|
*self
|
2020-08-15 00:14:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Solution {}
|
|
|
|
|
2023-12-05 17:57:01 -08:00
|
|
|
#[cfg(any(test, feature = "proptest-impl"))]
|
|
|
|
impl Default for Solution {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self([0; SOLUTION_SIZE])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 00:14:07 -07:00
|
|
|
impl ZcashSerialize for Solution {
|
2021-11-05 11:24:24 -07:00
|
|
|
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
|
|
|
|
zcash_serialize_bytes(&self.0.to_vec(), writer)
|
2020-08-15 00:14:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ZcashDeserialize for Solution {
|
|
|
|
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
|
2021-11-05 11:24:24 -07:00
|
|
|
let solution: Vec<u8> = (&mut reader).zcash_deserialize_into()?;
|
2024-01-11 06:41:01 -08:00
|
|
|
Self::from_bytes(&solution)
|
2020-08-15 00:14:07 -07:00
|
|
|
}
|
|
|
|
}
|