From 1bc833dbccfefb5b544fe2d1c002c1c0603f28c9 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Wed, 11 Nov 2020 14:23:23 -0800 Subject: [PATCH] Move work conversion helpers to test code --- Cargo.lock | 1 + zebra-chain/src/work/difficulty.rs | 27 --------------------------- zebra-state/Cargo.toml | 1 + zebra-state/src/tests.rs | 10 ++++++++-- 4 files changed, 10 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 741d50f2..e5c9d970 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3478,6 +3478,7 @@ dependencies = [ "lazy_static", "metrics", "once_cell", + "primitive-types", "proptest", "serde", "sled", diff --git a/zebra-chain/src/work/difficulty.rs b/zebra-chain/src/work/difficulty.rs index 919fb429..085aeffe 100644 --- a/zebra-chain/src/work/difficulty.rs +++ b/zebra-chain/src/work/difficulty.rs @@ -260,39 +260,12 @@ impl TryFrom for Work { } } -impl TryFrom for Work { - type Error = (); - - fn try_from(value: u128) -> Result { - if value == 0 { - Err(()) - } else { - Ok(Work(value)) - } - } -} - impl From for CompactDifficulty { fn from(value: ExpandedDifficulty) -> Self { value.to_compact() } } -impl From for CompactDifficulty { - fn from(value: Work) -> Self { - let expanded = ExpandedDifficulty::from(value); - Self::from(expanded) - } -} - -impl From for ExpandedDifficulty { - fn from(value: Work) -> Self { - let work: U256 = value.0.into(); - let expanded = (!work + 1) / work; - ExpandedDifficulty(expanded) - } -} - impl ExpandedDifficulty { /// Returns the difficulty of the hash. /// diff --git a/zebra-state/Cargo.toml b/zebra-state/Cargo.toml index a2134cd2..5bd87e93 100644 --- a/zebra-state/Cargo.toml +++ b/zebra-state/Cargo.toml @@ -35,3 +35,4 @@ spandoc = "0.2" tempdir = "0.3.7" tokio = { version = "0.2.22", features = ["full"] } proptest = "0.10.1" +primitive-types = "0.7.2" diff --git a/zebra-state/src/tests.rs b/zebra-state/src/tests.rs index c30b264c..772fc25d 100644 --- a/zebra-state/src/tests.rs +++ b/zebra-state/src/tests.rs @@ -4,6 +4,7 @@ use zebra_chain::{ block::{self, Block}, transaction::Transaction, transparent, + work::difficulty::ExpandedDifficulty, work::difficulty::Work, }; @@ -42,9 +43,14 @@ impl FakeChainHelper for Arc { } fn set_work(mut self, work: u128) -> Arc { - let work = Work::try_from(work).expect("tests should only pass in valid work values"); + use primitive_types::U256; + + let work: U256 = work.into(); + let expanded = (!work + 1) / work; + let expanded = ExpandedDifficulty::from(expanded); + let block = Arc::make_mut(&mut self); - block.header.difficulty_threshold = work.into(); + block.header.difficulty_threshold = expanded.into(); self } }