Skip to content

Commit

Permalink
fixes vector test, addresses remaining differences with zcashd (excep…
Browse files Browse the repository at this point in the history
…t the `chainwork` field), updates snapshots, and avoids a possible panic when there's a chain reorg between state queries.
  • Loading branch information
arya2 committed Oct 28, 2024
1 parent 068762b commit 80ea07b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 41 deletions.
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
}
}

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()
}
}
58 changes: 33 additions & 25 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use zebra_chain::{
subtree::NoteCommitmentSubtreeIndex,
transaction::{self, SerializedTransaction, Transaction, UnminedTx},
transparent::{self, Address},
work::difficulty::ExpandedDifficulty,
work::{
difficulty::{CompactDifficulty, ExpandedDifficulty},
equihash::Solution,
},
};
use zebra_node_services::mempool;
use zebra_state::{HashOrHeight, MinedTx, OutputIndex, OutputLocation, TransactionLocation};
Expand Down Expand Up @@ -936,6 +939,7 @@ where
) -> BoxFuture<Result<GetBlockHeader>> {
let state = self.state.clone();
let verbose = verbose.unwrap_or(true);
let network = self.network.clone();

async move {
let hash_or_height: HashOrHeight = hash_or_height.parse().map_server_error()?;
Expand All @@ -956,11 +960,6 @@ where
let response = if !verbose {
GetBlockHeader::Raw(HexData(header.zcash_serialize_to_vec().map_server_error()?))
} else {
// TODO:
// - Return block hash and height in BlockHeader response
// - Add the block height to the `getblock` response as well
// - Add a parameter to the BlockHeader request to indicate that the caller is interested in the next block hash?

let zebra_state::ReadResponse::SaplingTree(sapling_tree) = state
.clone()
.oneshot(zebra_state::ReadRequest::SaplingTree(hash_or_height))
Expand All @@ -970,8 +969,9 @@ where
panic!("unexpected response to SaplingTree request")
};

// TODO: Double-check that there's an empty Sapling root at Genesis.
let sapling_tree = sapling_tree.expect("should always have a sapling root");
// This could be `None` if there's a chain reorg between state queries.
let sapling_tree =
sapling_tree.ok_or_server_error("missing sapling tree for block")?;

let zebra_state::ReadResponse::Depth(depth) = state
.clone()
Expand All @@ -995,8 +995,15 @@ where
let mut nonce = *header.nonce;
nonce.reverse();

let mut final_sapling_root: [u8; 32] = sapling_tree.root().into();
final_sapling_root.reverse();
let final_sapling_root: [u8; 32] = if sapling_tree.position().is_some() {
let mut root: [u8; 32] = sapling_tree.root().into();
root.reverse();
root
} else {
[0; 32]
};

let difficulty = header.difficulty_threshold.relative_to_network(&network);

let block_header = GetBlockHeaderObject {
hash: GetBlockHash(hash),
Expand All @@ -1007,13 +1014,9 @@ where
final_sapling_root,
time: header.time.timestamp(),
nonce,
solution: header.solution,
bits: header.difficulty_threshold,
difficulty: header
.difficulty_threshold
.to_expanded()
.ok_or_server_error(
"could not convert compact difficulty to expanded difficulty",
)?,
difficulty,
previous_block_hash: GetBlockHash(header.previous_block_hash),
next_block_hash: next_block_hash.map(GetBlockHash),
};
Expand Down Expand Up @@ -1733,7 +1736,7 @@ impl Default for GetBlock {
/// Response to a `getblockheader` RPC request.
///
/// See the notes for the [`Rpc::get_block_header`] method.
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
#[serde(untagged)]
pub enum GetBlockHeader {
/// The request block, hex-encoded.
Expand All @@ -1743,7 +1746,7 @@ pub enum GetBlockHeader {
Object(Box<GetBlockHeaderObject>),
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
/// Verbose response to a `getblockheader` RPC request.
///
/// See the notes for the [`Rpc::get_block_header`] method.
Expand Down Expand Up @@ -1772,24 +1775,28 @@ pub struct GetBlockHeaderObject {
/// The block time of the requested block header in non-leap seconds since Jan 1 1970 GMT.
pub time: i64,

/// The nonce of the requested block header
/// The nonce of the requested block header.
#[serde(with = "hex")]
pub nonce: [u8; 32],

/// The difficulty threshold of the requested block header displayed in compact form.
/// The Equihash solution in the requested block header.
#[serde(with = "hex")]
pub bits: zebra_chain::work::difficulty::CompactDifficulty,
solution: Solution,

/// The difficulty threshold of the requested block header displayed in expanded form.
/// The difficulty threshold of the requested block header displayed in compact form.
#[serde(with = "hex")]
pub difficulty: zebra_chain::work::difficulty::ExpandedDifficulty,
pub bits: CompactDifficulty,

/// Floating point number that represents the difficulty limit for this block as a multiple
/// of the minimum difficulty for the network.
pub difficulty: f64,

/// The previous block hash of the requested block header.
#[serde(rename = "previousblockhash")]
pub previous_block_hash: GetBlockHash,

/// The next block hash after the requested block header.
#[serde(rename = "nextblockhash")]
#[serde(rename = "nextblockhash", skip_serializing_if = "Option::is_none")]
pub next_block_hash: Option<GetBlockHash>,
}

Expand All @@ -1812,8 +1819,9 @@ impl Default for GetBlockHeaderObject {
final_sapling_root: Default::default(),
time: 0,
nonce: [0; 32],
solution: Solution::for_proposal(),
bits: difficulty.to_compact(),
difficulty,
difficulty: 1.0,
previous_block_hash: Default::default(),
next_block_hash: Default::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ expression: block
"height": 1,
"version": 4,
"merkleroot": "851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609",
"finalsaplingroot": "fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
"finalsaplingroot": "0000000000000000000000000000000000000000000000000000000000000000",
"time": 1477671596,
"nonce": "9057977ea6d4ae867decc96359fcf2db8cdebcbfb3bd549de4f21f16cfe83475",
"solution": "002b2ee0d2f5d0c1ebf5a265b6f5b428f2fdc9aaea07078a6c5cab4f1bbfcd56489863deae6ea3fd8d3d0762e8e5295ff2670c9e90d8e8c68a54a40927e82a65e1d44ced20d835818e172d7b7f5ffe0245d0c3860a3f11af5658d68b6a7253b4684ffef5242fefa77a0bfc3437e8d94df9dc57510f5a128e676dd9ddf23f0ef75b460090f507499585541ab53a470c547ea02723d3a979930941157792c4362e42d3b9faca342a5c05a56909b046b5e92e2870fca7c932ae2c2fdd97d75b6e0ecb501701c1250246093c73efc5ec2838aeb80b59577741aa5ccdf4a631b79f70fc419e28714fa22108d991c29052b2f5f72294c355b57504369313470ecdd8e0ae97fc48e243a38c2ee7315bb05b7de9602047e97449c81e46746513221738dc729d7077a1771cea858865d85261e71e82003ccfbba2416358f023251206d6ef4c5596bc35b2b5bce3e9351798aa2c9904723034e5815c7512d260cc957df5db6adf9ed7272483312d1e68c60955a944e713355089876a704aef06359238f6de5a618f7bd0b4552ba72d05a6165e582f62d55ff2e1b76991971689ba3bee16a520fd85380a6e5a31de4dd4654d561101ce0ca390862d5774921eae2c284008692e9e08562144e8aa1f399a9d3fab0c4559c1f12bc945e626f7a89668613e8829767f4116ee9a4f832cf7c3ade3a7aba8cb04de39edd94d0d05093ed642adf9fbd9d373a80832ffd1c62034e4341546b3515f0e42e6d8570393c6754be5cdb7753b4709527d3f164aebf3d315934f7b3736a1b31052f6cc5699758950331163b3df05b9772e9bf99c8c77f8960e10a15edb06200106f45742d740c422c86b7e4f5a52d3732aa79ee54cfc92f76e03c268ae226477c19924e733caf95b8f350233a5312f4ed349d3ad76f032358f83a6d0d6f83b2a456742aad7f3e615fa72286300f0ea1c9793831ef3a5a4ae08640a6e32f53d1cba0be284b25e923d0d110ba227e54725632efcbbe17c05a9cde976504f6aece0c461b562cfae1b85d5f6782ee27b3e332ac0775f681682ce524b32889f1dc4231226f1aada0703beaf8d41732c9647a0a940a86f8a1be7f239c44fcaa7ed7a055506bdbe1df848f9e047226bee1b6d788a03f6e352eead99b419cfc41741942dbeb7a5c55788d5a3e636d8aab7b36b4db71d16700373bbc1cdeba8f9b1db10bf39a621bc737ea4f4e333698d6e09b51ac7a97fb6fd117ccad1d6b6b3a7451699d5bfe448650396d7b58867b3b0872be13ad0b43da267df0ad77025155f04e20c56d6a9befb3e9c7d23b82cbf3a534295ebda540682cc81be9273781b92519c858f9c25294fbacf75c3b3c15bda6d36de1c83336f93e96910dbdcb190d6ef123c98565ff6df1e903f57d4e4df167ba6b829d6d9713eb2126b0cf869940204137babcc6a1b7cb2f0b94318a7460e5d1a605c249bd2e72123ebad332332c18adcb285ed8874dbde084ebcd4f744465350d57110f037fffed1569d642c258749e65b0d13e117eaa37014a769b5ab479b7c77178880e77099f999abe712e543dbbf626ca9bcfddc42ff2f109d21c8bd464894e55ae504fdf81e1a7694180225da7dac8879abd1036cf26bb50532b8cf138b337a1a1bd1a43f8dd70b7399e2690c8e7a5a1fe099026b8f2a6f65fc0dbedda15ba65e0abd66c7176fb426980549892b4817de78e345a7aeab05744c3def4a2f283b4255b02c91c1af7354a368c67a11703c642a385c7453131ce3a78b24c5e22ab7e136a38498ce82082181884418cb4d6c2920f258a3ad20cfbe7104af1c6c6cb5e58bf29a9901721ad19c0a260cd09a3a772443a45aea4a5c439a95834ef5dc2e26343278947b7b796f796ae9bcadb29e2899a1d7313e6f7bfb6f8b",
"bits": "1f07ffff",
"difficulty": "0007ffff00000000000000000000000000000000000000000000000000000000",
"difficulty": 1.0,
"previousblockhash": "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08",
"nextblockhash": "0002a26c902619fc964443264feb16f1e3e2d71322fc53dcb81cc5d797e273ed"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ expression: block
"height": 1,
"version": 4,
"merkleroot": "f37e9f691fffb635de0999491d906ee85ba40cd36dae9f6e5911a8277d7c5f75",
"finalsaplingroot": "fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
"finalsaplingroot": "0000000000000000000000000000000000000000000000000000000000000000",
"time": 1477674473,
"nonce": "0000e5739438a096ca89cde16bcf6001e0c5a7ce6f7c591d26314c26c2560000",
"solution": "0053f4438864bc5d6dfc009d4bba545ac5e5feaaf46f9455b975b02115f842a966e26517ce678f1c074d09cc8d0049a190859eb505af5f3e760312fbbe54da115db2bc03c96408f39b679891790b539d2d9d17a801dc6af9af14ca3f6ba060edce2a1dd45aa45f11fe37dbaf1eb2647ae7c393f6680c3d5d7e53687e34530f48edf58924a04d3e0231c150b1c8218998f674bc171edd222bcb4ac4ba4ea52d7baa86399f371d5284043e1e166f9069dd0f2904ff94c7922a70fa7c660e0553cc40a20d9ee08eb3f47278485801ddae9c270411360773f0b74e03db2d92c50952c9bd4924bbca2a260e1235e99df51fe71e75744232f2d641ef94f394110a5ad05f51a057e4cb515b92c16cb1404a8cdcc43d4a4bb2caa54ca35dccf41aa7d832da65123b7029223c46ed2a13387d598d445435d3cb32fdad9e27672903864c90d86353b162033078327b5b7aaffc89b40096ae004f2d5c6bd2c99188574348518db66e9b6020f93f12ee1c06f7b00fe346fefceaffb1da9e3cdf08285057f549733eb10825737fcd1431bfdfb155f323f24e95a869212baacf445b30f2670206645779110e6547d5da90a5f2fe5151da911d5ecd5a833023661d1356b6c395d85968947678d53efd4db7b06f23b21125e74492644277ea0c1131b80d6a4e3e8093b82332556fbb3255a55ac3f0b7e4844c0e12bf577c37fd02323ae5ef4781772ed501d63b568032a3d31576c5104a48c01ac54f715286932351a8adc8cf2467a84a0572e99f366ee00f82c3735545fd4bb941d591ce70070425a81304272db89887949bc7dd8236bb7e82190f9815da938cd6e8fec7660e91354326a7a9bfe38120e97997fca3c289d54513ed00286c2b825fbe84f91a39528f335674b5e957425a6edfdd00f2feb2c2df575616197998c1e964e069875d4d934f419a9b02b100848d023b76d47bd4e284c3895ef9227a40d8ea8826e86c7155d6aa95b8f9175812523a32cd611efc700688e03f7c245c5bff01718281b5d75cefe8318b2c08962236b14a0bf79534c203df735fd9cced97cbae07c2b4ee9cda8c9993f3f6277ff3fec261fb94d3961c4befe4b0893dcf67b312c7d8d6ff7adc8539cb2b1d3534fccf109efddd07a9f1e77b94ab1e505b164221dca1c34621b1e9d234c31a032a401267d95f65b800d579a2482638dfeade804149c81e95d7ef5510ac0b6212231506b1c635a2e1d2f0c9712989f9f246762fadb4c55c20f707dcc0e510a33e9465fc5d5bdbfa524dab0d7a1c6a1baaa36869cf542aa2257c5c44ef07547a570343442c6091e13bc04d559dc0e6db5b001861914bf956816edce2a86b274bd97f27e2dbb08608c16a3e5d8595952faa91fb162d7fa6a7a47e849a1ad8fab3ba620ee3295a04fe13e5fb655ac92ae60d01020b8999526af8d56b28733e69c9ffb285de27c61edc0bf62261ac0787eff347d0fcd62257301ede9603106ea41650a3e3119bd5c4e86a7f6a3f00934f3a545f7f21d41699f3e35d38cf925a8bdaf2bf7eedea11c31c3d8bf6c527c77c6378281cdf02211a58fa5e46d28d7e7c5fb79d69b31703fd752395da115845952cf99aaeb2155c2ab951a69f67d938f223185567e52cfa3e57b62c790bf78674c4b02c12b7d3225fe8f705b408ba11c24245b3924482e2f3480994461b550641a88cd941d371139f3498afacdcba1249631402b20695760eaada5376e68df0e45139c410700effc9420dc3726515e7fcb3f349320f30511451964bd9b6530682efec65910ceb548aa2ab05ac3309e803161697213631ae8e13cc7d223ac28446c1bf94a19a8782ac16ff57df7ee4f10fb6e488c02c68d6b6dee6987f6d2c39227da366c59f54ff67e312ca530e7c467c3dc8",
"bits": "2007ffff",
"difficulty": "07ffff0000000000000000000000000000000000000000000000000000000000",
"difficulty": 1.0,
"previousblockhash": "05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38",
"nextblockhash": "00f1a49e54553ac3ef735f2eb1d8247c9a87c22a47dbd7823ae70adcd6c21a18"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ expression: block
"height": 1,
"version": 4,
"merkleroot": "851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609",
"finalsaplingroot": "fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
"finalsaplingroot": "0000000000000000000000000000000000000000000000000000000000000000",
"time": 1477671596,
"nonce": "9057977ea6d4ae867decc96359fcf2db8cdebcbfb3bd549de4f21f16cfe83475",
"solution": "002b2ee0d2f5d0c1ebf5a265b6f5b428f2fdc9aaea07078a6c5cab4f1bbfcd56489863deae6ea3fd8d3d0762e8e5295ff2670c9e90d8e8c68a54a40927e82a65e1d44ced20d835818e172d7b7f5ffe0245d0c3860a3f11af5658d68b6a7253b4684ffef5242fefa77a0bfc3437e8d94df9dc57510f5a128e676dd9ddf23f0ef75b460090f507499585541ab53a470c547ea02723d3a979930941157792c4362e42d3b9faca342a5c05a56909b046b5e92e2870fca7c932ae2c2fdd97d75b6e0ecb501701c1250246093c73efc5ec2838aeb80b59577741aa5ccdf4a631b79f70fc419e28714fa22108d991c29052b2f5f72294c355b57504369313470ecdd8e0ae97fc48e243a38c2ee7315bb05b7de9602047e97449c81e46746513221738dc729d7077a1771cea858865d85261e71e82003ccfbba2416358f023251206d6ef4c5596bc35b2b5bce3e9351798aa2c9904723034e5815c7512d260cc957df5db6adf9ed7272483312d1e68c60955a944e713355089876a704aef06359238f6de5a618f7bd0b4552ba72d05a6165e582f62d55ff2e1b76991971689ba3bee16a520fd85380a6e5a31de4dd4654d561101ce0ca390862d5774921eae2c284008692e9e08562144e8aa1f399a9d3fab0c4559c1f12bc945e626f7a89668613e8829767f4116ee9a4f832cf7c3ade3a7aba8cb04de39edd94d0d05093ed642adf9fbd9d373a80832ffd1c62034e4341546b3515f0e42e6d8570393c6754be5cdb7753b4709527d3f164aebf3d315934f7b3736a1b31052f6cc5699758950331163b3df05b9772e9bf99c8c77f8960e10a15edb06200106f45742d740c422c86b7e4f5a52d3732aa79ee54cfc92f76e03c268ae226477c19924e733caf95b8f350233a5312f4ed349d3ad76f032358f83a6d0d6f83b2a456742aad7f3e615fa72286300f0ea1c9793831ef3a5a4ae08640a6e32f53d1cba0be284b25e923d0d110ba227e54725632efcbbe17c05a9cde976504f6aece0c461b562cfae1b85d5f6782ee27b3e332ac0775f681682ce524b32889f1dc4231226f1aada0703beaf8d41732c9647a0a940a86f8a1be7f239c44fcaa7ed7a055506bdbe1df848f9e047226bee1b6d788a03f6e352eead99b419cfc41741942dbeb7a5c55788d5a3e636d8aab7b36b4db71d16700373bbc1cdeba8f9b1db10bf39a621bc737ea4f4e333698d6e09b51ac7a97fb6fd117ccad1d6b6b3a7451699d5bfe448650396d7b58867b3b0872be13ad0b43da267df0ad77025155f04e20c56d6a9befb3e9c7d23b82cbf3a534295ebda540682cc81be9273781b92519c858f9c25294fbacf75c3b3c15bda6d36de1c83336f93e96910dbdcb190d6ef123c98565ff6df1e903f57d4e4df167ba6b829d6d9713eb2126b0cf869940204137babcc6a1b7cb2f0b94318a7460e5d1a605c249bd2e72123ebad332332c18adcb285ed8874dbde084ebcd4f744465350d57110f037fffed1569d642c258749e65b0d13e117eaa37014a769b5ab479b7c77178880e77099f999abe712e543dbbf626ca9bcfddc42ff2f109d21c8bd464894e55ae504fdf81e1a7694180225da7dac8879abd1036cf26bb50532b8cf138b337a1a1bd1a43f8dd70b7399e2690c8e7a5a1fe099026b8f2a6f65fc0dbedda15ba65e0abd66c7176fb426980549892b4817de78e345a7aeab05744c3def4a2f283b4255b02c91c1af7354a368c67a11703c642a385c7453131ce3a78b24c5e22ab7e136a38498ce82082181884418cb4d6c2920f258a3ad20cfbe7104af1c6c6cb5e58bf29a9901721ad19c0a260cd09a3a772443a45aea4a5c439a95834ef5dc2e26343278947b7b796f796ae9bcadb29e2899a1d7313e6f7bfb6f8b",
"bits": "1f07ffff",
"difficulty": "0007ffff00000000000000000000000000000000000000000000000000000000",
"difficulty": 1.0,
"previousblockhash": "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08",
"nextblockhash": "0002a26c902619fc964443264feb16f1e3e2d71322fc53dcb81cc5d797e273ed"
}
Loading

0 comments on commit 80ea07b

Please sign in to comment.