chain: move BlockHeight into block

This commit is contained in:
Henry de Valence 2020-08-14 23:51:41 -07:00
parent 5f71bcd0d1
commit dad6340cd3
25 changed files with 88 additions and 84 deletions

View File

@ -4,6 +4,7 @@
mod difficulty; mod difficulty;
mod hash; mod hash;
mod header; mod header;
mod height;
mod light_client; mod light_client;
mod serialize; mod serialize;
@ -17,11 +18,11 @@ use std::{error, sync::Arc};
use proptest_derive::Arbitrary; use proptest_derive::Arbitrary;
use crate::transaction::Transaction; use crate::transaction::Transaction;
use crate::types::BlockHeight;
use crate::Network; use crate::Network;
pub use hash::BlockHeaderHash; pub use hash::BlockHeaderHash;
pub use header::BlockHeader; pub use header::BlockHeader;
pub use height::BlockHeight;
pub use light_client::LightClientRootHash; pub use light_client::LightClientRootHash;
/// A block in your blockchain. /// A block in your blockchain.

View File

@ -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<Self, Self::Err> {
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<Self>;
}

View File

@ -1,9 +1,10 @@
//! The LightClientRootHash enum, used for the corresponding block header field. //! The LightClientRootHash enum, used for the corresponding block header field.
use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash; use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash;
use crate::types::BlockHeight;
use crate::{Network, NetworkUpgrade, NetworkUpgrade::*}; use crate::{Network, NetworkUpgrade, NetworkUpgrade::*};
use super::BlockHeight;
/// Light client root hashes. /// Light client root hashes.
/// ///
/// The `BlockHeader.light_client_root_bytes` field is interpreted differently, /// The `BlockHeader.light_client_root_bytes` field is interpreted differently,

View File

@ -2,7 +2,7 @@
use NetworkUpgrade::*; use NetworkUpgrade::*;
use crate::types::BlockHeight; use crate::block::BlockHeight;
use crate::{Network, Network::*}; use crate::{Network, Network::*};
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};

View File

@ -3,7 +3,7 @@
use super::*; use super::*;
use NetworkUpgrade::*; use NetworkUpgrade::*;
use crate::types::BlockHeight; use crate::block::BlockHeight;
use crate::{Network, Network::*}; use crate::{Network, Network::*};
use std::collections::HashSet; use std::collections::HashSet;

View File

@ -19,8 +19,8 @@ pub use shielded_data::{Output, ShieldedData, Spend};
pub use transparent::{CoinbaseData, OutPoint, TransparentInput, TransparentOutput}; pub use transparent::{CoinbaseData, OutPoint, TransparentInput, TransparentOutput};
use crate::amount::Amount; use crate::amount::Amount;
use crate::block::BlockHeight;
use crate::proofs::{Bctv14Proof, Groth16Proof}; use crate::proofs::{Bctv14Proof, Groth16Proof};
use crate::types::BlockHeight;
/// A Zcash transaction. /// A Zcash transaction.
/// ///

View File

@ -3,8 +3,8 @@ use std::io;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{DateTime, TimeZone, Utc}; use chrono::{DateTime, TimeZone, Utc};
use crate::block::BlockHeight;
use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
use crate::types::BlockHeight;
/// A Bitcoin-style `locktime`, representing either a block height or an epoch /// A Bitcoin-style `locktime`, representing either a block height or an epoch
/// time. /// time.

View File

