zebra_chain/work/difficulty.rs
1//! Block difficulty data structures and calculations
2//!
3//! The block difficulty "target threshold" is stored in the block header as a
4//! 32-bit `CompactDifficulty`. The `block::Hash` must be less than or equal
5//! to the `ExpandedDifficulty` threshold, when represented as a 256-bit integer
6//! in little-endian order.
7//!
8//! The target threshold is also used to calculate the `Work` for each block.
9//! The block work is used to find the chain with the greatest total work. Each
10//! block's work value depends on the fixed threshold in the block header, not
11//! the actual work represented by the block header hash.
12
13use std::{
14 cmp::{Ordering, PartialEq, PartialOrd},
15 fmt,
16 iter::Sum,
17 ops::{Add, Div, Mul},
18};
19
20use hex::{FromHex, ToHex};
21
22use crate::{block, parameters::Network, serialization::BytesInDisplayOrder, BoxError};
23
24pub use crate::work::u256::U256;
25
26#[cfg(any(test, feature = "proptest-impl"))]
27mod arbitrary;
28#[cfg(test)]
29mod tests;
30
31/// A 32-bit "compact bits" value, which represents the difficulty threshold for
32/// a block header.
33///
34/// Used for:
35/// - checking the `difficulty_threshold` value in the block header,
36/// - calculating the 256-bit `ExpandedDifficulty` threshold, for comparison
37/// with the block header hash, and
38/// - calculating the block work.
39///
40/// # Consensus
41///
42/// This is a floating-point encoding, with a 24-bit signed mantissa,
43/// an 8-bit exponent, an offset of 3, and a radix of 256.
44/// (IEEE 754 32-bit floating-point values use a separate sign bit, an implicit
45/// leading mantissa bit, an offset of 127, and a radix of 2.)
46///
47/// The precise bit pattern of a `CompactDifficulty` value is
48/// consensus-critical, because it is used for the `difficulty_threshold` field,
49/// which is:
50/// - part of the `BlockHeader`, which is used to create the
51/// `block::Hash`, and
52/// - bitwise equal to the median `ExpandedDifficulty` value of recent blocks,
53/// when encoded to `CompactDifficulty` using the specified conversion
54/// function.
55///
56/// Without these consensus rules, some `ExpandedDifficulty` values would have
57/// multiple equivalent `CompactDifficulty` values, due to redundancy in the
58/// floating-point format.
59///
60/// > Deterministic conversions between a target threshold and a “compact" nBits value
61/// > are not fully defined in the Bitcoin documentation, and so we define them here:
62/// > (see equations in the Zcash Specification [section 7.7.4])
63///
64/// [section 7.7.4]: https://zips.z.cash/protocol/protocol.pdf#nbits
65#[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
66#[cfg_attr(any(test, feature = "proptest-impl"), derive(Default))]
67pub struct CompactDifficulty(pub(crate) u32);
68
69/// An invalid CompactDifficulty value, for testing.
70pub const INVALID_COMPACT_DIFFICULTY: CompactDifficulty = CompactDifficulty(u32::MAX);
71
72/// A 256-bit unsigned "expanded difficulty" value.
73///
74/// Used as a target threshold for the difficulty of a `block::Hash`.
75///
76/// # Consensus
77///
78/// The precise bit pattern of an `ExpandedDifficulty` value is
79/// consensus-critical, because it is compared with the `block::Hash`.
80///
81/// Note that each `CompactDifficulty` value can be converted from a
82/// range of `ExpandedDifficulty` values, because the precision of
83/// the floating-point format requires rounding on conversion.
84///
85/// Therefore, consensus-critical code must perform the specified
86/// conversions to `CompactDifficulty`, even if the original
87/// `ExpandedDifficulty` values are known.
88///
89/// Callers should avoid constructing `ExpandedDifficulty` zero
90/// values, because they are rejected by the consensus rules,
91/// and cause some conversion functions to panic.
92///
93/// > The difficulty filter is unchanged from Bitcoin, and is calculated using SHA-256d on the
94/// > whole block header (including solutionSize and solution). The result is interpreted as a
95/// > 256-bit integer represented in little-endian byte order, which MUST be less than or equal
96/// > to the target threshold given by ToTarget(nBits).
97///
98/// Zcash Specification [section 7.7.2].
99///
100/// [section 7.7.2]: https://zips.z.cash/protocol/protocol.pdf#difficulty
101//
102// TODO: Use NonZeroU256, when available
103#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
104pub struct ExpandedDifficulty(U256);
105
106/// A 128-bit unsigned "Work" value.
107///
108/// Used to calculate the total work for each chain of blocks.
109///
110/// # Consensus
111///
112/// The relative value of `Work` is consensus-critical, because it is used to
113/// choose the best chain. But its precise value and bit pattern are not
114/// consensus-critical.
115///
116/// We calculate work values according to the Zcash specification, but store
117/// them as u128, rather than the implied u256. We don't expect the total chain
118/// work to ever exceed 2^128. The current total chain work for Zcash is 2^58,
119/// and Bitcoin adds around 2^91 work per year. (Each extra bit represents twice
120/// as much work.)
121///
122/// > a node chooses the “best” block chain visible to it by finding the chain of valid blocks
123/// > with the greatest total work. The work of a block with value nBits for the nBits field in
124/// > its block header is defined as `floor(2^256 / (ToTarget(nBits) + 1))`.
125///
126/// Zcash Specification [section 7.7.5].
127///
128/// [section 7.7.5]: https://zips.z.cash/protocol/protocol.pdf#workdef
129#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)]
130pub struct Work(u128);
131
132impl Work {
133 /// Returns a value representing no work.
134 pub fn zero() -> Self {
135 Self(0)
136 }
137
138 /// Return the inner `u128` value.
139 pub fn as_u128(self) -> u128 {
140 self.0
141 }
142}
143
144impl fmt::Debug for Work {
145 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146 // There isn't a standard way to show different representations of the
147 // same value
148 f.debug_tuple("Work")
149 // Use hex, because expanded difficulty is in hex.
150 .field(&format_args!("{:#x}", self.0))
151 // Use decimal, to compare with zcashd
152 .field(&format_args!("{}", self.0))
153 // Use log2, to compare with zcashd
154 .field(&format_args!("{:.5}", (self.0 as f64).log2()))
155 .finish()
156 }
157}
158
159impl CompactDifficulty {
160 /// CompactDifficulty exponent base.
161 const BASE: u32 = 256;
162
163 /// CompactDifficulty exponent offset.
164 const OFFSET: i32 = 3;
165
166 /// CompactDifficulty floating-point precision.
167 const PRECISION: u32 = 24;
168
169 /// CompactDifficulty sign bit, part of the signed mantissa.
170 const SIGN_BIT: u32 = 1 << (CompactDifficulty::PRECISION - 1);
171
172 /// CompactDifficulty unsigned mantissa mask.
173 ///
174 /// Also the maximum unsigned mantissa value.
175 const UNSIGNED_MANTISSA_MASK: u32 = CompactDifficulty::SIGN_BIT - 1;
176
177 /// Calculate the ExpandedDifficulty for a compact representation.
178 ///
179 /// See `ToTarget()` in the Zcash Specification, and `CheckProofOfWork()` in
180 /// zcashd:
181 /// <https://zips.z.cash/protocol/protocol.pdf#nbits>
182 ///
183 /// Returns None for negative, zero, and overflow values. (zcashd rejects
184 /// these values, before comparing the hash.)
185 #[allow(clippy::unwrap_in_result)]
186 pub fn to_expanded(self) -> Option<ExpandedDifficulty> {
187 // The constants for this floating-point representation.
188 // Alias the struct constants here, so the code is easier to read.
189 const BASE: u32 = CompactDifficulty::BASE;
190 const OFFSET: i32 = CompactDifficulty::OFFSET;
191 const PRECISION: u32 = CompactDifficulty::PRECISION;
192 const SIGN_BIT: u32 = CompactDifficulty::SIGN_BIT;
193 const UNSIGNED_MANTISSA_MASK: u32 = CompactDifficulty::UNSIGNED_MANTISSA_MASK;
194
195 // Negative values in this floating-point representation.
196 // 0 if (x & 2^23 == 2^23)
197 // zcashd rejects negative values without comparing the hash.
198 if self.0 & SIGN_BIT == SIGN_BIT {
199 return None;
200 }
201
202 // The components of the result
203 // The fractional part of the floating-point number
204 // x & (2^23 - 1)
205 let mantissa = self.0 & UNSIGNED_MANTISSA_MASK;
206
207 // The exponent for the multiplier in the floating-point number
208 // 256^(floor(x/(2^24)) - 3)
209 //
210 // The i32 conversion is safe, because we've just divided self by 2^24.
211 let exponent = i32::try_from(self.0 >> PRECISION).expect("fits in i32") - OFFSET;
212
213 // Normalise the mantissa and exponent before multiplying.
214 //
215 // zcashd rejects non-zero overflow values, but accepts overflows where
216 // all the overflowing bits are zero. It also allows underflows.
217 let (mantissa, exponent) = match (mantissa, exponent) {
218 // Overflow: check for non-zero overflow bits
219 //
220 // If m is non-zero, overflow. If m is zero, invalid.
221 (_, e) if (e >= 32) => return None,
222 // If m is larger than the remaining bytes, overflow.
223 // Otherwise, avoid overflows in base^exponent.
224 (m, e) if (e == 31 && m > u8::MAX.into()) => return None,
225 (m, e) if (e == 31 && m <= u8::MAX.into()) => (m << 16, e - 2),
226 (m, e) if (e == 30 && m > u16::MAX.into()) => return None,
227 (m, e) if (e == 30 && m <= u16::MAX.into()) => (m << 8, e - 1),
228
229 // Underflow: perform the right shift.
230 // The abs is safe, because we've just divided by 2^24, and offset
231 // is small.
232 (m, e) if (e < 0) => (m >> ((e.abs() * 8) as u32), 0),
233 (m, e) => (m, e),
234 };
235
236 // Now calculate the result: mantissa*base^exponent
237 // Earlier code should make sure all these values are in range.
238 let mantissa: U256 = mantissa.into();
239 let base: U256 = BASE.into();
240 let exponent: U256 = exponent.into();
241 let result = mantissa * base.pow(exponent);
242
243 if result == U256::zero() {
244 // zcashd rejects zero values, without comparing the hash
245 None
246 } else {
247 Some(result.into())
248 }
249 }
250
251 /// Calculate the Work for a compact representation.
252 ///
253 /// See `Definition of Work` in the [Zcash Specification], and
254 /// `GetBlockProof()` in zcashd.
255 ///
256 /// Returns None if the corresponding ExpandedDifficulty is None.
257 /// Also returns None on Work overflow, which should be impossible on a
258 /// valid chain.
259 ///
260 /// [Zcash Specification]: https://zips.z.cash/protocol/protocol.pdf#workdef
261 pub fn to_work(self) -> Option<Work> {
262 let expanded = self.to_expanded()?;
263 Work::try_from(expanded).ok()
264 }
265
266 /// Return the difficulty bytes in big-endian byte-order.
267 ///
268 /// Zebra displays difficulties in big-endian byte-order,
269 /// following the u256 convention set by Bitcoin and zcashd.
270 pub fn bytes_in_display_order(&self) -> [u8; 4] {
271 self.0.to_be_bytes()
272 }
273
274 /// Convert bytes in big-endian byte-order into a [`CompactDifficulty`].
275 ///
276 /// Zebra displays difficulties in big-endian byte-order,
277 /// following the u256 convention set by Bitcoin and zcashd.
278 ///
279 /// Returns an error if the difficulty value is invalid.
280 pub fn from_bytes_in_display_order(
281 bytes_in_display_order: &[u8; 4],
282 ) -> Result<CompactDifficulty, BoxError> {
283 let internal_byte_order = u32::from_be_bytes(*bytes_in_display_order);
284
285 let difficulty = CompactDifficulty(internal_byte_order);
286
287 if difficulty.to_expanded().is_none() {
288 return Err("invalid difficulty value".into());
289 }
290
291 Ok(difficulty)
292 }
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 }
317}
318
319impl fmt::Debug for CompactDifficulty {
320 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
321 // There isn't a standard way to show different representations of the
322 // same value
323 f.debug_tuple("CompactDifficulty")
324 // Use hex, because it's a float
325 .field(&format_args!("{:#010x}", self.0))
326 // Use expanded difficulty, for bitwise difficulty comparisons
327 .field(&format_args!("{:?}", self.to_expanded()))
328 .finish()
329 }
330}
331
332impl fmt::Display for CompactDifficulty {
333 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
334 f.write_str(&self.encode_hex::<String>())
335 }
336}
337
338impl ToHex for &CompactDifficulty {
339 fn encode_hex<T: FromIterator<char>>(&self) -> T {
340 self.bytes_in_display_order().encode_hex()
341 }
342
343 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
344 self.bytes_in_display_order().encode_hex_upper()
345 }
346}
347
348impl ToHex for CompactDifficulty {
349 fn encode_hex<T: FromIterator<char>>(&self) -> T {
350 (&self).encode_hex()
351 }
352
353 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
354 (&self).encode_hex_upper()
355 }
356}
357
358impl FromHex for CompactDifficulty {
359 type Error = BoxError;
360
361 fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
362 let bytes_in_display_order = <[u8; 4]>::from_hex(hex)?;
363
364 CompactDifficulty::from_bytes_in_display_order(&bytes_in_display_order)
365 }
366}
367
368impl TryFrom<ExpandedDifficulty> for Work {
369 type Error = ();
370
371 fn try_from(expanded: ExpandedDifficulty) -> Result<Self, Self::Error> {
372 // Consensus:
373 //
374 // <https://zips.z.cash/protocol/protocol.pdf#workdef>
375 //
376 // We need to compute `2^256 / (expanded + 1)`, but we can't represent
377 // 2^256, as it's too large for a u256. However, as 2^256 is at least as
378 // large as `expanded + 1`, it is equal to
379 // `((2^256 - expanded - 1) / (expanded + 1)) + 1`, or
380 let result = (!expanded.0 / (expanded.0 + 1)) + 1;
381 if result <= u128::MAX.into() {
382 Ok(Work(result.as_u128()))
383 } else {
384 Err(())
385 }
386 }
387}
388
389impl From<ExpandedDifficulty> for CompactDifficulty {
390 fn from(value: ExpandedDifficulty) -> Self {
391 value.to_compact()
392 }
393}
394
395impl BytesInDisplayOrder for ExpandedDifficulty {
396 fn bytes_in_serialized_order(&self) -> [u8; 32] {
397 self.0.to_big_endian()
398 }
399
400 fn from_bytes_in_serialized_order(bytes: [u8; 32]) -> Self {
401 ExpandedDifficulty(U256::from_big_endian(&bytes))
402 }
403}
404
405impl ExpandedDifficulty {
406 /// Returns the difficulty of the hash.
407 ///
408 /// Used to implement comparisons between difficulties and hashes.
409 ///
410 /// Usage:
411 ///
412 /// Compare the hash with the calculated difficulty value, using Rust's
413 /// standard comparison operators.
414 ///
415 /// Hashes are not used to calculate the difficulties of future blocks, so
416 /// users of this module should avoid converting hashes into difficulties.
417 pub(super) fn from_hash(hash: &block::Hash) -> ExpandedDifficulty {
418 U256::from_little_endian(&hash.0).into()
419 }
420
421 /// Calculate the CompactDifficulty for an expanded difficulty.
422 ///
423 /// # Consensus
424 ///
425 /// See `ToCompact()` in the Zcash Specification, and `GetCompact()`
426 /// in zcashd:
427 /// <https://zips.z.cash/protocol/protocol.pdf#nbits>
428 ///
429 /// # Panics
430 ///
431 /// If `self` is zero.
432 ///
433 /// `ExpandedDifficulty` values are generated in two ways:
434 /// * conversion from `CompactDifficulty` values, which rejects zeroes, and
435 /// * difficulty adjustment calculations, which impose a non-zero minimum
436 /// `target_difficulty_limit`.
437 ///
438 /// Neither of these methods yield zero values.
439 pub fn to_compact(self) -> CompactDifficulty {
440 // The zcashd implementation supports negative and zero compact values.
441 // These values are rejected by the protocol rules. Zebra is designed so
442 // that invalid states are not representable. Therefore, this function
443 // does not produce negative compact values, and panics on zero compact
444 // values. (The negative compact value code in zcashd is unused.)
445 assert!(self.0 > 0.into(), "Zero difficulty values are invalid");
446
447 // The constants for this floating-point representation.
448 // Alias the constants here, so the code is easier to read.
449 const UNSIGNED_MANTISSA_MASK: u32 = CompactDifficulty::UNSIGNED_MANTISSA_MASK;
450 const OFFSET: i32 = CompactDifficulty::OFFSET;
451
452 // Calculate the final size, accounting for the sign bit.
453 // This is the size *after* applying the sign bit adjustment in `ToCompact()`.
454 let size = self.0.bits() / 8 + 1;
455
456 // Make sure the mantissa is non-negative, by shifting down values that
457 // would otherwise overflow into the sign bit
458 let mantissa = if self.0 <= UNSIGNED_MANTISSA_MASK.into() {
459 // Value is small, shift up if needed
460 self.0 << (8 * (3 - size))
461 } else {
462 // Value is large, shift down
463 self.0 >> (8 * (size - 3))
464 };
465
466 // This assertion also makes sure that size fits in its 8 bit compact field
467 assert!(
468 size < (31 + OFFSET) as _,
469 "256^size (256^{size}) must fit in a u256, after the sign bit adjustment and offset"
470 );
471 let size = u32::try_from(size).expect("a 0-6 bit value fits in a u32");
472
473 assert!(
474 mantissa <= UNSIGNED_MANTISSA_MASK.into(),
475 "mantissa {mantissa:x?} must fit in its compact field"
476 );
477 let mantissa = u32::try_from(mantissa).expect("a 0-23 bit value fits in a u32");
478
479 if mantissa > 0 {
480 CompactDifficulty(mantissa + (size << 24))
481 } else {
482 // This check catches invalid mantissas. Overflows and underflows
483 // should also be unreachable, but they aren't caught here.
484 unreachable!("converted CompactDifficulty values must be valid")
485 }
486 }
487}
488
489impl fmt::Display for ExpandedDifficulty {
490 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
491 f.write_str(&self.encode_hex::<String>())
492 }
493}
494
495impl fmt::Debug for ExpandedDifficulty {
496 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
497 f.debug_tuple("ExpandedDifficulty")
498 .field(&self.encode_hex::<String>())
499 .finish()
500 }
501}
502
503impl ToHex for &ExpandedDifficulty {
504 fn encode_hex<T: FromIterator<char>>(&self) -> T {
505 self.bytes_in_display_order().encode_hex()
506 }
507
508 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
509 self.bytes_in_display_order().encode_hex_upper()
510 }
511}
512
513impl ToHex for ExpandedDifficulty {
514 fn encode_hex<T: FromIterator<char>>(&self) -> T {
515 (&self).encode_hex()
516 }
517
518 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
519 (&self).encode_hex_upper()
520 }
521}
522
523impl FromHex for ExpandedDifficulty {
524 type Error = <[u8; 32] as FromHex>::Error;
525
526 fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
527 let bytes_in_display_order = <[u8; 32]>::from_hex(hex)?;
528
529 Ok(ExpandedDifficulty::from_bytes_in_display_order(
530 &bytes_in_display_order,
531 ))
532 }
533}
534
535impl From<U256> for ExpandedDifficulty {
536 fn from(value: U256) -> Self {
537 ExpandedDifficulty(value)
538 }
539}
540
541impl From<ExpandedDifficulty> for U256 {
542 fn from(value: ExpandedDifficulty) -> Self {
543 value.0
544 }
545}
546
547impl Sum<ExpandedDifficulty> for ExpandedDifficulty {
548 fn sum<I: Iterator<Item = ExpandedDifficulty>>(iter: I) -> Self {
549 iter.map(|d| d.0).fold(U256::zero(), Add::add).into()
550 }
551}
552
553impl<T> Div<T> for ExpandedDifficulty
554where
555 T: Into<U256>,
556{
557 type Output = ExpandedDifficulty;
558
559 fn div(self, rhs: T) -> Self::Output {
560 ExpandedDifficulty(self.0 / rhs)
561 }
562}
563
564impl<T> Mul<T> for ExpandedDifficulty
565where
566 U256: Mul<T>,
567 <U256 as Mul<T>>::Output: Into<U256>,
568{
569 type Output = ExpandedDifficulty;
570
571 fn mul(self, rhs: T) -> ExpandedDifficulty {
572 ExpandedDifficulty((self.0 * rhs).into())
573 }
574}
575
576impl PartialEq<block::Hash> for ExpandedDifficulty {
577 /// Is `self` equal to `other`?
578 ///
579 /// See `partial_cmp` for details.
580 fn eq(&self, other: &block::Hash) -> bool {
581 self.partial_cmp(other) == Some(Ordering::Equal)
582 }
583}
584
585impl PartialOrd<block::Hash> for ExpandedDifficulty {
586 /// # Consensus
587 ///
588 /// `block::Hash`es are compared with `ExpandedDifficulty` thresholds by
589 /// converting the hash to a 256-bit integer in little-endian order.
590 ///
591 /// Greater values represent *less* work. This matches the convention in
592 /// zcashd and bitcoin.
593 ///
594 /// <https://zips.z.cash/protocol/protocol.pdf#workdef>
595 fn partial_cmp(&self, other: &block::Hash) -> Option<Ordering> {
596 self.partial_cmp(&ExpandedDifficulty::from_hash(other))
597 }
598}
599
600impl PartialEq<ExpandedDifficulty> for block::Hash {
601 /// Is `self` equal to `other`?
602 ///
603 /// See `<ExpandedDifficulty as PartialOrd<block::Hash>::partial_cmp`
604 /// for details.
605 fn eq(&self, other: &ExpandedDifficulty) -> bool {
606 other.eq(self)
607 }
608}
609
610impl PartialOrd<ExpandedDifficulty> for block::Hash {
611 /// How does `self` compare to `other`?
612 ///
613 /// # Consensus
614 ///
615 /// See `<ExpandedDifficulty as PartialOrd<block::Hash>::partial_cmp`
616 /// for details.
617 #[allow(clippy::unwrap_in_result)]
618 fn partial_cmp(&self, other: &ExpandedDifficulty) -> Option<Ordering> {
619 Some(
620 // Use the canonical implementation, but reverse the order
621 other
622 .partial_cmp(self)
623 .expect("difficulties and hashes have a total order")
624 .reverse(),
625 )
626 }
627}
628
629impl std::ops::Add for Work {
630 type Output = PartialCumulativeWork;
631
632 fn add(self, rhs: Work) -> PartialCumulativeWork {
633 PartialCumulativeWork::from(self) + rhs
634 }
635}
636
637/// Partial work used to track relative work in non-finalized chains
638///
639/// # Consensus
640///
641/// Use to choose the best chain with the most work.
642///
643/// Since it is only relative values that matter, Zebra uses the partial work from a shared
644/// fork root block to find the best chain.
645///
646/// See [`Work`] for details.
647#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
648pub struct PartialCumulativeWork(u128);
649
650impl PartialCumulativeWork {
651 /// Returns a value representing no work.
652 pub fn zero() -> Self {
653 Self(0)
654 }
655
656 /// Return the inner `u128` value.
657 pub fn as_u128(self) -> u128 {
658 self.0
659 }
660
661 /// Returns a floating-point work multiplier that can be used for display.
662 /// The returned value is the work as a multiple of the target difficulty limit for `network`.
663 pub fn difficulty_multiplier_for_display(&self, network: Network) -> f64 {
664 // This calculation is similar to the `getdifficulty` RPC, see that code for details.
665
666 let pow_limit = network
667 .target_difficulty_limit()
668 .to_compact()
669 .to_work()
670 .expect("target difficult limit is valid work");
671
672 // Convert to u128 then f64.
673 let pow_limit = pow_limit.as_u128() as f64;
674 let work = self.as_u128() as f64;
675
676 work / pow_limit
677 }
678
679 /// Returns floating-point work bits that can be used for display.
680 /// The returned value is the number of hash bits represented by the work.
681 pub fn difficulty_bits_for_display(&self) -> f64 {
682 // This calculation is similar to `zcashd`'s bits display in its logs.
683
684 // Convert to u128 then f64.
685 let work = self.as_u128() as f64;
686
687 work.log2()
688 }
689}
690
691/// Network methods related to Difficulty
692pub trait ParameterDifficulty {
693 /// Returns the easiest target difficulty allowed on `network`.
694 ///
695 /// # Consensus
696 ///
697 /// See `PoWLimit` in the Zcash specification:
698 /// <https://zips.z.cash/protocol/protocol.pdf#constants>
699 fn target_difficulty_limit(&self) -> ExpandedDifficulty;
700}
701
702impl ParameterDifficulty for Network {
703 /// Returns the easiest target difficulty allowed on `network`.
704 /// See [`ParameterDifficulty::target_difficulty_limit`]
705 fn target_difficulty_limit(&self) -> ExpandedDifficulty {
706 let limit: U256 = match self {
707 // Mainnet PoWLimit is defined as `2^243 - 1` on page 73 of the protocol specification:
708 // <https://zips.z.cash/protocol/protocol.pdf>
709 Network::Mainnet => (U256::one() << 243) - 1,
710 // 2^251 - 1 for the default testnet, see `testnet::ParametersBuilder::default`()
711 Network::Testnet(params) => return params.target_difficulty_limit(),
712 };
713
714 // `zcashd` converts the PoWLimit into a compact representation before
715 // using it to perform difficulty filter checks.
716 //
717 // The Zcash specification converts to compact for the default difficulty
718 // filter, but not for testnet minimum difficulty blocks. (ZIP 205 and
719 // ZIP 208 don't specify this conversion either.) See #1277 for details.
720 ExpandedDifficulty(limit)
721 .to_compact()
722 .to_expanded()
723 .expect("difficulty limits are valid expanded values")
724 }
725}
726
727impl From<Work> for PartialCumulativeWork {
728 fn from(work: Work) -> Self {
729 PartialCumulativeWork(work.0)
730 }
731}
732
733impl std::ops::Add<Work> for PartialCumulativeWork {
734 type Output = PartialCumulativeWork;
735
736 fn add(self, rhs: Work) -> Self::Output {
737 let result = self
738 .0
739 .checked_add(rhs.0)
740 .expect("Work values do not overflow");
741
742 PartialCumulativeWork(result)
743 }
744}
745
746impl std::ops::AddAssign<Work> for PartialCumulativeWork {
747 fn add_assign(&mut self, rhs: Work) {
748 *self = *self + rhs;
749 }
750}
751
752impl std::ops::Sub<Work> for PartialCumulativeWork {
753 type Output = PartialCumulativeWork;
754
755 fn sub(self, rhs: Work) -> Self::Output {
756 let result = self.0
757 .checked_sub(rhs.0)
758 .expect("PartialCumulativeWork values do not underflow: all subtracted Work values must have been previously added to the PartialCumulativeWork");
759
760 PartialCumulativeWork(result)
761 }
762}
763
764impl std::ops::SubAssign<Work> for PartialCumulativeWork {
765 fn sub_assign(&mut self, rhs: Work) {
766 *self = *self - rhs;
767 }
768}