Skip to content
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

add(rpc): Adds getblockheader RPC method #8967

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
20 changes: 20 additions & 0 deletions zebra-chain/src/sapling/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ impl TryFrom<[u8; 32]> for Root {
}
}

impl ToHex for &Root {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
<[u8; 32]>::from(*self).encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
<[u8; 32]>::from(*self).encode_hex_upper()
}
}

impl ToHex for Root {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex_upper()
}
}

impl ZcashSerialize for Root {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_all(&<[u8; 32]>::from(*self)[..])?;
Expand Down
22 changes: 22 additions & 0 deletions zebra-chain/src/work/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,28 @@ impl CompactDifficulty {

Ok(difficulty)
}

/// Returns a floating-point number representing a difficulty as a multiple
/// of the minimum difficulty for the provided network.
pub fn relative_to_network(&self, network: &Network) -> f64 {
let network_difficulty = network.target_difficulty_limit().to_compact();

let [mut n_shift, ..] = self.0.to_be_bytes();
let [n_shift_amount, ..] = network_difficulty.0.to_be_bytes();
let mut d_diff = f64::from(network_difficulty.0 << 8) / f64::from(self.0 << 8);

while n_shift < n_shift_amount {
d_diff *= 256.0;
n_shift += 1;
}

while n_shift > n_shift_amount {
d_diff /= 256.0;
n_shift -= 1;
}

d_diff
}
Comment on lines +296 to +314
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we credit it in a comment or something?

}

impl fmt::Debug for CompactDifficulty {
Expand Down
22 changes: 21 additions & 1 deletion zebra-chain/src/work/equihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{fmt, io};

use hex::ToHex;
use serde_big_array::BigArray;

use crate::{
Expand Down Expand Up @@ -112,7 +113,6 @@ impl Solution {
}

/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
#[cfg(feature = "getblocktemplate-rpcs")]
pub fn for_proposal() -> Self {
// TODO: Accept network as an argument, and if it's Regtest, return the shorter null solution.
Self::Common([0; SOLUTION_SIZE])
Expand Down Expand Up @@ -195,3 +195,23 @@ impl ZcashDeserialize for Solution {
Self::from_bytes(&solution)
}
}

impl ToHex for &Solution {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
self.value().encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
self.value().encode_hex_upper()
}
}

impl ToHex for Solution {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex_upper()
}
}
Loading
Loading