use std::{fmt, io};
use serde_big_array::BigArray;
use crate::{
block::Header,
serialization::{
zcash_serialize_bytes, SerializationError, ZcashDeserialize, ZcashDeserializeInto,
ZcashSerialize,
},
};
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
#[error("invalid equihash solution for BlockHeader")]
pub struct Error(#[from] equihash::Error);
pub(crate) const SOLUTION_SIZE: usize = 1344;
#[derive(Deserialize, Serialize)]
pub struct Solution(#[serde(with = "BigArray")] pub [u8; SOLUTION_SIZE]);
impl Solution {
pub const INPUT_LENGTH: usize = 4 + 32 * 3 + 4 * 2;
#[allow(clippy::unwrap_in_result)]
pub fn check(&self, header: &Header) -> Result<(), Error> {
let n = 200;
let k = 9;
let nonce = &header.nonce;
let solution = &self.0;
let mut input = Vec::new();
header
.zcash_serialize(&mut input)
.expect("serialization into a vec can't fail");
let input = &input[0..Solution::INPUT_LENGTH];
equihash::is_valid_solution(n, k, input, nonce.as_ref(), solution)?;
Ok(())
}
#[cfg(feature = "getblocktemplate-rpcs")]
pub fn for_proposal() -> Self {
Self([0; SOLUTION_SIZE])
}
}
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()
}
}
impl Copy for Solution {}
impl Clone for Solution {
fn clone(&self) -> Self {
*self
}
}
impl Eq for Solution {}
impl ZcashSerialize for Solution {
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
zcash_serialize_bytes(&self.0.to_vec(), writer)
}
}
impl ZcashDeserialize for Solution {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let solution: Vec<u8> = (&mut reader).zcash_deserialize_into()?;
if solution.len() != SOLUTION_SIZE {
return Err(SerializationError::Parse(
"incorrect equihash solution size",
));
}
let mut bytes = [0; SOLUTION_SIZE];
bytes.copy_from_slice(&solution);
Ok(Self(bytes))
}
}