@ -4,6 +4,7 @@ use proptest::{arbitrary::any, array, collection::vec, option, prelude::*};
use crate::{ use crate::{
amount::{Amount, NonNegative}, amount::{Amount, NonNegative},
block::BlockHeight,
commitments, keys, commitments, keys,
notes::{sapling, sprout}, notes::{sapling, sprout},
proofs::{Bctv14Proof, Groth16Proof, ZkSnarkProof}, proofs::{Bctv14Proof, Groth16Proof, ZkSnarkProof},
@ -12,7 +13,7 @@ use crate::{
Transaction, TransparentInput, TransparentOutput, Transaction, TransparentInput, TransparentOutput,
}, },
treestate::{self, note_commitment_tree::SaplingNoteTreeRootHash}, treestate::{self, note_commitment_tree::SaplingNoteTreeRootHash},
types::{BlockHeight, Script}, types::Script,
}; };
impl Transaction { impl Transaction {

View File

@ -6,7 +6,8 @@ use proptest_derive::Arbitrary;
use crate::{ use crate::{
amount::{Amount, NonNegative}, amount::{Amount, NonNegative},
types::{BlockHeight, Script}, block::BlockHeight,
types::Script,
}; };
use super::TransactionHash; use super::TransactionHash;

View File

@ -8,64 +8,6 @@ use std::{
io::{self, Read}, 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<Self, Self::Err> {
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<Self>;
}
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 ... /// A sequence of message authentication tags ...
/// ///
/// binding h_sig to each a_sk of the JoinSplit description, computed as /// 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)] #[cfg(test)]
mod proptests { mod proptests {
use std::io::Cursor; use std::io::Cursor;

View File

@ -25,8 +25,8 @@ use std::{
use tokio::time; use tokio::time;
use tower::{buffer::Buffer, Service, ServiceExt}; use tower::{buffer::Buffer, Service, ServiceExt};
use zebra_chain::block::BlockHeight;
use zebra_chain::block::{Block, BlockHeaderHash}; use zebra_chain::block::{Block, BlockHeaderHash};
use zebra_chain::types::BlockHeight;
/// A service that verifies blocks. /// A service that verifies blocks.
#[derive(Debug)] #[derive(Debug)]

View File

@ -27,8 +27,8 @@ use std::{
use tower::{buffer::Buffer, Service, ServiceExt}; use tower::{buffer::Buffer, Service, ServiceExt};
use tracing_futures::Instrument; use tracing_futures::Instrument;
use zebra_chain::block::BlockHeight;
use zebra_chain::block::{Block, BlockHeaderHash}; use zebra_chain::block::{Block, BlockHeaderHash};
use zebra_chain::types::BlockHeight;
use zebra_chain::{Network, NetworkUpgrade::Sapling}; use zebra_chain::{Network, NetworkUpgrade::Sapling};
/// The maximum expected gap between blocks. /// The maximum expected gap between blocks.

View File

@ -38,8 +38,8 @@ use std::{
use tokio::sync::oneshot; use tokio::sync::oneshot;
use tower::Service; use tower::Service;
use zebra_chain::block::BlockHeight;
use zebra_chain::block::{Block, BlockHeaderHash}; use zebra_chain::block::{Block, BlockHeaderHash};
use zebra_chain::types::BlockHeight;
use zebra_chain::Network; use zebra_chain::Network;
/// The inner error type for CheckpointVerifier. /// The inner error type for CheckpointVerifier.

View File

@ -18,7 +18,7 @@ use std::{
}; };
use zebra_chain::block::BlockHeaderHash; use zebra_chain::block::BlockHeaderHash;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use zebra_chain::Network::{self, *}; use zebra_chain::Network::{self, *};
const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt"); const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt");

View File

@ -2,7 +2,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use Progress::*; use Progress::*;
use Target::*; use Target::*;

View File

@ -1,6 +1,6 @@
//! The minimum difficulty block rule for Zcash. //! The minimum difficulty block rule for Zcash.
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use zebra_chain::{Network, Network::*}; use zebra_chain::{Network, Network::*};
/// The testnet block height when minimum difficulty blocks start being /// The testnet block height when minimum difficulty blocks start being

View File

@ -2,7 +2,7 @@
use super::*; use super::*;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use zebra_chain::{Network, Network::*}; use zebra_chain::{Network, Network::*};
#[test] #[test]

View File

@ -18,7 +18,7 @@ use tower::Service;
use tracing::{span, Level}; use tracing::{span, Level};
use tracing_futures::Instrument; use tracing_futures::Instrument;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use crate::{ use crate::{
constants, constants,

View File

@ -9,13 +9,13 @@ use chrono::{TimeZone, Utc};
use tokio_util::codec::{Decoder, Encoder}; use tokio_util::codec::{Decoder, Encoder};
use zebra_chain::{ use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash}, block::{Block, BlockHeaderHash},
serialization::{ serialization::{
sha256d, ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize, sha256d, ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize,
ZcashSerialize, ZcashSerialize,
}, },
transaction::Transaction, transaction::Transaction,
types::BlockHeight,
Network, Network,
}; };

View File

@ -6,7 +6,7 @@ use std::{net, sync::Arc};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use zebra_chain::block::{Block, BlockHeader, BlockHeaderHash}; 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::inv::InventoryHash;
use super::types::*; use super::types::*;

View File

@ -4,7 +4,7 @@ use crate::constants::magics;
use std::fmt; use std::fmt;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
use zebra_chain::Network::{self, *}; use zebra_chain::Network::{self, *};
use zebra_chain::NetworkUpgrade::{self, *}; use zebra_chain::NetworkUpgrade::{self, *};

View File

@ -4,8 +4,8 @@ use std::{
sync::Arc, sync::Arc,
}; };
use zebra_chain::{ use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash}, block::{Block, BlockHeaderHash},
types::BlockHeight,
}; };
#[derive(Default)] #[derive(Default)]
pub(super) struct BlockIndex { pub(super) struct BlockIndex {

View File

@ -22,8 +22,8 @@ use std::{error, iter, sync::Arc};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use zebra_chain::{ use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash}, block::{Block, BlockHeaderHash},
types::BlockHeight,
Network, Network,
Network::*, Network::*,
}; };

View File

@ -13,8 +13,8 @@ use tower::{buffer::Buffer, util::BoxService, Service};
use tracing::instrument; use tracing::instrument;
use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
use zebra_chain::{ use zebra_chain::{
block::BlockHeight,
block::{Block, BlockHeaderHash}, block::{Block, BlockHeaderHash},
types::BlockHeight,
Network, Network,
}; };

View File

@ -18,7 +18,7 @@ use structopt::StructOpt;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use zebra_chain::block::BlockHeaderHash; use zebra_chain::block::BlockHeaderHash;
use zebra_chain::types::BlockHeight; use zebra_chain::block::BlockHeight;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::process::ExitStatusExt; use std::os::unix::process::ExitStatusExt;