From dad6340cd350f973a872f0d87abf52bd762afd48 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 14 Aug 2020 23:51:41 -0700 Subject: [PATCH] chain: move BlockHeight into block --- zebra-chain/src/block.rs | 3 +- zebra-chain/src/block/height.rs | 61 +++++++++++++++++++ zebra-chain/src/block/light_client.rs | 3 +- zebra-chain/src/parameters/network_upgrade.rs | 2 +- zebra-chain/src/parameters/tests.rs | 2 +- zebra-chain/src/transaction.rs | 2 +- zebra-chain/src/transaction/lock_time.rs | 2 +- .../src/transaction/tests/arbitrary.rs | 3 +- zebra-chain/src/transaction/transparent.rs | 3 +- zebra-chain/src/types.rs | 61 ------------------- zebra-consensus/src/block.rs | 2 +- zebra-consensus/src/chain.rs | 2 +- zebra-consensus/src/checkpoint.rs | 2 +- zebra-consensus/src/checkpoint/list.rs | 2 +- zebra-consensus/src/checkpoint/types.rs | 2 +- .../src/parameters/minimum_difficulty.rs | 2 +- zebra-consensus/src/parameters/tests.rs | 2 +- zebra-network/src/peer/handshake.rs | 2 +- zebra-network/src/protocol/external/codec.rs | 2 +- .../src/protocol/external/message.rs | 2 +- zebra-network/src/protocol/external/types.rs | 2 +- zebra-state/src/in_memory/block_index.rs | 2 +- zebra-state/src/lib.rs | 2 +- zebra-state/src/on_disk.rs | 2 +- zebra-utils/src/bin/zebra-checkpoints/main.rs | 2 +- 25 files changed, 88 insertions(+), 84 deletions(-) create mode 100644 zebra-chain/src/block/height.rs diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index dcef0d154..de0de8eb5 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -4,6 +4,7 @@ mod difficulty; mod hash; mod header; +mod height; mod light_client; mod serialize; @@ -17,11 +18,11 @@ use std::{error, sync::Arc}; use proptest_derive::Arbitrary; use crate::transaction::Transaction; -use crate::types::BlockHeight; use crate::Network; pub use hash::BlockHeaderHash; pub use header::BlockHeader; +pub use height::BlockHeight; pub use light_client::LightClientRootHash; /// A block in your blockchain. diff --git a/zebra-chain/src/block/height.rs b/zebra-chain/src/block/height.rs new file mode 100644 index 000000000..9358dc411 --- /dev/null +++ b/zebra-chain/src/block/height.rs @@ -0,0 +1,61 @@ +use crate::serialization::SerializationError; + +/// A u32 which represents a block height value. +/// +/// # Invariants +/// +/// Users should not construct block heights greater than `BlockHeight::MAX`. +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct BlockHeight(pub u32); + +impl std::str::FromStr for BlockHeight { + type Err = SerializationError; + fn from_str(s: &str) -> Result { + match s.parse() { + Ok(h) if (BlockHeight(h) <= BlockHeight::MAX) => Ok(BlockHeight(h)), + Ok(_) => Err(SerializationError::Parse( + "BlockHeight exceeds maximum height", + )), + Err(_) => Err(SerializationError::Parse( + "BlockHeight(u32) integer parse error", + )), + } + } +} + +impl BlockHeight { + /// The minimum BlockHeight. + /// + /// Due to the underlying type, it is impossible to construct block heights + /// less than `BlockHeight::MIN`. + /// + /// Style note: Sometimes, `BlockHeight::MIN` is less readable than + /// `BlockHeight(0)`. Use whichever makes sense in context. + pub const MIN: BlockHeight = BlockHeight(0); + + /// The maximum BlockHeight. + /// + /// Users should not construct block heights greater than `BlockHeight::MAX`. + pub const MAX: BlockHeight = BlockHeight(499_999_999); + + /// The maximum BlockHeight as a u32, for range patterns. + /// + /// `BlockHeight::MAX.0` can't be used in match range patterns, use this + /// alias instead. + pub const MAX_AS_U32: u32 = Self::MAX.0; +} + +#[cfg(test)] +use proptest::prelude::*; +#[cfg(test)] +impl Arbitrary for BlockHeight { + type Parameters = (); + + fn arbitrary_with(_args: ()) -> Self::Strategy { + (BlockHeight::MIN.0..=BlockHeight::MAX.0) + .prop_map(BlockHeight) + .boxed() + } + + type Strategy = BoxedStrategy; +} diff --git a/zebra-chain/src/block/light_client.rs b/zebra-chain/src/block/light_client.rs index 9b423abfe..3f0ad1823 100644 --- a/zebra-chain/src/block/light_client.rs +++ b/zebra-chain/src/block/light_client.rs @@ -1,9 +1,10 @@ //! The LightClientRootHash enum, used for the corresponding block header field. use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash; -use crate::types::BlockHeight; use crate::{Network, NetworkUpgrade, NetworkUpgrade::*}; +use super::BlockHeight; + /// Light client root hashes. /// /// The `BlockHeader.light_client_root_bytes` field is interpreted differently, diff --git a/zebra-chain/src/parameters/network_upgrade.rs b/zebra-chain/src/parameters/network_upgrade.rs index ed04b487b..828bfcd57 100644 --- a/zebra-chain/src/parameters/network_upgrade.rs +++ b/zebra-chain/src/parameters/network_upgrade.rs @@ -2,7 +2,7 @@ use NetworkUpgrade::*; -use crate::types::BlockHeight; +use crate::block::BlockHeight; use crate::{Network, Network::*}; use std::collections::{BTreeMap, HashMap}; diff --git a/zebra-chain/src/parameters/tests.rs b/zebra-chain/src/parameters/tests.rs index ad737b704..02196ffc1 100644 --- a/zebra-chain/src/parameters/tests.rs +++ b/zebra-chain/src/parameters/tests.rs @@ -3,7 +3,7 @@ use super::*; use NetworkUpgrade::*; -use crate::types::BlockHeight; +use crate::block::BlockHeight; use crate::{Network, Network::*}; use std::collections::HashSet; diff --git a/zebra-chain/src/transaction.rs b/zebra-chain/src/transaction.rs index 9d3ce0649..ea400c3f8 100644 --- a/zebra-chain/src/transaction.rs +++ b/zebra-chain/src/transaction.rs @@ -19,8 +19,8 @@ pub use shielded_data::{Output, ShieldedData, Spend}; pub use transparent::{CoinbaseData, OutPoint, TransparentInput, TransparentOutput}; use crate::amount::Amount; +use crate::block::BlockHeight; use crate::proofs::{Bctv14Proof, Groth16Proof}; -use crate::types::BlockHeight; /// A Zcash transaction. /// diff --git a/zebra-chain/src/transaction/lock_time.rs b/zebra-chain/src/transaction/lock_time.rs index 9eff8e42e..99dd86588 100644 --- a/zebra-chain/src/transaction/lock_time.rs +++ b/zebra-chain/src/transaction/lock_time.rs @@ -3,8 +3,8 @@ use std::io; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use chrono::{DateTime, TimeZone, Utc}; +use crate::block::BlockHeight; use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; -use crate::types::BlockHeight; /// A Bitcoin-style `locktime`, representing either a block height or an epoch /// time. diff --git a/zebra-chain/src/transaction/tests/arbitrary.rs b/zebra-chain/src/transaction/tests/arbitrary.rs index 6471ca26c..b6f2f518f 100644 --- a/zebra-chain/src/transaction/tests/arbitrary.rs +++ b/zebra-chain/src/transaction/tests/arbitrary.rs @@ -4,6 +4,7 @@ use proptest::{arbitrary::any, array, collection::vec, option, prelude::*}; use crate::{ amount::{Amount, NonNegative}, + block::BlockHeight, commitments, keys, notes::{sapling, sprout}, proofs::{Bctv14Proof, Groth16Proof, ZkSnarkProof}, @@ -12,7 +13,7 @@ use crate::{ Transaction, TransparentInput, TransparentOutput, }, treestate::{self, note_commitment_tree::SaplingNoteTreeRootHash}, - types::{BlockHeight, Script}, + types::Script, }; impl Transaction { diff --git a/zebra-chain/src/transaction/transparent.rs b/zebra-chain/src/transaction/transparent.rs index ad22aa521..f65fd456f 100644 --- a/zebra-chain/src/transaction/transparent.rs +++ b/zebra-chain/src/transaction/transparent.rs @@ -6,7 +6,8 @@ use proptest_derive::Arbitrary; use crate::{ amount::{Amount, NonNegative}, - types::{BlockHeight, Script}, + block::BlockHeight, + types::Script, }; use super::TransactionHash; diff --git a/zebra-chain/src/types.rs b/zebra-chain/src/types.rs index bd7dc2fe4..ea40182fc 100644 --- a/zebra-chain/src/types.rs +++ b/zebra-chain/src/types.rs @@ -8,64 +8,6 @@ use std::{ io::{self, Read}, }; -/// A u32 which represents a block height value. -/// -/// # Invariants -/// -/// Users should not construct block heights greater than `BlockHeight::MAX`. -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub struct BlockHeight(pub u32); - -impl std::str::FromStr for BlockHeight { - type Err = SerializationError; - fn from_str(s: &str) -> Result { - match s.parse() { - Ok(h) if (BlockHeight(h) <= BlockHeight::MAX) => Ok(BlockHeight(h)), - Ok(_) => Err(SerializationError::Parse( - "BlockHeight exceeds maximum height", - )), - Err(_) => Err(SerializationError::Parse( - "BlockHeight(u32) integer parse error", - )), - } - } -} - -#[cfg(test)] -impl Arbitrary for BlockHeight { - type Parameters = (); - - fn arbitrary_with(_args: ()) -> Self::Strategy { - (BlockHeight::MIN.0..=BlockHeight::MAX.0) - .prop_map(BlockHeight) - .boxed() - } - - type Strategy = BoxedStrategy; -} - -impl BlockHeight { - /// The minimum BlockHeight. - /// - /// Due to the underlying type, it is impossible to construct block heights - /// less than `BlockHeight::MIN`. - /// - /// Style note: Sometimes, `BlockHeight::MIN` is less readable than - /// `BlockHeight(0)`. Use whichever makes sense in context. - pub const MIN: BlockHeight = BlockHeight(0); - - /// The maximum BlockHeight. - /// - /// Users should not construct block heights greater than `BlockHeight::MAX`. - pub const MAX: BlockHeight = BlockHeight(499_999_999); - - /// The maximum BlockHeight as a u32, for range patterns. - /// - /// `BlockHeight::MAX.0` can't be used in match range patterns, use this - /// alias instead. - pub const MAX_AS_U32: u32 = Self::MAX.0; -} - /// A sequence of message authentication tags ... /// /// binding h_sig to each a_sk of the JoinSplit description, computed as @@ -118,9 +60,6 @@ impl ZcashDeserialize for Script { } } -#[cfg(test)] -use proptest::prelude::*; - #[cfg(test)] mod proptests { use std::io::Cursor; diff --git a/zebra-consensus/src/block.rs b/zebra-consensus/src/block.rs index ef18d1b4b..5e90b04e1 100644 --- a/zebra-consensus/src/block.rs +++ b/zebra-consensus/src/block.rs @@ -25,8 +25,8 @@ use std::{ use tokio::time; use tower::{buffer::Buffer, Service, ServiceExt}; +use zebra_chain::block::BlockHeight; use zebra_chain::block::{Block, BlockHeaderHash}; -use zebra_chain::types::BlockHeight; /// A service that verifies blocks. #[derive(Debug)] diff --git a/zebra-consensus/src/chain.rs b/zebra-consensus/src/chain.rs index 0eda619dc..7a594852a 100644 --- a/zebra-consensus/src/chain.rs +++ b/zebra-consensus/src/chain.rs @@ -27,8 +27,8 @@ use std::{ use tower::{buffer::Buffer, Service, ServiceExt}; use tracing_futures::Instrument; +use zebra_chain::block::BlockHeight; use zebra_chain::block::{Block, BlockHeaderHash}; -use zebra_chain::types::BlockHeight; use zebra_chain::{Network, NetworkUpgrade::Sapling}; /// The maximum expected gap between blocks. diff --git a/zebra-consensus/src/checkpoint.rs b/zebra-consensus/src/checkpoint.rs index 2ebf29b87..331a109c2 100644 --- a/zebra-consensus/src/checkpoint.rs +++ b/zebra-consensus/src/checkpoint.rs @@ -38,8 +38,8 @@ use std::{ use tokio::sync::oneshot; use tower::Service; +use zebra_chain::block::BlockHeight; use zebra_chain::block::{Block, BlockHeaderHash}; -use zebra_chain::types::BlockHeight; use zebra_chain::Network; /// The inner error type for CheckpointVerifier. diff --git a/zebra-consensus/src/checkpoint/list.rs b/zebra-consensus/src/checkpoint/list.rs index ba5287dca..8e3e5fced 100644 --- a/zebra-consensus/src/checkpoint/list.rs +++ b/zebra-consensus/src/checkpoint/list.rs @@ -18,7 +18,7 @@ use std::{ }; use zebra_chain::block::BlockHeaderHash; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use zebra_chain::Network::{self, *}; const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt"); diff --git a/zebra-consensus/src/checkpoint/types.rs b/zebra-consensus/src/checkpoint/types.rs index 9ac33a9d1..52b3205e6 100644 --- a/zebra-consensus/src/checkpoint/types.rs +++ b/zebra-consensus/src/checkpoint/types.rs @@ -2,7 +2,7 @@ use std::cmp::Ordering; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use Progress::*; use Target::*; diff --git a/zebra-consensus/src/parameters/minimum_difficulty.rs b/zebra-consensus/src/parameters/minimum_difficulty.rs index b0aa79d01..21edb7682 100644 --- a/zebra-consensus/src/parameters/minimum_difficulty.rs +++ b/zebra-consensus/src/parameters/minimum_difficulty.rs @@ -1,6 +1,6 @@ //! The minimum difficulty block rule for Zcash. -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use zebra_chain::{Network, Network::*}; /// The testnet block height when minimum difficulty blocks start being diff --git a/zebra-consensus/src/parameters/tests.rs b/zebra-consensus/src/parameters/tests.rs index b35e18956..9e9dcc465 100644 --- a/zebra-consensus/src/parameters/tests.rs +++ b/zebra-consensus/src/parameters/tests.rs @@ -2,7 +2,7 @@ use super::*; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use zebra_chain::{Network, Network::*}; #[test] diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index 7dc467983..6890594f3 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -18,7 +18,7 @@ use tower::Service; use tracing::{span, Level}; use tracing_futures::Instrument; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use crate::{ constants, diff --git a/zebra-network/src/protocol/external/codec.rs b/zebra-network/src/protocol/external/codec.rs index d81a0bf29..6e8582b07 100644 --- a/zebra-network/src/protocol/external/codec.rs +++ b/zebra-network/src/protocol/external/codec.rs @@ -9,13 +9,13 @@ use chrono::{TimeZone, Utc}; use tokio_util::codec::{Decoder, Encoder}; use zebra_chain::{ + block::BlockHeight, block::{Block, BlockHeaderHash}, serialization::{ sha256d, ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize, ZcashSerialize, }, transaction::Transaction, - types::BlockHeight, Network, }; diff --git a/zebra-network/src/protocol/external/message.rs b/zebra-network/src/protocol/external/message.rs index 3594703f2..35955df47 100644 --- a/zebra-network/src/protocol/external/message.rs +++ b/zebra-network/src/protocol/external/message.rs @@ -6,7 +6,7 @@ use std::{net, sync::Arc}; use chrono::{DateTime, Utc}; use zebra_chain::block::{Block, BlockHeader, BlockHeaderHash}; -use zebra_chain::{transaction::Transaction, types::BlockHeight}; +use zebra_chain::{block::BlockHeight, transaction::Transaction}; use super::inv::InventoryHash; use super::types::*; diff --git a/zebra-network/src/protocol/external/types.rs b/zebra-network/src/protocol/external/types.rs index 7657658a8..6802bc0dc 100644 --- a/zebra-network/src/protocol/external/types.rs +++ b/zebra-network/src/protocol/external/types.rs @@ -4,7 +4,7 @@ use crate::constants::magics; use std::fmt; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; use zebra_chain::Network::{self, *}; use zebra_chain::NetworkUpgrade::{self, *}; diff --git a/zebra-state/src/in_memory/block_index.rs b/zebra-state/src/in_memory/block_index.rs index 2e8c3bed6..e5069a480 100644 --- a/zebra-state/src/in_memory/block_index.rs +++ b/zebra-state/src/in_memory/block_index.rs @@ -4,8 +4,8 @@ use std::{ sync::Arc, }; use zebra_chain::{ + block::BlockHeight, block::{Block, BlockHeaderHash}, - types::BlockHeight, }; #[derive(Default)] pub(super) struct BlockIndex { diff --git a/zebra-state/src/lib.rs b/zebra-state/src/lib.rs index 105c00d1a..ffeefd058 100644 --- a/zebra-state/src/lib.rs +++ b/zebra-state/src/lib.rs @@ -22,8 +22,8 @@ use std::{error, iter, sync::Arc}; use tower::{Service, ServiceExt}; use zebra_chain::{ + block::BlockHeight, block::{Block, BlockHeaderHash}, - types::BlockHeight, Network, Network::*, }; diff --git a/zebra-state/src/on_disk.rs b/zebra-state/src/on_disk.rs index ca6de360c..dba523962 100644 --- a/zebra-state/src/on_disk.rs +++ b/zebra-state/src/on_disk.rs @@ -13,8 +13,8 @@ use tower::{buffer::Buffer, util::BoxService, Service}; use tracing::instrument; use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; use zebra_chain::{ + block::BlockHeight, block::{Block, BlockHeaderHash}, - types::BlockHeight, Network, }; diff --git a/zebra-utils/src/bin/zebra-checkpoints/main.rs b/zebra-utils/src/bin/zebra-checkpoints/main.rs index 5117f11b5..01126a394 100644 --- a/zebra-utils/src/bin/zebra-checkpoints/main.rs +++ b/zebra-utils/src/bin/zebra-checkpoints/main.rs @@ -18,7 +18,7 @@ use structopt::StructOpt; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use zebra_chain::block::BlockHeaderHash; -use zebra_chain::types::BlockHeight; +use zebra_chain::block::BlockHeight; #[cfg(unix)] use std::os::unix::process::ExitStatusExt;