Skip to content

Commit

Permalink
add(rpc): Adds getblockheader RPC method (#8967)
Browse files Browse the repository at this point in the history
* Adds getblockheader RPC method

* Updates snapshots, adds hash/height/next_block_hash fields to verbose response

* updates getblock snapshot

* updates getblockheader response type to hex-encode fields, adds ToHex impl for sapling::tree::Root, adds snapshot and vector tests for new RPC method, adds snapshots.

* rustfmt

* fixes snapshots, matches zcashd more closely

* fixes vectors test

* updates lwd failure messages (probably doesn't matter, but seems better to handle it now than risk debugging it later)

* fixes getblock_rpc test, fixes/reverses finalsaplingroot field byte-order.

* fixes vector test, addresses remaining differences with zcashd (except the `chainwork` field), updates snapshots, and avoids a possible panic when there's a chain reorg between state queries.

* Adds a comment noting that the `relative_to_network()` method was copied from zcashd

* Apply suggestions from code review

Co-authored-by: Alfredo Garcia <[email protected]>

---------

Co-authored-by: Alfredo Garcia <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 1dfac40 commit 1e46914
Show file tree
Hide file tree
Showing 21 changed files with 609 additions and 42 deletions.
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>
// 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
}
}

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

0 comments on commit 1e46914

Please sign in to comment.