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 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
24 changes: 24 additions & 0 deletions zebra-chain/src/work/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,30 @@ impl CompactDifficulty {

Ok(difficulty)
}

/// Returns a floating-point number representing a difficulty as a multiple
/// of the minimum difficulty for the provided network.
// Copied from <https://github.com/zcash/zcash/blob/99ad6fdc3a549ab510422820eea5e5ce9f60a5fd/src/rpc/blockchain.cpp#L34-L74>
arya2 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Explain here what this ported code is doing and why, request help to do so with the ECC team.
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
}
mpguerra marked this conversation as resolved.
Show resolved Hide resolved
}

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