Skip to content

Commit 1e46914

Browse files
arya2oxarbitragemergify[bot]
authored
add(rpc): Adds getblockheader RPC method (#8967)
* 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>
1 parent 1dfac40 commit 1e46914

21 files changed

+609
-42
lines changed

zebra-chain/src/sapling/tree.rs

+20
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ impl TryFrom<[u8; 32]> for Root {
147147
}
148148
}
149149

150+
impl ToHex for &Root {
151+
fn encode_hex<T: FromIterator<char>>(&self) -> T {
152+
<[u8; 32]>::from(*self).encode_hex()
153+
}
154+
155+
fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
156+
<[u8; 32]>::from(*self).encode_hex_upper()
157+
}
158+
}
159+
160+
impl ToHex for Root {
161+
fn encode_hex<T: FromIterator<char>>(&self) -> T {
162+
(&self).encode_hex()
163+
}
164+
165+
fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
166+
(&self).encode_hex_upper()
167+
}
168+
}
169+
150170
impl ZcashSerialize for Root {
151171
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
152172
writer.write_all(&<[u8; 32]>::from(*self)[..])?;

zebra-chain/src/work/difficulty.rs

+24
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,30 @@ impl CompactDifficulty {
290290

291291
Ok(difficulty)
292292
}
293+
294+
/// Returns a floating-point number representing a difficulty as a multiple
295+
/// of the minimum difficulty for the provided network.
296+
// Copied from <https://github.com/zcash/zcash/blob/99ad6fdc3a549ab510422820eea5e5ce9f60a5fd/src/rpc/blockchain.cpp#L34-L74>
297+
// TODO: Explain here what this ported code is doing and why, request help to do so with the ECC team.
298+
pub fn relative_to_network(&self, network: &Network) -> f64 {
299+
let network_difficulty = network.target_difficulty_limit().to_compact();
300+
301+
let [mut n_shift, ..] = self.0.to_be_bytes();
302+
let [n_shift_amount, ..] = network_difficulty.0.to_be_bytes();
303+
let mut d_diff = f64::from(network_difficulty.0 << 8) / f64::from(self.0 << 8);
304+
305+
while n_shift < n_shift_amount {
306+
d_diff *= 256.0;
307+
n_shift += 1;
308+
}
309+
310+
while n_shift > n_shift_amount {
311+
d_diff /= 256.0;
312+
n_shift -= 1;
313+
}
314+
315+
d_diff
316+
}
293317
}
294318

295319
impl fmt::Debug for CompactDifficulty {

zebra-chain/src/work/equihash.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::{fmt, io};
44

5+
use hex::ToHex;
56
use serde_big_array::BigArray;
67

78
use crate::{
@@ -112,7 +113,6 @@ impl Solution {
112113
}
113114

114115
/// Returns a [`Solution`] of `[0; SOLUTION_SIZE]` to be used in block proposals.
115-
#[cfg(feature = "getblocktemplate-rpcs")]
116116
pub fn for_proposal() -> Self {
117117
// TODO: Accept network as an argument, and if it's Regtest, return the shorter null solution.
118118
Self::Common([0; SOLUTION_SIZE])
@@ -195,3 +195,23 @@ impl ZcashDeserialize for Solution {
195195
Self::from_bytes(&solution)
196196
}
197197
}
198+
199+
impl ToHex for &Solution {
200+
fn encode_hex<T: FromIterator<char>>(&self) -> T {
201+
self.value().encode_hex()
202+
}
203+
204+
fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
205+
self.value().encode_hex_upper()
206+
}
207+
}
208+
209+
impl ToHex for Solution {
210+
fn encode_hex<T: FromIterator<char>>(&self) -> T {
211+
(&self).encode_hex()
212+
}
213+
214+
fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
215+
(&self).encode_hex_upper()
216+
}
217+
}

0 commit comments

Comments
 (0)